diff --git a/copernicusmarine/download_functions/subset_xarray.py b/copernicusmarine/download_functions/subset_xarray.py index 42d6af05..74e0f04d 100644 --- a/copernicusmarine/download_functions/subset_xarray.py +++ b/copernicusmarine/download_functions/subset_xarray.py @@ -44,6 +44,8 @@ "longitude": ["longitude", "nav_lon", "y", "lon"], "time": ["time_counter", "time"], "depth": ["depth", "deptht", "elevation"], + "x": ["x"], + "y": ["y"], } NETCDF_CONVENTION_VARIABLE_ATTRIBUTES = [ @@ -146,7 +148,7 @@ def _nearest_selection( def _dataset_custom_sel( dataset: xarray.Dataset, - coord_type: Literal["latitude", "longitude", "depth", "time"], + coord_type: Literal["latitude", "longitude", "depth", "time", "x", "y"], coord_selection: Union[float, slice, datetime, None], coordinates_selection_method: CoordinatesSelectionMethod, ) -> xarray.Dataset: @@ -290,9 +292,12 @@ def _y_subset( if minimum_y == maximum_y else slice(minimum_y, maximum_y) ) - dataset = dataset.sel( - {"y": y_selection} - ) # TODO: add the custom selection + return _dataset_custom_sel( + dataset, + "y", + y_selection, + coordinates_selection_method, + ) return dataset @@ -310,10 +315,12 @@ def _x_subset( if minimum_x == maximum_x else slice(minimum_x, maximum_x) ) - dataset = dataset.sel( - {"x": x_selection} - ) # TODO: add the custom selection - + return _dataset_custom_sel( + dataset, + "x", + x_selection, + coordinates_selection_method, + ) return dataset diff --git a/tests/test_originalGrid_datasets.py b/tests/test_originalGrid_datasets.py index 07575e5d..8bfbf17b 100644 --- a/tests/test_originalGrid_datasets.py +++ b/tests/test_originalGrid_datasets.py @@ -12,9 +12,9 @@ ["cmems_mod_arc_bgc_anfc_ecosmo_P1D-m", "2020"], ["cmems_mod_arc_bgc_anfc_ecosmo_P1M-m", "2020"], ["cmems_mod_arc_phy_anfc_6km_detided_PT1H-i", "2022"], - ["cmems_mod_arc_phy_anfc_6km_detided_PT6H-m", "2020"], - ["cmems_mod_arc_phy_anfc_6km_detided_P1D-m", "2020"], - ["cmems_mod_arc_phy_anfc_6km_detided_P1M-m", "2020"], + ["cmems_mod_arc_phy_anfc_6km_detided_PT6H-m", "2024"], + ["cmems_mod_arc_phy_anfc_6km_detided_P1D-m", "2024"], + ["cmems_mod_arc_phy_anfc_6km_detided_P1M-m", "2024"], # "cmems_mod_arc_phy_anfc_nextsim_P1M-m", "2020", # not yet available # "cmems_mod_arc_phy_anfc_nextsim_hm", # not yet available # "dataset-topaz6-arc-15min-3km-be", # not yet available @@ -149,39 +149,38 @@ def test_originalGrid_works_when_time_and_depth_subsetting(self, tmp_path): def test_originalGrid_works_when_subsetting(self): for dataset_info in datasets_w_originalGrid: - dataset_name = dataset_info[0] - dataset_year = dataset_info[1] - command = [ - "copernicusmarine", - "subset", - "-i", - dataset_name, - "--dataset-part", - "originalGrid", - "--maximum-x", - "8", - "--minimum-x", - "6", - "--maximum-y", - "10", - "--minimum-y", - "5", - "-t", - dataset_year, - "-T", - dataset_year, - "--dry-run", - ] - self.output = execute_in_terminal(command) - assert self.output.returncode == 0 - returned_value = loads(self.output.stdout) - assert ( - returned_value["coordinates_extent"][0]["coordinate_id"] == "y" - ) - assert returned_value["coordinates_extent"][0]["maximum"] == 10 - assert returned_value["coordinates_extent"][0]["minimum"] == 5 - assert ( - returned_value["coordinates_extent"][1]["coordinate_id"] == "x" - ) - assert returned_value["coordinates_extent"][1]["maximum"] == 8 - assert returned_value["coordinates_extent"][1]["minimum"] == 6 + self.run_one_dataset(dataset_info) + + def run_one_dataset(self, dataset_info): + dataset_name = dataset_info[0] + dataset_year = dataset_info[1] + command = [ + "copernicusmarine", + "subset", + "-i", + dataset_name, + "--dataset-part", + "originalGrid", + "--maximum-x", + "8", + "--minimum-x", + "6", + "--maximum-y", + "10", + "--minimum-y", + "5", + "-t", + dataset_year, + "-T", + dataset_year, + "--dry-run", + ] + self.output = execute_in_terminal(command) + assert self.output.returncode == 0 + returned_value = loads(self.output.stdout) + assert returned_value["coordinates_extent"][0]["coordinate_id"] == "y" + assert returned_value["coordinates_extent"][0]["maximum"] == 10 + assert returned_value["coordinates_extent"][0]["minimum"] == 5 + assert returned_value["coordinates_extent"][1]["coordinate_id"] == "x" + assert returned_value["coordinates_extent"][1]["maximum"] == 8 + assert returned_value["coordinates_extent"][1]["minimum"] == 6