Skip to content

Commit

Permalink
fix the use of regrid.most_common in land_cover.py due to changes in …
Browse files Browse the repository at this point in the history
…version xarray_regrid v0.4.0
  • Loading branch information
SarahAlidoost committed Oct 18, 2024
1 parent bba68a9 commit b63505f
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions src/zampy/datasets/land_cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,30 @@ def load(
)
raise ValueError(msg)
files = list((ingest_dir / self.name).glob(f"{self.name}_*.nc"))

ds = xr.open_mfdataset(files, chunks={"latitude": 200, "longitude": 200})
ds = ds.sel(time=slice(time_bounds.start, time_bounds.end))

grid = xarray_regrid.create_regridding_dataset(
utils.make_grid(spatial_bounds, resolution)
)
ds = ds.regrid.most_common(grid)

return ds
ds_regrid = {}
for variable in variable_names:
# select the variable to be regridded
da = ds[variable]

# get values for most common method
if "flag_values" in da.attrs:
regrid_values = da.attrs["flag_values"]
else:
regrid_values = np.unique(da.values)

da_regrid = da.regrid.most_common(grid, values=regrid_values)

# make sure dtype is the same
ds_regrid[variable] = da_regrid.astype(da.dtype)

return xr.Dataset(ds_regrid)

def convert(
self,
Expand Down Expand Up @@ -207,7 +221,7 @@ def extract_netcdf_to_zampy(file: Path) -> xr.Dataset:

# only keep land cover class variable
with xr.open_dataset(unzip_folder / zipped_file_name) as ds:
var_list = [var for var in ds.data_vars]
var_list = list(ds.data_vars)
raw_variable = "lccs_class"
var_list.remove(raw_variable)
ds = ds.drop_vars(var_list) # noqa: PLW2901
Expand All @@ -225,7 +239,23 @@ def extract_netcdf_to_zampy(file: Path) -> xr.Dataset:

target_dataset = xarray_regrid.create_regridding_dataset(new_grid)

ds_regrid = ds.regrid.most_common(target_dataset)
# select the variable to be regridded
da = ds[raw_variable]

# get values for most common method
if "flag_values" in da.attrs:
regrid_values = da.attrs["flag_values"]
else:
regrid_values = np.unique(da.values)


da_regrid = da.regrid.most_common(target_dataset, values=regrid_values)

# make sure dtype is the same
da_regrid = da_regrid.astype(da.dtype)

# convert dataarray to dataset
ds_regrid = da_regrid.to_dataset()

# rename variable to follow the zampy convention
variable_name = "land_cover"
Expand Down

0 comments on commit b63505f

Please sign in to comment.