- Timestamp:
- Apr 4, 2018, 10:14:04 PM (7 years ago)
- Location:
- trunk/tools
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/generic_tools.py
r1887 r1892 12723 12723 'vdimn': 'time', 'axis': 'T', 'length': -1} 12724 12724 """ 12725 fname = 'CF DIMvalues'12725 fname = 'CFcorValues' 12726 12726 12727 12727 # Dictionary for each dimension … … 12748 12748 12749 12749 # names to search for 12750 lonn = ['lon', 'X', 'Longitude', 'longitude', 'x', 'west_east' ]12751 latn = ['lat', 'Y', 'Latitude', 'latitude', 'y', 'south_north' ]12750 lonn = ['lon', 'X', 'Longitude', 'longitude', 'x', 'west_east', 'rlon'] 12751 latn = ['lat', 'Y', 'Latitude', 'latitude', 'y', 'south_north', 'rlat'] 12752 12752 pressn = ['pres', 'press', 'P', 'Press', 'Pressure', 'pressure', 'p'] 12753 12753 heightn = ['height', 'Z', 'Height', 'z'] -
trunk/tools/nc_var_tools.py
r1891 r1892 75 75 # gaddattr: Add a global attribute to a netCDF. Removes previous attribute if it exist 76 76 # gaddattrk: Add a global attribute to a netCDF caring about the type. Removes previous attribute if it exist 77 # genericNCvariable_Dict: Class to fake a netCDF varibale using dictionary values for 78 # dimensions 77 79 # genericNCvariable: Class to fake a netCDF varibale 78 80 # get_1str_nc: Function to get 1 string value in a netCDF variable as a chain of 1char values … … 195 197 # Radius of the Earth (m) 196 198 EarthR = 6378000. 199 200 class genericNCvariable_Dict(object): 201 """ Class to fake a netCDF varibale using dictionary values for dimensions 202 dicdim: dictionary with name of the dimension as key and its length as valu 203 dimlist: list of dimensions to give the shape of the variable 204 name: name of the variable 205 stdname: standard_name of the variable 206 longname: longname of the variable 207 units: units of the variable 208 values: values for the variable 209 >>> variable = genericNCvariable_Dict({'lon':25, 'lat':30, 'time': 2}, ['time', 'lat', 'lon'], 'tas', 'air_temperature', 'air temperature', 'K', np.ones((2,30,25),dtype=np.float)) 210 """ 211 def __init__(self, dicdim, dimlist, name, stdname, longname, units, values): 212 from collections import OrderedDict 213 totshape = np.prod(dicdim.values()) 214 dv = [] 215 for idd in dimlist: dv.append(dicdim[idd]) 216 self.values = values[:] 217 self.dimensions = tuple(list(dicdim.keys())) 218 self.shape = tuple(dv) 219 self.name = name 220 self.standard_name = stdname 221 self.long_name = longname 222 self.units = units 223 224 def __getitem__(self, val): 225 return self.values[val] 226 #variable = genericNCvariable_Dict({'lon':25, 'lat':30, 'time': 2}, ['time', 'lat', 'lon'], 'tas', 'air_temperature', 'air temperature', 'K', np.ones((2,30,25), dtype=np.float)) 227 #print variable 228 #print variable.dimensions 229 #print variable.shape 230 #print variable.units 231 #print variable[:].shape 197 232 198 233 class genericNCvariable(object): … … 21063 21098 ncfile + ' _______' 21064 21099 rvarns = ronc.variables.keys() 21065 print 'Lluis varns:', varns21066 21100 for vn in varns: 21067 21101 rdimns = list(ronc.dimensions) … … 23287 23321 23288 23322 if not CFvars.has_key(cfvarn): 23289 print errormsg23323 print infmsg 23290 23324 print ' ' + fname + ": file '" + inCFvarfile + "' does not have CF " + \ 23291 23325 "variable: '" + cfvarn + "' !!" … … 23645 23679 tvals, urefvals = compute_WRFtime(timewrfv, refdate=refT, \ 23646 23680 tunitsval='minutes') 23647 oaxisv = tvals 23681 #oaxisv = tvals 23682 oaxisv = genericNCvariable_Dict({'time':len(tvals)}, ['time'], 'time', \ 23683 'time', 'Time', urefvals, tvals[:]) 23648 23684 23649 23685 # CF values of axis … … 23666 23702 if len(oaxisv.shape) == CFdimvalues['maxrank']: 23667 23703 axisv = oaxisv[:] 23668 if CFdimvalues['length'] != -1: CFdimvalues['length'] = oaxisv.shape[0] 23669 CFvardimvalues.append(dimn) 23704 vardimdimn = [] 23705 vardimdimL = [] 23706 for dn in oaxisv.dimensions: 23707 vardimdimn.append(dn) 23708 if CFdimvalues['length'] != -1: 23709 CFdimvalues['length'] = len(getdim_listonc(dimn, afNs, allonc)) 23710 CFvardimvalues = vardimdimn 23670 23711 elif len(oaxisv.shape) == CFdimvalues['maxrank'] + 1: 23671 23712 print warnmsg … … 23926 23967 varv = np.where(varv == fill_value, gen.fillValueF, varv) 23927 23968 else: 23928 print 'Lluis: ', newVdims23929 23969 newvar=onewnc.createVariable(cfvarn, 'f4', tuple(newVdims), \ 23930 23970 fill_value=gen.fillValueF) … … 23972 24012 # Re-setting time values to the center of the bounds 23973 24013 ovtime[:] = tvals[:] 23974 elif vtb[0:1 0] == 'Ctime_bnds':24014 elif vtb[0:11] == 'CFtime_bnds': 23975 24015 vtbvals = vtb.split('|') 23976 24016 if len(vtbvals) != 4: … … 23992 24032 print infmsg 23993 24033 print ' ' + fname + ": creation of variable 'time_bnds' " + \ 23994 "from variable '" + tvtbvals[1] + "' using period '" + period +\24034 "from variable '" + vtbvals[1] + "' using period '" + period +\ 23995 24035 "' !!" 23996 odimvar = onc.variables[ tvtbvals[1]]24036 odimvar = onc.variables[vtbvals[1]] 23997 24037 timev = odimvar[:] 23998 24038 ttu = odimvar.getncattr('units') … … 24001 24041 'Y-m-dTH:M:SZ', 'YmdHMS') 24002 24042 tbvals, tvals, urefvals = compute_time_bnds(timev, ttu, period, \ 24003 kind WRFt='begperiod', refdate=refT, tunitsval='minutes')24043 kindt='begperiod', refdate=refT, tunitsval='minutes') 24004 24044 # Re-setting time values to the center of the bounds 24005 24045 ovtime[:] = tvals[:] … … 24272 24312 tfmt = tkind.split(',')[1] 24273 24313 tvals = [] 24314 utime = 'minutes since 1949-12-01 00:00:00' 24274 24315 mattvals = np.zeros((itvals.shape[0],6), dtype=int) 24275 24316 for it in range(itvals.shape[0]): 24276 24317 mattvals[it,:] = dt.datetime.string(''.join(itvals[it,:]),tfmt) 24277 24318 tvals[it] = gen.datetimeStr_conversion(mattvals[it,:].strftime("%Y%m%d"+ \ 24278 "%H%M%S"), 'YmdHMS', 'cfTime,minutes since 1949-12-01 00:00:00') 24319 "%H%M%S"), 'YmdHMS', 'cfTime,'+utime) 24320 24279 24321 else: 24280 24322 print errormsg
Note: See TracChangeset
for help on using the changeset viewer.