Skip to content

Commit

Permalink
Deleting valid_min and valid_max attributes (#104)
Browse files Browse the repository at this point in the history
Deleting the valid_min and valid_max attributes from the dimensions, as requested to comply with CF conventions.
  • Loading branch information
uriii3 authored and renaudjester committed Oct 28, 2024
1 parent 8d7f640 commit e3e61cf
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 60 deletions.
26 changes: 1 addition & 25 deletions copernicusmarine/download_functions/subset_xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@
VariableDoesNotExistInTheDataset,
)
from copernicusmarine.core_functions.models import SubsetMethod
from copernicusmarine.core_functions.utils import (
ServiceNotSupported,
convert_datetime64_to_netcdf_timestamp,
)
from copernicusmarine.core_functions.utils import ServiceNotSupported
from copernicusmarine.download_functions.subset_parameters import (
DepthParameters,
GeographicalParameters,
Expand Down Expand Up @@ -56,8 +53,6 @@
"units",
"unit_long",
"axis",
"valid_min",
"valid_max",
]
NETCDF_CONVENTION_DATASET_ATTRIBUTES = [
"title",
Expand Down Expand Up @@ -359,34 +354,15 @@ def _update_dataset_coordinate_attributes(
NETCDF_CONVENTION_COORDINATE_ATTRIBUTES.copy()
)
if "time" in coordinate_label:
min_time_dimension = coord.values.min()
max_time_dimension = coord.values.max()
netcdf_unit = coord.encoding["units"]
valid_min = convert_datetime64_to_netcdf_timestamp(
min_time_dimension, netcdf_unit
)
valid_max = convert_datetime64_to_netcdf_timestamp(
max_time_dimension, netcdf_unit
)
attrs["standard_name"] = "time"
attrs["long_name"] = "Time"
attrs["valid_min"] = valid_min
attrs["valid_max"] = valid_max
attrs["axis"] = "T"
attrs["unit_long"] = (
coord.encoding["units"].replace("_", " ").title()
)
coordinate_attributes.remove("units")
elif coordinate_label in ["depth", "elevation"]:
attrs["valid_min"] = coord.values.min()
attrs["valid_max"] = coord.values.max()
coordinate_attributes.append("positive")
elif coordinate_label == "latitude":
attrs["valid_min"] = coord.values.min()
attrs["valid_max"] = coord.values.max()
elif coordinate_label == "longitude":
coordinate_attributes.remove("valid_min")
coordinate_attributes.remove("valid_max")
coord.attrs = _filter_attributes(attrs, coordinate_attributes)

dataset.attrs = _filter_attributes(
Expand Down
30 changes: 0 additions & 30 deletions tests/test_command_line_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,36 +494,6 @@ def check_subset_request_with_no_subsetting(self):
b"copernicusmarine get --dataset-id " + bytes(dataset_id, "utf-8")
) in self.output.stderr

def test_if_dataset_coordinate_valid_minmax_attributes_are_setted(
self, tmp_path
):
self.base_request_dict = {
"--dataset-id": "cmems_mod_glo_phy-so_anfc_0.083deg_P1D-m",
"--variable": "so",
"--start-datetime": "2024-01-01",
"--end-datetime": "2024-01-02",
"--minimum-latitude": "0.0",
"--maximum-latitude": "0.1",
"--minimum-longitude": "0.2",
"--maximum-longitude": "0.3",
"--minimum-depth": "0.0",
"--maximum-depth": "5.0",
"-f": "self.output.nc",
"--output-directory": tmp_path,
}

self.check_default_subset_request(self.GEOSERIES.subpath, tmp_path)

dataset_path = pathlib.Path(tmp_path) / "self.output.nc"
dataset = xarray.open_dataset(dataset_path)

assert dataset.latitude.attrs["valid_min"] >= 0
assert dataset.latitude.attrs["valid_max"] <= 0.1
assert dataset.depth.attrs["valid_min"] >= 0
assert dataset.depth.attrs["valid_max"] <= 5
assert dataset.time.attrs["valid_min"] == 648672
assert dataset.time.attrs["valid_max"] == 648696

def test_retention_period_works(self):
self.command = [
"copernicusmarine",
Expand Down
6 changes: 1 addition & 5 deletions tests/test_python_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def test_open_dataset_with_retention_date(self):
password=os.getenv("COPERNICUSMARINE_SERVICE_PASSWORD"),
dataset_id="cmems_obs-oc_atl_bgc-plankton_nrt_l4-gapfree-multi-1km_P1D",
)
assert dataset.time.valid_min >= 45388
assert int(dataset.time.min().values) >= 1720735200000

def test_subset_modify_attr_for_depth(self):
dataset = open_dataset(
Expand Down Expand Up @@ -256,10 +256,6 @@ def test_subset_keeps_fillvalue_empty(self, tmp_path):
assert "_FillValue" not in subsetdata.time.attrs
assert "_FillValue" not in subsetdata.latitude.attrs
assert "_FillValue" not in subsetdata.depth.attrs
assert "valid_max" not in subsetdata.longitude.attrs
assert "valid_min" not in subsetdata.longitude.attrs
assert "valid_max" in subsetdata.latitude.attrs
assert "valid_max" in subsetdata.latitude.attrs
assert subsetdata.time.attrs["calendar"] == "gregorian"
assert subsetdata.time.attrs["units"] == "hours since 1950-01-01"

Expand Down

0 comments on commit e3e61cf

Please sign in to comment.