def test_generate_coord(): s = CoordinateSchema( name="zaxis_1", dims=["zaxis_1"], value=[1.0], ) expected = xr.DataArray(name="zaxis_1", dims=["zaxis_1"], data=[1.0]) xr.testing.assert_equal(generate(s), expected)
def test_generate_and_pickle_integration(): x = CoordinateSchema("x", ["x"], np.array([1, 2, 3])) a = VariableSchema( "a", ["x"], ChunkedArray(shape=[3], chunks=[1], dtype=np.dtype("float32"))) ranges = {"a": Range(0, 10)} ds = DatasetSchema(coords={x.name: x}, variables={a.name: a}) d = generate(ds, ranges) pickle.dumps(d)
def data_path(tmpdir): schema_path = pathlib.Path(__file__).parent / "data.zarr.json" with open(schema_path) as f: schema = synth.load(f) ranges = { "pressure_thickness_of_atmospheric_layer": synth.Range(0.99, 1.01) } ds = synth.generate(schema, ranges) ds.to_zarr(str(tmpdir), consolidated=True) return str(tmpdir)
def test_cftime_generate(): julian_time_attrs = { "calendar": "julian", "calendar_type": "JULIAN", "cartesian_axis": "T", "long_name": "time", "units": "seconds since 2016-08-01T00:15:00.000026", } store = {} group = zarr.open_group(store, mode="w") arr = group.zeros("time", shape=[1], dtype=np.float64) arr.attrs.update(julian_time_attrs) arr.attrs["_ARRAY_DIMENSIONS"] = ["time"] schema = read_schema_from_zarr(group, coords=["time"]) ds = generate(schema, {}) assert dict(ds.time.attrs) == dict(julian_time_attrs)
def test_generate_regression(regtest): """ Note: regtest fixture provided by pytest-regtest plugin: https://pypi.org/project/pytest-regtest/ """ x = CoordinateSchema("x", ["x"], np.array([1, 2, 3])) a = VariableSchema( "a", ["x"], ChunkedArray(shape=[3], chunks=[1], dtype=np.dtype("float32")), ) ranges = {"a": Range(0, 10)} ds = DatasetSchema(coords={x.name: x}, variables={a.name: a}) d = generate(ds, ranges) arr = d.a.values print(arr, file=regtest)
def _grid_spec(datadir, nx): scaling_factor = 384 / nx ranges = { "dx": synth.Range(20000 * scaling_factor, 28800 * scaling_factor), "dy": synth.Range(20000 * scaling_factor, 28800 * scaling_factor), "area": synth.Range(3.6205933e08 * scaling_factor**2, 8.3428736e08 * scaling_factor**2), } path = str(datadir.join("grid_spec.json")) with open(path) as f: schema = synth.load(f) ds = synth.generate(schema, ranges) subset = ds.isel(grid_xt=slice(nx), grid_yt=slice(nx), grid_x=slice(nx + 1), grid_y=slice(nx + 1)) return subset
def __getitem__(self, key: str) -> xr.Dataset: ds = synth.generate(self._schema).drop("initial_time") return ds
def open_schema(path_relative_to_file: str) -> xr.Dataset: path = Path(__file__) abspath = path.parent / path_relative_to_file with open(abspath) as f: return synth.generate(synth.load(f), ranges)
def _generate_from_schema(schema: DatasetSchema): return {tile: synth.generate(schema, ranges) for tile in tiles}