pytroll / satpy

Python package for earth-observing satellite data processing
http://satpy.readthedocs.org/en/latest/
GNU General Public License v3.0
1.08k stars 298 forks source link

units attribute is not CF conform for the for AMV datasets in the fci_l2_nc reader #2898

Open strandgren opened 2 months ago

strandgren commented 2 months ago

Describe the bug The AMV datasets returned by the fci_l2_nc reader contain the attribute unit, rather than units, which is the CF naming convention. This is an inherited issues since the FCI L2 NetCDF files use the unit attribute, which is then read and used by reader.

The reader contains a common method FciL2CommonFunctions._set_attributes (https://github.com/pytroll/satpy/blob/main/satpy/readers/fci_l2_nc.py#L84) which addresses this issue. However, this method is not used by the FciL2NCAMVFileHandler.

Furthermore, the FciL2NCAMVFileHandler contains a copy of the _get_global_attributes, which is already available in FciL2CommonFunctions.

Proposed solution Refactor FciL2CommonFunctions._set_attributes to something like this:

    def _set_attributes(self, variable, dataset_info, product_type="pixel"):
        """Set dataset attributes."""
        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 product_type in ["pixel", "segmented"] and 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:
            # Need to convert this attribute to the expected satpy entry
            variable.attrs.update({"units": variable.attrs["unit"]})
            del variable.attrs["unit"]

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

        import_enum_information = dataset_info.get("import_enum_information", False)
        if import_enum_information:
            variable = self._add_flag_values_and_meanings(self.filename, dataset_info["nc_key"], variable)

        if variable.attrs["units"] == "none":
            variable.attrs.update({"units": None})

        return variable

and then in FciL2NCAMVFileHandler delete _get_global_attributes and modify these lines

# Manage the attributes of the dataset
variable.attrs.update(dataset_info)
variable.attrs.update(self._get_global_attributes())

to

# Manage the attributes of the dataset
variable = self._set_attributes(variable, dataset_info, product_type='amv')

Furthermore, modify this line in FciL2NCSegmentFileHandler.get_dataset

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

to

variable = self._set_attributes(variable, dataset_info, product_type='segmented')
ameraner commented 1 month ago

^ @YouvaEUMex