37#include "tkelement_builder.h"
55using json = nlohmann::json;
57tkelement_builder::tkelement_builder(
tkdatabase *_database,
const char *_table_name) : fDataBase(_database), fTableName(_table_name)
65 glog.set_class(
"db_element_builder");
68 gdebug <<
"calling tkelement_builder::fill_database(...)" <<
do_endl;
71 read_atomic_db(_atomic_db);
72 read_xrays_db(_xrays_db);
76 char * sErrMsg =
nullptr;
78 fTable.
add_column(
"element_id",
"INT PRIMARY KEY NOT NULL");
80 for(
auto &idx: indexes) {
84 if(units.at(idx) !=
"None") {
89 for(
auto &xray: fxrays_possible_names) {
96 for(
auto &elt: fmap_of_atomic_properties) {
98 glog.progress_bar(fmap_of_atomic_properties.size(),idx,message.data());
101 int current_charge = get<2>(elt.second.at(
"charge")).atoi();
102 fTable[
"element_id"].set_value(current_charge+1);
103 tkstring element_symbol = get<2>(elt.second.at(
"symbol"));
104 if(element_symbol ==
"n") element_symbol =
"neutron";
106 for(
auto &prop : elt.second) {
107 tkstring name = get<0>(prop.second);
108 tkstring unit = get<1>(prop.second);
109 tkstring value = get<2>(prop.second);
110 tkstring type = get<3>(prop.second);
112 if(type.
contains(
"INT")) fTable[name.data()].set_value(value.
atoi());
113 else if(type.
contains(
"TEXT")) fTable[name.data()].set_value(value.data());
114 else if(type.
contains(
"FLOAT")) fTable[name.data()].set_value(value.
atof());
115 if(unit !=
"None") fTable[
tkstring::form(
"%s_unit",name.data())].set_value(unit.data());
117 if(fmap_of_xrays.count(current_charge)) {
118 for(
auto &xray: fmap_of_xrays.at(current_charge)) {
120 std::tie(name, unit, value) = xray;
121 fTable[name.data()].set_value(value);
124 bool do_push = (_only_charge==0 || _only_charge==current_charge);
127 fDataBase->
exec_sql(
tkstring::form(
"CREATE VIEW elt%s as select * from element where element.charge=%d",element_symbol.data(),current_charge));
131 sqlite3_exec(fDataBase->
get_sql_db(),
"END TRANSACTION",
nullptr,
nullptr, &sErrMsg);
132 fDataBase->
exec_sql(
"CREATE INDEX charge_index ON element(charge)");
139void tkelement_builder::read_atomic_db(
const char *_atomic_db) {
141 gdebug <<
"filling atomic database using : " << _atomic_db <<
do_endl;
144 f_in.open(_atomic_db);
145 json jf = json::parse(f_in);
149 auto &table = jf.at(
"Table");
151 for(
auto &entry: table.items()) {
152 if(entry.key()==
"Columns") {
153 for(
auto &col: entry.value().at(
"Column").items()) {
154 columns.push_back(col.value());
155 if(col.value()==
"CPKHexColor")
continue;
156 if(col.value()==
"electronegativity")
continue;
157 if(col.value()==
"electron_affinity")
continue;
158 if(col.value()==
"oxidation_states")
continue;
163 else if(entry.key()==
"Columns_Units") {
164 for(
auto &col: entry.value().at(
"Column").items()) {
165 units.push_back(col.value());
168 else if(entry.key()==
"Columns_Types") {
169 for(
auto &col: entry.value().at(
"Column").items()) {
170 types.push_back(col.value());
173 else if(entry.key()==
"Row") {
174 for(
auto &elt: entry.value().items()) {
175 auto col = elt.value().at(
"Cell");
176 int atomic_number = col.at(0).get<
tkstring>().atoi();
178 for(
auto &i: indexes) {
179 if(col.at(i).get<
tkstring>().length()==0)
continue;
180 properties.insert({columns.at(i),{columns.at(i),units.at(i),col.at(i).get<
tkstring>(),types.at(i)}});
182 fmap_of_atomic_properties.insert({atomic_number,
properties});
197void tkelement_builder::read_xrays_db(
const char *_xrays_db) {
200 f_in.open(_xrays_db);
202 json jf = json::parse(f_in);
204 auto &table = jf.at(
"Elements");
206 for(
auto &entry: table.items()) {
207 auto Z = entry.value().at(
"Z");
208 auto name = entry.value().at(
"name");
209 auto xrays = entry.value().at(
"Xrays");
211 vector<atomic_xray> atom_xrays;
212 for(
auto &xray: xrays) {
216 double value = xray.at(
"value").get<
double>()/1000.;
217 atom_xrays.emplace_back(name,unit,value);
218 if(!fxrays_possible_names.count(name)) fxrays_possible_names.insert(name);
220 if(!atom_xrays.empty()) fmap_of_xrays.insert({Z,atom_xrays});
Interface to the sqlite database.
tkdb_table & new_table(tkstring _table_name)
int exec_sql(const char *_cmd)
returns the first value for selection
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)
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)