Changeset 1376 in lmdz_wrf
- Timestamp:
- Dec 6, 2016, 6:46:48 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var_tools.py
r1374 r1376 22 22 # add_varattrs: Function to add variables attributes from a given netCDF object to other variables in another one 23 23 # add_vars: Function to add variables from a given netCDF object to another one 24 # addVar: Operation to add a new variable in a file using exisintg dimensions 24 25 # addvals: Function to add values to a given variable at a given dimension 25 26 # attrinvar: Check existence of an attribute in a netCDF variable object … … 104 105 # remapnn_old: Function to remap to the nearest neightbor a variable using projection from another fil 105 106 # reproject: Function to reproject values to another one 107 # rm_FillValue: Operation to remove the _FillValue from a variable inside a netCDF file 106 108 # seasmean: Function to compute the seasonal mean of a variable 107 109 # sellonlatbox: Function to select a lotlan box from a data-set … … 112 114 # set_attributek: Sets a value of an attribute of a netCDF variable with a kind. Removes previous attribute value if exists 113 115 # setvar_asciivalues: Function to set de values of a variable with an ASCII file (common Fortran-like format) 116 # setvar_nc: Operation to set values of a variable from values of an other one 114 117 # SliceVar: Function to slice a given variable throughout a given list of dimensions 115 118 # SliceVarDict: Function to slice a given variable throughout a dictionary … … 14633 14636 def VarVal_FillValue(values, filen, varn): 14634 14637 """ Function to transform a given value from a given variable to _FillValue in a netCDF file 14635 values= [value],[ fillVal]14638 values= [value],[oper],[fillVal] 14636 14639 [value]: value to pass to '_FillValue' 14637 [fillVal]: value for '_FillValue', 'std' for the standard value 14640 [oper]: operation to follow with [value] to transform to `_FillValue' 14641 'eq': equal than [value] 14642 'ge': greater equal than [value] 14643 'gt': greater than [value] 14644 'le': less equal than [value] 14645 'lt': less than [value] 14646 [fillVal]: value for '_FillValue', 'std' for the standard value which means: 14638 14647 Float = 1.e20 14639 14648 Character = '-' … … 14646 14655 fname = 'VarVal_FillValue' 14647 14656 14657 availopers = ['eq', 'ge', 'gt', 'le', 'lt'] 14658 14648 14659 if values == 'h': 14649 14660 print fname + '_____________________________________________________________' 14650 14661 print VarVal_FillValue.__doc__ 14651 14662 quit() 14663 14664 arguments = '[value],[oper],[fillVal]' 14665 gen.check_arguments(fname, values, arguments, ',') 14666 14667 value = values.split(',')[0] 14668 oper = values.split(',')[1] 14669 fval0 = values.split(',')[2] 14652 14670 14653 14671 onc = NetCDFFile(filen, 'a') … … 14657 14675 "variable '" + varn + "' !!" 14658 14676 quit(-1) 14659 14660 value = values.split(',')[0]14661 fval0 = values.split(',')[1]14662 14677 14663 14678 varo = onc.variables[varn] … … 14683 14698 14684 14699 fval = gen.fillvalue_kind(vartype, fval0) 14685 newvarvals = np.where(varvals==valchk, fval, varvals) 14700 14701 if oper == 'eq': 14702 newvarvals = np.where(varvals == valchk, fval, varvals) 14703 elif oper == 'ge': 14704 newvarvals = np.where(varvals >= valchk, fval, varvals) 14705 elif oper == 'gt': 14706 newvarvals = np.where(varvals > valchk, fval, varvals) 14707 elif oper == 'le': 14708 newvarvals = np.where(varvals <= valchk, fval, varvals) 14709 elif oper == 'lt': 14710 newvarvals = np.where(varvals < valchk, fval, varvals) 14711 else: 14712 print errormsg 14713 print ' ' + fname + ": operation '" + oper + "' not ready !!" 14714 print ' available ones:', availopers 14715 quit(-1) 14686 14716 14687 14717 if gen.searchInlist(varo.ncattrs(), 'missing_value'): … … 14708 14738 return 14709 14739 14710 #VarVal_FillValue('0., std', '/home/lluis/etudes/DYNAMICO/ORCHIDEE/interpolation/data/new_lai2D.nc', 'LAI')14740 #VarVal_FillValue('0.,eq,std', '/home/lluis/etudes/DYNAMICO/ORCHIDEE/interpolation/data/new_lai2D.nc', 'LAI') 14711 14741 14712 14742 def lonlatProj(dxres, dyres, project, wfile): … … 18117 18147 return 18118 18148 18149 def addVar(values, ncfile, varn): 18150 """ Operation to add a new variable in a file using exisintg dimensions 18151 values= [dnames]|[varattributes]|[kind] 18152 [dnames]: [dimn1],...,[dimnN], ',' list of name of dimensions for the new variable (already existing) 18153 [attributes]: [std_name]@[long_name]@[units], standard name, long name and units of the variable 18154 ('!' for spaces) 18155 [kind]: type of variable (standard netCDF4/C-like values, 'c', 'i', 'f', 'f8',...) 18156 ncfile= name of the file 18157 varn= name of the new variable 18158 """ 18159 fname = 'addVar' 18160 18161 if values == 'h': 18162 print fname + '_____________________________________________________________' 18163 print addVar.__doc__ 18164 quit() 18165 18166 expectargs = '[dimensions]|[varattributes]|[kind]' 18167 18168 gen.check_arguments(fname,values,expectargs,'|') 18169 18170 dnames = values.split('|')[0].split(',') 18171 attributes = values.split('|')[1].replace('!',' ') 18172 kind = values.split('|')[2] 18173 18174 onc = NetCDFFile(ncfile, 'a') 18175 18176 dimns = onc.dimensions 18177 varns = onc.variables.keys() 18178 18179 # Checking dimensions 18180 dsize = [] 18181 for dimn in dnames: 18182 if not gen.searchInlist(dimns, dimn): 18183 print errormsg 18184 print ' ' +fname+ ": file '" + ncfile + "' does not have dimension '" + \ 18185 dimn + "' !!" 18186 print ' existing ones:', dimns 18187 quit(-1) 18188 else: 18189 dsize.append(len(onc.dimensions[dimn])) 18190 18191 # Checking new varname 18192 if gen.searchInlist(varns, varn): 18193 print errormsg 18194 print ' ' +fname+ ": file '" + ncfile + "' already has variable '" + varn + \ 18195 "' !!" 18196 print ' existing ones:', varns 18197 quit(-1) 18198 18199 # Variable 18200 if kind == 'c': 18201 newvar = onc.createVariable(varn, 'c', tuple(dnames)) 18202 # newvar[:] = np.zeros(tuple(dsize), dtype=np.float) 18203 elif kind == 'f' or kind == 'f4': 18204 newvar = onc.createVariable(varn, 'f4', tuple(dnames), \ 18205 fill_value=gen.fillValueF) 18206 newvar[:] = np.zeros(tuple(dsize), dtype=np.float) 18207 elif kind == 'f8': 18208 newvar = onc.createVariable(varn, 'f8', tuple(dnames), \ 18209 fill_value=gen.fillValueD) 18210 newvar[:] = np.zeros(tuple(dsize), dtype=np.float64) 18211 elif kind == 'i': 18212 newvar = onc.createVariable(varn, 'i', tuple(dnames), \ 18213 fill_value=gen.fillValueI) 18214 newvar[:] = np.zeros(tuple(dsize), dtype=int) 18215 else: 18216 print errormsg 18217 print ' ' + fname + ": variable kind '" + kind + "' not ready!!" 18218 quit(-1) 18219 18220 sname = attributes.split('@')[0] 18221 lname = attributes.split('@')[1] 18222 u = attributes.split('@')[2] 18223 18224 newattr = basicvardef(newvar, sname, lname, u) 18225 18226 onc.sync() 18227 onc.close() 18228 18229 return 18230 18231 def setvar_nc(values, ncfile, varn): 18232 """ Operation to set values of a variable from values of an other one 18233 values= [infile],[invar] 18234 [infile]: name of the file with the reference variable 18235 [invar]: name of the variable in [infile] 18236 [dims]: '|' list of [dimn];[dimval] dimension and value to define slice of values to use from [invar] 18237 * [integer]: which value of the dimension 18238 * -1: all along the dimension 18239 * -9: last value of the dimension 18240 * [beg]:[end]:[frq] slice from [beg] to [end] every [frq] 18241 * NOTE, no dim name all the dimension size 18242 ncfile= name of the file with the variable to fill 18243 varn= name of the variable to fill 18244 """ 18245 fname = 'addVar' 18246 18247 if values == 'h': 18248 print fname + '_____________________________________________________________' 18249 print addVar.__doc__ 18250 quit() 18251 18252 expectargs = '[infile],[invar],[dims]' 18253 18254 gen.check_arguments(fname,values,expectargs,',') 18255 18256 infile = values.split(',')[0] 18257 invar = values.split(',')[1] 18258 dims = values.split(',')[2].split('|') 18259 18260 oinc = NetCDFFile(infile, 'r') 18261 onc = NetCDFFile(ncfile, 'a') 18262 18263 if not onc.variables.has_key(varn): 18264 print errormsg 18265 print ' '+fname+": file '" + ncfile + "' does not have variable '" + varn + \ 18266 "' to be filled !!" 18267 print ' existing ones:', onc.variables.keys() 18268 quit(-1) 18269 18270 if not oinc.variables.has_key(invar): 18271 print errormsg 18272 print ' '+fname+": file '" + infile + "' does not have variable '" + invar +\ 18273 "' to use for filling !!" 18274 print ' existing ones:', oinc.variables.keys() 18275 quit(-1) 18276 18277 oinvar = oinc.variables[invar] 18278 ovar = onc.variables[varn] 18279 18280 # Slicing input varibale 18281 slicedict = {} 18282 for dimns in dims: 18283 dimn = dimns.split(';')[0] 18284 dimv = dimns.split(';')[1] 18285 if dimv.find(':') != -1: 18286 slicedict[dimn] = np.array(dimv.split(':'), dtype=int) 18287 else: 18288 slicedict[dimn] = int(dimv) 18289 18290 invarslice, invardims = SliceVarDict(oinvar, slicedict) 18291 invals = oinvar[tuple(invarslice)] 18292 18293 # Checking same shapes 18294 gen.same_shape(invals, ovar[:]) 18295 18296 ovar[:] = invals[:] 18297 18298 oinc.close() 18299 18300 onc.sync() 18301 onc.close() 18302 18303 return 18304 18305 def rm_FillValue(values, ncfile, varn): 18306 """ Operation to remove the _FillValue from a variable inside a netCDF file 18307 values= [newvalue] 18308 [newvalue]: value to use to replace '_FillValue' 18309 ncfile= name of the file with the variable to fill 18310 varn= name of the variable to fill 18311 """ 18312 import numpy.ma as ma 18313 fname = 'rm_FillValue' 18314 18315 if values == 'h': 18316 print fname + '_____________________________________________________________' 18317 print rm_FillValue.__doc__ 18318 quit() 18319 18320 # expectargs = '[newvalue]' 18321 18322 # gen.check_arguments(fname,values,expectargs,',') 18323 18324 newvalue = values 18325 18326 onc = NetCDFFile(ncfile, 'a') 18327 18328 if not onc.variables.has_key(varn): 18329 print errormsg 18330 print ' ' + fname + ": file '" + ncfile + "' does not have variable '" + \ 18331 varn + "' !!" 18332 print ' available ones:'. onc.variables.keys() 18333 quit(-1) 18334 18335 ovar = onc.variables[varn] 18336 varattrs = ovar.ncattrs() 18337 18338 # Checking for _FillValue 18339 if not gen.searchInlist(varattrs, '_FillValue'): 18340 print warnmsg 18341 print ' ' + fname + ": varibale '" + varn + "' does not have _FillValue " + \ 18342 "attribute !!" 18343 print ' attributes:', varattrs 18344 print " Use `nc_var.py -o valmod -f " + ncfile + " -S '[...]' -v " + \ 18345 varn + " instead !!" 18346 quit(-1) 18347 18348 varattrs.remove('_FillValue') 18349 maskvarvals = ovar[:] 18350 newvals = maskvarvals.filled(gen.retype(newvalue,ovar.dtype)) 18351 attr = ovar.delncattr('_FillValue') 18352 18353 ovar[:] = newvals[:] 18354 onc.sync() 18355 onc.close() 18356 18357 return 18358 18119 18359 #quit()
Note: See TracChangeset
for help on using the changeset viewer.