-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyaml_xarray_dumpers.py
63 lines (48 loc) · 2.12 KB
/
yaml_xarray_dumpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""Class for dumping a LinkML model to YAML with paths to NumPy files."""
from pathlib import Path
from typing import List, Union
import numpy as np
import xarray as xr
from .yaml_array_file_dumper import YamlArrayFileDumper
class YamlXarrayNetCDFDumper(YamlArrayFileDumper):
"""Dumper class for LinkML models to YAML with paths to .nc file.
Each array is written to a netcdf dataset at path "/data" in a new .nc file.
"""
FILE_SUFFIX = ".nc" # used in parent class
FORMAT = "netcdf"
@classmethod
def write_array(
cls, array: Union[List, np.ndarray], output_file_path_no_suffix: Union[str, Path]
):
"""Write an array to a NumPy file."""
# TODO do not assume that there is only one by this name
# add suffix to the file name
if isinstance(output_file_path_no_suffix, str):
output_file_path_no_suffix = Path(output_file_path_no_suffix)
output_file_path = output_file_path_no_suffix.parent / (
output_file_path_no_suffix.name + cls.FILE_SUFFIX
)
data_array = xr.DataArray(data=array)
data_array.to_netcdf(output_file_path, engine="h5netcdf")
return output_file_path
class YamlXarrayZarrDumper(YamlArrayFileDumper):
"""Dumper class for LinkML models to YAML with paths to .zarr file.
Each array is written to a zarr dataset at path "/data" in a new .zarr file.
"""
FILE_SUFFIX = ".zarr" # used in parent class
FORMAT = "zarr"
@classmethod
def write_array(
cls, array: Union[List, np.ndarray], output_file_path_no_suffix: Union[str, Path]
):
"""Write an array to a NumPy file."""
# TODO do not assume that there is only one by this name
# add suffix to the file name
if isinstance(output_file_path_no_suffix, str):
output_file_path_no_suffix = Path(output_file_path_no_suffix)
output_file_path = output_file_path_no_suffix.parent / (
output_file_path_no_suffix.name + cls.FILE_SUFFIX
)
data_array = xr.DataArray(data=array)
data_array.to_zarr(output_file_path)
return output_file_path