Skip to content

Commit

Permalink
Fix reading netcdf generated from FA file (#401)
Browse files Browse the repository at this point in the history
* Fix reading netcdf generated from FA file
  • Loading branch information
sandorkertesz authored Jun 6, 2024
1 parent 36b2d9c commit 7017b9b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
15 changes: 12 additions & 3 deletions src/earthkit/data/readers/netcdf/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,18 @@ def _get_latlon(self, data_array, flatten=False, dtype=None):
keys, coords = self._get_xy_coords(data_array)

points = dict()
if "latitude" in self._ds and "longitude" in self._ds:
latitude = self._ds["latitude"]
longitude = self._ds["longitude"]

def _get_ll(keys):
for key in keys:
if key in self._ds:
return self._ds[key]

lat_keys = ["latitude", "lat"]
lon_keys = ["longitude", "lon"]
latitude = _get_ll(lat_keys)
longitude = _get_ll(lon_keys)

if latitude is not None and longitude is not None:
if latitude.dims == coords and longitude.dims == coords:
latitude = latitude.data
longitude = longitude.data
Expand Down
7 changes: 4 additions & 3 deletions src/earthkit/data/readers/netcdf/fieldlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ def get_fields_from_ds(
field_type=None,
check_only=False,
): # noqa C901
# Select only geographical variables
has_lat = False
has_lon = False

fields = []

Expand All @@ -47,6 +44,10 @@ def _skip_attr(v, attr_name):
_skip_attr(v, "grid_mapping")

for name in ds.data_vars:
# Select only geographical variables
has_lat = False
has_lon = False

if name in skip:
continue

Expand Down
8 changes: 8 additions & 0 deletions tests/netcdf/test_netcdf_geography.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,14 @@ def test_netcdf_to_latlon_laea():
assert np.isclose(v["lat"][x], ref[i]), f"{i=}, {x=}"


def test_netcdf_forecast_reference_time():
ds = from_source("url", earthkit_remote_test_data_file("test-data", "fa_ta850.nc"))

assert len(ds) == 37
assert ds[0].metadata("valid_datetime") == "2020-01-23T00:00:00"
assert ds[5].metadata("valid_datetime") == "2020-01-23T05:00:00"


if __name__ == "__main__":
from earthkit.data.testing import main

Expand Down

0 comments on commit 7017b9b

Please sign in to comment.