DATRW++ library: seismic data I/O with multiple formats
mseedread.h
Go to the documentation of this file.
1 /*! \file mseedread.h
2  * \brief read mini-SEED files (prototypes)
3  *
4  * ----------------------------------------------------------------------------
5  *
6  * \author Thomas Forbriger
7  * \date 15/07/2004
8  *
9  * read mini-SEED files (prototypes)
10  *
11  * ----
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25  * ----
26  *
27  * Copyright (c) 2004 by Thomas Forbriger (BFO Schiltach)
28  *
29  * REVISIONS and CHANGES
30  * - 15/07/2004 V1.0 Thomas Forbriger (thof)
31  * - 07/07/2006 V1.1 provide debug function when reading MiniSEED record
32  * - 22/07/2014 V1.2 thof: support new format modifier: estimateNframes
33  *
34  * ============================================================================
35  */
36 
37 // include guard
38 #ifndef DATRW_MSEEDREAD_H_VERSION
39 
40 #define DATRW_MSEEDREAD_H_VERSION \
41  "DATRW_MSEEDREAD_H V1.2"
42 
43 #include<new>
44 #include<datrwxx/seedstructs.h>
45 #include<libtime++.h>
46 #include<aff/series.h>
47 #include<datrwxx/mseed.h>
48 
49 namespace datrw {
50 
51  /*! \brief all the stuff to read mini-SEED data
52  *
53  * \defgroup group_mseed Reading module for: mini-SEED data
54  *
55  * \sa
56  * \ref sec_mseed_concept,
57  * \ref sec_mseed_ascii
58  */
59 
60  /*! \brief all the stuff to read mini-SEED data
61  *
62  * This module is predominantly coded to extract data from a SeisComP or
63  * COMSERV system.
64  *
65  * \ingroup group_mseed
66  */
67  namespace mseed {
68 
69 /*======================================================================*/
70 // reading classes
71 
72  /*! MiniSEED input block
73  *
74  * This class just encapsulates memory handling for data blocks of
75  * different size.
76  */
77  class MiniSEEDblock {
78  public:
79  //! type of data block value
80  typedef char Tvalue;
81  //! use aff shared heap to store and copy block
82  typedef aff::Series<Tvalue> Theap;
83  //! standard block size for mini-SEED files (in SeisComP)
84  const static int standard_block_size=0x200;
85  // constructor allocates new memory
86  MiniSEEDblock(const unsigned int& blocksize=standard_block_size);
87  // return size of block
88  unsigned int blocksize() const { return(Mblocksize); }
89  // return byte size of block
90  unsigned int bytesize() const
91  { return(this->blocksize()*sizeof(Tvalue)); }
92  // return pointer to first data value
93  Tvalue* block() const { return(Mblock.pointer()); }
94  // return pointer to first data value
95  Tvalue* block(const unsigned int& i) const
96  { return(Mblock.pointer()+i); }
97  // return pointer to any data value
98  Tvalue operator[](const unsigned int& i) { return(Mblock(i)); }
99  private:
100  // pointer to block
102  // size of block
103  unsigned int Mblocksize;
104  }; // MiniSEEDblock
105 
106  /*! The input operator for MiniSEEDblock allows to read data into a
107  * MiniSEEdblock instance from any istream.
108  */
109  inline std::istream& operator >> (std::istream& is, MiniSEEDblock& mb)
110  { is.read(mb.block(), mb.bytesize()); return(is); }
111 
112 /*----------------------------------------------------------------------*/
113  namespace util {
114 
115  std::string extractstring(const char* s, const int& l);
116 
117  } // namespace util
118 /*----------------------------------------------------------------------*/
119  /*! debug modes
120  *
121  * These debug modes control the behaviour of the MiniSEEDRecord class.
122  * They are meant to be usefull, when testing corrupt MiniSEED files. By
123  * setting the flags it is possible to scan the files by preventing
124  * inconsitencies from being fatal.
125  */
126  struct Debug {
127  //! constructor sets default values
128  Debug(const bool& nonfatalflag=false,
129  const bool& raflag=false):
130  inconsistencies_are_not_fatal(nonfatalflag) ,
132  { }
133  //! make inconsistencies non fatal
135  //! report ASCII lines
137  }; // struct Debug
138 
139 /*----------------------------------------------------------------------*/
140 
141  /*! MiniSEED class for decoded data
142  *
143  * This class provides the interface to read one MiniSEED record
144  * including all data sample frames (if requested).
145  *
146  * The read or the skipdata function have to be called after
147  * initialization of the record object. Prior to reading data the
148  * object's contents are not valid, which is indicated by the return
149  * value of the valid function.
150  *
151  * If debug modes are requested, they have to be passed on the instance
152  * of creation.
153  */
155  public:
156  //! type of sample data
157  typedef int Tvalue;
158  //! type of container for sample data
159  typedef aff::Series<Tvalue> Tseries;
160  MiniSEEDRecord(const Debug& d=Debug(false, false)):
161  Mdebug(d),
162  Mvalid(false),
163  Mhasblockette1000(false),
164  Mhasblockette1001(false),
165  MestimateNframes(false),
166  Mchecks(true, true) { }
167  //! read a full MiniSEED record including data samples
168  void read(std::istream& is);
169  //! only read header and blockettes, no data
170  void skipdata(std::istream& is);
171  //! true if Data Only SEED Blockette is present
172  bool hasblockette1000() const { return(Mhasblockette1000); }
173  //! true if Data Extension Blockette is present
174  bool hasblockette1001() const { return(Mhasblockette1001); }
175  //! returns Fixed Data Record Header
177  recordheader() const { return(Mrecordheader); }
178  //! returns Data Only SEED Blockette
180  blockette1000() const { return(Mblockette1000); }
181  //! returns Data Extension Blockette
183  blockette1001() const { return(Mblockette1001); }
184  //! return last sample of previous record
185  int xm1() const { return(Mxm1); }
186  //! true if record was successfully read
187  bool valid() const { return(Mvalid); }
188  /*! return data samples (meaningless after skipdata)
189  *
190  * Notice that a refernce to the array is passed, that will be
191  * reused by the instance to read the next block.
192  */
193  Tseries data() const;
194  //! return time of first sample
195  libtime::TAbsoluteTime date() const;
196  //! number of samples
197  unsigned int nsamples() const
198  { return(this->recordheader().nsamp); }
199  //! return sampling interval
200  double dt() const;
201  //! return station code
202  std::string station() const
203  { return(util::extractstring(this->recordheader().stat, 5)); }
204  //! return channel code
205  std::string channel() const
206  { return(util::extractstring(this->recordheader().chan, 3)); }
207  //! return location code
208  std::string location() const
209  { return(util::extractstring(this->recordheader().loc, 2)); }
210  //! return network code
211  std::string network() const
212  { return(util::extractstring(this->recordheader().net, 2)); }
213  //! return reference to debug flags
214  Debug& debug() { return(Mdebug); }
215  //! adjust flag for estimation of frame count
216  void estimateNframes(const bool& flag)
217  { MestimateNframes=flag; }
218  //! adjust flags consistency checks
219  void checks(const ConsistencyChecks& flags)
220  { Mchecks=flags; }
221  private:
222  //! read header and return block
223  MiniSEEDblock readheader(std::istream& is);
224  //! debug options
226  //! contains valid data
227  bool Mvalid;
228  //! Data Only SEED Blockette is present
230  //! Data Extension Blockette is present
232  //! estimate number of frames
234  //! flags controlling consistency checks
236  //! Fixed Data Record Header
238  //! Data Only SEED Blockette
240  //! Data Extension Blockette
242  //! Expected value of last sample in previous block
243  int Mxm1;
244  //! Container for sample data
246  }; // MiniSEEDRecord
247 
248  /*! read a MiniSEED record from a stream
249  */
250  inline std::istream& operator >> (std::istream& is, MiniSEEDRecord& mr)
251  { mr.read(is); return(is); }
252 
253 /*======================================================================*/
254 // functions and operators
255 
256  //! convert BTIME structure to libtime structure
257  libtime::TAbsoluteTime convert(const SEED::BTIME& t);
258 
259  //! check for bytesec
260  bool needswap(const unsigned char& bytesex);
261 
262  //! calculate sampling interval from srate and smult
263  double samplinginterval(const short int& srate,
264  const short int& srmult);
265 
266  //! function to print online help
267  void help(std::ostream& os=std::cout);
268 
269  } // namespace mseed
270 
271 } // namespace datrw
272 
273 #endif // DATRW_MSEEDREAD_H_VERSION (includeguard)
274 
275 /* ----- END OF mseedread.h ----- */
ConsistencyChecks Mchecks
flags controlling consistency checks
Definition: mseedread.h:235
aff::Series< Tvalue > Theap
use aff shared heap to store and copy block
Definition: mseedread.h:82
int xm1() const
return last sample of previous record
Definition: mseedread.h:185
MiniSEEDRecord(const Debug &d=Debug(false, false))
Definition: mseedread.h:160
char Tvalue
type of data block value
Definition: mseedread.h:80
aff::Series< double > Tseries
Definition: sfftest.cc:47
MiniSEEDblock readheader(std::istream &is)
read header and return block
void checks(const ConsistencyChecks &flags)
adjust flags consistency checks
Definition: mseedread.h:219
SEED::DataExtensionBlockette Mblockette1001
Data Extension Blockette.
Definition: mseedread.h:241
const SEED::FixedDataRecordHeader & recordheader() const
returns Fixed Data Record Header
Definition: mseedread.h:177
void read(std::istream &is)
read a full MiniSEED record including data samples
unsigned int bytesize() const
Definition: mseedread.h:90
Debug(const bool &nonfatalflag=false, const bool &raflag=false)
constructor sets default values
Definition: mseedread.h:128
std::istream & operator>>(std::istream &is, MiniSEEDblock &mb)
Definition: mseedread.h:109
std::string location() const
return location code
Definition: mseedread.h:208
std::string station() const
return station code
Definition: mseedread.h:202
std::string extractstring(const char *s, const int &l)
Return a maximum of l characters.
Definition: mseedread.cc:141
bool Mhasblockette1001
Data Extension Blockette is present.
Definition: mseedread.h:231
int Mxm1
Expected value of last sample in previous block.
Definition: mseedread.h:243
std::string channel() const
return channel code
Definition: mseedread.h:205
double samplinginterval(const short int &srate, const short int &srmult)
calculate sampling interval from srate and smult
Definition: mseedread.cc:180
double dt() const
return sampling interval
bool MestimateNframes
estimate number of frames
Definition: mseedread.h:233
const SEED::DataExtensionBlockette & blockette1001() const
returns Data Extension Blockette
Definition: mseedread.h:183
bool hasblockette1001() const
true if Data Extension Blockette is present
Definition: mseedread.h:174
const SEED::DataOnlySEEDBlockette & blockette1000() const
returns Data Only SEED Blockette
Definition: mseedread.h:180
bool needswap(const unsigned char &bytesex)
check bytesex
Definition: mseedread.cc:192
aff::Series< Tvalue > Tseries
type of container for sample data
Definition: mseedread.h:159
MiniSEEDblock(const unsigned int &blocksize=standard_block_size)
Definition: mseedread.cc:154
Root namespace of library.
Definition: aalibdatrwxx.cc:16
bool Mhasblockette1000
Data Only SEED Blockette is present.
Definition: mseedread.h:229
libtime::TAbsoluteTime convert(const SEED::BTIME &t)
convert BTIME structure to libtime structure
Definition: mseedread.cc:166
unsigned int blocksize() const
Definition: mseedread.h:88
int Tvalue
type of sample data
Definition: mseedread.h:157
Tvalue operator[](const unsigned int &i)
Definition: mseedread.h:98
bool valid() const
true if record was successfully read
Definition: mseedread.h:187
Debug & debug()
return reference to debug flags
Definition: mseedread.h:214
bool inconsistencies_are_not_fatal
make inconsistencies non fatal
Definition: mseedread.h:134
SEED::FixedDataRecordHeader Mrecordheader
Fixed Data Record Header.
Definition: mseedread.h:237
libtime::TAbsoluteTime date() const
return time of first sample
void estimateNframes(const bool &flag)
adjust flag for estimation of frame count
Definition: mseedread.h:216
bool hasblockette1000() const
true if Data Only SEED Blockette is present
Definition: mseedread.h:172
Tseries Mdata
Container for sample data.
Definition: mseedread.h:245
void help(std::ostream &os=std::cout)
function to print online help
Definition: gseread.cc:75
provide mini-SEED data (prototypes)
bool report_ascii_data_to_stdout
report ASCII lines
Definition: mseedread.h:136
std::string network() const
return network code
Definition: mseedread.h:211
unsigned int nsamples() const
number of samples
Definition: mseedread.h:197
static const int standard_block_size
standard block size for mini-SEED files (in SeisComP)
Definition: mseedread.h:84
void skipdata(std::istream &is)
only read header and blockettes, no data
SEED::DataOnlySEEDBlockette Mblockette1000
Data Only SEED Blockette.
Definition: mseedread.h:239
Debug Mdebug
debug options
Definition: mseedread.h:225
bool Mvalid
contains valid data
Definition: mseedread.h:227
Tvalue * block(const unsigned int &i) const
Definition: mseedread.h:95
Tvalue * block() const
Definition: mseedread.h:93
Tseries data() const
Return actual data samples stored in object.