Commit e93593e2 authored by jclamber's avatar jclamber

user sqlite3 own db cia .unsio

git-svn-id: http://svn.oamp.fr/repos/unsio/trunk@153 ce2cc22f-6084-46ce-a062-084b172ee5dc
parent 3083e65f
......@@ -22,7 +22,62 @@
#include <assert.h>
#include <cmath>
#include <cstdlib>
#include <fstream>
namespace uns {
// ============================================================================
// parseConfig
std::string CSnapshotInterfaceIn::parseConfig(std::string req)
{
std::string config_file(std::string(getenv("HOME")) + "/.unsio");
std::ifstream fi;
bool exist=true;
std::string key,value="";
fi.open(config_file.c_str(), std::ios::in);
if (! fi.is_open()) {
std::cerr << "Unable to open file ["<<config_file<<"] for reading, skipping...\n";
exist = false;
}
if (exist) {
bool stop = false;
while (!stop && ! fi.eof()) { // while ! eof
std::string line;
getline(fi,line); // read on eline
if ( ! fi.eof()) {
std::istringstream str(line); // stream line
std::string parse;
// following loop parse each lines previously read
//
int cpt=0;
bool equal=false;
while ( str >> parse && // something to read
parse[0] != '#' && // not commented out
parse[0] != '!' // not commented out
) {
cpt++;
if (cpt==1) { // key
key=parse;
}
if (cpt==2) { // sim type
if (parse == "=") {
equal=true;
} else {
equal = false;
}
}
if (cpt==3 && equal && key==req) { // value
value=parse;
stop=true; // found pair key value
}
}
}
}
fi.close();
}
return value;
}
// ============================================================================
// parseSelectTime
void CSnapshotInterfaceIn::parseSelectTime()
......
......@@ -138,6 +138,7 @@ namespace uns {
bool isValidData() { return valid; }
void setFileName(std::string _f) { filename = _f;}
bool getRangeSelect(const char *, int *, int *, int * , bool fortran=false);
std::string parseConfig(std::string);
//std::string getFileName() const { return filename;};
int getInterfaceIndex() { return interface_index; }
bool isFileExist() { return true; }
......
......@@ -10,7 +10,7 @@
// ============================================================================
/*
@author Jean-Charles Lambert <Jean-Charles.Lambert@oamp.fr>
@author Jean-Charles Lambert <Jean-Charles.Lambert@oamp.fr>
*/
#ifndef NOSQLITE3 // do not compite if no sqlite3 lib
#include "snapshotsim.h"
......@@ -21,23 +21,26 @@
#include <iomanip>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#define DEBUG 0
#include "unsdebug.h"
#include "uns.h"
namespace uns {
// ASCII database
std::string uns::CSnapshotInterfaceIn::sim_db_file="/pil/programs/DB/sim_info.txt";
std::string uns::CSnapshotInterfaceIn::nemo_range_file="/pil/programs/DB/nemo_range.txt";
std::string uns::CSnapshotInterfaceIn::eps_db_file="/pil/programs/DB/sim_eps.txt";
// SQLITE database
// ASCII database
std::string uns::CSnapshotInterfaceIn::sim_db_file="/pil/programs/DB/sim_info.txt";
std::string uns::CSnapshotInterfaceIn::nemo_range_file="/pil/programs/DB/nemo_range.txt";
std::string uns::CSnapshotInterfaceIn::eps_db_file="/pil/programs/DB/sim_eps.txt";
// SQLITE database
// ----------------------------------------------------------------------------
// constructor
CSnapshotSimIn::CSnapshotSimIn(const std::string _name,
const std::string _comp,
const std::string _time,
const bool verb)
const std::string _comp,
const std::string _time,
const bool verb)
:CSnapshotInterfaceIn(_name, _comp, _time, verb)
{
snapshot = NULL;
......@@ -64,9 +67,9 @@ ComponentRangeVector * CSnapshotSimIn::getSnapshotRange()
{
assert(snapshot != NULL);
assert(snapshot->isValidData());
if ((simtype == "Nemo") && nemosim != "" && crv.size()>0) {
if ((simtype == "Nemo") && nemosim != "" && crv.size()>0) {
return &crv;
}
}
else {
return snapshot->getSnapshotRange();
}
......@@ -99,6 +102,11 @@ int CSnapshotSimIn::nextFrameSelect(ComponentRangeVector * crvs)
bool CSnapshotSimIn::openSqlDb(std::string db)
{
sqlite_db = db;
std::string mydbname=parseConfig("dbname");
if (mydbname != "" ) {
sqlite_db = mydbname;
std::cerr << "Use user database name [" << mydbname << "]\n";
}
sql = new jclt::CSQLite3(sqlite_db);
bool status=sql->isOpen();
if (! status) {
......@@ -134,7 +142,7 @@ bool CSnapshotSimIn::findSqlSim()
else
if (interface_type == "Nemo") interface_index=0;
else {
std::cerr <<"CSnapshotSimIn::findSqlSim => Unknown interface type....\n";
std::cerr <<"CSnapshotSimIn::findSqlSim => Unknown interface type....\n";
}
}
return status;
......@@ -214,44 +222,44 @@ bool CSnapshotSimIn::findSim()
if ( ! fi.eof()) {
std::istringstream str(line); // stream line
std::string parse;
// following loop parse each lines previously read
// following loop parse each lines previously read
//
int cpt=0;
while ( str >> parse && // something to read
parse[0] != '#' && // not commented out
parse[0] != '!' // not commented out
) {
cpt++;
if (cpt==1) { // simname
simname=parse;
}
if (cpt==2) { // sim type
//std::istringstream ss(parse);
//ss >> simtype;
simtype=parse;
interface_type = simtype;
if (interface_type == "Gadget") interface_index=1;
else
if (interface_type == "Nemo") interface_index=0;
else {
std::cerr <<"CSnapshotSimIn::findSim => Unknown interface type....\n";
}
}
if (cpt==3) { // sim's dirname
dirname=parse;
}
if (cpt==4) { // sim's basename
basename=parse;
}
while ( str >> parse && // something to read
parse[0] != '#' && // not commented out
parse[0] != '!' // not commented out
) {
cpt++;
if (cpt==1) { // simname
simname=parse;
}
if (cpt==2) { // sim type
//std::istringstream ss(parse);
//ss >> simtype;
simtype=parse;
interface_type = simtype;
if (interface_type == "Gadget") interface_index=1;
else
if (interface_type == "Nemo") interface_index=0;
else {
std::cerr <<"CSnapshotSimIn::findSim => Unknown interface type....\n";
}
}
if (cpt==3) { // sim's dirname
dirname=parse;
}
if (cpt==4) { // sim's basename
basename=parse;
}
}
if (simname == filename) { // we found simulation
stop = true; // we have a snapshot
status = true; // so we can stop reading
std::cerr << "SIM DB:Found simulation ["<<simname<<"] in database !\n";
stop = true; // we have a snapshot
status = true; // so we can stop reading
std::cerr << "SIM DB:Found simulation ["<<simname<<"] in database !\n";
}
if (cpt != 4) {
std::cerr << "\n\nWarning, bad #strings ["<<cpt<<"] parsed\n"
<< "during CSnapshotSimIn::findSim()....\n";
std::cerr << "\n\nWarning, bad #strings ["<<cpt<<"] parsed\n"
<< "during CSnapshotSimIn::findSim()....\n";
}
}
else { // end of file
......@@ -281,54 +289,54 @@ bool CSnapshotSimIn::readEpsFile()
std::string line;
getline(fi,line); // read on eline
if ( ! fi.eof()) {
std::istringstream str(line); // stream line
std::string parse;
// following loop parse each lines previously read
//
int cpt=0;
while ( str >> parse && // something to read
parse[0] != '#' && // not commented out
parse[0] != '!' // not commented out
) {
cpt++;
if (cpt==1) { // simname
simname=parse;
if (simname == filename) { // we found simulation
//stop = true; // we have a snapshot
status = true; // we have all components
std::cerr << "EPS:Found simulation ["<<simname<<"] in database !\n";
}
}
if (simname == filename) { // we found simulation
std::istringstream ss(parse);
if (cpt < MAX_EPS+2) { //
ss >> eps[cpt-2]; // file EPS array
}
}
} // while ( str ....
//
if (simname == filename) {
stop=true; // simulation has been found
assert(cpt>1); // we must have read at least one eps
std::istringstream str(line); // stream line
std::string parse;
// following loop parse each lines previously read
//
int cpt=0;
while ( str >> parse && // something to read
parse[0] != '#' && // not commented out
parse[0] != '!' // not commented out
) {
cpt++;
if (cpt==1) { // simname
simname=parse;
if (simname == filename) { // we found simulation
//stop = true; // we have a snapshot
status = true; // we have all components
std::cerr << "EPS:Found simulation ["<<simname<<"] in database !\n";
}
}
if (simname == filename) { // we found simulation
std::istringstream ss(parse);
if (cpt < MAX_EPS+2) { //
ss >> eps[cpt-2]; // file EPS array
}
}
} // while ( str ....
//
if (simname == filename) {
stop=true; // simulation has been found
assert(cpt>1); // we must have read at least one eps
// copy last eps read to next eps
// it's a trick for NEMO simulations
// which have only one eps
for (int i=cpt-1; i<MAX_EPS; i++) {
std::cerr << "eps shift i="<<i<<" cpt="<<cpt<<" eps="<<eps[cpt-2]<<"\n";
eps[i] = eps[cpt-2];
}
}
// copy last eps read to next eps
// it's a trick for NEMO simulations
// which have only one eps
for (int i=cpt-1; i<MAX_EPS; i++) {
std::cerr << "eps shift i="<<i<<" cpt="<<cpt<<" eps="<<eps[cpt-2]<<"\n";
eps[i] = eps[cpt-2];
}
}
} // if !eof ...
else { // end of file
stop = true;
status = false;
stop = true;
status = false;
}
} // while (!stop ...
} // if (status....
if (! status) {
std::cerr<<"\n\nWARNING, simulation ["<<filename<<"] has no entry in the"
<<"EPS datafile ["<<uns::CSnapshotInterfaceIn::eps_db_file<<"]\n\n";
<<"EPS datafile ["<<uns::CSnapshotInterfaceIn::eps_db_file<<"]\n\n";
}
return status;
}
......@@ -370,48 +378,48 @@ bool CSnapshotSimIn::fillNemoRange()
std::string line;
getline(fi,line); // read on eline
if ( ! fi.eof()) {
std::istringstream str(line); // stream line
std::string parse;
// following loop parse each lines previously read
//
int cpt=0;
while ( str >> parse && // something to read
parse[0] != '#' && // not commented out
parse[0] != '!' // not commented out
) {
cpt++;
if (cpt==1) { // simname
simname=parse;
if (simname == filename) { // we found simulation
stop = true; // we have a snapshot
status = true; // we have all components
std::cerr << "Found simulation ["<<simname<<"] in database !\n";
crv.clear();
offset=0;
}
}
if (simname == filename) { // we found simulation
if (cpt==2) { // #total
addNemoComponent(offset,parse,"all");
}
if (cpt==3) { // #disk
addNemoComponent(offset,parse,"disk");
}
if (cpt==4) { // #bulge
addNemoComponent(offset,parse,"bulge");
}
if (cpt==5) { // #halo
addNemoComponent(offset,parse,"halo");
}
if (cpt==6) { // #halo2
addNemoComponent(offset,parse,"halo2");
}
}
} // while ( str ....
std::istringstream str(line); // stream line
std::string parse;
// following loop parse each lines previously read
//
int cpt=0;
while ( str >> parse && // something to read
parse[0] != '#' && // not commented out
parse[0] != '!' // not commented out
) {
cpt++;
if (cpt==1) { // simname
simname=parse;
if (simname == filename) { // we found simulation
stop = true; // we have a snapshot
status = true; // we have all components
std::cerr << "Found simulation ["<<simname<<"] in database !\n";
crv.clear();
offset=0;
}
}
if (simname == filename) { // we found simulation
if (cpt==2) { // #total
addNemoComponent(offset,parse,"all");
}
if (cpt==3) { // #disk
addNemoComponent(offset,parse,"disk");
}
if (cpt==4) { // #bulge
addNemoComponent(offset,parse,"bulge");
}
if (cpt==5) { // #halo
addNemoComponent(offset,parse,"halo");
}
if (cpt==6) { // #halo2
addNemoComponent(offset,parse,"halo2");
}
}
} // while ( str ....
} // if !eof ...
else { // end of file
stop = true;
status = false;
stop = true;
status = false;
}
} // while (!stop ...
} // if (status....
......@@ -421,7 +429,7 @@ bool CSnapshotSimIn::fillNemoRange()
// ============================================================================
// addNemoComponent
int CSnapshotSimIn::addNemoComponent(int& offset, std::string parse,
std::string comp )
std::string comp )
{
int nbody;
std::istringstream ss(parse);
......@@ -447,10 +455,10 @@ bool CSnapshotSimIn::isNewFrame()
}
else {
if (simtype=="Nemo") {
status=buildNemoFile();
status=buildNemoFile();
}
else {
std::cerr <<"\nUnknown simulation type ["<<simtype<<"]\n";
std::cerr <<"\nUnknown simulation type ["<<simtype<<"]\n";
}
}
if (status) {
......@@ -473,16 +481,16 @@ bool CSnapshotSimIn::buildNemoFile()
if (snapshot) delete snapshot;
if (0) { // ASCII database
if (fillNemoRange()) {
if (verbose) uns::ComponentRange::list(&crv);
if (verbose) uns::ComponentRange::list(&crv);
}
} else {
if (fillSqlNemoRange()) {
if (verbose) uns::ComponentRange::list(&crv);
if (verbose) uns::ComponentRange::list(&crv);
}
}
// try to open NEMO sim
PRINT("trying top instantiate CSnapshotNemo("<<myfile<<") verbose="<<verbose<<"\n";)
snapshot = new CSnapshotNemoIn(myfile, select_part, select_time,verbose);
snapshot = new CSnapshotNemoIn(myfile, select_part, select_time,verbose);
if (snapshot->isValidData()) {
status=true;
nemosim=myfile;
......@@ -507,21 +515,21 @@ bool CSnapshotSimIn::buildGadgetFile()
std::string myfile = dirname+'/'+basename+'_'+ss.str();
PRINT("CSnapshotSimIn::buildGadgetFile() myfile=["<<myfile<<"]\n";)
if (snapshot) delete snapshot;
if (snapshot) delete snapshot;
// try to open file
snapshot = new CSnapshotGadgetIn(myfile, select_part, select_time, verbose);
if (snapshot->isValidData()) { // file exist
if (snapshot->isValidData()) { // file exist
float t;
bool ok=snapshot->getData("time",&t);
if (ok && checkRangeTime(t)) { // time in range
status=true; // valid snap
stop=true; // get out loop
} else { // time out of range
delete snapshot; // del object
snapshot = NULL; // NULL for the next
nframe++; // try next frame
if (ok && checkRangeTime(t)) { // time in range
status=true; // valid snap
stop=true; // get out loop
} else { // time out of range
delete snapshot; // del object
snapshot = NULL; // NULL for the next
nframe++; // try next frame
}
}
}
else { // file does not exist
delete snapshot;
snapshot = NULL;
......@@ -541,10 +549,10 @@ bool CSnapshotSimIn::buildGadgetFile()
// -1 file does not exist
// 0 time does not exist
// 1 time found
int CSnapshotSimIn::getCod(const std::string select,
const float time, float * tcod,
const std::string base, const std::string ext)
const float time, float * tcod,
const std::string base, const std::string ext)
{
int status=-3; // sim not valid
if (valid) {
......@@ -555,40 +563,40 @@ int CSnapshotSimIn::getCod(const std::string select,
status = 0;
fi.open(codfile.c_str(),std::ios::in);
if (! fi.is_open()) {
std::cerr << "Unable to open file ["<<codfile<<"] for reading...\n";
status = -2;
std::cerr << "Unable to open file ["<<codfile<<"] for reading...\n";
status = -2;
}
else {
bool stop=false;
while (!stop && ! fi.eof()) { // while ! eof
status = 0; // time not match
std::string line;
getline(fi,line); // read line by line
if ( ! fi.eof()) {
std::istringstream str(line); // stream line
std::string parse;
int cpt=0;
// get time
str >> parse; // read time
std::stringstream str2; // convert to stream
str2 << parse;
str2 >> tcod[cpt++]; // convert to float
if (tcod[0]-0.00001 < time && tcod[0]+0.00001 > time) {
while ( str >> parse && // something to read
parse[0] != '#' && // not commented out
parse[0] != '!' // not commented out
) {
assert(cpt < 7);
std::stringstream str2(parse); // read cod data
str2 >> tcod[cpt++]; // store in float array
} // while str >> ...
assert(cpt==7); // bc cpt+1
status=1; // match cod time
stop=true; // we can stop so read
} // if (tcod[0]-0.00
} // !fi.eof
} // while !stop....
} // else
bool stop=false;
while (!stop && ! fi.eof()) { // while ! eof
status = 0; // time not match
std::string line;
getline(fi,line); // read line by line
if ( ! fi.eof()) {
std::istringstream str(line); // stream line
std::string parse;
int cpt=0;
// get time
str >> parse; // read time
std::stringstream str2; // convert to stream
str2 << parse;
str2 >> tcod[cpt++]; // convert to float
if (tcod[0]-0.00001 < time && tcod[0]+0.00001 > time) {
while ( str >> parse && // something to read
parse[0] != '#' && // not commented out
parse[0] != '!' // not commented out
) {
assert(cpt < 7);
std::stringstream str2(parse); // read cod data
str2 >> tcod[cpt++]; // store in float array
} // while str >> ...
assert(cpt==7); // bc cpt+1
status=1; // match cod time
stop=true; // we can stop so read
} // if (tcod[0]-0.00
} // !fi.eof
} // while !stop....
} // else
fi.close(); // close cod file
} else { // cod file does not exist
status=-1;
......
......@@ -208,7 +208,7 @@ int main(int argc, char ** argv )
uns->snapshot->getData("time",&time);
std::cout << "Nbody selected = " << nbody << "\nTime="<<time <<"\n";
if (file_structure=="range") {
if (0 && file_structure=="range") {
displayInfo(display,maxlines,"all",uns);
} else {
displayInfo(display,maxlines,"gas" ,uns);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment