Skip to content

Commit

Permalink
Fix VIIRS EDR using the wrong geolocation arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
djhoese committed Jun 27, 2024
1 parent 834f45d commit 6c8c8aa
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
36 changes: 23 additions & 13 deletions satpy/readers/viirs_edr.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ def _dynamic_variables_from_file(self, handled_var_names: set) -> Iterable[tuple
i_lon_name = f"longitude_i_{ftype}"
i_lat_name = f"latitude_i_{ftype}"
i_coords = (i_lon_name, i_lat_name)
coords: dict[str, dict] = {}
for var_name in self.nc.variables.keys():
data_arr = self.nc[var_name]
is_lon = "longitude" in var_name.lower()
Expand All @@ -276,19 +277,28 @@ def _dynamic_variables_from_file(self, handled_var_names: set) -> Iterable[tuple
"resolution": res,
"coordinates": m_coords if res == 750 else i_coords,
}
if is_lon:
ds_info["standard_name"] = "longitude"
ds_info["units"] = "degrees_east"
ds_info["name"] = m_lon_name if res == 750 else i_lon_name
# recursive coordinate/SwathDefinitions are not currently handled well in the base reader
del ds_info["coordinates"]
elif is_lat:
ds_info["standard_name"] = "latitude"
ds_info["units"] = "degrees_north"
ds_info["name"] = m_lat_name if res == 750 else i_lat_name
# recursive coordinate/SwathDefinitions are not currently handled well in the base reader
del ds_info["coordinates"]
yield True, ds_info
if not (is_lon or is_lat):
yield True, ds_info
continue

ds_info["standard_name"] = "longitude" if is_lon else "latitude"
ds_info["units"] = "degrees_east" if is_lon else "degrees_north"
# recursive coordinate/SwathDefinitions are not currently handled well in the base reader
del ds_info["coordinates"]
if var_name not in handled_var_names:
handled_var_names.add(var_name)
yield True, ds_info

# "standard" geolocation coordinate (assume shorter variable name is "better")
new_name = (m_lon_name if res == 750 else i_lon_name) if is_lon else (
m_lat_name if res == 750 else i_lat_name)
if new_name not in coords or len(var_name) < len(coords[new_name]["file_key"]):
ds_info = ds_info.copy()
ds_info["name"] = new_name
coords[ds_info["name"]] = ds_info

for coord_info in coords.values():
yield True, coord_info


class VIIRSSurfaceReflectanceWithVIHandler(VIIRSJRRFileHandler):
Expand Down
11 changes: 11 additions & 0 deletions satpy/tests/reader_tests/test_viirs_edr.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,14 @@ def cloud_height_file(tmp_path_factory: TempPathFactory) -> Path:
data_vars = _create_continuous_variables(
("CldTopTemp", "CldTopHght", "CldTopPres")
)
lon_pc = data_vars["Longitude"].copy(deep=True)
lat_pc = data_vars["Latitude"].copy(deep=True)
lon_pc.attrs["long_name"] = "BAD"
lat_pc.attrs["long_name"] = "BAD"
del lon_pc.encoding["_FillValue"]
del lat_pc.encoding["_FillValue"]
data_vars["Longitude_Pc"] = lon_pc
data_vars["Latitude_Pc"] = lat_pc
return _create_fake_file(tmp_path_factory, fn, data_vars)


Expand Down Expand Up @@ -556,6 +564,9 @@ def _shared_metadata_checks(data_arr: xr.DataArray) -> None:
assert lons.max() <= 180.0
assert lats.min() >= -90.0
assert lats.max() <= 90.0
# Some files (ex. CloudHeight) have other lon/lats that shouldn't be used
assert lons.attrs.get("long_name") != "BAD"
assert lats.attrs.get("long_name") != "BAD"

if "valid_range" in data_arr.attrs:
valid_range = data_arr.attrs["valid_range"]
Expand Down

0 comments on commit 6c8c8aa

Please sign in to comment.