Skip to content

Commit d3ecb21

Browse files
Update to Mapping
1 parent 7865c0b commit d3ecb21

File tree

4 files changed

+44
-26
lines changed

4 files changed

+44
-26
lines changed

zapata/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
**koopman** :
2828
Routines for Koopman decomposition and eigenvalues handling
2929
30+
Version 2.1
3031
"""
3132
import warnings
3233
warnings.simplefilter('ignore')

zapata/colormap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from lxml import etree
3030

3131
homedir = os.path.expanduser("~")
32-
COLPATHDIR = homedir + '/Dropbox (CMCC)/GitHub/Zapata/zapata/SciVis_colormaps'
32+
COLPATHDIR = homedir + '/Dropbox (CMCC)/ZapataLibrary/Zapata/zapata/SciVis_colormaps'
3333

3434
def make_cmap(xml,colpath=COLPATHDIR):
3535
'''

zapata/computation.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ def anomaly(var,option='anom',freq='month'):
196196
anom : xarray
197197
198198
"""
199+
# Correction for very small standard deviations
200+
TINY = 1.0e-14
199201

200202
frequency = 'time.' + freq
201203
if option == 'deviation':
@@ -211,7 +213,7 @@ def anomaly(var,option='anom',freq='month'):
211213
anom = xr.apply_ufunc(lambda x, m, s: (x - m) / s,
212214
var.groupby(frequency),
213215
clim,
214-
climstd )
216+
climstd + TINY)
215217
else:
216218
print(' Wrong option in `anomaly` {}'.format(option))
217219
raise SystemExit
@@ -275,8 +277,7 @@ def __init__(
275277
self.A = X.stack(z=dims).transpose()
276278
self._ntime = len(X.time.data)
277279
self._npoints = len(X.stack(z=dims).z.data)
278-
print(' Created mathematical matrix A, \n \
279-
stacked along dimensions {} '.format(dims))
280+
print('Created data Matrix X, stacked along dimensions {} '.format(dims))
280281
if self._opt == 'DropNaN':
281282
self.A = self.A.dropna(dim='z')
282283
print(' Creating Matrix with Drop NaN values')
@@ -289,7 +290,7 @@ def __call__(self, v ):
289290

290291
def __repr__(self):
291292
''' Printing Information '''
292-
print(' \n Math Data Matrix \n {} \n'.format(self.A))
293+
print(f' \n Math Data Matrix \n {self.A}\n')
293294
print(f' Shape of A numpy array {self.A.shape}')
294295
return '\n'
295296

zapata/mapping.py

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
'''
2525
import math as mp
2626
from operator import is_
27-
import sys
27+
from collections import namedtuple
2828
import cartopy.crs as car
2929
import 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

Comments
 (0)