-
-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add regridding #150
Merged
Merged
add regridding #150
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d021915
add regridding
peterdudfield 7b80cc5
lint
peterdudfield fbfc6e8
lint
peterdudfield 034ceaf
update to run more effeciently in app
peterdudfield c6fe944
fix
peterdudfield 280c082
Merge branch 'main' into regrid
peterdudfield 996ec62
Merge commit 'd90150118cca47cacde468ea49345ea7afa74845' into regrid
peterdudfield 64517bd
remove hard coding
peterdudfield be7a556
lint
peterdudfield 0344965
lint
peterdudfield 5b7d186
lint
peterdudfield 5d38b41
fix bug
peterdudfield 8d67aae
fix
peterdudfield File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,62 @@ | ||
import xarray as xr | ||
import os | ||
import logging | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def regrid_nwp_data(nwp_ds: xr.Dataset, target_coords_path: str) -> xr.Dataset: | ||
"""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. | ||
""" | ||
|
||
logger.info(f"Regridding NWP data to expected grid to {target_coords_path}") | ||
|
||
ds_raw = nwp_ds | ||
|
||
# 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 - skipping this step") | ||
return ds_raw | ||
|
||
# flip latitude, so its in ascending order | ||
if ds_raw.latitude[0] > ds_raw.latitude[-1]: | ||
ds_raw = ds_raw.reindex(latitude=ds_raw.latitude[::-1]) | ||
|
||
# clip to india coordindates | ||
ds_raw = ds_raw.sel( | ||
latitude=slice(0, 40), | ||
longitude=slice(65, 100), | ||
) | ||
|
||
# regrid | ||
logger.info(f"Regridding NWP to expected grid") | ||
ds_regridded = ds_raw.interp( | ||
latitude=ds_target_coords.latitude, longitude=ds_target_coords.longitude | ||
) | ||
|
||
# rechunking | ||
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 = ds_regridded.chunk( | ||
{k: save_chunk_dict[k] for k in list(ds_raw.xindexes) if k in save_chunk_dict} | ||
) | ||
|
||
return ds_regridded |
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,30 @@ | ||
""" 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_xr = xr.open_zarr(nwp_zarr) | ||
nwp_xr_regridded = regrid_nwp_data( | ||
nwp_xr, "india_forecast_app/data/mo_global/india_coords.nc" | ||
) | ||
|
||
# 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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be a separate PR but it would be good to do this for ECMWF as well as there is also a difference in spatial resolution between training and live for it, although it is worth noting that I think we the current HF config files we use it will error since we have hacked them to work and would need to change them back to the original number of pixels
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep, i was thinking a seperate PR for ECMWF stuff
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now in #153