14#include "tkelement_builder.h"
32using json = nlohmann::json;
42 glog.set_class(
"db_element_builder");
45 gdebug <<
"calling tkelement_builder::fill_database(...)" <<
do_endl;
47 tkdb_table &fTable = fDataBase->new_table(fTableName);
48 read_atomic_db(_atomic_db);
49 read_xrays_db(_xrays_db);
53 fTable.
add_column(
"element_id",
"INTEGER PRIMARY KEY NOT NULL");
55 for(
auto &idx: indexes) {
59 if(units.at(idx) !=
"None") {
64 for(
auto &xray: fxrays_possible_names) {
70 fDataBase->exec_sql(
"BEGIN TRANSACTION");
72 for(
auto &elt: fmap_of_atomic_properties) {
74 glog.progress_bar(fmap_of_atomic_properties.size(),idx,message.data());
77 int current_charge = get<2>(elt.second.at(
"charge")).
atoi();
78 fTable[
"element_id"].set_value(current_charge+1);
80 for(
auto &prop : elt.second) {
83 tkstring value = get<2>(prop.second);
86 if(type.
contains(
"INT")) fTable[name.data()].set_value(value.
atoi());
87 else if(type.
contains(
"TEXT")) fTable[name.data()].set_value(value.data());
88 else if(type.
contains(
"FLOAT")) fTable[name.data()].set_value(value.
atof());
89 if(unit !=
"None") fTable[
tkstring::form(
"%s_unit",name.data())].set_value(unit.data());
91 if(fmap_of_xrays.count(current_charge)) {
92 for(
auto &xray: fmap_of_xrays.at(current_charge)) {
94 std::tie(name, unit, value) = xray;
95 fTable[name.data()].set_value(value);
98 bool do_push = (_only_charge==0 || _only_charge==current_charge);
104 fDataBase->exec_sql(
"COMMIT");
105 fDataBase->exec_sql(
"CREATE INDEX charge_index ON element(charge)");
112void tkelement_builder::read_atomic_db(
const char *_atomic_db) {
114 gdebug <<
"filling atomic database using : " << _atomic_db <<
do_endl;
117 f_in.open(_atomic_db);
118 json jf = json::parse(f_in);
122 auto &table = jf.at(
"Table");
124 for(
auto &entry: table.items()) {
125 if(entry.key()==
"Columns") {
126 for(
auto &col: entry.value().at(
"Column").items()) {
127 columns.push_back(col.value());
128 if(col.value()==
"CPKHexColor")
continue;
129 if(col.value()==
"electronegativity")
continue;
130 if(col.value()==
"electron_affinity")
continue;
131 if(col.value()==
"oxidation_states")
continue;
136 else if(entry.key()==
"Columns_Units") {
137 const auto &cols = entry.value().at(
"Column");
138 auto cols_items = cols.items();
139 units.reserve(units.size() + cols.size());
140 std::transform(cols_items.begin(), cols_items.end(), std::back_inserter(units),
141 [](
const auto &col) { return col.value(); });
143 else if(entry.key()==
"Columns_Types") {
144 const auto &cols = entry.value().at(
"Column");
145 auto cols_items = cols.items();
146 types.reserve(types.size() + cols.size());
147 std::transform(cols_items.begin(), cols_items.end(), std::back_inserter(types),
148 [](
const auto &col) { return col.value(); });
150 else if(entry.key()==
"Row") {
151 for(
auto &elt: entry.value().items()) {
152 auto col = elt.value().at(
"Cell");
153 int atomic_number = col.at(0).get<tkstring>().atoi();
155 for(
auto &i: indexes) {
156 if(col.at(i).get<tkstring>().length()==0)
continue;
157 properties.insert({columns.at(i),{columns.at(i),units.at(i),col.at(i).get<tkstring>(),types.at(i)}});
159 fmap_of_atomic_properties.insert({atomic_number,
properties});
174void tkelement_builder::read_xrays_db(
const char *_xrays_db) {
177 f_in.open(_xrays_db);
179 json jf = json::parse(f_in);
181 auto &table = jf.at(
"Elements");
183 for(
auto &entry: table.items()) {
184 auto Z = entry.value().at(
"Z");
185 auto element_name = entry.value().at(
"name");
186 auto xrays = entry.value().at(
"Xrays");
188 vector<atomic_xray> atom_xrays;
189 for(
auto &xray: xrays) {
190 tkstring xray_name = xray.at(
"name").get<tkstring>();
192 tkstring unit = xray.at(
"unit").get<tkstring>();
193 double value = xray.at(
"value").get<
double>()/1000.;
194 atom_xrays.emplace_back(xray_name,unit,value);
195 if(!fxrays_possible_names.count(xray_name)) fxrays_possible_names.insert(xray_name);
197 if(!atom_xrays.empty()) fmap_of_xrays.insert({Z,atom_xrays});
Interface to the sqlite database.
Representaiton of a sqlite data table.
void add_column(const char *_colname, const char *_coltype)
Decoding of the element properties.
virtual ~tkelement_builder()
int fill_database(const char *_atomic_db, const char *_xrays_db, int _only_charge=0)
tkelement_builder(tkdatabase *_database, const char *_table_name="ELEMENT")
std::string with usefull tricks from TString (ROOT) and KVString (KaliVeda) and more....
static const char * form(const char *_format,...)
int atoi() const
Converts a string to integer value.
bool contains(const char *_pat, ECaseCompare _cmp=kExact) const
tkstring & prepend(const tkstring &_st)
double atof() const
Converts a string to double value.
std::map< tkstring, pair< tkstring, tkstring > > properties
tklog & do_endl(tklog &log)