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

Fix global mean calculation in Google Cloud notebook #72

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Changes from all commits
Commits
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
35 changes: 24 additions & 11 deletions notebooks/foundations/google-cloud-basic.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Plot the Data\n",
"\n",
"Plot a map from a specific date:"
]
},
Expand All @@ -193,28 +195,38 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a timeseries of global-average surface air temperature. For this we need the area weighting factor for each gridpoint."
"The global mean of a lat-lon field needs to be weighted by the area of each grid cell, which is proportional to the cosine of its latitude."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"df_area = df.query(\"variable_id == 'areacella' & source_id == 'CESM2'\")\n",
"ds_area = xr.open_zarr(fsspec.get_mapper(df_area.zstore.values[0]), consolidated=True)\n",
"ds_area"
"def global_mean(field):\n",
" weights = np.cos(np.deg2rad(field.lat))\n",
" return field.weighted(weights).mean(dim=['lat', 'lon'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can pass all of the temperature data through this function:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"total_area = ds_area.areacella.sum(dim=['lon', 'lat'])\n",
"ta_timeseries = (ds.tas * ds_area.areacella).sum(dim=['lon', 'lat']) / total_area\n",
"ta_timeseries = global_mean(ds.tas)\n",
"ta_timeseries"
]
},
Expand All @@ -241,8 +253,9 @@
"outputs": [],
"source": [
"ta_timeseries.plot(label='monthly')\n",
"ta_timeseries.rolling(time=12).mean().plot(label='12 month rolling mean')\n",
"ta_timeseries.rolling(time=12).mean().plot(label='12 month rolling mean', color='k')\n",
"plt.legend()\n",
"plt.grid()\n",
"plt.title('Global Mean Surface Air Temperature')"
]
},
Expand All @@ -258,7 +271,7 @@
"metadata": {},
"source": [
"## Summary\n",
"In this notebook, we opened a CESM2 dataset with `fsspec` and `zarr`. We also used a cell area dataset to calculate and plot global average surface air temperature. \n",
"In this notebook, we opened a CESM2 dataset with `fsspec` and `zarr`. We calculated and plotted global average surface air temperature. \n",
"\n",
"### What's next?\n",
"We will open a dataset with ESGF and OPenDAP."
Expand Down Expand Up @@ -291,7 +304,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
"version": "3.10.12"
},
"nbdime-conflicts": {
"local_diff": [
Expand Down
Loading