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