Newer
Older
from numpy import arange, dtype
from physicalParameter import Roscop
data = {}
dims = ['TIME', 'LATITUDE', 'LONGITUDE', 'DEPTH']
vars = dims.copy()
# move to main after tests
r = Roscop("code_roscop.csv")
# create netcdf file
nc = Dataset(fileName, "w", format="NETCDF3_CLASSIC")
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
# n is number of profiles, m the max size of profiles
time = nc.createDimension("TIME", fe.n)
lat = nc.createDimension("LATITUDE", fe.n)
lon = nc.createDimension("LONGITUDE", fe.n)
depth = nc.createDimension('DEPTH', fe.m)
logging.debug(" depth: {}, time: {}, lat: {}, lon: {}".format(
len(depth), len(time), len(lat), len(lon)))
# create variables
# add dimensions before variables list
for k in fe.keys:
vars.append(k)
for key in vars:
# for each variables get the attributes list
hash = r.returnCode(key)
# _FillValue attribute must be set when variable is created
# (using fill_value keyword to createVariable)
if '_FillValue' in hash:
fillvalue = hash['_FillValue']
# remove from the dictionary
hash.pop('_FillValue')
else:
fillvalue = None
# create the variable
data[key] = nc.createVariable(
key, dtype(hash['types']).char, dims, fill_value=fillvalue)
# remove from the dictionary
hash.pop('types')
# create dynamically variable attributes
for k in hash.keys():
setattr(data[key], k, hash[k])
# debug
for key in vars:
print(data[key])