def test_self_eof (self): var = self.var eof, eig, pc = EOF(var,num=1) netcdf.save("eof_test.nc", [eof,eig,pc]) std = (var - eof*eig*pc).stdev() rms = (var**2).mean() self.assertLess(std, rms*1E-3)
def test_io(): from pygeode.formats import netcdf as nc from pygeode.timeaxis import StandardTime from pygeode.axis import Pres from pygeode.dataset import Dataset import numpy as np tm = StandardTime(values=np.arange(365), units='days', startdate={'year':2001}) p = Pres(np.arange(100.)) v = (tm * p).rename('v') # Save the dataset, then reload it immediately before = Dataset([v]) nc.save('issue004_test.nc', before) after = nc.open('issue004_test.nc') # Compare all vars/axes/attributes for var in before: assert var.name in after, "Can't find var '%s'"%var.name var2 = getattr(after,var.name) assert var2.atts == var.atts, "mismatched metadata. Input: %s, Output %s"%(var.atts,var2.atts) for axis in before.axes: axis2 = [a for a in after.axes if a.name == axis.name] assert len(axis2) == 1, "can't find axis '%s'"%axis.name axis2 = axis2[0] # assert axis2.atts == axis.atts, "mismatched metadata. Input: %s, Output %s"%(axis.atts,axis2.atts) for attname in axis.atts.keys(): assert attname in axis2.atts, "attribute '%s' not found"%attname assert axis.atts[attname] == axis2.atts[attname], "attribute '%s' changed from '%s' to '%s'"%(attname, axis.atts[attname], axis2.atts[attname]) assert type(axis2) == type(axis), "mismatched axis types. Input: %s, Output %s"%(type(axis), type(axis2))
def test_self_eof(self): var = self.var eof, eig, pc = EOF(var, num=1) netcdf.save("eof_test.nc", [eof, eig, pc]) std = (var - eof * eig * pc).stdev() rms = (var**2).mean() self.assertLess(std, rms * 1E-3)
def test_io(): from pygeode.formats import netcdf as nc from pygeode.timeaxis import StandardTime from pygeode.axis import Pres from pygeode.dataset import Dataset import numpy as np tm = StandardTime(values=np.arange(365), units='days', startdate={'year':2001}) p = Pres(np.arange(100.)) v = (tm * p).rename('v') # Save the dataset, then reload it immediately before = Dataset([v]) nc.save('issue004_test.nc', before) after = nc.open('issue004_test.nc') # Compare all vars/axes/attributes for var in before: assert var.name in after, "Can't find var '%s'"%var.name var2 = getattr(after,var.name) assert var2.atts == var.atts, "mismatched metadata. Input: %s, Output %s"%(var.atts,var2.atts) for axis in before.axes: axis2 = [a for a in after.axes if a.name == axis.name] assert len(axis2) == 1, "can't find axis '%s'"%axis.name axis2 = axis2[0] # assert axis2.atts == axis.atts, "mismatched metadata. Input: %s, Output %s"%(axis.atts,axis2.atts) for attname in list(axis.atts.keys()): assert attname in axis2.atts, "attribute '%s' not found"%attname assert axis.atts[attname] == axis2.atts[attname], "attribute '%s' changed from '%s' to '%s'"%(attname, axis.atts[attname], axis2.atts[attname]) assert type(axis2) == type(axis), "mismatched axis types. Input: %s, Output %s"%(type(axis), type(axis2))
def test_opener(): from pygeode.formats import netcdf from pygeode.tutorial import t1 from pygeode.formats.multifile import openall netcdf.save("issue090.data", t1) my_opener = lambda filename: netcdf.open(filename) f = openall("issue090.d???", opener=my_opener)
def test_encode_decode(): from pygeode.formats import netcdf import numpy as np x = make_var() netcdf.save("issue068_test.nc", x) y = netcdf.open("issue068_test.nc").dummy type1 = type(x.station) type2 = type(y.station) assert type1 is type2, (type1, type2) assert x.station == y.station assert np.all(x.get() == y.get())
def test_issue025(): lat = Lat([80,70,60]) var = Var(axes=[lat], values=[1,2,3], name='2B') # Save the variable nc.save ("issue025_test.nc", var) # This may crash in some versions of the netcdf library. # Even if it doesn't crash, it's a good idea to enforce the legal # netcdf names f = nc.open("issue025_test.nc") assert len(f.vars) == 1 # Must not start with a digit (should have been filtered) assert not f.vars[0].name[0].isdigit()
def test_issue015(): from pygeode.formats import netcdf as nc from pygeode.axis import Lat, Lon, Pres # Create a simple variable lat = Lat([75,85,95]) lon = Lon([100, 110, 120, 130]) pres = Pres([1000,900,800,700]) x = (lat-80)**2 + lon - pres/2. x.name = "stuff" # Save as a netcdf file nc.save("issue015_test.nc", x) # Reload f = nc.open("issue015_test.nc") y = f.stuff.load()
def test_issue005(): from pygeode.timeaxis import ModelTime365 from pygeode.axis import TAxis import numpy as np from pygeode.var import Var from pygeode.formats import netcdf as nc from pygeode import timeutils # Make a time axis starting at year 0 startdate = dict(year=0,month=1,day=1) taxis = ModelTime365(values=10200, startdate=startdate, units='days') # Make some dummy variable np.random.seed(len(taxis)) values = np.random.randn(len(taxis)) var = Var(axes=[taxis], values=values, name='x') # Save it nc.save("issue005_test.nc", var) # Load it f = nc.open("issue005_test.nc") # Make sure we have a regular time axis # (no climatologies!) assert f.time.__class__ == ModelTime365 assert hasattr(f.time,'year') # Okay, now reload it, but override the axis coming in f = nc.open("issue005_test.nc", dimtypes=dict(time=TAxis(taxis.values))) # Make sure we dimtypes is still working properly assert f.x.axes[0].__class__ == TAxis # For good measure, test that climatologies are still produced taxis = timeutils.modify(taxis,exclude='year',uniquify=True) values = np.random.randn(len(taxis)) var = Var(axes=[taxis], values=values, name='c') nc.save("issue005_test.nc", var) f = nc.open("issue005_test.nc") assert not hasattr(f.time,'year')
def test_issue005(): from pygeode.timeaxis import ModelTime365 from pygeode.axis import TAxis import numpy as np from pygeode.var import Var from pygeode.formats import netcdf as nc from pygeode import timeutils # Make a time axis starting at year 0 startdate = dict(year=0, month=1, day=1) taxis = ModelTime365(values=10200, startdate=startdate, units='days') # Make some dummy variable np.random.seed(len(taxis)) values = np.random.randn(len(taxis)) var = Var(axes=[taxis], values=values, name='x') # Save it nc.save("issue005_test.nc", var) # Load it f = nc.open("issue005_test.nc") # Make sure we have a regular time axis # (no climatologies!) assert f.time.__class__ == ModelTime365 assert hasattr(f.time, 'year') # Okay, now reload it, but override the axis coming in f = nc.open("issue005_test.nc", dimtypes=dict(time=TAxis(taxis.values))) # Make sure we dimtypes is still working properly assert f.x.axes[0].__class__ == TAxis # For good measure, test that climatologies are still produced taxis = timeutils.modify(taxis, exclude='year', uniquify=True) values = np.random.randn(len(taxis)) var = Var(axes=[taxis], values=values, name='c') nc.save("issue005_test.nc", var) f = nc.open("issue005_test.nc") assert not hasattr(f.time, 'year')
# Issue 22 - plot attributes get overridden by NamedAxis defaults # https://github.com/pygeode/pygeode/issues/22 # Make a sample file with a non-annotated pressure axis from pygeode.axis import NamedAxis, Pres from pygeode.formats import netcdf as nc lat = NamedAxis(values=[-80,-70,-60,-50], name='lat') p1 = NamedAxis(values=[1000.,900.,800.], name='p1') x = lat * p1 x.name = 'x' nc.save("issue022_test.nc", x) # Load it back in d = nc.open("issue022_test.nc", dimtypes={'p1':Pres}) # plotatts should be from the new (dimtypes) axis, not copied from the old one? # (also implicitly asserts that other metadata (such as the name) are still # copied from the old axis) assert d.p1.plotatts == Pres.plotatts
import pygeode as pyg import numpy as np import pylab as pyl from pygeode.formats import netcdf as nc lat = pyg.gausslat(32) lon = pyg.Lon(np.arange(0, 360, 360 / 64.)) ln_grid, lt_grid = np.meshgrid(lon.values, lat.values) T_values = 260. + 40 * np.exp(-(lt_grid / 45.)**2) + 0.05 * lt_grid * np.sin( 3 * ln_grid * np.pi / 180.) T_c = 260. + 40 * np.exp(-(lt_grid / 45.)**2) T_wave = 0.05 * lt_grid * np.sin(3 * ln_grid * np.pi / 180.) T = pyg.Var((lat, lon), name='Temp', values=T_c + T_wave, atts={'units': 'K'}) d = pyg.Dataset( [T], atts={'history': 'Synthetic Temperature data generated by pygeode'}) nc.save('t_sample.nc', d)
def test_self_svd(self): var1 = self.var1 eof1, pc1, eof2, pc2 = SVD(var1, var1, num=1, subspace=1) netcdf.save("svd_test.nc", [eof1, pc1, eof2, pc2]) self.assertTrue(np.allclose(eof1.get(), eof2.get())) self.assertTrue(np.allclose(pc1.get(), pc2.get()))
# Issue 22 - plot attributes get overridden by NamedAxis defaults # https://github.com/pygeode/pygeode/issues/22 # Make a sample file with a non-annotated pressure axis from pygeode.axis import NamedAxis, Pres from pygeode.formats import netcdf as nc lat = NamedAxis(values=[-80, -70, -60, -50], name='lat') p1 = NamedAxis(values=[1000., 900., 800.], name='p1') x = lat * p1 x.name = 'x' nc.save("issue022_test.nc", x) # Load it back in d = nc.open("issue022_test.nc", dimtypes={'p1': Pres}) # plotatts should be from the new (dimtypes) axis, not copied from the old one? # (also implicitly asserts that other metadata (such as the name) are still # copied from the old axis) assert d.p1.plotatts == Pres.plotatts
def test_self_svd (self): var1 = self.var1 eof1, pc1, eof2, pc2 = SVD(var1,var1,num=1,subspace=1) netcdf.save("svd_test.nc", [eof1,pc1,eof2,pc2]) self.assertTrue(np.allclose(eof1.get(),eof2.get())) self.assertTrue(np.allclose(pc1.get(),pc2.get()))