-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b5b7224
commit d021915
Showing
5 changed files
with
142 additions
and
22 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import xarray as xr | ||
import os | ||
import logging | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def regrid_nwp_data(nwp_zarr: str, target_coords_path: str, nwp_zarr_save: str): | ||
"""This function loads the NWP data, then regrids and saves it back out if the data is not | ||
on the same grid as expected. The data is resaved in-place. | ||
method can be 'conservative' or 'bilinear' | ||
""" | ||
|
||
logger.info(f"Regridding NWP data {nwp_zarr} to expected grid to {target_coords_path}") | ||
|
||
ds_raw = xr.open_zarr(nwp_zarr) | ||
|
||
# These are the coords we are aiming for | ||
ds_target_coords = xr.load_dataset(target_coords_path) | ||
|
||
# Check if regridding step needs to be done | ||
needs_regridding = not ( | ||
ds_raw.latitude.equals(ds_target_coords.latitude) | ||
and ds_raw.longitude.equals(ds_target_coords.longitude) | ||
) | ||
|
||
if not needs_regridding: | ||
logger.info(f"No NWP regridding required for {nwp_zarr} - skipping this step") | ||
return | ||
|
||
logger.info(f"Regridding NWP {nwp_zarr} to expected grid") | ||
|
||
# Pull the raw data into RAM | ||
ds_raw = ds_raw.compute() | ||
|
||
# regrid | ||
ds_regridded = ds_raw.interp( | ||
latitude=ds_target_coords.latitude, longitude=ds_target_coords.longitude | ||
) | ||
|
||
# Re-save - including rechunking | ||
os.system(f"rm -rf {nwp_zarr_save}") | ||
ds_regridded["variable"] = ds_regridded["variable"].astype(str) | ||
|
||
# Rechunk to these dimensions when saving | ||
save_chunk_dict = { | ||
"step": 5, | ||
"latitude": 100, | ||
"longitude": 100, | ||
"x": 100, | ||
"y": 100, | ||
} | ||
|
||
ds_regridded.chunk( | ||
{k: save_chunk_dict[k] for k in list(ds_raw.xindexes) if k in save_chunk_dict} | ||
).to_zarr(nwp_zarr_save) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
""" Tests for the nwp regridding module """ | ||
import os | ||
import tempfile | ||
|
||
import xarray as xr | ||
|
||
from india_forecast_app.data.nwp import regrid_nwp_data | ||
|
||
|
||
def test_regrid_nwp_data(nwp_mo_global_data): | ||
"""Test the regridding of the nwp data""" | ||
|
||
# create a temporary dir | ||
with tempfile.TemporaryDirectory() as temp_dir: | ||
|
||
# save mo data to zarr | ||
nwp_zarr = os.environ["NWP_MO_GLOBAL_ZARR_PATH"] | ||
|
||
# regrid the data | ||
nwp_zarr_save = f"{temp_dir}/nwp_regrid.zarr" | ||
regrid_nwp_data( | ||
nwp_zarr, "india_forecast_app/data/mo_global/india_coords.nc", nwp_zarr_save | ||
) | ||
|
||
# open the regridded data | ||
nwp_xr = xr.open_zarr(nwp_zarr) | ||
nwp_xr_regridded = xr.open_zarr(nwp_zarr_save) | ||
|
||
# check the data is different in latitude and longitude | ||
assert not nwp_xr_regridded.latitude.equals(nwp_xr.latitude) | ||
assert not nwp_xr_regridded.longitude.equals(nwp_xr.longitude) | ||
|
||
assert len(nwp_xr_regridded.latitude) == 225 | ||
assert len(nwp_xr_regridded.longitude) == 150 |