[233] | 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 | |
---|
[180] | 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 | |
---|
[233] | 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 | |
---|
[195] | 162 | def getlschar ( namefile ): |
---|
| 163 | from netCDF4 import Dataset |
---|
| 164 | from timestuff import sol2ls |
---|
[233] | 165 | from numpy import array |
---|
[195] | 166 | nc = Dataset(namefile) |
---|
[237] | 167 | zetime = None |
---|
[233] | 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 \ |
---|
[225] | 173 | and 'vert' not in nc.variables: |
---|
[233] | 174 | #### strangely enough this does not work for api or ncrcat results! |
---|
[195] | 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 ) ) ) |
---|
[197] | 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 ) |
---|
[195] | 185 | else: |
---|
| 186 | lschar="" |
---|
[197] | 187 | zehour = 0 |
---|
| 188 | zehourin = 1 |
---|
| 189 | return lschar, zehour, zehourin |
---|
[195] | 190 | |
---|
[202] | 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 | |
---|
[184] | 197 | def getproj (nc): |
---|
[233] | 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" |
---|
[184] | 220 | return proj |
---|
| 221 | |
---|
[180] | 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 | |
---|
[184] | 231 | def wrfinterv (lon2d,lat2d): |
---|
| 232 | nx = len(lon2d[0,:])-1 |
---|
| 233 | ny = len(lon2d[:,0])-1 |
---|
[225] | 234 | lon1 = lon2d[0,0] |
---|
| 235 | lon2 = lon2d[nx,ny] |
---|
| 236 | lat1 = lat2d[0,0] |
---|
| 237 | lat2 = lat2d[nx,ny] |
---|
[233] | 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] |
---|
[225] | 241 | else: wlon = [lon2, lon1 + wider] |
---|
| 242 | if lat1 < lat2: wlat = [lat1, lat2] |
---|
| 243 | else: wlat = [lat2, lat1] |
---|
| 244 | return [wlon,wlat] |
---|
[184] | 245 | |
---|
[180] | 246 | def makeplotpngres (filename,res,pad_inches_value=0.25,folder='',disp=True): |
---|
| 247 | import matplotlib.pyplot as plt |
---|
| 248 | res = int(res) |
---|
[186] | 249 | name = filename+"_"+str(res)+".png" |
---|
| 250 | if folder != '': name = folder+'/'+name |
---|
[180] | 251 | plt.savefig(name,dpi=res,bbox_inches='tight',pad_inches=pad_inches_value) |
---|
[181] | 252 | if disp: display(name) |
---|
[180] | 253 | return |
---|
| 254 | |
---|
[233] | 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) |
---|
[180] | 257 | makeplotpngres(filename,minres+200.,pad_inches_value=pad_inches_value,folder=folder,disp=False) |
---|
| 258 | return |
---|
| 259 | |
---|
[233] | 260 | def dumpbdy (field,stag=None): |
---|
[184] | 261 | nx = len(field[0,:])-1 |
---|
| 262 | ny = len(field[:,0])-1 |
---|
[233] | 263 | if stag == 'U': nx = nx-1 |
---|
| 264 | if stag == 'V': ny = ny-1 |
---|
[184] | 265 | return field[5:ny-5,5:nx-5] |
---|
[180] | 266 | |
---|
[233] | 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 | |
---|
[184] | 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 | |
---|
[180] | 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 | |
---|
[233] | 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 |
---|
[202] | 334 | |
---|
[184] | 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 |
---|
[187] | 340 | posx = np.min(x) - np.std(x) / 10. |
---|
| 341 | posy = np.min(y) - np.std(y) / 10. |
---|
[184] | 342 | u = smooth(u,csmooth) |
---|
| 343 | v = smooth(v,csmooth) |
---|
[188] | 344 | widthvec = 0.003 #0.005 #0.003 |
---|
[184] | 345 | q = plt.quiver( x[::stride,::stride],\ |
---|
| 346 | y[::stride,::stride],\ |
---|
| 347 | u[::stride,::stride],\ |
---|
| 348 | v[::stride,::stride],\ |
---|
[228] | 349 | angles='xy',color=color,pivot='middle',\ |
---|
[184] | 350 | scale=factor,width=widthvec ) |
---|
[202] | 351 | if color in ['white','yellow']: kcolor='black' |
---|
| 352 | else: kcolor=color |
---|
[184] | 353 | if key: p = plt.quiverkey(q,posx,posy,scale,\ |
---|
[194] | 354 | str(int(scale)),coordinates='data',color=kcolor,labelpos='S',labelsep = 0.03) |
---|
[184] | 355 | return |
---|
[180] | 356 | |
---|
| 357 | def display (name): |
---|
[184] | 358 | from os import system |
---|
| 359 | system("display "+name+" > /dev/null 2> /dev/null &") |
---|
| 360 | return name |
---|
[180] | 361 | |
---|
| 362 | def findstep (wlon): |
---|
[184] | 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. |
---|
[180] | 372 | return step |
---|
| 373 | |
---|
[233] | 374 | def define_proj (char,wlon,wlat,back=None): |
---|
[180] | 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]) |
---|
[184] | 380 | if wlat[0] >= 80.: blat = 40. |
---|
| 381 | elif wlat[0] <= -80.: blat = -40. |
---|
| 382 | else: blat = wlat[0] |
---|
[207] | 383 | h = 50. ## en km |
---|
[202] | 384 | radius = 3397200. |
---|
[184] | 385 | if char == "cyl": m = Basemap(rsphere=radius,projection='cyl',\ |
---|
[180] | 386 | llcrnrlat=wlat[0],urcrnrlat=wlat[1],llcrnrlon=wlon[0],urcrnrlon=wlon[1]) |
---|
[184] | 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.) |
---|
[207] | 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]) |
---|
[184] | 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.) |
---|
[207] | 400 | if char in ["cyl","lcc","merc","nsper","laea"]: step = findstep(wlon) |
---|
| 401 | else: step = 10. |
---|
[225] | 402 | print step |
---|
[180] | 403 | m.drawmeridians(np.r_[-180.:180.:step*2.], labels=[0,0,0,1], color='grey', fontsize=fontsizemer) |
---|
| 404 | m.drawparallels(np.r_[-90.:90.:step], labels=[1,0,0,0], color='grey', fontsize=fontsizemer) |
---|
[233] | 405 | if back: m.warpimage(marsmap(back),scale=0.75) |
---|
| 406 | #if not back: |
---|
| 407 | # if not var: back = "mola" ## if no var: draw mola |
---|
| 408 | # elif typefile in ['mesoapi','meso','geo'] \ |
---|
| 409 | # and proj not in ['merc','lcc','nsper','laea']: back = "molabw" ## if var but meso: draw molabw |
---|
| 410 | # else: pass ## else: draw None |
---|
[180] | 411 | return m |
---|
| 412 | |
---|
[232] | 413 | #### test temporaire |
---|
| 414 | def putpoints (map,plot): |
---|
| 415 | #### from http://www.scipy.org/Cookbook/Matplotlib/Maps |
---|
| 416 | # lat/lon coordinates of five cities. |
---|
| 417 | lats = [18.4] |
---|
| 418 | lons = [-134.0] |
---|
| 419 | points=['Olympus Mons'] |
---|
| 420 | # compute the native map projection coordinates for cities. |
---|
| 421 | x,y = map(lons,lats) |
---|
| 422 | # plot filled circles at the locations of the cities. |
---|
| 423 | map.plot(x,y,'bo') |
---|
| 424 | # plot the names of those five cities. |
---|
| 425 | wherept = 0 #1000 #50000 |
---|
| 426 | for name,xpt,ypt in zip(points,x,y): |
---|
| 427 | plot.text(xpt+wherept,ypt+wherept,name) |
---|
| 428 | ## le nom ne s'affiche pas... |
---|
| 429 | return |
---|
| 430 | |
---|
[233] | 431 | def calculate_bounds(field,vmin=None,vmax=None): |
---|
| 432 | import numpy as np |
---|
| 433 | from mymath import max,min,mean |
---|
| 434 | ind = np.where(field < 9e+35) |
---|
| 435 | fieldcalc = field[ ind ] # la syntaxe compacte ne marche si field est un tuple |
---|
| 436 | ### |
---|
| 437 | dev = np.std(fieldcalc)*3.0 |
---|
| 438 | ### |
---|
| 439 | if vmin is None: |
---|
| 440 | zevmin = mean(fieldcalc) - dev |
---|
| 441 | else: zevmin = vmin |
---|
| 442 | ### |
---|
| 443 | if vmax is None: zevmax = mean(fieldcalc) + dev |
---|
| 444 | else: zevmax = vmax |
---|
| 445 | if vmin == vmax: |
---|
| 446 | zevmin = mean(fieldcalc) - dev ### for continuity |
---|
| 447 | zevmax = mean(fieldcalc) + dev ### for continuity |
---|
| 448 | ### |
---|
| 449 | if zevmin < 0. and min(fieldcalc) > 0.: zevmin = 0. |
---|
| 450 | print "field ", min(fieldcalc), max(fieldcalc) |
---|
| 451 | print "bounds ", zevmin, zevmax |
---|
| 452 | return zevmin, zevmax |
---|
[232] | 453 | |
---|
[233] | 454 | def bounds(what_I_plot,zevmin,zevmax): |
---|
| 455 | ### might be convenient to add the missing value in arguments |
---|
| 456 | what_I_plot[ what_I_plot < zevmin ] = zevmin*(1. + 1.e-7) |
---|
| 457 | what_I_plot[ what_I_plot > 9e+35 ] = -9e+35 |
---|
| 458 | what_I_plot[ what_I_plot > zevmax ] = zevmax*(1. - 1.e-7) |
---|
| 459 | return what_I_plot |
---|
| 460 | |
---|
| 461 | def zoomset (wlon,wlat,zoom): |
---|
| 462 | dlon = abs(wlon[1]-wlon[0])/2. |
---|
| 463 | dlat = abs(wlat[1]-wlat[0])/2. |
---|
| 464 | [wlon,wlat] = [ [wlon[0]+zoom*dlon/100.,wlon[1]-zoom*dlon/100.],\ |
---|
| 465 | [wlat[0]+zoom*dlat/100.,wlat[1]-zoom*dlat/100.] ] |
---|
| 466 | print "zoom %",zoom,wlon,wlat |
---|
| 467 | return wlon,wlat |
---|
| 468 | |
---|
[201] | 469 | def fmtvar (whichvar="def"): |
---|
[204] | 470 | fmtvar = { \ |
---|
| 471 | "tk": "%.0f",\ |
---|
| 472 | "tpot": "%.0f",\ |
---|
| 473 | "def": "%.1e",\ |
---|
| 474 | "PTOT": "%.0f",\ |
---|
| 475 | "HGT": "%.1e",\ |
---|
| 476 | "USTM": "%.2f",\ |
---|
[225] | 477 | "HFX": "%.0f",\ |
---|
[232] | 478 | "ICETOT": "%.1e",\ |
---|
[237] | 479 | "TAU_ICE": "%.2f",\ |
---|
[204] | 480 | } |
---|
| 481 | if whichvar not in fmtvar: |
---|
| 482 | whichvar = "def" |
---|
| 483 | return fmtvar[whichvar] |
---|
[201] | 484 | |
---|
[233] | 485 | #################################################################################################################### |
---|
| 486 | ### Colorbars http://www.scipy.org/Cookbook/Matplotlib/Show_colormaps?action=AttachFile&do=get&target=colormaps3.png |
---|
[202] | 487 | def defcolorb (whichone="def"): |
---|
[204] | 488 | whichcolorb = { \ |
---|
| 489 | "def": "spectral",\ |
---|
| 490 | "HGT": "spectral",\ |
---|
| 491 | "tk": "gist_heat",\ |
---|
| 492 | "QH2O": "PuBu",\ |
---|
| 493 | "USTM": "YlOrRd",\ |
---|
[225] | 494 | "HFX": "RdYlBu",\ |
---|
[232] | 495 | "ICETOT": "YlGnBu",\ |
---|
[237] | 496 | "TAU_ICE": "YlGnBu",\ |
---|
[204] | 497 | } |
---|
| 498 | if whichone not in whichcolorb: |
---|
| 499 | whichone = "def" |
---|
| 500 | return whichcolorb[whichone] |
---|
[202] | 501 | |
---|
| 502 | def definecolorvec (whichone="def"): |
---|
| 503 | whichcolor = { \ |
---|
| 504 | "def": "black",\ |
---|
| 505 | "vis": "yellow",\ |
---|
| 506 | "vishires": "yellow",\ |
---|
| 507 | "molabw": "yellow",\ |
---|
| 508 | "mola": "black",\ |
---|
| 509 | "gist_heat": "white",\ |
---|
| 510 | "hot": "tk",\ |
---|
| 511 | "gist_rainbow": "black",\ |
---|
| 512 | "spectral": "black",\ |
---|
| 513 | "gray": "red",\ |
---|
| 514 | "PuBu": "black",\ |
---|
| 515 | } |
---|
| 516 | if whichone not in whichcolor: |
---|
| 517 | whichone = "def" |
---|
| 518 | return whichcolor[whichone] |
---|
| 519 | |
---|
[180] | 520 | def marsmap (whichone="vishires"): |
---|
[233] | 521 | from os import uname |
---|
| 522 | mymachine = uname()[1] |
---|
| 523 | ### not sure about speed-up with this method... looks the same |
---|
| 524 | if "lmd.jussieu.fr" in mymachine: domain = "/u/aslmd/WWW/maps/" |
---|
| 525 | else: domain = "http://www.lmd.jussieu.fr/~aslmd/maps/" |
---|
[180] | 526 | whichlink = { \ |
---|
[233] | 527 | #"vis": "http://maps.jpl.nasa.gov/pix/mar0kuu2.jpg",\ |
---|
| 528 | #"vishires": "http://www.lmd.jussieu.fr/~aslmd/maps/MarsMap_2500x1250.jpg",\ |
---|
| 529 | #"geolocal": "http://dl.dropbox.com/u/11078310/geolocal.jpg",\ |
---|
| 530 | #"mola": "http://www.lns.cornell.edu/~seb/celestia/mars-mola-2k.jpg",\ |
---|
| 531 | #"molabw": "http://dl.dropbox.com/u/11078310/MarsElevation_2500x1250.jpg",\ |
---|
| 532 | "vis": domain+"mar0kuu2.jpg",\ |
---|
| 533 | "vishires": domain+"MarsMap_2500x1250.jpg",\ |
---|
| 534 | "geolocal": domain+"geolocal.jpg",\ |
---|
| 535 | "mola": domain+"mars-mola-2k.jpg",\ |
---|
| 536 | "molabw": domain+"MarsElevation_2500x1250.jpg",\ |
---|
[180] | 537 | } |
---|
| 538 | if whichone not in whichlink: |
---|
| 539 | print "marsmap: choice not defined... you'll get the default one... " |
---|
| 540 | whichone = "vishires" |
---|
| 541 | return whichlink[whichone] |
---|
| 542 | |
---|
| 543 | def earthmap (whichone): |
---|
| 544 | if whichone == "contrast": whichlink="http://users.info.unicaen.fr/~karczma/TEACH/InfoGeo/Images/Planets/EarthMapAtmos_2500x1250.jpg" |
---|
| 545 | elif whichone == "bw": whichlink="http://users.info.unicaen.fr/~karczma/TEACH/InfoGeo/Images/Planets/EarthElevation_2500x1250.jpg" |
---|
| 546 | elif whichone == "nice": whichlink="http://users.info.unicaen.fr/~karczma/TEACH/InfoGeo/Images/Planets/earthmap1k.jpg" |
---|
| 547 | return whichlink |
---|
| 548 | |
---|