diff --git a/erddapy/erddapy.py b/erddapy/erddapy.py index 8ba9d3d..b896fd9 100644 --- a/erddapy/erddapy.py +++ b/erddapy/erddapy.py @@ -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. @@ -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): diff --git a/tests/test_to_objects.py b/tests/test_to_objects.py index 72f5bd9..e3e0bbf 100644 --- a/tests/test_to_objects.py +++ b/tests/test_to_objects.py @@ -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."""