Changeset 763 for trunk/UTIL/PYTHON/myplot.py
- Timestamp:
- Aug 28, 2012, 3:08:35 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UTIL/PYTHON/myplot.py
r760 r763 29 29 return basename 30 30 31 ## Author: AS 32 def localtime(utc,lon): 33 ltst = utc + lon / 15. 31 ## Author: AS + AC 32 def localtime(time,lon,nc): # lon is the mean longitude of the plot, not of the domain. central lon of domain is taken from cen_lon 33 import numpy as np 34 dt_hour=1. 35 start=0. 36 if lon is not None: 37 if lon[0,1]!=lon[0,0]: mean_lon_plot = 0.5*(lon[0,1]-lon[0,0]) 38 else: mean_lon_plot=lon[0,0] 39 elif hasattr(nc, 'CEN_LON'): mean_lon_plot=getattr(nc, 'CEN_LON') 40 else: mean_lon_plot=0. 41 if hasattr(nc,'TITLE'): 42 title=getattr(nc, 'TITLE') 43 if hasattr(nc,'DT') and 'MRAMS' in title: dt_hour=getattr(nc, 'DT')/60. #LMD MMM is 1 output/hour (and not 1 output/timestep) 44 # MRAMS is 1 output/timestep, unless stride is added in ic.py 45 if hasattr(nc,'START_DATE'): 46 start_date=getattr(nc, 'START_DATE') 47 start_hour=np.float(start_date[11:13]) 48 start_minute=np.float(start_date[14:16])/60. 49 start=start_hour+start_minute # start is the local time of simu at longitude 0 50 ltst = start + time*dt_hour + mean_lon_plot / 15. 51 else: ltst = time*dt_hour + mean_lon_plot / 15. 52 else: ltst = time*dt_hour + mean_lon_plot / 15. 34 53 ltst = int (ltst * 10) / 10. 35 54 ltst = ltst % 24 … … 39 58 def check_localtime(time): 40 59 a=-1 60 print time 41 61 for i in range(len(time)-1): 42 62 if (time[i] > time[i+1]): a=i … … 102 122 # if (True in np.array(mask)):out=masked 103 123 # else:out=field 104 else:out=field 124 else: 125 # # missing values from MRAMS files are 0.100E+32 126 masked=np.ma.masked_where(field > 1e30,field) 127 masked.set_fill_value([np.NaN]) 128 mask = np.ma.getmask(masked) 129 if (True in np.array(mask)):out=masked 130 else:out=field 131 # else:out=field 105 132 return out 106 133 107 134 ## Author: AC 108 # Compute the norm of the winds 135 # Compute the norm of the winds or return an hodograph 109 136 # The corresponding variable to call is UV or uvmet (to use api) 110 def windamplitude (nc ):137 def windamplitude (nc,mode): 111 138 import numpy as np 112 139 varinfile = nc.variables.keys() 113 140 if "U" in varinfile: zu=getfield(nc,'U') 114 141 elif "Um" in varinfile: zu=getfield(nc,'Um') 142 else: errormess("you need slopex or U or Um in your file.") 115 143 if "V" in varinfile: zv=getfield(nc,'V') 116 144 elif "Vm" in varinfile: zv=getfield(nc,'Vm') 145 else: errormess("you need V or Vm in your file.") 117 146 znt,znz,zny,znx = np.array(zu).shape 118 if "U" in varinfile:znx=znx-1147 if hasattr(nc,'WEST-EAST_PATCH_END_UNSTAG'):znx=getattr(nc, 'WEST-EAST_PATCH_END_UNSTAG') 119 148 zuint = np.zeros([znt,znz,zny,znx]) 120 149 zvint = np.zeros([znt,znz,zny,znx]) 121 150 if "U" in varinfile: 122 for xx in np.arange(znx): zuint[:,:,:,xx] = (zu[:,:,:,xx] + zu[:,:,:,xx+1])/2. 123 for yy in np.arange(zny): zvint[:,:,yy,:] = (zv[:,:,yy,:] + zv[:,:,yy+1,:])/2. 151 if hasattr(nc,'SOUTH-NORTH_PATCH_END_STAG'): zny_stag=getattr(nc, 'SOUTH-NORTH_PATCH_END_STAG') 152 if hasattr(nc,'WEST-EAST_PATCH_END_STAG'): znx_stag=getattr(nc, 'WEST-EAST_PATCH_END_STAG') 153 if zny_stag == zny: zvint=zv 154 else: 155 for yy in np.arange(zny): zvint[:,:,yy,:] = (zv[:,:,yy,:] + zv[:,:,yy+1,:])/2. 156 if znx_stag == znx: zuint=zu 157 else: 158 for xx in np.arange(znx): zuint[:,:,:,xx] = (zu[:,:,:,xx] + zu[:,:,:,xx+1])/2. 124 159 else: 125 160 zuint=zu 126 161 zvint=zv 127 return np.sqrt(zuint**2 + zvint**2) 162 if mode=='amplitude': return np.sqrt(zuint**2 + zvint**2) 163 if mode=='hodograph': return zuint,zvint 164 if mode=='hodograph_2': return None, 360.*np.arctan(zvint/zuint)/(2.*np.pi) 128 165 129 166 ## Author: AC … … 214 251 if redope == "mint": input = min(input,axis=0) ; d1 = None 215 252 elif redope == "maxt": input = max(input,axis=0) ; d1 = None 253 elif redope == "edge_y1": input = input[:,:,0,:] ; d2 = None 254 elif redope == "edge_y2": input = input[:,:,-1,:] ; d2 = None 255 elif redope == "edge_x1": input = input[:,:,:,0] ; d1 = None 256 elif redope == "edge_x2": input = input[:,:,:,-1] ; d1 = None 216 257 else: errormess("not supported. but try lines in reducefield beforehand.") 217 258 #elif redope == "minz": input = min(input,axis=1) ; d2 = None … … 372 413 totalarea = mean(totalarea[:,:,d2,:],axis=2); totalarea = mean(totalarea[:,:,d1],axis=1) 373 414 except: print "(problem with areas. I skip this)" ; mesharea = 1. ; totalarea = 1. 374 output = output*mesharea; output = mean(output[:,:,d2,:],axis=2); output = mean(output[:,:,d1],axis= 1)/totalarea415 output = output*mesharea; output = mean(output[:,:,d2,:],axis=2); output = mean(output[:,:,d1],axis=2)/totalarea 375 416 elif d1 is not None: output = mean(input[:,:,:,d1],axis=3) 376 417 elif d2 is not None: … … 1272 1313 1273 1314 ## Author: TN 1274 def define_axis(lon,lat,vert,time,indexlon,indexlat,indexvert,indextime,what_I_plot,dim0,vertmode ):1315 def define_axis(lon,lat,vert,time,indexlon,indexlat,indexvert,indextime,what_I_plot,dim0,vertmode,redope): 1275 1316 # Purpose of define_axis is to find x and y axis scales in a smart way 1276 1317 # x axis priority: 1/time 2/lon 3/lat 4/vertical … … 1286 1327 x = time 1287 1328 count = count+1 1288 if indexlon is None and len(lon) > 1 :1329 if indexlon is None and len(lon) > 1 and redope not in ['edge_x1','edge_x2']: 1289 1330 print "AXIS is lon" 1290 1331 if count == 0: x = lon 1291 1332 else: y = lon 1292 1333 count = count+1 1293 if indexlat is None and len(lat) > 1 :1334 if indexlat is None and len(lat) > 1 and redope not in ['edge_y1','edge_y2']: 1294 1335 print "AXIS is lat" 1295 1336 if count == 0: x = lat … … 1322 1363 return what_I_plot,x,y 1323 1364 1324 # Author: TN + AS 1325 def determineplot(slon, slat, svert, stime ):1365 # Author: TN + AS + AC 1366 def determineplot(slon, slat, svert, stime, redope): 1326 1367 nlon = 1 # number of longitudinal slices -- 1 is None 1327 1368 nlat = 1 … … 1330 1371 nslices = 1 1331 1372 if slon is not None: 1332 nslices = nslices*len(slon) 1373 if slon[0,0]!=slon[0,1]: length=len(slon) 1374 else: length=1 1375 nslices = nslices*length 1333 1376 nlon = len(slon) 1334 1377 if slat is not None: 1335 nslices = nslices*len(slat) 1378 if slat[0,0]!=slat[0,1]: length=len(slat) 1379 else: length=1 1380 nslices = nslices*length 1336 1381 nlat = len(slat) 1337 1382 if svert is not None: 1338 nslices = nslices*len(svert) 1383 if svert[0,0]!=svert[0,1]: lenth=len(svert) 1384 else: length=1 1385 nslices = nslices*length 1339 1386 nvert = len(svert) 1340 1387 if stime is not None: 1341 nslices = nslices*len(stime) 1388 if stime[0,0]!=stime[0,1]: length=len(stime) 1389 else: length=1 1390 nslices = nslices*length 1342 1391 ntime = len(stime) 1343 1392 #else: 1344 1393 # nslices = 2 1345 1394 mapmode = 0 1346 if slon is None and slat is None :1395 if slon is None and slat is None and redope not in ['edge_x1','edge_x2','edge_y1','edge_y2']: 1347 1396 mapmode = 1 # in this case we plot a map, with the given projection 1348 1349 1397 return nlon, nlat, nvert, ntime, mapmode, nslices 1350 1351 ## Author: AC1352 ## Reduce complexity of main script by moving the contour part here. Also allow to call it from elsewhere1353 ## which can be usefull1354 #1355 #def call_contour(what_I_plot,error,x,y,m,lon,lat,vert,time,vertmode,ze_var2,indextime,indexlon,indexlat,indexvert,yintegral,mapmode,typefile,var2,ticks):1356 # from matplotlib.pyplot import contour, plot, clabel1357 # import numpy as np1358 # #what_I_plot = what_I_plot*mult1359 # if not error:1360 # if mapmode == 1:1361 # if typefile in ['mesoapi','meso']: what_I_plot = dumpbdy(what_I_plot,6)1362 # zevmin, zevmax = calculate_bounds(what_I_plot)1363 # zelevels = np.linspace(zevmin,zevmax,ticks) #20)1364 # if var2 == 'HGT': zelevels = np.arange(-10000.,30000.,2000.)1365 # if mapmode == 0:1366 # #if typefile in ['mesoideal']: what_I_plot = dumpbdy(what_I_plot,0,stag='W')1367 # itime=indextime1368 # if len(what_I_plot.shape) is 3: itime=[0]1369 # what_I_plot, x, y = define_axis(lon,lat,vert,time,indexlon,indexlat,indexvert,\1370 # itime,what_I_plot, len(ze_var2.shape),vertmode)1371 # ### If we plot a 2-D field1372 # if len(what_I_plot.shape) is 2:1373 # #zelevels=[1.]1374 # if mapmode == 0:cs = contour(x,y,what_I_plot, zelevels, colors='k', linewidths = 1 ) #0.33 colors='w' )# , alpha=0.5)1375 # elif mapmode == 1:cs = m.contour(x,y,what_I_plot, zelevels, colors='k', linewidths = 1 ) #0.33 colors='w' )# , alpha=0.5)1376 # #clabel(cs,zelevels,inline=3,fmt='%1.1f',fontsize=7)1377 # ### If we plot a 1-D field1378 # elif len(what_I_plot.shape) is 1:1379 # plot(what_I_plot,x)1380 # else:1381 # errormess("There is an error in reducing field !")1382 # return error1383 1398 1384 1399 ## Author : AS … … 1428 1443 ## Author : AC 1429 1444 ## Handles calls to specific computations (e.g. wind norm, enrichment factor...) 1430 def select_getfield(zvarname=None,znc=None,ztypefile=None,mode=None,ztsat=None,ylon=None,ylat=None,yalt=None,ytime=None ):1445 def select_getfield(zvarname=None,znc=None,ztypefile=None,mode=None,ztsat=None,ylon=None,ylat=None,yalt=None,ytime=None,analysis=None): 1431 1446 from mymath import get_tsat 1432 1447 1433 1448 ## Specific variables are described here: 1434 1449 # for the mesoscale: 1435 specificname_meso = ['UV','uv','uvmet','slopexy','SLOPEXY','deltat','DELTAT' ]1450 specificname_meso = ['UV','uv','uvmet','slopexy','SLOPEXY','deltat','DELTAT','hodograph','tk','hodograph_2'] 1436 1451 # for the gcm: 1437 1452 specificname_gcm = ['enfact'] … … 1452 1467 ## Get the corresponding variable: 1453 1468 if mode == 'getvar': 1469 plot_x = None ; plot_y = None ; 1454 1470 ### ----------- 1. saturation temperature 1455 1471 if zvarname in ["temp","t","T_nadir_nit","T_nadir_day","temp_day","temp_night"] and ztsat: … … 1460 1476 ### ----------- 2. wind amplitude 1461 1477 elif ((zvarname in ['UV','uv','uvmet']) and (ztypefile in ['meso']) and (zvarname not in znc.variables)): 1462 all_var=windamplitude(znc) 1478 all_var=windamplitude(znc,'amplitude') 1479 elif ((zvarname in ['hodograph','hodograph_2']) and (ztypefile in ['meso']) and (zvarname not in znc.variables)): 1480 plot_x, plot_y = windamplitude(znc,zvarname) 1481 if plot_x is not None: all_var=plot_x # dummy 1482 else: all_var=plot_y ; plot_x = None ; plot_y = None # Hodograph type 2 is not 'xy' mode 1463 1483 elif ((zvarname in ['slopexy','SLOPEXY']) and (ztypefile in ['meso']) and (zvarname not in znc.variables)): 1464 1484 all_var=slopeamplitude(znc) … … 1469 1489 elif ((ztypefile in ['gcm']) and (zvarname in ['enfact'])): 1470 1490 all_var=enrichment_factor(znc,ylon,ylat,ytime) 1491 ### ------------ 5. teta -> temp 1492 elif ((ztypefile in ['meso']) and (zvarname in ['tk']) and ('tk' not in znc.variables.keys())): 1493 all_var=teta_to_tk(znc) 1471 1494 else: 1472 1495 ### ----------- 999. Normal case 1473 1496 all_var = getfield(znc,zvarname) 1474 return all_var 1475 1497 if analysis is not None: 1498 if analysis in ['histo','density','histodensity']: plot_y=all_var ; plot_x = plot_y 1499 elif analysis == 'fft': plot_y, plot_x = spectrum(all_var,ytime,yalt,ylat,ylon) ; all_var = plot_y 1500 return all_var, plot_x, plot_y 1501 1502 # Author : A.C 1503 # FFT is computed before reducefield voluntarily, because we dont want to compute 1504 # ffts on averaged fields (which would kill all waves). Instead, we take the fft everywhere 1505 # (which is not efficient but it is still ok) and then, make the average (if the user wants to) 1506 def spectrum(var,time,vert,lat,lon): 1507 import numpy as np 1508 fft=np.fft.fft(var,axis=1) 1509 N=len(vert) 1510 step=(vert[1]-vert[0])*1000. 1511 print "step is: ",step 1512 fftfreq=np.fft.fftfreq(N,d=step) 1513 fftfreq=np.fft.fftshift(fftfreq) # spatial FFT => this is the wavenumber 1514 fft=np.fft.fftshift(fft) 1515 fftfreq = 1./fftfreq # => wavelength (div by 0 expected, don't panic) 1516 fft=np.abs(fft) # => amplitude spectrum 1517 # fft=np.abs(fft)**2 # => power spectrum 1518 return fft,fftfreq 1519 1520 # Author : A.C. 1521 # Computes temperature from potential temperature for mesoscale files, without the need to use API, i.e. using natural vertical grid 1522 def teta_to_tk(nc): 1523 import numpy as np 1524 varinfile = nc.variables.keys() 1525 p0=610. 1526 t0=220. 1527 r_cp=1./3.89419 1528 if "T" in varinfile: zteta=getfield(nc,'T') 1529 else: errormess("you need T in your file.") 1530 if "PTOT" in varinfile: zptot=getfield(nc,'PTOT') 1531 else: errormess("you need PTOT in your file.") 1532 zt=(zteta+220.)*(zptot/p0)**(r_cp) 1533 return zt
Note: See TracChangeset
for help on using the changeset viewer.