Skip to content

Commit

Permalink
Merge pull request #315 from callumrollo/callum-patch-12
Browse files Browse the repository at this point in the history
add request_kwargs to to_xarray
  • Loading branch information
ocefpaf authored Aug 9, 2023
2 parents 346506d + 170e3c2 commit ae358d5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
11 changes: 9 additions & 2 deletions erddapy/erddapy.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,11 @@ def to_ncCF(self, protocol: str = None, **kw):
url = self.get_download_url(response="ncCF", distinct=distinct)
return to_ncCF(url, protocol=protocol, requests_kwargs=dict(**kw))

def to_xarray(self, **kw):
def to_xarray(
self,
requests_kwargs: Optional[Dict] = None,
**kw,
):
"""Load the data request into a xarray.Dataset.
Accepts any `xr.open_dataset` keyword arguments.
Expand All @@ -380,7 +384,10 @@ def to_xarray(self, **kw):
response = "ncCF"
distinct = kw.pop("distinct", False)
url = self.get_download_url(response=response, distinct=distinct)
requests_kwargs = {"auth": self.auth}
if requests_kwargs:
requests_kwargs = {**{"auth": self.auth}, **requests_kwargs}
else:
requests_kwargs = {"auth": self.auth}
return to_xarray(url, response, requests_kwargs, xarray_kwargs=dict(**kw))

def to_iris(self, **kw):
Expand Down
11 changes: 11 additions & 0 deletions tests/test_to_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,17 @@ def test_to_xarray_tabledap(dataset_tabledap):
assert ds["temperature"].name == "temperature"


@pytest.mark.web
def test_to_xarray_requests_kwargs(dataset_tabledap):
"""Test converting tabledap to an xarray Dataset manually setting timeout"""
ds = dataset_tabledap.to_xarray(requests_kwargs={"timeout": 30})

assert isinstance(ds, xr.Dataset)
assert len(ds.variables) == 9
assert ds["time"].name == "time"
assert ds["temperature"].name == "temperature"


@pytest.mark.web
def test_to_xarray_griddap(dataset_griddap):
"""Test converting griddap to an xarray Dataset."""
Expand Down

0 comments on commit ae358d5

Please sign in to comment.