Ignore:
Timestamp:
Jun 30, 2011, 12:47:42 PM (14 years ago)
Author:
aslmd
Message:

MESOSCALE: PYTHON: a much improved winds.py script. try it out with full options: winds.py -f /home/aslmd/MESOWATERCYCLE/THARSIS_noBUG.171021/wrfout_d01_2024-05-02_01\:00\:00 -v QH2O -l 10 -t ~ -p ortho -s 7 -b vishires

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/MESOSCALE/PLOT/PYTHON/scripts/winds.py

    r181 r182  
    11#!/usr/bin/env python
    22
    3 ### A. Spiga LMD 29/05/2011
     3### A. Spiga and A. Colaitis -- LMD -- 30/05/2011
    44
    5 def usage():
    6     print 'USAGE:  winds.py nvert file (target)'
    7     print 'file  : name of input netcdf file.'
    8     print 'nvert : vertical level.'
    9     print '(target) : a directory with write rights.'
    10     print 'Example: winds.py 0 /d5/aslmd/LMD_MM_MARS_SIMUS/OM/OM6_TI85/wrfout_d01_2024-06-43_06:00:00_zabg ~/'
    11 
    12 def winds (namefile,nvert,proj="cyl",back=None,target=None):
     5def winds (namefile,\
     6           nvert,\
     7           proj="cyl",\
     8           back=None,\
     9           target=None,
     10           stride=5,\
     11           var='HGT'):
    1312    from netCDF4 import Dataset
    1413    from myplot import getcoord2d,define_proj,makeplotpng,simplinterv,vectorfield,ptitle
    1514    from matplotlib.pyplot import contourf, subplot, figure
     15    import numpy as np
    1616    nc  = Dataset(namefile)
    1717    [lon2d,lat2d] = getcoord2d(nc)
     
    1919    [u,v] = getwinds(nc)
    2020    nt = len(u[:,0,0,0])
     21    if var not in nc.variables:
     22        print "not found in file:",var
     23        exit()
     24    else:   
     25        dimension = np.array(nc.variables[var]).ndim
     26        if dimension == 3:     what_I_plot = nc.variables[var][:,:,:]
     27        elif dimension == 4:   what_I_plot = nc.variables[var][:,nvert,:,:] 
    2128    fig = figure()
    2229    fig.subplots_adjust(wspace = 0.0, hspace = 0.3)
     
    2734       m = define_proj(proj,wlon,wlat,back=back)
    2835       x, y = m(lon2d, lat2d)
    29        contourf(x, y, nc.variables['HGT'][0,:,:] / 1000., 30)
     36       contourf(x, y, what_I_plot[i,:,:], 30)
    3037       vectorfield(u[i,nvert,:,:], v[i,nvert,:,:],\
    31                       x, y, stride=5, csmooth=3,\
     38                      x, y, stride=stride, csmooth=2,\
    3239                      scale=20., factor=300., color='k')
    3340       sub += 1
    34     if not target:   zeplot = namefile+".winds"+str(nvert)
    35     else:            zeplot = target+"winds"+str(nvert)
     41    if not target:   zeplot = namefile+".winds"+var+str(nvert)
     42    else:            zeplot = target+"/winds"+var+str(nvert)
    3643    makeplotpng(zeplot,pad_inches_value=0.35)   
    3744
    38 def getwinds (nc,charu='Um',charv='Vm'):
     45def getwinds (nc,charu='U',charv='V'):
     46    import numpy as np
    3947    u = nc.variables[charu]
    4048    v = nc.variables[charv]
     49    if charu == 'U': u = u[:, :, :, 0:len(u[0,0,0,:])-1]
     50    if charv == 'V': v = v[:, :, 0:len(v[0,0,:,0])-1, :]
     51    #print np.array(u).shape, np.array(v).shape
    4152    return u,v
    4253
    4354if __name__ == "__main__":
    4455    import sys
    45     if (len(sys.argv)) == 3:     winds( str(sys.argv[2]) , int(sys.argv[1]) )
    46     elif (len(sys.argv)) == 4:   winds( str(sys.argv[2]) , int(sys.argv[1]) , target=str(sys.argv[3]))
     56    ### to be replaced by argparse
     57    from optparse import OptionParser
     58    parser = OptionParser()
     59    parser.add_option('-f', action='store', dest='namefile',    type="string",  default=None,  help='name of WRF file [NEEDED]')
     60    parser.add_option('-l', action='store', dest='nvert',       type="int",     default=0,     help='subscript for vertical level')
     61    parser.add_option('-p', action='store', dest='proj',        type="string",  default='cyl', help='projection')
     62    parser.add_option('-b', action='store', dest='back',        type="string",  default=None,  help='background')
     63    parser.add_option('-t', action='store', dest='target',      type="string",  default=None,  help='destination folder')
     64    parser.add_option('-s', action='store', dest='stride',      type="int",     default=5,     help='stride vectors')
     65    parser.add_option('-v', action='store', dest='var',         type="string",  default='HGT', help='variable contoured')
     66    (opt,args) = parser.parse_args()
     67    if opt.namefile is None:
     68        print "I want to eat one file at least ! Use winds.py -f name_of_my_file. Or type winds.py -h"
     69        exit()
     70    print "Options:", opt
     71    winds (opt.namefile,opt.nvert,proj=opt.proj,back=opt.back,target=opt.target,stride=opt.stride,var=opt.var)
Note: See TracChangeset for help on using the changeset viewer.