def test_obs_obsmanager_project_model(): # Load manager manager = test_obs_obsmanager_init_surf() # Load model f = DS(NCFILE_MANGA0, 'mars', level='surf', logger_level='error') temp = f('temp') f.close() temp.id += '_surf' # Interpolate model otemp = manager.project_model(temp) assert_allclose(otemp[1][0], [12.91558515, 10.58179214]) return manager
def test_obs_ncobsplatform_surf(): # Load and stack obs obs = NcObsPlatform(NCFILE_OBS_SURF, logger=LOGGER, lat=(45, 47.8), norms=[0.2, 0.1]) stacked = obs.stacked_data.copy() assert obs.lons.shape == (3, ) assert obs.ns == 6 assert obs.ndim == 1 assert_allclose(obs.means, [0, 0], atol=1e-7) assert obs.depths == 'surf' assert_allclose(stacked, [2.5, 1.5, 4., 1., 1.5, 4.]) # Named norms notnormed = obs.set_named_norms(temp=0.1) assert obs.norms == [0.1, 0.1] assert_allclose(obs.stacked_data[:3], 2 * stacked[:3]) assert_allclose(obs.stacked_data[3:], stacked[3:]) obs.set_named_norms(temp=0.2) # Interpolate model f = DS(NCFILE_MANGA0, 'mars', level=obs.depths, logger_level='error') temp = f('temp') sal = f('sal') f.close() temp.id += '_surf' sal.id += '_surf' otemp = obs.project_model(temp) osal = obs.project_model(sal) otem_true = [12.97311556, 12.91558515, 10.58179214] assert_allclose(otemp[0], otem_true) # Stack model otemp[:] -= 11.5 osal[:] -= 35.5 stacked_data = obs.restack([otemp, osal]) assert stacked_data.shape == (6, 15) assert_allclose(stacked_data[:3, 0] * obs.norms[0] + 11.5, otem_true) return obs
break else: args.var = args.var[:3] # Reading parameters kwread = eval('dict(%s)' % args.select) kwread['squeeze'] = 1 for ss in 'lon', 'lat', 'dep', 'time': if getattr(args, ss) is not None: kwread[ss] = get_sel(getattr(args, ss)) # Read variable vv = [f(vname, **kwread) for vname in args.var] if args.dvar: depth = f(args.dvar, **kwread) f.close() if args.verbose: for v in vv: v.info() order = vv[0].getOrder() ndim = vv[0].ndim # Reduce rank when needed if ndim > 2: for i in xrange(ndim - 2): if order[i] == 'z': islice = -1 if isdepthup(var[0].getAxis(0)) else 0 else: islice = 0 vv = [v[islice] for v in vv] order = vv[0].getOrder()
break else: args.var = args.var[:3] # Reading parameters kwread = eval('dict(%s)'%args.select) kwread['squeeze'] = 1 for ss in 'lon', 'lat', 'dep', 'time': if getattr(args, ss) is not None: kwread[ss] = get_sel(getattr(args, ss)) # Read variable vv = [f(vname, **kwread) for vname in args.var] if args.dvar: depth = f(args.dvar, **kwread) f.close() if args.verbose: for v in vv: v.info() order = vv[0].getOrder() ndim = vv[0].ndim # Reduce rank when needed if ndim>2: for i in xrange(ndim-2): if order[i]=='z': islice = -1 if isdepthup(var[0].getAxis(0)) else 0 else: islice = 0 vv = [v[islice] for v in vv] order = vv[0].getOrder()
class NcReader(object): """A generic interface to open and read a netcdf file Examples -------- >>> f = NcReader(ncfile, 'mars3d') # VACUMM Mars3D reader >>> u = f('u', lon=(10, 12)) # generic var name >>> u = f('+sst', lon=(10, 12)) # netcdf var name >>> f.close() >>> f = NcReader(ncfile, 'cdms2') # direct cdms2 reader >>> u = f('u', lon=(10, 12)) # netcdf var name >>> f.close() >>> f = NcReader(ncfile, 'netcdf') # python netcdf4 reader >>> u = f('u', slice(0, 10)) # <=> f['u'][:10] >>> f.close() """ def __init__(self, ncfile, readertype='generic', **kwargs): self.ncfile = ncfile if not isinstance(readertype, basestring): self.f = readertype(ncfile, **kwargs) self.type = 'callable' elif readertype=='cdms2': self.f = cdms2.open(ncfile, **kwargs) self.type = readertype elif readertype=='netcdf4': import netcdf4 self.f = netcdf4.Dataset(ncfile, **kwargs) self.type = readertype else: self.f = DS(ncfile, readertype, **kwargs) self.type = 'dataset' def __call__(self, vname, *args, **kwargs): if self.type=='netcdf4': args = tuple(filter(lambda x: isinstance(x, slice), args)) return self.f[vname][args] else: return self.f(vname, *args, **kwargs) def __getitem__(self, vname): return self.f[vname] def get_variables(self): if self.type=='netcdf4' or self.type=='cdms2': return self.variables.keys() if self.type=='dataset': return self.f.dataset[0].variables.keys() raise SONATError('Method yet not implemented for read type "{}"'.format(self.type)) def close(self): self.f.close()