Monarch  v3.8.2
Project 8 Data File Format Library
M3WriteTest.cc
Go to the documentation of this file.
1 #include "M3DataInterface.hh"
2 #include "M3Monarch.hh"
3 
4 #include "application.hh"
5 #include "logger.hh"
6 
7 #include <cstring> // for strcmp
8 
9 using namespace monarch3;
10 
11 LOGGER( mlog, "M3WriteTest" );
12 
13 int main( const int argc, const char** argv )
14 {
15  scarab::main_app theMain( false );
16 
17  std::string tFilename;
18  theMain.add_option( "Filename", tFilename, "Test output filename" )->default_val( "write_test_output.egg" );
19 
20  CLI11_PARSE( theMain, argc, argv );
21 
22  try
23  {
24  std::shared_ptr< Monarch3 > tWriteTest( Monarch3::OpenForWriting( tFilename ) );
25 
26  M3Header* tHeader = tWriteTest->GetHeader();
27  tHeader->Filename() = tFilename;
28  tHeader->SetRunDuration( 8675309 );
29  tHeader->Timestamp() = "Stardate 33515";
30  tHeader->Description() = "Bigger on the inside";
31 
32  // number of samples in each stream
33  unsigned tSSSamples = 10;
34  unsigned tDSSamples = 5;
35  unsigned tTSSamples = 5;
36  unsigned tFlSSamples = 10;
37  //unsigned tFlCompSamples = 5;
38 
39  LINFO( mlog, "Adding streams" );
40  unsigned tSingleStreamNum = tHeader->AddStream( "1-channel device", 500, tSSSamples, 1, 1, sDigitizedUS, 8, sBitsAlignedLeft );
41  unsigned tDoubleStreamNum = tHeader->AddStream( "2-channel device", 2, sInterleaved, 250, tDSSamples, 1, 2, sDigitizedUS, 16, sBitsAlignedLeft );
42  unsigned tTripleStreamNum = tHeader->AddStream( "3-channel device", 3, sSeparate, 100, tTSSamples, 1, 1, sDigitizedUS, 8, sBitsAlignedLeft );
43  unsigned tFloatStreamNum = tHeader->AddStream( "Floating-point device", 100, tFlSSamples, 1, 4, sAnalog, 8, sBitsAlignedLeft );
44  // multi-channel multi-sample writing commented out until fixed
45  //unsigned tFlCompStreamNum = tHeader->AddStream( "Complex Floating-point device", 5, sInterleaved, 100, tFlCompSamples, 2, 8, sAnalog, 16 );
46 
47  tWriteTest->WriteHeader();
48 
49  LINFO( mlog, "Wrote header:\n" << *tHeader );
50 
51  LINFO( mlog, "Writing data" );
52 
53  // Stream 0
54  M3Stream* tSingleStream = tWriteTest->GetStream( tSingleStreamNum );
55  byte_type* tSSData = tSingleStream->GetStreamRecord()->GetData();
56  for( unsigned iSample = 0; iSample < tSSSamples; ++iSample )
57  {
58  tSSData[ iSample ] = 1;
59  }
60  if( ! tSingleStream->WriteRecord( true ) )
61  {
62  LERROR( mlog, "Unable to write the record!" );
63  return RETURN_ERROR;
64  }
65 
66  for( unsigned iSample = 0; iSample < tSSSamples; ++iSample )
67  {
68  tSSData[ iSample ] = 10;
69  }
70  if( ! tSingleStream->WriteRecord( false ) )
71  {
72  LERROR( mlog, "Unable to write the record!" );
73  return RETURN_ERROR;
74  }
75 
76 
77  // Stream 1
78  M3Stream* tDoubleStream = tWriteTest->GetStream( tDoubleStreamNum );
79  M3DataWriter< uint16_t > tDSData0( tDoubleStream->GetChannelRecord( 0 )->GetData(), 2, sDigitizedUS );
80  M3DataWriter< uint16_t > tDSData1( tDoubleStream->GetChannelRecord( 1 )->GetData(), 2, sDigitizedUS );
81  for( unsigned iSample = 0; iSample < tDSSamples; ++iSample )
82  {
83  tDSData0.set_at( 1, iSample );
84  tDSData1.set_at( 2, iSample );
85  }
86  if( ! tDoubleStream->WriteRecord( true ) )
87  {
88  LERROR( mlog, "Unable to write the record!" );
89  return RETURN_ERROR;
90  }
91 
92  for( unsigned iSample = 0; iSample < tDSSamples; ++iSample )
93  {
94  tDSData0.set_at( 1000, iSample );
95  tDSData1.set_at( 2000, iSample );
96  }
97  if( ! tDoubleStream->WriteRecord( true ) )
98  {
99  LERROR( mlog, "Unable to write the record!" );
100  return RETURN_ERROR;
101  }
102 
103  for( unsigned iSample = 0; iSample < tDSSamples; ++iSample )
104  {
105  tDSData0.set_at( 10000, iSample );
106  tDSData1.set_at( 20000, iSample );
107  }
108  if( ! tDoubleStream->WriteRecord( false ) )
109  {
110  LERROR( mlog, "Unable to write the record!" );
111  return RETURN_ERROR;
112  }
113 
114 
115  // Stream 2
116  M3Stream* tTripleStream = tWriteTest->GetStream( tTripleStreamNum );
117  byte_type* tTSData0 = tTripleStream->GetChannelRecord( 0 )->GetData();
118  byte_type* tTSData1 = tTripleStream->GetChannelRecord( 1 )->GetData();
119  byte_type* tTSData2 = tTripleStream->GetChannelRecord( 2 )->GetData();
120  for( unsigned iSample = 0; iSample < tTSSamples; ++iSample )
121  {
122  tTSData0[ iSample ] = 1;
123  tTSData1[ iSample ] = 2;
124  tTSData2[ iSample ] = 3;
125  }
126  if( ! tTripleStream->WriteRecord( true ) )
127  {
128  LERROR( mlog, "Unable to write the record!" );
129  return RETURN_ERROR;
130  }
131 
132  for( unsigned iSample = 0; iSample < tTSSamples; ++iSample )
133  {
134  tTSData0[ iSample ] = 10;
135  tTSData1[ iSample ] = 20;
136  tTSData2[ iSample ] = 30;
137  }
138  if( ! tTripleStream->WriteRecord( false ) )
139  {
140  LERROR( mlog, "Unable to write the record!" );
141  return RETURN_ERROR;
142  }
143 
144 
145 
146  // Stream 3
147  M3Stream* tFloatStream = tWriteTest->GetStream( tFloatStreamNum );
148  M3DataWriter< float > tFlSData( tFloatStream->GetChannelRecord( 0 )->GetData(), 4, sAnalog );
149  for( unsigned iSample = 0; iSample < tFlSSamples; ++iSample )
150  {
151  tFlSData.set_at( 3.1415926535898, iSample );
152  }
153  if( ! tFloatStream->WriteRecord( true ) )
154  {
155  LERROR( mlog, "Unable to write the record!" );
156  return RETURN_ERROR;
157  }
158 
159  for( unsigned iSample = 0; iSample < tFlSSamples; ++iSample )
160  {
161  tFlSData.set_at( 2.71828182846, iSample );
162  }
163  if( ! tFloatStream->WriteRecord( true ) )
164  {
165  LERROR( mlog, "Unable to write the record!" );
166  return RETURN_ERROR;
167  }
168 
169 
170 
171 
172  // Stream 4
173  /*
174  M3Stream* tFlCompStream = tWriteTest->GetStream( tFlCompStreamNum );
175  M3ComplexDataWriter< f8_complex > tFlCompSData0( tFlCompStream->GetChannelRecord( 0 )->GetData(), 8, sAnalog, 2 );
176  M3ComplexDataWriter< f8_complex > tFlCompSData1( tFlCompStream->GetChannelRecord( 1 )->GetData(), 8, sAnalog, 2 );
177  M3ComplexDataWriter< f8_complex > tFlCompSData2( tFlCompStream->GetChannelRecord( 2 )->GetData(), 8, sAnalog, 2 );
178  M3ComplexDataWriter< f8_complex > tFlCompSData3( tFlCompStream->GetChannelRecord( 3 )->GetData(), 8, sAnalog, 2 );
179  M3ComplexDataWriter< f8_complex > tFlCompSData4( tFlCompStream->GetChannelRecord( 3 )->GetData(), 8, sAnalog, 2 );
180  f8_complex value0, value1, value2, value3, value4;
181  value0[ 0 ] = 0.0; value0[ 1 ] = 0.0;
182  value1[ 0 ] = 1.1; value1[ 1 ] = 1.001;
183  value2[ 0 ] = 2.2; value1[ 1 ] = 2.002;
184  value3[ 0 ] = 3.3; value1[ 1 ] = 3.003;
185  value4[ 0 ] = 4.4; value1[ 1 ] = 4.004;
186  for( unsigned iSample = 0; iSample < tFlCompSamples; ++iSample )
187  {
188  tFlCompSData0.set_at( value0, iSample );
189  tFlCompSData1.set_at( value1, iSample );
190  tFlCompSData2.set_at( value2, iSample );
191  tFlCompSData3.set_at( value3, iSample );
192  tFlCompSData4.set_at( value4, iSample );
193  }
194  if( ! tFlCompStream->WriteRecord( true ) )
195  {
196  LERROR( mlog, "Unable to write the record!" );
197  return RETURN_ERROR;
198  }
199 
200  value0[ 0 ] = -0.0; value0[ 1 ] = -0.0;
201  value1[ 0 ] = -1.1; value1[ 1 ] = -1.001;
202  value2[ 0 ] = -2.2; value1[ 1 ] = -2.002;
203  value3[ 0 ] = -3.3; value1[ 1 ] = -3.003;
204  value4[ 0 ] = -4.4; value1[ 1 ] = -4.004;
205  for( unsigned iSample = 0; iSample < tFlCompSamples; ++iSample )
206  {
207  tFlCompSData0.set_at( value0, iSample );
208  tFlCompSData1.set_at( value1, iSample );
209  tFlCompSData2.set_at( value2, iSample );
210  tFlCompSData3.set_at( value3, iSample );
211  tFlCompSData4.set_at( value4, iSample );
212  }
213  if( ! tFlCompStream->WriteRecord( false ) )
214  {
215  LERROR( mlog, "Unable to write the record!" );
216  return RETURN_ERROR;
217  }
218  */
219 
220  tWriteTest->FinishWriting();
221  LINFO( mlog, "File closed" );
222  }
223  catch( M3Exception& e )
224  {
225  LERROR( mlog, "Exception thrown during write test:\n" << e.what() );
226  return RETURN_ERROR;
227  }
228 
229  return RETURN_SUCCESS;
230 }
const M3Record * GetStreamRecord() const
Get the pointer to the stream record.
Definition: M3Stream.cc:299
static const uint32_t sSeparate
Definition: M3Constants.hh:51
static const uint32_t sDigitizedUS
Definition: M3Constants.hh:38
int main(const int argc, const char **argv)
Definition: M3WriteTest.cc:13
virtual const char * what() const
Definition: M3Exception.cc:34
Egg file header information.
Definition: M3Header.hh:152
uint8_t byte_type
Definition: M3Types.hh:22
void set_at(SetType value, unsigned index)
Read/write access for a data stream.
Definition: M3Stream.hh:41
static const uint32_t sInterleaved
Specifies whether the data channels are interleaved or separate in a stream.
Definition: M3Constants.hh:50
const byte_type * GetData() const
Definition: M3Record.hh:111
Specialized exception class for Monarch3.
Definition: M3Exception.hh:28
static scarab::logger mlog("M3Header")
const M3Record * GetChannelRecord(unsigned aChannel) const
Get the pointer to a particular channel record.
Definition: M3Stream.cc:304
Interface class for a variety of data types.
bool WriteRecord(bool aIsNewAcquisition)
Write the record contents to the file.
Definition: M3Stream.cc:412
static const uint32_t sBitsAlignedLeft
Definition: M3Constants.hh:44
static const uint32_t sAnalog
Definition: M3Constants.hh:40
static Monarch3 * OpenForWriting(const std::string &filename)
Definition: M3Monarch.cc:92
unsigned AddStream(const std::string &aSource, uint32_t anAcqRate, uint32_t aRecSize, uint32_t aSampleSize, uint32_t aDataTypeSize, uint32_t aDataFormat, uint32_t aBitDepth, uint32_t aBitAlignment, std::vector< unsigned > *aChanVec=NULL)
Definition: M3Header.cc:400