- Timestamp:
- Jun 19, 2016, 1:51:04 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var_tools.py
r893 r895 9025 9025 objnewnc = NetCDFFile(ofile, 'w') 9026 9026 9027 # Dimensions which should appear at the final file (from the computed variables) 9028 dimfinalfile = [] 9029 9027 9030 print ' ' + fname + ": '" + operkind + "' with:",dimsoper,'_______' 9028 9031 newNdim = 0 9029 9032 newNvar = 0 9030 for vn in varns + dimvn:9033 for vn in varns: 9031 9034 print "'" + vn + "' ... .. ." 9032 9035 if not objnc.variables.has_key(vn): … … 9036 9039 quit(-1) 9037 9040 9038 # Do we have to compute this variable? 9041 # Do we have to compute this variable? Only to compute that variables with 9042 # coincidence on the dimensions to operate 9039 9043 ov = objnc.variables[vn] 9040 9044 tocompute = False … … 9043 9047 9044 9048 if tocompute: 9049 # Dimensions which need to appear at the final file: that ones not opered 9050 # from all opered variables 9051 for vd in ov.dimensions: 9052 if not gen.searchInlist(dimfinalfile, vd) and \ 9053 not gen.searchInlist(dimsoper, vd): dimfinalfile.append(vd) 9054 9045 9055 varslice, varsliceddims = SliceVarDict(ov,dimslice) 9046 9056 varvals0 = ov[tuple(varslice)] … … 9121 9131 objnewnc.sync() 9122 9132 9133 # operating variable dimension 9134 print ' ' + fname + ': Opering slice of variable diension _______' 9135 for vn in dimvn: 9136 print "' " + vn + "' ... .. ." 9137 if not objnc.variables.has_key(vn): 9138 print warnmsg 9139 print ' ' + fname + ': netCDF file "' + ncfile + \ 9140 '" does not have variable "' + vn + '" skipping it !!' 9141 continue 9142 9143 # Do we have to compute this variable? 9144 ov = objnc.variables[vn] 9145 tocompute = False 9146 for vd in ov.dimensions: 9147 if gen.searchInlist(dimfinalfile, vd): tocompute = True 9148 9149 print fname + 'Lluis:', vn , tocompute, vd, dimfinalfile 9150 if tocompute: 9151 varslice, varsliceddims = SliceVarDict(ov,dimslice) 9152 varvals0 = ov[tuple(varslice)] 9153 varvals = np.squeeze(varvals0) 9154 9155 finalvarvals, finaldims = operdim(varvals, varsliceddims, dimsoper, operkind) 9156 9157 # Adding dimensions 9158 idn = 0 9159 for dn in finaldims: 9160 if newNdim == 0: 9161 if not objnc.dimensions[dn].isunlimited(): 9162 objnewnc.createDimension(dn, finalvarvals.shape[idn]) 9163 else: 9164 objnewnc.createDimension(dn, None) 9165 newNdim = newNdim + 1 9166 else: 9167 if not gen.searchInlist(objnewnc.dimensions, dn): 9168 if not objnc.dimensions[dn].isunlimited(): 9169 objnewnc.createDimension(dn, finalvarvals.shape[idn]) 9170 else: 9171 objnewnc.createDimension(dn, None) 9172 newNdim = newNdim + 1 9173 idn = idn + 1 9174 9175 # Adding variable 9176 varinf = variable_inf(ov) 9177 if varinf.FillValue is not None: 9178 newvar = objnewnc.createVariable(vn+operkind, nctype(varinf.dtype), \ 9179 tuple(finaldims), fillValue=varinf.FillValue) 9180 else: 9181 newvar = objnewnc.createVariable(vn+operkind, nctype(varinf.dtype), \ 9182 tuple(finaldims)) 9183 newNvar = newNvar + 1 9184 newvar[:] = finalvarvals[:] 9185 9186 # new variable 9187 oldvarattr = ov.ncattrs() 9188 9189 varname = gen.variables_values(vn)[0] 9190 if gen.searchInlist(oldvarattr, 'standard_name'): 9191 stdname = ov.getncattr('standard_name') 9192 else: 9193 stdname = gen.variables_values(vn)[1] 9194 9195 if gen.searchInlist(oldvarattr, 'long_name'): 9196 lname = ov.getncattr('long_name') 9197 else: 9198 lname = gen.variables_values(vn)[4].replace('|',' ') 9199 9200 if gen.searchInlist(oldvarattr, 'units'): 9201 uname = ov.getncattr('units') 9202 else: 9203 uname = gen.variables_values(vn)[5] 9204 9205 newattr = basicvardef(newvar, stdname + operkind, lname + ' '+ operkind +\ 9206 ' along ' + dimsoperS, uname) 9207 9208 for idim in ov.dimensions: 9209 for dv in dimslice.keys(): 9210 if dv == idim: 9211 if type(dimslice[dv]) == type([2]): 9212 Sdv = gen.numVector_Sring(dimslice[dv],', ') + ', 1' 9213 elif type(dimslice[dv]) == type(int(2)): 9214 if dimslice[dv] == -1: 9215 Sdv = str(len(objnc.dimensions[idim])) 9216 else: 9217 Sdv = str(dimslice[dv]) 9218 else: 9219 Sdv = str(len(objnc.dimensions[idim])) 9220 9221 if idim == ov.dimensions[0]: 9222 origdimsS = idim + '(' + Sdv + ')' 9223 else: 9224 origdimsS = origdimsS + ' ' + idim + '(' + Sdv + ')' 9225 9226 newattr = set_attribute(newvar, 'orig_dimensions', origdimsS) 9227 objnewnc.sync() 9228 9123 9229 # Including operation dimensions on the new slice for the variables dimension 9124 9230 # but, which value along the operated dimension should be taken? … … 9130 9236 sliceaddvars[cdn] = dimsliceval 9131 9237 9132 # variable dimension 9238 # slicing variable dimension 9239 print ' ' + fname + ': Adding slice of variable diension _______' 9133 9240 for vn in dimvn: 9134 print "' " + vn + "' ... .. ."9241 print "' " + vn + "' ... .. ." 9135 9242 if not objnc.variables.has_key(vn): 9136 print errormsg9243 print warnmsg 9137 9244 print ' ' + fname + ': netCDF file "' + ncfile + \ 9138 '" does not have variable "' + vn + '" !!' 9139 quit(-1) 9245 '" does not have variable "' + vn + '" skippping it!!' 9140 9246 9141 9247 # Do we have to add this dimension-variable? (only that ones with same … … 9144 9250 toadd = False 9145 9251 for vd in ov.dimensions: 9146 if gen.searchInlist(dim soper, vd): toadd = True9252 if gen.searchInlist(dimfinalfile, vd): toadd = True 9147 9253 9148 9254 if toadd:
Note: See TracChangeset
for help on using the changeset viewer.