Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the handling of AMVs unit to units by applying suggestion in #2898 #3031

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
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
@@ -1,4 +1,4 @@
#!/usr/bin/env python

Check notice on line 1 in satpy/tests/reader_tests/test_fci_l2_nc.py

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

✅ Getting better: Code Duplication

reduced similar code in: TestFciL2NCAMVFileHandler.test_all_basic. Avoid duplicated, aka copy-pasted, code inside the module. More duplication lowers the code health.
# -*- coding: utf-8 -*-
#
# Copyright (c) 2019 Satpy developers
Expand Down Expand Up @@ -616,13 +616,14 @@
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
Loading