Skip to content

Commit a38da97

Browse files
committed
cleanup
1 parent c4b3447 commit a38da97

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

notebooks/wp5/xch4_lev2_growth_rates.ipynb

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"outputs": [],
5050
"source": [
5151
"year_start = 2004\n",
52-
"year_stop = 2022"
52+
"year_stop = 2021"
5353
]
5454
},
5555
{
@@ -73,7 +73,7 @@
7373
" \"variable\": \"xch4\",\n",
7474
" \"sensor_and_algorithm\": \"merged_emma\",\n",
7575
" \"version\": \"4_5\",\n",
76-
" \"year\": [str(year) for year in range(year_start, year_stop + 1)],\n",
76+
" \"year\": [str(year) for year in range(year_start - 1, year_stop + 2)],\n",
7777
" \"month\": [f\"{i:02d}\" for i in range(1, 13)],\n",
7878
" \"day\": [f\"{i:02d}\" for i in range(1, 32)],\n",
7979
"}"
@@ -94,9 +94,17 @@
9494
"metadata": {},
9595
"outputs": [],
9696
"source": [
97-
"def spatial_weighted_mean(ds, dim=None):\n",
97+
"def weight_dataset(ds):\n",
9898
" weights = np.abs(np.cos(np.deg2rad(ds[\"latitude\"])))\n",
99-
" return ds.weighted(weights).mean(dim, keep_attrs=True)\n",
99+
" return ds.weighted(weights)\n",
100+
"\n",
101+
"\n",
102+
"def spatial_weighted_mean(ds, dim=None):\n",
103+
" return weight_dataset(ds).mean(dim, keep_attrs=True)\n",
104+
"\n",
105+
"\n",
106+
"def spatial_weighted_std(ds, dim=None):\n",
107+
" return weight_dataset(ds).std(dim, keep_attrs=True)\n",
100108
"\n",
101109
"\n",
102110
"def regrid(ds, d_lon, d_lat, lon1=180):\n",
@@ -199,8 +207,10 @@
199207
"dataarrays = []\n",
200208
"for zonal_first in [True, False]:\n",
201209
" da = compute_growth_rate(ds, zonal_first=zonal_first)\n",
202-
" dataarrays.append(da.expand_dims(zonal_first=[zonal_first]))\n",
203-
"da = xr.concat(dataarrays, \"zonal_first\")"
210+
" dataarrays.append(\n",
211+
" da.expand_dims(method=[\"Zonal-first\" if zonal_first else \"Standard\"])\n",
212+
" )\n",
213+
"da = xr.concat(dataarrays, \"method\").sel(time=slice(str(year_start), str(year_stop)))"
204214
]
205215
},
206216
{
@@ -218,7 +228,7 @@
218228
"metadata": {},
219229
"outputs": [],
220230
"source": [
221-
"facet = da.plot(col=\"latitude\", col_wrap=3, hue=\"zonal_first\")\n",
231+
"facet = da.plot(col=\"latitude\", col_wrap=3, hue=\"method\")\n",
222232
"for ax in facet.axs.flatten():\n",
223233
" ax.grid()\n",
224234
" for label in ax.get_xticklabels():\n",
@@ -240,11 +250,14 @@
240250
"metadata": {},
241251
"outputs": [],
242252
"source": [
243-
"da_year = da.groupby(\"time.year\").map(spatial_weighted_mean, dim=[\"time\", \"latitude\"])\n",
244-
"df_year = da_year.to_pandas()\n",
245-
"ax = df_year.T.plot.bar()\n",
253+
"grouped = da.groupby(\"time.year\")\n",
254+
"da_mean = grouped.map(spatial_weighted_mean, dim=[\"time\", \"latitude\"])\n",
255+
"da_std = grouped.map(spatial_weighted_std, dim=[\"time\", \"latitude\"])\n",
256+
"df_mean = da_mean.to_pandas().T\n",
257+
"df_std = da_std.to_pandas().T\n",
258+
"ax = df_mean.plot.bar(yerr=df_std)\n",
246259
"ax.grid()\n",
247-
"_ = ax.set_ylabel(f\"{da_year.long_name} [{da_year.units}]\")"
260+
"_ = ax.set_ylabel(f\"{da_mean.long_name} [{da_mean.units}]\")"
248261
]
249262
}
250263
],

0 commit comments

Comments
 (0)