Changeset 408 in lmdz_wrf for trunk/tools
- Timestamp:
- May 4, 2015, 11:42:22 AM (10 years ago)
- Location:
- trunk/tools
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var.py
r404 r408 17 17 import nc_var_tools as ncvar 18 18 19 operations=['addvals', 'chdimname', 'checkallvars', 'checkAllValues', 'checkNaNs', \ 19 operations=['addvals', 'chdimname', 'changevartype', 'checkallvars', \ 20 'checkAllValues', 'checkNaNs', \ 20 21 'chgtimestep', 'chvarname', 'compute_deaccum', 'compute_opersvarsfiles', \ 21 22 'compute_opervaralltime', 'compute_opervartimes', 'compute_tevolboxtraj', \ … … 99 100 elif oper == 'chdimname': 100 101 ncvar.chdimname(opts.values, opts.ncfile, opts.varname) 102 elif oper == 'changevartype': 103 ncvar.changevartype(opts.values, opts.ncfile, opts.varname) 101 104 elif oper == 'checkallvars': 102 105 ncvar.checkallvars(opts.values, opts.ncfile) -
trunk/tools/nc_var_tools.py
r407 r408 14150 14150 14151 14151 return 14152 14153 14152 #increaseDimvar('shan:29:1:y', 'test.nc', 'var') 14153 14154 def changevartype(values, ncfile, varn): 14155 """ Function to change the type of a variable (when possible) 14156 values=[newtype] type to use ('c', character; 'i', integer, 'f', float, 'd', double) 14157 ncfile= netCDF file to use 14158 varn= variable to change its type 14159 14160 """ 14161 import subprocess as sub 14162 fname = 'changevartype' 14163 14164 if values == 'h': 14165 print fname + '_____________________________________________________________' 14166 print changevartype.__doc__ 14167 quit() 14168 14169 newtype = values 14170 14171 ofile = ncfile + '_new.nc' 14172 14173 onc = NetCDFFile(ncfile, 'r') 14174 14175 if not searchInlist(onc.variables, varn): 14176 print errormsg 14177 print ' ' + fname + ": file '" + ncfile + "' has not variable '" + varn + \ 14178 "' !!" 14179 quit(-1) 14180 14181 dims = onc.dimensions 14182 varns = onc.variables 14183 14184 # Creation of a new file with the new variable dimensions 14185 newnc = NetCDFFile(ofile, 'w') 14186 14187 # Creation of dimensions 14188 for dn in dims: 14189 print 'adding dim:',dn,' ...' 14190 if onc.dimensions[dn].isunlimited(): 14191 newdim = newnc.createDimension(dn, None) 14192 else: 14193 newdim = newnc.createDimension(dn, len(onc.dimensions[dn])) 14194 14195 newnc.sync() 14196 14197 # Transforming variable 14198 varo = onc.variables[varn] 14199 vartype = varo.dtype 14200 vardims = varo.dimensions 14201 varatts = varo.ncattrs() 14202 varshape = varo.shape 14203 varv = varo[:] 14204 14205 if newtype == 'c': 14206 print errormsg 14207 print ' ' + fname + ": new type '" + newtype + "' not ready!!'" 14208 elif newtype == 'i': 14209 newvarv = varv.astype(int) 14210 elif newtype == 'f': 14211 newvarv = varv.astype(np.float) 14212 elif newtype == 'd': 14213 newvarv = varv.astype(np.float64) 14214 else: 14215 print errormsg 14216 print ' ' + fname + ": type '" + newtpe + "' not ready !!" 14217 print ' availables: i, f, d' 14218 quit(-1) 14219 14220 if searchInlist(varatts, '_FillValue'): 14221 fillv = varo.getncattr('_FillValue') 14222 newvar = newnc.createVariable(varn, newtype, vardims, fill_value=fillv) 14223 else: 14224 newvar = newnc.createVariable(varn, newtype, vardims) 14225 14226 for nattr in varo.ncattrs(): 14227 if not nattr == '_FillValue': 14228 nattrv = varo.getncattr(nattr) 14229 newattr = newvar.setncattr(nattr, nattrv) 14230 14231 newvar[:] = newvarv 14232 14233 # Adding variables 14234 for vn in varns: 14235 print 'adding variable:',vn,'...' 14236 vno = onc.variables[vn] 14237 if vn != varn: 14238 vdim = vno.dimensions 14239 vtype = vno.dtype 14240 varatts = vno.ncattrs() 14241 14242 if searchInlist(varatts, '_FillValue'): 14243 fillv = vno.getncattr('_FillValue') 14244 newvar = newnc.createVariable(vn, vtype, vdim, fill_value=fillv) 14245 else: 14246 newvar = newnc.createVariable(vn, vtype, vdim) 14247 14248 for nattr in vno.ncattrs(): 14249 if not nattr == '_FillValue': 14250 nattrv = vno.getncattr(nattr) 14251 newattr = newvar.setncattr(nattr, nattrv) 14252 14253 newvar[:] = vno[:] 14254 14255 onc.close() 14256 newnc.sync() 14257 newnc.close() 14258 14259 # Adding attributes 14260 fgaddattr(ncfile, ofile) 14261 sub.call(['mv',ofile,ncfile]) 14262 14263 return 14264 14265 #changevartype('i', 'test.nc', 'var') 14266 #quit() 14154 14267 14155 14268 """operation to make:
Note: See TracChangeset
for help on using the changeset viewer.