Skip to content

Commit d794eb3

Browse files
committed
Tune plotmap and its interface with a C-ESM-EP run. Fix hurs plot params
For hurs bias : avoid defining 24 levels when using a 12 colors colormap
1 parent 9532196 commit d794eb3

File tree

6 files changed

+82
-27
lines changed

6 files changed

+82
-27
lines changed

climaf/classes.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,10 @@ def __init__(self, **kwargs):
642642
self.frequency = attval.get('frequency', "*")
643643
# Normalized name is annual_cycle, but allow also for 'seasonal' for the time being
644644
if self.frequency in ['seasonal', 'annual_cycle']:
645-
self.period.fx = True
645+
if type(self.period) is cperiod:
646+
self.period.fx = True
647+
else:
648+
self.period = cperiod("fx")
646649
freqs_dic = frequencies.get(self.project, None)
647650
if freqs_dic:
648651
for k in freqs_dic:

climaf/find_files.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def selectGenericFiles(urls, kwargs, return_combinations=None, use_frequency=Fal
246246

247247

248248
def fixed_frequency(kwargs):
249-
return kwargs.get('frequency') in ["fx", "seasonnal", "annual_cycle"] or \
249+
return kwargs.get('frequency') in ["fx", "seasonal", "annual_cycle"] or \
250250
kwargs.get('period') in [cperiod("fx"), ] or \
251251
kwargs.get('table') in ['fx', ]
252252

@@ -278,7 +278,7 @@ def check_period_and_store(f, period, values, kwargs, wildcards, merge_periods_o
278278
if fixed_frequency(kwargs):
279279
return store_wildcard_facet_values(f, values, kwargs, wildcards, merge_periods_on, combinations)
280280
else:
281-
fperiod = values['period']
281+
fperiod = values.get('period', values.get('clim_period'))
282282
if fperiod and isinstance(fperiod, six.string_types):
283283
fperiod = init_period(fperiod)
284284
#

climaf/plot/atmos_plot_params.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
'hurs': {
2828
'default': {'focus': 'ocean'},
2929
'full_field': {'colors': ranges_to_string(ranges=[72, 92, 2]), 'color': 'precip_11lev'},
30-
'bias': {'colors': ranges_to_string(ranges=[0, 10, 1], sym=True), 'color': 'precip_diff_12lev'},
31-
'model_model': {'colors': ranges_to_string(ranges=[0, 10, 1], sym=True), 'color': 'precip_diff_12lev'},
30+
'bias': {'colors': ranges_to_string(ranges=[0, 10, 2], sym=True), 'color': 'precip_diff_12lev'},
31+
'model_model': {'colors': ranges_to_string(ranges=[0, 10, 2], sym=True), 'color': 'precip_diff_12lev'},
3232
},
3333
'rstt': {
3434
'full_field': {'colors': ranges_to_string(ranges=[0, 320, 20]), 'color': 'WhiteBlueGreenYellowRed'},

climaf/plot_operators.py

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from climaf import __path__ as cpath
88
from climaf import operators
99
from climaf.operators import cscript, fixed_fields
10+
from climaf.cache import cdrop
1011

1112
scriptpath = cpath[0] + '/../scripts/'
1213
binpath = cpath[0] + '/../bin/'
@@ -95,8 +96,10 @@ def load_plot_operators():
9596
"--contours_map_max='${cntx}' "
9697
"--contours_map_scale='${contours_map_scale}' "
9798
"--contours_map_scale='${cnts}' "
99+
"--scale_aux=${scale_aux} "
98100
"--contours_map_offset='${contours_map_offset}' "
99101
"--contours_map_offset='${cnto}' "
102+
"--offset_aux=${offset_aux} "
100103
#
101104
# Vector map
102105
#
@@ -180,15 +183,22 @@ def load_plot_operators():
180183

181184

182185
def plot(*largs, forbid_plotmap=False, **kwargs):
183-
"""
184-
A replacement for old CliMAF plot operator, which uses plotmap,
185-
simplifies the plot if needed, and keep tracks of arguments not managed by plotmaps
186+
"""
187+
A replacement for old CliMAF plot operator, which uses plotmap
188+
if possible, simplifies the plot if needed, and keep tracks of
189+
arguments not managed by plotmap
190+
186191
"""
187192
caller = sys._getframe().f_back.f_code.co_name
188193

189194
if not env.environment.plot_use_plotmap or forbid_plotmap:
195+
if not env.environment.plot_use_plotmap :
196+
reason = "because of global setting"
197+
else:
198+
reason = "because of call argument"
190199
rep = operators.plot(*largs, **kwargs)
191-
clogger.info("Plotmap warning: using old plot for %s. Caller is :%s" % (rep.crs, caller))
200+
clogger.info("Plotmap warning: using old plot for %s %s. Caller is :%s" %
201+
(rep.crs, reason, caller))
192202
return (rep)
193203

194204
# Should check that input data has two horizontal dimensions ...
@@ -226,7 +236,6 @@ def plot(*largs, forbid_plotmap=False, **kwargs):
226236
"Gnomonic", "Mercator", "LambertConformal", "Robinson",
227237
"Hammer", "Mollweide", "PlateCarree"]
228238
must_use_old_plot = False
229-
230239
# shade_above et al. : a affiner
231240
outargs = dict()
232241
for arg in kwargs.keys():
@@ -245,9 +254,10 @@ def plot(*largs, forbid_plotmap=False, **kwargs):
245254
". Caller is :%s" % caller)
246255
outargs.pop(arg)
247256

248-
# "proj" : change Ncl names in Cartopy's , when applicable
257+
# "proj" : check that projection is known by plotmap; adapt default options
249258
if arg == "proj":
250259
proj = kwargs["proj"]
260+
clogger.info("Processing proj %s"%proj)
251261
if proj[0:2] in ["NH", "SH"]:
252262
pass
253263
elif proj in compatible_projections:
@@ -329,7 +339,14 @@ def plot(*largs, forbid_plotmap=False, **kwargs):
329339
outargs[oarg] = 'streamplot'
330340

331341
elif arg.lower() == "mpcenterlonf":
332-
outargs["proj_options"] = {'central_longitude': kwargs[arg]}
342+
if "proj" in kwargs and kwargs['proj'][0:2] not in ["NH", "SH"]:
343+
if 'proj_options' not in outargs:
344+
outargs['proj_options'] = dict()
345+
outargs['proj_options']['central_longitude'] = kwargs[arg]
346+
else:
347+
# Mimic NCl behaviour, which just neglect that option
348+
# for polar stereo projections
349+
pass
333350

334351
elif arg == "gsnLeftString":
335352
if "title_options" not in outargs:
@@ -381,13 +398,20 @@ def plot(*largs, forbid_plotmap=False, **kwargs):
381398
# if 'proj' not in outargs:
382399
# outargs["proj"] = "PlateCarree"
383400

401+
if 'proj' in outargs:
402+
if outargs['proj'] == 'Robinson':
403+
if 'proj_options' not in outargs:
404+
outargs['proj_options'] = dict()
405+
if 'central_longitude' not in outargs['proj_options']:
406+
outargs['proj_options']['central_longitude'] = 180.
407+
384408
if must_use_old_plot:
385409
rep = operators.plot(*largs, **kwargs)
386410
clogger.info("Plotmap warning: using old plot for %s" % rep.crs +
387411
". Caller is :%s" % caller)
388-
return (rep)
389-
390-
rep = operators.plotmap(main, aux, u, v, shade2, **outargs)
391-
if env.environment.teach_me_plotmap:
392-
print("Plotmap call: %s" % rep.crs)
412+
else:
413+
rep = operators.plotmap(main, aux, u, v, shade2, **outargs)
414+
if env.environment.teach_me_plotmap:
415+
print("Plotmap call: %s" % rep.crs)
416+
#cdrop(rep)
393417
return (rep)

scripts/plotmap.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,15 @@ def find_ccrs(crs_name, options=dict(), data_filename=None):
103103

104104
# If there is a file, try yo get CRS from its metadata
105105
if fic is not None:
106-
crs_name, options = ccrs_from_metadata(fic)
107-
106+
crs_name, file_options = ccrs_from_metadata(fic)
107+
file_options.update(options)
108+
options = file_options
109+
108110
# Default CRS is PlateCarree
109111
if crs_name is None:
110112
# raise ValueError(
111113
# "crs_name is None (can't deduce it from file %s)" % fic)
112-
return ccrs.PlateCarree()
114+
return ccrs.PlateCarree(**options)
113115

114116
if debug:
115117
print("In find_ccrs, crs_name=", crs_name)
@@ -127,6 +129,8 @@ def ccrs_from_metadata(filename):
127129
http://cfconventions.org/Data/cf-conventions/cf-conventions-1.8/cf-conventions.html#appendix-grid-mappings
128130
129131
Yet limited to the case of Lambert Conformal projection, as coded in Aladin model outputs.
132+
133+
Could be easily improved to identify PlateCaree, based on even lat and lon cooordinates
130134
"""
131135
ccrs_name = None
132136
ccrs_options = {}
@@ -226,7 +230,8 @@ def get_variable_and_coordinates_from_dataset(
226230
variable_dataset, dimensions, selection_options)
227231
for d in dimensions:
228232
if len(variable_dataset[d]) < 2:
229-
raise ValueError("Dimension %s has too short a size for a map (%d)" % (d, len(variable_dataset[d])))
233+
raise ValueError("Dimension %s has too short a size for a map (%d)" % \
234+
(d, len(variable_dataset[d])))
230235

231236
# add cyclic point if one of the dimensions is a longitude and longitude range is ~ 360
232237
d0 = dimensions[0] # Name of first dimension
@@ -240,8 +245,11 @@ def get_variable_and_coordinates_from_dataset(
240245
if lon_range > 359.9 or lon_range < 0.1:
241246
if debug:
242247
print("Adding cyclic longitude")
243-
variable_dataset = gv.xr_add_cyclic_longitudes(
244-
variable_dataset, d0)
248+
try:
249+
variable_dataset = gv.xr_add_cyclic_longitudes(
250+
variable_dataset, d0)
251+
except:
252+
pass
245253

246254
# Retrieve and possibly compute coordinates data
247255
variable_coordinates_data = list()
@@ -316,7 +324,7 @@ def create_norm(nlevels, cmap, z, zmin, zmax):
316324
levels = MaxNLocator(nbins=nlevels).tick_values(zmin, zmax)
317325
if type(cmap) is str:
318326
cmap = plt.colormaps[cmap]
319-
norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True)
327+
norm = BoundaryNorm(levels, ncolors=cmap.N, extend='both', clip=True)
320328
return norm
321329

322330

@@ -401,7 +409,23 @@ def plot_colored_map(fig, ax, coordinates, colored_map_file, colored_map_variabl
401409
contourf_args = dict(zorder=0, cmap=colored_map_cmap)
402410
contourf_args.update(colored_map_engine_options)
403411
if colored_map_levels is not None and 'levels' not in contourf_args:
412+
colored_map_levels = [ float(l) for l in colored_map_levels ]
404413
contourf_args['levels'] = colored_map_levels
414+
415+
# Mimic Ncl, which copes with too short colormaps
416+
# Method is quite direct : forgot some of the highest levels
417+
if colored_map_cmap.N < len(colored_map_levels) + 1:
418+
to_skip = len(colored_map_levels) + 1 - colored_map_cmap.N
419+
colored_map_levels = colored_map_levels[0: -to_skip ]
420+
if debug:
421+
print("Reducing the number of levels to match the number of colors")
422+
print("Levels list is now ", colored_map_levels)
423+
424+
contourf_args['norm'] = BoundaryNorm(colored_map_levels,
425+
ncolors=colored_map_cmap.N,
426+
extend='both')
427+
428+
contourf_args['extend'] = 'both'
405429
contourf_args["transform"] = transform
406430
#
407431
if colored_map_engine == "contourf":

scripts/plotmap_parsing.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,6 @@ def mimic_gplot(args, selection_options_list):
438438
For most cases, this consists in changing 'args'
439439
For the remaining cases, this translates in returned dict 'settings'
440440
441-
Currenty handles : trim, date, time, units, focus, vcb
442441
"""
443442

444443
settings = dict()
@@ -497,6 +496,8 @@ def mimic_gplot(args, selection_options_list):
497496
"https://scitools.org.uk/cartopy/docs/latest/gallery/lines_and_polygons/always_circular_stereo.html")
498497
if len(args.projection) > 2:
499498
latitude_limit = float(args.projection[2:])
499+
else:
500+
latitude_limit = 50.
500501
ranges_dict = dict(lon_range=[-180, 180])
501502
if args.projection[0:2] == 'NH':
502503
args.projection = "NorthPolarStereo"
@@ -512,7 +513,10 @@ def mimic_gplot(args, selection_options_list):
512513
if args.projection_options is None:
513514
args.projection_options = dict()
514515
if "central_longitude" not in args.projection_options:
515-
args.projection_options["central_longitude"] = 0.0
516+
if args.projection in [ "NorthPolarStereo", "SouthPolarStereo" ]:
517+
args.projection_options["central_longitude"] = 0.0
518+
else:
519+
args.projection_options["central_longitude"] = 0.0
516520
if 'gridlines' not in args.axis_methods:
517521
args.axis_methods['gridlines'] = [dict(draw_labels=True, linestyle="--",
518522
color='black', alpha=0.5)]
@@ -579,7 +583,7 @@ def mimic_gplot(args, selection_options_list):
579583
args.colored_map_min,
580584
args.colored_map_max + args.colored_map_delta,
581585
args.colored_map_delta))
582-
cdic['levels'] = args.colored_map_levels
586+
#cdic['levels'] = args.colored_map_levels
583587

584588
if args.debug:
585589
print("Colors/levels options for colormap engine=", cdic)

0 commit comments

Comments
 (0)