generated from ACCESS-NRI/template
-
Notifications
You must be signed in to change notification settings - Fork 0
Add a pre-industrial nitrogen example #104
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b9acda6
Add a pre-industrial nitrogen example
penguian b93a801
Rename and improve the nitrogen example
penguian 6c42413
Add scaffold code for nitrogen forcing
penguian ab384d6
Add forgotten args argument to save_cmip7_pi_nitrogen
penguian bee78a9
Add forgotten args argument to fix_coords
penguian ded4df8
Use arg.ancil_target_dirname
penguian 32d30d4
Use esm_pi_nitrogen_save_dirpath
penguian a34c24e
Change the units and STASH code to match ACCESS-ESM1.6
penguian c0f8638
In the example notebook, change the units and STASH code to match ACC…
penguian 35ebbcb
Remove trailing whitespace
penguian 24900f4
Use STASH code m01s00i884
penguian 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
20 changes: 20 additions & 0 deletions
20
CMIP7/esm1p6/atmosphere/nitrogen/cmip7_HI_nitrogen_generate.py
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,20 @@ | ||
| from argparse import ArgumentParser | ||
|
|
||
| from cmip7_ancil_argparse import common_parser | ||
|
|
||
|
|
||
| def parse_args(): | ||
| parser = ArgumentParser( | ||
| parents=[common_parser()], | ||
| prog="cmip7_HI_nitrogen_generate", | ||
| description=( | ||
| "Generate input files from CMIP7 historical nitrogen forcings" | ||
| ), | ||
| ) | ||
| parser.add_argument("--dataset-date-range-list") | ||
| parser.add_argument("--save-filename") | ||
| return parser.parse_args() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| args = parse_args() |
109 changes: 109 additions & 0 deletions
109
CMIP7/esm1p6/atmosphere/nitrogen/cmip7_PI_nitrogen_generate.py
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,109 @@ | ||
| from argparse import ArgumentParser | ||
| from pathlib import Path | ||
|
|
||
| import iris | ||
| from cmip7_ancil_argparse import common_parser | ||
| from cmip7_ancil_common import ( | ||
| INTERPOLATION_SCHEME, | ||
| esm_grid_mask_cube, | ||
| fix_coords, | ||
| save_ancil, | ||
| ) | ||
| from cmip7_ancil_constants import ANCIL_TODAY | ||
| from iris.util import equalise_attributes | ||
| from nitrogen.cmip7_nitrogen import ( | ||
| NITROGEN_SPECIES, | ||
| NITROGEN_STASH_ITEM, | ||
| cmip7_nitrogen_dirpath, | ||
| ) | ||
|
|
||
|
|
||
| def parse_args(): | ||
| parser = ArgumentParser( | ||
| parents=[common_parser()], | ||
| prog="cmip7_PI_nitrogen_generate", | ||
| description=( | ||
| "Generate input files from CMIP7 pre-industrial nitrogen forcings" | ||
| ), | ||
| ) | ||
| parser.add_argument("--dataset-date-range") | ||
| parser.add_argument("--save-filename") | ||
| return parser.parse_args() | ||
|
|
||
|
|
||
| def cmip7_pi_nitrogen_filepath(args, species): | ||
| dirpath = cmip7_nitrogen_dirpath(args, "monC", species) | ||
| filename = ( | ||
| f"{species}_input4MIPs_surfaceFluxes_CMIP_" | ||
| f"{args.dataset_version}_gn_" | ||
| f"{args.dataset_date_range}-clim.nc" | ||
| ) | ||
| return dirpath / filename | ||
|
|
||
|
|
||
| def load_cmip7_pi_nitrogen_species(args, species): | ||
| species_filepath = cmip7_pi_nitrogen_filepath(args, species) | ||
| cube = iris.load_cube(species_filepath) | ||
| return cube | ||
|
|
||
|
|
||
| def load_cmip7_pi_nitrogen(args): | ||
| # Load all of the PI nitrogen datasets into a CubeList | ||
| nitrogen_cubes = iris.cube.CubeList() | ||
| for species in NITROGEN_SPECIES: | ||
| species_cube = load_cmip7_pi_nitrogen_species(args, species) | ||
| nitrogen_cubes.append(species_cube) | ||
| # Remove all attributes that differ between cubes | ||
| equalise_attributes(nitrogen_cubes) | ||
| # Add the cubes together | ||
| cube_tot = nitrogen_cubes[0].copy() | ||
| for cube_nbr in range(1, 4): | ||
| iris.analysis.maths.add( | ||
| cube_tot, nitrogen_cubes[cube_nbr], in_place=True | ||
| ) | ||
atteggiani marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # Change the units from kg m-2 s-1 to g m-2 day-1 | ||
| cube_tot.convert_units("g m-2 day-1") | ||
| return cube_tot | ||
|
|
||
|
|
||
| def regrid_cmip7_pi_nitrogen(args, cube): | ||
| # Make the coordinates comaptible with the ESM1.5 grid mask | ||
| fix_coords(args, cube) | ||
| # Regrid using the ESM1.5 grid mask | ||
| esm_cube = cube.regrid(esm_grid_mask_cube(args), INTERPOLATION_SCHEME) | ||
| esm_cube.data = esm_cube.data.filled(0.0) | ||
| return esm_cube | ||
|
|
||
|
|
||
| def esm_pi_nitrogen_save_dirpath(args): | ||
| return ( | ||
| Path(args.ancil_target_dirname) | ||
| / "modern" | ||
| / "pre-industrial" | ||
| / "atmosphere" | ||
| / "land" | ||
| / "biogeochemistry" | ||
| / args.esm_grid_rel_dirname | ||
| / ANCIL_TODAY | ||
| ) | ||
|
|
||
|
|
||
| def save_cmip7_pi_nitrogen(args, cube): | ||
| # Add STASH metadata | ||
| cube.attributes["STASH"] = iris.fileformats.pp.STASH( | ||
| model=1, section=0, item=NITROGEN_STASH_ITEM | ||
| ) | ||
| # Save as an ancillary file | ||
| save_dirpath = esm_pi_nitrogen_save_dirpath(args) | ||
| save_ancil(cube, save_dirpath, args.save_filename) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| args = parse_args() | ||
|
|
||
| # Load the CMIP7 datasets | ||
| nitrogen_cube = load_cmip7_pi_nitrogen(args) | ||
| # Regrid to match the ESM1.5 mask | ||
| esm_cube = regrid_cmip7_pi_nitrogen(args, nitrogen_cube) | ||
| # Save the ancillary | ||
| save_cmip7_pi_nitrogen(args, esm_cube) | ||
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,21 @@ | ||
| from pathlib import Path | ||
|
|
||
| NITROGEN_SPECIES = ("drynhx", "drynoy", "wetnhx", "wetnoy") | ||
| """ | ||
| The STASH code to use is m01s00i884 NITROGEN DEPOSITION as per | ||
| https://github.com/ACCESS-NRI/access-esm1.6-configs/blob/dev-preindustrial%2Bconcentrations/atmosphere/prefix.PRESM_A#L711 | ||
| """ | ||
| NITROGEN_STASH_ITEM = 884 | ||
|
|
||
|
|
||
| def cmip7_nitrogen_dirpath(args, period, species): | ||
| return ( | ||
| Path(args.cmip7_source_data_dirname) | ||
| / "FZJ" | ||
| / args.dataset_version | ||
| / "atmos" | ||
| / period | ||
| / species | ||
| / "gn" | ||
| / args.dataset_vdate | ||
| ) |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.