/
file.py
72 lines (65 loc) · 2.93 KB
/
file.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
import numpy as np
from aa.axis import Axes
class File(object) :
def __init__(self, variables=None, axes=None) :
if axes == None :
self.axes = Axes()
else :
self.axes = axes
if variables == None :
self.variables = {}
else :
self.variables = variables
def __getattr__(self, attributeName) :
if 'variables' in self.__dict__ :
if attributeName in self.variables :
return self.variables[attributeName]
if 'axes' in self.__dict__ :
return self.axes[attributeName]
raise AttributeError
def __getitem__(self, item) :
return getattr(self, item)
def close(self) :
pass
def write(self, filePath, compress = True) :
from netCDF4 import Dataset
# making sure the variable's axes are given
# to the file is the user's responsability
with Dataset(filePath, 'w') as output :
for axisName, axis in self.axes.items() :
axisName = Axes.ncStandardize(axisName)
output.createDimension(axisName, len(axis))
if axisName == 'time' :
output.createVariable('time', int, ('time',))
output.variables['time'].units = 'seconds since 1970-1-1'
from datetime import datetime
epoch = datetime(1970, 1, 1)
output.variables['time'][:] = [
(instant-epoch).total_seconds()
for instant in axis.data]
else :
# this is a real axis with information worth recording
if axis.units != 'indices' or len(axis) - 1 != axis[-1] :
output.createVariable(
axisName,
type(np.asscalar(axis.data.ravel()[0])),
(axisName,))
output.variables[axisName][:] = axis.data
if axis.units != None :
output.variables[axisName].units = axis.units
# TODO metadata...
for variableName, variable in self.variables.items() :
#variable.censor_nans(ratio = 1./3)
variable.censor_nans(ratio = 1)
if variableName == '~' :
variableName = 'unknown'
output.createVariable(
variableName,
type(np.asscalar(variable.data.ravel()[0])),
tuple(
[Axes.ncStandardize(axisName) for axisName in
variable.axes.keys()]),
zlib = compress)
if 'units' in variable.metadata :
output.variables[variableName].units = variable.units
output.variables[variableName][:] = variable.data