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 notebook conventions #476

Merged
merged 2 commits into from
Oct 10, 2024
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
65 changes: 27 additions & 38 deletions scripts/global_tas_average_obs.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,8 @@
},
"outputs": [],
"source": [
"import os\n",
"import re\n",
"import sys\n",
"\n",
"os.environ[\"ESMFMKFILE\"] = os.path.join(sys.prefix, \"lib\", \"esmf.mk\")\n",
"from datetime import datetime, timedelta\n",
"from pathlib import Path\n",
"\n",
"import dask\n",
"import matplotlib.pyplot as plt\n",
Expand All @@ -58,7 +54,7 @@
"source": [
"# WMO's reference period\n",
"ref_period = [1981, 2010]\n",
"# Tempertarue difference between the reference period and 1850-1900\n",
"# Temperature difference between the reference period and 1850-1900\n",
"# See source. Computed according to the IPCC's AR6 WG1.\n",
"ref_delta = 0.69\n",
"\n",
Expand Down Expand Up @@ -110,14 +106,17 @@
"outputs": [],
"source": [
"# Get data\n",
"with open(\"Berkeley_data.txt\", \"wb\") as f:\n",
"file = Path(\"Berkeley_data.txt\")\n",
"\n",
"with file.open(\"wb\") as f:\n",
" res = requests.get(\n",
" \"https://berkeley-earth-temperature.s3.us-west-1.amazonaws.com/Global/Land_and_Ocean_summary.txt\"\n",
" \"https://berkeley-earth-temperature.s3.us-west-1.amazonaws.com/Global/Land_and_Ocean_summary.txt\",\n",
" timeout=15,\n",
" )\n",
" f.write(res.content)\n",
"\n",
"df = pd.read_table(\n",
" \"Berkeley_data.txt\",\n",
" file,\n",
" skiprows=58,\n",
" usecols=[0, 1],\n",
" names=[\"year\", \"temp\"],\n",
Expand All @@ -127,20 +126,19 @@
"da = df.temp.to_xarray().assign_attrs(units=\"°C\")\n",
"\n",
"# Get global average for the reference period of the data\n",
"with open(\"Berkeley_data.txt\") as f:\n",
"with file.open(\"r\") as f:\n",
" for line in f:\n",
" if \"% Estimated Jan 1951-Dec 1980 global mean temperature (C)\" in line:\n",
" data = re.search(r\"(\\d{2}.\\d{3})\", next(f))\n",
" break\n",
"refAvg = float(data.groups()[0])\n",
"refAvg\n",
"ref_avg = float(data.groups()[0])\n",
"\n",
"daAbs = da + refAvg\n",
"da_abs = da + ref_avg\n",
"\n",
"daWMO = clean(da)\n",
"da_wmo = clean(da)\n",
"\n",
"temps.append(daAbs.expand_dims(source=[\"Berkeley-Raw\"]))\n",
"temps.append(daWMO.expand_dims(source=[\"Berkeley\"]))"
"temps.append(da_abs.expand_dims(source=[\"Berkeley-Raw\"]))\n",
"temps.append(da_wmo.expand_dims(source=[\"Berkeley\"]))"
]
},
{
Expand All @@ -154,10 +152,10 @@
"source": [
"# A figure to look at it\n",
"fig, ax = plt.subplots(figsize=(10, 3))\n",
"(daAbs - daAbs.sel(year=slice(1850, 1900)).mean()).plot(ax=ax, label=\"Raw\")\n",
"daWMO.plot(ax=ax, label=\"WMO\")\n",
"(da_abs - da_abs.sel(year=slice(1850, 1900)).mean()).plot(ax=ax, label=\"Raw\")\n",
"da_wmo.plot(ax=ax, label=\"WMO\")\n",
"ax.set_title(\n",
" \"Global Average Temperature according to Bekerley - anomalies vs 1850-1900\"\n",
" \"Global Average Temperature according to Berkeley - anomalies vs 1850-1900\"\n",
")\n",
"ax.set_xlabel(\"years\")\n",
"ax.set_ylabel(\"[°C]\")\n",
Expand Down Expand Up @@ -193,9 +191,9 @@
")\n",
"da = df[\"J-D\"].to_xarray().rename(Year=\"year\").rename(\"temp\").assign_attrs(units=\"°C\")\n",
"\n",
"daWMO = clean(da)\n",
"da_wmo = clean(da)\n",
"\n",
"temps.append(daWMO.expand_dims(source=[\"GISTEMPv4\"]))"
"temps.append(da_wmo.expand_dims(source=[\"GISTEMPv4\"]))"
]
},
{
Expand Down Expand Up @@ -231,9 +229,9 @@
" .assign_attrs(units=\"°C\")\n",
")\n",
"\n",
"daWMO = clean(da)\n",
"da_wmo = clean(da)\n",
"\n",
"temps.append(daWMO.expand_dims(source=[\"HadCRUT5\"]))"
"temps.append(da_wmo.expand_dims(source=[\"HadCRUT5\"]))"
]
},
{
Expand Down Expand Up @@ -266,9 +264,9 @@
"\n",
"da = df.temp.to_xarray().assign_attrs(units=\"°C\")\n",
"\n",
"daWMO = clean(da)\n",
"da_wmo = clean(da)\n",
"\n",
"temps.append(daWMO.expand_dims(source=[\"NOAAGlobalTempv5\"]))"
"temps.append(da_wmo.expand_dims(source=[\"NOAAGlobalTempv5\"]))"
]
},
{
Expand Down Expand Up @@ -340,8 +338,8 @@
"da = da.assign_coords(time=da.time.dt.year).rename(time=\"year\")\n",
"\n",
"with ProgressBar():\n",
" daWMO = clean(da).load()\n",
"temps.append(daWMO.expand_dims(source=[\"JRA-55\"]))"
" da_wmo = clean(da).load()\n",
"temps.append(da_wmo.expand_dims(source=[\"JRA-55\"]))"
]
},
{
Expand Down Expand Up @@ -375,7 +373,7 @@
"outputs": [],
"source": [
"# A figure to look at it\n",
"fig, ax = plt.subplots(figsize=(10, 3))\n",
"fig, ax = plt.subplots(figsize=(10, 3)) # noqa\n",
"ds.plot(ax=ax, hue=\"source\")\n",
"ax.set_title(\"Global Average Temperature - Obs\")\n",
"ax.set_xlabel(\"years\")\n",
Expand Down Expand Up @@ -436,8 +434,7 @@
"source": [
"db2 = xr.concat([db, ds2], \"simulation\")\n",
"db2.attrs[\"description\"] = (\n",
" db.attrs[\"description\"]\n",
" + \" Observational datasets were also added following the WMO guidelines.\"\n",
" f\"{db.attrs['description']} Observational datasets were also added following the WMO guidelines.\"\n",
")"
]
},
Expand All @@ -464,14 +461,6 @@
"source": [
"db2.tas.plot(hue=\"simulation\", add_legend=False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "25",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
Loading