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 7 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
12 changes: 10 additions & 2 deletions erddapy/erddapy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Dict, List, Optional, Tuple, Union

import pandas as pd
import xarray as xr

from erddapy.core.griddap import (
_griddap_check_constraints,
Expand Down Expand Up @@ -367,7 +368,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,
) -> "xr.Dataset":
"""Load the data request into a xarray.Dataset.

Accepts any `xr.open_dataset` keyword arguments.
Expand All @@ -380,7 +385,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
1 change: 0 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,3 @@ twine
types-pytz
types-requests
wheel
xarray
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
httpx
pandas>=0.25.2,<3
pytz
xarray
ocefpaf marked this conversation as resolved.
Show resolved Hide resolved
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