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 char * sErrMsg =
nullptr;
55 fTable.
add_column(
"element_id",
"INT PRIMARY KEY NOT NULL");
57 for(
auto &idx: indexes) {
61 if(units.at(idx) !=
"None") {
66 for(
auto &xray: fxrays_possible_names) {
73 for(
auto &elt: fmap_of_atomic_properties) {
75 glog.progress_bar(fmap_of_atomic_properties.size(),idx,message.data());
78 int current_charge = get<2>(elt.second.at(
"charge")).
atoi();
79 fTable[
"element_id"].set_value(current_charge+1);
80 tkstring element_symbol = get<2>(elt.second.at(
"symbol"));
81 if(element_symbol ==
"n") element_symbol =
"neutron";
83 for(
auto &prop : elt.second) {
86 tkstring value = get<2>(prop.second);
89 if(type.
contains(
"INT")) fTable[name.data()].set_value(value.
atoi());
90 else if(type.
contains(
"TEXT")) fTable[name.data()].set_value(value.data());
91 else if(type.
contains(
"FLOAT")) fTable[name.data()].set_value(value.
atof());
92 if(unit !=
"None") fTable[
tkstring::form(
"%s_unit",name.data())].set_value(unit.data());
94 if(fmap_of_xrays.count(current_charge)) {
95 for(
auto &xray: fmap_of_xrays.at(current_charge)) {
97 std::tie(name, unit, value) = xray;
98 fTable[name.data()].set_value(value);
101 bool do_push = (_only_charge==0 || _only_charge==current_charge);
104 fDataBase->exec_sql(
tkstring::form(
"CREATE VIEW elt%s as select * from element where element.charge=%d",element_symbol.data(),current_charge));
108 sqlite3_exec(fDataBase->get_sql_db(),
"END TRANSACTION",
nullptr,
nullptr, &sErrMsg);
109 fDataBase->exec_sql(
"CREATE INDEX charge_index ON element(charge)");
116void tkelement_builder::read_atomic_db(
const char *_atomic_db) {
118 gdebug <<
"filling atomic database using : " << _atomic_db <<
do_endl;
121 f_in.open(_atomic_db);
122 json jf = json::parse(f_in);
126 auto &table = jf.at(
"Table");
128 for(
auto &entry: table.items()) {
129 if(entry.key()==
"Columns") {
130 for(
auto &col: entry.value().at(
"Column").items()) {
131 columns.push_back(col.value());
132 if(col.value()==
"CPKHexColor")
continue;
133 if(col.value()==
"electronegativity")
continue;
134 if(col.value()==
"electron_affinity")
continue;
135 if(col.value()==
"oxidation_states")
continue;
140 else if(entry.key()==
"Columns_Units") {
141 const auto &cols = entry.value().at(
"Column");
142 auto cols_items = cols.items();
143 units.reserve(units.size() + cols.size());
144 std::transform(cols_items.begin(), cols_items.end(), std::back_inserter(units),
145 [](
const auto &col) { return col.value(); });
147 else if(entry.key()==
"Columns_Types") {
148 const auto &cols = entry.value().at(
"Column");
149 auto cols_items = cols.items();
150 types.reserve(types.size() + cols.size());
151 std::transform(cols_items.begin(), cols_items.end(), std::back_inserter(types),
152 [](
const auto &col) { return col.value(); });
154 else if(entry.key()==
"Row") {
155 for(
auto &elt: entry.value().items()) {
156 auto col = elt.value().at(
"Cell");
157 int atomic_number = col.at(0).get<tkstring>().atoi();
159 for(
auto &i: indexes) {
160 if(col.at(i).get<tkstring>().length()==0)
continue;
161 properties.insert({columns.at(i),{columns.at(i),units.at(i),col.at(i).get<tkstring>(),types.at(i)}});
163 fmap_of_atomic_properties.insert({atomic_number,
properties});
178void tkelement_builder::read_xrays_db(
const char *_xrays_db) {
181 f_in.open(_xrays_db);
183 json jf = json::parse(f_in);
185 auto &table = jf.at(
"Elements");
187 for(
auto &entry: table.items()) {
188 auto Z = entry.value().at(
"Z");
189 auto element_name = entry.value().at(
"name");
190 auto xrays = entry.value().at(
"Xrays");
192 vector<atomic_xray> atom_xrays;
193 for(
auto &xray: xrays) {
194 tkstring xray_name = xray.at(
"name").get<tkstring>();
196 tkstring unit = xray.at(
"unit").get<tkstring>();
197 double value = xray.at(
"value").get<
double>()/1000.;
198 atom_xrays.emplace_back(xray_name,unit,value);
199 if(!fxrays_possible_names.count(xray_name)) fxrays_possible_names.insert(xray_name);
201 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)