Cheetah - SKA - PSS - Prototype Time Domain Search Pipeline
SigProcHeader.cpp
1 /*
2  * The MIT License (MIT)
3  *
4  * Copyright (c) 2016 The SKA organisation
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 "cheetah/sigproc/SigProcHeader.h"
25 #include "cheetah/data/Units.h"
26 #include "panda/Error.h"
27 #include <boost/units/quantity.hpp>
28 
29 #include <cstring>
30 #include <string>
31 #include <iostream>
32 #include <iomanip>
33 #include <limits>
34 #include <cmath>
35 
36 namespace ska {
37 namespace cheetah {
38 namespace sigproc {
39 
40 
41 // buffer for reading in data to pass to a std::string - TODO use stream buff directly
42 //static char _buffer[80];
43 
44 // strong typing for the Header labels
45 /*
46 struct SigProcLabel : public std::string
47 {
48  SigProcLabel() {}
49  SigProcLabel(std::string const& s) : std::string(s) {}
50  SigProcLabel(const char * s) : std::string(s) {}
51  std::size_t size() const {
52  return std::string::size() + sizeof(int32_t);
53  }
54 };
55 
56 // sigproc format types are preceded by the size
57 // read
58 template<typename T>
59 unsigned get_var(std::istream& stream, T& var) {
60  stream.read(reinterpret_cast<char*>(&var), sizeof(T));
61  return stream.gcount();
62 }
63 
64 template<>
65 unsigned get_var<SigProcHeader::FrequencyType>(std::istream& stream, SigProcHeader::FrequencyType& var) {
66  typedef SigProcHeader::FrequencyType::value_type ValueType;
67  stream.read(reinterpret_cast<char*>(const_cast<double*>(&var.value())), sizeof(ValueType));
68  return stream.gcount();
69 }
70 
71 // write
72 template<typename T>
73 unsigned write_var(std::ostream& stream, T const& var) {
74  stream.write(reinterpret_cast<const char*>(&var), sizeof(T));
75  return sizeof(T);
76 }
77 
78 // sigproc format string types are preceded by the string length
79 // read
80 unsigned get_var(std::istream& stream, std::string& var) {
81  // read in the size of the string
82  int32_t size=0;
83  stream.read(reinterpret_cast<char*>(&size), sizeof(size));
84  auto s = sizeof(int32_t);
85 
86  if( size < 0 || size > 80 ) {
87  panda::Error e("SigProc::read: illegal size of string: ");
88  e << size;
89  throw e;
90  }
91 
92  if(!stream.fail()) {
93  stream.read(_buffer, size);
94  s += stream.gcount();
95  var.assign(_buffer, size); // unnessasary copy we could get rid of if reqd
96  }
97  return s;
98 }
99 
100 // write
101 template<>
102 unsigned write_var(std::ostream& stream, std::string const& var) {
103 
104  int32_t size=static_cast<int32_t>(var.size());
105  if( size < 0 || size > 80 ) {
106  panda::Error e("SigProc::write: illegal size of string: ");
107  e << size;
108  throw e;
109  }
110  // write out the size of the string
111  unsigned s = write_var(stream, size);
112  stream.write(var.data(), size);
113  return size + s;
114 }
115 
116 std::istream& operator>>(std::istream& stream, SigProcLabel& var)
117 {
118  get_var(stream, static_cast<std::string&>(var));
119  return stream;
120 }
121 
122 std::ostream& operator<<(std::ostream& stream, SigProcLabel const& var)
123 {
124  write_var(stream, static_cast<std::string const&>(var));
125  return stream;
126 }
127 */
128 
129 SigProcHeader::SigProcHeader()
130 {
131  reset();
132 }
133 
134 SigProcHeader::~SigProcHeader()
135 {
136 }
137 
138 void SigProcHeader::start_time(typename utils::ModifiedJulianClock::time_point const& timestamp)
139 {
140  //_tstart = timestamp.time_since_epoch().count();
141  this->tstart(timestamp);
142 }
143 
144 std::vector<SigProcHeader::FrequencyType> const& SigProcHeader::frequency_channels() const
145 {
146  return BaseT::frequency_channels();
147 }
148 
149 void SigProcHeader::frequency_channels(std::vector<SigProcHeader::FrequencyType> const& freqs)
150 {
151  BaseT::frequency_channels(freqs);
152  if(freqs.size())
153  number_of_channels(freqs.size());
154 }
155 
156 typename utils::ModifiedJulianClock::time_point SigProcHeader::start_time() const {
157  if(this->tstart().is_set()) return *this->tstart();
158  return utils::ModifiedJulianClock::time_point();
159 }
160 
161 std::ostream& operator<<(std::ostream& os, SigProcHeader const& h)
162 {
163  os << pss::astrotypes::sigproc::Header::Info() << h;
164  // << "\tstart_time : " << std::setprecision(std::numeric_limits<double>::digits10 + 1) << h.start_time() << "\n"
165  return os;
166 }
167 
168 } // namespace sigproc
169 } // namespace cheetah
170 } // namespace ska
std::vector< FrequencyType > const & frequency_channels() const
returns the frequencies each channel represents
Some limits and constants for FLDO.
Definition: Brdz.h:35
struct for the header information of a SigProc file
Definition: SigProcHeader.h:43