- Timestamp:
- Jan 8, 2016, 6:08:34 PM (9 years ago)
- Location:
- trunk/tools
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var.py
r656 r662 36 36 'remapnn', \ 37 37 'seasmean', 'sellonlatbox', 'sellonlatlevbox', 'selvar', 'setvar_asciivalues', \ 38 'sorttimesmat', 'spacemean', 'statcompare_files', 'submns', 'subyrs', 'TimeInf', \ 38 'sorttimesmat', 'spacemean', 'SpatialWeightedMean', 'statcompare_files', 'submns', \ 39 'subyrs', 'TimeInf', \ 39 40 'TimeSplitmat', 'timemean', 'valmod', 'valmod_dim','varaddattrk', 'varaddattr', \ 40 41 'varaddref', \ … … 220 221 elif oper == 'spacemean': 221 222 ncvar.spacemean(opts.ncfile, opts.varname) 223 elif oper == 'SpatialWeightedMean': 224 ncvar.SpatialWeightedMean(opts.values, opts.ncfile, opts.varname) 222 225 elif oper == 'statcompare_files': 223 226 ncvar.statcompare_files(opts.values) -
trunk/tools/nc_var_tools.py
r660 r662 17110 17110 return 17111 17111 17112 17112 def DimsLoop(ovar,seldims): 17113 """ Function to provide the shape of the dimensions which are not selected 17114 ova= variable object 17115 seldims= list of dimensions to select 17116 """ 17117 fname = 'DimsLoop' 17118 17119 newshape = [] 17120 noloopdims = [] 17121 17122 idim = 0 17123 for dim in list(ovar.dimensions): 17124 if not searchInlist(seldims,dim): 17125 newshape.append(ovar.shape[idim]) 17126 noloopdims.append(dim) 17127 idim = idim + 1 17128 17129 return newshape, noloopdims 17130 17131 def SliceVar(ovar,dloop,dloopindex): 17132 """ Function to slice a given variable throughout a given list of dimensions 17133 ovar= variable object to slice 17134 dloop= list of dimensions to get a single value 17135 dloopindex= value of the loop dimenion to get 17136 """ 17137 fname = 'SliceVar' 17138 17139 varshape = ovar.shape 17140 17141 slicevals = [] 17142 idim = 0 17143 for dim in ovar.dimensions: 17144 if searchInlist(dloop,dim): 17145 slicevals.append(dloopindex[idim]) 17146 else: 17147 slicevals.append(slice(0,varshape[idim])) 17148 idim = idim + 1 17149 17150 return slicevals 17151 17152 def SpatialWeightedMean(values, filen, varn): 17153 """ Function to compute the spatial mean using weights from a netCDF file 17154 values= [weightskind],[xdimname],[ydimname] 17155 [weightskind]: type of weights: 17156 * 'varnames',[varname],[oper]: using a variable [varname] with an operation [oper] as area size 17157 [oper]: available operations 17158 'inv': inverse of the values 1/[varname] 17159 'nothing': direct values 17160 * 'reglonlat',[lonname],[latname]: regular lon/lat projection to compute the area size 17161 [xdimname]: name of the dimension for the x-axis 17162 [ydimname]: name of the dimension for the y-axis 17163 filen= name of the netCDF file 17164 varn= name of the variable 17165 """ 17166 fname = 'SpatialWeightedMean' 17167 17168 if values == 'h': 17169 print fname + '_____________________________________________________________' 17170 print SpatialWeightedMean.__doc__ 17171 quit() 17172 17173 operations = ['inv','nothing'] 17174 17175 weightk = values.split(',')[0] 17176 xdimname = values.split(',')[3] 17177 ydimname = values.split(',')[4] 17178 17179 ofile = 'SpatialWeightedMean_' + weightk + '.nc' 17180 17181 if weightk == 'varnames': 17182 arguments = '[weightk],[varname],[oper],[xdimname],[ydimname]' 17183 check_arguments(fname, len(values.split(',')), arguments, ',', \ 17184 len(arguments.split(','))) 17185 varname = values.split(',')[1] 17186 oper = values.split(',')[2] 17187 if not searchInlist(operations, oper): 17188 print errormsg 17189 print ' ' + fname + ": operation '" + oper + "' not ready!!" 17190 print ' ready operations:',operations 17191 quit(-1) 17192 elif weightk == 'reglonlat': 17193 arguments = '[weightk],[lonname],[latname],[xdimname],[ydimname]' 17194 check_arguments(fname, len(values.split(',')), arguments, ',', \ 17195 len(arguments.split(','))) 17196 lonname = values.split(',')[1] 17197 latname = values.split(',')[2] 17198 else: 17199 print errormsg 17200 print ' ' + fname + ": wieght type '" + weightk + "' not ready!!" 17201 quit(-1) 17202 17203 # Operations 17204 ## 17205 onc = NetCDFFile(filen, 'r') 17206 if not onc.variables.has_key(varn): 17207 print errormsg 17208 print ' ' + fname + ": file '" + filen + "' does not have variable '" + \ 17209 varn + "' !!" 17210 quit(-1) 17211 iovar = onc.variables[varn] 17212 if not searchInlist(list(iovar.dimensions),xdimname) and not \ 17213 searchInlist(list(iovar.dimensions),ydimname): 17214 print errormsg 17215 print ' ' + fname + ": variable '" + varn + "' does not have dimension '" + \ 17216 xdimname + "' neither '" + ydimname + "' !!" 17217 quit(-1) 17218 17219 # Getting shape of output variables for future average loop 17220 ## 17221 loopshape, dimsloop = DimsLoop(iovar,[xdimname, ydimname]) 17222 17223 if len(loopshape) > 3: 17224 print erromsg 17225 print ' ' + fname + ': output size not ready!!' 17226 quit(-1) 17227 17228 ivarv = iovar[:] 17229 17230 if weightk == 'varnames': 17231 if not onc.variables.has_key(varname): 17232 print errormsg 17233 print ' ' + fname + ": file '" + filen + "' does not have variable '" + \ 17234 varname + "' !!" 17235 quit(-1) 17236 iovarwgt = onc.variables[varname] 17237 17238 ivarwgtv = iovarwgt[:] 17239 if oper == 'inv': 17240 longvarname = 'using inverse of variable ' + varname + ' as space weights' 17241 if len(loopshape) == 1: 17242 newvals = np.ones((loopshape[0]), dtype=np.float)*fillValueF 17243 for id1 in range(loopshape[0]): 17244 slicevalues = SliceVar(iovar,dimsloop,[id1]) 17245 newvals[id1] = np.mean(ivarv[tuple(slicevalues)] / \ 17246 ivarwgtv[tuple(slicevalues)]) 17247 elif len(loopshape) == 2: 17248 newvals = np.ones((loopshape[0],loopshape[1]),dtype=np.float)* \ 17249 fillValueF 17250 for id1 in range(loopshape[0]): 17251 for id2 in range(loopshape[1]): 17252 slicevalues = SliceVar(iovar,dimsloop,[id1,id2]) 17253 newvals[id1,id2] = np.mean(ivarv[tuple(slicevalues)] / \ 17254 ivarwgtv[tuple(slicevalues)]) 17255 elif len(loopshape) == 3: 17256 newvals = np.ones((loopshape[0],loopshape[1],loopshape[2]), \ 17257 dtype=np.float)*fillValueF 17258 for id1 in range(loopshape[0]): 17259 for id2 in range(loopshape[1]): 17260 for id3 in range(loopshape[1]): 17261 slicevalues = SliceVar(iovar,dimsloop,[id1,id2,id3]) 17262 newvals[id1,id2,id3]= np.mean(ivarv[tuple(slicevalues)]/ \ 17263 ivarwgtv[tuple(slicevalues)]) 17264 elif oper == 'nothing': 17265 longvarname = 'using variable ' + varname + ' as space weights' 17266 if len(loopshape) == 1: 17267 newvals = np.ones((loopshape[0]), dtype=np.float)*fillValueF 17268 for id1 in range(loopshape[0]): 17269 slicevalues = SliceVar(iovar,dimsloop,[id1]) 17270 newvals[id1] = np.mean(ivarv[tuple(slicevalues)]* \ 17271 ivarwgtv[tuple(slicevalues)]) 17272 elif len(loopshape) == 2: 17273 newvals = np.ones((loopshape[0],loopshape[1]), dtype=np.float)* \ 17274 fillValueF 17275 for id1 in range(loopshape[0]): 17276 for id2 in range(loopshape[1]): 17277 slicevalues = SliceVar(iovar,dimsloop,[id1,id2]) 17278 newvals[id1,id2] = np.mean(ivarv[tuple(slicevalues)]* \ 17279 ivarwgtv[tuple(slicevalues)]) 17280 elif len(loopshape) == 3: 17281 newvals = np.ones((loopshape[0],loopshape[1],loopshape[2]), \ 17282 dtype=np.float)*fillValueF 17283 for id1 in range(loopshape[0]): 17284 for id2 in range(loopshape[1]): 17285 for id3 in range(loopshape[2]): 17286 slicevalues = SliceVar(iovar,dimsloop,[id1,id2.id3]) 17287 newvals[id1,id2,id3]=np.mean(ivarv[tuple(slicevalues)]* \ 17288 ivarwgtv[tuple(slicevalues)]) 17289 elif weightk == 'reglonlat': 17290 longvarname = 'using cos(latitude) variable ' + latname + ' as space weights' 17291 if not onc.variables.has_key(lonname): 17292 print errormsg 17293 print ' ' + fname + ": file '" + filen + "' does not have variable '" + \ 17294 lonname + "' !!" 17295 quit(-1) 17296 if not onc.variables.has_key(latname): 17297 print errormsg 17298 print ' ' + fname + ": file '" + filen + "' does not have variable '" + \ 17299 latname + "' !!" 17300 quit(-1) 17301 17302 iolon = onc.variables[lonname] 17303 iolat = onc.variables[latname] 17304 ilonv = iolon[:] 17305 ilatv = iolat[:] 17306 17307 lonv, latv = lonlat2D(ilonv, ilatv) 17308 17309 ivarwgtv = np.abs(np.cos(latv*np.pi/180.)) 17310 if len(loopshape) == 1: 17311 newvals = np.ones((loopshape[0]), dtype=np.float)*fillValueF 17312 for id1 in range(loopshape[0]): 17313 slicevalues = SliceVar(iovar,dimsloop,[id1]) 17314 newvals[id1] = np.mean(ivarv[tuple(slicevalues)] * ivarwgtv) 17315 elif len(loopshape) == 2: 17316 newvals = np.ones((loopshape[0],loopshape[1]), dtype=np.float)*fillValueF 17317 for id1 in range(loopshape[0]): 17318 for id2 in range(loopshape[1]): 17319 slicevalues = SliceVar(iovar,dimsloop,[id1,id2]) 17320 newvals[id1,id2] = np.mean(ivarv[tuple(slicevalues)] * ivarwgtv) 17321 elif len(loopshape) == 3: 17322 newvals = np.ones((loopshape[0],loopshape[1],loopshape[2]), \ 17323 dtype=np.float)*fillValueF 17324 for id1 in range(loopshape[0]): 17325 for id2 in range(loopshape[1]): 17326 for id3 in range(loopshape[2]): 17327 slicevalues = SliceVar(iovar,dimsloop,[id1,id2,id3]) 17328 newvals[id1,id2,id3] = np.mean(ivarv[tuple(slicevalues)] * \ 17329 ivarwgtv) 17330 17331 # Writting output file 17332 ## 17333 onewnc = NetCDFFile(ofile, 'w') 17334 17335 # Dimensions creation 17336 ## 17337 for dim in dimsloop: 17338 newdim = onewnc.createDimension(dim, len(onc.dimensions[dim])) 17339 17340 # Output variable 17341 ## 17342 newvar = onewnc.createVariable(varn + 'spaceweightmean', 'f4', tuple(dimsloop), \ 17343 fill_value=fillValueF) 17344 basicvardef(newvar, varn + 'spaceweightmean', 'space weighted ' + varn + ' ' + \ 17345 longvarname, iovar.getncattr('units')) 17346 newvar[:] = newvals 17347 17348 # Global attributes 17349 ## 17350 onewnc.setncattr('script', fname) 17351 onewnc.setncattr('version', '1.0') 17352 onewnc.setncattr('author', 'L. Fita') 17353 onewnc.setncattr('institution', 'Laboratoire de Meteorology Dynamique') 17354 onewnc.setncattr('university', 'Pierre et Marie Curie') 17355 onewnc.setncattr('country', 'France') 17356 for attrs in onc.ncattrs(): 17357 attrv = onc.getncattr(attrs) 17358 attr = set_attribute(onewnc, attrs, attrv) 17359 17360 onc.close() 17361 17362 print fname + ": Successfull written of file: '" + ofile + "' !!" 17363 17364 return 17365 17366 #SpatialWeightedMean('varnames,MAPFAC_M,inv,west_east,south_north','wrfout_d01_2001-11-11_00:00:00','HGT') 17367 #SpatialWeightedMean('reglonlat,XLONG,XLAT,west_east,south_north','wrfout_d01_2001-11-11_00:00:00','HGT') 17113 17368 #quit() 17114 17369
Note: See TracChangeset
for help on using the changeset viewer.