2424'''
2525import math as mp
2626from operator import is_
27- import sys
27+ from collections import namedtuple
2828import cartopy .crs as car
2929import cartopy .util as utl
3030
@@ -151,27 +151,31 @@ def choose_projection(proview):
151151 Projection object
152152 '''
153153
154- #Defaults for Satellite
154+ #Defaults for Satellite
155155 default_satellite = {'centlon' : 0.0 , 'centlat' : 0.0 , 'z' : 35785831 }
156156
157157 # This fixes the projection for the mapping:
158158 # Data Projection is then fixed in mapping routine
159159 # Select Projection parameters
160160 if isinstance (proview ,str ):
161- if proview == 'Pacific' :
162- projection = car .PlateCarree (central_longitude = 180. )
163- elif proview == 'Atlantic' :
164- projection = car .PlateCarree (central_longitude = 0. )
165- elif proview == 'NHStereoEurope' :
166- projection = car .NorthPolarStereo (central_longitude = 0. )
167- elif proview == 'NHStereoAmerica' :
168- projection = car .NorthPolarStereo (central_longitude = - 90. )
169- elif proview == 'SHStereoAfrica' :
170- projection = car .SouthPolarStereo (central_longitude = 90. )
171- elif proview == 'RobAtlantic' :
172- projection = car .Robinson (central_longitude = 0. )
173- elif proview == 'RobPacific' :
174- projection = car .Robinson (central_longitude = 180. )
161+ match proview :
162+ case 'Pacific' :
163+ projection = car .PlateCarree (central_longitude = 180. )
164+ case 'Atlantic' :
165+ projection = car .PlateCarree (central_longitude = 0. )
166+ case 'NHStereoEurope' :
167+ projection = car .NorthPolarStereo (central_longitude = 0. )
168+ case 'NHStereoAmerica' :
169+ projection = car .NorthPolarStereo (central_longitude = - 90. )
170+ case 'SHStereoAfrica' :
171+ projection = car .SouthPolarStereo (central_longitude = 90. )
172+ case 'RobAtlantic' :
173+ projection = car .Robinson (central_longitude = 0. )
174+ case 'RobPacific' :
175+ projection = car .Robinson (central_longitude = 180. )
176+ case _:
177+ print (f' Error in init_figure projection { proview } ' )
178+ print (f'It should be one of the following: Pacific, Atlantic, NHStereoEurope, NHStereoAmerica, SHStereoAfrica, RobAtlantic, RobPacific' )
175179 elif isinstance (proview ,dict ):
176180 temp = { ** default_satellite ,** proview }
177181 print (temp ['z' ])
@@ -202,7 +206,8 @@ def xmap(data, cont, prol, ax=None, fill=True,contour=True, \
202206 cyclic = False ,\
203207 colorbar = False ,cmap = 'coolwarm' ,\
204208 coasts = True ,color_land = 'lightgray' ,\
205- quiet = True ,\
209+ quiet = True ,
210+ custom = False ,\
206211 label_style = {}):
207212 """
208213 Mapping function based on cartopy and matplotlib.
@@ -273,6 +278,8 @@ def xmap(data, cont, prol, ax=None, fill=True,contour=True, \
273278 The projection used is the geographical projection `pro`
274279 quiet:
275280 (False) Suppress all output
281+ custom:
282+ (False) Return a named tuple with full detail on the plot, including handles, gl etc...
276283
277284
278285 Returns
@@ -360,7 +367,11 @@ def xmap(data, cont, prol, ax=None, fill=True,contour=True, \
360367 xlim = xlimit
361368
362369 if ylimit is None :
363- ylim = [np .amin (data .lat .values ),np .amax (data .lat .values )]
370+ match this :
371+ case 'NorthPolarStereo' :
372+ ylim = [20 ,90 ]
373+ case 'SouthPolarStereo' :
374+ ylim = [- 90 ,- 20 ]
364375 else :
365376 ylim = ylimit
366377 print (' Plotting with x limits {} ' .format (xlim ) )
@@ -386,12 +397,12 @@ def xmap(data, cont, prol, ax=None, fill=True,contour=True, \
386397
387398 cc = choose_contour (vmin ,vmax ,cont )
388399 if not quiet :
400+ print (title )
389401 print (f'Contouring from { vmin .data } to { vmax .data } with { cc } contours' )
390402 if cc is None :
391403 print (f'******** Constant field ***********' )
392404 return None
393405
394-
395406 handles = dict ()
396407 if fill :
397408 handles ['filled' ] = ax .contourf (data .lon , data .lat , data , levels = cc , cmap = cmap , transform = data_proj )
@@ -434,7 +445,12 @@ def xmap(data, cont, prol, ax=None, fill=True,contour=True, \
434445 cax = divider .append_axes ('right' ,size = "2.5%" , pad = 0.2 , axes_class = plt .Axes )
435446 ax .get_figure ().colorbar (handles ['filled' ], cax = cax ,orientation = 'vertical' )
436447
437- return handles
448+ # Choose output
449+ Output = namedtuple ('Output' , 'handles gl' )
450+ if custom :
451+ return Output (handles ,gl )
452+ else :
453+ return handles
438454
439455@d .get_sections (base = 'zonal_plot' )
440456@d .dedent
@@ -665,7 +681,7 @@ def choose_contour(vmin,vmax,cont):
665681 """
666682 if len (cont ) == 3 :
667683 print ("Setting Fixed Contours" )
668- cc = np .arange (cont [0 ]- cont [ 2 ] ,cont [1 ]+ 2 * cont [2 ],cont [2 ])
684+ cc = np .arange (cont [0 ],cont [1 ]+ cont [2 ],cont [2 ])
669685 print (' Contouring from ' , cont [0 ], ' to' , cont [1 ],' with interval ' ,cont [2 ])
670686 elif len (cont )> 3 :
671687 cc = cont
0 commit comments