Changeset 2195 in lmdz_wrf
- Timestamp:
- Oct 18, 2018, 6:12:36 PM (6 years ago)
- Location:
- trunk/tools
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var.py
r2168 r2195 62 62 ## e.g. # nc_var.py -o temporal_stats -S 'Time:WRFtime:day@1@min,LTday@-3@1@min:bottom_top@ZNU,south_north@XLAT,west_east@XLONG' -f wrfout_d01_1995-01-01_00:00:00 -v T2,Q2 63 63 ## e.g. # nc_var.py -o retrieve_stations -f wrfout_d01_1995-01-01_00:00:00 -S 'tmin_percentiles.nc:stname:None:stlon:stlat:None:nearest:west_east:XLONG:south_north:XLAT:HGT:Time:WRFtime' -v T2,QVAPOR 64 ## e.g. # nc_var.py -o compute_slice2Dstats -S 'XLAT,-63.,19.,2.,HGT,250.,7000.,500.,Time|Times:west_east|XLONG:south_north|XLAT' -f wrfout_d01_1995-01-01_00:00:00 -v T2,Q2 64 65 65 66 from optparse import OptionParser … … 89 90 # compute_opervaralltime: Function to compute opervaralltime: operation of variable successible allover the time-steps 90 91 # compute_opervartimes: Function to compute opervartimes: operation of variable for a given sub-set of time-steps 92 # compute_slice2Dstats: Function to compute stats of variables of a file following slices along 2 variables 91 93 # compute_tevolboxtraj: Function to compute tevolboxtraj: temporal evolution at a given point along a box following a trajectory 92 94 # computevar_model: Function to provide the way to compute a CF-variable providing its name … … 203 205 'chgtimestep', 'chvarname', 'cleaning_varsfile', 'compute_deaccum', \ 204 206 'compute_opersvarsfiles', \ 205 'compute_opervaralltime', 'compute_opervartimes', 'compute_tevolboxtraj', \ 207 'compute_opervaralltime', 'compute_opervartimes', 'compute_slice2Dstats', \ 208 'compute_tevolboxtraj', \ 206 209 'computevar_model', 'curve_section', 'DatesFiles', \ 207 210 'DataSetSection', 'DataSetSection_multidims', 'DataSetSection_multivars', \ … … 342 345 elif oper == 'compute_opervartimes': 343 346 ncvar.compute_opervartimes(opts.values, opts.ncfile, opts.varname) 347 elif oper == 'compute_slice2Dstats': 348 ncvar.compute_slice2Dstats(opts.values, opts.ncfile, opts.varname) 344 349 elif oper == 'compute_tevolboxtraj': 345 350 ncvar.compute_tevolboxtraj(opts.values, opts.ncfile, opts.varname) -
trunk/tools/nc_var_tools.py
r2194 r2195 46 46 # compute_opervaralltime: Function to compute opervaralltime: operation of variable successible allover the time-steps 47 47 # compute_opervartimes: Function to compute opervartimes: operation of variable for a given sub-set of time-steps 48 # compute_slice2Dstats: Function to compute stats of variables of a file following slices along 2 variables 48 49 # computevar_model: Function to provide the way to compute a CF-variable providing its name 49 50 # compute_tevolboxtraj: Function to compute tevolboxtraj: temporal evolution at a given point along a box following a trajectory … … 26132 26133 26133 26134 def compute_slice2Dstats(values, ncfile, variable): 26134 """ Function to compute stats of variables of a file following slices along 2 variables 26135 values=dimn1,varn1,minvar1,maxvar1,slcevar1,dimn2,varn2,minvar2,maxvar2,slcevar2 26136 dimn[1/2]: dimension from which produce the slices of variable 1/2 ('all' 26137 for non-specific dimension to cut along) 26135 """ Function to compute stats of variables of a file following slices along 2 26136 variables 26137 values=varn1,minvar1,maxvar1,slcevar1,varn2,minvar2,maxvar2,slcevar2 26138 26138 varn[1/2]: name of the variable 1/2 26139 26139 minvar[1/2]: minimum value to start the slices for variable 1/2 26140 26140 maxvar[1/2]: maximum value to end the slices for variable 1/2 26141 26141 slcevar[1/2]: length of slices in variable 1/2 26142 dimvars: ':' separated list of [dimn]|[vardimn] dimension name [dimn] and 26143 its associated dimension-variable to get its values to provide values for 26144 the final file 26142 26145 ncfile= netCDF file to use 26143 26146 variable: ',' list of variables ('all' for all variables) … … 26150 26153 quit() 26151 26154 26152 expectargs = ' dimn1,varn1,minvar1,maxvar1,slcevar1,dimn2,varn2,minvar2,' +\26153 'maxvar2,slcevar2'26155 expectargs = 'varn1,minvar1,maxvar1,slcevar1,varn2,minvar2,maxvar2,slcevar2,' + \ 26156 'dimvars' 26154 26157 gen.check_arguments(fname, values, expectargs, ',') 26155 26158 26156 dimn1 = values.split(',')[0] 26157 varn1 = values.split(',')[1] 26158 minvar1 = np.float(values.split(',')[2]) 26159 maxvar1 = np.float(values.split(',')[3]) 26160 slicevar1 = np.float(values.split(',')[4]) 26161 dimn2 = values.split(',')[5] 26162 varn2 = values.split(',')[6] 26163 minvar2 = np.float(values.split(',')[7]) 26164 maxvar2 = np.float(values.split(',')[8]) 26165 slicevar2 = np.float(values.split(',')[9]) 26159 varn1 = values.split(',')[0] 26160 minvar1 = np.float(values.split(',')[1]) 26161 maxvar1 = np.float(values.split(',')[2]) 26162 slicevar1 = np.float(values.split(',')[3]) 26163 varn2 = values.split(',')[4] 26164 minvar2 = np.float(values.split(',')[5]) 26165 maxvar2 = np.float(values.split(',')[6]) 26166 slicevar2 = np.float(values.split(',')[7]) 26167 dimvars = gen.stringS_dictvar(values.split(',')[8], Dc=':', DVs='|') 26166 26168 26167 26169 onc = NetCDFFile(ncfile, 'r') 26168 26169 if dimn1 != 'all' and not gen.searchInlist(onc.dimensions, dimn1):26170 print errormsg26171 print ' ' + fname + ": file '" + ncfile + "' does not have dimension 1: '" +\26172 dimn1 + "' !!"26173 dimns = list(onc.dimensions)26174 dimns.sort()26175 print ' available ones:', dimns26176 quit(-1)26177 26178 if dimn2 != 'all' and not gen.searchInlist(onc.dimensions, dimn2):26179 print errormsg26180 print ' ' + fname + ": file '" + ncfile + "' does not have dimension 2: '" +\26181 dimn2 + "' !!"26182 dimns = list(onc.dimensions)26183 dimns.sort()26184 print ' available ones:', dimns26185 quit(-1)26186 26170 26187 26171 if not onc.variables.has_key(varn1): … … 26203 26187 quit(-1) 26204 26188 26189 for dn in dimvars.keys(): 26190 if not gen.searchInlist(onc.dimensions,dn): 26191 print errormsg 26192 print ' ' +fname+ ": file '" + ncfile + "' does not have dimension: '"+ \ 26193 dn + "' !!" 26194 dimns = list(onc.dimensions) 26195 dimns.sort() 26196 print ' available ones:', dimns 26197 quit(-1) 26198 26199 26205 26200 ovar1 = onc.variables[varn1] 26206 26201 ovar2 = onc.variables[varn2] 26202 v1u = ovar1.units 26203 v2u = ovar2.units 26207 26204 26208 26205 dvar1 = (maxvar1 - minvar1+slicevar1)/slicevar1 … … 26223 26220 sliceshape1 = [Nslices1] + list(ovar1.shape) 26224 26221 slcvar1 = np.zeros(tuple(sliceshape1), dtype=bool) 26222 slcvalsc1 = np.zeros((Nslices1), dtype=np.float) 26223 slcvals1 = np.zeros((Nslices1,2), dtype=np.float) 26225 26224 var1v = ovar1[:] 26226 26225 for islc in range(Nslices1-1): 26227 26226 slcvar1[islc,] = ma.masked_inside(var1v, slices1[islc], slices1[islc+1]).mask 26227 slcvalsc1[islc] = (slices1[islc+1]+slices1[islc+1])/2. 26228 slcvals1[islc,0] = slices1[islc] 26229 slcvals1[islc,1] = slices1[islc+1] 26228 26230 26229 26231 # Slices var2 26230 26232 sliceshape2 = [Nslices2] + list(ovar2.shape) 26231 26233 slcvar2 = np.zeros(tuple(sliceshape2), dtype=bool) 26234 slcvalsc2 = np.zeros((Nslices2), dtype=np.float) 26235 slcvals2 = np.zeros((Nslices2,2), dtype=np.float) 26232 26236 var2v = ovar2[:] 26233 26237 for islc in range(Nslices2-1): 26234 26238 slcvar2[islc,] = ma.masked_inside(var2v, slices2[islc], slices2[islc+1]).mask 26239 slcvalsc2[islc] = (slices2[islc+1]+slices2[islc+1])/2. 26240 slcvals2[islc,0] = slices2[islc] 26241 slcvals2[islc,1] = slices2[islc+1] 26235 26242 26236 26243 if variable == 'all': … … 26239 26246 varns = gen.str_list(variable, ',') 26240 26247 26248 onewnc = NetCDFFile(fname + '.nc', 'w') 26249 # dimensions 26250 newdim = onewnc.createDimension('slice_'+varn1, Nslices1) 26251 newdim = onewnc.createDimension('slice_'+varn2, Nslices2) 26252 newdim = onewnc.createDimension('slice_bnds', 2) 26253 26254 # variable dimensions 26255 newvar = onewnc.createVariable('slice_'+varn1, 'f', ('slice_'+varn1)) 26256 newvar[:] = slcvalsc1[:] 26257 basicvardef(newvar, 'slice_'+varn1, 'slices for variable ' + varn1, v1u) 26258 26259 newvar = onewnc.createVariable('slice_'+varn1+'_bnds', 'f', ('slice_'+varn1, \ 26260 'slice_bnds')) 26261 newvar[:] = slcvals1[:] 26262 basicvardef(newvar, 'slice_'+varn1+'_bnds', 'boundaries of slices for variable '+\ 26263 varn1, v1u) 26264 26265 newvar = onewnc.createVariable('slice_'+varn2, 'f', ('slice_'+varn2)) 26266 newvar[:] = slcvalsc2[:] 26267 basicvardef(newvar, 'slice_'+varn2, 'slices for variable ' + varn2, v2u) 26268 26269 newvar = onewnc.createVariable('slice_'+varn2+'_bnds', 'f', ('slice_'+varn2, \ 26270 'slice_bnds')) 26271 newvar[:] = slcvals2[:] 26272 basicvardef(newvar, 'slice_'+varn2+'_bnds', 'boundaries of slices for variable '+\ 26273 varn2, v2u) 26274 26241 26275 for varn in varns: 26276 print ' ' + varn + ' ...' 26242 26277 ovar = onc.variables[varn] 26243 26278 varv = ovar[:] 26244 26279 vdimns = list(ovar.dimensions) 26245 26280 vshape = list(ovar.shape) 26281 vrank = len(vdimns) 26282 vu = ovar.units 26283 26284 for dn in vdimns: 26285 if not gen.searchInlist(onewnc.dimensions,dn): add_dims(onc, onewnc, [dn]) 26246 26286 26247 26287 # mask1 in var slice … … 26272 26312 26273 26313 newvarshape = [Nslices2, Nslices1] + vshape 26274 newvar2masked = np. zeros(tuple(newvarshape), dtype=ovar.dtype)26314 newvar2masked = np.ones(tuple(newvarshape), dtype=ovar.dtype)*gen.fillValueF 26275 26315 26276 26316 for islc2 in range(Nslices2): 26277 26317 newmask2 = mask2varslice[islc2] 26278 26318 for islc1 in range(Nslices1): 26319 slcnewvar = [] 26320 slcnewvar.append(islc2) 26321 slcnewvar.append(islc1) 26322 for iid in vshape: 26323 slcnewvar.append(slice(0,iid)) 26279 26324 newmask1 = mask1varslice[islc1] 26280 newmask = newmask2+newmask1 26281 26282 print ' Lluis shapes: newvar2masked', newvar2masked[islc2,islc1,].shape, 'varv:', varv[newmask].shape 26283 newvar2masked[islc2,islc1,] = ma.masked_array(varv, mask=~newmask) 26284 26285 onewnc = NetCDFFile(fname + '.nc', 'w') 26286 # dimensions 26287 newdim = onewnc.createDimension('lon', varv.shape[2]) 26288 newdim = onewnc.createDimension('lat', varv.shape[1]) 26289 newdim = onewnc.createDimension('time', None) 26290 newdim = onewnc.createDimension('slice_'+varn1, Nslices1) 26291 newdim = onewnc.createDimension('slice_'+varn2, Nslices2) 26292 26325 newmask = newmask2*newmask1 26326 mavals = ma.array(varv, mask=~newmask) 26327 newvar2masked[tuple(slcnewvar)] = mavals.filled(gen.fillValueF) 26328 26293 26329 # variables 26294 newvar = onewnc.createVariable(varn, 'f', ('slice_'+varn2, 'slice_'+varn1, 'time', 'lat', 'lon')) 26330 newvard = ['slice_'+varn2,'slice_'+varn1] + vdimns 26331 newvar = onewnc.createVariable(varn+'sliced', 'f', tuple(newvard), \ 26332 fill_value=gen.fillValueF) 26295 26333 newvar[:] = newvar2masked[:] 26334 basicvardef(newvar, varn+'sliced', varn + 'sliced by '+varn1+' & '+varn2, vu) 26335 add_varattrs(onc, onewnc, [varn], [varn+'sliced']) 26336 26337 manewvar = ma.masked_equal(newvar2masked, gen.fillValueF) 26338 26339 newvard = ['slice_'+varn2,'slice_'+varn1] 26340 newvarn = onewnc.createVariable(varn+'minsliced', 'f', tuple(newvard), \ 26341 fill_value=gen.fillValueF) 26342 basicvardef(newvarn, varn+'minsliced', 'minimum value of '+varn+'sliced by '+ \ 26343 varn1+' & '+varn2, vu) 26344 add_varattrs(onc, onewnc, [varn], [varn+'minsliced']) 26345 26346 newvarx = onewnc.createVariable(varn+'maxsliced', 'f', tuple(newvard), \ 26347 fill_value=gen.fillValueF) 26348 basicvardef(newvarx, varn+'maxsliced', 'maximum value of '+varn+'sliced by '+ \ 26349 varn1+' & '+varn2, vu) 26350 add_varattrs(onc, onewnc, [varn], [varn+'maxsliced']) 26351 26352 newvarm = onewnc.createVariable(varn+'meansliced', 'f', tuple(newvard), \ 26353 fill_value=gen.fillValueF) 26354 basicvardef(newvarm, varn+'meansliced', 'mean value of '+varn+'sliced by ' + \ 26355 varn1+' & '+varn2, vu) 26356 add_varattrs(onc, onewnc, [varn], [varn+'meansliced']) 26357 26358 newvars = onewnc.createVariable(varn+'stdsliced', 'f', tuple(newvard), \ 26359 fill_value=gen.fillValueF) 26360 basicvardef(newvars, varn+'stdsliced', 'standard deviation of '+varn+ \ 26361 'sliced by '+varn1+' & '+varn2, vu) 26362 add_varattrs(onc, onewnc, [varn], [varn+'stdsliced']) 26363 26364 newvarn[:] = np.min(manewvar, axis=tuple(np.arange(vrank) + 2)) 26365 newvarx[:] = np.max(manewvar, axis=tuple(np.arange(vrank) + 2)) 26366 newvarm[:] = np.mean(manewvar, axis=tuple(np.arange(vrank) + 2)) 26367 newvars[:] = np.std(manewvar, axis=tuple(np.arange(vrank) + 2)) 26296 26368 26297 26369 onewnc.sync() 26298 onewnc.close() 26299 26300 quit() 26370 # Adding dimension-variables 26371 for dn in vdimns: 26372 if not dimvars.has_key(dn): 26373 print errormsg 26374 print ' ' + fname + ": no dimension-variable provided for " + \ 26375 "dimension '" + dn + "' !!" 26376 print ' provided ones _______' 26377 gen.printing_dictionary(dimvars) 26378 quit(-1) 26379 if not onewnc.variables.has_key(dimvars[dn]): 26380 add_vars(onc, onewnc, [dimvars[dn]]) 26381 onewnc.sync() 26382 26383 # Add global attributes 26384 add_global_PyNCplot(onewnc, 'nc_var_tools', fname, '1.0') 26385 add_globattrs(onc,onewnc,'all') 26386 onewnc.sync() 26387 onc.close() 26388 onewnc.close() 26389 print fname + ": successfull written of file '" + fname + ".nc' !!" 26301 26390 26302 26391 return 26303 26392 26304 values='south_north,XLAT,-63.,19.,2.,all,HGT,250.,7000.,500.' 26305 26306 compute_slice2Dstats(values, '/home/lluis/PY/wrfout_d01_1995-01-01_00:00:00', 'T2') 26393 #values='XLAT,-63.,19.,2.,HGT,250.,7000.,500.,Time|Times:west_east|XLONG:south_north|XLAT' 26394 #compute_slice2Dstats(values, '/home/lluis/PY/wrfout_d01_1995-01-01_00:00:00', 'T2,Q2') 26307 26395 26308 26396 #quit()
Note: See TracChangeset
for help on using the changeset viewer.