Repository: ecmwf/earthkit-data
Pull request #188 (2023-09-18 10:51:02)

187 netcdf reader not initiated correctly
I am still not exactly sure which circumstances trigger the bug, but I did find that it happened when handling level 2 satellite data (orbits). So I have included a test to ensure that level 2 data is readable. We can add more conditions when we know what methods we want available to this data object


Patch:
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 4893d17..d817a23 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -52,6 +52,7 @@ jobs:
         environment-name: DEVELOP
         channels: conda-forge
         cache-env: true
+        cache-env-key: ubuntu-latest-3.10
         extra-specs: |
           python=3.10
     - name: Install package
@@ -60,9 +61,32 @@ jobs:
     - name: Run tests
       run: |
         make unit-tests
-    - name: Run tests without eccodes
+
+  unit-tests-no-eccodes:
+    name: unit-tests (3.10)
+    if: ${{ !github.event.pull_request.head.repo.fork && github.event.action != 'labeled' || github.event.label.name == 'approved-for-ci' }}
+    runs-on: ubuntu-latest
+
+    steps:
+    - uses: actions/checkout@v3
+      with:
+        ref: ${{ github.event.pull_request.head.sha || github.ref }}
+    - name: Install Conda environment with Micromamba
+      uses: mamba-org/provision-with-micromamba@v14
+      with:
+        environment-file: tests/environment-unit-tests.yml
+        environment-name: DEVELOP
+        channels: conda-forge
+        cache-env: true
+        cache-env-key: ubuntu-latest-3.10-no-eccodes
+        extra-specs: |
+          python=3.10
+    - name: Install package
       run: |
+        python -m pip install --no-deps -e .
         micromamba remove eccodes
+    - name: Run tests without eccodes
+      run: |
         python -m pytest -v -m 'no_eccodes'
 
 
diff --git a/earthkit/data/readers/netcdf.py b/earthkit/data/readers/netcdf.py
index 0074ff2..90cd068 100644
--- a/earthkit/data/readers/netcdf.py
+++ b/earthkit/data/readers/netcdf.py
@@ -628,7 +628,6 @@ class NetCDFFieldListReader(NetCDFFieldListInOneFile, Reader):
 class NetCDFReader(Reader):
     def __init__(self, source, path):
         Reader.__init__(self, source, path)
-        NetCDFFieldList.__init__(self, path)
 
     def __repr__(self):
         return "NetCDFReader(%s)" % (self.path,)


Test patch:
diff --git a/tests/readers/test_netcdf_reader.py b/tests/readers/test_netcdf_reader.py
index 8eeddf7..9011383 100644
--- a/tests/readers/test_netcdf_reader.py
+++ b/tests/readers/test_netcdf_reader.py
@@ -19,8 +19,10 @@ import pytest
 from earthkit.data import from_source
 from earthkit.data.readers.netcdf import NetCDFField
 from earthkit.data.testing import (
+    NO_CDS,
     earthkit_examples_file,
     earthkit_file,
+    earthkit_remote_test_data_file,
     earthkit_test_data_file,
 )
 
@@ -98,9 +100,9 @@ def test_dummy_netcdf_4():
 
 
 @pytest.mark.long_test
+@pytest.mark.download
+@pytest.mark.skipif(NO_CDS, reason="No access to CDS")
 def test_netcdf_multi_cds():
-    if not os.path.exists(os.path.expanduser("~/.cdsapirc")):
-        pytest.skip("No ~/.cdsapirc")
     s1 = from_source(
         "cds",
         "reanalysis-era5-single-levels",
@@ -223,6 +225,29 @@ def test_get_fields_missing_standard_name_attr_in_coord_array():
         assert len(fs) == 2
 
 
+@pytest.mark.no_eccodes
+def test_netcdf_non_fieldlist():
+    ek_ch4_l2 = from_source(
+        "url",
+        earthkit_remote_test_data_file(
+            "test-data/20210101-C3S-L2_GHG-GHG_PRODUCTS-TANSO2-GOSAT2-SRFP-DAILY-v2.0.0.nc"
+        )
+        # Data from this CDS request:
+        # "cds",
+        # "satellite-methane",
+        # {
+        #     "processing_level": "level_2",
+        #     "sensor_and_algorithm": "tanso2_fts2_srfp",
+        #     "year": "2021",
+        #     "month": "01",
+        #     "day": "01",
+        #     "version": "2.0.0",
+        # },
+    )
+    # TODO: add more conditions to this test when it is clear what methods it should have
+    ek_ch4_l2.to_xarray()
+
+
 if __name__ == "__main__":
     from earthkit.data.testing import main