def test_create_gcm_dataset(test_dataset): ds = test_dataset gcm = GCMDataset(ds) # should fail if any of the variables is missing for v in ds: with pytest.raises(KeyError): gcm = GCMDataset(ds.drop(v))
def test_vertical_diff(gfdl_datadir): ds = xr.open_dataset(os.path.join(gfdl_datadir, 'test_gfdl.nc'), decode_times=False) dom = GCMDataset(ds) # fake a constant lapse rate from surface temp upwards lapse_rate = 10.0 ds['const_lapse'] = (ds.temp.sel(pfull=ds.pfull.max()) - lapse_rate*(ds.pfull)) dTdz = dom.diff(ds.const_lapse, 'z') assert np.allclose(ds.const_lapse.diff('pfull') / ds.pfull.diff('pfull'), -lapse_rate)
def test_vertical_integral(test_dataset): ds = test_dataset H = ds.attrs['H'] dz = ds.attrs['dz'] f = np.sin(np.pi * ds.Z.values / H) ds['f'] = (('Z'), f) ds['fint'] = (f*dz).sum() ds['favg'] = ds['fint'] / H gcm = GCMDataset(ds) gcm_fint = gcm.integrate_z(ds.f) assert gcm_fint.equals(ds.fint), (gcm_fint, ds.fint) gcm_favg = gcm.integrate_z(ds.f, average=True) assert gcm_favg.equals(ds.favg), (gcm_favg, ds.favg)
def test_vertical_integral(test_dataset): ds = test_dataset H = ds.attrs["H"] dz = ds.attrs["dz"] f = np.sin(np.pi * ds.Z.values / H) ds["f"] = (("Z"), f) ds["fint"] = (f * dz).sum() ds["favg"] = ds["fint"] / H gcm = GCMDataset(ds) gcm_fint = gcm.integrate_z(ds.f) assert gcm_fint.equals(ds.fint), (gcm_fint, ds.fint) gcm_favg = gcm.integrate_z(ds.f, average=True) assert gcm_favg.equals(ds.favg), (gcm_favg, ds.favg)
def test_vertical_integral(test_dataset): ds = test_dataset H = ds.attrs['H'] dz = ds.attrs['dz'] f = np.sin(np.pi * ds.Z.values / H) ds['f'] = (('Z'), f) ds['fint'] = (f * dz).sum() ds['favg'] = ds['fint'] / H gcm = GCMDataset(ds) gcm_fint = gcm.integrate_z(ds.f) assert gcm_fint.equals(ds.fint), (gcm_fint, ds.fint) gcm_favg = gcm.integrate_z(ds.f, average=True) assert gcm_favg.equals(ds.favg), (gcm_favg, ds.favg)
def test_horizontal_derivatives(test_dataset): ds = test_dataset dx = ds.attrs["dx"] dy = ds.attrs["dy"] Lx = ds.attrs["Lx"] Ly = ds.attrs["Ly"] # perdiodic function of Xp1 f = np.sin(np.pi * ds.Xp1.values / Lx) ds["f"] = ("Xp1", f) ds["df"] = ("X", np.roll(f, -1) - f) ds["dfdx"] = ds.df / dx # periodic function of Yp1 g = np.cos(np.pi * ds.Yp1.values / Ly) ds["g"] = ("Yp1", g) ds["dg"] = ("Y", np.roll(g, -1) - g) ds["dgdy"] = ds.dg / dy gcm = GCMDataset(ds) gcm_df = gcm.diff_xp1_to_x(ds.f) assert gcm_df.equals(ds.df), (gcm_df, ds.df) gcm_dg = gcm.diff_yp1_to_y(ds.g) assert gcm_dg.equals(ds.dg), (gcm_dg, ds.dg)
def test_horizontal_derivatives(test_dataset): ds = test_dataset dx = ds.attrs['dx'] dy = ds.attrs['dy'] Lx = ds.attrs['Lx'] Ly = ds.attrs['Ly'] # perdiodic function of Xp1 f = np.sin(np.pi * ds.Xp1.values / Lx) ds['f'] = ('Xp1', f) ds['df'] = ('X', np.roll(f,-1) - f) ds['dfdx'] = ds.df/dx # periodic function of Yp1 g = np.cos(np.pi * ds.Yp1.values / Ly) ds['g'] = ('Yp1', g) ds['dg'] = ('Y', np.roll(g,-1) - g) ds['dgdy'] = ds.dg/dy gcm = GCMDataset(ds) gcm_df = gcm.diff_xp1_to_x(ds.f) assert gcm_df.equals(ds.df), (gcm_df, ds.df) gcm_dg = gcm.diff_yp1_to_y(ds.g) assert gcm_dg.equals(ds.dg), (gcm_dg, ds.dg)
def test_horizontal_derivatives(test_dataset): ds = test_dataset dx = ds.attrs['dx'] dy = ds.attrs['dy'] Lx = ds.attrs['Lx'] Ly = ds.attrs['Ly'] # perdiodic function of Xp1 f = np.sin(np.pi * ds.Xp1.values / Lx) ds['f'] = ('Xp1', f) ds['df'] = ('X', np.roll(f, -1) - f) ds['dfdx'] = ds.df / dx # periodic function of Yp1 g = np.cos(np.pi * ds.Yp1.values / Ly) ds['g'] = ('Yp1', g) ds['dg'] = ('Y', np.roll(g, -1) - g) ds['dgdy'] = ds.dg / dy gcm = GCMDataset(ds) gcm_df = gcm.diff_xp1_to_x(ds.f) assert gcm_df.equals(ds.df), (gcm_df, ds.df) gcm_dg = gcm.diff_yp1_to_y(ds.g) assert gcm_dg.equals(ds.dg), (gcm_dg, ds.dg)
def test_multiple_dim_coords(gfdl_datadir): ds = xr.open_dataset(os.path.join(gfdl_datadir, 'test_gfdl.nc'), decode_times=False) dom = GCMDataset(ds) ds['single_height'] = ds.pfull * 100.0 assert dom.get_primary_dim_coord_name('z', ds.single_height) == 'pfull' ds['double_height'] = (ds.pfull + ds.phalf) with pytest.raises(ValueError): dom.get_primary_dim_coord_name('z', ds.double_height) ds['no_height'] = ds.lat * 30.0 with pytest.raises(ValueError): dom.get_primary_dim_coord_name('z', ds.no_height)
def test_vertical_derivatives(test_dataset): ds = test_dataset H = ds.attrs["H"] dz = ds.attrs["dz"] # vertical function of z at cell interface f = np.sin(np.pi * ds.Zp1.values / H) ds["f"] = (("Zp1"), f) ds["fl"] = ("Zl", f[:-1]) # TODO: build in negative sign logic more carefully df = -np.diff(f) ds["df"] = ("Z", df) fill_value = 0.0 ds["dfl"] = ("Z", np.hstack([df[:-1], f[-2] - fill_value])) ds["dfdz"] = ds["df"] / dz ds["dfldz"] = ds["dfl"] / dz # vertical function at cell center g = np.sin(np.pi * ds.Z.values / H) ds["g"] = ("Z", g) dg = -np.diff(g) dsdg = DataArray(dg, {"Zp1": ds.Zp1[1:-1]}, "Zp1") dsdgdf = dsdg / dz gcm = GCMDataset(ds) gcm_df = gcm.diff_zp1_to_z(ds.f) assert gcm_df.equals(ds.df), (gcm_df, ds.df) gcm_dfdz = gcm.derivative_zp1_to_z(ds.f) assert gcm_dfdz.equals(ds.dfdz), (gcm_dfdz, ds.dfdz) gcm_dfl = gcm.diff_zl_to_z(ds.fl, fill_value) assert gcm_dfl.equals(ds.dfl), (gcm_dfl, ds.dfl) gcm_dfldz = gcm.derivative_zl_to_z(ds.fl, fill_value) assert gcm_dfldz.equals(ds.dfldz), (gcm_dfldz, ds.dfldz) gcm_dg = gcm.diff_z_to_zp1(ds.g) assert gcm_dg.equals(dsdg), (gcm_dg, dsdg) gcm_dgdf = gcm.derivative_z_to_zp1(ds.g) assert gcm_dgdf.equals(dsdgdf), (gcm_dgdf, dsdgdf)
def test_vertical_derivatives(test_dataset): ds = test_dataset H = ds.attrs['H'] dz = ds.attrs['dz'] # vertical function of z at cell interface f = np.sin(np.pi * ds.Zp1.values / H) ds['f'] = (('Zp1'), f) ds['fl'] = ('Zl', f[:-1]) # TODO: build in negative sign logic more carefully df = -np.diff(f) ds['df'] = ('Z', df) fill_value = 0. ds['dfl'] = ('Z', np.hstack([df[:-1], f[-2] - fill_value])) ds['dfdz'] = ds['df'] / dz ds['dfldz'] = ds['dfl'] / dz # vertical function at cell center g = np.sin(np.pi * ds.Z.values / H) ds['g'] = ('Z', g) dg = -np.diff(g) dsdg = DataArray(dg, {'Zp1': ds.Zp1[1:-1]}, 'Zp1') dsdgdf = dsdg / dz gcm = GCMDataset(ds) gcm_df = gcm.diff_zp1_to_z(ds.f) assert gcm_df.equals(ds.df), (gcm_df, ds.df) gcm_dfdz = gcm.derivative_zp1_to_z(ds.f) assert gcm_dfdz.equals(ds.dfdz), (gcm_dfdz, ds.dfdz) gcm_dfl = gcm.diff_zl_to_z(ds.fl, fill_value) assert gcm_dfl.equals(ds.dfl), (gcm_dfl, ds.dfl) gcm_dfldz = gcm.derivative_zl_to_z(ds.fl, fill_value) assert gcm_dfldz.equals(ds.dfldz), (gcm_dfldz, ds.dfldz) gcm_dg = gcm.diff_z_to_zp1(ds.g) assert gcm_dg.equals(dsdg), (gcm_dg, dsdg) gcm_dgdf = gcm.derivative_z_to_zp1(ds.g) assert gcm_dgdf.equals(dsdgdf), (gcm_dgdf, dsdgdf)
def test_vertical_derivatives(test_dataset): ds = test_dataset H = ds.attrs['H'] dz = ds.attrs['dz'] # vertical function of z at cell interface f = np.sin(np.pi * ds.Zp1.values / H) ds['f'] = (('Zp1'), f) ds['fl'] = ('Zl', f[:-1]) # TODO: build in negative sign logic more carefully df = -np.diff(f) ds['df'] = ('Z', df) fill_value = 0. ds['dfl'] = ('Z', np.hstack([df[:-1], f[-2]-fill_value])) ds['dfdz'] = ds['df'] / dz ds['dfldz'] = ds['dfl'] / dz # vertical function at cell center g = np.sin(np.pi * ds.Z.values / H) ds['g'] = ('Z', g) dg = -np.diff(g) dsdg = DataArray(dg, {'Zp1': ds.Zp1[1:-1]}, 'Zp1') dsdgdf = dsdg / dz gcm = GCMDataset(ds) gcm_df = gcm.diff_zp1_to_z(ds.f) assert gcm_df.equals(ds.df), (gcm_df, ds.df) gcm_dfdz = gcm.derivative_zp1_to_z(ds.f) assert gcm_dfdz.equals(ds.dfdz), (gcm_dfdz, ds.dfdz) gcm_dfl = gcm.diff_zl_to_z(ds.fl, fill_value) assert gcm_dfl.equals(ds.dfl), (gcm_dfl, ds.dfl) gcm_dfldz = gcm.derivative_zl_to_z(ds.fl, fill_value) assert gcm_dfldz.equals(ds.dfldz), (gcm_dfldz, ds.dfldz) gcm_dg = gcm.diff_z_to_zp1(ds.g) assert gcm_dg.equals(dsdg), (gcm_dg, dsdg) gcm_dgdf = gcm.derivative_z_to_zp1(ds.g) assert gcm_dgdf.equals(dsdgdf), (gcm_dgdf, dsdgdf)
def test_periodic_left(gfdl_datadir): ds = xr.open_dataset(os.path.join(gfdl_datadir, 'test_gfdl.nc'), decode_times=False) dom = GCMDataset(ds) ptemp = dom.make_periodic_left(ds.temp) assert len(ptemp.coords['lon']) == 129 assert np.all(ptemp.isel(lon=0) == ptemp.isel(lon=-1))
def test_gfdl_manifest(gfdl_datadir): ds = xr.open_dataset(os.path.join(gfdl_datadir, 'test_gfdl.nc'), decode_times=False) dom = GCMDataset(ds) assert dom.manifest == manifests.gfdl