snapshotgadgeth5.h 5.35 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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
// ============================================================================
// Copyright Jean-Charles LAMBERT - 2008-2016
//           Centre de donneeS Astrophysiques de Marseille (CeSAM)
// e-mail:   Jean-Charles.Lambert@lam.fr
// address:  Aix Marseille Universite, CNRS, LAM
//           Laboratoire d'Astrophysique de Marseille
//           Pole de l'Etoile, site de Chateau-Gombert
//           38, rue Frederic Joliot-Curie
//           13388 Marseille cedex 13 France
//           CNRS UMR 7326
// ============================================================================

/**
        @author Jean-Charles Lambert <Jean-Charles.Lambert@lam.fr>
 */

#ifndef SNAPSHOTGADGETH5_H
#define SNAPSHOTGADGETH5_H

#include <string>
#include <assert.h>
#include <map>
#include <iostream>
#include <sstream>
#include "snapshotinterface.h"
#include <H5Cpp.h>
#ifndef H5_NO_NAMESPACE
using namespace H5;
#endif
namespace uns {
// HDF5 gadget header
typedef struct h5_header {
  std::vector<double> MassTable; //[6]; "
  double   Time;
  double   Redshift;
  int      Flag_DoublePrecision;
  int      Flag_IC_Info;
  int      Flag_Metals;
  int      Flag_Cooling;
  int      Flag_Sfr;
  int      Flag_StellarAge;
  int      flag_feedback;
  std::vector<int> NumPart_Total;//[6];
  std::vector<int> NumPart_Total_HighWord;//[6];
  std::vector<int> NumPart_ThisFile;//[6];
  int      NumFilesPerSnapshot;
  double   BoxSize;
  double   Omega0;
  double   OmegaLambda;
  double   HubbleParam;
} t_h5_header;

//
// class GH5
//
template <class T> class GH5 {
public:
58
  GH5(const std::string _f_name,unsigned int mode, const bool verb=false);
59 60
 ~GH5();
  t_h5_header getHeader() { return header; }
LAMBERT Jean-charles's avatar
LAMBERT Jean-charles committed
61 62 63 64 65 66 67 68 69 70 71
  int close() {
    int status=0;
    if (myfile) {
      header_group.close(); // ! necessary to close this opened group
      myfile->close();
      //delete myfile;
      //myfile=NULL;
      status=1;
    }
    return status;
  }
72

73
  // reading
74 75 76
  template <class U> std::vector<U> getDataset(std::string dset_name, U );
  template <class U> std::vector<U> getAttribute(std::string attr_name);
  int getNpartTotal() { return npart_total; }
77 78 79 80
  // writing
  template <class U>
  bool setDataset(std::string dset_name, U * data, const unsigned int n, const unsigned int second_dim);
  template <class U> bool setAttribute(std::string attr_name, U *, const int );
81
private:
82 83
  std::map<std::string, bool> histo_group;

84 85 86
  bool verbose;
  void readHeaderAttributes();
  int npart_total;
87 88
  template <class U>
  DataType guessType(U);
89 90
  std::string f_name;
  H5File * myfile;
91
  Group header_group;
92 93 94 95
  t_h5_header header;

};

96
// ---------------------------------------------------
97
// class CSnapshotGadgetH5In
98 99
// READING class
// ---------------------------------------------------
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
template <class T> class CSnapshotGadgetH5In : public CSnapshotInterfaceIn<T> {

public:
  CSnapshotGadgetH5In(const std::string, const std::string, const std::string, const bool verb=false);
  ~CSnapshotGadgetH5In();

  int nextFrame(uns::UserSelection &);
  ComponentRangeVector * getSnapshotRange();

  // virtual function implemented
  //template <class U> std::vector<U> getData(const std::string, const std::string);
  template <class U> void getData(const std::string, const std::string);
  bool getData(const std::string,int *n,T **);
  bool getData(const std::string,       T * );
  bool getData(const std::string,int *n,int   **);
  bool getData(const std::string,       int   * );
  bool getData(const std::string, const std::string ,int *,T **);
  bool getData(const std::string, const std::string ,int *,int   **);

  bool isNewFrame() { return first_loc;}
 int close();

private:
 bool first_loc;

 unsigned int comp_bits;
LAMBERT Jean-charles's avatar
LAMBERT Jean-charles committed
126 127
 std::vector <T> pos,vel,mass, acc,pot, hsml,rho,temp,nh,sfr,
                 age,s_metal, g_metal,uenerg;
128 129 130 131 132 133 134 135 136 137 138 139 140
 std::vector <int> indexes,id;

 // HDF5 gadget object
 GH5<T> * myH5;
 void read(uns::UserSelection user_select);
 // methods
 T   getTime()   { return myH5->getHeader().Time;}
 template <class U>
 bool loadCommonDataset(std::string tag, std::vector<U> &data, const int dim);
 template <class U>
 bool loadDataset(std::string tag, std::vector<U> &data);
 void storeComponents();

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
}; // End of class snapshotgadgetH5In

// ---------------------------------------------------
// class CSnapshotGadgetH5Out
// WRITING class
// ---------------------------------------------------
template <class T> class CSnapshotGadgetH5Out : public CSnapshotInterfaceOut<T> {

public:
  CSnapshotGadgetH5Out(const std::string, const std::string, const bool);
  ~CSnapshotGadgetH5Out();
  int setHeader(void * );
  int setNbody(const int _n);
  int setData(std::string, T);
  int setData(std::string, const int , T *,const bool _addr=false);
  // array by double keys
  int setData(std::string, std::string, const int , T *,const bool _addr=false);
  int setData(std::string, std::string, const int , int   *,const bool _addr=false);

  int setData(std::string, const int , int *,const bool _addr=false);
  int setData(std::string, const int ,
      T *, T *, T *, const bool _addr=false);
  int save();

private:
  // methods
  template <class U>
  bool saveCommonDataset(std::string name,std::string dataset,  const int n ,U * data, const unsigned int);
  template <class U>
  bool checkMasses(const int n ,U * data, const int comp_id);
  // HDF5 gadget object
  GH5<T> * myH5;
  t_h5_header header;
174

175
}; // End of class snapshotgadgetH5Out
176 177 178

} // namespace
#endif // SNAPSHOTGADGETH5_H