Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add request_kwargs to to_xarray #315

Merged
merged 8 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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