Loading, processing, and visualizing data
This notebook shows how to load a dataset, use the preprocessor functions, and visualize the result. In this notebook we will plot the annual mean temperature from 1850 till 2100 from one model.
[1]:
%matplotlib inline
import matplotlib.pyplot as plt
import iris.quickplot
from esmvalcore.config import CFG
from esmvalcore.dataset import Dataset
from esmvalcore.esgf import download, ESGFFile
from esmvalcore.preprocessor import area_statistics, annual_statistics
Configure ESMValCore so it searches the ESGF for data
[2]:
CFG['offline'] = False
Define the dataset we are going to use. In this case surface air temperature (tas
).
[3]:
tas = Dataset(
short_name='tas',
mip='Amon',
project='CMIP5',
dataset='CanESM2',
ensemble='r1i1p1',
exp='historical',
timerange='1850/2000',
)
tas
[3]:
Dataset:
{'dataset': 'CanESM2',
'project': 'CMIP5',
'mip': 'Amon',
'short_name': 'tas',
'ensemble': 'r1i1p1',
'exp': 'historical',
'timerange': '1850/2000'}
In order to compute the area average later on, we add the cell areas (areacella
) as an ancillary dataset. This will append a new dataset to the list of ancillary datasets. Its facets are copied from the tas
dataset and updated with the provided facets:
[4]:
tas.add_ancillary(short_name='areacella', mip='fx', ensemble='r0i0p0')
tas.ancillaries
[4]:
[Dataset:
{'dataset': 'CanESM2',
'project': 'CMIP5',
'mip': 'fx',
'short_name': 'areacella',
'ensemble': 'r0i0p0',
'exp': 'historical',
'timerange': '1850/2000'}]
ESMValCore can automatically add extra facets based on the project
, mip
, short_name
, and dataset
. These extra facets are automatically added and used when searching for input files.
[5]:
tas.augment_facets()
tas
[5]:
Dataset:
{'dataset': 'CanESM2',
'project': 'CMIP5',
'mip': 'Amon',
'short_name': 'tas',
'ensemble': 'r1i1p1',
'exp': 'historical',
'frequency': 'mon',
'institute': ['CCCma'],
'long_name': 'Near-Surface Air Temperature',
'modeling_realm': ['atmos'],
'original_short_name': 'tas',
'product': ['output1', 'output2'],
'standard_name': 'air_temperature',
'timerange': '1850/2000',
'units': 'K'}
ancillaries:
{'dataset': 'CanESM2',
'project': 'CMIP5',
'mip': 'fx',
'short_name': 'areacella',
'ensemble': 'r0i0p0',
'exp': 'historical',
'frequency': 'fx',
'institute': ['CCCma'],
'long_name': 'Atmosphere Grid-Cell Area',
'modeling_realm': ['atmos', 'land'],
'original_short_name': 'areacella',
'product': ['output1', 'output2'],
'standard_name': 'cell_area',
'units': 'm2'}
session: 'session-efd66689-5d5c-4bc6-b471-a73ab1d44bef_20230112_103658'
Use the find_files
method to find the files corresponding to the dataset.
[6]:
tas.find_files()
print(tas.files)
for anc in tas.ancillaries:
print(anc.files)
[ESGFFile:cmip5/output1/CCCma/CanESM2/historical/mon/atmos/Amon/r1i1p1/v20120718/tas_Amon_CanESM2_historical_r1i1p1_185001-200512.nc on hosts ['esgf.ceda.ac.uk', 'esgf.nci.org.au', 'esgf2.dkrz.de']]
[ESGFFile:cmip5/output1/CCCma/CanESM2/historical/fx/atmos/fx/r0i0p0/v20120410/areacella_fx_CanESM2_historical_r0i0p0.nc on hosts ['aims3.llnl.gov', 'esgf.ceda.ac.uk', 'esgf.nci.org.au', 'esgf2.dkrz.de']]
If the files are not available locally, ESMValCore can download them for us.
[7]:
files = list(tas.files)
for anc in tas.ancillaries:
files.extend(anc.files)
files = [file for file in files if isinstance(file, ESGFFile)]
download(files, CFG['download_dir'])
tas.find_files()
print(tas.files)
for anc in tas.ancillaries:
print(anc.files)
[LocalFile('/home/bandela/climate_data/cmip5/output1/CCCma/CanESM2/historical/mon/atmos/Amon/r1i1p1/v20120718/tas_Amon_CanESM2_historical_r1i1p1_185001-200512.nc')]
[LocalFile('/home/bandela/climate_data/cmip5/output1/CCCma/CanESM2/historical/fx/atmos/fx/r0i0p0/v20120410/areacella_fx_CanESM2_historical_r0i0p0.nc')]
The data in the files can be loaded into an iris.cube.Cube. ESMValCore will automatically check for and fix problems with the data formatting and attach the cell area.
[8]:
cube = tas.load()
cube
WARNING:esmvalcore.cmor.check:There were warnings in variable tas:
Coordinate lon units degrees converted to degrees_east
Coordinate lat units degrees converted to degrees_north
loaded from file /home/bandela/climate_data/cmip5/output1/CCCma/CanESM2/historical/mon/atmos/Amon/r1i1p1/v20120718/tas_Amon_CanESM2_historical_r1i1p1_185001-200512.nc
WARNING:esmvalcore.cmor.check:There were warnings in variable areacella:
Coordinate lon units degrees converted to degrees_east
Coordinate lat units degrees converted to degrees_north
loaded from file /home/bandela/climate_data/cmip5/output1/CCCma/CanESM2/historical/fx/atmos/fx/r0i0p0/v20120410/areacella_fx_CanESM2_historical_r0i0p0.nc
[8]:
Air Temperature (K) | time | latitude | longitude |
---|---|---|---|
Shape | 1812 | 64 | 128 |
Dimension coordinates | |||
time | x | - | - |
latitude | - | x | - |
longitude | - | - | x |
Cell measures | |||
cell_area | - | x | x |
Scalar coordinates | |||
height | 2.0 m | ||
Cell methods | |||
mean | time (15 minutes) | ||
Attributes | |||
CCCma_data_licence | '1) GRANT OF LICENCE - The Government of Canada (Environment Canada) is ...' | ||
CCCma_parent_runid | 'IGA' | ||
CCCma_runid | 'IGM' | ||
Conventions | 'CF-1.4' | ||
associated_files | 'baseURL: http://cmip-pcmdi.llnl.gov/CMIP5/dataLocation gridspecFile: gridspec_atmos_fx_CanESM2_historical_r0i0p0.nc ...' | ||
branch_time | 171915.0 | ||
branch_time_YMDH | '2321:01:01:00' | ||
cmor_version | '2.5.4' | ||
contact | 'cccma_info@ec.gc.ca' | ||
creation_date | '2011-03-16T18:49:59Z' | ||
experiment | 'historical' | ||
experiment_id | 'historical' | ||
forcing | 'GHG,Oz,SA,BC,OC,LU,Sl,Vl (GHG includes CO2,CH4,N2O,CFC11,effective CFC ...' | ||
frequency | 'mon' | ||
history | "2011-03-16T18:49:59Z altered by CMOR: Treated scalar dimension: 'height'. ..." | ||
initialization_method | 1 | ||
institute_id | 'CCCma' | ||
institution | 'CCCma (Canadian Centre for Climate Modelling and Analysis, Victoria, BC, ...' | ||
model_id | 'CanESM2' | ||
modeling_realm | 'atmos' | ||
original_name | 'ST' | ||
parent_experiment | 'pre-industrial control' | ||
parent_experiment_id | 'piControl' | ||
parent_experiment_rip | 'r1i1p1' | ||
physics_version | 1 | ||
product | 'output' | ||
project_id | 'CMIP5' | ||
realization | 1 | ||
references | 'http://www.cccma.ec.gc.ca/models' | ||
source | 'CanESM2 2010 atmosphere: CanAM4 (AGCM15i, T63L35) ocean: CanOM4 (OGCM4.0, ...' | ||
table_id | 'Table Amon (31 January 2011) 53b766a395ac41696af40aab76a49ae5' | ||
title | 'CanESM2 model output prepared for CMIP5 historical' | ||
tracking_id | '7ddaef12-497a-4354-bfd9-f113ef6dfaee' |
[9]:
cell_area = cube.cell_measures()[0]
cell_area
[9]:
<CellMeasure: cell_area / (m2) <lazy> shape(64, 128)>
This code shows how to use some esmvalcore.preprocessor functions to compute the global annual mean temperature in degrees Celsius:
[10]:
cube = area_statistics(cube, operator='mean')
cube = annual_statistics(cube, operator='mean')
cube.convert_units('degrees_C')
The iris.quickplot module has useful functions for quickly plotting the results:
[11]:
iris.quickplot.plot(cube)
plt.show()
