snapshotsim.cc 25.5 KB
Newer Older
jcl's avatar
jcl committed
1
// ============================================================================
2
// Copyright Jean-Charles LAMBERT - 2008-2016
jclamber's avatar
jclamber committed
3 4 5
//           Centre de donneeS Astrophysiques de Marseille (CeSAM)          
// e-mail:   Jean-Charles.Lambert@lam.fr                                      
// address:  Aix Marseille Universite, CNRS, LAM 
jcl's avatar
jcl committed
6 7 8 9
//           Laboratoire d'Astrophysique de Marseille                          
//           Pole de l'Etoile, site de Chateau-Gombert                         
//           38, rue Frederic Joliot-Curie                                     
//           13388 Marseille cedex 13 France                                   
jclamber's avatar
jclamber committed
10
//           CNRS UMR 7326                                       
jcl's avatar
jcl committed
11 12 13
// ============================================================================

/* 
jclamber's avatar
jclamber committed
14
  @author Jean-Charles Lambert <Jean-Charles.Lambert@lam.fr>
jcl's avatar
jcl committed
15 16 17 18
 */
#ifndef NOSQLITE3  // do not compite if no sqlite3 lib
#include "snapshotsim.h"
#include "snapshotgadget.h"
19
#include "snapshotgadgeth5.h"
jcl's avatar
jcl committed
20
#include "snapshotnemo.h"
jclamber's avatar
jclamber committed
21
#include "snapshotramses.h"
jcl's avatar
jcl committed
22 23 24 25 26
#include "ctools.h"
#include <sstream>
#include <iomanip>
#include <iostream>

27 28 29
#include <cstdio>
#include <cstdlib>

jcl's avatar
jcl committed
30 31 32 33 34
#define DEBUG 0
#include "unsdebug.h"
#include "uns.h"

namespace uns {
35
// ASCII database
36 37 38
template <class T>  std::string uns::CSnapshotInterfaceIn<T>::sim_db_file="/pil/programs/DB/sim_info.txt";
template <class T>  std::string uns::CSnapshotInterfaceIn<T>::nemo_range_file="/pil/programs/DB/nemo_range.txt";
template <class T>  std::string uns::CSnapshotInterfaceIn<T>::eps_db_file="/pil/programs/DB/sim_eps.txt";
39 40
// SQLITE database

jcl's avatar
jcl committed
41 42
// ----------------------------------------------------------------------------
// constructor
43
template <class T> CSnapshotSimIn<T>::CSnapshotSimIn(const std::string _name,
44 45 46
                               const std::string _comp,
                               const std::string _time,
                               const bool        verb)
47
  :CSnapshotInterfaceIn<T>(_name, _comp, _time, verb)
jcl's avatar
jcl committed
48 49 50 51 52
{
  snapshot = NULL;
  sql      = NULL;
  nframe   = 0;   // # frames read
  nemosim  = "";
53 54
  this->verbose  = verb;
  sim_filename = this->filename;
55 56 57
  sel_from_index = false;
  force_stop = false;
  checkSimIndex(); // check if index has been specified with simname
jcl's avatar
jcl committed
58
  if (0) {
59
    this->valid=openDbFile();
jcl's avatar
jcl committed
60
  } else { // SQLite3
61
    this->valid=openSqlDb();
jcl's avatar
jcl committed
62 63 64 65
  }
}
// ----------------------------------------------------------------------------
// constructor
66
template <class T> CSnapshotSimIn<T>::~CSnapshotSimIn()
jcl's avatar
jcl committed
67 68 69 70 71 72
{
  if (snapshot) delete snapshot;
  if (sql) delete sql;
}
// ============================================================================
// getSnapshotRange                                                            
73
template <class T> ComponentRangeVector * CSnapshotSimIn<T>::getSnapshotRange()
jcl's avatar
jcl committed
74 75 76
{
  assert(snapshot != NULL);
  assert(snapshot->isValidData());
77
  if ((tools::Ctools::tolower(this->interface_type) == "nemo") && nemosim != "" && crv.size()>0) {
jcl's avatar
jcl committed
78
    return &crv;
79
  }
jcl's avatar
jcl committed
80 81 82 83 84 85
  else {
    return snapshot->getSnapshotRange();
  }
}
// ============================================================================
// getSnapshotRange 
86
template <class T> int CSnapshotSimIn<T>::nextFrame(uns::UserSelection &user_select)
jcl's avatar
jcl committed
87 88 89
{
  assert(snapshot != NULL);
  assert(snapshot->isValidData()==true);
90
  snapshot->setNsel(this->nsel);
jcl's avatar
jcl committed
91 92 93 94
  return (snapshot->nextFrame(user_select));
}
// ============================================================================
// nextFrame 
95
template <class T> int CSnapshotSimIn<T>::nextFrameSelect(ComponentRangeVector * crvs)
jcl's avatar
jcl committed
96
{
97 98 99
  snapshot->user_select.setSelection(this->getSelectPart(),crvs);
  this->setNsel(snapshot->user_select.getNSel());
  snapshot->setReqBits(this->req_bits);
jcl's avatar
jcl committed
100 101 102
  snapshot->setNsel(snapshot->user_select.getNSel());
  return(snapshot->nextFrame(snapshot->user_select));
}
103 104
// ============================================================================
// checkSimIndex
105
template <class T> bool CSnapshotSimIn<T>::checkSimIndex()
106 107 108 109 110 111 112
{
  std::size_t found = sim_filename.find("%");
  if (found!= std::string::npos) { // % found
    std::string name=sim_filename.substr(0,found);
    std::string index=sim_filename.substr(found+1,sim_filename.length()-1);
    std::istringstream s_nframe(index);
    s_nframe >> nframe; // convert string to int
113
    if (this->verbose) {
114 115 116 117 118 119 120 121
      std::cerr << "checkSimIndex name =" << name << " index=" << index << " nframe="<< nframe <<"\n";
    }
    sim_filename=name; // set a correct name for the simulation
    sel_from_index = true;
  }
  return sel_from_index;
}

jcl's avatar
jcl committed
122 123 124 125 126 127
//                     - - - - - - - - - - - - - - 
//                           SQlite database       
//                     - - - - - - - - - - - - - - 

// ============================================================================
// openSqlDb                                                                   
128
template <class T> bool CSnapshotSimIn<T>::openSqlDb(std::string db)
jcl's avatar
jcl committed
129 130
{
  sqlite_db = db;
131
  std::string mydbname=this->parseConfig("dbname");
132 133
  if (mydbname != "" ) {
    sqlite_db = mydbname;
134
  }
135
  if (this->verbose) {
136
    std::cerr << "Using sqlite3 database file [" << sqlite_db << "]\n";
137
  }
jcl's avatar
jcl committed
138 139 140
  sql = new jclt::CSQLite3(sqlite_db);
  bool status=sql->isOpen();
  if (! status) {
141
    std::cerr << "Unable to load sqlite3 database file [" << sqlite_db << "]\n";
jcl's avatar
jcl committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155
    //std::cerr << __FILE__<< " " << __LINE__ << "Aborting ....\n";
    //std::exit(1);
  } else {
    status = findSqlSim();
    if (status) {
      eps_exist = readSqlEps();
    } else {
      eps_exist = false;
    }
  }
  return status;
}
// ============================================================================
// findSqlSim                                                                  
156
template <class T> bool CSnapshotSimIn<T>::findSqlSim()
jcl's avatar
jcl committed
157
{
158
  std::string select="select * from info where name='"+sim_filename+"'";
159
  if (this->verbose) std::cerr << "select = "<<select <<"\n";
jcl's avatar
jcl committed
160 161
  int status = sql->exe(select);
  if (status) {
162
    if (this->verbose) sql->display();
163
    assert(sql->vdata[0]==sim_filename);
jcl's avatar
jcl committed
164 165 166 167 168
    simname        = sql->vdata[0];
    simtype        = sql->vdata[1];
    dirname        = sql->vdata[2];
    basename       = sql->vdata[3];
    
169 170 171
    this->interface_type = simtype;
#if 0  // off since march/07/2016
    if (tools::Ctools::tolower(this->interface_type) == "gadget") this->interface_index=1;
jcl's avatar
jcl committed
172
    else
173
      if (tools::Ctools::tolower(this->interface_type) == "nemo") this->interface_index=0;
jclamber's avatar
jclamber committed
174
      else
175
        if (tools::Ctools::tolower(this->interface_type) == "ramses") this->interface_index=2;
jclamber's avatar
jclamber committed
176
        else {
177
          std::cerr <<"CSnapshotSimIn<T>::findSqlSim => Unknown interface type....\n";
jclamber's avatar
jclamber committed
178
        }
179
#endif
jcl's avatar
jcl committed
180 181 182 183 184
  }
  return status;
}
// ============================================================================
// readSqlEps                                                                  
185
template <class T> bool CSnapshotSimIn<T>::readSqlEps()
jcl's avatar
jcl committed
186
{
187
  std::string select="select * from eps where name='"+sim_filename+"'";
188
  if (this->verbose) std::cerr << "select = "<<select <<"\n";
jcl's avatar
jcl committed
189 190
  int status = sql->exe(select);
  if (status) {
191
    if (this->verbose) sql->display();
192
    assert(sql->vdata[0]==sim_filename);
jcl's avatar
jcl committed
193 194 195
    std::stringstream str;
    for (unsigned int i=1; i< sql->vdata.size(); i++) {
      str << sql->vdata[i];  // convert string to stream string
196
      str >> this->eps[i-1];       // convert to float
jcl's avatar
jcl committed
197 198 199 200 201 202 203
    }
  }
  return status;
}
// ============================================================================
// fillSqlNemoRange                                                            
// look for simulation in Sql Database file                                    
204
template <class T> bool CSnapshotSimIn<T>::fillSqlNemoRange()
jcl's avatar
jcl committed
205
{
206
  std::string select="select * from nemorange where name='"+sim_filename+"'";
207
  if (this->verbose) std::cerr << "select = "<<select <<"\n";
jcl's avatar
jcl committed
208 209
  int status = sql->exe(select);
  if (status) {
210
    if (this->verbose) sql->display();
jcl's avatar
jcl committed
211
    int offset=0;
212
    assert(sql->vdata[0]==sim_filename);
213 214
    addNemoComponent(offset,sql->vdata[1],"all"  );
    addNemoComponent(offset,sql->vdata[2],"disk" );
jcl's avatar
jcl committed
215
    addNemoComponent(offset,sql->vdata[3],"bulge");
216
    addNemoComponent(offset,sql->vdata[4],"halo" );
jcl's avatar
jcl committed
217
    addNemoComponent(offset,sql->vdata[5],"halo2");
218 219 220
    addNemoComponent(offset,sql->vdata[6],"gas"  );
    addNemoComponent(offset,sql->vdata[7],"bndry");
    addNemoComponent(offset,sql->vdata[8],"stars");
jcl's avatar
jcl committed
221 222 223 224 225 226 227 228 229
  }
  return status;
}
//                       - - - - - - - - - - - - - - 
//                             ASCII database        
//                       - - - - - - - - - - - - - - 

// ============================================================================
// opendbFile                                                                  
230
template <class T> bool CSnapshotSimIn<T>::openDbFile()
jcl's avatar
jcl committed
231 232
{
  bool status=true;
233
  fi.open(this->sim_db_file.c_str(),std::ios::in);
jcl's avatar
jcl committed
234
  if (! fi.is_open()) {
235
    std::cerr << "Unable to open file ["<<sim_filename<<"] for reading, aborting...\n";
jcl's avatar
jcl committed
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
    status = false;
  }
  if (status) {
    status = findSim();
    if (status) {
      eps_exist = readEpsFile();
    } else {
      eps_exist = false;
    }
  }
  return status;
}
// ============================================================================
// findSim                                                                     
// look for simulation in Database file                                        
251
template <class T> bool CSnapshotSimIn<T>::findSim()
jcl's avatar
jcl committed
252 253 254 255 256 257 258 259 260
{
  bool status=false;
  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;
261
      // following loop parse each lines previously read
jcl's avatar
jcl committed
262 263
      //
      int cpt=0;
264 265 266 267 268 269 270 271 272 273 274 275
      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;
276 277 278 279 280 281 282
          this->interface_type = simtype;
//          if (this->interface_type == "Gadget") this->interface_index=1;
//          else
//            if (this->interface_type == "Nemo") this->interface_index=0;
//            else {
//              std::cerr <<"CSnapshotSimIn<T>::findSim => Unknown interface type....\n";
//            }
283 284 285 286 287 288 289
        }
        if (cpt==3) { // sim's dirname
          dirname=parse;
        }
        if (cpt==4) { // sim's basename
          basename=parse;
        }
jcl's avatar
jcl committed
290
      }
291
      if (simname == sim_filename) { // we found simulation
292 293 294
        stop   = true; // we have a snapshot
        status = true; // so we can stop reading
        std::cerr << "SIM DB:Found simulation ["<<simname<<"] in database !\n";
jcl's avatar
jcl committed
295 296
      }
      if (cpt != 4) {
297
        std::cerr << "\n\nWarning, bad #strings ["<<cpt<<"] parsed\n"
298
                  << "during CSnapshotSimIn<T>::findSim()....\n";
jcl's avatar
jcl committed
299 300 301 302 303 304 305 306 307 308 309 310
      }
    }
    else { // end of file
      stop   = true;
      status = false;
    }
  }
  return status;
}
// ============================================================================
// readEpsFile                                                                 
// Read Eps database file                                                      
311
template <class T> bool CSnapshotSimIn<T>::readEpsFile()
jcl's avatar
jcl committed
312 313 314 315 316 317
{
  bool stop = false;
  bool status=true;

  std::ifstream fi;
  std::string simname;
318
  fi.open(this->eps_db_file.c_str(),std::ios::in);
jcl's avatar
jcl committed
319
  if (! fi.is_open()) {
320
    std::cerr << "Warning !!! Unable to open file ["<<sim_filename<<"] for reading...\n";
jcl's avatar
jcl committed
321 322 323 324 325 326 327
    status = false;
  }
  if (status) {
    while (!stop && ! fi.eof()) {           // while ! eof
      std::string line;
      getline(fi,line); // read on eline
      if ( ! fi.eof()) {
328 329 330 331 332 333 334 335 336 337 338 339
        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;
340
            if (simname == sim_filename) { // we found simulation
341 342 343 344 345
              //stop   = true; // we have a snapshot
              status = true; // we have all components
              std::cerr << "EPS:Found simulation ["<<simname<<"] in database !\n";
            }
          }
346
          if (simname == sim_filename) { // we found simulation
347 348
            std::istringstream ss(parse);
            if (cpt < MAX_EPS+2) { //
349
              ss >> this->eps[cpt-2];    // file EPS array
350 351 352 353
            }
          }
        } // while ( str ....
        //
354
        if (simname == sim_filename) {
355 356
          stop=true;     // simulation has been found
          assert(cpt>1); // we must have read at least one eps
jcl's avatar
jcl committed
357

358 359 360 361
          // 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++) {
362 363
            std::cerr << "eps shift i="<<i<<" cpt="<<cpt<<" eps="<<this->eps[cpt-2]<<"\n";
            this->eps[i] = this->eps[cpt-2];
364 365
          }
        }
jcl's avatar
jcl committed
366 367
      } // if !eof ...
      else { // end of file
368 369
        stop   = true;
        status = false;
jcl's avatar
jcl committed
370 371 372 373
      }
    } // while (!stop ...
  } // if (status....
  if (! status) {
374
    std::cerr<<"\n\nWARNING, simulation ["<<sim_filename<<"] has no entry in the"
375
            <<"EPS datafile ["<<uns::CSnapshotInterfaceIn<T>::eps_db_file<<"]\n\n";
jcl's avatar
jcl committed
376 377 378 379 380 381 382 383
  }
  return status;
}

// ============================================================================
// getEps                                                                      
// return the component according to the component requested                   
// if eps does not exist return -1                                             
384
template <class T> T CSnapshotSimIn<T>::getEps(const std::string comp)
jcl's avatar
jcl committed
385
{
386
  T status=-1.0;
jcl's avatar
jcl committed
387
  if (eps_exist) {
388 389 390 391 392
    if (comp == "gas"  ) status=this->eps[0];
    if (comp == "halo" ) status=this->eps[1];
    if (comp == "disk" ) status=this->eps[2];
    if (comp == "bulge") status=this->eps[3];
    if (comp == "stars") status=this->eps[4];
jcl's avatar
jcl committed
393 394 395 396 397 398 399
  }
  //std::cerr << "comp ="<<comp<<" status="<<status<<" eps_exist="<<eps_exist<<"\n";
  return status;
}
// ============================================================================
// fillNemoRange                                                               
// look for simulation in Database file                                        
400
template <class T> bool CSnapshotSimIn<T>::fillNemoRange()
jcl's avatar
jcl committed
401 402 403 404 405 406
{
  bool stop = false;
  bool status=true;

  std::ifstream fi;
  int offset;
407
  fi.open(this->nemo_range_file.c_str(),std::ios::in);
jcl's avatar
jcl committed
408
  if (! fi.is_open()) {
409
    std::cerr << "Unable to open file ["<<sim_filename<<"] for reading, aborting...\n";
jcl's avatar
jcl committed
410 411 412 413 414 415 416
    status = false;
  }
  if (status) {
    while (!stop && ! fi.eof()) {           // while ! eof
      std::string line;
      getline(fi,line); // read on eline
      if ( ! fi.eof()) {
417 418 419 420 421 422 423 424 425 426 427 428
        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;
429
            if (simname == sim_filename) { // we found simulation
430 431 432 433 434 435 436
              stop   = true; // we have a snapshot
              status = true; // we have all components
              std::cerr << "Found simulation ["<<simname<<"] in database !\n";
              crv.clear();
              offset=0;
            }
          }
437
          if (simname == sim_filename) { // we found simulation
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
            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 ....
jcl's avatar
jcl committed
455 456
      } // if !eof ...
      else { // end of file
457 458
        stop   = true;
        status = false;
jcl's avatar
jcl committed
459 460 461 462 463 464 465 466
      }
    } // while (!stop ...
  } // if (status....

  return status;
}
// ============================================================================
// addNemoComponent                                                            
467
template <class T> int CSnapshotSimIn<T>::addNemoComponent(int& offset, std::string parse,
468
                                     std::string comp )
jcl's avatar
jcl committed
469
{
470
#if 0
jcl's avatar
jcl committed
471 472 473 474 475 476 477 478 479 480 481 482
  int nbody;
  std::istringstream ss(parse);
  ss >> nbody;
  if (nbody) {
    uns::ComponentRange cr;
    cr.setData(offset,nbody-1,comp);
    crv.push_back(cr);
    if (comp!="all") {
      offset+=nbody;
    }
  }
  return offset;
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
#else
  if (parse!="") { // there is something for the component
    std::size_t found = parse.find(":");

    std::istringstream ss;
    //std::cerr << "substr=[" << parse.substr(0,found-1) << "]\n";
    ss.str(parse.substr(0,found));
    int start;
    ss >> start;
    ss.clear();
    ss.str(parse.substr(found+1));
    int end;
    ss >> end;
    uns::ComponentRange cr;
    //std::cerr << "parse="<< parse << " start="<< start << " end="<< end << " comp="<<comp << "\n";
    cr.setData(start,end,comp);
    crv.push_back(cr);
  }
  return 1;
#endif
jcl's avatar
jcl committed
503 504 505
}
// ============================================================================
// isNewFrame                                                                  
506
template <class T> bool CSnapshotSimIn<T>::isNewFrame()
jcl's avatar
jcl committed
507 508
{
  bool status=false;
509
  if (this->valid) {
510 511 512
    if (tools::Ctools::tolower(simtype)=="gadget" ||
        tools::Ctools::tolower(simtype)=="gadget3") {
      status=buildAllGadgetFile();
jcl's avatar
jcl committed
513 514
    }
    else {
jclamber's avatar
jclamber committed
515
      if (tools::Ctools::tolower(simtype)=="nemo") {
516
        status=buildNemoFile();
jcl's avatar
jcl committed
517 518
      }
      else {
jclamber's avatar
jclamber committed
519 520 521
        if (tools::Ctools::tolower(simtype)=="ramses") {
          status=buildRamsesFile();
        }
522 523
        else {          
          std::cerr <<"\nUnknown simulation type ["<<simtype<<"]\n";
jclamber's avatar
jclamber committed
524
        }
jcl's avatar
jcl committed
525 526 527
      }
    }
    if (status) {
528 529
      this->interface_type  = snapshot->getInterfaceType();
      this->interface_index = snapshot->getInterfaceIndex();
jcl's avatar
jcl committed
530 531 532 533 534 535
    }
  }
  return status;
}
// ============================================================================
// buildNemoFile                                                             
536
template <class T> bool CSnapshotSimIn<T>::buildNemoFile()
jcl's avatar
jcl committed
537 538 539 540 541 542 543 544 545 546
{
  bool status=false;
  if (nemosim != "") {
    status = true;
  }
  else {
    std::string myfile=dirname+'/'+basename;
    if (snapshot) delete snapshot;
    if (0) { // ASCII database
      if (fillNemoRange()) {
547
        if (this->verbose) uns::ComponentRange::list(&crv);
jcl's avatar
jcl committed
548 549 550
      }
    } else {
      if (fillSqlNemoRange()) {
551
        if (this->verbose) uns::ComponentRange::list(&crv);
jcl's avatar
jcl committed
552 553 554
      }
    }
    // try to open NEMO sim
555 556
    PRINT("trying top instantiate  CSnapshotNemo("<<myfile<<") verbose="<<this->verbose<<"\n";)
        snapshot = new CSnapshotNemoIn<T>(myfile, this->select_part, this->select_time,this->verbose);
jcl's avatar
jcl committed
557 558 559 560 561 562 563 564 565
    if (snapshot->isValidData()) {
      status=true;
      nemosim=myfile;
    } else {
      status=false;
    }
  }
  return status;
}
566

jcl's avatar
jcl committed
567
// ============================================================================
568 569
// buildAllGadgetFile (gadget 1,2 or 3 format)
template <class T> bool CSnapshotSimIn<T>::buildAllGadgetFile()
570 571 572 573 574 575 576 577 578 579
{
  bool stop=false,status=false;
  int cpt=1;
  // loop on all the possibility of file
  // dirname+basename+nframe
  // ex : gas001_0 gas001_00 gas001_000
  while (!force_stop && !stop && cpt<=5) {
    std::ostringstream ss;
    ss << std::setw(cpt) << std::setfill('0') << nframe;
    std::string myfile = dirname+'/'+basename+'_'+ss.str();
580
    PRINT("CSnapshotSimIn<T>::buildGadgetFile()  myfile=["<<myfile<<"]\n";)
581

582 583 584
    if (snapshot) delete snapshot;
    // try to open Gadget1/2 file
    snapshot = new CSnapshotGadgetIn<T>(myfile, this->select_part, this->select_time, this->verbose);
585 586
    if (!snapshot->isValidData()) {
      delete snapshot;
587

588 589
      // try to open gadget3 file with ".hdf5 extension"
      snapshot = new CSnapshotGadgetH5In<T>(myfile+".hdf5", this->select_part, this->select_time, this->verbose);
jcl's avatar
jcl committed
590

591 592 593 594 595 596 597
      if (!snapshot->isValidData()) {
        delete snapshot;
        // try to open gadget3 file without ".hdf5 extension"
        snapshot = new CSnapshotGadgetH5In<T>(myfile, this->select_part, this->select_time, this->verbose);
      }

    }
598
    if (snapshot->isValidData()) {                // file exist
599
      T t;
jcl's avatar
jcl committed
600
      bool ok=snapshot->getData("time",&t);
601
      if (ok && this->checkRangeTime(t)) {              //  time in range
602 603 604 605 606 607
        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
jcl's avatar
jcl committed
608
      }
609
    }
jcl's avatar
jcl committed
610 611 612 613 614 615 616 617 618 619
    else {                                        // file does not exist
      delete snapshot;
      snapshot = NULL;
      cpt++;
    }
  }

  if (status) {
    nframe++;  // next frame index
  }
620 621 622
  if (sel_from_index) { // occurs only one time
    force_stop = true;
  }
jcl's avatar
jcl committed
623 624 625
  return status;
}
// ============================================================================
jclamber's avatar
jclamber committed
626
// buildRamsesFile
627
template <class T> bool CSnapshotSimIn<T>::buildRamsesFile()
jclamber's avatar
jclamber committed
628 629 630 631 632
{
  bool status=false;

  std::string myfile = dirname+'/'+basename;
  if (nframe==0) {
633
    snapshot = new CSnapshotRamsesIn<T>(myfile, this->select_part, this->select_time, this->verbose);
jclamber's avatar
jclamber committed
634
    if (snapshot->isValidData()) {                // file exist
635
      T t;
jclamber's avatar
jclamber committed
636
      bool ok=snapshot->getData("time",&t);
637
      if (ok && this->checkRangeTime(t)) {              //  time in range
jclamber's avatar
jclamber committed
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
        status=true;                              //   valid snap
        nframe++;
      } 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;
    }
  }
  return status;
}
// ============================================================================
jcl's avatar
jcl committed
654 655 656 657 658 659
// getCod
// returns: 
// -2 file exist but can't open
// -1 file does not exist
// 0  time does not exist
// 1  time found 
660

661
template <class T> int CSnapshotSimIn<T>::getCod(const std::string select,
662 663
                           const float time, float * tcod,
                           const std::string base, const std::string ext)
jcl's avatar
jcl committed
664 665
{
  int status=-3;  // sim not valid
666
  if (this->valid) {
jcl's avatar
jcl committed
667 668 669 670 671 672 673
    std::string codfile=dirname+'/'+base+'/'+simname+'.'+select+'.'+ext;
    if (tools::Ctools::isFileExist(codfile)) { // cod file exist
      std::cerr << "cod file = "<< codfile << "\n";
      std::ifstream fi;
      status = 0;
      fi.open(codfile.c_str(),std::ios::in);
      if (! fi.is_open()) {
674 675
        std::cerr << "Unable to open file ["<<codfile<<"] for reading...\n";
        status = -2;
jcl's avatar
jcl committed
676 677
      }
      else {
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
        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
jcl's avatar
jcl committed
708 709 710 711 712 713 714 715
      fi.close(); // close cod file
    } else { // cod file does not exist
      status=-1;
      std::cerr << "cod file = "<< codfile << " does not exist\n";
    }
  }
  return status;
}
716 717 718 719 720 721 722 723 724
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// Templates instantiation MUST be declared **AFTER** templates declaration
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// C++11
//extern template class CSnapshotSimIn<float>;
template class CSnapshotSimIn<float>;
//extern template class CSnapshotSimIn<double>;
template class CSnapshotSimIn<double>;
} // end of namespace
jcl's avatar
jcl committed
725
#endif // NOSQLITE3