-
Notifications
You must be signed in to change notification settings - Fork 10
Add a new CLI for vertical-only regridding #175
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
Open
Jenny Hickson (jennyhickson)
wants to merge
12
commits into
MetOffice:main
Choose a base branch
from
jennyhickson:96-vertical-cli
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
287eb8b
#96 first pass vertical regridder
jennyhickson 250bbfc
#96 second pass vertical regridder
jennyhickson 4634305
#96 expand possibly axis to altitude
jennyhickson 37717a1
#96 add rose-stem tests
jennyhickson 56b4e7e
KGO test setup - INCLUDES temporary filepaths
jennyhickson cc2008b
#96 KGO test setup for aerosols - INCLUDES temporary filepaths
jennyhickson ec00ac7
#96 add check for vertical only and docs
jennyhickson 0142f7b
#96 linting
jennyhickson 712db4b
#96 tidy docs, including fixing linkcheck error
jennyhickson b525331
Merge branch 'main' into 96-vertical-cli
jennyhickson 371f882
#96 add some KGO words
jennyhickson e279062
#96 adjust rose-stem testing with smaller input and UM-file output
jennyhickson 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
This file contains hidden or 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,16 @@ | ||
| .. meta:: | ||
| :description lang=en: ancil_vertical_regrid.py | ||
| :keywords: ANTS, utils | ||
| :property=og:locale: en_GB | ||
|
|
||
| ======================== | ||
| ancil_vertical_regrid.py | ||
| ======================== | ||
|
|
||
| For a full description see :mod:`ants.cli.ancil_vertical_regrid`. | ||
|
|
||
| .. argparse:: | ||
| :module: ants.cli.ancil_vertical_regrid | ||
| :func: _get_parser | ||
| :prog: ancil_vertical_regrid | ||
| :nodescription: |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,171 @@ | ||
| #!/usr/bin/env python | ||
| # (C) Crown Copyright, Met Office. All rights reserved. | ||
| # | ||
| # This file is part of ANTS and is released under the BSD 3-Clause license. | ||
| # See LICENSE.txt in the root of the repository for full licensing details. | ||
| """ | ||
| A vertical regrid application | ||
| ***************************** | ||
|
|
||
| Regrids data from a source to a target grid using | ||
| :class:`ants.regrid.GeneralRegridScheme`. The result is written to an output | ||
| file. The application supports only vertical regridding. The | ||
| regrid algorithm can be specified in the ants configuration file as described | ||
| in :class:`ants.config.GlobalConfiguration`. See :mod:`ants.regrid` for further | ||
| details. | ||
| """ | ||
| import ants | ||
| import ants.io.save as save | ||
| import ants.utils | ||
| from ants.utils.cube import create_time_constrained_cubes | ||
|
|
||
|
|
||
| def load_data( | ||
| source, | ||
| target_grid, | ||
| begin=None, | ||
| end=None, | ||
| ): | ||
| source_cubes = ants.io.load.load(source) | ||
| if begin is not None: | ||
| source_cubes = create_time_constrained_cubes(source_cubes, begin, end) | ||
|
|
||
| target_cube = ants.io.load.load_grid(target_grid) | ||
|
|
||
| check_target(target_cube, source_cubes) | ||
|
|
||
| return source_cubes, target_cube | ||
|
|
||
|
|
||
| def check_target( | ||
| target_cube, | ||
| source_cubes, | ||
| ): | ||
| """ | ||
| Check the target cube against common pitfalls. This application is for | ||
| structured mesh and vertical regridding only. | ||
| """ | ||
| if ants.utils.cube._is_ugrid(target_cube): | ||
| raise ValueError( | ||
| "Target appears to be a UGrid mesh - the regrid to mesh application in " | ||
| "UG-ANTS should be used instead." | ||
| ) | ||
|
|
||
| target_coords = [coord.name() for coord in target_cube.coords()] | ||
|
|
||
| if "latitude" in target_coords: | ||
| for cube in source_cubes: | ||
| if cube.coord("latitude") != target_cube.coord("latitude"): | ||
| raise ValueError( | ||
| "Target grid latitude coordinates do not match source grid " | ||
| "latitude coordinates" | ||
| ) | ||
|
|
||
| if "longitude" in target_coords: | ||
| for cube in source_cubes: | ||
| if cube.coord("longitude") != target_cube.coord("longitude"): | ||
| raise ValueError( | ||
| "Target grid longitude coordinates do not match source grid " | ||
| "longitude coordinates" | ||
| ) | ||
|
|
||
|
|
||
| def regrid(sources, target): | ||
| sources = ants.utils.cube.as_cubelist(sources) | ||
| results = [] | ||
| scheme = ants.regrid.GeneralRegridScheme() | ||
| for source in sources: | ||
| results.append(source.regrid(target, scheme)) | ||
| return results | ||
|
|
||
|
|
||
| def main( | ||
| source_path, | ||
| output_path, | ||
| target_path, | ||
| begin, | ||
| end, | ||
| save_ukca, | ||
| netcdf_only, | ||
| ): | ||
| """ | ||
| Vertical regrid application top level call function. | ||
|
|
||
| Loads source data cubes, regrids them to match target data cube | ||
| co-ordinates, and saves result to output. In addition to writing the | ||
| resulting data cube to disk, also returns the regridded data cube. | ||
|
|
||
| Parameters | ||
| ---------- | ||
|
|
||
| source_path : str | ||
| File path for one or more files which contain the data to be | ||
| regridded. | ||
| target_path : str | ||
| File path for files that provide the grid to which the source data | ||
| cubes will be mapped. e.g. a namelist for vertical levels. | ||
| output_path : str | ||
| Output file path to write the regridded data to. | ||
| begin : :obj:`datetime`, optional | ||
| If provided, all source data prior to this year is discarded. Default is to | ||
| include all source data. | ||
| end : :obj:`datetime`, optional | ||
| If provided, all source data after this year is discarded. Default is to | ||
| include all source data. | ||
|
|
||
|
|
||
| Returns | ||
| ------- | ||
| : :class:`~iris.cube.Cube` | ||
| A single data cube with the regridded data. | ||
|
|
||
| """ | ||
| source_cubes, target_cube = load_data( | ||
| source_path, | ||
| target_path, | ||
| begin, | ||
| end, | ||
| ) | ||
|
|
||
| regridded_cubes = regrid(source_cubes, target_cube) | ||
|
|
||
| if save_ukca: | ||
| save.ukca_netcdf(regridded_cubes, output_path) | ||
| else: | ||
| if not netcdf_only: | ||
| save.ancil(regridded_cubes, output_path) | ||
| save.netcdf(regridded_cubes, output_path) | ||
|
|
||
| print(regridded_cubes) | ||
| return regridded_cubes | ||
|
|
||
|
|
||
| def _get_parser(): | ||
| parser = ants.AntsArgParser(target_grid=True, time_constraints=True) | ||
| parser.add_argument( | ||
| "--save-ukca", | ||
| action="store_true", | ||
| help="Save to a UKCA-specific netCDF file", | ||
| required=False, | ||
| ) | ||
| return parser | ||
|
|
||
|
|
||
| def cli_interface(): | ||
| parser = _get_parser() | ||
| args = parser.parse_args() | ||
|
|
||
| source = args.sources | ||
| main( | ||
| source, | ||
| args.output, | ||
| args.target_grid, | ||
| args.begin, | ||
| args.end, | ||
| args.save_ukca, | ||
| args.netcdf_only, | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| cli_interface() | ||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -178,7 +178,11 @@ def scalar_coord(cube, coord_name): | |
|
|
||
|
|
||
| def _guess_axis_interpolation(cube): | ||
| dims = cube.coord_dims("model_level_number") | ||
| try: | ||
| dims = cube.coord_dims("model_level_number") | ||
| except iris.exceptions.CoordinateNotFoundError: | ||
| dims = cube.coord_dims("altitude") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Possibly a silly question, but is that because there isn't a model level number for regridding vertically or some other reason? |
||
|
|
||
| if len(dims) != 1: | ||
| raise ValueError( | ||
| "Expecting only a single axis of interpolation, " "got {}".format(len(dims)) | ||
|
|
||
This file contains hidden or 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
5 changes: 5 additions & 0 deletions
5
rose-stem/app/ancil_vertical_regrid/opt/rose-app-aerosol_3d.conf
This file contains hidden or 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,5 @@ | ||
| [env] | ||
| output=${ROSE_DATA}/${ROSE_TASK_NAME} | ||
| source=/data/users/jennifer.hickson/ants_vertical_cli/ancil_vertical_regrid_aerosol_3d/seasalt_n48.nc | ||
| target=/data/users/jennifer.hickson/ants_vertical_cli/vertlevs_L70_50t_20s_80km | ||
| target_type=grid |
5 changes: 5 additions & 0 deletions
5
rose-stem/app/ancil_vertical_regrid/opt/rose-app-ozone_zonal_mean.conf
This file contains hidden or 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,5 @@ | ||
| [env] | ||
| output=${ROSE_DATA}/${ROSE_TASK_NAME} | ||
| source=/data/users/jennifer.hickson/ants_vertical_cli/ancil_vertical_regrid_ozone_zonal_mean/Ozone_CMIP5_SPARCex_M1994-2005_N36L85_li-ch_G.nc | ||
| target=/data/users/jennifer.hickson/ants_vertical_cli/vertlevs_L70_50t_20s_80km | ||
| target_type=grid |
This file contains hidden or 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,17 @@ | ||
| [ants_logging] | ||
| enabled=True | ||
|
|
||
| [ants_metadata] | ||
| history=${CYLC_WORKFLOW_ID}@${ROSE_SUITE_REVISION}:${ROSE_TASK_NAME} | ||
|
|
||
| [ants_regridding_vertical] | ||
| scheme=Linear | ||
|
|
||
|
|
||
| [command] | ||
| default=ants-launch ancil_vertical_regrid.py \ | ||
| =${source} -o ${output} --ants-config ${ANTS_CONFIG} --target-${target_type} ${target} \ | ||
| =${begin} ${end} | ||
|
|
||
| [env] | ||
| ANTS_CONFIG=rose-app-run.conf |
This file contains hidden or 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,6 @@ | ||
| [env] | ||
| filelist=ancil_vertical_regrid_ozone_zonal_mean | ||
| ancil_vertical_regrid_ozone_zonal_mean.nc | ||
| ancil_vertical_regrid_aerosol_3d | ||
| ancil_vertical_regrid_aerosol_3d.nc | ||
|
|
This file contains hidden or 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
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.
Unless you specifically want this print statement?