Repository: ioos/erddapy

Issue:
erddapy with griddap .to_xarray() .to_netcdf() response forces .ncCF (not a griddap option)
I know griddap isn't officially supported but it previously had worked for the purposes I needed... seems the implementation of .ncCF response may be throwing it off (url building it forces .ncCF as the response type now but there isn't a .ncCF type for griddap)

if I change the url to response nc - i get what I expect as returned data

gist of a simple example showing the failure in trying to load - https://gist.github.com/shaunwbell/b11e4ae63638010866b6ae9d64261004

and my adhoc solution (which is purpose built, not holistic or i would push a pull request)
```python
    
  def to_xarray(self, **kw):
        """Load the data request into a xarray.Dataset.

        Accepts any `xr.open_dataset` keyword arguments.
        """
        import xarray as xr

        url = self.get_download_url(response="nc")
        nc = _nc_dataset(url, auth=self.auth, **self.requests_kwargs)
        return xr.open_dataset(xr.backends.NetCDF4DataStore(nc), **kw)
```

am i missing a part of the API to set this explicitly or is it hardcoded into the script?

thanks

erddapy = 0.7.0
python 3.8.5

Discussion:

Patch:
diff --git a/.isort.cfg b/.isort.cfg
index 4fd1b03..47bb6f5 100644
--- a/.isort.cfg
+++ b/.isort.cfg
@@ -1,2 +1,2 @@
 [settings]
-known_third_party = pandas,pendulum,pytest,pytz,requests,setuptools
+known_third_party = iris,pandas,pendulum,pytest,pytz,requests,setuptools,xarray
diff --git a/erddapy/erddapy.py b/erddapy/erddapy.py
index 2e0d0a9..955af9e 100644
--- a/erddapy/erddapy.py
+++ b/erddapy/erddapy.py
@@ -356,6 +356,8 @@ class ERDDAP(object):
         """Load the data request into a Climate and Forecast compliant netCDF4-python object.
 
         """
+        if self.protocol == "griddap":
+            return ValueError(f"Cannot use ncCF with griddap.")
         url = self.get_download_url(response="ncCF", **kw)
         nc = _nc_dataset(url, auth=self.auth, **self.requests_kwargs)
         return nc
@@ -367,7 +369,8 @@ class ERDDAP(object):
         """
         import xarray as xr
 
-        url = self.get_download_url(response="ncCF")
+        response = "nc" if self.protocol == "griddap" else "ncCF"
+        url = self.get_download_url(response=response)
         nc = _nc_dataset(url, auth=self.auth, **self.requests_kwargs)
         return xr.open_dataset(xr.backends.NetCDF4DataStore(nc), **kw)
 
@@ -378,7 +381,8 @@ class ERDDAP(object):
         """
         import iris
 
-        url = self.get_download_url(response="ncCF", **kw)
+        response = "nc" if self.protocol == "griddap" else "ncCF"
+        url = self.get_download_url(response=response, **kw)
         data = urlopen(url, auth=self.auth, **self.requests_kwargs).read()
         with _tempnc(data) as tmp:
             cubes = iris.load_raw(tmp, **kw)


Test patch:
diff --git a/tests/test_to_objects.py b/tests/test_to_objects.py
new file mode 100644
index 0000000..fab80f2
--- /dev/null
+++ b/tests/test_to_objects.py
@@ -0,0 +1,81 @@
+import iris
+import pytest
+import xarray as xr
+
+from erddapy import ERDDAP
+
+
+@pytest.fixture
+@pytest.mark.web
+def e():
+    yield ERDDAP(
+        server="https://upwell.pfeg.noaa.gov/erddap", response="htmlTable",
+    )
+
+
+@pytest.fixture
+def bermuda_1_msl(e):
+    e.dataset_id = "noaa_ngdc_9cfa_244d_0065"
+    e.constraints = None
+    e.protocol = "griddap"
+    yield e
+
+
+@pytest.fixture
+def taodata(e):
+    e.dataset_id = "pmelTao5dayIso"
+    e.protocol = "tabledap"
+    e.constraints = {
+        "time>=": "1977-11-10T12:00:00Z",
+        "time<=": "2019-01-26T12:00:00Z",
+        "latitude>=": 10,
+        "latitude<=": 10,
+        "longitude>=": 265,
+        "longitude<=": 265,
+    }
+    e.variables = ["ISO_6", "time"]
+    yield e
+
+
+@pytest.mark.web
+def test_to_pandas(taodata):
+    import pandas as pd
+
+    df = taodata.to_pandas(index_col="time (UTC)", parse_dates=True).dropna()
+
+    assert isinstance(df, pd.DataFrame)
+    assert df.index.name == "time (UTC)"
+    assert len(df.columns) == 1
+    assert df.columns[0] == "ISO_6 (m)"
+
+
+@pytest.mark.web
+def test_to_xarray_tabledap(taodata):
+    ds = taodata.to_xarray()
+
+    assert isinstance(ds, xr.Dataset)
+    assert len(ds.variables) == 7
+    assert ds["time"].name == "time"
+    assert ds["ISO_6"].name == "ISO_6"
+
+
+@pytest.mark.web
+def test_to_xarray_griddap(bermuda_1_msl):
+    ds = bermuda_1_msl.to_xarray()
+
+    assert isinstance(ds, xr.Dataset)
+
+
+@pytest.mark.web
+def test_to_iris_tabledap(taodata):
+    cubes = taodata.to_iris()
+
+    assert isinstance(cubes, iris.cube.CubeList)
+    assert isinstance(cubes.extract_strict("depth"), iris.cube.Cube)
+    assert isinstance(cubes.extract_strict("20C Isotherm Depth"), iris.cube.Cube)
+
+
+@pytest.mark.web
+def test_to_iris_griddap(bermuda_1_msl):
+    cubes = bermuda_1_msl.to_iris()
+    assert isinstance(cubes, iris.cube.CubeList)
diff --git a/tests/test_url_builder.py b/tests/test_url_builder.py
index e7affed..e4c312a 100644
--- a/tests/test_url_builder.py
+++ b/tests/test_url_builder.py
@@ -20,21 +20,6 @@ def e():
     )
 
 
-@pytest.fixture
-def taodata(e):
-    e.dataset_id = "pmelTao5dayIso"
-    e.constraints = {
-        "time>=": "1977-11-10T12:00:00Z",
-        "time<=": "2019-01-26T12:00:00Z",
-        "latitude>=": 10,
-        "latitude<=": 10,
-        "longitude>=": 265,
-        "longitude<=": 265,
-    }
-    e.variables = ["ISO_6", "time"]
-    yield e
-
-
 @pytest.mark.web
 def test_search_url_bad_request(e):
     """Test if a bad request returns HTTPError."""
@@ -152,41 +137,6 @@ def test_download_url_constrained(e):
     assert options["longitude<"] == str(max_lon)
 
 
-@pytest.mark.web
-def test_to_pandas(taodata):
-    import pandas as pd
-
-    df = taodata.to_pandas(index_col="time (UTC)", parse_dates=True).dropna()
-
-    assert isinstance(df, pd.DataFrame)
-    assert df.index.name == "time (UTC)"
-    assert len(df.columns) == 1
-    assert df.columns[0] == "ISO_6 (m)"
-
-
-@pytest.mark.web
-def test_to_xarray(taodata):
-    import xarray as xr
-
-    ds = taodata.to_xarray()
-
-    assert isinstance(ds, xr.Dataset)
-    assert len(ds.variables) == 7
-    assert ds["time"].name == "time"
-    assert ds["ISO_6"].name == "ISO_6"
-
-
-@pytest.mark.web
-def test_to_iris(taodata):
-    import iris
-
-    cubes = taodata.to_iris()
-
-    assert isinstance(cubes, iris.cube.CubeList)
-    assert isinstance(cubes.extract_strict("depth"), iris.cube.Cube)
-    assert isinstance(cubes.extract_strict("20C Isotherm Depth"), iris.cube.Cube)
-
-
 @pytest.mark.web
 def test_get_var_by_attr(e):
     variables = e.get_var_by_attr(dataset_id="gtoppAT", axis="X")