Changeset 404 in lmdz_wrf for trunk/tools
- Timestamp:
- Apr 30, 2015, 5:08:15 PM (10 years ago)
- Location:
- trunk/tools
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var.py
r397 r404 25 25 'filter_2dim', \ 26 26 'flipdim', 'fvaradd', 'gaddattrk', 'gaddattr', 'get_namelist_vars', 'grattr', \ 27 'grmattr', 'igattrs', 'isgattrs', 'isvattrs', 'ivars', 'ivattrs', 'maskvar', \ 27 'grmattr', 'igattrs', 'increaseDimvar', 'isgattrs', 'isvattrs', 'ivars', 'ivattrs',\ 28 'maskvar', \ 28 29 'ncreplace', 'ncstepdiff', 'netcdf_concatenation', 'netcdf_fold_concatenation', \ 29 30 'compute_opersvarsfiles', \ … … 156 157 elif oper == 'igattrs': 157 158 ncvar.igattrs(opts.ncfile) 159 elif oper == 'increaseDimvar': 160 ncvar.increaseDimvar(opts.values, opts.ncfile, opts.varname) 158 161 elif oper == 'isgattrs': 159 162 ncvar.isgattrs(opts.values, opts.ncfile) -
trunk/tools/nc_var_tools.py
r399 r404 2062 2062 ncref.close() 2063 2063 2064 return 2065 2064 2066 def varrm(ncfile, var): 2065 2067 """ Removing a variable from a file … … 14006 14008 14007 14009 #dimToUnlimited('lon', 'test.nc') 14008 #quit() 14010 14011 def increaseDimvar(values, ncfile, varn): 14012 """ Function to increase with 1 dimension an existing variable within a netcdf file. Values 14013 of the variable will be repeated along the new dimension 14014 values='[dimname]:[size]:[position]:[unlimited]' 14015 dimname: name of the new dimension 14016 size: size of the new dimension 14017 position: position for the dimensions (starting from 0, outter most [left]) 14018 unlimited: y/n value for an unlimited dimension 14019 ncfile= netCDF file to use 14020 varn= variable to change its dimensions 14021 14022 """ 14023 import subprocess as sub 14024 14025 fname = 'increaseDimvar' 14026 14027 ofile = ncfile + '_new.nc' 14028 14029 if values == 'h': 14030 print fname + '_____________________________________________________________' 14031 print increaseDimvar.__doc__ 14032 quit() 14033 14034 expectargs = '[dimname]:[size]:[position]:[unlimited]' 14035 14036 check_arguments(fname,len(expectargs.split(':')),values,':',expectargs) 14037 14038 dimname = values.split(':')[0] 14039 size = int(values.split(':')[1]) 14040 position = int(values.split(':')[2]) 14041 unlimited = values.split(':')[3] 14042 14043 onc = NetCDFFile(ncfile, 'r') 14044 14045 dims = onc.dimensions 14046 if searchInlist(dims, dimname): 14047 print errormsg 14048 print ' ' + fname + ": file '" + ncfile + "' already has dimension '" + \ 14049 dimname + "' !!" 14050 quit(-1) 14051 14052 varns = onc.variables 14053 if not searchInlist(varns, varn): 14054 print errormsg 14055 print ' ' + fname + ": file '" + ncfile + "' has not variable '" + varn + \ 14056 "' !!" 14057 quit(-1) 14058 14059 varobj = onc.variables[varn] 14060 vardims = varobj.dimensions 14061 varkind = varobj.dtype 14062 Norigdims = len(vardims) 14063 14064 if position > Norigdims - 1: 14065 print errormsg 14066 print ' ' + fname + ": wrong position ", position," for the new dimension ",\ 14067 "on variable '" + varn + "' has only", Norigdims," dimensions !!" 14068 quit(-1) 14069 14070 newdimns = [] 14071 newdimvs = [] 14072 14073 idorig = Norigdims - 1 14074 for idn in range(Norigdims + 1): 14075 if idn == position: 14076 newdimns.append(dimname) 14077 newdimvs.append(size) 14078 else: 14079 newdimns.append(vardims[idorig]) 14080 newdimvs.append(varobj.shape[idorig]) 14081 idorig = idorig - 1 14082 14083 # New variable 14084 newvarv = np.zeros(tuple(newdimvs), dtype=varkind) 14085 for inewdim in range(size): 14086 slicev = [] 14087 for idn in range(Norigdims + 1): 14088 if idn == position: 14089 slicev.append(inewdim) 14090 else: 14091 slicev.append(slice(0,varobj.shape[idorig])) 14092 14093 newvarv[tuple(slicev)] = varobj[:] 14094 14095 # Creation of a new file with the new variable dimensions 14096 newnc = NetCDFFile(ofile, 'w') 14097 14098 # Creation of dimensions 14099 for dn in dims: 14100 print 'adding dim:',dn,' ...' 14101 if onc.dimensions[dn].isunlimited(): 14102 newdim = newnc.createDimension(dn, None) 14103 else: 14104 newdim = newnc.createDimension(dn, len(onc.dimensions[dn])) 14105 14106 if unlimited == 'y': 14107 newdim = newnc.createDimension(dimname, None) 14108 else: 14109 newdim = newnc.createDimension(dimname, size) 14110 14111 newnc.sync() 14112 14113 # Adding variables 14114 for vn in varns: 14115 print 'adding variable:',vn,'...' 14116 vno = onc.variables[vn] 14117 if vn == varn: 14118 vdim = tuple(newdimns) 14119 else: 14120 vdim = vno.dimensions 14121 vtype = vno.dtype 14122 varatts = vno.ncattrs() 14123 14124 if searchInlist(varatts, '_FillValue'): 14125 fillv = vno.getncattr('_FillValue') 14126 newvar = newnc.createVariable(vn, vtype, vdim, fill_value=fillv) 14127 else: 14128 newvar = newnc.createVariable(vn, vtype, vdim) 14129 14130 for nattr in vno.ncattrs(): 14131 if not nattr == '_FillValue': 14132 nattrv = vno.getncattr(nattr) 14133 newattr = newvar.setncattr(nattr, nattrv) 14134 14135 if vn == varn: 14136 newvar[:] = newvarv[:] 14137 else: 14138 newvar[:] = vno[:] 14139 14140 14141 onc.close() 14142 newnc.sync() 14143 newnc.close() 14144 14145 # Adding attributes 14146 fgaddattr(ncfile, ofile) 14147 sub.call(['mv',ofile,ncfile]) 14148 14149 return 14150 14151 #increaseDimvar('shan:29:1:y', 'test.nc', 'var') 14009 14152 14010 14153 """operation to make:
Note: See TracChangeset
for help on using the changeset viewer.