Skip to content
Snippets Groups Projects
Commit d1249cef authored by Aiko Voigt's avatar Aiko Voigt
Browse files

Adds analysis plots of temperature and precipitation

parent 6e315993
Branches
No related tags found
No related merge requests found
......@@ -2,4 +2,7 @@ runs/amip/logfiles/
runs/slab4x-sun/logfiles/
runs/slab4x-vap/logfiles/
runs/slab4x/logfiles/
runs/slabctr/logfiles/
\ No newline at end of file
runs/slabctr/logfiles/
analysis/.ipynb_checkpoints/
analysis/__pycache__/
import xarray as xr
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# load yearly mean dataset
def load_yearmean_dataset(file: str, startdate: str):
ds = xr.load_dataset(file)
ds["time"] = pd.date_range(startdate, freq="ME", periods=ds.time.size)
return ds.groupby(ds.time.dt.year).mean()
# compute global mean
def globalmean(ds):
weights = np.cos(np.deg2rad(ds.lat))
weights.name = "weights"
ds_weighted = ds.weighted(weights)
return ds_weighted.mean(("lon", "lat"))
def beautify_timeseries(ax, yaxis0):
""" Makes plots of time series nicer in terms of axes and labeling"""
# adjust spines
ax = plt.gca()
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',yaxis0))
\ No newline at end of file
File added
File added
%% Cell type:markdown id:1d1abe75-9ec9-46a7-b938-7eefaf5e37ad tags:
# Evolution of global mean surface temperature and sea ice area
%% Cell type:code id:80f55624-7ed4-4e11-ab49-4ce095a98d71 tags:
``` python
import xarray as xr
import numpy as np
import matplotlib.pyplot as plt
```
%% Cell type:code id:dda27ec2-7f9f-44d5-9c31-cbf8188fe59e tags:
``` python
import core as core
```
%% Cell type:code id:caf0443b-bb04-4952-b0c0-2432a95d991f tags:
``` python
# path to model output
path="/home/voigta80/LEHRE/climmodlab_s2024/output4students/"
```
%% Cell type:code id:115178b5-2b01-4441-a373-eaf55555dc6f tags:
``` python
slabctr = core.load_yearmean_dataset(file=path+"/slabctr/slabctr_atm_2d_ml_197901-202901.r180x90.nc", startdate="1979-01-01")
slab4x = core.load_yearmean_dataset(file=path+"/slab4x/slab4x_atm_2d_ml_199002-203901.r180x90.nc", startdate="1999-02-01")
slabsun = core.load_yearmean_dataset(file=path+"/slab4x-sun/slab4x-sun_atm_2d_ml_199002-203901.r180x90.nc", startdate="1999-02-01")
slabvap = core.load_yearmean_dataset(file=path+"/slab4x-vap/slab4x-vap_atm_2d_ml_199002-203901.r180x90.nc", startdate="1999-02-01")
```
%% Cell type:code id:364a4154-b864-4d60-ba69-4ac5384e05cb tags:
``` python
slabctr_mean = core.globalmean(slabctr)
slab4x_mean = core.globalmean(slab4x)
slabsun_mean = core.globalmean(slabsun)
slabvap_mean = core.globalmean(slabvap)
```
%% Cell type:code id:8dfda055-ea17-4a09-854f-617107fb7c6d tags:
``` python
# some data massaging to remove faulty years
# year 2029 of slabctr is not complete
slabctr_mean = slabctr_mean.where(slabctr_mean.year < 2029, drop=True)
# year 2026 of slabvap has missing values
slabvap_mean = xr.merge([slabvap_mean.where(slabvap_mean.year < 2026, drop=True), slabvap_mean.where(slabvap_mean.year > 2026, drop=True)])
```
%% Cell type:markdown id:16822ce6-5606-4693-9f3b-7359bc89dd92 tags:
Global mean surface temperature
%% Cell type:code id:0e30e8ce-54d8-4aa9-85b8-2d8e5f76bec6 tags:
``` python
# print global mean surface temperatures averaged over 10 years to screen
print("Slab control :", slabctr_mean["ts"].isel(year=slice(-10,-1)).mean().values)
print("Slab 4x :", slab4x_mean["ts"].isel(year=slice(-10,-1)).mean().values)
print("Slab 4x-vapor:", slabvap_mean["ts"].isel(year=slice(-10,-1)).mean().values)
print("Slab 4x-sun :", slabsun_mean["ts"].isel(year=slice(-10,-1)).mean().values)
```
%% Output
Slab control : 287.910243809416
Slab 4x : 294.4904180230014
Slab 4x-vapor: 293.87587189765486
Slab 4x-sun : 290.28140743731694
%% Cell type:code id:179d1984-872d-42bf-8722-7a53b9b87989 tags:
``` python
plt.figure(figsize=(6,4))
ax=plt.subplot(1,1,1)
plt.plot(slabctr_mean.year, slabctr_mean["ts"], color="royalblue")
plt.plot(slab4x_mean.year, slab4x_mean["ts"], color="crimson")
plt.plot(slabvap_mean.year, slabvap_mean["ts"], color="coral")
plt.plot(slabsun_mean.year, slabsun_mean["ts"], color="seagreen")
plt.ylim(287,295)
plt.xlim(1980,2038)
core.beautify_timeseries(ax, yaxis0=286.5)
plt.xlabel("year", loc="right", size=12)
plt.ylabel("global-mean surface temperature / K", loc="top", size=12)
plt.text(2038,288.2, "present-day simulation: 287.9 K", ha="right", color="royalblue", size=12)
plt.text(2038,294.7, "4xCO2: +6.5 K", ha="right", color="crimson", size=12)
plt.text(2038,293.2, "4xCO2-vapor: +6.1 K", ha="right", color="coral", size=12)
plt.text(2038,290.6, "4xCO2-sun: +2.4 K", ha="right", color="seagreen", size=12)
plt.savefig("globalmean_ts.pdf", bbox_inches="tight")
```
%% Output
%% Cell type:markdown id:4dbf37eb-835a-40ed-b2fc-71b91080f4ca tags:
Global mean sea ice cover
%% Cell type:code id:e74f9f0c-b147-45b3-8075-be7efa5eefbb tags:
``` python
plt.figure(figsize=(6,4))
ax=plt.subplot(1,1,1)
# surface area of earth in km2
aearth=4*np.pi*np.power(6371,2)
plt.plot(slabctr_mean.year, 1e-6*aearth*slabctr_mean["sic"], color="royalblue")
plt.plot(slab4x_mean.year, 1e-6*aearth*slab4x_mean["sic"], color="crimson")
plt.plot(slabvap_mean.year, 1e-6*aearth*slabvap_mean["sic"], color="coral")
plt.plot(slabsun_mean.year, 1e-6*aearth*slabsun_mean["sic"], color="seagreen")
plt.ylim(7, 20)
plt.xlim(1980,2038)
core.beautify_timeseries(ax, yaxis0=6)
plt.xlabel("year", loc="right", size=12)
plt.ylabel(r"global sea ice area / 10$^\text{6}$ km$^\text{2}$", loc="top", size=12)
plt.savefig("globalmean_sic.pdf", bbox_inches="tight")
```
%% Output
File added
File added
Source diff could not be displayed: it is too large. Options to address this: view the blob.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment