Skip to content

Commit

Permalink
More verbose; adjust kw-only-ness
Browse files Browse the repository at this point in the history
  • Loading branch information
zmoon committed Mar 22, 2024
1 parent 07351b3 commit 9b1eb73
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions monetio/models/icap_mme.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
)


def build_urls(dates, *, filetype="MMC", data_var="dustaod550", verbose=True):
def build_urls(dates, filetype="MMC", data_var="dustaod550", *, verbose=True):
"""Construct URLs for downloading NEPS data.
Parameters
Expand Down Expand Up @@ -57,15 +57,16 @@ def build_urls(dates, *, filetype="MMC", data_var="dustaod550", verbose=True):
return urls, fnames


def check_remote_file_exists(file_url):
def check_remote_file_exists(file_url, *, verbose=True):
import requests

r = requests.head(file_url)

if r.status_code == 200:
return True
else:
print(f"HTTP Error {r.status_code} - {r.reason}")
if verbose:
print(f"HTTP Error {r.status_code} - {r.reason}")
return False


Expand Down Expand Up @@ -112,23 +113,23 @@ def retrieve(url, fname, *, download=False, verbose=True):
return p


def _check_file_url(url):
def _check_file_url(url, *, verbose=True):
"""
Raises
------
ValueError
If the file URL HEAD request doesn't return 200,
with info about how to check available products / data vars for the date.
"""
if not check_remote_file_exists(url):
if not check_remote_file_exists(url, verbose=verbose):
raise ValueError(
f"File does not exist on ICAP HTTPS server: {url}. "
f"Check {url[:url.index('icap_')]} to see the available "
"`product` and `data_var`s for this month."
)


def open_dataset(date, product="MMC", data_var="dustaod550", download=False, verbose=True):
def open_dataset(date, product="MMC", data_var="dustaod550", *, download=False, verbose=True):
"""
Parameters
----------
Expand Down Expand Up @@ -167,13 +168,13 @@ def open_dataset(date, product="MMC", data_var="dustaod550", download=False, ver
urls, fnames = build_urls(d, filetype=product, data_var=data_var, verbose=verbose)
url = urls.values[0]
fname = fnames.values[0]
_check_file_url(url)
_check_file_url(url, verbose=verbose)
dset = xr.open_dataset(retrieve(url, fname, download=download, verbose=verbose))

return dset


def open_mfdataset(dates, product="MMC", data_var="dustaod550", download=False, verbose=True):
def open_mfdataset(dates, product="MMC", data_var="dustaod550", *, download=False, verbose=True):
"""
Parameters
----------
Expand Down Expand Up @@ -217,13 +218,13 @@ def open_mfdataset(dates, product="MMC", data_var="dustaod550", download=False,
if download is True:
paths = []
for url, fname in zip(urls, fnames):
_check_file_url(url)
_check_file_url(url, verbose=verbose)
paths.append(retrieve(url, fname, download=True, verbose=verbose))
dset = xr.open_mfdataset(paths, combine="nested", concat_dim="time")
else:
dsets = []
for url, fname in zip(urls, fnames):
_check_file_url(url)
_check_file_url(url, verbose=verbose)
o = retrieve(url, fname, download=False, verbose=verbose)
dsets.append(xr.open_dataset(o))
dset = xr.concat(dsets, dim="time")
Expand Down

0 comments on commit 9b1eb73

Please sign in to comment.