forked from serapred/netcdf-to-zarr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.py
executable file
·289 lines (199 loc) · 7.6 KB
/
convert.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import zarr
import re
from utils.encoder import json_encode
from netCDF4 import Dataset
from itertools import chain
from concurrent.futures import ProcessPoolExecutor
from concurrent.futures import ThreadPoolExecutor
# shared memory path
SHARED = '/dev/shm/'
"""
Convert NetCDF files to Zarr store
N.B. To use threads or processes a thread/process safe version
of hdf5 (underlying netcdf4) is required.
this is because even if only concurrent reads on different data are issued,
the HDF5 library modifies global data structures that are independent
of a particular HDF5 dataset or HDF5 file.
HDF5 relies on a semaphore around the library API calls in the thread-safe
version of the library to protect the data structure from corruption
by simultaneous manipulation from different threads.
"""
def netcdf_to_zarr(src, dst, axis=None, mode='serial', nested=False):
"""Summary
Args:
src (TYPE): Description
dst (TYPE): Description
axis (None, optional): Description
mode (str, optional): Description
nested (bool, optional): Description
"""
if isinstance(dst, str):
if nested:
local_store = zarr.NestedDirectoryStore(dst)
else:
local_store = zarr.DirectoryStore(dst)
else:
local_store = dst
root = zarr.group(store=local_store, overwrite=True)
for i, dname in enumerate(src):
# cycling over groups, the first one is the root.
for j, gname in enumerate(__get_groups(dname)):
if j == 0:
group = root
ds = ''
else:
group = __set_group(gname, root)
ds = dname
if i == 0:
__set_meta(ds + gname, group)
__set_vars(ds + gname, group, mode)
else:
__append_vars(gname, group, axis, mode)
zarr.convenience.consolidate_metadata(local_store, metadata_key='.zmetadata')
# Open netcdf files and groups with the same interface
def __nc_open(ds, *args, **kwargs):
base, ext, path = re.split(r'(\.nc|\.hdf5)', ds, maxsplit=1, flags=re.IGNORECASE)
filename = base + ext
if path == '':
return Dataset(filename, *args, **kwargs)
else:
return Dataset(filename, *args, **kwargs)[path]
# Return serielizable attributes as dicts
def __get_meta(dataset):
# JSON encode attributes so they can be serialized
return {key: json_encode(getattr(dataset, key)) for key in dataset.ncattrs()}
# Return chunking informations about the given variable
def __get_chunks(var):
if var.chunking() != 'contiguous' and var.chunking() is not None:
return tuple(var.chunking())
return None
# Returns an iterator with every group of the file (root included)
def __get_groups(ds):
# recursive version with generator
def walktree(top):
values = top.groups.values()
if values:
for v in values:
yield v.path
for value in values:
for children in walktree(value):
yield children
dataset = Dataset(ds)
grps = (g for g in walktree(dataset))
return chain((ds,), grps)
# Set file group
def __set_group(ds, store):
print('creating group: ' + ds)
return store.create_group(ds)
# Set file metadata
def __set_meta(ds, store):
print("Setting meta for: " + ds)
store.attrs.put(__get_meta(__nc_open(ds)))
# Set variable data, including dimensions and metadata
def __set_var(ds, store, name, syncro=None):
print("Setting variable " + name)
dataset = __nc_open(ds)
var = dataset.variables[name]
store.create_dataset(name,
data=var,
shape=var.shape,
chunks=(__get_chunks(var)),
dtype=var.dtype,
synchronizer=syncro
)
attrs = __get_meta(dataset)
attrs['_ARRAY_DIMENSIONS'] = list(var.dimensions)
store[name].attrs.put(attrs)
# Append data to existing variable
def __append_var(ds, store, name, dim, syncro=None):
print("Appending " + name + " from " + ds)
dataset = __nc_open(ds)
var = dataset.variables[name]
if dim in var.dimensions:
axis = store[name].attrs['_ARRAY_DIMENSIONS'].index(dim)
array = zarr.open_array(store=store[name],
mode='r+',
synchronizer=syncro
)
array.append(var, axis)
# setting executor
def __set_vars(ds, store, mode='serial'):
print("Setting variables for: " + ds)
dataset = __nc_open(ds)
if mode == 'serial':
for name in dataset.variables.keys():
__set_var(ds, store, name)
elif mode == 'processes':
with ProcessPoolExecutor(max_workers=8) as executor:
syncro = zarr.ProcessSynchronizer(SHARED + 'ntz.sync')
for name in dataset.variables.keys():
executor.submit(__set_var, ds, store, name, syncro)
elif mode == 'threads':
with ThreadPoolExecutor(max_workers=8) as executor:
syncro = zarr.ThreadSynchronizer()
for name in dataset.variables.keys():
executor.submit(__set_var, ds, store, name, syncro)
else:
raise ValueError('the mode %s is not valid.' % mode)
# appending executor
def __append_vars(ds, store, dim, mode='serial'):
print("Append vars")
dataset = __nc_open(ds)
store[dim].append(dataset[dim])
if mode == 'serial':
for name in dataset.variables.keys():
__append_var(ds, store, name, dim)
elif mode == 'processes':
with ProcessPoolExecutor(max_workers=8) as executor:
syncro = zarr.ProcessSynchronizer(SHARED + 'ntz.sync')
for name in dataset.variables.keys():
executor.submit(__append_var, ds, store, name, dim, syncro)
elif mode == 'threads':
with ThreadPoolExecutor(max_workers=8) as executor:
syncro = zarr.ThreadSynchronizer()
for name in dataset.variables.keys():
executor.submit(__append_var, ds, store, name, dim, syncro)
else:
raise ValueError('the mode %s is not valid.' % mode)
def __set_dim(ds, group, name):
print("Set dim")
dataset = Dataset(ds)
dim = dataset.dimensions[name]
group.create_dataset(name, \
data=np.arange(dim.size), \
shape=(dim.size,), \
chunks=(1<<16,) if dim.isunlimited() else (dim.size,), \
dtype=np.int32 \
)
# Set dimension attrs
group[name].attrs['_ARRAY_DIMENSIONS'] = [name]
# Set dimensions
def __set_dims(ds, group, mode):
dataset = __nc_open(ds)
if mode == 'serial':
for name in dataset.variables.keys():
__set_dim(ds, group, name)
elif mode == 'processes':
with ProcessPoolExecutor(max_workers=8) as executor:
syncro = zarr.ProcessSynchronizer(SHARED + 'ntz.sync')
for name in dataset.variables.keys():
executor.submit(__set_dim, ds, group, name, syncro)
elif mode == 'threads':
with ThreadPoolExecutor(max_workers=8) as executor:
syncro = zarr.ThreadSynchronizer()
for name in dataset.variables.keys():
executor.submit(__set_dim, ds, group, name, syncro)
else:
raise ValueError('the mode %s is not valid.' % mode)
# main
def main(args):
import utils.parser as p
args = p.configure(args)
netcdf_to_zarr(**vars(args))
if __name__ == '__main__':
'''
Usage:
python convert.py 'src1.nc src2.nc...srcN.nc' dst.zarr [-options]
'''
import sys
main(sys.argv[1:])