Skip to content
Snippets Groups Projects
Commit e4efeaf5 authored by karin.meier-fleischer's avatar karin.meier-fleischer Committed by Ralf Mueller
Browse files

Add data processing exercise

parent 6647b9d9
No related branches found
No related tags found
1 merge request!2Draft: add link to debugging document
%% Cell type:markdown id: tags:
# Exercise: CMIP6 - Plot Time Series of annual means
<br>
In this task, the global annual means of the CMIP6 variable `tas` are to be calculated and plotted.
Since one time series alone would be quite boring we want to generate a total of 4 time series of <br>
the scenarios
SSP 1-2.6
SSP 2-4.5
SSP 3-7.0
SSP 5-8.5
For more information about the scenarios see
https://www.dkrz.de/en/communication/climate-simulations/cmip6-en/the-ssp-scenarios
The data are stored in several separate files and are to be merged with CDO and then their field mean average calculated.
### In the exercise you will:
1. Learn to use the netCDF input files
1. Process the data with CDO or xarray
1. Visualize the processed data
An example plot of the scenarios of some more ensembles and their mean values can be found on the DKRZ <br>
page for CMIP6
https://www.dkrz.de/de/kommunikation/klimasimulationen/cmip6-de/cmip6-aktivitaeten-am-dkrz-ueberblick
### Input Data
**Historical**:
/pool/data/CMIP6/data/CMIP/MPI-M/MPI-ESM1-2-LR/historical/r1i1p1f1/Amon/tas/gn/v20190710/
**Scenario SSP 1-2.6**:
/pool/data/CMIP6/data/ScenarioMIP/MPI-M/MPI-ESM1-2-LR/ssp126/r1i1p1f1/Amon/tas/gn/v20190710/
**Scenario SSP 2-4.5**:
/pool/data/CMIP6/data/ScenarioMIP/MPI-M/MPI-ESM1-2-LR/ssp245/r1i1p1f1/Amon/tas/gn/v20190710/
**Scenario SSP 3-7.0**:
/pool/data/CMIP6/data/ScenarioMIP/MPI-M/MPI-ESM1-2-LR/ssp370/r1i1p1f1/Amon/tas/gn/v20190710/
**Scenario SSP 5-8.5**:
/pool/data/CMIP6/data/ScenarioMIP/MPI-M/MPI-ESM1-2-LR/ssp585/r1i1p1f1/Amon/tas/gn/v20190710/
### CDO hints:
1. Merge files along time (mergetime)
1. Compute the annual mean (yearmean)
1. Compute the field means (fldmean)
1. Take care of the shapes
1. Important: clean temporary files
### Xarray hints:
1. Open multiple files at once
1. Compute the weights (http://xarray.pydata.org/en/stable/examples/area_weighted_temperature.html)
1. Compute the annual mean (resample)
1. Compute the field means (mean)
%% Cell type:markdown id: tags:
## Import CDO module
%% Cell type:code id: tags:
``` python
from cdo import *
cdo = Cdo()
```
%% Cell type:markdown id: tags:
## Import Matplotlib module
%% Cell type:code id: tags:
``` python
from matplotlib import pyplot as plt
%matplotlib inline
```
%% Cell type:markdown id: tags:
## Input files
%% Cell type:code id: tags:
``` python
data_ssp126 = '/pool/data/CMIP6/data/ScenarioMIP/MPI-M/MPI-ESM1-2-LR/ssp126/r1i1p1f1/Amon/tas/gn/v20190710/tas_*.nc'
data_ssp245 = '/pool/data/CMIP6/data/ScenarioMIP/MPI-M/MPI-ESM1-2-LR/ssp245/r1i1p1f1/Amon/tas/gn/v20190710/tas_*.nc'
data_ssp370 = '/pool/data/CMIP6/data/ScenarioMIP/MPI-M/MPI-ESM1-2-LR/ssp370/r1i1p1f1/Amon/tas/gn/v20190710/tas_*.nc'
data_ssp585 = '/pool/data/CMIP6/data/ScenarioMIP/MPI-M/MPI-ESM1-2-LR/ssp585/r1i1p1f1/Amon/tas/gn/v20190710/tas_*.nc'
```
%% Cell type:markdown id: tags:
## Compute the field mean of the yearly mean
%% Cell type:code id: tags:
``` python
tas126_ymean = cdo.fldmean(input='-yearmean -mergetime ' + data_ssp126, returnXArray='tas')
tas245_ymean = cdo.fldmean(input='-yearmean -mergetime ' + data_ssp245, returnXArray='tas')
tas370_ymean = cdo.fldmean(input='-yearmean -mergetime ' + data_ssp370, returnXArray='tas')
tas585_ymean = cdo.fldmean(input='-yearmean -mergetime ' + data_ssp585, returnXArray='tas')
```
%% Cell type:markdown id: tags:
## Keep an eye on the data shapes
```
print(tas126_ymean.shape)
print(tas245_ymean.shape)
print(tas370_ymean.shape)
print(tas585_ymean.shape)
(86, 1, 1)
(86, 1, 1)
(86, 1, 1)
(86, 1, 1)
```
Remove the lat and lon dimensions
```
data126 = tas126_ymean[:,0,0]
data245 = tas245_ymean[:,0,0]
data370 = tas370_ymean[:,0,0]
data585 = tas585_ymean[:,0,0]
```
%% Cell type:markdown id: tags:
To prevent this step use CDO's option `--reduce_dims` within the CDO command line.
For example
`tas126_ymean = cdo.fldmean(options='--reduce_dim', input='-yearmean -mergetime ' + data_ssp126, returnXArray='tas')`
%% Cell type:code id: tags:
``` python
data126 = cdo.fldmean(options='--reduce_dim', input='-yearmean -mergetime ' + data_ssp126, returnXArray='tas')
data245 = cdo.fldmean(options='--reduce_dim', input='-yearmean -mergetime ' + data_ssp245, returnXArray='tas')
data370 = cdo.fldmean(options='--reduce_dim', input='-yearmean -mergetime ' + data_ssp370, returnXArray='tas')
data585 = cdo.fldmean(options='--reduce_dim', input='-yearmean -mergetime ' + data_ssp585, returnXArray='tas')
```
%% Cell type:markdown id: tags:
## Define x-axis
%% Cell type:code id: tags:
``` python
x = range(2015,2101,1)
```
%% Cell type:markdown id: tags:
## Create the time series plot
%% Cell type:code id: tags:
``` python
fig, ax = plt.subplots(figsize=(8,4))
ax.plot(x, data126, label='ssp126')
ax.plot(x, data245, label='ssp245')
ax.plot(x, data370, label='ssp370')
ax.plot(x, data585, label='ssp585')
plt.grid()
ax.set_title('CMIP6: tas global yearly mean')
ax.set_xlabel('years')
ax.set_ylabel(data126.long_name+' ['+data126.units+']')
legend = ax.legend(loc='upper left', shadow=True, fontsize='small')
legend.get_frame().set_facecolor('lightgray')
```
%% Cell type:markdown id: tags:
## Delete temporary files
%% Cell type:code id: tags:
``` python
cdo.cleanTempDir()
```
%% Cell type:markdown id: tags:
## Xarray not CDO
%% Cell type:code id: tags:
``` python
import xarray as xr
import numpy as np
```
%% Cell type:code id: tags:
``` python
ds126 = xr.open_mfdataset(data_ssp126)
ds245 = xr.open_mfdataset(data_ssp245)
ds370 = xr.open_mfdataset(data_ssp370)
ds585 = xr.open_mfdataset(data_ssp585)
```
%% Cell type:markdown id: tags:
### Yearly means
%% Cell type:code id: tags:
``` python
tas126 = ds126.tas.resample(time="Y").mean()
tas245 = ds245.tas.resample(time="Y").mean()
tas370 = ds370.tas.resample(time="Y").mean()
tas585 = ds585.tas.resample(time="Y").mean()
```
%% Cell type:code id: tags:
``` python
weights = np.cos(np.deg2rad(ds126.lat))
weights.name = "weights"
```
%% Cell type:code id: tags:
``` python
tas126_weighted = tas126.weighted(weights)
tas245_weighted = tas245.weighted(weights)
tas370_weighted = tas370.weighted(weights)
tas585_weighted = tas585.weighted(weights)
```
%% Cell type:code id: tags:
``` python
tas126_weightedmean = tas126_weighted.mean(('lon', 'lat'))
tas245_weightedmean = tas245_weighted.mean(('lon', 'lat'))
tas370_weightedmean = tas370_weighted.mean(('lon', 'lat'))
tas585_weightedmean = tas585_weighted.mean(('lon', 'lat'))
```
%% Cell type:code id: tags:
``` python
fig, ax = plt.subplots(figsize=(8,4))
ax.plot(x, tas126_weightedmean, label='ssp126 weighted mean')
ax.plot(x, tas245_weightedmean, label='ssp245 weighted mean')
ax.plot(x, tas370_weightedmean, label='ssp370 weighted mean')
ax.plot(x, tas585_weightedmean, label='ssp585 weighted mean')
plt.grid()
ax.set_title('CMIP6: tas global yearly mean')
ax.set_xlabel('years')
ax.set_ylabel(data126.long_name+' ['+data126.units+']')
legend = ax.legend(loc='upper left', shadow=True, fontsize='small')
legend.get_frame().set_facecolor('lightgray')
```
%% Cell type:code id: tags:
``` python
```
# Exercise: CMIP6 - Plot Time Series of annual means
<br>
In this task, the global annual means of the CMIP6 variable `tas` are to be calculated and plotted.
Since one time series alone would be quite boring we want to generate a total of 4 time series of <br>
the scenarios
SSP 1-2.6
SSP 2-4.5
SSP 3-7.0
SSP 5-8.5
For more information about the scenarios see
https://www.dkrz.de/en/communication/climate-simulations/cmip6-en/the-ssp-scenarios
The data are stored in several separate files and are to be merged with CDO and then their field mean average calculated.
### In the exercise you will:
1. Learn to use the netCDF input files
1. Process the data with CDO or xarray
1. Visualize the processed data
An example plot of the scenarios of some more ensembles and their mean values can be found on the DKRZ <br>
page for CMIP6
https://www.dkrz.de/de/kommunikation/klimasimulationen/cmip6-de/cmip6-aktivitaeten-am-dkrz-ueberblick
### Input Data
**Historical**:
/pool/data/CMIP6/data/CMIP/MPI-M/MPI-ESM1-2-LR/historical/r1i1p1f1/Amon/tas/gn/v20190710/
**Scenario SSP 1-2.6**:
/pool/data/CMIP6/data/ScenarioMIP/MPI-M/MPI-ESM1-2-LR/ssp126/r1i1p1f1/Amon/tas/gn/v20190710/
**Scenario SSP 2-4.5**:
/pool/data/CMIP6/data/ScenarioMIP/MPI-M/MPI-ESM1-2-LR/ssp245/r1i1p1f1/Amon/tas/gn/v20190710/
**Scenario SSP 3-7.0**:
/pool/data/CMIP6/data/ScenarioMIP/MPI-M/MPI-ESM1-2-LR/ssp370/r1i1p1f1/Amon/tas/gn/v20190710/
**Scenario SSP 5-8.5**:
/pool/data/CMIP6/data/ScenarioMIP/MPI-M/MPI-ESM1-2-LR/ssp585/r1i1p1f1/Amon/tas/gn/v20190710/
### CDO hints:
1. Merge files along time (mergetime)
1. Compute the annual mean (yearmean)
1. Compute the field means (fldmean)
1. Take care of the shapes
1. Important: clean temporary files
### Xarray hints:
1. Open multiple files at once
1. Compute the weights (http://xarray.pydata.org/en/stable/examples/area_weighted_temperature.html)
1. Compute the annual mean (resample)
1. Compute the field means (mean)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment