Psyllid  v1.12.4
Project 8 Data Acquisisition Software
test_monarch3_write.cc
Go to the documentation of this file.
1 #include "M3DataInterface.hh"
2 //#include "M3Monarch.hh"
3 
4 #include "logger.hh"
5 
6 #include "monarch3_wrap.hh"
7 #include "psyllid_error.hh"
8 
9 #include <cstring> // for strcmp
10 
11 using namespace monarch3;
12 using namespace psyllid;
13 
14 LOGGER( plog, "test_monarch3_write" );
15 
16 int main( const int argc, const char** argv )
17 {
18  if( argc < 2 || strcmp( argv[1], "-h" ) == 0 )
19  {
20  LINFO( plog, "usage:\n"
21  << " test_monarch3_write [-h] <output egg file>\n"
22  << " -h: print this usage information" );
23  return -1;
24  }
25 
26  try
27  {
28  monarch_wrap_ptr t_mwp( new monarch_wrapper( argv[1] ) );
29  //Monarch3* tWriteTest = Monarch3::OpenForWriting( argv[1] );
30 
31  header_wrap_ptr t_hwp( t_mwp->get_header() );
32  //M3Header* tHeader = tWriteTest->GetHeader();
33  t_hwp->header().SetFilename( argv[1] );
34  t_hwp->header().SetRunDuration( 8675309 );
35  t_hwp->header().SetTimestamp( "Stardate 33515" );
36  t_hwp->header().SetDescription( "Bigger on the inside" );
37 
38  // number of samples in each stream
39  unsigned tSSSamples = 10;
40  unsigned tDSSamples = 5;
41  unsigned tTSSamples = 5;
42  unsigned tFlSSamples = 10;
43  //unsigned tFlCompSamples = 5;
44 
45  LINFO( plog, "Adding streams" );
46  unsigned tSingleStreamNum = t_hwp->header().AddStream( "1-channel device", 500, tSSSamples, 1, 1, sDigitizedUS, 8, sBitsAlignedLeft );
47  unsigned tDoubleStreamNum = t_hwp->header().AddStream( "2-channel device", 2, sInterleaved, 250, tDSSamples, 1, 2, sDigitizedUS, 16, sBitsAlignedLeft );
48  unsigned tTripleStreamNum = t_hwp->header().AddStream( "3-channel device", 3, sSeparate, 100, tTSSamples, 1, 1, sDigitizedUS, 8, sBitsAlignedLeft );
49  unsigned tFloatStreamNum = t_hwp->header().AddStream( "Floating-point device", 100, tFlSSamples, 1, 4, sAnalog, 8, sBitsAlignedLeft );
50  // multi-channel multi-sample writing commented out until fixed
51  //unsigned tFlCompStreamNum = t_hwp->header().AddStream( "Complex Floating-point device", 5, sInterleaved, 100, tFlCompSamples, 2, 8, sAnalog, 16 );
52 
53  //tWriteTest->WriteHeader();
54 
55  LINFO( plog, "Wrote header:\n" << t_hwp->header() );
56 
57  LINFO( plog, "Writing data" );
58 
59  // Stream 0
60  stream_wrap_ptr t_swp0 = t_mwp->get_stream( tSingleStreamNum );
61  //M3Stream* tSingleStream = tWriteTest->GetStream( tSingleStreamNum );
62  byte_type* tSSData = t_swp0->get_stream_record()->GetData();
63  for( unsigned iSample = 0; iSample < tSSSamples; ++iSample )
64  {
65  tSSData[ iSample ] = 1;
66  }
67  if( ! t_swp0->write_record( true ) )
68  {
69  LERROR( plog, "Unable to write the record!" );
70  //delete tWriteTest;
71  return -1;
72  }
73 
74  for( unsigned iSample = 0; iSample < tSSSamples; ++iSample )
75  {
76  tSSData[ iSample ] = 10;
77  }
78  if( ! t_swp0->write_record( false ) )
79  {
80  LERROR( plog, "Unable to write the record!" );
81  //delete tWriteTest;
82  return -1;
83  }
84 
85  t_mwp->finish_stream( tSingleStreamNum );
86 
87 
88  // Stream 1
89  stream_wrap_ptr t_swp1 = t_mwp->get_stream( tDoubleStreamNum );
90  //M3Stream* tDoubleStream = tWriteTest->GetStream( tDoubleStreamNum );
91  M3DataWriter< uint16_t > tDSData0( t_swp1->get_channel_record( 0 )->GetData(), 2, sDigitizedUS );
92  M3DataWriter< uint16_t > tDSData1( t_swp1->get_channel_record( 1 )->GetData(), 2, sDigitizedUS );
93  for( unsigned iSample = 0; iSample < tDSSamples; ++iSample )
94  {
95  tDSData0.set_at( 1, iSample );
96  tDSData1.set_at( 2, iSample );
97  }
98  if( ! t_swp1->write_record( true ) )
99  {
100  LERROR( plog, "Unable to write the record!" );
101  //delete tWriteTest;
102  return -1;
103  }
104 
105  for( unsigned iSample = 0; iSample < tDSSamples; ++iSample )
106  {
107  tDSData0.set_at( 1000, iSample );
108  tDSData1.set_at( 2000, iSample );
109  }
110  if( ! t_swp1->write_record( true ) )
111  {
112  LERROR( plog, "Unable to write the record!" );
113  //delete tWriteTest;
114  return -1;
115  }
116 
117  for( unsigned iSample = 0; iSample < tDSSamples; ++iSample )
118  {
119  tDSData0.set_at( 10000, iSample );
120  tDSData1.set_at( 20000, iSample );
121  }
122  if( ! t_swp1->write_record( false ) )
123  {
124  LERROR( plog, "Unable to write the record!" );
125  //delete tWriteTest;
126  return -1;
127  }
128 
129  t_mwp->finish_stream( tDoubleStreamNum );
130 
131 
132  // Stream 2
133  stream_wrap_ptr t_swp2 = t_mwp->get_stream( tTripleStreamNum );
134  //M3Stream* tTripleStream = tWriteTest->GetStream( tTripleStreamNum );
135  byte_type* tTSData0 = t_swp2->get_channel_record( 0 )->GetData();
136  byte_type* tTSData1 = t_swp2->get_channel_record( 1 )->GetData();
137  byte_type* tTSData2 = t_swp2->get_channel_record( 2 )->GetData();
138  for( unsigned iSample = 0; iSample < tTSSamples; ++iSample )
139  {
140  tTSData0[ iSample ] = 1;
141  tTSData1[ iSample ] = 2;
142  tTSData2[ iSample ] = 3;
143  }
144  if( ! t_swp2->write_record( true ) )
145  {
146  LERROR( plog, "Unable to write the record!" );
147  //delete tWriteTest;
148  return -1;
149  }
150 
151  for( unsigned iSample = 0; iSample < tTSSamples; ++iSample )
152  {
153  tTSData0[ iSample ] = 10;
154  tTSData1[ iSample ] = 20;
155  tTSData2[ iSample ] = 30;
156  }
157  if( ! t_swp2->write_record( false ) )
158  {
159  LERROR( plog, "Unable to write the record!" );
160  //delete tWriteTest;
161  return -1;
162  }
163 
164  t_mwp->finish_stream( tTripleStreamNum );
165 
166 
167  // Stream 3
168  stream_wrap_ptr t_swp3 = t_mwp->get_stream( tFloatStreamNum );
169  //M3Stream* tFloatStream = tWriteTest->GetStream( tFloatStreamNum );
170  M3DataWriter< float > tFlSData( t_swp3->get_channel_record( 0 )->GetData(), 4, sAnalog );
171  for( unsigned iSample = 0; iSample < tFlSSamples; ++iSample )
172  {
173  tFlSData.set_at( 3.1415926535898, iSample );
174  }
175  if( ! t_swp3->write_record( true ) )
176  {
177  LERROR( plog, "Unable to write the record!" );
178  //delete tWriteTest;
179  return -1;
180  }
181 
182  for( unsigned iSample = 0; iSample < tFlSSamples; ++iSample )
183  {
184  tFlSData.set_at( 2.71828182846, iSample );
185  }
186  if( ! t_swp3->write_record( true ) )
187  {
188  LERROR( plog, "Unable to write the record!" );
189  //delete tWriteTest;
190  return -1;
191  }
192 
193  t_mwp->finish_stream( tFloatStreamNum );
194 
195 
196  // Stream 4
197  /*
198  stream_wrap_ptr t_swp4 = t_mwp->get_stream( tSingleStreamNum );
199  M3Stream* tFlCompStream = tWriteTest->GetStream( tFlCompStreamNum );
200  M3ComplexDataWriter< f8_complex > tFlCompSData0( tFlCompStream->GetChannelRecord( 0 )->GetData(), 8, sAnalog, 2 );
201  M3ComplexDataWriter< f8_complex > tFlCompSData1( tFlCompStream->GetChannelRecord( 1 )->GetData(), 8, sAnalog, 2 );
202  M3ComplexDataWriter< f8_complex > tFlCompSData2( tFlCompStream->GetChannelRecord( 2 )->GetData(), 8, sAnalog, 2 );
203  M3ComplexDataWriter< f8_complex > tFlCompSData3( tFlCompStream->GetChannelRecord( 3 )->GetData(), 8, sAnalog, 2 );
204  M3ComplexDataWriter< f8_complex > tFlCompSData4( tFlCompStream->GetChannelRecord( 3 )->GetData(), 8, sAnalog, 2 );
205  f8_complex value0, value1, value2, value3, value4;
206  value0[ 0 ] = 0.0; value0[ 1 ] = 0.0;
207  value1[ 0 ] = 1.1; value1[ 1 ] = 1.001;
208  value2[ 0 ] = 2.2; value1[ 1 ] = 2.002;
209  value3[ 0 ] = 3.3; value1[ 1 ] = 3.003;
210  value4[ 0 ] = 4.4; value1[ 1 ] = 4.004;
211  for( unsigned iSample = 0; iSample < tFlCompSamples; ++iSample )
212  {
213  tFlCompSData0.set_at( value0, iSample );
214  tFlCompSData1.set_at( value1, iSample );
215  tFlCompSData2.set_at( value2, iSample );
216  tFlCompSData3.set_at( value3, iSample );
217  tFlCompSData4.set_at( value4, iSample );
218  }
219  if( ! tFlCompStream->WriteRecord( true ) )
220  {
221  LERROR( plog, "Unable to write the record!" );
222  delete tWriteTest;
223  return -1;
224  }
225 
226  value0[ 0 ] = -0.0; value0[ 1 ] = -0.0;
227  value1[ 0 ] = -1.1; value1[ 1 ] = -1.001;
228  value2[ 0 ] = -2.2; value1[ 1 ] = -2.002;
229  value3[ 0 ] = -3.3; value1[ 1 ] = -3.003;
230  value4[ 0 ] = -4.4; value1[ 1 ] = -4.004;
231  for( unsigned iSample = 0; iSample < tFlCompSamples; ++iSample )
232  {
233  tFlCompSData0.set_at( value0, iSample );
234  tFlCompSData1.set_at( value1, iSample );
235  tFlCompSData2.set_at( value2, iSample );
236  tFlCompSData3.set_at( value3, iSample );
237  tFlCompSData4.set_at( value4, iSample );
238  }
239  if( ! tFlCompStream->WriteRecord( false ) )
240  {
241  LERROR( plog, "Unable to write the record!" );
242  delete tWriteTest;
243  return -1;
244  }
245  */
246 
247 
248  //tWriteTest->FinishWriting();
249  t_mwp->finish_file();
250  LINFO( plog, "File closed" );
251 
252  //delete tWriteTest;
253 
254  }
255  catch( error& e )
256  {
257  LERROR( plog, "Exception thrown during write test:\n" << e.what() );
258  }
259 
260  return 0;
261 }
std::shared_ptr< header_wrapper > header_wrap_ptr
const char * what() const
std::shared_ptr< stream_wrapper > stream_wrap_ptr
int main(const int argc, const char **argv)
static scarab::logger plog("test_monarch3_write")
std::shared_ptr< monarch_wrapper > monarch_wrap_ptr
LOGGER(plog, "egg_writer")
Wrapper class for a monarch3::M3Monarch object.