Changeset 670 in lmdz_wrf for trunk/tools/nc_var_tools.py
- Timestamp:
- Jan 15, 2016, 2:46:34 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var_tools.py
r669 r670 17164 17164 return slicevals 17165 17165 17166 def varDimension(oncf, dname): 17167 """ Function to find the variable with the values of a dimension. It is assumed, that given variable 17168 might have a shape with only the dimension, double precision 17169 oncf: netCDF object 17170 dname: name of the dimension 17171 """ 17172 fname = 'varDimension' 17173 17174 if not oncf.dimensions._has_key(dname): 17175 print errormsg 17176 print ' ' + fname + ": NetCDF object does not have dimension '" + dname + "' !!" 17177 quit(-1) 17178 17179 # 1D variables with the dimension name 17180 ## 17181 d1var = [] 17182 for vn in oncf.variables: 17183 ovn = oncf.variables[vn] 17184 if len(ovn.shape) == 1 and ovn.dimensions[0] == dname: d1var.append(vn) 17185 17186 if len(d1var) != 0: 17187 print ' ' + fname + ": 1D variables with dimension '" + dname + "':",d1var 17188 else: 17189 print warnmsg 17190 print ' ' + fname ": Any 1D variables with dimension '" + dname + "' !!" 17191 17192 # Dpouble precision 17193 ## 17194 if len(d1var) == 0: 17195 dimvarname = None 17196 if len(d1var) > 1: 17197 d1vardouble = [] 17198 for vn in d1var 17199 ovn = oncf.variables[vn] 17200 if ovn.dtype == np.float64(1.): d1vardouble.append(vn) 17201 if len(d1vardouble) != 1: 17202 print errormsg 17203 print ' ' + fname + ": More than 1D double variable with dimension '" + dname + "' !!" 17204 quit(-1) 17205 else: 17206 dimvarname = d1vardouble[0] 17207 else: 17208 dimvarname = d1var[0] 17209 17210 return dimvarname 17211 17212 def ovar_onc(ovar, oncf): 17213 """ Function to copy an object variable to a nother netcdf object 17214 17215 """ 17216 fname = 'ovar_onc' 17217 17218 varname = ovar.name 17219 varadims = ovar.dimensions 17220 vartype = ovar.dtype 17221 17222 newvar = oncf.createVariable(varname, vartype, vardims) 17223 newvar = ovar[:] 17224 17225 for ncatr in ovar.ncattrs(): 17226 ncatrv = ovar.get_ncattribute(ncattr) 17227 set_ncatrribute(newvar, ncatr, ncatrv) 17228 17229 oncf.sync() 17230 17231 return 17232 17166 17233 def SpatialWeightedMean(values, filen, varn): 17167 17234 """ Function to compute the spatial mean using weights from a netCDF file … … 17285 17352 slicevalues = SliceVar(iovar,dimsloop,[id1]) 17286 17353 slicewgt = SliceVar(iovarwgt,dimsloop,[id1]) 17287 newvals[id1] = np.mean(ivarv[tuple(slicevalues)] / \ 17288 ivarwgtv[tuple(slicewgt)]) 17354 TOTsumwgt = np.sum(1./ivarwgtv[tuple(slicewgt)]) 17355 newvals[id1] = np.sum(ivarv[tuple(slicevalues)] / \ 17356 ivarwgtv[tuple(slicewgt)]) / TOTsumwgt 17289 17357 elif len(loopshape) == 2: 17290 17358 newvals = np.ones((loopshape[0],loopshape[1]),dtype=np.float)* \ … … 17294 17362 slicevalues = SliceVar(iovar,dimsloop,[id1,id2]) 17295 17363 slicewgt = SliceVar(iovarwgt,dimsloop,[id1,id2]) 17296 newvals[id1,id2] = np.mean(ivarv[tuple(slicevalues)] / \ 17297 ivarwgtv[tuple(slicewgt)]) 17364 TOTsumwgt = np.sum(1./ivarwgtv[tuple(slicewgt)]) 17365 newvals[id1,id2] = np.sum(ivarv[tuple(slicevalues)] / \ 17366 ivarwgtv[tuple(slicewgt)]) / TOTsumwgt 17298 17367 elif len(loopshape) == 3: 17299 17368 newvals = np.ones((loopshape[0],loopshape[1],loopshape[2]), \ … … 17304 17373 slicevalues = SliceVar(iovar,dimsloop,[id1,id2,id3]) 17305 17374 slicewgt = SliceVar(iovarwgt,dimsloop,[id1,id2,id3]) 17306 newvals[id1,id2,id3]= np.mean(ivarv[tuple(slicevalues)]/ \ 17307 ivarwgtv[tuple(slicewgt)]) 17375 TOTsumwgt = np.sum(1./ivarwgtv[tuple(slicewgt)]) 17376 newvals[id1,id2,id3]= np.sum(ivarv[tuple(slicevalues)]/ \ 17377 ivarwgtv[tuple(slicewgt)]) / TOTsumwgt 17308 17378 outweightvals = 1./ivarwgtv[tuple(slicewgt)] 17309 17379 elif oper == 'nothing': … … 17314 17384 slicevalues = SliceVar(iovar,dimsloop,[id1]) 17315 17385 slicewgt = SliceVar(iovarwgt,dimsloop,[id1]) 17316 newvals[id1] = np.mean(ivarv[tuple(slicevalues)]* \ 17317 ivarwgtv[tuple(slicewgt)]) 17386 TOTsumwgt = np.sum(ivarwgtv[tuple(slicewgt)]) 17387 newvals[id1] = np.sum(ivarv[tuple(slicevalues)]* \ 17388 ivarwgtv[tuple(slicewgt)]) / TOTsumwgt 17318 17389 elif len(loopshape) == 2: 17319 17390 newvals = np.ones((loopshape[0],loopshape[1]), dtype=np.float)* \ … … 17323 17394 slicevalues = SliceVar(iovar,dimsloop,[id1,id2]) 17324 17395 slicewgt = SliceVar(iovarwgt,dimsloop,[id1,id2]) 17325 newvals[id1,id2] = np.mean(ivarv[tuple(slicevalues)]* \ 17326 ivarwgtv[tuple(slicewgt)]) 17396 TOTsumwgt = np.sum(ivarwgtv[tuple(slicewgt)]) 17397 newvals[id1,id2] = np.sum(ivarv[tuple(slicevalues)]* \ 17398 ivarwgtv[tuple(slicewgt)]) / TOTsumwgt 17327 17399 elif len(loopshape) == 3: 17328 17400 newvals = np.ones((loopshape[0],loopshape[1],loopshape[2]), \ … … 17333 17405 slicevalues = SliceVar(iovar,dimsloop,[id1,id2.id3]) 17334 17406 slicewgt = SliceVar(iovarwgt,dimsloop,[id1,id2,id3]) 17335 newvals[id1,id2,id3]=np.mean(ivarv[tuple(slicevalues)]* \ 17336 ivarwgtv[tuple(slicewgt)]) 17407 TOTsumwgt = np.sum(ivarwgtv[tuple(slicewgt)]) 17408 newvals[id1,id2,id3]=np.sum(ivarv[tuple(slicevalues)]* \ 17409 ivarwgtv[tuple(slicewgt)]) / TOTsumwgt 17337 17410 outweightvals = ivarwgtv[tuple(slicewgt)] 17338 17411 if oper == 'prodinv': … … 17354 17427 slicevalues = SliceVar(iovar,dimsloop,[id1,id2]) 17355 17428 slicewgt = SliceVar(iovarwgt,dimsloop,[id1,id2]) 17356 newvals[id1,id2] = np.mean(ivarv[tuple(slicevalues)] / \ 17357 ivarwgtv[tuple(slicewgt)]) 17429 TOTsumwgt = np.sum(1./ivarwgtv[tuple(slicewgt)]) 17430 newvals[id1,id2] = np.sum(ivarv[tuple(slicevalues)] / \ 17431 ivarwgtv[tuple(slicewgt)]) / TOTsumwgt 17358 17432 elif len(loopshape) == 3: 17359 17433 newvals = np.ones((loopshape[0],loopshape[1],loopshape[2]), \ … … 17364 17438 slicevalues = SliceVar(iovar,dimsloop,[id1,id2,id3]) 17365 17439 slicewgt = SliceVar(iovarwgt,dimsloop,[id1,id2,id3]) 17366 newvals[id1,id2,id3]= np.mean(ivarv[tuple(slicevalues)]/ \ 17367 ivarwgtv[tuple(slicewgt)]) 17440 TOTsumwgt = np.sum(1./ivarwgtv[tuple(slicewgt)]) 17441 newvals[id1,id2,id3]= np.sum(ivarv[tuple(slicevalues)]/ \ 17442 ivarwgtv[tuple(slicewgt)]) / TOTsumwgt 17368 17443 outweightvals = 1./ivarwgtv[tuple(slicewgt)] 17369 17444 elif weightk == 'reglonlat': … … 17388 17463 17389 17464 ivarwgtv = np.abs(np.cos(latv*np.pi/180.))/(lonv.shape[0]*lonv.shape[1]) 17465 TOTsumwgt = np.sum(ivarwgt) 17390 17466 if len(loopshape) == 1: 17391 17467 newvals = np.ones((loopshape[0]), dtype=np.float)*fillValueF 17392 17468 for id1 in range(loopshape[0]): 17393 17469 slicevalues = SliceVar(iovar,dimsloop,[id1]) 17394 newvals[id1] = np. mean(ivarv[tuple(slicevalues)] * ivarwgtv)17470 newvals[id1] = np.sum(ivarv[tuple(slicevalues)]*ivarwgtv)/TOTsumwgt 17395 17471 elif len(loopshape) == 2: 17396 17472 newvals = np.ones((loopshape[0],loopshape[1]), dtype=np.float)*fillValueF … … 17398 17474 for id2 in range(loopshape[1]): 17399 17475 slicevalues = SliceVar(iovar,dimsloop,[id1,id2]) 17400 newvals[id1,id2] = np.mean(ivarv[tuple(slicevalues)] * ivarwgtv) 17476 newvals[id1,id2] = np.sum(ivarv[tuple(slicevalues)]*ivarwgtv) / \ 17477 TOTsumwgt 17401 17478 elif len(loopshape) == 3: 17402 17479 newvals = np.ones((loopshape[0],loopshape[1],loopshape[2]), \ … … 17406 17483 for id3 in range(loopshape[2]): 17407 17484 slicevalues = SliceVar(iovar,dimsloop,[id1,id2,id3]) 17408 newvals[id1,id2,id3] = np. mean(ivarv[tuple(slicevalues)] *\17409 ivarwgtv) 17485 newvals[id1,id2,id3] = np.sum(ivarv[tuple(slicevalues)] * \ 17486 ivarwgtv) / TOTsumwgt 17410 17487 outweightvals = ivarwgtv 17411 17488 … … 17418 17495 for dim in dimsloop: 17419 17496 newdim = onewnc.createDimension(dim, len(onc.dimensions[dim])) 17497 vardim = varDimension(onc, dim) 17498 if vardim is not None: 17499 ovar_nc(onc.variables[vardim], onewnc) 17500 17420 17501 17421 17502 # Output variable
Note: See TracChangeset
for help on using the changeset viewer.