Skip to content

Commit 80a9653

Browse files
author
Diana
committed
add more debugging and validation notebooks
1 parent 75c11da commit 80a9653

File tree

6 files changed

+5775
-0
lines changed

6 files changed

+5775
-0
lines changed
Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"%matplotlib inline\n",
10+
"import intake\n",
11+
"import xarray as xr\n",
12+
"import os \n",
13+
"import pandas as pd\n",
14+
"import numpy as np\n",
15+
"import zarr \n",
16+
"import rhg_compute_tools.kubernetes as rhgk\n",
17+
"\n",
18+
"import matplotlib.pyplot as plt\n",
19+
"\n",
20+
"import re\n",
21+
"import yaml\n",
22+
"\n",
23+
"import ast\n",
24+
"\n",
25+
"import warnings "
26+
]
27+
},
28+
{
29+
"cell_type": "code",
30+
"execution_count": 2,
31+
"metadata": {},
32+
"outputs": [
33+
{
34+
"data": {
35+
"text/plain": [
36+
"'client, cluster = rhgk.get_standard_cluster()\\ncluster'"
37+
]
38+
},
39+
"execution_count": 2,
40+
"metadata": {},
41+
"output_type": "execute_result"
42+
}
43+
],
44+
"source": [
45+
"'''client, cluster = rhgk.get_standard_cluster()\n",
46+
"cluster'''"
47+
]
48+
},
49+
{
50+
"cell_type": "code",
51+
"execution_count": 3,
52+
"metadata": {},
53+
"outputs": [],
54+
"source": [
55+
"col = intake.open_esm_datastore(\"https://storage.googleapis.com/cmip6/pangeo-cmip6.json\")"
56+
]
57+
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": 4,
61+
"metadata": {},
62+
"outputs": [],
63+
"source": [
64+
"def _paramfile_to_tuple(model, variable):\n",
65+
" \"\"\"\n",
66+
" takes in a model and variable, returns tuple from parameter file. \n",
67+
" \"\"\"\n",
68+
" param_file = '/home/jovyan/downscaling/downscale/workflows/parameters/{}-{}.yaml'.format(model, variable)\n",
69+
" with open(param_file, 'r') as f:\n",
70+
" var_dict = yaml.full_load(f)\n",
71+
" # some string parsing \n",
72+
" line = var_dict['jobs']\n",
73+
" line1 = re.sub(r\"\\n\", \"\", line)\n",
74+
" line2 = re.sub(r\"[\\[\\]]\", \"\", line1)\n",
75+
" return ast.literal_eval(line2.strip())\n",
76+
"\n",
77+
"def _get_cmip6_dataset(model, variable, tuple_id, period='ssp'):\n",
78+
" d_ssp = _paramfile_to_tuple(model, variable)[tuple_id][period]\n",
79+
" cat = col.search(\n",
80+
" activity_id=d_ssp['activity_id'],\n",
81+
" experiment_id=d_ssp['experiment_id'],\n",
82+
" table_id=d_ssp['table_id'],\n",
83+
" variable_id=d_ssp['variable_id'],\n",
84+
" source_id=d_ssp['source_id'],\n",
85+
" member_id=d_ssp['member_id'],\n",
86+
" grid_label=d_ssp['grid_label'],\n",
87+
" version=int(d_ssp['version']),\n",
88+
" )\n",
89+
" return cat.to_dataset_dict(progressbar=False)\n",
90+
"\n",
91+
"def compute_dtr(model, tuple_id=1):\n",
92+
" \"\"\"\n",
93+
" takes in tasmax and tasmin Datasets, computes DTR (returns it lazily)\n",
94+
" \"\"\"\n",
95+
" tasmax = _get_cmip6_dataset(model, 'tasmax', tuple_id)\n",
96+
" k_tasmax = list(tasmax.keys())\n",
97+
" if len(k_tasmax) != 1:\n",
98+
" raise ValueError(\"there is likely an issue with {} tasmax\".format(model))\n",
99+
" tasmin = _get_cmip6_dataset(model, 'tasmin', tuple_id)\n",
100+
" k_tasmin = list(tasmin.keys())\n",
101+
" if len(k_tasmin) != 1:\n",
102+
" raise ValueError(\"there is likely an issue with {} tasmin\".format(model))\n",
103+
" return tasmax[k_tasmax[0]]['tasmax'] - tasmin[k_tasmin[0]]['tasmin'] \n",
104+
"\n",
105+
"def check_dtr(dtr, model):\n",
106+
" \"\"\"\n",
107+
" \"\"\"\n",
108+
" min_dtr = dtr.min('time')\n",
109+
" neg_count = min_dtr.where(min_dtr <= 0).count().values\n",
110+
" if neg_count > 0:\n",
111+
" warnings.warn(\"DTR has negative values for {}\".format(model))"
112+
]
113+
},
114+
{
115+
"cell_type": "markdown",
116+
"metadata": {},
117+
"source": [
118+
"checking models "
119+
]
120+
},
121+
{
122+
"cell_type": "markdown",
123+
"metadata": {},
124+
"source": [
125+
"DTR negative: \n",
126+
"- GFDL-ESM4\n",
127+
"- GFDL-CM4\n",
128+
"\n",
129+
"DTR positive: \n",
130+
"- CanESM5\n",
131+
"- INM-CM4-8\n",
132+
"- INM-CM5-0\n",
133+
"- NorESM2-MM\n",
134+
"- NorESM2-LM\n",
135+
"- MIROC6\n",
136+
"- EC-Earth3-Veg-LR\n",
137+
"- EC-Earth3-Veg\n",
138+
"- EC-Earth3\n",
139+
"- KIOST-ESM\n",
140+
"- MIROC-ES2L\n",
141+
"- MPI-ESM1-2-LR\n",
142+
"- MPI-ESM1-2-HR\n",
143+
"- NESM3\n",
144+
"- MRI-ESM2-0\n",
145+
"- FGOALS-g3\n",
146+
"- CMCC-ESM2\n",
147+
"- BCC-CSM2-MR\n",
148+
"- AWI-CM-1-1-MR\n",
149+
"- ACCESS-CM2\n",
150+
"\n",
151+
"Parameter files to add or fix (could not check DTR): \n",
152+
"- UKESM1-0-LL\n",
153+
"- ACCESS-ESM1-5\n",
154+
"- MPI-ESM1-2-HAM\n",
155+
"\n",
156+
"Tasmin parameter files to add (could not check DTR): \n",
157+
"- CAMS-CSM1-0"
158+
]
159+
},
160+
{
161+
"cell_type": "code",
162+
"execution_count": 5,
163+
"metadata": {},
164+
"outputs": [],
165+
"source": [
166+
"model = 'NorESM2-MM'"
167+
]
168+
},
169+
{
170+
"cell_type": "code",
171+
"execution_count": 8,
172+
"metadata": {},
173+
"outputs": [],
174+
"source": [
175+
"# _get_cmip6_dataset(model, 'tasmax', 0)"
176+
]
177+
},
178+
{
179+
"cell_type": "code",
180+
"execution_count": 6,
181+
"metadata": {},
182+
"outputs": [],
183+
"source": [
184+
"dtr = compute_dtr(model, tuple_id=0)"
185+
]
186+
},
187+
{
188+
"cell_type": "code",
189+
"execution_count": 39,
190+
"metadata": {},
191+
"outputs": [],
192+
"source": [
193+
"check_dtr(dtr, model)"
194+
]
195+
},
196+
{
197+
"cell_type": "code",
198+
"execution_count": null,
199+
"metadata": {},
200+
"outputs": [],
201+
"source": []
202+
}
203+
],
204+
"metadata": {
205+
"kernelspec": {
206+
"display_name": "Python 3",
207+
"language": "python",
208+
"name": "python3"
209+
},
210+
"language_info": {
211+
"codemirror_mode": {
212+
"name": "ipython",
213+
"version": 3
214+
},
215+
"file_extension": ".py",
216+
"mimetype": "text/x-python",
217+
"name": "python",
218+
"nbconvert_exporter": "python",
219+
"pygments_lexer": "ipython3",
220+
"version": "3.8.6"
221+
},
222+
"widgets": {
223+
"application/vnd.jupyter.widget-state+json": {
224+
"state": {},
225+
"version_major": 2,
226+
"version_minor": 0
227+
}
228+
}
229+
},
230+
"nbformat": 4,
231+
"nbformat_minor": 4
232+
}

0 commit comments

Comments
 (0)