astrotypes  0.0
sigproc_cat.cpp
Go to the documentation of this file.
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2018 PulsarSearchSoft
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
26 #include <fstream>
27 void usage(const char* program_name)
28 {
29  std::cout << "Usage:\n"
30  << "\t" << program_name << " [options] input_filterbank_files... output_filterbank_file\n"
31  << "Synopsis:\n"
32  << "\tCatenates all input_filterbank_files to output_filterbank_file,\n"
33  << "\twith appropriatw src_file header field pointing to the first file\n"
34  << "Options:\n"
35  << "\t--as_time_series: save as multiple channels (time series)\n"
36  << "\t--as_filterbank : save as multiple spectra (filterbank)\n"
37  << "\t--help : this message\n";
38 }
39 
40 // Generates code for each DataType that we might encounter
41 template<typename SigProcTraits>
42 struct CatData
43 {
44  static
46  , std::ostream& output_file
47  , std::vector<std::string> files
48  , std::ifstream& input_file
49  , bool as_time_series)
50  {
51  // we choose to load in data 1000 samples at a time to keep down the memory requirements of the app
52  // Note that the number of samples in the file does not have to be a multiple of this value, the
53  // streamer adapters will adjust the chunk size as necessary if there are insufficent data to fill a whole
54  // data structure.
55  typename SigProcTraits::DataType data(number_of_channels,
57 
58  typename SigProcTraits::Adapter sigproc_adapter;
59 
60  unsigned file_index = 0;
61  while(true) {
62  while(!input_file.eof()) {
63  input_file >> sigproc_adapter >> data;
64  // we output the data as contiguos spectra. To output as a series of channles
65  // use the SigProcFormat<pss::astrotypes::units::Frequency, pss::astrotypes::units::Time>() adapter
66  if(as_time_series) {
67  output_file << pss::astrotypes::sigproc::SigProcFormat<pss::astrotypes::units::Frequency, pss::astrotypes::units::Time>() << data;
68  }
69  else
70  {
71  output_file << pss::astrotypes::sigproc::SigProcFormat<pss::astrotypes::units::Time, pss::astrotypes::units::Frequency>() << data;
72  }
73  }
74 
75  // read in the next file and check the haader is consistent
76  // with what we expect
77  if(++file_index < files.size()) {
78  input_file.open(files[file_index]);
80  input_file >> header;
81  if(header.number_of_bits() != sizeof(typename decltype(data)::value_type)) {
82  std::cerr << "Error: file " << files[file_index]
83  << " has " << header.number_of_bits() << " bit data."
84  << " (expecting " << sizeof(typename decltype(data)::value_type) << " bits)";
85  return 1;
86  }
87  continue;
88  }
89  return 0;
90  }
91  }
92 }; // end CatData struct
93 
94 int main(int argc, char** argv) {
95 
96  bool as_time_series = false;
97  bool as_filterbank = false;
98  std::vector<std::string> files;
99 
100  // process command line
101  for(int a=1; a < argc; ++a) {
102  if((char)argv[a][0] == '-') {
103  if(std::string("--as_time_series") == argv[a])
104  {
105  as_time_series = true;
106  }
107  if(std::string("--as_filterbank") == argv[a])
108  {
109  as_filterbank = true;
110  }
111  else if(std::string("--help") == argv[a])
112  {
113  usage(argv[0]);
114  return 0;
115  }
116  else {
117  std::cerr << "unknown parameter " << argv[a] << std::endl;
118  usage(argv[0]);
119  return 1;
120  }
121  }
122  files.push_back(argv[a]);
123  }
124  if(files.size() < 2) {
125  std::cerr << "Must specify one or more input files and an output file" << std::endl;
126  usage(argv[0]);
127  return 1;
128  }
129  if( as_time_series && as_filterbank ) {
130  std::cerr << "--as_filterbank and --as_time_series are mutually incompatible" << std::endl;
131  }
132  std::string out_file = files.back();
133  files.pop_back();
134 
135  // read in a sigproc file
136  std::ifstream input_file_stream(argv[1], std::ios::binary);
137 
139  input_file_stream >> header;
140 
141  // dump header information to the screen in a human readable format
142  std::cout << pss::astrotypes::sigproc::Header::Info() << header;
143 
144  // open the output stream and write out a suitable header
145  header.raw_data_file(files[0]); // we can set any/all the different parameters of the header
146 
147  if(as_time_series) {
149  }
150  if(as_filterbank) {
152  }
153  std::ofstream output_file(out_file);
154  output_file << header;
155 
156  // not launch the appropriate CatData::exec function that matches the header information
158  , header.number_of_channels()
159  , output_file
160  , files
161  , input_file_stream
162  , as_time_series);
163 
164 }
int main(int argc, char **argv)
Definition: sigproc_cat.cpp:94
Store SigProc header information.
Definition: Header.h:73
utils::Optional< std::string > const & raw_data_file() const
return the value of the raw data filename (if any)
Definition: Header.cpp:167
A compile time dimesion tagging of size_t.
Definition: DimensionSize.h:39
static bool exec(pss::astrotypes::DimensionSize< pss::astrotypes::units::Frequency > number_of_channels, std::ostream &output_file, std::vector< std::string > files, std::ifstream &input_file, bool as_time_series)
Definition: sigproc_cat.cpp:45
static void exec(Header const &h, Args &&...args)
Definition: DataFactory.h:178
void usage(const char *program_name)
Definition: sigproc_cat.cpp:27