Skip to content

Commit

Permalink
Merge pull request #3031 from YouvaEUMex/fix_2898
Browse files Browse the repository at this point in the history
Fix the handling of AMVs unit to units by applying suggestion in #2898
  • Loading branch information
mraspaud authored Jan 21, 2025
2 parents 6fc15fe + 38d106d commit 8082f99
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 35 deletions.
55 changes: 22 additions & 33 deletions satpy/readers/fci_l2_nc.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def ssp_lon(self):
f"of {SSP_DEFAULT} degrees east instead")
return SSP_DEFAULT

def _get_global_attributes(self):
def _get_global_attributes(self, product_type="pixel"):
"""Create a dictionary of global attributes to be added to all datasets.
Returns:
Expand All @@ -70,26 +70,36 @@ def _get_global_attributes(self):
ssp_lon: longitude of subsatellite point
sensor: name of sensor
platform_name: name of the platform
Only for AMVs product:
channel: channel at which the AMVs have been retrieved
"""
attributes = {
"filename": self.filename,
"spacecraft_name": self.spacecraft_name,
"ssp_lon": self.ssp_lon,
"sensor": self.sensor_name,
"platform_name": self.spacecraft_name,
"ssp_lon": self.ssp_lon,
}

if product_type=="amv":
attributes["channel"] = self.filename_info["channel"]

return attributes

def _set_attributes(self, variable, dataset_info, segmented=False):
def _set_attributes(self, variable, dataset_info, product_type="pixel"):
"""Set dataset attributes."""
if segmented:
xdim, ydim = "number_of_FoR_cols", "number_of_FoR_rows"
else:
xdim, ydim = "number_of_columns", "number_of_rows"
if product_type in ["pixel", "segmented"]:
if product_type == "pixel":
xdim, ydim = "number_of_columns", "number_of_rows"
elif product_type == "segmented":
xdim, ydim = "number_of_FoR_cols", "number_of_FoR_rows"

if dataset_info["nc_key"] not in ["product_quality", "product_completeness", "product_timeliness"]:
variable = variable.swap_dims({ydim: "y", xdim: "x"})
if dataset_info["nc_key"] not in ["product_quality",
"product_completeness",
"product_timeliness"]:
variable = variable.swap_dims({ydim: "y", xdim: "x"})

variable.attrs.setdefault("units", None)
if "unit" in variable.attrs:
Expand All @@ -98,7 +108,7 @@ def _set_attributes(self, variable, dataset_info, segmented=False):
del variable.attrs["unit"]

variable.attrs.update(dataset_info)
variable.attrs.update(self._get_global_attributes())
variable.attrs.update(self._get_global_attributes(product_type=product_type))

import_enum_information = dataset_info.get("import_enum_information", False)
if import_enum_information:
Expand Down Expand Up @@ -382,7 +392,7 @@ def get_dataset(self, dataset_id, dataset_info):
if "fill_value" in dataset_info:
variable = self._mask_data(variable, dataset_info["fill_value"])

variable = self._set_attributes(variable, dataset_info, segmented=True)
variable = self._set_attributes(variable, dataset_info, product_type="segmented")

return variable

Expand Down Expand Up @@ -457,26 +467,6 @@ def nc(self):
}
)

def _get_global_attributes(self):
"""Create a dictionary of global attributes to be added to all datasets.
Returns:
dict: A dictionary of global attributes.
filename: name of the product file
spacecraft_name: name of the spacecraft
sensor: name of sensor
platform_name: name of the platform
"""
attributes = {
"filename": self.filename,
"spacecraft_name": self.spacecraft_name,
"sensor": self.sensor_name,
"platform_name": self.spacecraft_name,
"channel": self.filename_info["channel"]
}
return attributes

def get_dataset(self, dataset_id, dataset_info):
"""Get dataset using the nc_key in dataset_info."""
var_key = dataset_info["nc_key"]
Expand All @@ -489,7 +479,6 @@ def get_dataset(self, dataset_id, dataset_info):
return None

# Manage the attributes of the dataset
variable.attrs.update(dataset_info)
variable.attrs.update(self._get_global_attributes())
variable = self._set_attributes(variable, dataset_info, product_type="amv")

return variable
5 changes: 3 additions & 2 deletions satpy/tests/reader_tests/test_fci_l2_nc.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,13 +616,14 @@ def test_all_basic(self, amv_filehandler, amv_file):
assert amv_filehandler.sensor_name == "test_data_source"
assert amv_filehandler.ssp_lon == 0.0

global_attributes = amv_filehandler._get_global_attributes()
global_attributes = amv_filehandler._get_global_attributes(product_type="amv")
expected_global_attributes = {
"filename": amv_file,
"spacecraft_name": "test_platform",
"sensor": "test_data_source",
"platform_name": "test_platform",
"channel": "test_channel"
"channel": "test_channel",
"ssp_lon": 0.0,
}
assert global_attributes == expected_global_attributes

Expand Down

0 comments on commit 8082f99

Please sign in to comment.