A.I.M.S


genesisR.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2  * Institut Federatif de Recherche 49
3  * CEA/NeuroSpin, Batiment 145,
4  * 91191 Gif-sur-Yvette cedex
5  * France
6  *
7  * This software is governed by the CeCILL-B license under
8  * French law and abiding by the rules of distribution of free software.
9  * You can use, modify and/or redistribute the software under the
10  * terms of the CeCILL-B license as circulated by CEA, CNRS
11  * and INRIA at the following URL "http://www.cecill.info".
12  *
13  * As a counterpart to the access to the source code and rights to copy,
14  * modify and redistribute granted by the license, users are provided only
15  * with a limited warranty and the software's author, the holder of the
16  * economic rights, and the successive licensors have only limited
17  * liability.
18  *
19  * In this respect, the user's attention is drawn to the risks associated
20  * with loading, using, modifying and/or developing or reproducing the
21  * software by the user in light of its specific status of free software,
22  * that may mean that it is complicated to manipulate, and that also
23  * therefore means that it is reserved for developers and experienced
24  * professionals having in-depth computer knowledge. Users are therefore
25  * encouraged to load and test the software's suitability as regards their
26  * requirements in conditions enabling the security of their systems and/or
27  * data to be ensured and, more generally, to use and operate it in the
28  * same conditions as regards security.
29  *
30  * The fact that you are presently reading this means that you have had
31  * knowledge of the CeCILL-B license and that you accept its terms.
32  */
33 
34 /*
35  * Data reader class
36  */
37 
38 #ifndef AIMS_IO_GENESISR_H
39 #define AIMS_IO_GENESISR_H
40 
42 #include <aims/io/defaultItemR.h>
43 #include <aims/io/genesisheader.h>
44 #include <aims/io/datatypecode.h>
45 #include <aims/data/data.h>
51 
52 namespace aims
53 {
54 
55  template<class T> class GenesisReader
56  {
57  public:
58  GenesisReader( const std::string & name ) : _name( name ) {}
60 
61  void read( AimsData<T> & thing, const carto::AllocatorContext & context,
62  carto::Object options );
63 
64  private:
65  std::string _name;
66  };
67 
68  template <class T>
69  inline GenesisReader<T> &
71  {
72  reader.read( thing );
73  return reader;
74  }
75 
76  template <class T>
77  inline
79  const carto::AllocatorContext & context,
80  carto::Object options )
81  {
82  GenesisHeader *hdr = new GenesisHeader( _name );
83  try
84  {
85  hdr->read();
86  }
87  catch( std::exception & e )
88  {
89  delete hdr;
90  throw;
91  }
92 
94  if( hdr->dataType() != dtc.dataType() )
96  ( std::string( "data type / reader type mismatch : file is " )
97  + hdr->dataType() + ", expecting " + dtc.dataType(), _name );
98 
99  int frame = -1, border = 0;
100  options->getProperty( "frame", frame );
101  options->getProperty( "border", border );
102 
103  int dimt, tmin;
104  if( frame >= 0 )
105  {
106  if( frame >= hdr->dimT() )
107  {
108  delete hdr;
109  throw std::domain_error( "frame higher than file dimT" );
110  }
111  dimt = 1;
112  tmin = frame;
113  }
114  else
115  {
116  dimt = hdr->dimT();
117  tmin = 0;
118  }
119 
120  /*cout << "dimensions: " << hdr->dimX() << ", " << hdr->dimY() << ", "
121  << hdr->dimZ() << ", " << dimt << endl;*/
122 
123  carto::AllocatorContext
124  ac( context.accessMode(),
126  ( new carto::FileDataSource( _name, 0, carto::DataSource::Read ) ),
127  false, context.useFactor() );
128 
129  AimsData<T> data( hdr->dimX(), hdr->dimY(), hdr->dimZ(), dimt,
130  border, ac );
131 
132  data.setSizeX( hdr->sizeX() );
133  data.setSizeY( hdr->sizeY() );
134  data.setSizeZ( hdr->sizeZ() );
135  data.setSizeT( hdr->sizeT() );
136  data.setHeader( hdr );
137 
138  // we must scandir to obtain a files list
139 
140  char s = carto::FileUtil::separator();
141  std::string::size_type pos = _name.rfind( s );
142  std::string dirname;
143  if( pos == std::string::npos )
144  dirname = ".";
145  else
146  dirname = _name.substr( 0, pos );
147 
148  carto::Directory dir( dirname );
149  std::set<std::string> names = dir.files();
150  std::set<std::string>::iterator in, en = names.end();
151  std::string name;
152  int z, t, ss = 0, es = 0;
153  std::vector<bool> reads( data.dimT() * data.dimZ() );
154 
155  z = data.dimZ(); // temp
156  for( in=names.begin(); in!=en; ++in )
157  {
158  name = dirname + s + *in;
159  //cout << "read " << name << endl;
160  GenesisHeader h( name ); // (re)read each header
161 
162  //std::cout << "read " << name << "(byte swapping: " << h.byteSwapping() << ")" << std::endl;
163  try
164  {
165  h.read();
166  std::ifstream is( name.c_str(),
167  std::ios::in | std::ios::binary );
168  if( !is )
169  carto::io_error::launchErrnoExcept( name );
170  is.unsetf( std::ios::skipws );
171  is.seekg( h.dataOffset() );
172  if( !is )
173  carto::io_error::launchErrnoExcept( name );
174  if( h.compressCode() != 1 )
175  throw
176  carto::format_mismatch_error( "Genesis reader can't deal with "
177  "compressed data", name );
178  t = 0; // temporarily
179  h.getProperty( "slice", z );
180  h.getProperty( "start_slice", ss );
181  h.getProperty( "end_slice", es );
182  z = abs(es - z);
183  if( (z >= 0) && (z < data.dimZ()) /*&& (t >= 0) && (t < data.dimT())*/ ) //FR: unuseful checks
184  {
185  if( reads[ t*data.dimZ() + z ] )
186  {
187  std::cerr << "warning: slice z=" << z << ", t=" << t
188  << " already read" << std::endl;
189  }
190  else
191  {
192  std::cerr << "........reading slice z=" << z << ", t=" << t << std::endl;
193  reads[ t*data.dimZ() + z ] = true;
194 
196  ItemReader<T> *ir = ib1.reader( "binar", h.byteSwapping() );
197  for( int y=0; y<data.dimY(); ++y )
198  ir->read( is, &data(0,y,z,t), data.dimX() );
199 
200  delete ir;
201  }
202  }
203  }
204  catch( std::exception & )
205  {
206  std::cout << name << " not part of genesis volume\n";
207  }
208  }
209 
210  //cout << "read finished, checking if we've got all slices...\n";
211  for( t=0; t<data.dimT(); ++t )
212  for( z=0; z<data.dimZ(); ++z )
213  if( !reads[ t*data.dimZ() + z ] )
214  std::cerr << "warning: missing slice " << z << ", t=" << t
215  << std::endl;
216  thing = data;
217  }
218 }
219 
220 #endif
GenesisReader< T > & operator>>(GenesisReader< T > &reader, AimsData< T > &thing)
Definition: genesisR.h:70
std::string dataType()
virtual bool getProperty(const std::string &key, Object &value) const =0
int dimZ() const
Definition: genesisheader.h:61
float sizeY() const
Definition: genesisheader.h:64
GenesisReader(const std::string &name)
Definition: genesisR.h:58
Default low-levels readers.
Definition: defaultItemR.h:55
void setSizeX(float sizex)
GE Genesis format header.
Definition: genesisheader.h:50
float sizeX() const
Definition: genesisheader.h:63
bool byteSwapping() const
Definition: genesisheader.h:72
static char separator()
std::set< std::string > files() const
virtual ItemReader< T > * reader(const std::string &openmode="binar", bool bswap=false) const
Definition: defaultItemR.h:154
int dimT() const
Definition: genesisheader.h:62
std::string dataType() const
Definition: genesisheader.h:68
float sizeZ() const
Definition: genesisheader.h:65
int compressCode() const
Definition: genesisheader.h:70
virtual void read(std::istream &is, T &item) const
Definition: itemR.h:103
int dimX() const
Definition: genesisheader.h:59
virtual bool getProperty(const std::string &, Object &) const
float sizeT() const
Definition: genesisheader.h:66
int dimY() const
Definition: genesisheader.h:60
int dataOffset() const
Definition: genesisheader.h:71
Low-level "small item" reader, used by higher-level file readers.
Definition: itemR.h:98
void read(AimsData< T > &thing, const carto::AllocatorContext &context, carto::Object options)
Definition: genesisR.h:78