snapshotlist.cc 6.36 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
// ============================================================================

/**
14
    @author Jean-Charles Lambert <Jean-Charles.Lambert@lam.fr>
jcl's avatar
jcl committed
15 16 17 18 19 20 21 22 23
 */
#include "uns.h"
#include "snapshotlist.h"
#include "ctools.h"
#include <sstream>
#include <iomanip>
#include <iostream>

namespace uns {
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

// ----------------------------------------------------------------------------
// constructor
template <class T> CSnapshotList<T>::CSnapshotList(const std::string _name,
                                                   const std::string _comp,
                                                   const std::string _time,
                                                   const bool        verb)
  :CSnapshotInterfaceIn<T>(_name, _comp, _time, verb)
{
  snapshot = NULL;
  unsin    = NULL;
  nframe   = 0;   // # frames read
  nemosim  = "";
  this->valid=openFileList();
}
// ----------------------------------------------------------------------------
// constructor
template <class T> CSnapshotList<T>::~CSnapshotList()
{
  if (unsin) delete unsin;
}
// ============================================================================
// getSnapshotRange
template <class T> int CSnapshotList<T>::nextFrame(uns::UserSelection &user_select)
{
  assert(snapshot != NULL);
  assert(snapshot->isValidData()==true);
  snapshot->setNsel(this->nsel);
  return (snapshot->nextFrame(user_select));
}
// ============================================================================
// nextFrame
template <class T> int CSnapshotList<T>::nextFrameSelect(ComponentRangeVector * crvs)
{
  snapshot->user_select.setSelection(this->getSelectPart(),crvs);
  this->setNsel(snapshot->user_select.getNSel());
  snapshot->setReqBits(this->req_bits);
  snapshot->setNsel(snapshot->user_select.getNSel());
  return(snapshot->nextFrame(snapshot->user_select));
}
// ============================================================================
// isNewFrame()
template <class T> bool CSnapshotList<T>::isNewFrame()
{
  bool stop=false;
  while (!stop && getLine()) {
    if (unsin) {
      delete unsin;
    }
    unsin = new uns::CunsIn2<T>(snapname.c_str(),this->select_part.c_str(),this->select_time.c_str(),this->verbose);
    T t;
    bool ok=unsin->snapshot->getData("time",&t);
    if (unsin->isValid() && ok && this->checkRangeTime(t)) {
      snapshot = unsin->snapshot;
      stop=true;
      this->interface_type = snapshot->getInterfaceType();
    }
jcl's avatar
jcl committed
81
  }
82 83 84 85 86 87 88 89 90 91 92
  if (!stop) this->end_of_data = true; // EOF reached
  return stop;

}
// ============================================================================
template <class T> ComponentRangeVector * CSnapshotList<T>::getSnapshotRange()
{
  assert(snapshot != NULL);
  assert(snapshot->isValidData());
  if ((tools::Ctools::tolower(simtype) == "nemo") && nemosim != "" && crv.size()>0) {
    return &crv;
jcl's avatar
jcl committed
93
  }
94 95
  else {
    return snapshot->getSnapshotRange();
jcl's avatar
jcl committed
96
  }
97 98 99 100 101 102 103 104 105 106 107 108 109
}
// ----------------------------------------------------------------------------
// openFileList
template <class T> bool CSnapshotList<T>::openFileList()
{
  bool status=false;
  if (this->filename == "-" )
    ;
  else
    fi.open(this->filename.c_str(),std::ios::in);
  if (! fi.is_open()) {
    std::cerr << "Unable to open file ["<<this->filename<<"] for reading, aborting...\n";
    status = false;
jcl's avatar
jcl committed
110
  }
111 112 113 114 115 116 117 118 119 120 121 122
  else {
    // read magic numberé
    std::string line;
    if (getLine(true)) { // read the first file
      // -----------------------------------------------
      // instantiate a new UNS input object (for reading)
      uns::CunsIn2<T> * test_data = new uns::CunsIn2<T>(snapname.c_str(),this->select_part.c_str(),this->select_time.c_str(),this->verbose);

      if (test_data->isValid()) { // it's a valid snaphot
        delete test_data;
        status = true;
        fi.seekg(0, std::ios::beg); // go back to the beginning
jcl's avatar
jcl committed
123 124 125
      }
    }
    else {
126 127
      status=false;
      fi.close();
jcl's avatar
jcl committed
128 129
    }
  }
130 131 132 133 134 135 136 137 138
  return status;
}
// ----------------------------------------------------------------------------
//
template <class T> bool CSnapshotList<T>::getLine(const bool force)
{
  bool status=false,stop=false;
  if (this->valid || force) {
    while (!stop && ! fi.eof()) {           // while ! eof
jcl's avatar
jcl committed
139
      std::string line;
140 141 142 143 144 145 146 147 148 149 150 151 152 153
      getline(fi,line);
      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
                 parse[0] != '\n'               // not a blank line
                 ) {
          cpt++;
          if (cpt==1) snapname=parse;
jcl's avatar
jcl committed
154
        }
155 156 157 158 159 160 161
        if (cpt > 0 ) {
          unsigned int i=0;
          while(i<snapname.length() && snapname[i]==' ') i++; // search first non blank
          if (i<snapname.length() && snapname[i]!='/')        // first char not a '/'
          {;}//snapname = dirpath.toStdString() + snapname;      // append to dirpath
          stop   = true; // we have a snapname
          status = true; // so we can stop reading
jcl's avatar
jcl committed
162 163
        }
      }
164 165 166 167
      else { // end of file
        stop   = true;
        status = false;
      }
jcl's avatar
jcl committed
168 169
    }
  }
170 171 172
  else status=false;
  return status;
}
173 174 175 176 177 178 179 180 181 182
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// Templates instantiation MUST be declared **AFTER** templates declaration
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// C++11
//extern template class CSnapshotList<float>;
template class CSnapshotList<float>;

//extern template class CSnapshotList<double>;
template class CSnapshotList<double>;
} // end of namespace
jcl's avatar
jcl committed
183 184 185


//