def _getvar(self, varkey): datamap = self._datamap stdname, stdunit = stdprops.get(varkey, (varkey, 'unknown')) if varkey in datamap['surface'].dtype.names: vhead = datamap['surface'][varkey]['head'] v11 = vhead['VAR1'] EXP = vhead['EXP'] props = dict([(pk, vhead[pk][0]) for pk in vhead.dtype.names if pk not in ('YYMMDDHHFF', 'LEVEL')]) bytes = datamap['surface'][varkey]['data'] vdata = unpack(bytes, v11, EXP) # if varkey == 'MXLR': # CVAR, PREC, NEXP, VAR1, KSUM = pack2d(vdata[21], verbose = True) out = PseudoNetCDFVariable(self, varkey, 'f', ('time', 'y', 'x'), values=vdata, units=stdunit, standard_name=stdname, **props) elif varkey in self._layvarkeys: laykeys = datamap['layers'].dtype.names mylaykeys = [ laykey for laykey in laykeys if varkey in datamap['layers'][laykey].dtype.names ] bytes = np.array([ datamap['layers'][lk][varkey]['data'] for lk in mylaykeys ]).swapaxes(0, 1) vhead = np.array([ datamap['layers'][lk][varkey]['head'] for lk in mylaykeys ]).swapaxes(0, 1) v11 = vhead['VAR1'] EXP = vhead['EXP'] props = dict([(pk, vhead[pk][0, 0]) for pk in vhead.dtype.names if pk not in ('YYMMDDHHFF', 'LEVEL')]) props['LEVEL_START'] = vhead['LEVEL'][0, 0] props['LEVEL_END'] = vhead['LEVEL'][-1, -1] vdata = unpack(bytes, v11, EXP) out = PseudoNetCDFVariable(self, varkey, 'f', ('time', 'z', 'y', 'x'), values=vdata, units=stdunit, standard_name=stdname, **props) if self._cache: self.variables[varkey] = out return out
def gettime(ifile): from PseudoNetCDF import PseudoNetCDFVariable from datetime import datetime, timedelta if 'time' in ifile.variables: time = ifile.variables['time'] unit = time.units tunit, datestr = unit.split(' since ') sdate = datetime.strptime(datestr.replace( ' UTC', ''), '%Y-%m-%d %H:%M:%S') time = np.array([sdate + timedelta(**{tunit: t}) for t in time[:]]) unit = 'time' elif 'TFLAG' in ifile.variables: x = ifile.variables['TFLAG'][:].copy() for axi in range(1, x.ndim - 1): x = x[:, 0] time = np.array( [(datetime.strptime('%07d %06d' % (d, t), '%Y%j %H%M%S')) for d, t in x]) unit = 'time' elif 'time' in ifile.dimensions: time = np.arange(len(ifile.dimensions['time'])) unit = 'steps' elif 'TSTEP' in ifile.dimensions: time = np.arange(1, len(ifile.dimensions['TSTEP']) + 1) unit = 'steps' else: raise KeyError('No time found') return PseudoNetCDFVariable(None, 'time', 'f', ('time',), values=time[:], units=unit)
def __init__(self, path): self.dimensions = {} self.variables = {} fobj = open(path) jdate = int(fobj.readline().split()[0]) nlevels = int(fobj.readline().split()[0]) levels = [float(x) for x in fobj.readline().split()] nlats = int(fobj.readline().split()[0]) lats = [float(x) for x in fobj.readline().split()] nangles = int(fobj.readline().split()[0]) angles = [float(x) for x in fobj.readline().split()] nrxns = int(fobj.readline().split()[0]) scaling = dict() rxns = [] for ri in range(nrxns): tname, scale = fobj.readline().split(',') tname = tname.replace("'", "").strip() scaling[tname] = eval(scale.strip()) rxns.append(tname) lines = fobj.readlines() header = re.compile('\s+\d+\s+\d+\s+\d+\s*') data = [] headers = [] for line in lines: if header.match(line): headers.append(line) else: data.append(line) data = np.fromstring(''.join(data), sep = ' ').reshape(nlevels, nlats, nrxns, nangles) headers = np.fromstring(''.join(headers), sep = ' ').reshape(nlevels, nlats, nrxns, 3) assert((np.diff(headers[..., 0], axis = 0) == 1).all()) assert((np.diff(headers[..., 1], axis = 1) == 1).all()) assert((np.diff(headers[..., 2], axis = 2) == 1).all()) self.SDATE = jdate self.NLAYS = nlevels self.NLATS = nlats self.NANGLES = nangles self.NRXNS = nrxns self.createDimension('LAY', nlevels) self.variables['LAY'] = PseudoNetCDFVariable(self, 'LAY', 'f', ('LAY',), values = np.array(levels, dtype = 'f'), units = 'm') self.createDimension('LAT', nlats) self.variables['LAT'] = PseudoNetCDFVariable(self, 'LAT', 'f', ('LAT',), values = np.array(lats, dtype = 'f'), units = 'degrees') self.createDimension('ANGLE', nangles) self.variables['ANGLE'] = PseudoNetCDFVariable(self, 'ANGLE', 'f', ('ANGLE',), values = np.array(angles, dtype = 'f'), units = 'hours from noon') for rxni, rxn in enumerate(rxns): self.variables[rxn] = PseudoNetCDFVariable(self, rxn, 'f', ('LAY', 'LAT', 'ANGLE'), values = data[..., rxni, :].copy(), units = 's**-1')
def _getvar(self, k): datamap = self._datamap if k in self._sfckeys: vold = datamap['surface'][k]['head']['VAR1'].astype('f') scale = 2.0**(7-datamap['surface'][k]['head']['EXP'].astype('i')) invscale = 1. / scale data = (datamap['surface'][k]['data'].view('uint8') - 127.) * invscale[:, None, None] data[:, 0, 0] += vold data[:, :, 0] = cumsum(data[:, :, 0], axis = 1) vdata = cumsum(data, axis = 2) return PseudoNetCDFVariable(self, k, 'f', ('time', 'y', 'x'), values = vdata, units = 'unknown') elif k in self._laykeys: vold = datamap['upper']['layers'][k]['head']['VAR1'].astype('f') scale = 2.0**(7-datamap['upper']['layers'][k]['head']['EXP'].astype('i')) invscale = 1. / scale data = (datamap['upper']['layers'][k]['data'].view('uint8') - 127.) * invscale[:, :, None, None] data[:, :, 0, 0] += vold data[:, :, :, 0] = cumsum(data[:, :, :, 0], axis = 2) vdata = cumsum(data, axis = 3) return PseudoNetCDFVariable(self, k, 'f', ('time', 'z', 'y', 'x'), values = vdata, units = 'unknown')
def getmapdef(ifileo, add=True): gridname = _gdnames[ifileo.GDTYP] if add: mapdef = ifileo.createVariable(gridname, 'i', ()) else: from PseudoNetCDF import PseudoNetCDFVariable mapdef = PseudoNetCDFVariable(ifileo, gridname, 'i', ()) mapdef.grid_mapping_name = gridname if mapdef.grid_mapping_name == "latitude_longitude": pass elif mapdef.grid_mapping_name == "polar_stereographic": mapdef.latitude_of_projection_origin = ifileo.P_ALP * 90 mapdef.straight_vertical_longitude_from_pole = ifileo.P_GAM mapdef.standard_parallel = np.array([ifileo.P_BET], dtype='d') # mapdef.standard_parallel = np.array([ifileo.P_BET], dtype = 'f') else: mapdef.standard_parallel = np.array([ifileo.P_ALP, ifileo.P_BET], dtype='d') if mapdef.grid_mapping_name != "latitude_longitude": mapdef.latitude_of_projection_origin = ifileo.YCENT mapdef.longitude_of_central_meridian = ifileo.XCENT mapdef.false_northing = -ifileo.YORIG mapdef.false_easting = -ifileo.XORIG mapdef._CoordinateTransformType = "Projection" mapdef._CoordinateAxes = "x y" ioapi_sphere = get_ioapi_sphere() mapdef.semi_major_axis = getattr( ifileo, 'semi_major_axis', getattr(ifileo, 'earth_radius', ioapi_sphere[0])) mapdef.semi_minor_axis = getattr( ifileo, 'semi_minor_axis', getattr(ifileo, 'earth_radius', ioapi_sphere[1])) return mapdef
def const(*args, **kwds): vals = arange(24 * 4 * 5 * 6).reshape((24, 4, 5, 6)) dims = ('TSTEP', 'LAY', 'ROW', 'COL') return PseudoNetCDFVariable(tncf, args[0], 'f', dims, values=vals)