Skip to content
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

Make a version of the notebooks for the AFR-22 domain #136

Merged
merged 24 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
e514778
add copies of existing notebooks in new directory
nhsavage Apr 22, 2022
963aa1b
Gitignore issue fixed
nhsavage Apr 22, 2022
9eabd4d
Gitignore issue fixed
nhsavage Apr 22, 2022
b4c3e72
modified worksheet 1 for AFR domain
nhsavage Apr 22, 2022
9ce40c7
add changes for worksheet 2
nhsavage Apr 22, 2022
47590a5
update worksheet 3
nhsavage Apr 22, 2022
54de3b7
Add changes to makedata notebook
nhsavage Apr 25, 2022
9954daa
update worksheet 4 for AFR-22 and some other fixes
nhsavage Apr 26, 2022
927198c
finish sec 5.1
nhsavage Apr 26, 2022
0b1be25
Final changes, including bug fix for 1mm threshold
nhsavage Apr 29, 2022
286ddde
don't include soft link
nhsavage May 5, 2022
167d115
Start new module zero on basic Python
nhsavage May 7, 2022
a4b6101
add section on for loops
nhsavage May 7, 2022
9b45521
complete worksheet 0
nhsavage May 7, 2022
cc22230
update environment, create portable lock file from it
nhsavage May 10, 2022
afafb6d
save to HISTDIR in WS2 for consistency
Aug 4, 2022
d83673d
remove print statement
Aug 4, 2022
3defd94
clear cell output
rosannaamato Aug 4, 2022
ab0d48b
fix wet day count to >1mm (see #140)
rosannaamato Aug 5, 2022
72fed13
model_wetdays in correct place - period loop
rosannaamato Aug 5, 2022
d7f9ce6
Merge branch 'develop' into new/afr_22
nhsavage Sep 26, 2022
ff2eb31
fixes to makedata from review #136
nhsavage Sep 26, 2022
62d3b46
make rsync and link on MO desktop work properly
nhsavage Sep 26, 2022
26ea6d5
fix wildcard to match a file that actually exists
nhsavage Sep 26, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
notebooks/.ipynb_checkpoints/
/data/stash_select
notebooks/data_v2/
notebooks/AFR-22/data_afr22/
*.pyc
.idea
2 changes: 2 additions & 0 deletions environment.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
name: pyprecis-environment
channels:
- conda-forge
- conda-main
dependencies:
- python=3.6.10
- iris=2.4.0
- iris-sample-data
- numpy=1.17.4
- matplotlib=3.1.3
- nc-time-axis=1.2.0
Expand Down
1 change: 1 addition & 0 deletions notebooks/AFR-22/img
230 changes: 230 additions & 0 deletions notebooks/AFR-22/makedata.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import glob\n",
"import os\n",
"import iris\n",
"from iris.time import PartialDateTime\n",
"from iris.experimental.equalise_cubes import equalise_attributes\n",
"\n",
"DATADIR = '/project/ciid/projects/PRECIS/worksheets/data_afr22/AFR-22/'\n",
"# write outputs to own directory so that can be checked before upload under ciid\n",
"OUTDIR = f'{os.getenv(\"SCRATCH\")}/cordex_inputs/'\n",
"retval = os.system('mkdir -p '+OUTDIR)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Make data for worksheet 4"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Prep future data (2041-2060) from ESGF CORDEX files\n",
"# Note this assumes that only want to process the REMO2015 data.\n",
"\n",
"shortnames = {'MOHC-HadGEM2-ES':'hadgem2-es', 'MPI-M-MPI-ESM-LR':'mpi-esm-lr'}\n",
"\n",
"# Make future 2021-2050 data\n",
"future_time_constraint = iris.Constraint(time=lambda cell: PartialDateTime(year=2041) \n",
" <= cell.point <= PartialDateTime(year=2060))\n",
"for gcmid in ['MOHC-HadGEM2-ES', 'MPI-M-MPI-ESM-LR']:\n",
"\n",
" # set a short name consistent with what used in the exercises [would it be better to use full names?]\n",
" shortgcm = shortnames[gcmid]\n",
" for var in ['pr', 'tas']:\n",
"\n",
" # find all the files to consolidate \n",
" flist = glob.glob(DATADIR + f'{var}_*_{gcmid}_rcp85_*_GERICS-REMO2015_*_mon_*.nc')\n",
" print (f'input files: {flist}')\n",
"\n",
" # merge the cubes\n",
" datacubes = iris.load(flist, future_time_constraint)\n",
" equalise_attributes(datacubes)\n",
" cube = datacubes.concatenate_cube()\n",
" \n",
" if cube.name() == 'air_temperature':\n",
" cube.convert_units('celsius')\n",
" outpath = os.path.join(OUTDIR, 'future', shortgcm + '.mon.2041_2060.GERICS-REMO2015.tm.C.nc')\n",
" iris.save(cube, outpath)\n",
" if cube.name() == 'precipitation_flux':\n",
" cube.convert_units('kg m-2 day-1')\n",
" cube.units = 'mm day-1'\n",
" outpath = os.path.join(OUTDIR, 'future', shortgcm + '.mon.2041_2060.GERICS-REMO2015.pr.mmday-1.nc')\n",
" iris.save(cube, outpath)\n",
" print(f'saved file: {outpath}')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Prep baseline data (1986-2005) from \n",
"historical_time_constraint = iris.Constraint(time=lambda cell: PartialDateTime(year=1986) \n",
" <= cell.point <= PartialDateTime(year=2005))\n",
"for gcmid in ['MOHC-HadGEM2-ES', 'MPI-M-MPI-ESM-LR']:\n",
"\n",
" # set a short name consistent with what used in the exercises [would it be better to use full names?]\n",
" shortgcm = shortnames[gcmid]\n",
" for var in ['pr', 'tas']:\n",
"\n",
" # find all the files to consolidate \n",
" flist = glob.glob(DATADIR + f'{var}_*_{gcmid}_historical_*_GERICS-REMO2015_*_mon_*.nc')\n",
" flist = glob.glob(DATADIR + f'{var}_*_{gcmid}_historical_*_GERICS-REMO2015_*_mon_*.nc')\n",
" print (f'input files: {flist}')\n",
"\n",
" # merge the cubes\n",
" datacubes = iris.load(flist, historical_time_constraint)\n",
" equalise_attributes(datacubes)\n",
" cube = datacubes.concatenate_cube()\n",
" \n",
" if cube.name() == 'air_temperature':\n",
" cube.convert_units('celsius')\n",
" outpath = os.path.join(OUTDIR, 'historical', shortgcm + '.mon.1986_2005.GERICS-REMO2015.tm.C.nc')\n",
" iris.save(cube, outpath)\n",
" if cube.name() == 'precipitation_flux':\n",
" cube.convert_units('kg m-2 day-1')\n",
" cube.units = 'mm day-1'\n",
" outpath = os.path.join(OUTDIR, 'historical', shortgcm + '.mon.1986_2005.GERICS-REMO2015.pr.mmday-1.nc')\n",
" iris.save(cube, outpath)\n",
" print(f'saved file: {outpath}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Make data for Worksheet 5"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The files needed are daily precipitation, with the two periods (historical and future) consolidated into a single file\n",
"\n",
"Note that these datasets should be saved with compression to reduce their file size. If for some reason this needs to be done during a PRECIS course, it is quicker to save them without compression using `iris.save(cube, outfile)` but the filesize is significantly larger.\n",
"\n",
"Unlike the previous PRECIS data, the memory requirements of this code are modest (probably due to the lack of rim removal meaning the data remains lazy). It does take several minutes (~5) to run each example."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Prep future data (2041-2060) from CORDEX files\n",
"\n",
"shortnames = {'MOHC-HadGEM2-ES':'hadgem2-es', 'MPI-M-MPI-ESM-LR':'mpi-esm-lr'}\n",
"\n",
"# Make future 2041-2060 data\n",
"future_time_constraint = iris.Constraint(time=lambda cell: PartialDateTime(year=2041) \n",
" <= cell.point <= PartialDateTime(year=2060))\n",
"for gcmid in ['MOHC-HadGEM2-ES', 'MPI-M-MPI-ESM-LR']:\n",
"\n",
" # set a short name consistent with what used in the exercises [would it be better to use full names?]\n",
" shortgcm = shortnames[gcmid]\n",
"\n",
" # find all the files \n",
" flist = glob.glob(DATADIR + f'pr_AFR-22_{gcmid}_rcp85_*_GERICS-REMO2015_*_day_*.nc')\n",
" print (f'input files: {flist}')\n",
" \n",
" # load using time constraint above\n",
" datacubes = iris.load(flist, future_time_constraint)\n",
"\n",
" # merge the cubes\n",
" equalise_attributes(datacubes)\n",
" cube = datacubes.concatenate_cube()\n",
" \n",
" if cube.name() == 'air_temperature':\n",
" cube.convert_units('celsius')\n",
" outpath = os.path.join(OUTDIR, 'future', shortgcm + '.day.2041_2060.GERICS-REMO2015.tm.C.nc')\n",
" iris.save(cube, outpath, least_significant_digit=3, \n",
" complevel=4, zlib=True)\n",
" if cube.name() == 'precipitation_flux':\n",
" cube.convert_units('kg m-2 day-1')\n",
" cube.units = 'mm day-1'\n",
" outpath = os.path.join(OUTDIR, 'future', shortgcm + '.day.2041_2060.GERICS-REMO2015.pr.mmday-1.nc')\n",
" iris.save(cube, outpath, least_significant_digit=3, \n",
" complevel=4, zlib=True)\n",
" print(f'saved file: {outpath}')\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Prep baseline data (1986-2006) from CORDEX files\n",
"historical_time_constraint = iris.Constraint(time=lambda cell: PartialDateTime(year=1986) \n",
" <= cell.point <= PartialDateTime(year=2005))\n",
"# Make baseline data\n",
"for gcmid in ['MOHC-HadGEM2-ES', 'MPI-M-MPI-ESM-LR']:\n",
"\n",
" # set a short name consistent with what used in the exercises [would it be better to use full names?]\n",
" shortgcm = shortnames[gcmid]\n",
"\n",
" # find all the files \n",
" flist = glob.glob(DATADIR + f'pr_AFR-22_{gcmid}_historical_*_GERICS-REMO2015_*_day_*.nc')\n",
" print (f'input files: {flist}')\n",
" \n",
" # load using time constraint above\n",
" datacubes = iris.load(flist, historical_time_constraint)\n",
"\n",
" # merge the cubes\n",
" equalise_attributes(datacubes)\n",
" cube = datacubes.concatenate_cube()\n",
" \n",
" if cube.name() == 'air_temperature':\n",
" cube.convert_units('celsius')\n",
" outpath = os.path.join(OUTDIR, 'historical', shortgcm + '.day.1986_2005.GERICS-REMO2015.tm.C.nc')\n",
" iris.save(cube, outpath, least_significant_digit=3, \n",
" complevel=4, zlib=True)\n",
" if cube.name() == 'precipitation_flux':\n",
" cube.convert_units('kg m-2 day-1')\n",
" cube.units = 'mm day-1'\n",
" outpath = os.path.join(OUTDIR, 'historical', shortgcm + '.day.1986_2005.GERICS-REMO2015.pr.mmday-1.nc')\n",
" iris.save(cube, outpath, least_significant_digit=3, \n",
" complevel=4, zlib=True)\n",
" print(f'saved file: {outpath}')\n",
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "pyprecis-environment",
"language": "python",
"name": "pyprecis-environment"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.10"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading