[180] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
[184] | 3 | ### A. Spiga -- LMD -- 30/05/2011 |
---|
| 4 | ### Thanks to A. Colaitis for the parser trick |
---|
[180] | 5 | |
---|
[184] | 6 | |
---|
| 7 | #################################### |
---|
| 8 | #################################### |
---|
| 9 | ### The main program to plot vectors |
---|
[182] | 10 | def winds (namefile,\ |
---|
| 11 | nvert,\ |
---|
[184] | 12 | proj=None,\ |
---|
[182] | 13 | back=None,\ |
---|
| 14 | target=None, |
---|
[184] | 15 | stride=3,\ |
---|
| 16 | numplot=4,\ |
---|
[187] | 17 | var=None,\ |
---|
| 18 | colorb=None): |
---|
[184] | 19 | |
---|
| 20 | ################################# |
---|
| 21 | ### Load librairies and functions |
---|
[180] | 22 | from netCDF4 import Dataset |
---|
[184] | 23 | from myplot import getcoord2d,define_proj,makeplotpng,simplinterv,vectorfield,ptitle,latinterv,getproj,wrfinterv,dumpbdy |
---|
[187] | 24 | from matplotlib.pyplot import contourf, subplot, figure, rcParams, savefig, colorbar |
---|
[182] | 25 | import numpy as np |
---|
[184] | 26 | |
---|
| 27 | ###################### |
---|
| 28 | ### Load NETCDF object |
---|
[180] | 29 | nc = Dataset(namefile) |
---|
[184] | 30 | |
---|
| 31 | ################################### |
---|
| 32 | ### Recognize predefined file types |
---|
| 33 | if 'controle' in nc.variables: typefile = 'gcm' |
---|
| 34 | elif 'Um' in nc.variables: typefile = 'mesoapi' |
---|
| 35 | elif 'U' in nc.variables: typefile = 'meso' |
---|
| 36 | else: |
---|
| 37 | print "typefile not supported." |
---|
[182] | 38 | exit() |
---|
[184] | 39 | |
---|
| 40 | ############################################################## |
---|
| 41 | ### Try to guess the projection from wrfout if not set by user |
---|
| 42 | if typefile in ['mesoapi','meso']: |
---|
| 43 | if proj == None: proj = getproj(nc) |
---|
| 44 | ### (il faudrait passer CEN_LON dans la projection ?) |
---|
| 45 | elif typefile in ['gcm']: |
---|
| 46 | if proj == None: proj = "cyl" |
---|
| 47 | ## pb avec les autres (de trace derriere la sphere ?) |
---|
| 48 | |
---|
[186] | 49 | #################################################################### |
---|
| 50 | #### For mesoscale results plot the underlying topography by default |
---|
[184] | 51 | if typefile in ['mesoapi','meso']: |
---|
[186] | 52 | if var == None: back="mola" #var = 'HGT' |
---|
[184] | 53 | |
---|
| 54 | #################################################### |
---|
| 55 | ### Get geographical coordinates and plot boundaries |
---|
| 56 | if typefile in ['mesoapi','meso']: |
---|
| 57 | [lon2d,lat2d] = getcoord2d(nc) |
---|
| 58 | lon2d = dumpbdy(lon2d) |
---|
| 59 | lat2d = dumpbdy(lat2d) |
---|
| 60 | elif typefile in ['gcm']: |
---|
| 61 | [lon2d,lat2d] = getcoord2d(nc,nlat="latitude",nlon="longitude",is1d=True) |
---|
| 62 | if proj == "npstere": [wlon,wlat] = latinterv("North_Pole") |
---|
| 63 | elif proj == "lcc": [wlon,wlat] = wrfinterv(lon2d,lat2d) |
---|
| 64 | else: [wlon,wlat] = simplinterv(lon2d,lat2d) |
---|
| 65 | |
---|
| 66 | ################## |
---|
| 67 | ### Get local time |
---|
| 68 | if typefile in ['mesoapi','meso']: ltst = int(getattr(nc, 'GMT') + 0.5*(wlon[0]+wlon[1])/15.) |
---|
| 69 | elif typefile in ['gcm']: ltst = 0 |
---|
| 70 | |
---|
| 71 | ############################################################################## |
---|
| 72 | ### Get winds and know if those are meteorological winds (ie. zon, mer) or not |
---|
| 73 | if typefile is 'mesoapi': |
---|
| 74 | [u,v] = getwinds(nc) |
---|
| 75 | metwind = True ## meteorological (zon/mer) |
---|
| 76 | elif typefile is 'gcm': |
---|
| 77 | [u,v] = getwinds(nc,charu='u',charv='v') |
---|
| 78 | metwind = True ## meteorological (zon/mer) |
---|
| 79 | elif typefile is 'meso': |
---|
| 80 | [u,v] = getwinds(nc,charu='U',charv='V') |
---|
| 81 | metwind = False ## geometrical (wrt grid) |
---|
[186] | 82 | print "Beware ! Not using meteorological winds. You trust numerical grid as being (x,y)." |
---|
[184] | 83 | |
---|
| 84 | #################################################### |
---|
| 85 | ### Load the chosen variables, whether it is 2D or 3D |
---|
| 86 | if var: |
---|
| 87 | if var not in nc.variables: |
---|
| 88 | print "not found in file:",var |
---|
| 89 | exit() |
---|
| 90 | else: |
---|
| 91 | dimension = np.array(nc.variables[var]).ndim |
---|
| 92 | if dimension == 2: field = nc.variables[var][:,:] |
---|
| 93 | elif dimension == 3: field = nc.variables[var][:,:,:] |
---|
| 94 | elif dimension == 4: field = nc.variables[var][:,nvert,:,:] |
---|
[186] | 95 | nt = len(u[:,0,0,0]) |
---|
[184] | 96 | |
---|
[186] | 97 | ######################################### |
---|
| 98 | ### Name for title and graphics save file |
---|
| 99 | basename = "WINDS" |
---|
| 100 | if var: |
---|
| 101 | basename = basename + "_" + var |
---|
| 102 | basename = basename + "_z" + str(nvert) |
---|
| 103 | |
---|
[184] | 104 | ################################## |
---|
| 105 | ### Open a figure and set subplots |
---|
[180] | 106 | fig = figure() |
---|
[186] | 107 | if numplot > 0: |
---|
| 108 | if numplot == 4: |
---|
| 109 | sub = 221 |
---|
| 110 | fig.subplots_adjust(wspace = 0.1, hspace = 0.3) |
---|
| 111 | rcParams['font.size'] = int( rcParams['font.size'] * 2. / 3. ) |
---|
| 112 | elif numplot == 2: |
---|
| 113 | sub = 121 |
---|
| 114 | fig.subplots_adjust(wspace = 0.3) |
---|
| 115 | rcParams['font.size'] = int( rcParams['font.size'] * 3. / 4. ) |
---|
| 116 | elif numplot == 3: |
---|
| 117 | sub = 131 |
---|
| 118 | fig.subplots_adjust(wspace = 0.3) |
---|
| 119 | rcParams['font.size'] = int( rcParams['font.size'] * 2. / 3. ) |
---|
| 120 | elif numplot == 6: |
---|
| 121 | sub = 231 |
---|
| 122 | fig.subplots_adjust(wspace = 0.4, hspace = 0.0) |
---|
| 123 | rcParams['font.size'] = int( rcParams['font.size'] * 2. / 3. ) |
---|
| 124 | elif numplot == 8: |
---|
| 125 | sub = 331 #241 |
---|
| 126 | fig.subplots_adjust(wspace = 0.1, hspace = 0.3) |
---|
| 127 | rcParams['font.size'] = int( rcParams['font.size'] * 1. / 2. ) |
---|
| 128 | elif numplot == 9: |
---|
| 129 | sub = 331 |
---|
| 130 | fig.subplots_adjust(wspace = 0.1, hspace = 0.3) |
---|
| 131 | rcParams['font.size'] = int( rcParams['font.size'] * 1. / 2. ) |
---|
| 132 | elif numplot == 1: |
---|
| 133 | pass |
---|
| 134 | else: |
---|
| 135 | print "supported: 1,2,3,4,6,8" |
---|
| 136 | exit() |
---|
| 137 | ### Prepare time loop |
---|
| 138 | if nt <= numplot or numplot == 1: |
---|
| 139 | tabrange = [0] |
---|
| 140 | numplot = 1 |
---|
| 141 | else: |
---|
| 142 | tabrange = range(0,nt,int(nt/numplot)) #nt-1 |
---|
| 143 | tabrange = tabrange[0:numplot] |
---|
| 144 | else: |
---|
| 145 | tabrange = range(0,nt,1) |
---|
| 146 | sub = 99999 |
---|
| 147 | print tabrange |
---|
[184] | 148 | |
---|
| 149 | ################################# |
---|
| 150 | ### Time loop for plotting device |
---|
[186] | 151 | found_lct = False |
---|
[184] | 152 | for i in tabrange: |
---|
| 153 | |
---|
| 154 | ### General plot settings |
---|
[186] | 155 | if numplot > 1: |
---|
| 156 | subplot(sub) |
---|
| 157 | found_lct = True |
---|
| 158 | elif numplot == 1: |
---|
| 159 | found_lct = True |
---|
| 160 | ### If only one local time is requested (numplot < 0) |
---|
| 161 | elif numplot <= 0: |
---|
| 162 | #print (ltst+i)%24, numplot, (ltst+i)%24+numplot |
---|
| 163 | if (ltst+i)%24 + numplot != 0: continue |
---|
| 164 | else: found_lct = True |
---|
[184] | 165 | |
---|
| 166 | ### Map projection |
---|
[180] | 167 | m = define_proj(proj,wlon,wlat,back=back) |
---|
| 168 | x, y = m(lon2d, lat2d) |
---|
[184] | 169 | |
---|
| 170 | #### Contour plot |
---|
| 171 | if var: |
---|
| 172 | if typefile in ['mesoapi','meso']: what_I_plot = dumpbdy(field[i,:,:]) |
---|
| 173 | elif typefile in ['gcm']: |
---|
| 174 | if dimension == 2: what_I_plot = field[:,:] |
---|
| 175 | elif dimension == 3: what_I_plot = field[i,:,:] |
---|
| 176 | contourf(x, y, what_I_plot, 30) |
---|
[187] | 177 | if colorb: colorbar(fraction=0.05,pad=0.1) |
---|
[184] | 178 | |
---|
| 179 | ### Vector plot |
---|
| 180 | if typefile in ['mesoapi','meso']: |
---|
| 181 | [vecx,vecy] = [dumpbdy(u[i,nvert,:,:]), dumpbdy(v[i,nvert,:,:])] |
---|
| 182 | key = True |
---|
| 183 | elif typefile in ['gcm']: |
---|
| 184 | [vecx,vecy] = [ u[i,nvert,:,:] , v[i,nvert,:,:] ] |
---|
| 185 | key = False |
---|
| 186 | if metwind: [vecx,vecy] = m.rotate_vector(vecx, vecy, lon2d, lat2d) |
---|
| 187 | vectorfield(vecx, vecy,\ |
---|
[186] | 188 | x, y, stride=stride, csmooth=stride,\ |
---|
[184] | 189 | scale=15., factor=200., color='k', key=key) |
---|
[186] | 190 | ## or csmooth=2 |
---|
[184] | 191 | |
---|
| 192 | ### Next subplot |
---|
[186] | 193 | ptitle( basename + "_LT"+str((ltst+i)%24) ) |
---|
[180] | 194 | sub += 1 |
---|
[184] | 195 | |
---|
| 196 | ########################################################################## |
---|
| 197 | ### Save the figure in a file in the data folder or an user-defined folder |
---|
[186] | 198 | if not target: zeplot = namefile+"_"+basename |
---|
| 199 | else: zeplot = target+"/"+basename |
---|
| 200 | if numplot <= 0: zeplot = zeplot + "_LT"+str(abs(numplot)) |
---|
| 201 | if found_lct: makeplotpng(zeplot,pad_inches_value=0.35) |
---|
| 202 | else: print "Local time not found" |
---|
[180] | 203 | |
---|
[184] | 204 | |
---|
| 205 | |
---|
| 206 | #################################################### |
---|
| 207 | #################################################### |
---|
| 208 | ### A simple program to get wind vectors' components |
---|
| 209 | def getwinds (nc,charu='Um',charv='Vm'): |
---|
[182] | 210 | import numpy as np |
---|
[180] | 211 | u = nc.variables[charu] |
---|
| 212 | v = nc.variables[charv] |
---|
[182] | 213 | if charu == 'U': u = u[:, :, :, 0:len(u[0,0,0,:])-1] |
---|
| 214 | if charv == 'V': v = v[:, :, 0:len(v[0,0,:,0])-1, :] |
---|
[184] | 215 | ### ou alors prendre les coordonnees speciales |
---|
[180] | 216 | return u,v |
---|
| 217 | |
---|
[184] | 218 | |
---|
| 219 | |
---|
| 220 | ########################################################################################### |
---|
| 221 | ########################################################################################### |
---|
| 222 | ### What is below relate to running the file as a command line executable (very convenient) |
---|
[180] | 223 | if __name__ == "__main__": |
---|
| 224 | import sys |
---|
[184] | 225 | from optparse import OptionParser ### to be replaced by argparse |
---|
[186] | 226 | from api_wrapper import api_onelevel |
---|
[182] | 227 | parser = OptionParser() |
---|
| 228 | parser.add_option('-f', action='store', dest='namefile', type="string", default=None, help='name of WRF file [NEEDED]') |
---|
[186] | 229 | parser.add_option('-l', action='store', dest='nvert', type="float", default=0, help='vertical level (def=0)') |
---|
[184] | 230 | parser.add_option('-p', action='store', dest='proj', type="string", default=None, help='projection') |
---|
[182] | 231 | parser.add_option('-b', action='store', dest='back', type="string", default=None, help='background') |
---|
| 232 | parser.add_option('-t', action='store', dest='target', type="string", default=None, help='destination folder') |
---|
[186] | 233 | parser.add_option('-s', action='store', dest='stride', type="int", default=3, help='stride vectors (def=3)') |
---|
[184] | 234 | parser.add_option('-v', action='store', dest='var', type="string", default=None, help='variable contoured') |
---|
[187] | 235 | parser.add_option('-n', action='store', dest='numplot', type="int", default=4, help='number of plots (def=1)(<0: 1 plot of LT -*numplot*)') |
---|
[186] | 236 | parser.add_option('-i', action='store', dest='interp', type="int", default=None, help='interpolation method (done at level *nvert* km)') |
---|
[187] | 237 | parser.add_option('-c', action='store', dest='colorb', type="string", default=None, help='colorbar') |
---|
[182] | 238 | (opt,args) = parser.parse_args() |
---|
| 239 | if opt.namefile is None: |
---|
| 240 | print "I want to eat one file at least ! Use winds.py -f name_of_my_file. Or type winds.py -h" |
---|
| 241 | exit() |
---|
| 242 | print "Options:", opt |
---|
[184] | 243 | |
---|
[186] | 244 | zefile = opt.namefile |
---|
| 245 | zelevel = opt.nvert |
---|
| 246 | if opt.interp is not None: |
---|
[187] | 247 | if opt.nvert is 0 and opt.interp is 4: zelevel = 0.010 |
---|
[186] | 248 | if opt.var is None : zefields = 'uvmet' |
---|
| 249 | else : zefields = 'uvmet,'+opt.var |
---|
| 250 | zefile = api_onelevel ( path_to_input = '', \ |
---|
| 251 | input_name = zefile, \ |
---|
| 252 | path_to_output = opt.target, \ |
---|
| 253 | fields = zefields, \ |
---|
| 254 | interp_method = opt.interp, \ |
---|
| 255 | onelevel = zelevel ) |
---|
| 256 | zelevel = 0 |
---|
[184] | 257 | |
---|
[187] | 258 | winds (zefile,int(zelevel),proj=opt.proj,back=opt.back,target=opt.target,stride=opt.stride,var=opt.var,numplot=opt.numplot,colorb=opt.colorb) |
---|