/
xml_maker.py
184 lines (166 loc) · 6.08 KB
/
xml_maker.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
from __future__ import print_function, division
from lxml import etree
import cdms2
import numpy
import os
import cdtime
typeConv = { "Float32": "Float",
"Float": "Double",
"Float64": "Double",
"Float128": "Double",
"Int32": "Int",
}
def ingestCSV(filename):
with open(filename) as f:
csv = f.readlines()
vars = csv[0].strip().split(",")
times = []
for i,l in enumerate(csv[2:],start=2):
if l.find("file_ix")>-1:
break
files = []
for l in csv[i+1:]:
print(l)
files.append(l.strip().split(",")[1][1:-1])
return vars, files, csv[2:i]
def createMapping(filename):
vars, files, csv = ingestCSV(filename)
print("FILE ):<{}>".format(files[0]))
out = {}
f = cdms2.open(files[0])
times = []
time_values = []
time_partition = []
nontimes = []
for v in f.listvariables():
tim = f[v].getTime()
if tim is None: # not a time one
nontimes.append(v)
else:
times.append(v)
map_files = []
index = 0
for l in csv:
if l.strip() == "":
break
sp = l.strip().split(",")
time_values.append(cdtime.s2r(sp[0], tim.units, tim.getCalendar()).value)
indx = vars.index(times[0])
location = int(sp[indx].split(":")[0])
if len(map_files) == 0:
map_files.append([location, [index, index]])
time_partition.append(index)
elif map_files[-1][0] != location:
map_files.append([location, [index, index]])
time_partition.append(index)
else: # same file
map_files[-1][1][-1] = index + 1
time_partition[-1] = index + 1
index += 1
print(map_files)
mapping = "[[" + str(times).replace("'","") + ",["
mapping += ",".join(["[{},{},-,-,-,{}]".format(v[0],v[1],os.path.basename(files[nm])) for nm,v in map_files])
mapping+="]],["+str(nontimes).replace("'","") +",[[-,-,-,-,-,{}]]]]".format(os.path.basename(files[map_files[0][0]]))
out["mapping"] = mapping
out["time"] = {"id": tim.id, "values":time_values, "partition":time_partition}
out["files"] = files
out["vars"] = vars
out["directory"] = os.path.dirname(files[map_files[0][0]])
return out
def csv2xml(csv_file):
info = createMapping(csv_file)
time_name = info["time"]["id"]
time_values = " ".join([str(v) for v in info["time"]["values"]])
time_partition = "[{}]".format(" ".join([str(v) for v in info["time"]["partition"]]))
time_length = str(len(info["time"]["values"]))
dataset = etree.Element("dataset")
f = cdms2.open(info["files"][0])
# split vars into timebased and non time based ones
timesVars = []
nonTimeVars = []
for v in f.listvariables():
V = f[v]
if V.getTime() is not None:
timesVars.append(v)
else:
nonTimeVars.append(v)
map = []
cdscan_file_Attributes = ["institution", "production", "calendar", "Conventions", "history"]
for att in f.listglobal():
if att in cdscan_file_Attributes:
dataset.set(att, getattr(f, att))
else:
elt = etree.Element("attr")
elt.set("datatype", "String")
elt.set("name", att)
elt.text = getattr(f, att)
dataset.append(elt)
dataset.set("cdms_filemap",info["mapping"])
dataset.set("directory",info["directory"])
dataset.set("id", "none")
for att in cdscan_file_Attributes:
if not att in dataset.keys():
dataset.set(att,"")
dims = f.listdimension()
for data in f.listdimension()+f.listvariables():
if data in dims:
etype = "axis"
else:
etype = "variable"
elt = etree.Element(etype)
for att in f[data].attributes:
value = f[data].attributes[att]
if not isinstance(value, (str, int, float)):
elt.set(att, "{}".format(float(f[data].attributes[att])))
else:
elt.set(att, "{}".format(f[data].attributes[att]))
elt.set("id",f[data].id)
dtype = numpy.typeNA.get(f[data].typecode())
dtype= typeConv.get(dtype,dtype)
elt.set("datatype", dtype)
if data in dims:
ax = f[data]
if ax.isTime():
ax2 = ax.clone()
ax2.setCalendar(ax.getCalendar())
dataset.set("calendar",ax2.calendar)
elt.set("calendar",ax2.calendar)
elt.set("length", time_length)
elt.set("partition",time_partition)
elt.set("id", time_name)
elt.set("name_in_file", ax2.id)
values_string = time_values
else:
elt.set("length", "{}".format(len(ax)))
if ax.isCircular():
topo = "circular"
else:
topo = "linear"
topotree = etree.Element("attr")
topotree.set("datatype","String")
topotree.set("name","realtopology")
topotree.text = topo
elt.append(topotree)
values_string = " ".join(["{}".format(x) for x in ax[:]])
elt.text="[{}]".format(values_string)
else:
domain = etree.Element("domain")
for ax in f[data].getAxisList():
domElt = etree.Element("domElem")
domElt.set("start","0")
if ax.isTime():
domElt.set("length", time_length)
domElt.set("name", time_name)
else:
domElt.set("length", str(len(ax)))
domElt.set("name", ax.id)
domain.append(domElt)
elt.append(domain)
dataset.append(elt)
return etree.tostring(dataset)
out_file = "test.xml"
in_file = "../charles.csv"
with open(out_file,"w") as f:
print('<?xml version="1.0"?>', file=f)
print('<!DOCTYPE dataset SYSTEM "http://www-pcmdi.llnl.gov/software/cdms/cdml.dtd">', file=f)
print(csv2xml(in_file), file=f)