1 | def errormess(text): |
---|
2 | print text |
---|
3 | exit() |
---|
4 | return |
---|
5 | |
---|
6 | def whatkindfile (nc): |
---|
7 | if 'controle' in nc.variables: typefile = 'gcm' |
---|
8 | elif 'vert' in nc.variables: typefile = 'mesoapi' |
---|
9 | elif 'U' in nc.variables: typefile = 'meso' |
---|
10 | elif 'HGT_M' in nc.variables: typefile = 'geo' |
---|
11 | else: errormess("whatkindfile: typefile not supported.") |
---|
12 | return typefile |
---|
13 | |
---|
14 | def getfield (nc,var): |
---|
15 | ## this allows to get much faster (than simply referring to nc.variables[var]) |
---|
16 | dimension = len(nc.variables[var].dimensions) |
---|
17 | if dimension == 2: field = nc.variables[var][:,:] |
---|
18 | elif dimension == 3: field = nc.variables[var][:,:,:] |
---|
19 | elif dimension == 4: field = nc.variables[var][:,:,:,:] |
---|
20 | return field |
---|
21 | |
---|
22 | def reducefield (input,d4=None,d3=None,d2=None,d1=None): |
---|
23 | ### do it the reverse way to be compliant with netcdf "t z y x" or "t y x" or "y x" |
---|
24 | ### it would be actually better to name d4 d3 d2 d1 as t z y x |
---|
25 | import numpy as np |
---|
26 | dimension = np.array(input).ndim |
---|
27 | shape = np.array(input).shape |
---|
28 | print 'dim,shape: ',dimension,shape |
---|
29 | output = input |
---|
30 | error = False |
---|
31 | if dimension == 2: |
---|
32 | if d2 >= shape[0]: error = True |
---|
33 | elif d1 >= shape[1]: error = True |
---|
34 | elif d1 is not None and d2 is not None: output = input[d2,d1] |
---|
35 | elif d1 is not None: output = input[:,d1] |
---|
36 | elif d2 is not None: output = input[d2,:] |
---|
37 | elif dimension == 3: |
---|
38 | if d4 >= shape[0]: error = True |
---|
39 | elif d2 >= shape[1]: error = True |
---|
40 | elif d1 >= shape[2]: error = True |
---|
41 | elif d4 is not None and d2 is not None and d1 is not None: output = input[d4,d2,d1] |
---|
42 | elif d4 is not None and d2 is not None: output = input[d4,d2,:] |
---|
43 | elif d4 is not None and d1 is not None: output = input[d4,:,d1] |
---|
44 | elif d2 is not None and d1 is not None: output = input[:,d2,d1] |
---|
45 | elif d1 is not None: output = input[:,:,d1] |
---|
46 | elif d2 is not None: output = input[:,d2,:] |
---|
47 | elif d4 is not None: output = input[d4,:,:] |
---|
48 | elif dimension == 4: |
---|
49 | if d4 >= shape[0]: error = True |
---|
50 | elif d3 >= shape[1]: error = True |
---|
51 | elif d2 >= shape[2]: error = True |
---|
52 | elif d1 >= shape[3]: error = True |
---|
53 | elif d4 is not None and d3 is not None and d2 is not None and d1 is not None: output = input[d4,d3,d2,d1] |
---|
54 | elif d4 is not None and d3 is not None and d2 is not None: output = input[d4,d3,d2,:] |
---|
55 | elif d4 is not None and d3 is not None and d1 is not None: output = input[d4,d3,:,d1] |
---|
56 | elif d4 is not None and d2 is not None and d1 is not None: output = input[d4,:,d2,d1] |
---|
57 | elif d3 is not None and d2 is not None and d1 is not None: output = input[:,d3,d2,d1] |
---|
58 | elif d4 is not None and d3 is not None: output = input[d4,d3,:,:] |
---|
59 | elif d4 is not None and d2 is not None: output = input[d4,:,d2,:] |
---|
60 | elif d4 is not None and d1 is not None: output = input[d4,:,:,d1] |
---|
61 | elif d3 is not None and d2 is not None: output = input[:,d3,d2,:] |
---|
62 | elif d3 is not None and d1 is not None: output = input[:,d3,:,d1] |
---|
63 | elif d2 is not None and d1 is not None: output = input[:,:,d2,d1] |
---|
64 | elif d1 is not None: output = input[:,:,:,d1] |
---|
65 | elif d2 is not None: output = input[:,:,d2,:] |
---|
66 | elif d3 is not None: output = input[:,d3,:,:] |
---|
67 | elif d4 is not None: output = input[d4,:,:,:] |
---|
68 | dimension = np.array(output).ndim |
---|
69 | shape = np.array(output).shape |
---|
70 | print 'dim,shape: ',dimension,shape |
---|
71 | return output, error |
---|
72 | |
---|
73 | def latinterv (area): |
---|
74 | if area == "Europe": |
---|
75 | wlat = [20.,80.] |
---|
76 | wlon = [-50.,50.] |
---|
77 | elif area == "Central_America": |
---|
78 | wlat = [-10.,40.] |
---|
79 | wlon = [230.,300.] |
---|
80 | elif area == "Africa": |
---|
81 | wlat = [-20.,50.] |
---|
82 | wlon = [-50.,50.] |
---|
83 | elif area == "Whole": |
---|
84 | wlat = [-90.,90.] |
---|
85 | wlon = [-180.,180.] |
---|
86 | elif area == "Southern_Hemisphere": |
---|
87 | wlat = [-90.,60.] |
---|
88 | wlon = [-180.,180.] |
---|
89 | elif area == "Northern_Hemisphere": |
---|
90 | wlat = [-60.,90.] |
---|
91 | wlon = [-180.,180.] |
---|
92 | elif area == "Tharsis": |
---|
93 | wlat = [-30.,60.] |
---|
94 | wlon = [-170.,-10.] |
---|
95 | elif area == "Whole_No_High": |
---|
96 | wlat = [-60.,60.] |
---|
97 | wlon = [-180.,180.] |
---|
98 | elif area == "Chryse": |
---|
99 | wlat = [-60.,60.] |
---|
100 | wlon = [-60.,60.] |
---|
101 | elif area == "North_Pole": |
---|
102 | wlat = [60.,90.] |
---|
103 | wlon = [-180.,180.] |
---|
104 | elif area == "Close_North_Pole": |
---|
105 | wlat = [75.,90.] |
---|
106 | wlon = [-180.,180.] |
---|
107 | return wlon,wlat |
---|
108 | |
---|
109 | def definesubplot ( numplot, fig ): |
---|
110 | from matplotlib.pyplot import rcParams |
---|
111 | rcParams['font.size'] = 12. ## default (important for multiple calls) |
---|
112 | if numplot == 4: |
---|
113 | sub = 221 |
---|
114 | fig.subplots_adjust(wspace = 0.3, hspace = 0.3) |
---|
115 | rcParams['font.size'] = int( rcParams['font.size'] * 2. / 3. ) |
---|
116 | elif numplot == 2: |
---|
117 | sub = 121 |
---|
118 | fig.subplots_adjust(wspace = 0.35) |
---|
119 | rcParams['font.size'] = int( rcParams['font.size'] * 3. / 4. ) |
---|
120 | elif numplot == 3: |
---|
121 | sub = 131 |
---|
122 | fig.subplots_adjust(wspace = 0.5) |
---|
123 | rcParams['font.size'] = int( rcParams['font.size'] * 1. / 2. ) |
---|
124 | elif numplot == 6: |
---|
125 | sub = 231 |
---|
126 | fig.subplots_adjust(wspace = 0.4, hspace = 0.0) |
---|
127 | rcParams['font.size'] = int( rcParams['font.size'] * 1. / 2. ) |
---|
128 | elif numplot == 8: |
---|
129 | sub = 331 #241 |
---|
130 | fig.subplots_adjust(wspace = 0.3, hspace = 0.3) |
---|
131 | rcParams['font.size'] = int( rcParams['font.size'] * 1. / 2. ) |
---|
132 | elif numplot == 9: |
---|
133 | sub = 331 |
---|
134 | fig.subplots_adjust(wspace = 0.3, hspace = 0.3) |
---|
135 | rcParams['font.size'] = int( rcParams['font.size'] * 1. / 2. ) |
---|
136 | elif numplot == 1: |
---|
137 | sub = 99999 |
---|
138 | elif numplot < 0: |
---|
139 | sub = 99999 |
---|
140 | else: |
---|
141 | print "supported: 1,2,3,4,6,8,9" |
---|
142 | exit() |
---|
143 | return sub |
---|
144 | |
---|
145 | def getstralt(nc,nvert): |
---|
146 | typefile = whatkindfile(nc) |
---|
147 | if typefile is 'meso': |
---|
148 | stralt = "_lvl" + str(nvert) |
---|
149 | elif typefile is 'mesoapi': |
---|
150 | zelevel = int(nc.variables['vert'][nvert]) |
---|
151 | if abs(zelevel) < 10000.: strheight=str(zelevel)+"m" |
---|
152 | else: strheight=str(int(zelevel/1000.))+"km" |
---|
153 | if 'altitude' in nc.dimensions: stralt = "_"+strheight+"-AMR" |
---|
154 | elif 'altitude_abg' in nc.dimensions: stralt = "_"+strheight+"-ALS" |
---|
155 | elif 'bottom_top' in nc.dimensions: stralt = "_"+strheight |
---|
156 | elif 'pressure' in nc.dimensions: stralt = "_"+str(zelevel)+"Pa" |
---|
157 | else: stralt = "" |
---|
158 | else: |
---|
159 | stralt = "" |
---|
160 | return stralt |
---|
161 | |
---|
162 | def getlschar ( namefile ): |
---|
163 | from netCDF4 import Dataset |
---|
164 | from timestuff import sol2ls |
---|
165 | from numpy import array |
---|
166 | nc = Dataset(namefile) |
---|
167 | zetime = None |
---|
168 | if 'Times' in nc.variables: |
---|
169 | zetime = nc.variables['Times'][0] |
---|
170 | shape = array(nc.variables['Times']).shape |
---|
171 | if shape[0] < 2: zetime = None |
---|
172 | if zetime is not None \ |
---|
173 | and 'vert' not in nc.variables: |
---|
174 | #### strangely enough this does not work for api or ncrcat results! |
---|
175 | zetimestart = getattr(nc, 'START_DATE') |
---|
176 | zeday = int(zetime[8]+zetime[9]) - int(zetimestart[8]+zetimestart[9]) |
---|
177 | if zeday < 0: lschar="" ## might have crossed a month... fix soon |
---|
178 | else: lschar="_Ls"+str( int( sol2ls ( getattr( nc, 'JULDAY' ) + zeday ) ) ) |
---|
179 | ### |
---|
180 | zetime2 = nc.variables['Times'][1] |
---|
181 | one = int(zetime[11]+zetime[12]) + int(zetime[14]+zetime[15])/37. |
---|
182 | next = int(zetime2[11]+zetime2[12]) + int(zetime2[14]+zetime2[15])/37. |
---|
183 | zehour = one |
---|
184 | zehourin = abs ( next - one ) |
---|
185 | else: |
---|
186 | lschar="" |
---|
187 | zehour = 0 |
---|
188 | zehourin = 1 |
---|
189 | return lschar, zehour, zehourin |
---|
190 | |
---|
191 | def getprefix (nc): |
---|
192 | prefix = 'LMD_MMM_' |
---|
193 | prefix = prefix + 'd'+str(getattr(nc,'GRID_ID'))+'_' |
---|
194 | prefix = prefix + str(int(getattr(nc,'DX')/1000.))+'km_' |
---|
195 | return prefix |
---|
196 | |
---|
197 | def getproj (nc): |
---|
198 | typefile = whatkindfile(nc) |
---|
199 | if typefile in ['mesoapi','meso','geo']: |
---|
200 | ### (il faudrait passer CEN_LON dans la projection ?) |
---|
201 | map_proj = getattr(nc, 'MAP_PROJ') |
---|
202 | cen_lat = getattr(nc, 'CEN_LAT') |
---|
203 | if map_proj == 2: |
---|
204 | if cen_lat > 10.: |
---|
205 | proj="npstere" |
---|
206 | print "NP stereographic polar domain" |
---|
207 | else: |
---|
208 | proj="spstere" |
---|
209 | print "SP stereographic polar domain" |
---|
210 | elif map_proj == 1: |
---|
211 | print "lambert projection domain" |
---|
212 | proj="lcc" |
---|
213 | elif map_proj == 3: |
---|
214 | print "mercator projection" |
---|
215 | proj="merc" |
---|
216 | else: |
---|
217 | proj="merc" |
---|
218 | elif typefile in ['gcm']: proj="cyl" ## pb avec les autres (de trace derriere la sphere ?) |
---|
219 | else: proj="ortho" |
---|
220 | return proj |
---|
221 | |
---|
222 | def ptitle (name): |
---|
223 | from matplotlib.pyplot import title |
---|
224 | title(name) |
---|
225 | print name |
---|
226 | |
---|
227 | def simplinterv (lon2d,lat2d): |
---|
228 | import numpy as np |
---|
229 | return [[np.min(lon2d),np.max(lon2d)],[np.min(lat2d),np.max(lat2d)]] |
---|
230 | |
---|
231 | def wrfinterv (lon2d,lat2d): |
---|
232 | nx = len(lon2d[0,:])-1 |
---|
233 | ny = len(lon2d[:,0])-1 |
---|
234 | lon1 = lon2d[0,0] |
---|
235 | lon2 = lon2d[nx,ny] |
---|
236 | lat1 = lat2d[0,0] |
---|
237 | lat2 = lat2d[nx,ny] |
---|
238 | if abs(0.5*(lat1+lat2)) > 60.: wider = 0.5 * (abs(lon1)+abs(lon2)) * 0.1 |
---|
239 | else: wider = 0. |
---|
240 | if lon1 < lon2: wlon = [lon1, lon2 + wider] |
---|
241 | else: wlon = [lon2, lon1 + wider] |
---|
242 | if lat1 < lat2: wlat = [lat1, lat2] |
---|
243 | else: wlat = [lat2, lat1] |
---|
244 | return [wlon,wlat] |
---|
245 | |
---|
246 | def makeplotpngres (filename,res,pad_inches_value=0.25,folder='',disp=True): |
---|
247 | import matplotlib.pyplot as plt |
---|
248 | res = int(res) |
---|
249 | name = filename+"_"+str(res)+".png" |
---|
250 | if folder != '': name = folder+'/'+name |
---|
251 | plt.savefig(name,dpi=res,bbox_inches='tight',pad_inches=pad_inches_value) |
---|
252 | if disp: display(name) |
---|
253 | return |
---|
254 | |
---|
255 | def makeplotpng (filename,pad_inches_value=0.25,minres=100.,folder='',disp=True): |
---|
256 | makeplotpngres(filename,minres, pad_inches_value=pad_inches_value,folder=folder,disp=disp) |
---|
257 | makeplotpngres(filename,minres+200.,pad_inches_value=pad_inches_value,folder=folder,disp=False) |
---|
258 | return |
---|
259 | |
---|
260 | def dumpbdy (field,stag=None): |
---|
261 | nx = len(field[0,:])-1 |
---|
262 | ny = len(field[:,0])-1 |
---|
263 | if stag == 'U': nx = nx-1 |
---|
264 | if stag == 'V': ny = ny-1 |
---|
265 | return field[5:ny-5,5:nx-5] |
---|
266 | |
---|
267 | def getcoorddef ( nc ): |
---|
268 | ## getcoord2d for predefined types |
---|
269 | typefile = whatkindfile(nc) |
---|
270 | if typefile in ['mesoapi','meso']: |
---|
271 | [lon2d,lat2d] = getcoord2d(nc) |
---|
272 | lon2d = dumpbdy(lon2d) |
---|
273 | lat2d = dumpbdy(lat2d) |
---|
274 | elif typefile in ['gcm']: |
---|
275 | [lon2d,lat2d] = getcoord2d(nc,nlat="latitude",nlon="longitude",is1d=True) |
---|
276 | elif typefile in ['geo']: |
---|
277 | [lon2d,lat2d] = getcoord2d(nc,nlat='XLAT_M',nlon='XLONG_M') |
---|
278 | return lon2d,lat2d |
---|
279 | |
---|
280 | def getcoord2d (nc,nlat='XLAT',nlon='XLONG',is1d=False): |
---|
281 | import numpy as np |
---|
282 | if is1d: |
---|
283 | lat = nc.variables[nlat][:] |
---|
284 | lon = nc.variables[nlon][:] |
---|
285 | [lon2d,lat2d] = np.meshgrid(lon,lat) |
---|
286 | else: |
---|
287 | lat = nc.variables[nlat][0,:,:] |
---|
288 | lon = nc.variables[nlon][0,:,:] |
---|
289 | [lon2d,lat2d] = [lon,lat] |
---|
290 | return lon2d,lat2d |
---|
291 | |
---|
292 | def smooth (field, coeff): |
---|
293 | ## actually blur_image could work with different coeff on x and y |
---|
294 | if coeff > 1: result = blur_image(field,int(coeff)) |
---|
295 | else: result = field |
---|
296 | return result |
---|
297 | |
---|
298 | def gauss_kern(size, sizey=None): |
---|
299 | import numpy as np |
---|
300 | ## FROM COOKBOOK http://www.scipy.org/Cookbook/SignalSmooth |
---|
301 | # Returns a normalized 2D gauss kernel array for convolutions |
---|
302 | size = int(size) |
---|
303 | if not sizey: |
---|
304 | sizey = size |
---|
305 | else: |
---|
306 | sizey = int(sizey) |
---|
307 | x, y = np.mgrid[-size:size+1, -sizey:sizey+1] |
---|
308 | g = np.exp(-(x**2/float(size)+y**2/float(sizey))) |
---|
309 | return g / g.sum() |
---|
310 | |
---|
311 | def blur_image(im, n, ny=None) : |
---|
312 | from scipy.signal import convolve |
---|
313 | ## FROM COOKBOOK http://www.scipy.org/Cookbook/SignalSmooth |
---|
314 | # blurs the image by convolving with a gaussian kernel of typical size n. |
---|
315 | # The optional keyword argument ny allows for a different size in the y direction. |
---|
316 | g = gauss_kern(n, sizey=ny) |
---|
317 | improc = convolve(im, g, mode='same') |
---|
318 | return improc |
---|
319 | |
---|
320 | def getwinddef (nc): |
---|
321 | ## getwinds for predefined types |
---|
322 | typefile = whatkindfile(nc) |
---|
323 | ### |
---|
324 | if typefile is 'mesoapi': [uchar,vchar] = ['Um','Vm'] |
---|
325 | elif typefile is 'gcm': [uchar,vchar] = ['u','v'] |
---|
326 | elif typefile is 'meso': [uchar,vchar] = ['U','V'] |
---|
327 | else: [uchar,vchar] = ['not found','not found'] |
---|
328 | ### |
---|
329 | if typefile in ['meso']: metwind = False ## geometrical (wrt grid) |
---|
330 | else: metwind = True ## meteorological (zon/mer) |
---|
331 | if metwind is False: print "Not using meteorological winds. You trust numerical grid as being (x,y)" |
---|
332 | ### |
---|
333 | return uchar,vchar,metwind |
---|
334 | |
---|
335 | def vectorfield (u, v, x, y, stride=3, scale=15., factor=250., color='black', csmooth=1, key=True): |
---|
336 | ## scale regle la reference du vecteur |
---|
337 | ## factor regle toutes les longueurs (dont la reference). l'AUGMENTER pour raccourcir les vecteurs. |
---|
338 | import matplotlib.pyplot as plt |
---|
339 | import numpy as np |
---|
340 | posx = np.min(x) - np.std(x) / 10. |
---|
341 | posy = np.min(y) - np.std(y) / 10. |
---|
342 | u = smooth(u,csmooth) |
---|
343 | v = smooth(v,csmooth) |
---|
344 | widthvec = 0.003 #0.005 #0.003 |
---|
345 | q = plt.quiver( x[::stride,::stride],\ |
---|
346 | y[::stride,::stride],\ |
---|
347 | u[::stride,::stride],\ |
---|
348 | v[::stride,::stride],\ |
---|
349 | angles='xy',color=color,pivot='middle',\ |
---|
350 | scale=factor,width=widthvec ) |
---|
351 | if color in ['white','yellow']: kcolor='black' |
---|
352 | else: kcolor=color |
---|
353 | if key: p = plt.quiverkey(q,posx,posy,scale,\ |
---|
354 | str(int(scale)),coordinates='data',color=kcolor,labelpos='S',labelsep = 0.03) |
---|
355 | return |
---|
356 | |
---|
357 | def display (name): |
---|
358 | from os import system |
---|
359 | system("display "+name+" > /dev/null 2> /dev/null &") |
---|
360 | return name |
---|
361 | |
---|
362 | def findstep (wlon): |
---|
363 | steplon = int((wlon[1]-wlon[0])/4.) #3 |
---|
364 | step = 120. |
---|
365 | while step > steplon and step > 15. : step = step / 2. |
---|
366 | if step <= 15.: |
---|
367 | while step > steplon and step > 5. : step = step - 5. |
---|
368 | if step <= 5.: |
---|
369 | while step > steplon and step > 1. : step = step - 1. |
---|
370 | if step <= 1.: |
---|
371 | step = 1. |
---|
372 | return step |
---|
373 | |
---|
374 | def define_proj (char,wlon,wlat,back=None): |
---|
375 | from mpl_toolkits.basemap import Basemap |
---|
376 | import numpy as np |
---|
377 | import matplotlib as mpl |
---|
378 | meanlon = 0.5*(wlon[0]+wlon[1]) |
---|
379 | meanlat = 0.5*(wlat[0]+wlat[1]) |
---|
380 | if wlat[0] >= 80.: blat = 40. |
---|
381 | elif wlat[0] <= -80.: blat = -40. |
---|
382 | else: blat = wlat[0] |
---|
383 | h = 50. ## en km |
---|
384 | radius = 3397200. |
---|
385 | if char == "cyl": m = Basemap(rsphere=radius,projection='cyl',\ |
---|
386 | llcrnrlat=wlat[0],urcrnrlat=wlat[1],llcrnrlon=wlon[0],urcrnrlon=wlon[1]) |
---|
387 | elif char == "moll": m = Basemap(rsphere=radius,projection='moll',lon_0=meanlon) |
---|
388 | elif char == "ortho": m = Basemap(rsphere=radius,projection='ortho',lon_0=meanlon,lat_0=meanlat) |
---|
389 | elif char == "lcc": m = Basemap(rsphere=radius,projection='lcc',lat_1=meanlat,lat_0=meanlat,lon_0=meanlon,\ |
---|
390 | llcrnrlat=wlat[0],urcrnrlat=wlat[1],llcrnrlon=wlon[0],urcrnrlon=wlon[1]) |
---|
391 | elif char == "npstere": m = Basemap(rsphere=radius,projection='npstere', boundinglat=blat, lon_0=0.) |
---|
392 | elif char == "spstere": m = Basemap(rsphere=radius,projection='spstere', boundinglat=blat, lon_0=0.) |
---|
393 | elif char == "nplaea": m = Basemap(rsphere=radius,projection='nplaea', boundinglat=wlat[0], lon_0=meanlon) |
---|
394 | elif char == "laea": m = Basemap(rsphere=radius,projection='laea',lon_0=meanlon,lat_0=meanlat,lat_ts=meanlat,\ |
---|
395 | llcrnrlat=wlat[0],urcrnrlat=wlat[1],llcrnrlon=wlon[0],urcrnrlon=wlon[1]) |
---|
396 | elif char == "nsper": m = Basemap(rsphere=radius,projection='nsper',lon_0=meanlon,lat_0=meanlat,satellite_height=h*1000.) |
---|
397 | elif char == "merc": m = Basemap(rsphere=radius,projection='merc',lat_ts=0.,\ |
---|
398 | llcrnrlat=wlat[0],urcrnrlat=wlat[1],llcrnrlon=wlon[0],urcrnrlon=wlon[1]) |
---|
399 | fontsizemer = int(mpl.rcParams['font.size']*3./4.) |
---|
400 | if char in ["cyl","lcc","merc","nsper","laea"]: step = findstep(wlon) |
---|
401 | else: step = 10. |
---|
402 | steplon = step*2. |
---|
403 | #if back in ["geolocal"]: |
---|
404 | # step = np.min([5.,step]) |
---|
405 | # steplon = step |
---|
406 | print step |
---|
407 | m.drawmeridians(np.r_[-180.:180.:steplon], labels=[0,0,0,1], color='grey', fontsize=fontsizemer) |
---|
408 | m.drawparallels(np.r_[-90.:90.:step], labels=[1,0,0,0], color='grey', fontsize=fontsizemer) |
---|
409 | if back: m.warpimage(marsmap(back),scale=0.75) |
---|
410 | #if not back: |
---|
411 | # if not var: back = "mola" ## if no var: draw mola |
---|
412 | # elif typefile in ['mesoapi','meso','geo'] \ |
---|
413 | # and proj not in ['merc','lcc','nsper','laea']: back = "molabw" ## if var but meso: draw molabw |
---|
414 | # else: pass ## else: draw None |
---|
415 | return m |
---|
416 | |
---|
417 | #### test temporaire |
---|
418 | def putpoints (map,plot): |
---|
419 | #### from http://www.scipy.org/Cookbook/Matplotlib/Maps |
---|
420 | # lat/lon coordinates of five cities. |
---|
421 | lats = [18.4] |
---|
422 | lons = [-134.0] |
---|
423 | points=['Olympus Mons'] |
---|
424 | # compute the native map projection coordinates for cities. |
---|
425 | x,y = map(lons,lats) |
---|
426 | # plot filled circles at the locations of the cities. |
---|
427 | map.plot(x,y,'bo') |
---|
428 | # plot the names of those five cities. |
---|
429 | wherept = 0 #1000 #50000 |
---|
430 | for name,xpt,ypt in zip(points,x,y): |
---|
431 | plot.text(xpt+wherept,ypt+wherept,name) |
---|
432 | ## le nom ne s'affiche pas... |
---|
433 | return |
---|
434 | |
---|
435 | def calculate_bounds(field,vmin=None,vmax=None): |
---|
436 | import numpy as np |
---|
437 | from mymath import max,min,mean |
---|
438 | ind = np.where(field < 9e+35) |
---|
439 | fieldcalc = field[ ind ] # la syntaxe compacte ne marche si field est un tuple |
---|
440 | ### |
---|
441 | dev = np.std(fieldcalc)*3.0 |
---|
442 | ### |
---|
443 | if vmin is None: |
---|
444 | zevmin = mean(fieldcalc) - dev |
---|
445 | else: zevmin = vmin |
---|
446 | ### |
---|
447 | if vmax is None: zevmax = mean(fieldcalc) + dev |
---|
448 | else: zevmax = vmax |
---|
449 | if vmin == vmax: |
---|
450 | zevmin = mean(fieldcalc) - dev ### for continuity |
---|
451 | zevmax = mean(fieldcalc) + dev ### for continuity |
---|
452 | ### |
---|
453 | if zevmin < 0. and min(fieldcalc) > 0.: zevmin = 0. |
---|
454 | print "field ", min(fieldcalc), max(fieldcalc) |
---|
455 | print "bounds ", zevmin, zevmax |
---|
456 | return zevmin, zevmax |
---|
457 | |
---|
458 | def bounds(what_I_plot,zevmin,zevmax): |
---|
459 | ### might be convenient to add the missing value in arguments |
---|
460 | what_I_plot[ what_I_plot < zevmin ] = zevmin*(1. + 1.e-7) |
---|
461 | what_I_plot[ what_I_plot > 9e+35 ] = -9e+35 |
---|
462 | what_I_plot[ what_I_plot > zevmax ] = zevmax*(1. - 1.e-7) |
---|
463 | return what_I_plot |
---|
464 | |
---|
465 | def zoomset (wlon,wlat,zoom): |
---|
466 | dlon = abs(wlon[1]-wlon[0])/2. |
---|
467 | dlat = abs(wlat[1]-wlat[0])/2. |
---|
468 | [wlon,wlat] = [ [wlon[0]+zoom*dlon/100.,wlon[1]-zoom*dlon/100.],\ |
---|
469 | [wlat[0]+zoom*dlat/100.,wlat[1]-zoom*dlat/100.] ] |
---|
470 | print "zoom %",zoom,wlon,wlat |
---|
471 | return wlon,wlat |
---|
472 | |
---|
473 | def fmtvar (whichvar="def"): |
---|
474 | fmtvar = { \ |
---|
475 | "tk": "%.0f",\ |
---|
476 | "tpot": "%.0f",\ |
---|
477 | "def": "%.1e",\ |
---|
478 | "PTOT": "%.0f",\ |
---|
479 | "HGT": "%.1e",\ |
---|
480 | "USTM": "%.2f",\ |
---|
481 | "HFX": "%.0f",\ |
---|
482 | "ICETOT": "%.1e",\ |
---|
483 | "TAU_ICE": "%.2f",\ |
---|
484 | } |
---|
485 | if whichvar not in fmtvar: |
---|
486 | whichvar = "def" |
---|
487 | return fmtvar[whichvar] |
---|
488 | |
---|
489 | #################################################################################################################### |
---|
490 | ### Colorbars http://www.scipy.org/Cookbook/Matplotlib/Show_colormaps?action=AttachFile&do=get&target=colormaps3.png |
---|
491 | def defcolorb (whichone="def"): |
---|
492 | whichcolorb = { \ |
---|
493 | "def": "spectral",\ |
---|
494 | "HGT": "spectral",\ |
---|
495 | "tk": "gist_heat",\ |
---|
496 | "QH2O": "PuBu",\ |
---|
497 | "USTM": "YlOrRd",\ |
---|
498 | "HFX": "RdYlBu",\ |
---|
499 | "ICETOT": "YlGnBu",\ |
---|
500 | "TAU_ICE": "Blues",\ |
---|
501 | } |
---|
502 | if whichone not in whichcolorb: |
---|
503 | whichone = "def" |
---|
504 | return whichcolorb[whichone] |
---|
505 | |
---|
506 | def definecolorvec (whichone="def"): |
---|
507 | whichcolor = { \ |
---|
508 | "def": "black",\ |
---|
509 | "vis": "yellow",\ |
---|
510 | "vishires": "yellow",\ |
---|
511 | "molabw": "yellow",\ |
---|
512 | "mola": "black",\ |
---|
513 | "gist_heat": "white",\ |
---|
514 | "hot": "tk",\ |
---|
515 | "gist_rainbow": "black",\ |
---|
516 | "spectral": "black",\ |
---|
517 | "gray": "red",\ |
---|
518 | "PuBu": "black",\ |
---|
519 | } |
---|
520 | if whichone not in whichcolor: |
---|
521 | whichone = "def" |
---|
522 | return whichcolor[whichone] |
---|
523 | |
---|
524 | def marsmap (whichone="vishires"): |
---|
525 | from os import uname |
---|
526 | mymachine = uname()[1] |
---|
527 | ### not sure about speed-up with this method... looks the same |
---|
528 | if "lmd.jussieu.fr" in mymachine: domain = "/u/aslmd/WWW/maps/" |
---|
529 | else: domain = "http://www.lmd.jussieu.fr/~aslmd/maps/" |
---|
530 | whichlink = { \ |
---|
531 | #"vis": "http://maps.jpl.nasa.gov/pix/mar0kuu2.jpg",\ |
---|
532 | #"vishires": "http://www.lmd.jussieu.fr/~aslmd/maps/MarsMap_2500x1250.jpg",\ |
---|
533 | #"geolocal": "http://dl.dropbox.com/u/11078310/geolocal.jpg",\ |
---|
534 | #"mola": "http://www.lns.cornell.edu/~seb/celestia/mars-mola-2k.jpg",\ |
---|
535 | #"molabw": "http://dl.dropbox.com/u/11078310/MarsElevation_2500x1250.jpg",\ |
---|
536 | "vis": domain+"mar0kuu2.jpg",\ |
---|
537 | "vishires": domain+"MarsMap_2500x1250.jpg",\ |
---|
538 | "geolocal": domain+"geolocal.jpg",\ |
---|
539 | "mola": domain+"mars-mola-2k.jpg",\ |
---|
540 | "molabw": domain+"MarsElevation_2500x1250.jpg",\ |
---|
541 | "clouds": "http://www.johnstonsarchive.net/spaceart/marswcloudmap.jpg",\ |
---|
542 | "jupiter": "http://www.mmedia.is/~bjj/data/jupiter_css/jupiter_css.jpg",\ |
---|
543 | "jupiter_voy": "http://www.mmedia.is/~bjj/data/jupiter/jupiter_vgr2.jpg",\ |
---|
544 | } |
---|
545 | ### see http://www.mmedia.is/~bjj/planetary_maps.html |
---|
546 | if whichone not in whichlink: |
---|
547 | print "marsmap: choice not defined... you'll get the default one... " |
---|
548 | whichone = "vishires" |
---|
549 | return whichlink[whichone] |
---|
550 | |
---|
551 | def earthmap (whichone): |
---|
552 | if whichone == "contrast": whichlink="http://users.info.unicaen.fr/~karczma/TEACH/InfoGeo/Images/Planets/EarthMapAtmos_2500x1250.jpg" |
---|
553 | elif whichone == "bw": whichlink="http://users.info.unicaen.fr/~karczma/TEACH/InfoGeo/Images/Planets/EarthElevation_2500x1250.jpg" |
---|
554 | elif whichone == "nice": whichlink="http://users.info.unicaen.fr/~karczma/TEACH/InfoGeo/Images/Planets/earthmap1k.jpg" |
---|
555 | return whichlink |
---|
556 | |
---|