astrotypes  0.0
Header.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  */
24 #include "HeaderField.cpp"
25 #include "HeaderFieldDataType.cpp"
26 #include <sstream>
27 #include <exception>
28 
29 namespace pss {
30 namespace astrotypes {
31 namespace sigproc {
32 
33 static const SigProcLabel telescope_id_label("telescope_id");
34 static const SigProcLabel machine_id_label("machine_id");
35 static const SigProcLabel data_type_label("data_type");
36 static const SigProcLabel raw_data_file_label("raw_data_file");
37 static const SigProcLabel source_name_label("source_name");
38 static const SigProcLabel barycentric_label("barycentric");
39 static const SigProcLabel pulsarcentric_label("pulsarcentric");
40 static const SigProcLabel az_start_label("az_start");
41 static const SigProcLabel za_start_label("za_start");
42 static const SigProcLabel src_raj_label("src_raj");
43 static const SigProcLabel src_dej_label("src_dej");
44 static const SigProcLabel tsamp_label("tsamp");
45 static const SigProcLabel tstart_label("tstart");
46 static const SigProcLabel nbits_label("nbits");
47 static const SigProcLabel nsamples_label("nsamples");
48 static const SigProcLabel fch1_label("fch1");
49 static const SigProcLabel foff_label("foff");
50 static const SigProcLabel fchannel_label("fchannel");
51 static const SigProcLabel fchannel_start_label("FREQUENCY_START");
52 static const SigProcLabel fchannel_end_label("FREQUENCY_END");
53 static const SigProcLabel nchans_label("nchans");
54 static const SigProcLabel nifs_label("nifs");
55 static const SigProcLabel refdm_label("refdm");
56 static const SigProcLabel period_label("period");
57 // non-standard, but common headers
58 static const SigProcLabel ibeam_label("ibeam");
59 static const SigProcLabel nbeams_label("nbeams");
60 
62  : _telescope_id(telescope_id_label, *this)
63  , _machine_id(machine_id_label, *this)
64  , _data_type(data_type_label, *this)
65  , _raw_data_file(raw_data_file_label, *this)
66  , _source_name(source_name_label, *this)
67  , _barycentric(barycentric_label, *this)
68  , _pulsarcentric(pulsarcentric_label, *this)
69  , _az_start(az_start_label, *this)
70  , _za_start(za_start_label, *this)
71  , _src_raj(src_raj_label, *this)
72  , _src_dej(src_dej_label, *this)
73  , _tsamp(tsamp_label, *this)
74  , _tstart(tstart_label, *this, _tsamp)
75  , _n_bits(nbits_label, *this)
76  , _nsamples(nsamples_label, *this)
77  , _fch1(fch1_label, *this)
78  , _foff(foff_label, *this)
79  , _freq_channels(fchannel_start_label, fchannel_label, fchannel_end_label, *this)
80  , _n_chans(nchans_label, *this)
81  , _nifs(nifs_label, *this)
82  , _refdm(refdm_label, *this)
83  , _period(period_label, *this)
84  , _ibeam(ibeam_label, *this)
85  , _nbeams(nbeams_label, *this)
86 {
87 }
88 
89 inline Header::Header(Header const& h)
90  : BaseT(h)
91  , _telescope_id(telescope_id_label, *this, h._telescope_id)
92  , _machine_id(machine_id_label, *this, h._machine_id)
93  , _data_type(data_type_label, *this, h._data_type)
94  , _raw_data_file(raw_data_file_label, *this, h._raw_data_file)
95  , _source_name(source_name_label, *this, h._source_name)
96  , _barycentric(barycentric_label, *this, h._barycentric)
97  , _pulsarcentric(pulsarcentric_label, *this, h._pulsarcentric)
98  , _az_start(az_start_label, *this, h._az_start)
99  , _za_start(za_start_label, *this, h._za_start)
100  , _src_raj(src_raj_label, *this, h._src_raj)
101  , _src_dej(src_dej_label, *this, h._src_dej)
102  , _tsamp(tsamp_label, *this, h._tsamp)
103  , _tstart(tstart_label, *this, _tsamp, h._tstart)
104  , _n_bits(nbits_label, *this, h._n_bits)
105  , _nsamples(nsamples_label, *this, h._nsamples)
106  , _fch1(fch1_label, *this, h._fch1)
107  , _foff(foff_label, *this, h._foff)
108  , _freq_channels(fchannel_start_label, fchannel_label, fchannel_end_label, *this, h._freq_channels)
109  , _n_chans(nchans_label, *this, h._n_chans)
110  , _nifs(nifs_label, *this, h._nifs)
111  , _refdm(refdm_label, *this, h._refdm)
112  , _period(period_label, *this, h._period)
113  , _ibeam(ibeam_label, *this, h._ibeam)
114  , _nbeams(nbeams_label, *this, h._nbeams)
115 {
116 }
117 
118 inline Header& Header::operator=(Header const& h)
119 {
120  return this->copy_header_values(h);
121 }
122 
123 template<typename T, typename Alloc>
124 Header::OStreamAdapter<astrotypes::TimeFrequency<T, Alloc>> Header::operator<<(astrotypes::TimeFrequency<T, Alloc> const& data)
125 {
126  _n_bits = sizeof(T) * 8;
127  _n_chans = data.template size<units::Frequency>();
128  return OStreamAdapter<astrotypes::TimeFrequency<T, Alloc>>(*this, data);
129 }
130 
131 template<typename T>
132 Header::OStreamAdapter<T>::OStreamAdapter(Header const& h, T const& data)
133  : _h(h)
134  , _d(data)
135 {
136 }
137 
138 template<typename T>
139 std::ostream& Header::OStreamAdapter<T>::operator<<(std::ostream& os) const
140 {
141  os << _h;
142  os << _d;
143  return os;
144 }
145 
146 // --------- getters and setters --------------------------
148 {
149  return _telescope_id;
150 }
151 
152 inline void Header::telescope_id(unsigned id)
153 {
154  _telescope_id = id;
155 }
156 
158 {
159  return _machine_id;
160 }
161 
162 inline void Header::machine_id(unsigned id)
163 {
164  _machine_id = id;
165 }
166 
168 {
169  return _raw_data_file;
170 }
171 
173 {
174  switch(data_type()) {
176  this->remove_compare_field(nsamples_label);
177  this->add_compare_field(nchans_label);
178  break;
180  this->remove_compare_field(nchans_label);
181  this->add_compare_field(nsamples_label);
182  break;
183  default:
184  throw std::runtime_error("unknown datatype in sigproc");
185  }
186 }
187 
189 {
190  bool do_update=true;
191  if(data_type() == type) {
192  do_update=false;
193  }
194 
195  _data_type = static_cast<unsigned>(type);
196 
197  if(do_update) updated_data_type();
198 }
199 
201 {
202  if(_data_type.is_set()) return static_cast<DataType>((int)_data_type);
203  return DataType::Undefined;
204 }
205 
206 inline void Header::raw_data_file(std::string const& filename)
207 {
208  _raw_data_file = filename;
209 }
210 
212 {
213  return _source_name;
214 }
215 
216 inline void Header::source_name(std::string const& name)
217 {
218  _source_name = name;
219 }
220 
222 {
223  return _barycentric;
224 }
225 
226 inline void Header::barycentric(bool b)
227 {
228  _barycentric = b;
229 }
230 
232 {
233  return _pulsarcentric;
234 }
235 
236 inline void Header::pulsarcentric(bool b)
237 {
238  _pulsarcentric = b;
239 }
240 
242 {
243  return _az_start;
244 }
245 
246 inline void Header::az_start(boost::units::quantity<units::Degree, double> const& v)
247 {
248  _az_start = v;
249 }
250 
252 {
253  return _za_start;
254 }
255 
256 inline void Header::za_start(boost::units::quantity<units::Degree, double> const& v)
257 {
258  _za_start = v;
259 }
260 
262 {
263  return _tstart;
264 }
265 
267 {
268  _tstart = mjd;
269 }
270 
271 inline boost::units::quantity<units::Seconds, double> Header::sample_interval() const
272 {
273  if(_tsamp.is_set()) return _tsamp;
274  return boost::units::quantity<units::Seconds, double>(0.0 * units::seconds);
275 }
276 
277 inline void Header::sample_interval(boost::units::quantity<units::Seconds, double> tsamp)
278 {
279  _tsamp = tsamp;
280 }
281 
283 {
284  return _fch1;
285 }
286 
287 inline void Header::fch1(boost::units::quantity<units::MegaHertz, double> const& f)
288 {
289  _fch1 = f;
290 }
291 
293 {
294  return _foff;
295 }
296 
297 inline void Header::foff(boost::units::quantity<units::MegaHertz, double> const& f)
298 {
299  _foff = f;
300 }
301 
302 inline std::vector<boost::units::quantity<units::MegaHertz, double>> const& Header::frequency_channels() const
303 {
304  return *_freq_channels;
305 }
306 
307 inline void Header::frequency_channels(std::vector<boost::units::quantity<units::MegaHertz, double>> const& frequency_channels)
308 {
309  *_freq_channels = frequency_channels;
310 }
311 
313 {
314  return _refdm;
315 }
316 
318 {
319  _refdm = dm;
320 }
321 
323 {
324  return _period;
325 }
326 
327 inline void Header::period(boost::units::quantity<units::Seconds, double> p)
328 {
329  _period = p;
330 }
331 
333 {
334  if(_n_chans.is_set()) return DimensionSize<units::Frequency>(_n_chans);
336 }
337 
338 inline void Header::number_of_channels(std::size_t n)
339 {
340  _n_chans = n;
341 }
342 
344 {
345  _n_chans = static_cast<std::size_t>(n);
346 }
347 
348 inline unsigned Header::number_of_bits() const
349 {
350  if(_n_bits.is_set()) return _n_bits;
351  return 0;
352 }
353 
354 inline void Header::number_of_bits(unsigned n)
355 {
356  _n_bits = n;
357 }
358 
360 {
361  return _nsamples;
362 }
363 
364 inline void Header::number_of_samples(unsigned n)
365 {
366  _nsamples = n;
367 }
368 
369 inline void Header::number_of_ifs(unsigned i)
370 {
371  _nifs = i;
372 }
373 
374 inline unsigned Header::number_of_ifs() const
375 {
376  if(_nifs) return _nifs;
377  return 2;
378 }
379 
381 {
382  return _ibeam;
383 }
384 
386 {
387  return _nbeams;
388 }
389 
390 
391 inline void Header::info(std::ostream& os) const
392 {
393  os << Info() << *this;
394 }
395 
396 template<typename Stream>
397 inline Header::InfoSentry<Stream> operator<<(Stream& os, Header::Info const& adapter)
398 {
399  return adapter.sentry(os);
400 }
401 
402 
403 } // namespace sigproc
404 } // namespace astrotypes
405 } // namespace pss
boost::units::quantity< units::Seconds, double > sample_interval() const
return the sample_interval (if defined)
Definition: Header.cpp:271
unsigned number_of_ifs() const
return the number of IF streams (e.g. polarisations) in each channel
Definition: Header.cpp:374
unsigned number_of_bits() const
return the number of bits
Definition: Header.cpp:348
utils::Optional< unsigned > const & number_of_samples() const
return the number of time samples (optional)
Definition: Header.cpp:359
utils::Optional< unsigned > nbeams() const
return the number of beams used in an abservation
Definition: Header.cpp:385
utils::Optional< boost::units::quantity< units::Seconds, double > > const & period() const
return the folding period (if defined)
Definition: Header.cpp:322
utils::Optional< std::string > const & source_name() const
return the value of the source name (if any)
Definition: Header.cpp:211
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
utils::Optional< unsigned > const & barycentric() const
the value of the barycentric field (if any)
Definition: Header.cpp:221
void remove_compare_field(SigProcLabel const &)
remvoe a field to be checked in operator==
DataType data_type() const
set the data type
Definition: Header.cpp:200
boost::units::quantity< DispersionMeasureUnit, T > DispersionMeasure
utils::Optional< boost::units::quantity< units::Degree, double > > az_start() const
getters/setters for the telscope azimuth at start of scan (in degrees)
Definition: Header.cpp:241
void add_compare_field(SigProcLabel const &)
add a field to be checked in operator==
std::vector< boost::units::quantity< units::MegaHertz, double > > const & frequency_channels() const
return a list of frequency channels (if set)
Definition: Header.cpp:302
utils::Optional< unsigned > telescope_id() const
return the telescope_id identifier (if any)
Definition: Header.cpp:147
DimensionSize< units::Frequency > number_of_channels() const
return the number of frequency channels
Definition: Header.cpp:332
utils::Optional< boost::units::quantity< units::MegaHertz, double > > const & fch1() const
get the frequency of the first channel
Definition: Header.cpp:282
Header & operator=(Header const &)
Definition: Header.cpp:118
utils::Optional< unsigned > machine_id() const
return the machine_id identifier (if any)
Definition: Header.cpp:157
utils::Optional< unsigned > ibeam() const
return the beam index number
Definition: Header.cpp:380
utils::Optional< boost::units::quantity< units::MegaHertz, double > > const & foff() const
get the band width of a channel (assumes homogenous badwidth for each channel)
Definition: Header.cpp:292
utils::Optional< units::ModifiedJulianDate > const & tstart() const
return the date of the start of the data block
Definition: Header.cpp:261
utils::Optional< boost::units::quantity< units::Degree, double > > za_start() const
getters/setters for the telscope zenith at start of scan (in degrees)
Definition: Header.cpp:251
utils::Optional< units::DispersionMeasure< double > > const & ref_dm() const
return the reference Dispersion Measure
Definition: Header.cpp:312
bool is_set() const override
return true if the variable has been set, false otherwise
Definition: HeaderField.cpp:96
Header::InfoSentry< Stream > operator<<(Stream &os, Header::Info const &adapter)
Definition: Header.cpp:397
void info(std::ostream &stream) const
human readable summary of the header written to the stream
Definition: Header.cpp:391
Header & copy_header_values(Headerconst &dst)
copy values from one header to another
ModifiedJulianClock::time_point ModifiedJulianDate
Definition: Time.h:46
utils::Optional< unsigned > const & pulsarcentric() const
the value of the pulsarcentric field (if any)
Definition: Header.cpp:231