Changeset 345 for trunk/MESOSCALE/LMD_MM_MARS/SRC
- Timestamp:
- Nov 4, 2011, 2:45:06 PM (13 years ago)
- Location:
- trunk/MESOSCALE/LMD_MM_MARS/SRC/PYTHON
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/MESOSCALE/LMD_MM_MARS/SRC/PYTHON/mymath.py
r310 r345 5 5 def max (field,axis=None): 6 6 import numpy as np 7 return np.array(field).max(axis=axis) 7 if field is None: return None 8 else: return np.array(field).max(axis=axis) 8 9 9 10 def mean (field,axis=None): -
trunk/MESOSCALE/LMD_MM_MARS/SRC/PYTHON/myplot.py
r317 r345 1 ## Author: AS 1 2 def errormess(text,printvar=None): 2 3 print text … … 5 6 return 6 7 8 ## Author: AS 7 9 def getname(var=False,winds=False,anomaly=False): 8 10 if var and winds: basename = var + '_UV' … … 13 15 return basename 14 16 17 ## Author: AS 15 18 def localtime(utc,lon): 16 19 ltst = utc + lon / 15. … … 19 22 return ltst 20 23 24 ## Author: AS 21 25 def whatkindfile (nc): 22 26 if 'controle' in nc.variables: typefile = 'gcm' … … 25 29 elif 'U' in nc.variables: typefile = 'meso' 26 30 elif 'HGT_M' in nc.variables: typefile = 'geo' 27 else: errormess("whatkindfile: typefile not supported.") 31 #else: errormess("whatkindfile: typefile not supported.") 32 else: typefile = 'gcm' # for lslin-ed files from gcm 28 33 return typefile 29 34 35 ## Author: AS 30 36 def getfield (nc,var): 31 37 ## this allows to get much faster (than simply referring to nc.variables[var]) 32 38 dimension = len(nc.variables[var].dimensions) 39 print " Opening variable with", dimension, "dimensions ..." 33 40 if dimension == 2: field = nc.variables[var][:,:] 34 41 elif dimension == 3: field = nc.variables[var][:,:,:] … … 36 43 return field 37 44 45 ## Author: AS + TN 38 46 def reducefield (input,d4=None,d3=None,d2=None,d1=None): 39 47 ### we do it the reverse way to be compliant with netcdf "t z y x" or "t y x" or "y x" 40 48 ### it would be actually better to name d4 d3 d2 d1 as t z y x 41 49 import numpy as np 50 from mymath import max 42 51 dimension = np.array(input).ndim 43 52 shape = np.array(input).shape … … 45 54 output = input 46 55 error = False 56 ### this is needed to cope the case where d4,d3,d2,d1 are single integers and not arrays 57 if d4 is not None and not isinstance(d4, np.ndarray): d4=[d4] 58 if d3 is not None and not isinstance(d3, np.ndarray): d3=[d3] 59 if d2 is not None and not isinstance(d2, np.ndarray): d2=[d2] 60 if d1 is not None and not isinstance(d1, np.ndarray): d1=[d1] 61 ### now the main part 47 62 if dimension == 2: 48 63 if d2 >= shape[0]: error = True … … 52 67 elif d2 is not None: output = input[d2,:] 53 68 elif dimension == 3: 54 if d4>= shape[0]: error = True55 elif d2>= shape[1]: error = True56 elif d1>= shape[2]: error = True69 if max(d4) >= shape[0]: error = True 70 elif max(d2) >= shape[1]: error = True 71 elif max(d1) >= shape[2]: error = True 57 72 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,:,:]73 elif d4 is not None and d2 is not None: output=np.mean(input[d4,:,:],axis=0); output=np.mean(output[d2,:],axis=0) 74 elif d4 is not None and d1 is not None: output=np.mean(input[d4,:,:],0); output=np.mean(output[:,d1],1) 75 elif d2 is not None and d1 is not None: output=np.mean(input[:,d2,:],axis=1); output=np.mean(output[:,d1],axis=1) 76 elif d1 is not None: output = np.mean(input[:,:,d1],axis=2) 77 elif d2 is not None: output = np.mean(input[:,d2,:],axis=1) 78 elif d4 is not None: output = np.mean(input[d4,:,:],axis=0) 64 79 elif dimension == 4: 65 if d4>= shape[0]: error = True66 elif d3>= shape[1]: error = True67 elif d2>= shape[2]: error = True68 elif d1>= shape[3]: error = True80 if max(d4) >= shape[0]: error = True 81 elif max(d3) >= shape[1]: error = True 82 elif max(d2) >= shape[2]: error = True 83 elif max(d1) >= shape[3]: error = True 69 84 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 85 elif d4 is not None and d3 is not None and d2 is not None: output = input[d4,d3,d2,:] … … 72 87 elif d4 is not None and d2 is not None and d1 is not None: output = input[d4,:,d2,d1] 73 88 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]89 elif d4 is not None and d3 is not None: output = np.mean(input[d4,:,:,:],0); output = np.mean(output[d3,:,:],0) 90 elif d4 is not None and d2 is not None: output = np.mean(input[d4,:,:,:],0); output = np.mean(output[:,d2,:],1) 91 elif d4 is not None and d1 is not None: output = np.mean(input[d4,:,:,:],0); output = np.mean(output[:,:,d1],2) 92 elif d3 is not None and d2 is not None: output = np.mean(input[:,d3,:,:],1); output = np.mean(output[:,d2,:],1) 93 elif d3 is not None and d1 is not None: output = np.mean(input[:,d3,:,:],1); output = np.mean(output[:,:,d1],0) 94 elif d2 is not None and d1 is not None: output = np.mean(input[:,:,d2,:],2); output = np.mean(output[:,:,d1],2) 80 95 elif d1 is not None: output = input[:,:,:,d1] 81 96 elif d2 is not None: output = input[:,:,d2,:] … … 87 102 return output, error 88 103 104 ## Author: AS + TN 89 105 def definesubplot ( numplot, fig ): 90 106 from matplotlib.pyplot import rcParams 91 107 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. ) 108 if numplot <= 0: 109 subv = 99999 110 subh = 99999 111 elif numplot == 1: 112 subv = 99999 113 subh = 99999 96 114 elif numplot == 2: 97 sub = 121 115 subv = 1 116 subh = 2 98 117 fig.subplots_adjust(wspace = 0.35) 99 118 rcParams['font.size'] = int( rcParams['font.size'] * 3. / 4. ) 100 119 elif numplot == 3: 101 sub = 131 120 subv = 3 121 subh = 1 102 122 fig.subplots_adjust(wspace = 0.5) 103 123 rcParams['font.size'] = int( rcParams['font.size'] * 1. / 2. ) 104 elif numplot == 6: 105 sub = 231 124 elif numplot == 4: 125 subv = 2 126 subh = 2 127 fig.subplots_adjust(wspace = 0.3, hspace = 0.3) 128 rcParams['font.size'] = int( rcParams['font.size'] * 2. / 3. ) 129 elif numplot <= 6: 130 subv = 2 131 subh = 3 106 132 fig.subplots_adjust(wspace = 0.4, hspace = 0.0) 107 133 rcParams['font.size'] = int( rcParams['font.size'] * 1. / 2. ) 108 elif numplot == 8: 109 sub = 331 #241 134 elif numplot <= 8: 135 subv = 2 136 subh = 4 110 137 fig.subplots_adjust(wspace = 0.3, hspace = 0.3) 111 138 rcParams['font.size'] = int( rcParams['font.size'] * 1. / 2. ) 112 elif numplot == 9: 113 sub = 331 139 elif numplot <= 9: 140 subv = 3 141 subh = 3 114 142 fig.subplots_adjust(wspace = 0.3, hspace = 0.3) 115 143 rcParams['font.size'] = int( rcParams['font.size'] * 1. / 2. ) 116 elif numplot == 1: 117 sub = 99999 118 elif numplot <= 0: 119 sub = 99999 144 elif numplot <= 12: 145 subv = 3 146 subh = 4 147 fig.subplots_adjust(wspace = 0.1, hspace = 0.1) 148 rcParams['font.size'] = int( rcParams['font.size'] * 1. / 2. ) 149 elif numplot <= 16: 150 subv = 4 151 subh = 4 152 fig.subplots_adjust(wspace = 0.3, hspace = 0.3) 153 rcParams['font.size'] = int( rcParams['font.size'] * 1. / 2. ) 120 154 else: 121 print " supported: 1,2,3,4,6,8,9"155 print "number of plot supported: 1 to 16" 122 156 exit() 123 return sub 124 157 return subv,subh 158 159 ## Author: AS 125 160 def getstralt(nc,nvert): 126 161 typefile = whatkindfile(nc) … … 140 175 return stralt 141 176 177 ## Author: AS 142 178 def getlschar ( namefile ): 143 179 from netCDF4 import Dataset … … 169 205 return lschar, zehour, zehourin 170 206 207 ## Author: AS 171 208 def getprefix (nc): 172 209 prefix = 'LMD_MMM_' … … 175 212 return prefix 176 213 214 ## Author: AS 177 215 def getproj (nc): 178 216 typefile = whatkindfile(nc) … … 200 238 return proj 201 239 240 ## Author: AS 202 241 def ptitle (name): 203 242 from matplotlib.pyplot import title … … 205 244 print name 206 245 246 ## Author: AS 207 247 def polarinterv (lon2d,lat2d): 208 248 import numpy as np … … 212 252 return [wlon,wlat] 213 253 254 ## Author: AS 214 255 def simplinterv (lon2d,lat2d): 215 256 import numpy as np 216 257 return [[np.min(lon2d),np.max(lon2d)],[np.min(lat2d),np.max(lat2d)]] 217 258 259 ## Author: AS 218 260 def wrfinterv (lon2d,lat2d): 219 261 nx = len(lon2d[0,:])-1 … … 231 273 return [wlon,wlat] 232 274 275 ## Author: AS 233 276 def makeplotres (filename,res=None,pad_inches_value=0.25,folder='',disp=True,ext='png',erase=False): 234 277 import matplotlib.pyplot as plt … … 246 289 return 247 290 291 ## Author: AS 248 292 def dumpbdy (field,n,stag=None): 249 293 nx = len(field[0,:])-1 … … 253 297 return field[n:ny-n,n:nx-n] 254 298 299 ## Author: AS 255 300 def getcoorddef ( nc ): 256 301 import numpy as np … … 267 312 return lon2d,lat2d 268 313 314 ## Author: AS 269 315 def getcoord2d (nc,nlat='XLAT',nlon='XLONG',is1d=False): 270 316 import numpy as np … … 279 325 return lon2d,lat2d 280 326 327 ## Author: AS 281 328 def smooth (field, coeff): 282 329 ## actually blur_image could work with different coeff on x and y … … 285 332 return result 286 333 334 ## FROM COOKBOOK http://www.scipy.org/Cookbook/SignalSmooth 287 335 def gauss_kern(size, sizey=None): 288 336 import numpy as np 289 ## FROM COOKBOOK http://www.scipy.org/Cookbook/SignalSmooth290 337 # Returns a normalized 2D gauss kernel array for convolutions 291 338 size = int(size) … … 298 345 return g / g.sum() 299 346 347 ## FROM COOKBOOK http://www.scipy.org/Cookbook/SignalSmooth 300 348 def blur_image(im, n, ny=None) : 301 349 from scipy.signal import convolve 302 ## FROM COOKBOOK http://www.scipy.org/Cookbook/SignalSmooth303 350 # blurs the image by convolving with a gaussian kernel of typical size n. 304 351 # The optional keyword argument ny allows for a different size in the y direction. … … 307 354 return improc 308 355 356 ## Author: AS 309 357 def getwinddef (nc): 310 358 ## getwinds for predefined types … … 322 370 return uchar,vchar,metwind 323 371 372 ## Author: AS 324 373 def vectorfield (u, v, x, y, stride=3, scale=15., factor=250., color='black', csmooth=1, key=True): 325 374 ## scale regle la reference du vecteur … … 344 393 return 345 394 395 ## Author: AS 346 396 def display (name): 347 397 from os import system … … 349 399 return name 350 400 401 ## Author: AS 351 402 def findstep (wlon): 352 403 steplon = int((wlon[1]-wlon[0])/4.) #3 … … 361 412 return step 362 413 363 def define_proj (char,wlon,wlat,back=None): 414 ## Author: AS 415 def define_proj (char,wlon,wlat,back=None,blat=False): 364 416 from mpl_toolkits.basemap import Basemap 365 417 import numpy as np … … 368 420 meanlon = 0.5*(wlon[0]+wlon[1]) 369 421 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] 422 if not blat: 423 if wlat[0] >= 80.: blat = 40. 424 elif wlat[1] <= -80.: blat = -40. 425 elif wlat[1] >= 0.: blat = wlat[0] 426 elif wlat[0] <= 0.: blat = wlat[1] 374 427 print "blat ", blat 375 428 h = 50. ## en km … … 407 460 return m 408 461 462 ## Author: AS 409 463 #### test temporaire 410 464 def putpoints (map,plot): … … 425 479 return 426 480 481 ## Author: AS 427 482 def calculate_bounds(field,vmin=None,vmax=None): 428 483 import numpy as np … … 448 503 return zevmin, zevmax 449 504 505 ## Author: AS 450 506 def bounds(what_I_plot,zevmin,zevmax): 451 507 from mymath import max,min,mean … … 461 517 return what_I_plot 462 518 519 ## Author: AS 463 520 def nolow(what_I_plot): 464 521 from mymath import max,min … … 468 525 return what_I_plot 469 526 527 ## Author: AS 470 528 def zoomset (wlon,wlat,zoom): 471 529 dlon = abs(wlon[1]-wlon[0])/2. … … 476 534 return wlon,wlat 477 535 536 ## Author: AS 478 537 def fmtvar (whichvar="def"): 479 538 fmtvar = { \ … … 489 548 "TAU_ICE": "%.2f",\ 490 549 "VMR_ICE": "%.1e",\ 491 "MTOT": "%. 0f",\550 "MTOT": "%.1f",\ 492 551 "anomaly": "%.1f",\ 493 552 "W": "%.1f",\ … … 497 556 "ALBBARE": "%.2f",\ 498 557 "TAU": "%.1f",\ 558 #### T.N. 559 "TEMP": "%.0f",\ 560 "VMR_H2OICE": "%.0f",\ 561 "VMR_H2OVAP": "%.0f",\ 562 "TAUTES": "%.2f",\ 563 "TAUTESAP": "%.2f",\ 564 499 565 } 500 566 if whichvar not in fmtvar: … … 502 568 return fmtvar[whichvar] 503 569 570 ## Author: AS 504 571 #################################################################################################################### 505 572 ### Colorbars http://www.scipy.org/Cookbook/Matplotlib/Show_colormaps?action=AttachFile&do=get&target=colormaps3.png … … 514 581 "HFX": "RdYlBu",\ 515 582 "ICETOT": "YlGnBu_r",\ 516 "MTOT": "PuBu",\ 583 #"MTOT": "PuBu",\ 584 "CCNQ": "YlOrBr",\ 585 "CCNN": "YlOrBr",\ 586 "TEMP": "Jet",\ 517 587 "TAU_ICE": "Blues",\ 518 588 "VMR_ICE": "Blues",\ … … 523 593 "ALBBARE": "spectral",\ 524 594 "TAU": "YlOrBr_r",\ 595 #### T.N. 596 "MTOT": "Jet",\ 597 "H2O_ICE_S": "RdBu",\ 598 "VMR_H2OICE": "PuBu",\ 599 "VMR_H2OVAP": "PuBu",\ 525 600 } 526 601 #W --> spectral ou jet … … 531 606 return whichcolorb[whichone] 532 607 608 ## Author: AS 533 609 def definecolorvec (whichone="def"): 534 610 whichcolor = { \ … … 549 625 return whichcolor[whichone] 550 626 627 ## Author: AS 551 628 def marsmap (whichone="vishires"): 552 629 from os import uname … … 588 665 # return whichlink 589 666 667 ## Author: AS 590 668 def latinterv (area="Whole"): 591 669 list = { \ … … 609 687 return olon,olat 610 688 689 ## Author: TN 690 def separatenames (name): 691 from numpy import concatenate 692 # look for comas in the input name to separate different names (files, variables,etc ..) 693 if name is None: 694 names = None 695 else: 696 names = [] 697 stop = 0 698 currentname = name 699 while stop == 0: 700 indexvir = currentname.find(',') 701 if indexvir == -1: 702 stop = 1 703 name1 = currentname 704 else: 705 name1 = currentname[0:indexvir] 706 names = concatenate((names,[name1])) 707 currentname = currentname[indexvir+1:len(currentname)] 708 return names 709 710 ## Author: TN [old] 711 def getopmatrix (kind,n): 712 import numpy as np 713 # compute matrix of operations between files 714 # the matrix is 'number of files'-square 715 # 1: difference (row minus column), 2: add 716 # not 0 in diag : just plot 717 if n == 1: 718 opm = np.eye(1) 719 elif kind == 'basic': 720 opm = np.eye(n) 721 elif kind == 'difference1': # show differences with 1st file 722 opm = np.zeros((n,n)) 723 opm[0,:] = 1 724 opm[0,0] = 0 725 elif kind == 'difference2': # show differences with 1st file AND show 1st file 726 opm = np.zeros((n,n)) 727 opm[0,:] = 1 728 else: 729 opm = np.eye(n) 730 return opm 731 732 ## Author: TN [old] 733 def checkcoherence (nfiles,nlat,nlon,ntime): 734 if (nfiles > 1): 735 if (nlat > 1): 736 errormess("what you asked is not possible !") 737 return 1 738 739 ## Author: TN 740 def readslices(saxis): 741 from numpy import empty 742 if saxis == None: 743 zesaxis = None 744 else: 745 zesaxis = empty((len(saxis),2)) 746 for i in range(len(saxis)): 747 a = separatenames(saxis[i]) 748 if len(a) == 1: 749 zesaxis[i,:] = float(a[0]) 750 else: 751 zesaxis[i,0] = float(a[0]) 752 zesaxis[i,1] = float(a[1]) 753 754 return zesaxis 755 756 ## Author: TN 757 def getsindex(saxis,index,axis): 758 # input : all the desired slices and the good index 759 # output : all indexes to be taken into account for reducing field 760 import numpy as np 761 if saxis is None: 762 zeindex = None 763 else: 764 aaa = int(np.argmin(abs(saxis[index,0] - axis))) 765 bbb = int(np.argmin(abs(saxis[index,1] - axis))) 766 [imin,imax] = np.sort(np.array([aaa,bbb])) 767 zeindex = np.array(range(imax-imin+1))+imin 768 # because -180 and 180 are the same point in longitude, 769 # we get rid of one for averaging purposes. 770 if axis[imin] == -180 and axis[imax] == 180: 771 zeindex = zeindex[0:len(zeindex)-1] 772 print "whole longitude averaging asked, so last point is not taken into account." 773 return zeindex 774 775 ## Author: TN 776 def define_axis(lon,lat,vert,time,indexlon,indexlat,indexvert,indextime,what_I_plot,dim0,vertmode): 777 # Purpose of define_axis is to find x and y axis scales in a smart way 778 # x axis priority: 1/time 2/lon 3/lat 4/vertical 779 # To be improved !!!... 780 from numpy import array,swapaxes 781 x = None 782 y = None 783 count = 0 784 what_I_plot = array(what_I_plot) 785 shape = what_I_plot.shape 786 if indextime is None: 787 x = time 788 count = count+1 789 if indexlon is None: 790 if count == 0: x = lon 791 else: y = lon 792 count = count+1 793 if indexlat is None: 794 if count == 0: x = lat 795 else: y = lat 796 count = count+1 797 if indexvert is None and dim0 is 4: 798 if vertmode == 0: # vertical axis is as is (GCM grid) 799 if count == 0: x=range(len(vert)) 800 else: y=range(len(vert)) 801 count = count+1 802 else: # vertical axis is in kms 803 if count == 0: x = vert 804 else: y = vert 805 count = count+1 806 x = array(x) 807 y = array(y) 808 if len(shape) == 1: 809 if shape != len(x): 810 print "WARNING HERE !!!" 811 x = y 812 elif len(shape) == 2: 813 print shape[1], len(y), shape[0], len(x) 814 if shape[1] == len(y) and shape[0] == len(x) and shape[0] != shape[1]: 815 what_I_plot = swapaxes(what_I_plot,0,1) 816 print "swapaxes", what_I_plot.shape, shape 817 #x0 = x 818 #x = y 819 #y = x0 820 #print "define_axis", x, y 821 return what_I_plot,x,y -
trunk/MESOSCALE/LMD_MM_MARS/SRC/PYTHON/winds.py
r317 r345 82 82 ### Open a figure and set subplots 83 83 fig = figure() 84 sub = definesubplot( numplot, fig )84 subv,subh = definesubplot( numplot, fig ) 85 85 86 86 ################################# … … 102 102 if nplot > numplot: break 103 103 if numplot > 1: 104 if typefile not in ['geo']: subplot(sub +nplot-1)104 if typefile not in ['geo']: subplot(subv,subh,nplot) 105 105 found_lct = True 106 106 ### If only one local time is requested (numplot < 0)
Note: See TracChangeset
for help on using the changeset viewer.