# A script to build the ground state mapping from the published
# toss.data.  This is necessary for all species/ionisation levels
# before importing the line_tap part of the RD.

import pyvo
import json

ground_levels = {}
svc = pyvo.dal.TAPService("http://dc.g-vo.org/tap")
for row in svc.run_sync("""
		select max(final_level_energy) as ground_level_a,
			max(initial_level_energy) as ground_level_b,
			chemical_element,
			ion_stage
		from toss.data
		group by chemical_element, ion_stage"""):
	ground_levels.setdefault(row["chemical_element"], {}
		)[row["ion_stage"]] = max(row["ground_level_a"], row["ground_level_b"])

with open("ground_states.json", "w") as f:
	json.dump(ground_levels, f)
