- Timestamp:
- Jun 19, 2016, 1:02:32 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var_tools.py
r891 r893 149 149 150 150 self.values = np.arange((totshape), dtype=np.float).reshape(dt,dz,dy,dx) 151 self.dimensions = OrderedDict(sorted(dimdict.items(), key=lambda t: t[0])) 151 # Here for a fake netCDF file 152 # self.dimensions = OrderedDict(sorted(dimdict.items(), key=lambda t: t[0])) 153 self.dimensions = (dnt, dnz, dny, dnx) 152 154 self.shape = (dt, dz, dy, dx) 153 155 self.standard_name = 'general_var' … … 8990 8992 dimvn = values.split(',')[3].split(':') 8991 8993 8994 # Operations with which when the variable dimensions will be ajusted to the file 8995 # the zero value along the operated dimensions will be taken 8996 zerodimops = ['max', 'mean', 'mean2', 'min', 'sum'] 8997 8992 8998 dimsoperS = gen.numVector_String(dimsoper,', ') 8993 8999 objnc = NetCDFFile(ncfile, 'r') … … 9022 9028 newNdim = 0 9023 9029 newNvar = 0 9024 for vn in varns :9030 for vn in varns + dimvn: 9025 9031 print "'" + vn + "' ... .. ." 9026 9032 if not objnc.variables.has_key(vn): … … 9034 9040 tocompute = False 9035 9041 for vd in ov.dimensions: 9036 print 'Lluis vd:', vd, gen.searchInlist(dimsoper, vd)9037 9042 if gen.searchInlist(dimsoper, vd): tocompute = True 9038 9039 print 'Lluis tocompute:', tocompute, ov.dimensions, dimsoper 9040 9041 if tocompute: 9042 varslice, varsliceddims = SliceVarDict(ov,dimslice) 9043 varvals0 = ov[tuple(varslice)] 9044 varvals = np.squeeze(varvals0) 9045 9046 finalvarvals, finaldims = operdim(varvals, varsliceddims, dimsoper, operkind) 9047 9048 # Adding dimensions 9049 idn = 0 9050 for dn in finaldims: 9051 print 'Lluis: dn unlimited;', dn, objnc.dimensions[dn].isunlimited(), idn 9052 if newNdim == 0: 9053 if not objnc.dimensions[dn].isunlimited(): 9054 objnewnc.createDimension(dn, finalvarvals.shape[idn]) 9055 else: 9056 objnewnc.createDimension(dn, None) 9057 newNdim = newNdim + 1 9058 else: 9059 if not gen.searchInlist(objnewnc.dimensions, dn): 9060 if not objnc.dimensions[dn].isunlimited(): 9061 objnewnc.createDimension(dn, finalvarvals.shape[idn]) 9062 else: 9063 objnewnc.createDimension(dn, None) 9064 newNdim = newNdim + 1 9065 idn = idn + 1 9066 9067 # Adding variable 9068 varinf = variable_inf(ov) 9069 if varinf.FillValue is not None: 9070 newvar = objnewnc.createVariable(vn+operkind, nctype(varinf.dtype), \ 9071 tuple(finaldims), fillValue=varinf.FillValue) 9072 else: 9073 newvar = objnewnc.createVariable(vn+operkind, nctype(varinf.dtype), \ 9074 tuple(finaldims)) 9075 newNvar = newNvar + 1 9076 newvar[:] = finalvarvals[:] 9077 9078 # new variable 9079 oldvarattr = ov.ncattrs() 9080 9081 varname = gen.variables_values(vn)[0] 9082 if gen.searchInlist(oldvarattr, 'standard_name'): 9083 stdname = ov.getncattr('standard_name') 9084 else: 9085 stdname = gen.variables_values(vn)[1] 9086 9087 if gen.searchInlist(oldvarattr, 'long_name'): 9088 lname = ov.getncattr('long_name') 9089 else: 9090 lname = gen.variables_values(vn)[4].replace('|',' ') 9091 9092 if gen.searchInlist(oldvarattr, 'units'): 9093 uname = ov.getncattr('units') 9094 else: 9095 uname = gen.variables_values(vn)[5] 9096 9097 newattr = basicvardef(newvar, stdname + operkind, lname + ' '+ operkind +\ 9098 ' along ' + dimsoperS, uname) 9099 9100 for idim in ov.dimensions: 9101 for dv in dimslice.keys(): 9102 if dv == idim: 9103 if type(dimslice[dv]) == type([2]): 9104 Sdv = gen.numVector_Sring(dimslice[dv],', ') 9105 elif type(dimslice[dv]) == type(int(2)): 9106 if dimslice[dv] == -1: 9107 Sdv = str(len(objnc.dimensions[idim])) + ', 1' 9108 else: 9109 Sdv = str(dimslice[dv]) 9110 else: 9111 Sdv = str(len(objnc.dimensions[idim])) 9112 9113 if idim == ov.dimensions[0]: 9114 origdimsS = idim + '(' + Sdv + ')' 9115 else: 9116 origdimsS = origdimsS + ' ' + idim + '(' + Sdv + ')' 9117 9118 newattr = set_attribute(newvar, 'orig_dimensions', origdimsS) 9119 objnewnc.sync() 9120 9121 # variable dimension 9122 for vn in dimvn: 9123 print "'" + vn + "' ... .. ." 9124 if not objnc.variables.has_key(vn): 9125 print errormsg 9126 print ' ' + fname + ': netCDF file "' + ncfile + \ 9127 '" does not have variable "' + vn + '" !!' 9128 quit(-1) 9129 9130 # Do we have to compute this variable? 9131 ov = objnc.variables[vn] 9132 tocompute = False 9133 for vd in ov.dimensions: 9134 if gen.searchInlist(dimsoper, vd): tocompute = True 9135 9043 9136 9044 if tocompute: 9137 9045 varslice, varsliceddims = SliceVarDict(ov,dimslice) … … 9162 9070 varinf = variable_inf(ov) 9163 9071 if varinf.FillValue is not None: 9164 newvar = objnewnc.createVariable(vn , nctype(varinf.dtype),\9072 newvar = objnewnc.createVariable(vn+operkind, nctype(varinf.dtype), \ 9165 9073 tuple(finaldims), fillValue=varinf.FillValue) 9166 9074 else: 9167 newvar = objnewnc.createVariable(vn , nctype(varinf.dtype),\9075 newvar = objnewnc.createVariable(vn+operkind, nctype(varinf.dtype), \ 9168 9076 tuple(finaldims)) 9169 9077 newNvar = newNvar + 1 … … 9192 9100 ' along ' + dimsoperS, uname) 9193 9101 9102 for idim in ov.dimensions: 9103 for dv in dimslice.keys(): 9104 if dv == idim: 9105 if type(dimslice[dv]) == type([2]): 9106 Sdv = gen.numVector_Sring(dimslice[dv],', ') + ', 1' 9107 elif type(dimslice[dv]) == type(int(2)): 9108 if dimslice[dv] == -1: 9109 Sdv = str(len(objnc.dimensions[idim])) 9110 else: 9111 Sdv = str(dimslice[dv]) 9112 else: 9113 Sdv = str(len(objnc.dimensions[idim])) 9114 9115 if idim == ov.dimensions[0]: 9116 origdimsS = idim + '(' + Sdv + ')' 9117 else: 9118 origdimsS = origdimsS + ' ' + idim + '(' + Sdv + ')' 9119 9120 newattr = set_attribute(newvar, 'orig_dimensions', origdimsS) 9121 objnewnc.sync() 9122 9123 # Including operation dimensions on the new slice for the variables dimension 9124 # but, which value along the operated dimension should be taken? 9125 sliceaddvars = dimslice 9126 if gen.searchInlist(zerodimops, operkind): 9127 dimsliceval = 0 9128 9129 for cdn in dimsoper: 9130 sliceaddvars[cdn] = dimsliceval 9131 9132 # variable dimension 9133 for vn in dimvn: 9134 print "'" + vn + "' ... .. ." 9135 if not objnc.variables.has_key(vn): 9136 print errormsg 9137 print ' ' + fname + ': netCDF file "' + ncfile + \ 9138 '" does not have variable "' + vn + '" !!' 9139 quit(-1) 9140 9141 # Do we have to add this dimension-variable? (only that ones with same 9142 # dimensions as desired variable) 9143 ov = objnc.variables[vn] 9144 toadd = False 9145 for vd in ov.dimensions: 9146 if gen.searchInlist(dimsoper, vd): toadd = True 9147 9148 if toadd: 9149 varinf = variable_inf(ov) 9150 varslice, varsliceddims = SliceVarDict(ov,sliceaddvars) 9151 varvals0 = ov[tuple(varslice)] 9152 varvals = np.squeeze(varvals0) 9153 9154 print 'Lluis: variables to add:', varsliceddims 9155 9156 for idv in range(len(varvals.shape)): 9157 if not gen.searchInlist(objnewnc.dimensions, varsliceddims[idv]): 9158 newdim = objnewnc.createDimension(varsliceddims[idv], \ 9159 varvals.shape[idv]) 9160 9161 newvar = objnewnc.createVariable(vn, nctype(varinf.dtype), \ 9162 tuple(varsliceddims)) 9163 newvar[:] = varvals 9164 9165 for attrn in varinf.attributes: 9166 attrv = ov.getncattr(attrn) 9167 newattr = set_attribute(newvar, attrn, attrv) 9168 # LLUIS 9194 9169 objnewnc.sync() 9195 9170 … … 15665 15640 self.vardxn = 'XLONG' 15666 15641 self.vardyn = 'XLAT' 15667 self.vardzn = ' DN'15642 self.vardzn = 'ZNU' 15668 15643 self.vardtn = 'Times' 15669 15644 if ncfile is not None and ncfile[0:2] == 'geo':
Note: See TracChangeset
for help on using the changeset viewer.