-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxstore.py
executable file
·149 lines (124 loc) · 5.6 KB
/
xstore.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import os
import uuid
import shutil
import zipfile
import xarray as xr
STORE_DIRECTORY = None
def _zip_zarr(zarr_DS, delete_DS=True):
"""
Zip a zarr DirectoryStore.
"""
filename = zarr_DS+os.path.extsep+"zip"
with zipfile.ZipFile(filename, "w", compression=zipfile.ZIP_STORED, allowZip64=True) as fh:
for root, _, filenames in os.walk(zarr_DS):
for each_filename in filenames:
each_filename = os.path.join(root, each_filename)
fh.write(
each_filename,
os.path.relpath(each_filename, zarr_DS))
if delete_DS:
shutil.rmtree(zarr_DS)
def _store(obj, file_name=None, clobber=False, file_format='zarr_DS'):
"""
Write an xarray object to disk and read it back
Parameters
----------
obj : xarray DataArray or Dataset
data to store and read back
file_name : str, optional
Name of file to write to disk. If not given, a random name will be generated
clobber : boolean, optional
If True, replace file if it already exists
file_format : string, optional
file format of the stored data. Options are 'zarr_DS' (zarr DirectoryStore), \
'zarr_ZS' (zarr ZipStore), 'netcdf'
Returns
-------
xarray DataArray or Dataset
Same data as input, but now read directly from disk
"""
if STORE_DIRECTORY == None:
raise ValueError('Please provide a store directory by setting the xstore.STORE_DIRECTORY attribute')
else:
os.makedirs(STORE_DIRECTORY, exist_ok=True)
for var in obj.variables:
obj[var].encoding = {}
obj = obj.unify_chunks()
if file_name is None:
file_name = uuid.uuid4().hex
if (file_format == 'zarr_DS') & (not file_name.endswith(os.path.extsep+'zarr')):
file_name = file_name+os.path.extsep+'zarr'
elif (file_format == 'zarr_ZS') & (not file_name.endswith(os.path.extsep+'zip')):
file_name = file_name+os.path.extsep+'zip'
elif (file_format == 'netcdf') & (not file_name.endswith(os.path.extsep+'nc')):
file_name = file_name+os.path.extsep+'nc'
store_file = os.path.join(STORE_DIRECTORY, file_name)
if file_format == 'zarr_DS':
if (not os.path.exists(store_file)) | clobber==True:
obj.to_zarr(store_file, mode='w', consolidated=True, compute=True)
return xr.open_zarr(store_file, consolidated=True)
elif file_format == 'zarr_ZS':
if (os.path.splitext(store_file)[-1] != os.path.extsep+'zip'):
tmp_file = store_file
store_file = store_file+os.path.extsep+'zip'
else:
tmp_file = store_file.strip(os.path.extsep+'zip')
if (not os.path.exists(store_file)) | clobber==True:
obj.to_zarr(tmp_file, mode='w', consolidated=True, compute=True)
_zip_zarr(tmp_file, delete_DS=True)
return xr.open_zarr(store_file, consolidated=True)
elif file_format == 'netcdf':
if (not os.path.exists(store_file)) | clobber==True:
obj.to_netcdf(store_file, mode='w')
return xr.open_dataset(store_file, chunks={})
else:
raise ValueError("Unrecognised file_format. Options are 'zarr_DS', 'zarr_ZS' and 'netcdf'")
@xr.register_dataarray_accessor("xst")
class XCacheAccessor:
def __init__(self, xarray_obj):
self._obj = xarray_obj
def store(self, file_name=None, clobber=False, file_format='zarr_DS'):
"""
Write an xarray object to disk and read it back
Parameters
----------
obj : xarray DataArray or Dataset
data to store and read back
file_name : str, optional
Name of file to write to disk. If not given, a random name will be generated
clobber : boolean, optional
If True, replace file if it already exists
file_format : string, optional
file format of the stored data. Options are 'zarr_DS' (zarr DirectoryStore), \
'zarr_ZS' (zarr ZipStore), 'netcdf'
Returns
-------
xarray DataArray or Dataset
Same data as input, but now read directly from disk
"""
name = self._obj.name if self._obj.name else '_stored_variable'
return _store(self._obj.to_dataset(name=name), file_name=file_name, clobber=clobber, file_format=file_format)[name]
@xr.register_dataset_accessor("xst")
class XCacheAccessor:
def __init__(self, xarray_obj):
self._obj = xarray_obj
def store(self, file_name=None, clobber=False, file_format='zarr_DS'):
"""
Write an xarray object to disk and read it back
Parameters
----------
obj : xarray DataArray or Dataset
data to store and read back
file_name : str, optional
Name of file to write to disk. If not given, a random name will be generated
clobber : boolean, optional
If True, replace file if it already exists
file_format : string, optional
file format of the stored data. Options are 'zarr_DS' (zarr DirectoryStore), \
'zarr_ZS' (zarr ZipStore), 'netcdf'
Returns
-------
xarray DataArray or Dataset
Same data as input, but now read directly from disk
"""
return _store(self._obj, file_name=file_name, clobber=clobber, file_format=file_format)