Changeset 672 in lmdz_wrf for trunk/tools/nc_var_tools.py
- Timestamp:
- Jan 15, 2016, 4:02:25 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var_tools.py
r671 r672 17172 17172 fname = 'varDimension' 17173 17173 17174 if not oncf.dimensions. _has_key(dname):17174 if not oncf.dimensions.has_key(dname): 17175 17175 print errormsg 17176 17176 print ' ' + fname + ": NetCDF object does not have dimension '" + dname + "' !!" … … 17188 17188 else: 17189 17189 print warnmsg 17190 print ' ' + fname ": Any 1D variables with dimension '" + dname + "' !!"17190 print ' ' + fname + ": Any 1D variables with dimension '" + dname + "' !!" 17191 17191 17192 17192 # Dpouble precision … … 17196 17196 if len(d1var) > 1: 17197 17197 d1vardouble = [] 17198 for vn in d1var 17198 for vn in d1var: 17199 17199 ovn = oncf.variables[vn] 17200 if ovn.dtype == np.float64(1.): d1vardouble.append(vn)17201 if len(d1vardouble) !=1:17200 if ovn.dtype == type(np.float64(1.)): d1vardouble.append(vn) 17201 if len(d1vardouble) > 1: 17202 17202 print errormsg 17203 print ' ' + fname + ": More than 1D double variable with dimension '" + dname + "' !!" 17203 print ' ' + fname + ": Found more than 1D double variable with " + \ 17204 "dimension '" + dname + "' !!" 17205 print ' found:', d1vardouble 17204 17206 quit(-1) 17207 elif len(d1vardouble) == 0: 17208 print warnmsg 17209 print ' ' + fname + ": Found any 1D double variable with dimension '" + \ 17210 dname + "' !!" 17211 dimvarname = None 17205 17212 else: 17206 17213 dimvarname = d1vardouble[0] … … 17210 17217 return dimvarname 17211 17218 17212 def ovar_onc( ovar, oncf):17219 def ovar_onc(incf, ovar, oncf): 17213 17220 """ Function to copy an object variable to a nother netcdf object 17221 incf: input netCDF object 17214 17222 ovar= variable object 17215 17223 oncf= netCDF object … … 17217 17225 fname = 'ovar_onc' 17218 17226 17219 varname = ovar.name 17220 varadims = ovar.dimensions 17227 varname = ovar._name 17228 17229 if oncf.variables.has_key(varname): 17230 print warnmsg 17231 print ' ' + fname + ": netCDF object already has variable '" + ovar.name + \ 17232 "' !!" 17233 print ' doing noting' 17234 return 17235 17236 vardims = ovar.dimensions 17221 17237 vartype = ovar.dtype 17238 17239 for dn in vardims: 17240 if not searchInlist(oncf.dimensions, dn): 17241 if incf.dimensions[dn].isunlimited(): 17242 newdim = oncf.createDimension(dn, None) 17243 else: 17244 newdim = oncf.createDimension(dn, len(incf.dimensions[dn])) 17222 17245 17223 17246 newvar = oncf.createVariable(varname, vartype, vardims) 17224 newvar = ovar[:]17225 17226 for ncatrin ovar.ncattrs():17227 ncatrv = ovar.get_ncattribute(ncattr)17228 set_ ncatrribute(newvar, ncatr, ncatrv)17247 newvar[:] = ovar[:] 17248 17249 for attrn in ovar.ncattrs(): 17250 attrv = ovar.getncattr(attrn) 17251 set_attribute(newvar, attrn, attrv) 17229 17252 17230 17253 oncf.sync() … … 17234 17257 def SpatialWeightedMean(values, filen, varn): 17235 17258 """ Function to compute the spatial mean using weights from a netCDF file 17236 values= [weightskind],[xdimname],[ydimname] 17259 values= [weightskind],[xdimname],[ydimname],[addvars] 17237 17260 [weightskind]: type of weights: 17238 17261 * 'varnames',[varname],[oper]: using a variable [varname] with an operation [oper] as area size … … 17245 17268 [xdimname]: name of the dimension for the x-axis 17246 17269 [ydimname]: name of the dimension for the y-axis 17270 [addvars]: ':', separetd list of name of variables to add to the output file 17247 17271 filen= name of the netCDF file 17248 17272 varn= name of the variable … … 17260 17284 xdimname = values.split(',')[3] 17261 17285 ydimname = values.split(',')[4] 17286 addvars = values.split(',')[5] 17262 17287 17263 17288 ofile = 'SpatialWeightedMean_' + weightk + '.nc' 17264 17289 17265 17290 if weightk == 'varnames': 17266 arguments = '[weightk],[varname],[oper],[xdimname],[ydimname] '17291 arguments = '[weightk],[varname],[oper],[xdimname],[ydimname],[addvars]' 17267 17292 check_arguments(fname, len(values.split(',')), arguments, ',', \ 17268 17293 len(arguments.split(','))) … … 17498 17523 vardim = varDimension(onc, dim) 17499 17524 if vardim is not None: 17500 ovar_ nc(onc.variables[vardim], onewnc)17525 ovar_onc(onc, onc.variables[vardim], onewnc) 17501 17526 17502 17503 17527 # Output variable 17504 17528 ## … … 17508 17532 longvarname, iovar.getncattr('units')) 17509 17533 newvar[:] = newvals 17534 17535 onewnc.sync() 17510 17536 17511 17537 # Spatial weight … … 17517 17543 basicvardef(newvar, 'spatialweight', 'space weight ' + longvarname, '-') 17518 17544 newvar[:] = outweightvals 17545 17546 onewnc.sync() 17547 17548 # Additional variables 17549 ## 17550 for vn in addvars.split(':'): 17551 if not onc.variables.has_key(vn): 17552 print errormsg 17553 print ' ' + fname + ": file '" + filen + "' does not have variable '" + \ 17554 vn + "' !!" 17555 quit(-1) 17556 ovar_onc(onc, onc.variables[vn], onewnc) 17557 onewnc.sync() 17519 17558 17520 17559 # Global attributes … … 17531 17570 17532 17571 onc.close() 17572 onewnc.sync() 17573 onewnc.close() 17574 17533 17575 17534 17576 print fname + ": Successfull written of file: '" + ofile + "' !!"
Note: See TracChangeset
for help on using the changeset viewer.