uns.cc 14.2 KB
Newer Older
jcl's avatar
jcl committed
1
// ============================================================================
jclamber's avatar
jclamber committed
2 3 4 5
// Copyright Jean-Charles LAMBERT - 2008-2014
//           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 14 15 16 17 18 19
// ============================================================================

#include "uns.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <assert.h>
#include "snapshotinterface.h"
#include "snapshotgadget.h"
20
#include "snapshotramses.h"
jcl's avatar
jcl committed
21 22 23 24 25 26 27 28 29 30
#include "snapshotnemo.h"
#include "snapshotsim.h"
#include "snapshotlist.h"
#include "userselection.h"
#include "ctools.h"

#define DEBUG 0
#include "unsdebug.h"

namespace uns {
jclamber's avatar
jclamber committed
31 32 33
// static variable to store DATA string
std::map<std::string, StringData> CunsOut::s_mapStringValues;
std::map<std::string, int> CunsIn::s_mapCompInt;
jclamber's avatar
jclamber committed
34

jclamber's avatar
jclamber committed
35 36 37 38 39
// ----------------------------------------------------------------------------
// READING OPERATIONS
CunsIn::CunsIn(const std::string _name ,const std::string _comp ,const std::string _time, const bool verb)
{
  init(_name,_comp,_time,verb);
jclamber's avatar
jclamber committed
40 41 42
  if (verbose) {
    std::cerr << "CunsIn::CunsIn -- UNSIO version = "<<uns::getVersion()<< "\n";
  }
jclamber's avatar
jclamber committed
43
}
jcl's avatar
jcl committed
44

jclamber's avatar
jclamber committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
// ----------------------------------------------------------------------------
// constructor for READING operations
CunsIn::CunsIn(const char * _name ,const char * _comp, const char * _time,
               const bool verb)
{
  init(_name,_comp,_time,verb);
}
// ----------------------------------------------------------------------------
//
void CunsIn::init(const std::string _name ,const std::string _comp ,const std::string _time, const bool verb )
{
  valid = false;
  simname  = tools::Ctools::fixFortran(_name.c_str(),false);
  sel_comp = tools::Ctools::fixFortran(_comp.c_str(),false);
  sel_time = tools::Ctools::fixFortran(_time.c_str(),false);
jclamber's avatar
jclamber committed
60

jclamber's avatar
jclamber committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
  // initialise some maps
  CunsIn::s_mapCompInt["gas"        ] = 0;
  CunsIn::s_mapCompInt["halo"       ] = 1;
  CunsIn::s_mapCompInt["dm"         ] = 1;
  CunsIn::s_mapCompInt["disk"       ] = 2;
  CunsIn::s_mapCompInt["bulge"      ] = 3;
  CunsIn::s_mapCompInt["stars"      ] = 4;
  CunsIn::s_mapCompInt["bndry"      ] = 5;
  CunsIn::s_mapCompInt["all"        ] =-1;

  // to lower
  //simname  = tools::Ctools::tolower(simname);
  //sel_comp = tools::Ctools::tolower(sel_comp);
  //sel_time = tools::Ctools::tolower(sel_time);

  verbose=verb;
  snapshot = NULL;
  PRINT("name    ["<< simname  <<"]\n");
  PRINT("sel_comp["<< sel_comp <<"]\n");
  PRINT("sel_time["<< sel_time <<"]\n");
  CunsOut::initializeStringMap(verbose);
  if (simname == "-") { // we assume here that "-"
    tryNemo();          // is standard input and a NEMO stream...
  } else {
    if (tools::Ctools::isFileExist(simname)   && // file exist
        ! tools::Ctools::isDirectory(simname)) { // not a directory
      tryGadget();               // try gadget
      if (!valid) {              // gadget failed
        tryRamses();             // try ramses
jcl's avatar
jcl committed
90
      }
jclamber's avatar
jclamber committed
91 92 93 94 95 96 97 98
      if (!valid) {              // gadget failed
        tryNemo();               // try nemo
      }
      if (!valid) {              // nemo
        trySnapList();           // try snapshotlist
      }
      if (!valid) {
        trySimDB();              // try DataBase
jcl's avatar
jcl committed
99 100
      }
    }
jclamber's avatar
jclamber committed
101 102 103 104 105
    else {                       // file does not exist
      tryGadget();               // try gadget parallel output
      if (!valid) {
        trySimDB();              // try DataBase
      }
106
    }
jcl's avatar
jcl committed
107
  }
jclamber's avatar
jclamber committed
108 109 110
  if (valid && verb) {
    std::cerr << "File      : " << snapshot->getFileName() << "\n";
    std::cerr << "Interface : " << snapshot->getInterfaceType() << "\n";
111
  }
jclamber's avatar
jclamber committed
112 113
  if (!valid){
    std::cerr << "\nFile ["<< snapshot->getFileName() <<"], unknown UNS file format, aborting.....\n\n";
jcl's avatar
jcl committed
114
  }
jclamber's avatar
jclamber committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
}
// ----------------------------------------------------------------------------
// destructor for READING operations
CunsIn::~CunsIn()
{
  if (snapshot) delete snapshot;
}

// ----------------------------------------------------------------------------
// tryGadget
void CunsIn::tryGadget()
{
  PRINT("tryGadget("<<simname<<")\n");
  snapshot = new CSnapshotGadgetIn(simname, sel_comp, sel_time, verbose);
  valid = snapshot->isValidData();
}
// ----------------------------------------------------------------------------
// tryRamses
void CunsIn::tryRamses()
{
  PRINT("tryRamses("<<simname<<")\n");
  snapshot = new CSnapshotRamsesIn(simname, sel_comp, sel_time, verbose);
  valid = snapshot->isValidData();
}
// ----------------------------------------------------------------------------
// tryNemo
void CunsIn::tryNemo()
{
  PRINT("tryNemo("<<simname<<")\n");
  snapshot = new CSnapshotNemoIn(simname, sel_comp, sel_time, verbose);
  valid = snapshot->isValidData();
}
// ----------------------------------------------------------------------------
// trySim
void CunsIn::trySimDB()
{
jcl's avatar
jcl committed
151
#ifndef NOSQLITE3  
jclamber's avatar
jclamber committed
152 153 154 155 156
  snapshot = new CSnapshotSimIn(simname, sel_comp, sel_time, verbose);
  valid = snapshot->isValidData();
  if (valid && verbose) {
    std::cerr << "CunsIn::trySimDB() It's recorded to sqlite3 database...\n";
  }
jcl's avatar
jcl committed
157
#else
jclamber's avatar
jclamber committed
158
  valid = false;
jcl's avatar
jcl committed
159
#endif
jclamber's avatar
jclamber committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
}
// ----------------------------------------------------------------------------
// trySnapList
void CunsIn::trySnapList()
{
  snapshot = new CSnapshotList(simname, sel_comp, sel_time, verbose);
  valid = snapshot->isValidData();
}
// ----------------------------------------------------------------------------
// nextFrame
int CunsIn::nextFrame(const char *  _bits) {
  std::string bits(_bits);
  int ok=snapshot->nextFrame(bits);
  return ok;
}
// ----------------------------------------------------------------------------
// getData
bool CunsIn::getData(const std::string  comp,const std::string  prop,
                     int * size,float ** farray) {
  float * data=NULL;
  int nbody=0;
  *size=0;
  bool ok=snapshot->getData(comp,prop,&nbody,&data);
  if (ok) {
    int dim=1;
    if (prop=="pos" || prop == "vel" || prop == "acc") dim=3;
    *farray = data;
    *size=nbody*dim;
jcl's avatar
jcl committed
188
  }
jclamber's avatar
jclamber committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
  return ok;
}
// ----------------------------------------------------------------------------
// getData
bool CunsIn::getData(const std::string  prop,
                     int * size,float ** farray) {
  float * data=NULL;
  int nbody=0;
  *size=0;
  bool ok=snapshot->getData(prop,&nbody,&data);
  if (ok) {
    int dim=1;
    if (prop=="pos" || prop == "vel" || prop == "acc") dim=3;
    *farray = data;
    *size=nbody*dim;
jclamber's avatar
jclamber committed
204
  }
jclamber's avatar
jclamber committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
  return ok;
}
// ----------------------------------------------------------------------------
// getData
bool CunsIn::getData(const std::string  prop,float * fvalue) {
  bool ok=snapshot->getData(prop,fvalue);
  return ok;
}
// ----------------------------------------------------------------------------
// getData
// int
bool CunsIn::getData(const std::string  comp,const std::string  prop,
                     int * size,int ** iarray) {
  int * data=NULL;
  int nbody=0;
  *size=0;
  bool ok=snapshot->getData(comp,prop,&nbody,&data);
  if (ok) {
    int dim=1;
    *iarray = data;
    *size=nbody*dim;
jclamber's avatar
jclamber committed
226
  }
jclamber's avatar
jclamber committed
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
  return ok;
}
// ----------------------------------------------------------------------------
// getData
// int
bool CunsIn::getData(const std::string  prop,
                     int * size,int ** iarray) {
  int * data=NULL;
  int nbody=0;
  *size=0;
  bool ok=snapshot->getData(prop,&nbody,&data);
  if (ok) {
    int dim=1;
    *iarray = data;
    *size=nbody*dim;
jclamber's avatar
jclamber committed
242
  }
jclamber's avatar
jclamber committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
  return ok;
}
// ----------------------------------------------------------------------------
// getData
// int
bool CunsIn::getData(const std::string  prop,int * ivalue) {
  bool ok=snapshot->getData(prop,ivalue);
  return ok;
}




// ----------------------------------------------------------------------------
// WRITING OPERATIONS
//  ---------------------------------------------------------------------------
// constructor
CunsOut::CunsOut(const std::string _name, const std::string _type, const bool _verb )
{
  simname  = tools::Ctools::fixFortran(_name.c_str(),false);
  simtype  = tools::Ctools::fixFortran(_type.c_str(),false);
  verbose = _verb;
  snapshot= NULL;
jclamber's avatar
jclamber committed
266 267 268
  if (verbose) {
    std::cerr << "CunsOut::CunsOut -- UNSIO version = "<<uns::getVersion()<< "\n";
  }
jclamber's avatar
jclamber committed
269 270 271 272 273 274 275
  initializeStringMap(verbose);
  simtype = tools::Ctools::tolower(simtype);
  if (simtype == "gadget2") {
    snapshot = new CSnapshotGadgetOut(simname,simtype,verbose);
  } else {
    if (simtype == "nemo") {
      snapshot = new CSnapshotNemoOut(simname,simtype,verbose);
jclamber's avatar
jclamber committed
276
    }
jclamber's avatar
jclamber committed
277 278 279 280
    else {
      std::cerr << "Unkonwn UNS output file format => ["<<simtype<<"]"
                << " aborting program...... \n\n";
      std::exit(1);
jcl's avatar
jcl committed
281 282
    }
  }
jclamber's avatar
jclamber committed
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
}
// ----------------------------------------------------------------------------
// destructor for READING operations
CunsOut::~CunsOut()
{
  if (snapshot) delete snapshot;
}
// ----------------------------------------------------------------------------
// setData comp prop farray
int CunsOut::setData(const std::string  comp,const std::string  prop,
            int  size,float * farray, const bool _addr) {
  int status = snapshot->setData(comp,prop,size,farray,_addr);
  return status;
}
// ----------------------------------------------------------------------------
// setData prop farray
int CunsOut::setData(const std::string  prop,
            int  size,float * farray, const bool _addr) {
  int status = snapshot->setData(prop,size,farray,_addr);
  return status;
}
// ----------------------------------------------------------------------------
// setData prop fvalue
int CunsOut::setData(const std::string  prop,float fvalue) {
  int status = snapshot->setData(prop,fvalue);
  return status;
}
// ----------------------------------------------------------------------------
// setData comp prop iarray
int CunsOut::setData(const std::string  comp,const std::string  prop,
                     int  size,int * iarray, const bool _addr) {
  int status = snapshot->setData(comp,prop,size,iarray,_addr);
  return status;
}
// ----------------------------------------------------------------------------
// setData prop iarray
int CunsOut::setData(const std::string  prop,
                     int  size,int * iarray, const bool _addr) {
  int status = snapshot->setData(prop,size,iarray,_addr);
  return status;
}
// ----------------------------------------------------------------------------
// setData prop ivalue
int CunsOut::setData(const std::string  prop,int ivalue) {
  int status = snapshot->setData(prop,ivalue);
  return status;
}
// ----------------------------------------------------------------------------
// setData
int CunsOut::save()
{
  return snapshot->save();
}
// ----------------------------------------------------------------------------
// initializeStringMap
void  CunsOut::initializeStringMap(const bool verbose)
{

  CunsOut::s_mapStringValues["time"       ] = uns::Time;
  CunsOut::s_mapStringValues["redshift"   ] = uns::Redshift;
  CunsOut::s_mapStringValues["pos"        ] = uns::Pos;
  CunsOut::s_mapStringValues["vel"        ] = uns::Vel;
  CunsOut::s_mapStringValues["mass"       ] = uns::Mass;
  CunsOut::s_mapStringValues["id"         ] = uns::Id;
  CunsOut::s_mapStringValues["rho"        ] = uns::Rho;
  CunsOut::s_mapStringValues["hsml"       ] = uns::Hsml;
  CunsOut::s_mapStringValues["u"          ] = uns::U;
  CunsOut::s_mapStringValues["aux"        ] = uns::Aux;
  CunsOut::s_mapStringValues["acc"        ] = uns::Acc;
  CunsOut::s_mapStringValues["pot"        ] = uns::Pot;
  CunsOut::s_mapStringValues["eps"        ] = uns::Eps;
  CunsOut::s_mapStringValues["keys"       ] = uns::Keys;
  CunsOut::s_mapStringValues["age"        ] = uns::Age;
  CunsOut::s_mapStringValues["temp"       ] = uns::Temp;
  CunsOut::s_mapStringValues["metal"      ] = uns::Metal;
  CunsOut::s_mapStringValues["gas_metal"  ] = uns::GasMetal;
  CunsOut::s_mapStringValues["stars_metal"] = uns::StarsMetal;
  CunsOut::s_mapStringValues["nsel"       ] = uns::Nsel;
  CunsOut::s_mapStringValues["nbody"      ] = uns::Nbody;
  CunsOut::s_mapStringValues["ngas"       ] = uns::Ngas;
  CunsOut::s_mapStringValues["nhalo"      ] = uns::Nhalo;
  CunsOut::s_mapStringValues["ndisk"      ] = uns::Ndisk;
  CunsOut::s_mapStringValues["nbulge"     ] = uns::Nbulge;
  CunsOut::s_mapStringValues["nstars"     ] = uns::Nstars;
  CunsOut::s_mapStringValues["nbndry"     ] = uns::Nbndry;
  CunsOut::s_mapStringValues["gas"        ] = uns::Gas;
  CunsOut::s_mapStringValues["halo"       ] = uns::Halo;
  CunsOut::s_mapStringValues["dm"         ] = uns::Halo;
  CunsOut::s_mapStringValues["ndm"        ] = uns::Halo;
  CunsOut::s_mapStringValues["bulge"      ] = uns::Bulge;
  CunsOut::s_mapStringValues["disk"       ] = uns::Disk;
  CunsOut::s_mapStringValues["stars"      ] = uns::Stars;
  CunsOut::s_mapStringValues["bndry"      ] = uns::Bndry;
  CunsOut::s_mapStringValues["all"        ] = uns::All;
  CunsOut::s_mapStringValues["gas_mpv"    ] = uns::GasMPV;
  CunsOut::s_mapStringValues["halo_mpv"   ] = uns::HaloMPV;
  CunsOut::s_mapStringValues["bulge_mpv"  ] = uns::BulgeMPV;
  CunsOut::s_mapStringValues["disk_mpv"   ] = uns::DiskMPV;
  CunsOut::s_mapStringValues["stars_mpv"  ] = uns::StarsMPV;
  CunsOut::s_mapStringValues["bndry_mpv"  ] = uns::BndryMPV;
  CunsOut::s_mapStringValues["zs"         ] = uns::Zs;
  CunsOut::s_mapStringValues["zsmt"       ] = uns::ZSMT;
  CunsOut::s_mapStringValues["im"         ] = uns::Im;
  CunsOut::s_mapStringValues["ssl"        ] = uns::Ssl;
  CunsOut::s_mapStringValues["cm"         ] = uns::Cm;
  CunsOut::s_mapStringValues["czs"        ] = uns::Czs;
  CunsOut::s_mapStringValues["czsmt"      ] = uns::Czsmt;
  if (verbose) {
    std::cout << "CunsOut::initializeStringMap s_mapStringValues contains "
              << CunsOut::s_mapStringValues.size() << " entries." << std::endl;
jcl's avatar
jcl committed
393
  }
jclamber's avatar
jclamber committed
394 395 396
}


jcl's avatar
jcl committed
397 398 399 400
}
// ---------------------------------------------------------------------------- 
// End of file                                                                 
// ----------------------------------------------------------------------------