S.Sénési for Météo-France - sept 2019 to march 2021
import os
figure_title = None # if None, an automatic title will be generated
auto_title_prefix = "Seasonal"
#
figure_name = "Fig8-15s" # Used for a symbolic link to an explicit filename,and for metadata filename
# Version number will be a suffix for figure filename. Use e.g. "_V1" for legibility
version = ""
scheme = "AR5"
confidence_factor = 1.645 # For AR6 comprehensive scheme : Multiplicative factor applied to control run
# variability for deciding a change is significant (besides sqrt(2))
sign_threshold = 0.66 # For AR6 simple scheme : threshold on cross-model change sign agreeement fraction
#See doc for data_versions in sibling directory data_versions
data_versions_tag = "20200719d"
data_versions_dir = os.getenv("CAMMAC")+"/data"
excluded_models = []
included_models = None # If a list is provided, only these models may be used
#
variable = "pr"
threshold = None
table = "Amon"
field_type = "means_rchange" # Any type of field as documented in function CAMMAClib.changes.change_fields
derivation_label = "plain" # Can plot the change of the raw variable, or its variability, or ... see CAMMAClib.changes.derivations
custom_plot = {} #{"colors": "-5 -2 -1 -0.5 -0.25 0. 0.25 0.5 1 2 5", "units":"-", "color":"AR6_Precip_12"} # Should be tuned, usually
#
seasons = ["DJF","MAM", "JJA", "SON"] # any CDO season, and/or "ANN".
experiment = "ssp245"
proj_period = "2081-2100"
ref_experiment = "historical"
ref_period = "1995-2014"
confidence_factor = 1.645 # Multiplicative factor applied to control run
# variability for deciding a change is significant (above sqrt(2))
#
# If pre-computed fields for this experiment, seasons and projection_period are available, should we use it ?
# Set it to False for recomputing either :
# - for printing fields and field changes statistics
# - or if any external script launched by CliMAF was internally changed
# - if the content of data version dict is changed while its tag is unchanged
use_cached_proj_fields = True
drop_old_figures = False
# When executing the final computation, set next variable to True
write_cached_proj_fields = True
print_statistics = True
# If some basic fields are to be plotted for each model :
# - which fields should be actually plotted
# plot_for_each_model = [ "reference", "projection", "change", "rchange", "schange", "variability" ]
plot_for_each_model = [ ]
# - and with which common plot_parameters
custom_plot_all_fields = { "proj" : "Robinson", "mpCenterLonF" : 0., "options" : "lbBoxEndCapStyle=TriangleBothEnds", "focus":"land"}
all_models_page_args = {"page_width":3675,"page_height":5188,"insert_width":2000,"pt":48, "ybox":133,"y":52}
# - and which range should be used
ranges = {} # The baseline value !
# Values below fit the case of mrso
#ranges={ "reference" : { "min" : 0., "max" : 3000. , "delta" : 200. } ,
# "projection" : { "min" : 0., "max" : 3000. , "delta" : 200. } ,
# "change" : { "min" :1000.,"max":-1000. , "delta":200.} ,
# "rchange" : { "min" : -25., "max" : 25. , "delta" : 5. } ,
# "schange" : { "colors": "-5 -2 -1 -0.5 -0.25 0. 0.25 0.5 1 2 5" , "units":"-", "color":"AR6_Precip_12" } ,
# "variability": { "min" : 0., "max" : 1. , "delta" : 0.1 } ,
# }
#
outdir = "./figures"
cache_dir = "./cache"
figure_details = {"page_width":2450,"page_height":3444, "insert_width":1600,"pt":50, "ybox":133,"y":52, "font":"Liberation-Sans"}
common_grid = "r360x180"
# Should we apply a mask to the figures (grid must fit with 'common_grid')
figure_mask = None
variability_sampling_args={"house_keeping":True,"compute":True,"detrend":True,"shift":100,"nyears":20,"number":20}
do_test = False
if do_test :
version = "_test"
proj_period = "2100"
ref_period = "2014"
included_models = ["CNRM-CM6-1"]
variability_sampling_args={}
#plot_for_each_model = [ "reference", "projection", "change", "rchange", "schange", "variability" ]
import sys, os
from climaf.api import *
climaf.cache.stamping=False
climaf.driver.dig_hard_into_cache = False
from CAMMAClib.changes import change_figure_with_caching
from CAMMAClib.ancillary import create_labelbar, prettier_label
from CAMMAClib.mips_et_al import TSU_metadata, read_versions_dictionnary
# Load some user settings, if available
settings_file=os.getenv("CAMMAC_USER_PYTHON_CODE_DIR",".")+'/cammac_user_settings.py'
if os.path.exists(settings_file) :
exec(compile(open(settings_file).read(),settings_file,'exec'))
from IPython.core.display import display, HTML, Image
display(HTML("<style>.container { width:100% !important; }</style>"))
def afigure(experiment,season, title=None ,labelbar="False",outfile=None,same=False,scheme=scheme):
fil,fig,dic,variab_models,used_models= \
change_figure_with_caching(variable,experiment,season,
data_versions_tag=data_versions_tag, data_versions_dir=data_versions_dir,
models=included_models, excluded_models=excluded_models,
ref_period=ref_period, proj_period=proj_period,
table=table, ref_experiment=ref_experiment,
derivation_label=derivation_label, field_type=field_type,
title=title,
custom_plot=custom_plot, labelbar=labelbar,
outdir=outdir, outfile=outfile,
#
common_grid=common_grid, mask=figure_mask,
variab_sampling_args=variability_sampling_args,
cache_dir=cache_dir, print_statistics=print_statistics,
deep=None, read=use_cached_proj_fields, write=write_cached_proj_fields,
same_models_for_variability_and_changes=same, threshold=threshold, scheme=scheme,
low_change_agree_threshold=confidence_factor,
change_sign_agree_threshold = sign_threshold,
drop=drop_old_figures)
#csync()
return fig,dic,variab_models,used_models
figs=dict()
dics=dict()
number=0
letters=["a","b","c","d"]
metadata=""
data_versions=read_versions_dictionnary(data_versions_tag, data_versions_dir)
if len(plot_for_each_model) > 0 :
use_cached_proj_fields = False
for season in seasons:
title="(%s) %s"%(letters[number],season)
#
figs[season],dic,variab_models,models = afigure(experiment,season,title=title)
#
if dic is not None :
dics[season]=dic[experiment][season]
metadata+=TSU_metadata([experiment,ref_experiment],models, variable,table,data_versions,letters[number])
metadata+=TSU_metadata(["piControl"], variab_models,variable,table,data_versions,letters[number])
#cdrop(figs[exp][season])
number+=1
import os.path
if not os.path.exists(outdir):
os.makedirs(outdir)
with open("%s/%s%s_md.txt"%(outdir,figure_name,version),"w") as f:
f.write(metadata)
# create a figure wih labelbar (using same plot settings as for figures above)
labelbar_figure=afigure(experiment,seasons[0],labelbar="True",outfile="./fig_with_label.png")
# Call utility function for extracting labelbar and adding fill pattern for shadings
create_labelbar("./fig_with_label.png", "./insert.png",missing=False,scheme=scheme)
if figure_title in [ None, "None" ] :
figure_title="%s %s for %s and %s (%s vs %s)"%(auto_title_prefix,prettier_label.get(field_type,field_type),
prettier_label.get(experiment,experiment),prettier_label.get(variable,variable),proj_period,ref_period)
# Create multi-panel figure
page=cpage([
[figs[seasons[0]],figs[seasons[1]]],
[figs[seasons[2]],figs[seasons[3]]]],
title=figure_title,
insert="./insert.png",
**figure_details
)
#
outfile="%s_%s_%s_4seasons_%s_%s%s.png"%(variable,field_type,experiment,proj_period,data_versions_tag,version)
if drop_old_figures :
cdrop(page)
cfile(page,outdir+"/"+outfile)
os.system("cd %s ; ln -sf %s %s%s.png"%(outdir,outfile,figure_name,version))
#
small=outfile.replace(".png",".small.png")
os.system("cd %s ; convert -geometry 50%% %s %s"%(outdir,outfile,small))
os.system("cd %s ; ln -sf %s %s%s_small.png"%(outdir,small,figure_name,version))
#
os.system("rm ./insert.png")
os.system("rm ./fig_with_label.png")
Image(outfile,width=800)
dic[experiment][season][key][derivation_label][model] where key has values : reference, projection, change, rchange, schange, variability
for field_type in plot_for_each_model :
#
exp=experiment
if field_type=="variability":
exp="piControl"
plotargs=custom_plot_all_fields.copy()
plotargs.update(ranges.get(field_type,{}))
#
for season in seasons :
if season in dics :
allplots=plot(cens(dics[season][field_type][derivation_label]),**plotargs)
title=title="%s_%s_%s_%s_%s"%(variable,derivation_label,field_type,exp,season)
page=cpage(allplots,title=title,**all_models_page_args)
cdrop(page)
pagename="%s/all_models_%s_%s_%s_%s_%s_%s.png"%(outdir,variable,derivation_label,field_type,experiment,season,data_versions_tag)
cfile(page,pagename)