from IPython.display import Image
Image(url='https://vesg.ipsl.upmc.fr/thredds/fileServer/IPSLFS/jservon/CliMAF_Notebooks_html/CliMAF-logo-small.png')
In this notebook we will see:
from climaf.api import *
clog('critical') # min verbosity = critical < warning < info < debug = max verbosity
Here is the list of tables for CMIP6: https://github.com/WCRP-CMIP/CMIP6_CVs/blob/master/CMIP6_table_id.json
Basically, a table is a combination of the realm considered (A for atmosphere, O for ocean, SI for Sea Ice, etc.) and the frequency (mon for monthly, day for daily, etc.)
# -- Here is an example of ssp585 scenario of IPSL-CM6A-LR, monthly tas
dat_cmip6 = ds(project = 'CMIP6',
model = 'IPSL-CM6A-LR',
experiment = 'ssp585',
period = '2015-2100',
variable = 'tas',
# -- CMIP6 new features
realization = 'r1i1p1f1',
table= 'Amon',
grid = 'gr'
)
summary(dat_cmip6)
summary(dat_cmip6.explore('resolve'))
The CMIP6 archive is slightly different from the CMIP5 archive.
The key 'institute' and the new key 'grid' implies that we can't build a CMIP6 multi-model ensemble in one move with .explore('ensemble') (example and solution below), since we easily have more than one key with multiple values.
Building a multi-realization ensemble on one model still remains an easy task.
# -- Let's start with an easy example, with the same behavior as described
# for the CMIP5 multi-model ensemble
req_cmip6 = ds(project='CMIP6', variable='pr', period='*',
table='Amon', model='CNRM-CM6-1',
realization='*'
)
req_cmip6.explore('choices')
# -- realization is the only key with a list of values, we can thus use .explore('ensemble')
# to create an ensemble
multirealization_cmip6 = req_cmip6.explore('ensemble')
summary(multirealization_cmip6)
Let's now build a multi-model ensemble with only one realization (next step 2.3 is about multi-model multi-realization ensemble)
# -- CMIP6: multi-model ensemble on the first member r1i1p1f1
req_cmip6 = ds(project='CMIP6',
variable='pr',
period='2050',
experiment='ssp585',
table='Amon',
model='*',
realization='r1i1p1f1' # -- we set r1i1p1f1
)
req_cmip6.explore('choices')
We use a try/except test on .explore('resolve') to see if CliMAF finds automatically the (potential) only grid available. If there are multiple grids available, it returns a message
# -- we create a 'request dictionary' with the main keyword/values (without model)
req_dict = dict(project='CMIP6',
variable='pr',
period='2050',
experiment='ssp585',
table='Amon',
grid = 'g*',
realization='r1i1p1f1' # -- we set r1i1p1f1
)
# -- We make a first request to retrieve the list of models
req_cmip6 = ds(model='*', **req_dict)
list_of_models = req_cmip6.explore('choices')['model']
# -- Then, we loop on the models:
# - we initialize an empty dictionary ens_dict
# - for each model that has only one dataset (check with .explore('resolve')),
# we add the dataset to ens_dict with the key 'model'
# - after the loop, we use cens to create the ensemble from ens_dict
ens_dict = dict()
for model in list_of_models:
try:
ens_dict[model] = ds(model=model, **req_dict).explore('resolve')
print model
print ens_dict[model].baseFiles()
except:
print 'Check the result of .explore("choices") for model ', model
print ds(model=model, **req_dict).explore('choices')
# -- Last step: we create the ensemble with cens
myens = cens(ens_dict)
# -- we create a 'request dictionary' with the main keyword/values (without model)
req_dict = dict(project='CMIP6',
variable='tos',
period='2050',
experiment='ssp585',
table='Omon',
grid = 'g*',
realization='r1i1p1f1' # -- we set r1i1p1f1
)
# -- We make a first request to retrieve the list of models
req_cmip6 = ds(model='*', **req_dict)
list_of_models = req_cmip6.explore('choices')['model']
# -- Then, we loop on the models
ens_dict = dict()
for model in list_of_models:
try:
ens_dict[model] = ds(model=model, **req_dict).explore('resolve')
print model
print ens_dict[model].baseFiles()
except:
print 'Check the result of .explore("choices") for model ', model
print ds(model=model, **req_dict).explore('choices')
# -- Last step: we create the ensemble with cens
myens = cens(ens_dict)
# -- we create a 'request dictionary' with the main keyword/values (without model)
req_dict = dict(project='CMIP6',
variable='tos',
period='2050',
experiment='ssp585',
table='Omon',
grid = 'g*',
realization='r1i1p1f1' # -- we set r1i1p1f1
)
# -- We make a first request to retrieve the list of models
req_cmip6 = ds(model='*', **req_dict)
# -- Retrieve the list of models
list_of_models = req_cmip6.explore('choices')['model']
# -- We make a list of grids in order of preference
mygrids = ['gn','gr','gr1','gr2']
# -- Then, we loop on the models
ens_dict = dict()
for model in list_of_models:
# -- If we find a matching dataset, we add it to ens_dict
try:
ens_dict[model] = ds(model=model, **req_dict).explore('resolve')
print model
except:
# -- if there are multiple grids available:
# - we retrieve the list of available grids
# - and keep the first grid from mygrids that is available
available_grids = ds(model=model, **req_dict).explore('choices')['grid']
if isinstance(available_grids,list):
# -- Loop on my own list of grids and stop at the first available
for mygrid in mygrids:
if mygrid in available_grids:
break
tmp_req_dict = req_dict.copy()
tmp_req_dict['grid'] = mygrid
ens_dict[model] = ds(model=model, **tmp_req_dict).explore('resolve')
print 'Picked grid = ',mygrid, 'for model', model
print ens_dict[model].baseFiles()
# -- Create ensemble
myens = cens(ens_dict)
# -- we create a 'request dictionary' with the main keyword/values (without model)
req_dict = dict(project='CMIP6',
variable='tos',
period='2050',
experiment='ssp585',
table='Omon',
grid = 'g*',
realization='*' # -- we set r1i1p1f1
)
# -- We make a first request to retrieve the list of models
req_cmip6 = ds(model='*', **req_dict)
list_of_models = req_cmip6.explore('choices')['model']
# -- We make a list of grids in order of preference
mygrids = ['gn','gr','gr1','gr2']
ens_dict = dict()
from natsort import natsorted
# -- Then, we loop on the models
for model in list_of_models:
tmp_req_dict = req_dict.copy()
available_choices_for_model = ds(model=model, **req_dict).explore('choices')
available_grids = available_choices_for_model['grid']
if isinstance(available_grids,list):
# -- Loop on my own list of grids and stop at the first available
for mygrid in mygrids:
if mygrid in available_grids:
break
else:
mygrid = available_grids
#
# -- We retrieve the available realizations:
# - if it's a list, we take the first one from a 'natsorted' list
# - if not, we take the available dataset
available_realizations = available_choices_for_model['realization']
if isinstance(available_realizations, list):
tmp_req_dict['realization'] = natsorted(available_realizations)[0]
else:
tmp_req_dict['realization'] = available_realizations
#tmp_req_dict['realization'] = available_realizations[0]
tmp_req_dict['grid'] = mygrid
ens_dict[model] = ds(model=model, **tmp_req_dict).explore('resolve')
print model,';', ens_dict[model].baseFiles()
# -- we create a 'request dictionary' with the main keyword/values (without model)
req_dict = dict(project='CMIP6',
variable='tos',
period='2050',
experiment='ssp585',
table='Omon',
grid = 'g*',
realization='*' # -- we set r1i1p1f1
)
# -- We make a first request to retrieve the list of models
req_cmip6 = ds(model='*', **req_dict)
list_of_models = req_cmip6.explore('choices')['model']
# -- We make a list of grids in order of preference
mygrids = ['gn','gr','gr1','gr2']
ens_dict = dict()
from natsort import natsorted
# -- Then, we loop on the models
for model in list_of_models:
available_choices_for_model = ds(model=model, **req_dict).explore('choices')
available_grids = available_choices_for_model['grid']
if isinstance(available_grids,list):
# -- Loop on my own list of grids and stop at the first available
for mygrid in mygrids:
if mygrid in available_grids:
break
else:
mygrid = available_grids
if isinstance(available_choices_for_model['realization'], list):
available_realizations = natsorted(available_choices_for_model['realization'])
else:
available_realizations = [available_choices_for_model['realization']]
print model,'==>'
for realization in available_realizations:
tmp_req_dict = req_dict.copy()
tmp_req_dict['model'] = model
tmp_req_dict['realization'] = realization
tmp_req_dict['grid'] = mygrid
ens_dict[model+'_'+realization] = ds(**tmp_req_dict).explore('resolve')
print realization, ' = ', ens_dict[model+'_'+realization].baseFiles()
#
# -- and we create the meta-ensemble
meta_ensemble = cens(ens_dict)
# -- we create a 'request dictionary' with the main keyword/values (without model)
req_dict = dict(project='CMIP6',
variable='tos',
period='2050',
experiment='ssp585',
table='Omon',
grid = 'g*',
realization='*' # -- we set r1i1p1f1
)
# -- We make a first request to retrieve the list of models
req_cmip6 = ds(model='*', **req_dict)
list_of_models = req_cmip6.explore('choices')['model']
# -- We make a list of grids in order of preference
mygrids = ['gn','gr','gr1','gr2']
model_ens_dict = dict()
from natsort import natsorted
# -- Then, we loop on the models
for model in list_of_models:
ens_dict = dict()
available_choices_for_model = ds(model=model, **req_dict).explore('choices')
available_grids = available_choices_for_model['grid']
if isinstance(available_grids,list):
# -- Loop on my own list of grids and stop at the first available
for mygrid in mygrids:
if mygrid in available_grids:
break
else:
mygrid = available_grids
if isinstance(available_choices_for_model['realization'], list):
available_realizations = natsorted(available_choices_for_model['realization'])
else:
available_realizations = [available_choices_for_model['realization']]
print model,'==>'
for realization in available_realizations:
tmp_req_dict = req_dict.copy()
tmp_req_dict['model'] = model
tmp_req_dict['realization'] = realization
tmp_req_dict['grid'] = mygrid
ens_dict[realization] = ds(**tmp_req_dict).explore('resolve')
print realization
#
# -- CliMAF doesn't work on 'one-member ensembles';
# if there is only one member for the model,
# don't make an ensemble with cens and simply provide the dictionary ens_dict
# !! it implies that you will have to separate both cases
# ('one-member' model or 'multi-realization' model) when you will do your analyses
if len(available_realizations)==1:
model_ens_dict[model] = ens_dict
else:
model_ens_dict[model] = cens(ens_dict, order=available_realizations)
model_ens_dict
dat_histEXT = ds(project='IPSL-CM6_historical-EXT',
variable='tas',
table='Amon',
period='1950-2060',
realization='r1i1p1f1'
)
summary(dat_histEXT)
from climaf.api import *
req_histEXT = ds(project='IPSL-CM6_historical-EXT',
variable='tas',
table='Amon',
period='1950-2060',
realization='*'
)
req_histEXT.explore('choices')
ens_histEXT = req_histEXT.explore('ensemble')
summary(ens_histEXT)
from natsort import natsorted
ens_histEXT.order = natsorted(ens_histEXT.order)
summary(ens_histEXT)