| 1 |
import os |
| 2 |
|
| 3 |
import fsspec |
| 4 |
import numpy as np |
| 5 |
from packaging.version import Version |
| 6 |
import pytest |
| 7 |
from kerchunk import netCDF3 |
| 8 |
|
| 9 |
from fsspec.implementations.asyn_wrapper import AsyncFileSystemWrapper |
| 10 |
from kerchunk.utils import refs_as_store |
| 11 |
|
| 12 |
xr = pytest.importorskip("xarray") |
| 13 |
|
| 14 |
|
| 15 |
has_xarray_2023_8_0 = Version(xr.__version__) >= Version("2023.8.0") |
| 16 |
|
| 17 |
|
| 18 |
arr = np.random.rand(1, 10, 10) |
| 19 |
data = xr.DataArray( |
| 20 |
data=arr.squeeze(), |
| 21 |
dims=["x", "y"], |
| 22 |
name="data", |
| 23 |
) |
| 24 |
bdata = xr.Dataset({"data": data}, attrs={"attr0": 3}).to_netcdf( |
| 25 |
format="NETCDF3_CLASSIC" |
| 26 |
) |
| 27 |
|
| 28 |
|
| 29 |
def test_one(m): |
| 30 |
m.pipe("data.nc3", bdata) |
| 31 |
h = netCDF3.netcdf_recording_file("memory://data.nc3") |
| 32 |
out = h.translate() |
| 33 |
|
| 34 |
print(out) |
| 35 |
|
| 36 |
store = refs_as_store(out) |
| 37 |
|
| 38 |
ds = xr.open_dataset( |
| 39 |
store, |
| 40 |
engine="zarr", |
| 41 |
backend_kwargs={ |
| 42 |
"consolidated": False, |
| 43 |
"zarr_format": 2, |
| 44 |
}, |
| 45 |
) |
| 46 |
assert (ds.data == data).all() |
| 47 |
|
| 48 |
|
| 49 |
@pytest.fixture() |
| 50 |
def unlimited_dataset(tmpdir): |
| 51 |
# https://unidata.github.io/netcdf4-python/#creatingopeningclosing-a-netcdf-file |
| 52 |
from netCDF4 import Dataset |
| 53 |
|
| 54 |
fn = os.path.join(tmpdir, "test.nc") |
| 55 |
rootgrp = Dataset(fn, "w", format="NETCDF3_CLASSIC") |
| 56 |
rootgrp.createDimension("time", None) |
| 57 |
rootgrp.createDimension("lat", 10) |
| 58 |
rootgrp.createDimension("lon", 5) |
| 59 |
rootgrp.createVariable("time", "f8", ("time",)) |
| 60 |
# reference time is an unbounded dimension that is a half byte long, so it |
| 61 |
# has padding to line up to take up exactly one byte. It is here to test that |
| 62 |
# kerchunk can handle the padding correctly and read following variables |
| 63 |
# correctly. |
| 64 |
rootgrp.createVariable("reference_time", "h", ("time",)) |
| 65 |
rootgrp.title = "testing" |
| 66 |
latitudes = rootgrp.createVariable("lat", "f4", ("lat",)) |
| 67 |
longitudes = rootgrp.createVariable("lon", "f4", ("lon",)) |
| 68 |
temp = rootgrp.createVariable( |
| 69 |
"temp", |
| 70 |
"f4", |
| 71 |
( |
| 72 |
"time", |
| 73 |
"lat", |
| 74 |
"lon", |
| 75 |
), |
| 76 |
) |
| 77 |
temp.units = "K" |
| 78 |
latitudes[:] = np.arange(-0.5, 0.5, 0.1) |
| 79 |
longitudes[:] = np.arange(0, 0.5, 0.1) |
| 80 |
for i in range(8): |
| 81 |
temp[i] = np.random.uniform(size=(1, 10, 5)) |
| 82 |
rootgrp.close() |
| 83 |
return fn |
| 84 |
|
| 85 |
|
| 86 |
def test_unlimited(unlimited_dataset): |
| 87 |
fn = unlimited_dataset |
| 88 |
expected = xr.open_dataset(fn, engine="scipy") |
| 89 |
h = netCDF3.NetCDF3ToZarr(fn) |
| 90 |
out = h.translate() |
| 91 |
|
| 92 |
fs = AsyncFileSystemWrapper(fsspec.filesystem("file")) |
| 93 |
store = refs_as_store(out, fs) |
| 94 |
|
| 95 |
ds = xr.open_zarr( |
| 96 |
store, |
| 97 |
zarr_format=2, |
| 98 |
consolidated=False, |
| 99 |
) |
| 100 |
assert ds.attrs["title"] == "testing" |
| 101 |
assert ds.temp.attrs["units"] == "K" |
| 102 |
assert (ds.lat.values == expected.lat.values).all() |
| 103 |
assert (ds.lon.values == expected.lon.values).all() |
| 104 |
assert (ds.temp.values == expected.temp.values).all() |
| 105 |
|
| 106 |
|
| 107 |
@pytest.fixture() |
| 108 |
def matching_coordinate_dimension_dataset(tmpdir): |
| 109 |
"""Create a dataset with a coordinate dimension that matches the name of a |
| 110 |
variable dimension.""" |
| 111 |
# https://unidata.github.io/netcdf4-python/#creatingopeningclosing-a-netcdf-file |
| 112 |
from netCDF4 import Dataset |
| 113 |
|
| 114 |
fn = os.path.join(tmpdir, "test.nc") |
| 115 |
rootgrp = Dataset(fn, "w", format="NETCDF3_64BIT") |
| 116 |
rootgrp.createDimension("node", 2) |
| 117 |
rootgrp.createDimension("sigma", 2) |
| 118 |
|
| 119 |
node = rootgrp.createVariable("node", "i4", ("node",)) |
| 120 |
sigma = rootgrp.createVariable("sigma", "f8", ("sigma", "node")) |
| 121 |
|
| 122 |
node[:] = [0, 1] |
| 123 |
for i in range(2): |
| 124 |
sigma[i] = np.random.uniform(size=(2,)) |
| 125 |
|
| 126 |
rootgrp.close() |
| 127 |
return fn |
| 128 |
|
| 129 |
|
| 130 |
@pytest.mark.skipif( |
| 131 |
not has_xarray_2023_8_0, reason="XArray 2023.08.0 is required for this behavior." |
| 132 |
) |
| 133 |
def test_matching_coordinate_dimension(matching_coordinate_dimension_dataset): |
| 134 |
fn = matching_coordinate_dimension_dataset |
| 135 |
expected = xr.open_dataset(fn, engine="scipy") |
| 136 |
h = netCDF3.NetCDF3ToZarr(fn) |
| 137 |
out = h.translate() |
| 138 |
ds = xr.open_dataset( |
| 139 |
"reference://", |
| 140 |
engine="zarr", |
| 141 |
backend_kwargs={ |
| 142 |
"consolidated": False, |
| 143 |
"storage_options": {"fo": out}, |
| 144 |
}, |
| 145 |
) |
| 146 |
assert (ds.node.values == expected.node.values).all() |
| 147 |
assert (ds.sigma.values == expected.sigma.values).all() |