Changeset 1834 in lmdz_wrf for trunk/tools/nc_var_tools.py
- Timestamp:
- Mar 20, 2018, 8:22:37 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var_tools.py
r1831 r1834 18060 18060 """ Function to vertically interpolate using subroutines from the p_interp.F90 NCAR program 18061 18061 Using fortran codes: module_generic.F90 18062 values= [interplevs],[linloginterp],[extrap],[dimtfrac],[dimyfrac] 18063 [interplevs]: ':' separated list of pressure values to interpolate [Pa] (decreassing in Pa) 18064 [linloginterp]: kind of linear interpolation 18065 1: linear interp in pressure 18066 2: linear interp in ln(pressure) 18067 [extrap]: whether to set to missing value below/above model ground and top (0), or extrapolate (1) 18068 [dimtfrac]: fractions of time to interpolate to avoid memory issues ('auto' for 5) 18069 [dimyfrac]: fractions of y-axis to interpolate to avoid memory issues ('auto' for 50) 18070 ncfile= WRF file to use 18071 variables = ',' separated list of names of variables to interpolate ('all', fo all 4D-atmospheric variables) 18072 'WRFght': for WRF geopotential height 18073 'WRFrh': for WRF relative humidity 18074 'WRFt': for WRF temperature 18075 'WRFu': for WRF x-wind de-staggered 18076 'WRFuer': for WRF x-wind de-staggered Earth-rotated 18077 'WRFv': for WRF y-wind de-staggered 18078 'WRFver': for WRF y-wind de-staggered Earth-rotated 18079 'WRFw': for WRF z-wind de-staggered 18080 """ 18081 import module_ForInt as fin 18082 fname = 'pinterp' 18083 18084 if values == 'h': 18085 print fname + '_____________________________________________________________' 18086 print pinterp.__doc__ 18087 quit() 18088 18089 arguments = '[interplevs],[linloginterp],[extrap],[dimtfrac],[dimyfrac]' 18090 gen.check_arguments(fname, values, arguments, ',') 18091 18092 if isFR64: 18093 interplevs = np.array(values.split(',')[0].split(':'), dtype=np.float64) 18094 else: 18095 interplevs = np.array(values.split(',')[0].split(':'), dtype=np.float) 18096 linloginterp = np.int32(values.split(',')[1]) 18097 extrap = np.int32(values.split(',')[2]) 18098 dimtfrac = gen.auto_val(values.split(',')[3], 5) 18099 dimyfrac = gen.auto_val(values.split(',')[4], 50) 18100 18101 ofile = 'pinterp.nc' 18102 18103 CFdims = ['time', 'pres', 'lat', 'lon'] 18104 # Just in case there are weird units, attribute value will be taken from input file 18105 CFattrs = ['standard_name', 'long_name'] 18106 18107 onc = NetCDFFile(ncfile, 'r') 18108 18109 # Gessing orgin of the file 18110 dimsinfile = onc.dimensions.keys() 18111 varsinfile = onc.variables.keys() 18112 18113 if gen.searchInlist(dimsinfile,'bottom_top'): 18114 modname = 'WRF' 18115 print warnmsg 18116 print ' ' + fname + ": gessing that file '" + ncfile + "' comes from '" + \ 18117 modname + "' !!" 18118 18119 # Variables to interpolate 18120 MODdims = ['Time', 'bottom_top', 'south_north', 'west_east'] 18121 newMODdims = ['Time', 'pres', 'south_north', 'west_east'] 18122 notCHK = ['WRFght', 'WRFhus', 'WRFrh', 'WRFt', 'WRFu', 'WRFuer', 'WRFv', \ 18123 'WRFver', 'WRFw'] 18124 MODvarrequired = ['P', 'PB', 'PSFC', 'PH', 'PHB', 'HGT', 'T', 'QVAPOR', \ 18125 'XLONG', 'XLAT', 'Times'] 18126 MODvardims = ['XLONG', 'XLAT', 'Times'] 18127 elif gen.searchInlist(dimsinfile,'presnivs'): 18128 modname = 'LMDZ' 18129 print warnmsg 18130 print ' ' + fname + ": gessing that file '" + ncfile + "' comes from '" + \ 18131 modname + "' !!" 18132 18133 # Variables to interpolate 18134 MODdims = ['time_counter', 'presnivs', 'lat', 'lon'] 18135 newMODdims = ['time_counter', 'pres', 'lat', 'lon'] 18136 notCHK = [] 18137 MODvarrequired = ['pres', 'psol', 'geop', 'phis', 'temp', 'ovap', 'lon', \ 18138 'lat', 'time_counter'] 18139 MODvardims = ['lon', 'lat', 'time_counter'] 18140 elif gen.searchInlist(dimsinfile,'pres') and gen.searchInlist(varsinfile, \ 18141 'psol'): 18142 modname = 'cfLMDZ' 18143 print warnmsg 18144 print ' ' + fname + ": gessing that file '" + ncfile + "' comes from '" + \ 18145 modname + "' ('pres, time_counter' variables renamed as 'p, time') !!" 18146 18147 # Variables to interpolate 18148 MODdims = ['time', 'pres', 'lat', 'lon'] 18149 newMODdims = ['time', 'pres', 'lat', 'lon'] 18150 notCHK = [] 18151 MODvarrequired = ['p', 'psol', 'geop', 'phis', 'temp', 'ovap', 'lon', \ 18152 'lat', 'time'] 18153 MODvardims = ['lon', 'lat', 'time'] 18154 else: 18155 modname = 'CF' 18156 print warnmsg 18157 print ' ' + fname + ": gessing that file '" + ncfile + "' comes from '" + \ 18158 modname + "' !!" 18159 18160 # Variables to interpolate 18161 MODdims = ['time', 'pres', 'lat', 'lon'] 18162 newMODdims = ['time', 'pres', 'lat', 'lon'] 18163 notCHK = [] 18164 MODvarrequired = ['p', 'ps', 'z', 'orog', 'ta', 'hus', 'lon', 'lat', 'time'] 18165 MODvardims = ['lon', 'lat', 'time'] 18166 18167 varns = gen.str_list(variables, ',') 18168 if variables == 'all': 18169 varns = [] 18170 for vn in onc.variables: 18171 ovn = onc.variables[vn] 18172 coincdims = set(WRFdims) & set(ovn.dimensions) 18173 18174 if len(MODdims) == len(ovn.dimensions) and len(coincdims) == len(MODdims): 18175 varns.append(vn) 18176 varns = varns + notCHK 18177 18178 for vn in varns: 18179 if not gen.searchInlist(onc.variables, vn) and \ 18180 not gen.searchInlist(notCHK, vn): 18181 print errormsg 18182 print ' ' + fname + "': file '" + ncfile + "' does not have variable '"+\ 18183 vn + "' !!" 18184 quit(-1) 18185 18186 # looking for model required variables 18187 for var in MODvarrequired: 18188 if not gen.searchInlist(onc.variables, var): 18189 print errormsg 18190 print ' ' + fname + ": file '" + ncfile + "' does not have required " + \ 18191 "variable '" + var + "' !!" 18192 quit(-1) 18193 18194 # Computing necessary variables 18195 # pressure 18196 if modname == 'WRF': 18197 ovar1 = onc.variables['P'] 18198 ovar2 = onc.variables['PB'] 18199 18200 pres0 = ovar1[:] + ovar2[:] 18201 if isFR64: 18202 pres = pres0.astype('float64') 18203 else: 18204 pres = pres0.copy() 18205 18206 dimx = pres.shape[3] 18207 dimy = pres.shape[2] 18208 dimz = pres.shape[1] 18209 dimt = pres.shape[0] 18210 MODdimvs = {'Time':dimt, 'bottom_top':dimz, 'south_north':dimy, \ 18211 'west_east': dimx} 18212 CFdimvs = {'time': dimt, 'bottom_top': dimz, 'lat': dimy, 'lon': dimx} 18213 unstaggerDIM = 'west_east' 18214 elif modname == 'LMDZ': 18215 ovar1 = onc.variables['pres'] 18216 if isFR64: 18217 pres = ovar1[:].astype('float64') 18218 else: 18219 pres = ovar1[:] 18220 dimx = pres.shape[3] 18221 dimy = pres.shape[2] 18222 dimz = pres.shape[1] 18223 dimt = pres.shape[0] 18224 MODdimvs = {'time_counter':dimt, 'presnivs':dimz, 'lat':dimy, 'lon': dimx} 18225 CFdimvs = {'time': dimt, 'presnivs': dimz, 'lat': dimy, 'lon': dimx} 18226 unstaggerDIM = 'lon' 18227 elif modname == 'cfLMDZ': 18228 ovar1 = onc.variables['p'] 18229 if isFR64: 18230 pres = ovar1[:].astype('float64') 18231 else: 18232 pres = ovar1[:] 18233 dimx = pres.shape[3] 18234 dimy = pres.shape[2] 18235 dimz = pres.shape[1] 18236 dimt = pres.shape[0] 18237 MODdimvs = {'time':dimt, 'pres':dimz, 'lat':dimy, 'lon': dimx} 18238 CFdimvs = {'time': dimt, 'pres': dimz, 'lat': dimy, 'lon': dimx} 18239 unstaggerDIM = 'lon' 18240 else: 18241 ovar1 = onc.variables['p'] 18242 if isFR64: 18243 pres = ovar1[:].astype('float64') 18244 else: 18245 pres = ovar1[:] 18246 dimx = pres.shape[3] 18247 dimy = pres.shape[2] 18248 dimz = pres.shape[1] 18249 dimt = pres.shape[0] 18250 MODdimvs = {'time':dimt, 'pres':dimz, 'lat':dimy, 'lon': dimx} 18251 CFdimvs = {'time': dimt, 'pres': dimz, 'lat': dimy, 'lon': dimx} 18252 unstaggerDIM = 'lon' 18253 18254 print ' ' + fname + ': CF dimension lengths:', CFdimvs 18255 18256 # sfc pressure 18257 if modname == 'WRF': 18258 ovar1 = onc.variables['PSFC'] 18259 if isFR64: 18260 psfc = ovar1[:].astype('float64') 18261 else: 18262 psfc = ovar1[:] 18263 elif modname == 'LMDZ' or modname == 'cfLMDZ': 18264 ovar1 = onc.variables['psol'] 18265 if isFR64: 18266 psfc = ovar1[:].astype('float64') 18267 else: 18268 psfc = ovar1[:] 18269 else: 18270 ovar1 = onc.variables['ps'] 18271 if isFR64: 18272 psfc = ovar1[:].astype('float64') 18273 else: 18274 psfc = ovar1[:] 18275 18276 # geopotential height 18277 if modname == 'WRF': 18278 ovar1 = onc.variables['PH'] 18279 ovar2 = onc.variables['PHB'] 18280 geop0 = ovar1[:] + ovar2[:] 18281 unstg = list(geop0.shape) 18282 unstg[1] = unstg[1] - 1 18283 if isFR64: 18284 geop = np.zeros(tuple(unstg), dtype=np.float64) 18285 geop = 0.5*(geop0[:,1:dimz+1,:,:] + geop0[:,0:dimz,:,:]).astype('float64') 18286 else: 18287 geop = np.zeros(tuple(unstg), dtype=np.float) 18288 geop = 0.5*(geop0[:,1:dimz+1,:,:] + geop0[:,0:dimz,:,:]) 18289 elif modname == 'LMDZ' or modname == 'cfLMDZ': 18290 ovar1 = onc.variables['geop'] 18291 if isFR64: 18292 geop = ovar1[:].astype('float64') 18293 else: 18294 geop = ovar1[:] 18295 else: 18296 ovar1 = onc.variables['z'] 18297 if isFR64: 18298 geop = ovar1[:].astype('float64') 18299 else: 18300 geop = ovar1[:] 18301 18302 # terrain height 18303 if modname == 'WRF': 18304 ovar1 = onc.variables['HGT'] 18305 if isFR64: 18306 hgt = ovar1[0,:,:].astype('float64') 18307 else: 18308 hgt = ovar1[0,:,:] 18309 elif modname == 'LMDZ' or modname == 'cfLMDZ': 18310 grav = 9.81 18311 ovar1 = onc.variables['phis'] 18312 if isFR64: 18313 hgt = (ovar1[0,:,:]/grav).astype('float64') 18314 else: 18315 hgt = (ovar1[0,:,:]/grav) 18316 else: 18317 ovar1 = onc.variables['orog'] 18318 if isFR64: 18319 hgt = ovar1[:].astype('float64') 18320 else: 18321 hgt = ovar1[:] 18322 18323 # water vapour mixing ratio 18324 if modname == 'WRF': 18325 ovar1 = onc.variables['QVAPOR'] 18326 if isFR64: 18327 qv = ovar1[:].astype('float64') 18328 else: 18329 qv = ovar1[:] 18330 elif modname == 'LMDZ' or modname == 'cfLMDZ': 18331 ovar1 = onc.variables['ovap'] 18332 if isFR64: 18333 qv = ovar1[:].astype('float64') 18334 else: 18335 qv = ovar1[:] 18336 else: 18337 ovar1 = onc.variables['hus'] 18338 if isFR64: 18339 qv = ovar1[:].astype('float64') 18340 else: 18341 qv = ovar1[:] 18342 18343 # temperature 18344 if modname == 'WRF': 18345 if isFR64: 18346 Rd = np.float64(287.04) 18347 Cp = np.float64(7.*Rd/2.) 18348 RCP = np.float64(Rd/Cp) 18349 p0 = np.float64(100000.) 18350 else: 18351 Rd = 287.04 18352 Cp = 7.*Rd/2. 18353 RCP = Rd/Cp 18354 p0 = 100000. 18355 ovar10 = onc.variables['T'] 18356 var10 = ovar10[:] 18357 if isFR64: 18358 ovar1 = (var10).astype('float64') 18359 else: 18360 ovar1 = var10[:] 18361 temp0 = (ovar1[:]+300.)*(pres[:]/p0)**RCP 18362 if isFR64: 18363 temp = temp0.astype('float64') 18364 else: 18365 temp = temp0.copy() 18366 elif modname == 'LMDZ' or modname == 'cfLMDZ': 18367 ovar1 = onc.variables['temp'] 18368 if isFR64: 18369 temp = ovar1[:].astype('float64') 18370 else: 18371 temp = ovar1[:] 18372 else: 18373 ovar1 = onc.variables['ta'] 18374 if isFR64: 18375 temp = ovar1[:].astype('float64') 18376 else: 18377 temp = ovar1[:] 18378 18379 print ' ' + fname + ": Opening output file 'pinterp.nc'" 18380 onewnc = NetCDFFile(ofile, 'w') 18381 # Creation of dimensions 18382 if modname == 'WRF': 18383 newdim = onewnc.createDimension('west_east', dimx) 18384 newdim = onewnc.createDimension('south_north', dimy) 18385 newdim = onewnc.createDimension('pres', len(interplevs)) 18386 newdim = onewnc.createDimension('Time', None) 18387 elif modname == 'LMDZ': 18388 newdim = onewnc.createDimension('lon', dimx) 18389 newdim = onewnc.createDimension('lat', dimy) 18390 newdim = onewnc.createDimension('pres', len(interplevs)) 18391 newdim = onewnc.createDimension('time_counter', None) 18392 else: 18393 newdim = onewnc.createDimension('lon', dimx) 18394 newdim = onewnc.createDimension('lat', dimy) 18395 newdim = onewnc.createDimension('pres', len(interplevs)) 18396 newdim = onewnc.createDimension('time', None) 18397 18398 # Creation of variable dimensions 18399 for var in MODvardims: 18400 ovar = onc.variables[var] 18401 varinf = variable_inf(ovar) 18402 newvard = [] 18403 for vd in varinf.dimns: 18404 if not gen.searchInlist(onewnc.dimensions, vd) and \ 18405 not gen.searchInlist(CFdims, vd): 18406 newdim = onewnc.createDimension(vd, len(onc.dimensions[vd])) 18407 if gen.searchInlist(CFdims,vd): 18408 icfdim = CFdims.index(vd) 18409 newvard.append(MODdims[icfdim]) 18410 else: 18411 newvard.append(vd) 18412 18413 newvar = onewnc.createVariable(var, nctype(varinf.dtype), \ 18414 tuple(newvard)) 18415 newvar[:] = ovar[:] 18416 for attrn in ovar.ncattrs(): 18417 attrv = ovar.getncattr(attrn) 18418 attr = set_attribute(newvar, attrn, attrv) 18419 18420 # Creation of pressure variable dimension 18421 newvar = onewnc.createVariable('pres', 'f8', ('pres')) 18422 newvar[:] = interplevs[:] 18423 basicvardef(newvar, 'pressure', 'Pressure', 'Pa') 18424 attr = set_attribute(newvar, 'positive', 'down') 18425 attr = set_attribute(newvar, 'axis', 'Z') 18426 18427 print ' ' + fname + ' interpolating at pressure levels _______' 18428 print ' ', interplevs 18429 tsaid = False 18430 ysaid = False 18431 18432 Nplevs = len(interplevs) 18433 for vn in varns: 18434 print " '" + vn + "' ..." 18435 newvarattr = {} 18436 varin = None 18437 18438 # There is a memroy issue, thus is necessary to split the matrix... 18439 if np.prod(list(temp.shape)) > 24*150*150*39: 18440 dimtfrac = np.min([5,dimt]) 18441 if not tsaid: 18442 print warnmsg 18443 print ' ' + fname + ': domain to interpolate:', temp.shape, \ 18444 'too big!!' 18445 print ' p-interpolation will be done by time-slices of 5 time-steps' 18446 print range(0,dimtfrac*int(dimt/dimtfrac),dimtfrac) 18447 else: 18448 dimtfrac = dimt 18449 if np.prod([dimz, dimx, dimy]) > 150*150*39: 18450 dimyfrac = 50 18451 if not ysaid: 18452 print warnmsg 18453 print ' ' + fname + ': variable to interpolate:', temp.shape, \ 18454 'too big!!' 18455 print ' p-interpolation will be done by yaxis-slices of', \ 18456 dimyfrac,' grid-points' 18457 print range(0,dimyfrac*int(dimy/dimyfrac),dimyfrac) 18458 else: 18459 dimyfrac = dimy 18460 18461 varinterp = np.zeros(tuple([dimx,dimyfrac,len(interplevs),dimtfrac]), dtype=np.float) 18462 18463 # Splitting 18464 for itt in range(0,dimtfrac*int(dimt/dimtfrac),dimtfrac): 18465 tini = itt 18466 tend = itt + dimtfrac 18467 Ndt = len(range(tini,tend)) 18468 for iyy in range(0,dimyfrac*int(dimy/dimyfrac),dimyfrac): 18469 yini = iyy 18470 yend = iyy + dimyfrac 18471 Ndy = len(range(yini,yend)) 18472 18473 if gen.searchInlist(notCHK, vn): 18474 if vn == 'WRFght': 18475 varin = geop[tini:tend,:,yini:yend,:] 18476 isgeop = True 18477 varattrs = gen.variables_values('WRFght') 18478 CFvn = varattrs[0] 18479 newvarattr['standard_name'] = varattrs[1] 18480 newvarattr['long_name'] = varattrs[4].replace('|',' ') 18481 newvarattr['units'] = varattrs[5] 18482 elif vn == 'WRFhus': 18483 # specific humidity 18484 varin = qv[tini:tend,:,yini:yend,:]/(1.+qv[tini:tend,:,yini:yend,:]) 18485 isgeop = False 18486 varattrs = gen.variables_values('WRFhus') 18487 CFvn = varattrs[0] 18488 newvarattr['standard_name'] = varattrs[1] 18489 newvarattr['long_name'] = varattrs[4].replace('|',' ') 18490 newvarattr['units'] = varattrs[5] 18491 elif vn == 'WRFt': 18492 varin = temp[tini:tend,:,yini:yend,:] 18493 isgeop = False 18494 varattrs = gen.variables_values('WRFt') 18495 CFvn = varattrs[0] 18496 newvarattr['standard_name'] = varattrs[1] 18497 newvarattr['long_name'] = varattrs[4].replace('|',' ') 18498 newvarattr['units'] = varattrs[5] 18499 elif vn == 'WRFu': 18500 ovarin = onc.variables['U'] 18501 print infmsg 18502 print ' ' + fname + ': De-staggering x-wind variable !!' 18503 print ' from:', ovarin.shape, 'to', (dimt, dimz, dimy, dimx) 18504 if isFR64: 18505 varin = np.zeros((Ndt, dimz, Ndy, dimx), dtype=np.float64) 18506 else: 18507 varin = np.zeros((Ndt, dimz, Ndy, dimx), dtype=np.float) 18508 varin[tini:tend,:,yini:yend,:] = \ 18509 0.5*(ovarin[tini:tend,:,yini:yend,0:dimx] + \ 18510 ovarin[tini:tend,:,yini:yend,1:dimx+1]) 18511 18512 # Not pro, but less memory problems! 18513 #for it in range(dimt): 18514 # varin[it,:,::] = 0.5*(ovarin[it,:,:,0:dimx] + ovarin[it,:,:,1:dimx+1]) 18515 isgeop = False 18516 varattrs = gen.variables_values('ua') 18517 CFvn = varattrs[0] 18518 newvarattr['standard_name'] = varattrs[1] 18519 newvarattr['long_name'] = varattrs[4].replace('|',' ') 18520 newvarattr['units'] = varattrs[5] 18521 elif vn == 'WRFuer': 18522 ovarin = onc.variables['U'] 18523 ovarin2 = onc.variables['V'] 18524 # Earth-rotating 18525 osina = onc.variables['SINALPHA'] 18526 ocosa = onc.variables['COSALPHA'] 18527 print infmsg 18528 print ' ' + fname + ': De-staggering x-wind variable & ' + \ 18529 'Earth rotating!!' 18530 print ' from:', ovarin.shape, 'to', (dimt,dimz,dimy,dimx) 18531 if isFR64: 18532 varin = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64) 18533 varin0 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64) 18534 varin02 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64) 18535 else: 18536 varin = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float) 18537 varin0 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float) 18538 varin02 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float) 18539 # Not pro, but less memory problems! 18540 for it in range(tini,tend): 18541 varin0[it,:,yini:yend,:] = \ 18542 0.5*(ovarin[it,:,yini:yend,0:dimx] + \ 18543 ovarin[it,:,yini:yend,1:dimx+1]) 18544 varin02[it,:,yini:yend,:] = \ 18545 0.5*(ovarin2[it,:,yini:yend,:] + \ 18546 ovarin2[it,:,yini+1:yend+1,:]) 18547 for iz in range(dimz): 18548 varin[tini:tend,iz,yini:yend,:] = \ 18549 varin0[tini:tend,iz,yini:yend,:]* \ 18550 ocosa[tini:tend,yini:yend,:] - \ 18551 varin02[tini:tend,iz,yini:yend,:]* \ 18552 osina[tini:tend,yini:yend,:] 18553 isgeop = False 18554 varattrs = gen.variables_values('ua') 18555 CFvn = varattrs[0] 18556 newvarattr['standard_name'] = varattrs[1] 18557 newvarattr['long_name'] = varattrs[4].replace('|',' ') 18558 newvarattr['units'] = varattrs[5] 18559 elif vn == 'WRFv': 18560 ovarin = onc.variables['V'] 18561 print infmsg 18562 print ' ' + fname + ': De-staggering y-wind variable !!' 18563 print ' from:', ovarin.shape, 'to', (dimt,dimz,dimy,dimx) 18564 if isFR64: 18565 varin = np.zeros((Ndt, dimz, Ndy, dimx), dtype=np.float64) 18566 else: 18567 varin = np.zeros((Ndt, dimz, Ndy, dimx), dtype=np.float) 18568 # Not pro, but less memory problems! 18569 for it in range(tini,tend): 18570 varin[it,:,yini:yend,:] = 0.5*(ovarin[it,:,yini:yend,:]+ \ 18571 ovarin[it,:,yini+1:yend+1,:]) 18572 isgeop = False 18573 varattrs = gen.variables_values('va') 18574 CFvn = varattrs[0] 18575 newvarattr['standard_name'] = varattrs[1] 18576 newvarattr['long_name'] = varattrs[4].replace('|',' ') 18577 newvarattr['units'] = varattrs[5] 18578 elif vn == 'WRFver': 18579 ovarin = onc.variables['U'] 18580 ovarin2 = onc.variables['V'] 18581 # Earth-rotating 18582 osina = onc.variables['SINALPHA'] 18583 ocosa = onc.variables['COSALPHA'] 18584 print infmsg 18585 print ' ' + fname + ': De-staggering y-wind variable & ' + \ 18586 'Earth rotating!!' 18587 print ' from:', ovarin.shape, 'to', (dimt,dimz,dimy,dimx) 18588 if isFR64: 18589 varin = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64) 18590 varin0 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64) 18591 varin02 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64) 18592 else: 18593 varin = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float) 18594 varin0 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float) 18595 varin02 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float) 18596 # Not pro, but less memory problems! 18597 for it in range(tini,tend): 18598 varin0[it,:,yini:yend,:] = \ 18599 0.5*(ovarin[it,:,yini:yend,0:dimx] + \ 18600 ovarin[it,:,yini:yend,1:dimx+1]) 18601 varin02[it,:,yini:yend,:] = \ 18602 0.5*(ovarin2[it,:,yini:yend,:] + \ 18603 ovarin2[it,:,yini+1:yend+1,:]) 18604 for iz in range(dimz): 18605 varin[tini:tend,iz,yini:yend,:] = \ 18606 varin0[tini:tend,iz,yini:yend,:]* \ 18607 osina[tini:tend,yini:yend,:] + \ 18608 varin02[tini:tend,iz,yini:yend,:]* \ 18609 ocosa[tini:tend,yini:yend,:] 18610 isgeop = False 18611 varattrs = gen.variables_values('va') 18612 CFvn = varattrs[0] 18613 newvarattr['standard_name'] = varattrs[1] 18614 newvarattr['long_name'] = varattrs[4].replace('|',' ') 18615 newvarattr['units'] = varattrs[5] 18616 elif vn == 'WRFw': 18617 ovarin = onc.variables['W'] 18618 print infmsg 18619 print ' ' + fname + ': De-staggering z-wind variable !!' 18620 print ' from:', ovarin.shape, 'to', (dimt,dimz,dimy,dimx) 18621 if isFR64: 18622 varin = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64) 18623 else: 18624 varin = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float) 18625 # Not pro, but less memory problems! 18626 for it in range(tini,tend): 18627 varin[it,:,yini:yend,:] = \ 18628 0.5*(ovarin[it,0:dimz,yini:yend,:] + \ 18629 ovarin[it,1:dimz+1,yini:yend,:]) 18630 isgeop = False 18631 varattrs = gen.variables_values('wa') 18632 CFvn = varattrs[0] 18633 newvarattr['standard_name'] = varattrs[1] 18634 newvarattr['long_name'] = varattrs[4].replace('|',' ') 18635 newvarattr['units'] = varattrs[5] 18636 elif vn == 'pres': 18637 varin = pres[tini:tend,:,yini:yend,:] 18638 isgeop = False 18639 varattrs = gen.variables_values('pres') 18640 CFvn = varattrs[0] 18641 newvarattr['standard_name'] = varattrs[1] 18642 newvarattr['long_name'] = varattrs[4].replace('|',' ') 18643 newvarattr['units'] = varattrs[5] 18644 # elif not gen.searchInlist(MODvarrequired, vn) or vn == 'QVAPOR': 18645 else: 18646 ovarin = onc.variables[vn] 18647 varinf = variable_inf(ovarin) 18648 varin = ovarin[tini:tend,:,yini:yend,:] 18649 # de-staggering 18650 #print ' Lluis:', ovarin.dimensions,'unsDIM', unstaggerDIM, 'CFdim:', CFdimvs 18651 #if gen.searchInlist(ovarin.dimensions, unstaggerDIM): 18652 # varin = gen.stagger_unstagger(varin,list(ovarin.dimensions),MODdimvs) 18653 #else: 18654 # varin = gen.stagger_unstagger(varin,list(ovarin.dimensions),CFdimvs) 18655 isgeop = False 18656 if vn == 'z': isgeop = True 18657 varattrs = gen.variables_values(vn) 18658 CFvn = varattrs[0] 18659 for attrn in ovarin.ncattrs(): 18660 attrv = ovarin.getncattr(attrn) 18661 newvarattr[attrn] = attrv 18662 newvarattr['standard_name'] = varattrs[1] 18663 newvarattr['long_name'] = varattrs[4].replace('|',' ') 18664 18665 if varin is not None: 18666 if not gen.searchInlist(onewnc.variables,CFvn): 18667 newvar = onewnc.createVariable(CFvn, 'f4', tuple(newMODdims),\ 18668 fill_value=gen.fillValueF) 18669 varint = varin.transpose() 18670 prest = pres[tini:tend,:,yini:yend,:].transpose() 18671 psfct = psfc[tini:tend,yini:yend,:].transpose() 18672 hgtt = hgt[yini:yend,:].transpose() 18673 tempt = temp[tini:tend,:,yini:yend,:].transpose() 18674 qvt = qv[tini:tend,:,yini:yend,:].transpose() 18675 varinterp = fin.module_forinterpolate.interp( data_in=varint, \ 18676 pres_field=prest, interp_levels=interplevs, psfc=psfct, \ 18677 ter=hgtt, tk=tempt, qv=qvt, linlog=linloginterp, \ 18678 extrapolate=extrap, geopt=isgeop, missing=gen.fillValueF, \ 18679 ix=dimx, iy=Ndy, iz=dimz, it=Ndt, num_metgrid_levels=Nplevs) 18680 newvar[tini:tend,:,yini:yend,:] = varinterp.transpose() 18681 onewnc.sync() 18682 18683 # Finishing split along y-axis 18684 if dimyfrac != dimy and yend != dimy-2: 18685 yini = yend + 0 18686 yend = dimy 18687 if not ysaid: 18688 print ' finishing yaxis-splitting:', yini, ', ', yend 18689 ysaid = True 18690 18691 Ndy = len(range(yini,yend)) 18692 if gen.searchInlist(notCHK, vn): 18693 if vn == 'WRFght': 18694 varin = geop[tini:tend,:,yini:yend,:] 18695 isgeop = True 18696 varattrs = gen.variables_values('WRFght') 18697 CFvn = varattrs[0] 18698 newvarattr['standard_name'] = varattrs[1] 18699 newvarattr['long_name'] = varattrs[4].replace('|',' ') 18700 newvarattr['units'] = varattrs[5] 18701 elif vn == 'WRFhus': 18702 # specific humidity 18703 varin = qv[tini:tend,:,yini:yend,:]/(1.+qv[tini:tend,:,yini:yend,:]) 18704 isgeop = False 18705 varattrs = gen.variables_values('WRFhus') 18706 CFvn = varattrs[0] 18707 newvarattr['standard_name'] = varattrs[1] 18708 newvarattr['long_name'] = varattrs[4].replace('|',' ') 18709 newvarattr['units'] = varattrs[5] 18710 elif vn == 'WRFt': 18711 varin = temp[tini:tend,:,yini:yend,:] 18712 isgeop = False 18713 varattrs = gen.variables_values('WRFt') 18714 CFvn = varattrs[0] 18715 newvarattr['standard_name'] = varattrs[1] 18716 newvarattr['long_name'] = varattrs[4].replace('|',' ') 18717 newvarattr['units'] = varattrs[5] 18718 elif vn == 'WRFu': 18719 ovarin = onc.variables['U'] 18720 print infmsg 18721 print ' ' + fname + ': De-staggering x-wind variable !!' 18722 print ' from:', ovarin.shape, 'to', (dimt, dimz, dimy, dimx) 18723 if isFR64: 18724 varin = np.zeros((Ndt, dimz, Ndy, dimx), dtype=np.float64) 18725 else: 18726 varin = np.zeros((Ndt, dimz, Ndy, dimx), dtype=np.float) 18727 varin[tini:tend,:,yini:yend,:] = \ 18728 0.5*(ovarin[tini:tend,:,yini:yend,0:dimx] + \ 18729 ovarin[tini:tend,:,yini:yend,1:dimx+1]) 18730 18731 # Not pro, but less memory problems! 18732 #for it in range(dimt): 18733 # varin[it,:,::] = 0.5*(ovarin[it,:,:,0:dimx] + ovarin[it,:,:,1:dimx+1]) 18734 isgeop = False 18735 varattrs = gen.variables_values('ua') 18736 CFvn = varattrs[0] 18737 newvarattr['standard_name'] = varattrs[1] 18738 newvarattr['long_name'] = varattrs[4].replace('|',' ') 18739 newvarattr['units'] = varattrs[5] 18740 elif vn == 'WRFuer': 18741 ovarin = onc.variables['U'] 18742 ovarin2 = onc.variables['V'] 18743 # Earth-rotating 18744 osina = onc.variables['SINALPHA'] 18745 ocosa = onc.variables['COSALPHA'] 18746 print infmsg 18747 print ' ' + fname + ': De-staggering x-wind variable & ' + \ 18748 'Earth rotating!!' 18749 print ' from:', ovarin.shape, 'to', (dimt,dimz,dimy,dimx) 18750 if isFR64: 18751 varin = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64) 18752 varin0 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64) 18753 varin02 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64) 18754 else: 18755 varin = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float) 18756 varin0 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float) 18757 varin02 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float) 18758 # Not pro, but less memory problems! 18759 for it in range(tini,tend): 18760 varin0[it,:,yini:yend,:] = \ 18761 0.5*(ovarin[it,:,yini:yend,0:dimx] + \ 18762 ovarin[it,:,yini:yend,1:dimx+1]) 18763 varin02[it,:,yini:yend,:] = \ 18764 0.5*(ovarin2[it,:,yini:yend,:] + \ 18765 ovarin2[it,:,yini+1:yend+1,:]) 18766 for iz in range(dimz): 18767 varin[tini:tend,iz,yini:yend,:] = \ 18768 varin0[tini:tend,iz,yini:yend,:]* \ 18769 ocosa[tini:tend,yini:yend,:] - \ 18770 varin02[tini:tend,iz,yini:yend,:]* \ 18771 osina[tini:tend,yini:yend,:] 18772 isgeop = False 18773 varattrs = gen.variables_values('ua') 18774 CFvn = varattrs[0] 18775 newvarattr['standard_name'] = varattrs[1] 18776 newvarattr['long_name'] = varattrs[4].replace('|',' ') 18777 newvarattr['units'] = varattrs[5] 18778 elif vn == 'WRFv': 18779 ovarin = onc.variables['V'] 18780 print infmsg 18781 print ' ' + fname + ': De-staggering y-wind variable !!' 18782 print ' from:', ovarin.shape, 'to', (dimt,dimz,dimy,dimx) 18783 if isFR64: 18784 varin = np.zeros((Ndt, dimz, Ndy, dimx), dtype=np.float64) 18785 else: 18786 varin = np.zeros((Ndt, dimz, Ndy, dimx), dtype=np.float) 18787 # Not pro, but less memory problems! 18788 for it in range(tini,tend): 18789 varin[it,:,yini:yend,:] = 0.5*(ovarin[it,:,yini:yend,:]+ \ 18790 ovarin[it,:,yini+1:yend+1,:]) 18791 isgeop = False 18792 varattrs = gen.variables_values('va') 18793 CFvn = varattrs[0] 18794 newvarattr['standard_name'] = varattrs[1] 18795 newvarattr['long_name'] = varattrs[4].replace('|',' ') 18796 newvarattr['units'] = varattrs[5] 18797 elif vn == 'WRFver': 18798 ovarin = onc.variables['U'] 18799 ovarin2 = onc.variables['V'] 18800 # Earth-rotating 18801 osina = onc.variables['SINALPHA'] 18802 ocosa = onc.variables['COSALPHA'] 18803 print infmsg 18804 print ' ' + fname + ': De-staggering y-wind variable & ' + \ 18805 'Earth rotating!!' 18806 print ' from:', ovarin.shape, 'to', (dimt,dimz,dimy,dimx) 18807 if isFR64: 18808 varin = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64) 18809 varin0 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64) 18810 varin02 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64) 18811 else: 18812 varin = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float) 18813 varin0 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float) 18814 varin02 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float) 18815 # Not pro, but less memory problems! 18816 for it in range(tini,tend): 18817 varin0[it,:,yini:yend,:] = \ 18818 0.5*(ovarin[it,:,yini:yend,0:dimx] + \ 18819 ovarin[it,:,yini:yend,1:dimx+1]) 18820 varin02[it,:,yini:yend,:] = \ 18821 0.5*(ovarin2[it,:,yini:yend,:] + \ 18822 ovarin2[it,:,yini+1:yend+1,:]) 18823 for iz in range(dimz): 18824 varin[tini:tend,iz,yini:yend,:] = \ 18825 varin0[tini:tend,iz,yini:yend,:]* \ 18826 osina[tini:tend,yini:yend,:] + \ 18827 varin02[tini:tend,iz,yini:yend,:]* \ 18828 ocosa[tini:tend,yini:yend,:] 18829 isgeop = False 18830 varattrs = gen.variables_values('va') 18831 CFvn = varattrs[0] 18832 newvarattr['standard_name'] = varattrs[1] 18833 newvarattr['long_name'] = varattrs[4].replace('|',' ') 18834 newvarattr['units'] = varattrs[5] 18835 elif vn == 'WRFw': 18836 ovarin = onc.variables['W'] 18837 print infmsg 18838 print ' ' + fname + ': De-staggering z-wind variable !!' 18839 print ' from:', ovarin.shape, 'to', (dimt,dimz,dimy,dimx) 18840 if isFR64: 18841 varin = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64) 18842 else: 18843 varin = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float) 18844 # Not pro, but less memory problems! 18845 for it in range(tini,tend): 18846 varin[it,:,yini:yend,:] = \ 18847 0.5*(ovarin[it,0:dimz,yini:yend,:] + \ 18848 ovarin[it,1:dimz+1,yini:yend,:]) 18849 isgeop = False 18850 varattrs = gen.variables_values('wa') 18851 CFvn = varattrs[0] 18852 newvarattr['standard_name'] = varattrs[1] 18853 newvarattr['long_name'] = varattrs[4].replace('|',' ') 18854 newvarattr['units'] = varattrs[5] 18855 elif vn == 'pres': 18856 varin = pres[tini:tend,:,yini:yend,:] 18857 isgeop = False 18858 varattrs = gen.variables_values('pres') 18859 CFvn = varattrs[0] 18860 newvarattr['standard_name'] = varattrs[1] 18861 newvarattr['long_name'] = varattrs[4].replace('|',' ') 18862 newvarattr['units'] = varattrs[5] 18863 # elif not gen.searchInlist(MODvarrequired, vn) or vn == 'QVAPOR': 18864 else: 18865 ovarin = onc.variables[vn] 18866 varinf = variable_inf(ovarin) 18867 varin = ovarin[tini:tend,:,yini:yend,:] 18868 # de-staggering 18869 #print ' Lluis:', ovarin.dimensions,'unsDIM', unstaggerDIM, 'CFdim:', CFdimvs 18870 #if gen.searchInlist(ovarin.dimensions, unstaggerDIM): 18871 # varin = gen.stagger_unstagger(varin,list(ovarin.dimensions),MODdimvs) 18872 #else: 18873 # varin = gen.stagger_unstagger(varin,list(ovarin.dimensions),CFdimvs) 18874 isgeop = False 18875 if vn == 'z': isgeop = True 18876 varattrs = gen.variables_values(vn) 18877 CFvn = varattrs[0] 18878 for attrn in ovarin.ncattrs(): 18879 attrv = ovarin.getncattr(attrn) 18880 newvarattr[attrn] = attrv 18881 newvarattr['standard_name'] = varattrs[1] 18882 newvarattr['long_name'] = varattrs[4].replace('|',' ') 18883 18884 if varin is not None: 18885 varint = varin.transpose() 18886 prest = pres[tini:tend,:,yini:yend,:].transpose() 18887 psfct = psfc[tini:tend,yini:yend,:].transpose() 18888 hgtt = hgt[yini:yend,:].transpose() 18889 tempt = temp[tini:tend,:,yini:yend,:].transpose() 18890 qvt = qv[tini:tend,:,yini:yend,:].transpose() 18891 varinterp = fin.module_forinterpolate.interp( data_in=varint, \ 18892 pres_field=prest, interp_levels=interplevs, psfc=psfct, \ 18893 ter=hgtt, tk=tempt, qv=qvt, linlog=linloginterp, \ 18894 extrapolate=extrap, geopt=isgeop, missing=gen.fillValueF, \ 18895 ix=dimx, iy=Ndy, iz=dimz, it=Ndt, num_metgrid_levels=Nplevs) 18896 newvar[tini:tend,:,yini:yend,:] = varinterp.transpose() 18897 onewnc.sync() 18898 18899 # Finishing dimtfrac 18900 if dimtfrac != dimt and tend != dimt-2: 18901 if not tsaid: 18902 print ' finishing time-splitting:', tend, ', ', dimt 18903 tsaid = True 18904 tini = tend 18905 tend = dimt 18906 Ndt = len(range(tini,tend)) 18907 for iyy in range(0,dimyfrac*int(dimy/dimyfrac),dimyfrac): 18908 yini = iyy 18909 yend = iyy + dimyfrac 18910 Ndy = len(range(yini,yend)) 18911 18912 if gen.searchInlist(notCHK, vn): 18913 if vn == 'WRFght': 18914 varin = geop[tini:tend,:,yini:yend,:] 18915 isgeop = True 18916 varattrs = gen.variables_values('WRFght') 18917 CFvn = varattrs[0] 18918 newvarattr['standard_name'] = varattrs[1] 18919 newvarattr['long_name'] = varattrs[4].replace('|',' ') 18920 newvarattr['units'] = varattrs[5] 18921 elif vn == 'WRFhus': 18922 # specific humidity 18923 varin = qv[tini:tend,:,yini:yend,:]/(1.+qv[tini:tend,:,yini:yend,:]) 18924 isgeop = False 18925 varattrs = gen.variables_values('WRFhus') 18926 CFvn = varattrs[0] 18927 newvarattr['standard_name'] = varattrs[1] 18928 newvarattr['long_name'] = varattrs[4].replace('|',' ') 18929 newvarattr['units'] = varattrs[5] 18930 elif vn == 'WRFt': 18931 varin = temp[tini:tend,:,yini:yend,:] 18932 isgeop = False 18933 varattrs = gen.variables_values('WRFt') 18934 CFvn = varattrs[0] 18935 newvarattr['standard_name'] = varattrs[1] 18936 newvarattr['long_name'] = varattrs[4].replace('|',' ') 18937 newvarattr['units'] = varattrs[5] 18938 elif vn == 'WRFu': 18939 ovarin = onc.variables['U'] 18940 print infmsg 18941 print ' ' + fname + ': De-staggering x-wind variable !!' 18942 print ' from:', ovarin.shape, 'to', (dimt, dimz, dimy, dimx) 18943 if isFR64: 18944 varin = np.zeros((Ndt, dimz, Ndy, dimx), dtype=np.float64) 18945 else: 18946 varin = np.zeros((Ndt, dimz, Ndy, dimx), dtype=np.float) 18947 varin[tini:tend,:,yini:yend,:] = \ 18948 0.5*(ovarin[tini:tend,:,yini:yend,0:dimx] + \ 18949 ovarin[tini:tend,:,yini:yend,1:dimx+1]) 18950 18951 # Not pro, but less memory problems! 18952 #for it in range(dimt): 18953 # varin[it,:,::] = 0.5*(ovarin[it,:,:,0:dimx] + ovarin[it,:,:,1:dimx+1]) 18954 isgeop = False 18955 varattrs = gen.variables_values('ua') 18956 CFvn = varattrs[0] 18957 newvarattr['standard_name'] = varattrs[1] 18958 newvarattr['long_name'] = varattrs[4].replace('|',' ') 18959 newvarattr['units'] = varattrs[5] 18960 elif vn == 'WRFuer': 18961 ovarin = onc.variables['U'] 18962 ovarin2 = onc.variables['V'] 18963 # Earth-rotating 18964 osina = onc.variables['SINALPHA'] 18965 ocosa = onc.variables['COSALPHA'] 18966 print infmsg 18967 print ' ' + fname + ': De-staggering x-wind variable & ' + \ 18968 'Earth rotating!!' 18969 print ' from:', ovarin.shape, 'to', (dimt,dimz,dimy,dimx) 18970 if isFR64: 18971 varin = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64) 18972 varin0 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64) 18973 varin02 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64) 18974 else: 18975 varin = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float) 18976 varin0 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float) 18977 varin02 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float) 18978 # Not pro, but less memory problems! 18979 for it in range(tini,tend): 18980 varin0[it,:,yini:yend,:] = \ 18981 0.5*(ovarin[it,:,yini:yend,0:dimx] + \ 18982 ovarin[it,:,yini:yend,1:dimx+1]) 18983 varin02[it,:,yini:yend,:] = \ 18984 0.5*(ovarin2[it,:,yini:yend,:] + \ 18985 ovarin2[it,:,yini+1:yend+1,:]) 18986 for iz in range(dimz): 18987 varin[tini:tend,iz,yini:yend,:] = \ 18988 varin0[tini:tend,iz,yini:yend,:]* \ 18989 ocosa[tini:tend,yini:yend,:] - \ 18990 varin02[tini:tend,iz,yini:yend,:]* \ 18991 osina[tini:tend,yini:yend,:] 18992 isgeop = False 18993 varattrs = gen.variables_values('ua') 18994 CFvn = varattrs[0] 18995 newvarattr['standard_name'] = varattrs[1] 18996 newvarattr['long_name'] = varattrs[4].replace('|',' ') 18997 newvarattr['units'] = varattrs[5] 18998 elif vn == 'WRFv': 18999 ovarin = onc.variables['V'] 19000 print infmsg 19001 print ' ' + fname + ': De-staggering y-wind variable !!' 19002 print ' from:', ovarin.shape, 'to', (dimt,dimz,dimy,dimx) 19003 if isFR64: 19004 varin = np.zeros((Ndt, dimz, Ndy, dimx), dtype=np.float64) 19005 else: 19006 varin = np.zeros((Ndt, dimz, Ndy, dimx), dtype=np.float) 19007 # Not pro, but less memory problems! 19008 for it in range(tini,tend): 19009 varin[it,:,yini:yend,:] = 0.5*(ovarin[it,:,yini:yend,:]+ \ 19010 ovarin[it,:,yini+1:yend+1,:]) 19011 isgeop = False 19012 varattrs = gen.variables_values('va') 19013 CFvn = varattrs[0] 19014 newvarattr['standard_name'] = varattrs[1] 19015 newvarattr['long_name'] = varattrs[4].replace('|',' ') 19016 newvarattr['units'] = varattrs[5] 19017 elif vn == 'WRFver': 19018 ovarin = onc.variables['U'] 19019 ovarin2 = onc.variables['V'] 19020 # Earth-rotating 19021 osina = onc.variables['SINALPHA'] 19022 ocosa = onc.variables['COSALPHA'] 19023 print infmsg 19024 print ' ' + fname + ': De-staggering y-wind variable & ' + \ 19025 'Earth rotating!!' 19026 print ' from:', ovarin.shape, 'to', (dimt,dimz,dimy,dimx) 19027 if isFR64: 19028 varin = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64) 19029 varin0 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64) 19030 varin02 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64) 19031 else: 19032 varin = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float) 19033 varin0 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float) 19034 varin02 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float) 19035 # Not pro, but less memory problems! 19036 for it in range(tini,tend): 19037 varin0[it,:,yini:yend,:] = \ 19038 0.5*(ovarin[it,:,yini:yend,0:dimx] + \ 19039 ovarin[it,:,yini:yend,1:dimx+1]) 19040 varin02[it,:,yini:yend,:] = \ 19041 0.5*(ovarin2[it,:,yini:yend,:] + \ 19042 ovarin2[it,:,yini+1:yend+1,:]) 19043 for iz in range(dimz): 19044 varin[tini:tend,iz,yini:yend,:] = \ 19045 varin0[tini:tend,iz,yini:yend,:]* \ 19046 osina[tini:tend,yini:yend,:] + \ 19047 varin02[tini:tend,iz,yini:yend,:]* \ 19048 ocosa[tini:tend,yini:yend,:] 19049 isgeop = False 19050 varattrs = gen.variables_values('va') 19051 CFvn = varattrs[0] 19052 newvarattr['standard_name'] = varattrs[1] 19053 newvarattr['long_name'] = varattrs[4].replace('|',' ') 19054 newvarattr['units'] = varattrs[5] 19055 elif vn == 'WRFw': 19056 ovarin = onc.variables['W'] 19057 print infmsg 19058 print ' ' + fname + ': De-staggering z-wind variable !!' 19059 print ' from:', ovarin.shape, 'to', (dimt,dimz,dimy,dimx) 19060 if isFR64: 19061 varin = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64) 19062 else: 19063 varin = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float) 19064 # Not pro, but less memory problems! 19065 for it in range(tini,tend): 19066 varin[it,:,yini:yend,:] = \ 19067 0.5*(ovarin[it,0:dimz,yini:yend,:] + \ 19068 ovarin[it,1:dimz+1,yini:yend,:]) 19069 isgeop = False 19070 varattrs = gen.variables_values('wa') 19071 CFvn = varattrs[0] 19072 newvarattr['standard_name'] = varattrs[1] 19073 newvarattr['long_name'] = varattrs[4].replace('|',' ') 19074 newvarattr['units'] = varattrs[5] 19075 elif vn == 'pres': 19076 varin = pres[tini:tend,:,yini:yend,:] 19077 isgeop = False 19078 varattrs = gen.variables_values('pres') 19079 CFvn = varattrs[0] 19080 newvarattr['standard_name'] = varattrs[1] 19081 newvarattr['long_name'] = varattrs[4].replace('|',' ') 19082 newvarattr['units'] = varattrs[5] 19083 # elif not gen.searchInlist(MODvarrequired, vn) or vn == 'QVAPOR': 19084 else: 19085 ovarin = onc.variables[vn] 19086 varinf = variable_inf(ovarin) 19087 varin = ovarin[tini:tend,:,yini:yend,:] 19088 # de-staggering 19089 #print ' Lluis:', ovarin.dimensions,'unsDIM', unstaggerDIM, 'CFdim:', CFdimvs 19090 #if gen.searchInlist(ovarin.dimensions, unstaggerDIM): 19091 # varin = gen.stagger_unstagger(varin,list(ovarin.dimensions),MODdimvs) 19092 #else: 19093 # varin = gen.stagger_unstagger(varin,list(ovarin.dimensions),CFdimvs) 19094 isgeop = False 19095 if vn == 'z': isgeop = True 19096 varattrs = gen.variables_values(vn) 19097 CFvn = varattrs[0] 19098 for attrn in ovarin.ncattrs(): 19099 attrv = ovarin.getncattr(attrn) 19100 newvarattr[attrn] = attrv 19101 newvarattr['standard_name'] = varattrs[1] 19102 newvarattr['long_name'] = varattrs[4].replace('|',' ') 19103 19104 if varin is not None: 19105 varint = varin.transpose() 19106 prest = pres[tini:tend,:,yini:yend,:].transpose() 19107 psfct = psfc[tini:tend,yini:yend,:].transpose() 19108 hgtt = hgt[yini:yend,:].transpose() 19109 tempt = temp[tini:tend,:,yini:yend,:].transpose() 19110 qvt = qv[tini:tend,:,yini:yend,:].transpose() 19111 varinterp = fin.module_forinterpolate.interp( data_in=varint, \ 19112 pres_field=prest, interp_levels=interplevs, psfc=psfct, \ 19113 ter=hgtt, tk=tempt, qv=qvt, linlog=linloginterp, \ 19114 extrapolate=extrap, geopt=isgeop, missing=gen.fillValueF, \ 19115 ix=dimx, iy=Ndy, iz=dimz, it=Ndt, num_metgrid_levels=Nplevs) 19116 newvar[tini:tend,:,yini:yend,:] = varinterp.transpose() 19117 onewnc.sync() 19118 19119 # Finishing split along y-axis 19120 if dimyfrac != dimy and yend != dimy-2: 19121 yini = yend + 0 19122 yend = dimy 19123 if not ysaid: 19124 print ' finishing yaxis-splitting:', yini, ', ', yend 19125 ysaid = True 19126 19127 Ndy = len(range(yini,yend)) 19128 if gen.searchInlist(notCHK, vn): 19129 if vn == 'WRFght': 19130 varin = geop[tini:tend,:,yini:yend,:] 19131 isgeop = True 19132 varattrs = gen.variables_values('WRFght') 19133 CFvn = varattrs[0] 19134 newvarattr['standard_name'] = varattrs[1] 19135 newvarattr['long_name'] = varattrs[4].replace('|',' ') 19136 newvarattr['units'] = varattrs[5] 19137 elif vn == 'WRFhus': 19138 # specific humidity 19139 varin = qv[tini:tend,:,yini:yend,:]/(1.+qv[tini:tend,:,yini:yend,:]) 19140 isgeop = False 19141 varattrs = gen.variables_values('WRFhus') 19142 CFvn = varattrs[0] 19143 newvarattr['standard_name'] = varattrs[1] 19144 newvarattr['long_name'] = varattrs[4].replace('|',' ') 19145 newvarattr['units'] = varattrs[5] 19146 elif vn == 'WRFt': 19147 varin = temp[tini:tend,:,yini:yend,:] 19148 isgeop = False 19149 varattrs = gen.variables_values('WRFt') 19150 CFvn = varattrs[0] 19151 newvarattr['standard_name'] = varattrs[1] 19152 newvarattr['long_name'] = varattrs[4].replace('|',' ') 19153 newvarattr['units'] = varattrs[5] 19154 elif vn == 'WRFu': 19155 ovarin = onc.variables['U'] 19156 print infmsg 19157 print ' ' + fname + ': De-staggering x-wind variable !!' 19158 print ' from:', ovarin.shape, 'to', (dimt, dimz, dimy, dimx) 19159 if isFR64: 19160 varin = np.zeros((Ndt, dimz, Ndy, dimx), dtype=np.float64) 19161 else: 19162 varin = np.zeros((Ndt, dimz, Ndy, dimx), dtype=np.float) 19163 varin[tini:tend,:,yini:yend,:] = \ 19164 0.5*(ovarin[tini:tend,:,yini:yend,0:dimx] + \ 19165 ovarin[tini:tend,:,yini:yend,1:dimx+1]) 19166 19167 # Not pro, but less memory problems! 19168 #for it in range(dimt): 19169 # varin[it,:,::] = 0.5*(ovarin[it,:,:,0:dimx] + ovarin[it,:,:,1:dimx+1]) 19170 isgeop = False 19171 varattrs = gen.variables_values('ua') 19172 CFvn = varattrs[0] 19173 newvarattr['standard_name'] = varattrs[1] 19174 newvarattr['long_name'] = varattrs[4].replace('|',' ') 19175 newvarattr['units'] = varattrs[5] 19176 elif vn == 'WRFuer': 19177 ovarin = onc.variables['U'] 19178 ovarin2 = onc.variables['V'] 19179 # Earth-rotating 19180 osina = onc.variables['SINALPHA'] 19181 ocosa = onc.variables['COSALPHA'] 19182 print infmsg 19183 print ' ' + fname + ': De-staggering x-wind variable & ' + \ 19184 'Earth rotating!!' 19185 print ' from:', ovarin.shape, 'to', (dimt,dimz,dimy,dimx) 19186 if isFR64: 19187 varin = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64) 19188 varin0 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64) 19189 varin02 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64) 19190 else: 19191 varin = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float) 19192 varin0 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float) 19193 varin02 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float) 19194 # Not pro, but less memory problems! 19195 for it in range(tini,tend): 19196 varin0[it,:,yini:yend,:] = \ 19197 0.5*(ovarin[it,:,yini:yend,0:dimx] + \ 19198 ovarin[it,:,yini:yend,1:dimx+1]) 19199 varin02[it,:,yini:yend,:] = \ 19200 0.5*(ovarin2[it,:,yini:yend,:] + \ 19201 ovarin2[it,:,yini+1:yend+1,:]) 19202 for iz in range(dimz): 19203 varin[tini:tend,iz,yini:yend,:] = \ 19204 varin0[tini:tend,iz,yini:yend,:]* \ 19205 ocosa[tini:tend,yini:yend,:] - \ 19206 varin02[tini:tend,iz,yini:yend,:]* \ 19207 osina[tini:tend,yini:yend,:] 19208 isgeop = False 19209 varattrs = gen.variables_values('ua') 19210 CFvn = varattrs[0] 19211 newvarattr['standard_name'] = varattrs[1] 19212 newvarattr['long_name'] = varattrs[4].replace('|',' ') 19213 newvarattr['units'] = varattrs[5] 19214 elif vn == 'WRFv': 19215 ovarin = onc.variables['V'] 19216 print infmsg 19217 print ' ' + fname + ': De-staggering y-wind variable !!' 19218 print ' from:', ovarin.shape, 'to', (dimt,dimz,dimy,dimx) 19219 if isFR64: 19220 varin = np.zeros((Ndt, dimz, Ndy, dimx), dtype=np.float64) 19221 else: 19222 varin = np.zeros((Ndt, dimz, Ndy, dimx), dtype=np.float) 19223 # Not pro, but less memory problems! 19224 for it in range(tini,tend): 19225 varin[it,:,yini:yend,:] = 0.5*(ovarin[it,:,yini:yend,:]+ \ 19226 ovarin[it,:,yini+1:yend+1,:]) 19227 isgeop = False 19228 varattrs = gen.variables_values('va') 19229 CFvn = varattrs[0] 19230 newvarattr['standard_name'] = varattrs[1] 19231 newvarattr['long_name'] = varattrs[4].replace('|',' ') 19232 newvarattr['units'] = varattrs[5] 19233 elif vn == 'WRFver': 19234 ovarin = onc.variables['U'] 19235 ovarin2 = onc.variables['V'] 19236 # Earth-rotating 19237 osina = onc.variables['SINALPHA'] 19238 ocosa = onc.variables['COSALPHA'] 19239 print infmsg 19240 print ' ' + fname + ': De-staggering y-wind variable & ' + \ 19241 'Earth rotating!!' 19242 print ' from:', ovarin.shape, 'to', (dimt,dimz,dimy,dimx) 19243 if isFR64: 19244 varin = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64) 19245 varin0 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64) 19246 varin02 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64) 19247 else: 19248 varin = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float) 19249 varin0 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float) 19250 varin02 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float) 19251 # Not pro, but less memory problems! 19252 for it in range(tini,tend): 19253 varin0[it,:,yini:yend,:] = \ 19254 0.5*(ovarin[it,:,yini:yend,0:dimx] + \ 19255 ovarin[it,:,yini:yend,1:dimx+1]) 19256 varin02[it,:,yini:yend,:] = \ 19257 0.5*(ovarin2[it,:,yini:yend,:] + \ 19258 ovarin2[it,:,yini+1:yend+1,:]) 19259 for iz in range(dimz): 19260 varin[tini:tend,iz,yini:yend,:] = \ 19261 varin0[tini:tend,iz,yini:yend,:]* \ 19262 osina[tini:tend,yini:yend,:] + \ 19263 varin02[tini:tend,iz,yini:yend,:]* \ 19264 ocosa[tini:tend,yini:yend,:] 19265 isgeop = False 19266 varattrs = gen.variables_values('va') 19267 CFvn = varattrs[0] 19268 newvarattr['standard_name'] = varattrs[1] 19269 newvarattr['long_name'] = varattrs[4].replace('|',' ') 19270 newvarattr['units'] = varattrs[5] 19271 elif vn == 'WRFw': 19272 ovarin = onc.variables['W'] 19273 print infmsg 19274 print ' ' + fname + ': De-staggering z-wind variable !!' 19275 print ' from:', ovarin.shape, 'to', (dimt,dimz,dimy,dimx) 19276 if isFR64: 19277 varin = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64) 19278 else: 19279 varin = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float) 19280 # Not pro, but less memory problems! 19281 for it in range(tini,tend): 19282 varin[it,:,yini:yend,:] = \ 19283 0.5*(ovarin[it,0:dimz,yini:yend,:] + \ 19284 ovarin[it,1:dimz+1,yini:yend,:]) 19285 isgeop = False 19286 varattrs = gen.variables_values('wa') 19287 CFvn = varattrs[0] 19288 newvarattr['standard_name'] = varattrs[1] 19289 newvarattr['long_name'] = varattrs[4].replace('|',' ') 19290 newvarattr['units'] = varattrs[5] 19291 elif vn == 'pres': 19292 varin = pres[tini:tend,:,yini:yend,:] 19293 isgeop = False 19294 varattrs = gen.variables_values('pres') 19295 CFvn = varattrs[0] 19296 newvarattr['standard_name'] = varattrs[1] 19297 newvarattr['long_name'] = varattrs[4].replace('|',' ') 19298 newvarattr['units'] = varattrs[5] 19299 # elif not gen.searchInlist(MODvarrequired, vn) or vn == 'QVAPOR': 19300 else: 19301 ovarin = onc.variables[vn] 19302 varinf = variable_inf(ovarin) 19303 varin = ovarin[tini:tend,:,yini:yend,:] 19304 # de-staggering 19305 #print ' Lluis:', ovarin.dimensions,'unsDIM', unstaggerDIM, 'CFdim:', CFdimvs 19306 #if gen.searchInlist(ovarin.dimensions, unstaggerDIM): 19307 # varin = gen.stagger_unstagger(varin,list(ovarin.dimensions),MODdimvs) 19308 #else: 19309 # varin = gen.stagger_unstagger(varin,list(ovarin.dimensions),CFdimvs) 19310 isgeop = False 19311 if vn == 'z': isgeop = True 19312 varattrs = gen.variables_values(vn) 19313 CFvn = varattrs[0] 19314 for attrn in ovarin.ncattrs(): 19315 attrv = ovarin.getncattr(attrn) 19316 newvarattr[attrn] = attrv 19317 newvarattr['standard_name'] = varattrs[1] 19318 newvarattr['long_name'] = varattrs[4].replace('|',' ') 19319 19320 if varin is not None: 19321 varint = varin.transpose() 19322 prest = pres[tini:tend,:,yini:yend,:].transpose() 19323 psfct = psfc[tini:tend,yini:yend,:].transpose() 19324 hgtt = hgt[yini:yend,:].transpose() 19325 tempt = temp[tini:tend,:,yini:yend,:].transpose() 19326 qvt = qv[tini:tend,:,yini:yend,:].transpose() 19327 varinterp = fin.module_forinterpolate.interp( data_in=varint, \ 19328 pres_field=prest, interp_levels=interplevs, psfc=psfct, \ 19329 ter=hgtt, tk=tempt, qv=qvt, linlog=linloginterp, \ 19330 extrapolate=extrap, geopt=isgeop, missing=gen.fillValueF, \ 19331 ix=dimx, iy=Ndy, iz=dimz, it=Ndt, num_metgrid_levels=Nplevs) 19332 newvar[tini:tend,:,yini:yend,:] = varinterp.transpose() 19333 onewnc.sync() 19334 19335 for attrn in newvarattr.keys(): 19336 if attrn != '_FillValue': 19337 attr = set_attribute(newvar, attrn, newvarattr[attrn]) 19338 onewnc.sync() 19339 19340 # Global attributes 19341 19342 onewnc.setncattr('author', 'L. Fita') 19343 newattr = set_attributek(onewnc, 'institution', unicode('Laboratoire de M' + \ 19344 unichr(233) + 't' + unichr(233) + 'orologie Dynamique'), 'U') 19345 onewnc.setncattr('university', 'Pierre Marie Curie - Jussieu') 19346 onewnc.setncattr('center', 'Centre National de Recherches Scientifiques') 19347 onewnc.setncattr('city', 'Paris') 19348 onewnc.setncattr('country', 'France') 19349 onewnc.setncattr('script', 'nc_var_tools.py') 19350 onewnc.setncattr('function', 'pinterp') 19351 onewnc.setncattr('version', '1.0') 19352 onewnc.setncattr('oringal', 'Interpolation using p_interp.F90 subroutines') 19353 newattr = set_attributek(onewnc, 'original_subroutines_author', unicode('Cindy '+\ 19354 'Bruy' + unichr(232) + 're'), 'U') 19355 onewnc.setncattr('original_subroutines_date', 'November 2007') 19356 19357 for attrn in onc.ncattrs(): 19358 attrv = onc.getncattr(attrn) 19359 attr = set_attribute(onewnc, attrn, attrv) 19360 19361 onc.close() 19362 19363 onewnc.sync() 19364 onewnc.close() 19365 19366 print fname + ": successfull written of '" + ofile + "' !!" 19367 19368 return 19369 19370 def pinterp_simple(values, ncfile, variables): 19371 """ Function to vertically interpolate using subroutines from the p_interp.F90 NCAR program 19372 Using fortran codes: module_generic.F90 18062 19373 values= [interplevs],[linloginterp],[extrap] 18063 19374 [interplevs]: ':' separated list of pressure values to interpolate [Pa] (decreassing in Pa) … … 18774 20085 return 18775 20086 20087 18776 20088 def get_Variables(values, ncfile, variables): 18777 20089 """ Function to get a list of variables from an existing file
Note: See TracChangeset
for help on using the changeset viewer.