forked from OpenExoplanetCatalogue/open_exoplanet_catalogue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanup.py
executable file
·293 lines (253 loc) · 9.86 KB
/
cleanup.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
290
291
292
#!/usr/bin/python
import xml.etree.ElementTree as ET
import glob
import os
import hashlib
import sys
import datetime
# Variables to keep track of progress
fileschecked = 0
issues = 0
xmlerrors = 0
fileschanged = 0
# Calculate md5 hash to check for changes in file.
def md5_for_file(f, block_size=2 ** 20):
md5 = hashlib.md5()
while True:
data = f.read(block_size)
if not data:
break
md5.update(data)
return md5.digest()
# Nicely indents the XML output
def indent(elem, level=0):
i = "\n" + level * "\t"
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + "\t"
if not elem.tail or not elem.tail.strip():
elem.tail = i
for elem in elem:
indent(elem, level + 1)
if not elem.tail or not elem.tail.strip():
elem.tail = i
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i
# Removes empty nodes from the tree
def removeemptytags(elem):
if elem.text:
elem.text = elem.text.strip()
toberemoved = []
for child in elem:
if len(child) == 0 and child.text is None and len(child.attrib) == 0:
toberemoved.append(child)
for child in toberemoved:
elem.remove(child)
for child in elem:
removeemptytags(child)
# Convert error to errorminus and errorplus
if 'ep' in elem.attrib:
err = elem.attrib['ep']
del elem.attrib['ep']
elem.attrib['errorplus'] = err
if 'em' in elem.attrib:
err = elem.attrib['em']
del elem.attrib['em']
elem.attrib['errorminus'] = err
if 'error' in elem.attrib:
err = elem.attrib['error']
del elem.attrib['error']
elem.attrib['errorminus'] = err
elem.attrib['errorplus'] = err
# Check if an unknown tag is present (most likely an indication for a typo)
validtags = [
"system", "name", "new", "description", "ascendingnode", "discoveryyear",
"lastupdate", "list", "discoverymethod", "semimajoraxis", "period", "magV", "magJ",
"magH", "magR", "magB", "magK", "magI", "distance",
"longitude", "imagedescription", "image", "age", "declination", "rightascension",
"metallicity", "inclination", "spectraltype", "binary", "planet", "periastron", "star",
"mass", "eccentricity", "radius", "temperature", "videolink", "transittime", "spinorbitalignment",
"istransiting"]
validattributes = [
"error",
"errorplus",
"errorminus",
"unit",
"upperlimit",
"lowerlimit",
"type"]
validdiscoverymethods = ["RV", "transit", "timing", "imaging", "microlensing"]
tagsallowmultiple = ["list", "name", "planet", "star", "binary"]
def checkforvalidtags(elem):
problematictag = None
for child in elem:
_tmp = checkforvalidtags(child)
if _tmp:
problematictag = _tmp
if elem.tag not in validtags:
problematictag = elem.tag
for a in elem.attrib:
if a not in validattributes:
return a
return problematictag
# Convert units (makes data entry easier)
def convertunitattrib(elem, attribname, factor):
if attribname in elem.attrib:
elem.attrib[attribname] = "%f" % ( float(elem.attrib[attribname]) * factor)
def convertunit(elem, factor):
print "Converting unit of tag \"" + elem.tag + "\"."
del elem.attrib['unit']
if elem.text:
elem.text = "%f" % (float(elem.text) * factor)
convertunitattrib(elem, "error", factor)
convertunitattrib(elem, "errorplus", factor)
convertunitattrib(elem, "errorminus", factor)
convertunitattrib(elem, "ep", factor)
convertunitattrib(elem, "em", factor)
convertunitattrib(elem, "upperlimit", factor)
convertunitattrib(elem, "lowerlimit", factor)
def checkForBinaryPlanet(root, criteria, liststring):
""" Checks if binary planets have been added to corresponding list
"""
global fileschanged
planets = root.findall(criteria)
for planet in planets:
plists = planet.findall(".//list")
if liststring not in [plist.text for plist in plists]:
ET.SubElement(planet, "list").text = liststring
print "Added '" + filename + "' to list '" + liststring + "'."
fileschanged += 1
def checkForTransitingPlanets(root):
""" Checks for transisting planets by first seeing if there is a transittime and then checking the discovery
method
"""
global fileschanged
global issues
planets = root.findall(".//planet")
for planet in planets:
if not planet.findtext('.//istransiting'):
addtag = 0
hasTransittime = planet.findtext(".//transittime")
discoveryMethod = planet.findtext(".//discoverymethod")
planetRadius = planet.findtext(".//radius")
if hasTransittime or 'transit' == discoveryMethod:
addtag = 1
else:
if planetRadius: # only measured from transits, imaging for now
planetName = planet.findtext(".//name")
excludeList = ('Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Pluto',
'PSR J1719-1438 b', # radius estimated from Roche Lobe radius
'',
)
if planetName not in excludeList:
if not discoveryMethod == 'imaging':
print '{} in {} has a radius but is is missing a istransiting tag'.format(planetName, filename)
issues += 1
if addtag:
ET.SubElement(planet, "istransiting").text = '1'
print 'Added istransiting tag to {}'.format(filename)
fileschanged += 1
# Loop over all files and create new data
for filename in glob.glob("systems*/*.xml"):
fileschecked += 1
# Save md5 for later
f = open(filename, 'rt')
md5_orig = md5_for_file(f)
# Open file
f = open(filename, 'rt')
# Try to parse file
try:
root = ET.parse(f).getroot()
planets = root.findall(".//planet")
stars = root.findall(".//star")
binaries = root.findall(".//binary")
except ET.ParseError as error:
print '{}, {}'.format(filename, error)
xmlerrors += 1
issues += 1
continue
finally:
f.close()
# Find tags with range=1 and convert to default error format
for elem in root.findall(".//*[@range='1']"):
fragments = elem.text.split()
elem.text = fragments[0]
elem.attrib["errorminus"] = "%f" % (float(fragments[0]) - float(fragments[1]))
elem.attrib["errorplus"] = "%f" % (float(fragments[2]) - float(fragments[0]))
del elem.attrib["range"]
print "Converted range to errorbars in tag '" + elem.tag + "'."
# Convert units to default units
for mass in root.findall(".//planet/mass[@unit='me']"):
convertunit(mass, 0.0031457007)
for radius in root.findall(".//planet/radius[@unit='re']"):
convertunit(radius, 0.091130294)
for angle in root.findall(".//*[@unit='rad']"):
convertunit(angle, 57.2957795130823)
# Check lastupdate tag for correctness
for lastupdate in root.findall(".//planet/lastupdate"):
la = lastupdate.text.split("/")
if len(la) != 3 or len(lastupdate.text) != 8:
print "Date format not following 'yy/mm/dd' convention: " + filename
issues += 1
if int(la[0]) + 2000 - datetime.date.today().year > 0 or int(la[1]) > 12 or int(la[2]) > 31:
print "Date not valid: " + filename
issues += 1
# Check that names follow conventions
if not root.findtext("./name") + ".xml" == os.path.basename(filename):
print "Name of system not the same as filename: " + filename
issues += 1
for obj in planets + stars:
name = obj.findtext("./name")
if not name:
print "Didn't find name tag for object \"" + obj.tag + "\" in file \"" + filename + "\"."
issues += 1
# Check if tags are valid and have valid attributes
problematictag = checkforvalidtags(root)
if problematictag:
print "Problematic tag/attribute '" + problematictag + "' found in file \"" + filename + "\"."
issues += 1
discoverymethods = root.findall(".//discoverymethod")
for dm in discoverymethods:
if not (dm.text in validdiscoverymethods):
print "Problematic discoverymethod '" + dm.text + "' found in file \"" + filename + "\"."
issues += 1
# Check if there are duplicate tags
for obj in planets + stars + binaries:
uniquetags = []
for child in obj:
if not child.tag in tagsallowmultiple:
if child.tag in uniquetags:
print "Error: Found duplicate tag \"" + child.tag + "\" in file \"" + filename + "\"."
issues += 1
else:
uniquetags.append(child.tag)
# Check binary planet lists
checkForBinaryPlanet(root, ".//binary/planet", "Planets in binary systems, P-type")
checkForBinaryPlanet(root, ".//binary/star/planet", "Planets in binary systems, S-type")
# Check transiting planets
checkForTransitingPlanets(root)
# Cleanup XML
removeemptytags(root)
indent(root)
# Write XML to file.
ET.ElementTree(root).write(filename, encoding="UTF-8", xml_declaration=False)
# Check for new md5
f = open(filename, 'rt')
md5_new = md5_for_file(f)
if md5_orig != md5_new:
fileschanged += 1
errorcode = 0
print "Cleanup script finished. %d files checked." % fileschecked
if fileschanged > 0:
print "%d file(s) modified." % fileschanged
errorcode = 1
if xmlerrors > 0:
print "%d XML errors found." % xmlerrors
errorcode = 2
if issues > 0:
print "Number of issues: %d (see above)." % issues
errorcode = 3
else:
print "No issues found."
sys.exit(errorcode)