astrotypes  0.0
FileReader.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 
25 namespace pss {
26 namespace astrotypes {
27 namespace sigproc {
28 
29 template<typename HeaderType>
31 {
32 }
33 
34 template<typename HeaderType>
35 FileReader<HeaderType>::FileReader(std::string const& file_name)
36 {
37  do_open(file_name);
38 }
39 
40 template<typename HeaderType>
42 {
43 }
44 
45 template<typename HeaderType>
46 void FileReader<HeaderType>::open(std::string const& file_name)
47 {
48  if(_stream.is_open()) _stream.close();
49  do_open(file_name);
50 }
51 
52 template<typename HeaderType>
53 void FileReader<HeaderType>::do_open(std::string const& file_name)
54 {
55  _file_name = file_name;
56  _stream.open(_file_name, std::ios::in | std::ios::binary);
57  if(!_stream) throw std::runtime_error(file_name + " failed to open");
58  this->new_header(_stream);
59 }
60 
61 template<typename HeaderType>
63 {
64  struct stat file_info;
65  stat(_file_name.c_str(), &file_info);
66  return ((file_info.st_size - this->_header.size())*(8.0/this->_header.number_of_bits()));
67 }
68 
69 template<typename HeaderType>
70 template<typename DataType>
71 typename std::enable_if<has_dimensions<DataType, units::Time, units::Frequency>::value, FileReader<HeaderType>>::type &
73 {
74  BaseT::read(_stream, data);
75  return *this;
76 }
77 
78 namespace {
79 // helpers for the dimesion method
80 template<typename Dimension>
81 struct DimensionHelper {
82  template<typename HeaderType>
83  inline static DimensionSize<Dimension> exec(FileReader<HeaderType> const&)
84  {
85  return DimensionSize<Dimension>(0);
86  }
87 };
88 
89 template<>
90 struct DimensionHelper<units::Time> {
91  template<typename HeaderType>
92  inline static DimensionSize<units::Time> exec(FileReader<HeaderType> const& fr)
93  {
94  return DimensionSize<units::Time>(fr.number_of_data_points()/ (fr.header().number_of_ifs() * fr.header().number_of_channels()));
95  }
96 };
97 
98 template<>
99 struct DimensionHelper<units::Frequency> {
100  template<typename HeaderType>
101  inline static DimensionSize<units::Frequency> exec(FileReader<HeaderType> const& fr)
102  {
103  return fr.header().number_of_channels();
104  }
105 };
106 
107 } // namespace
108 template<typename HeaderType>
109 template<typename Dimension>
111 {
112  return DimensionHelper<Dimension>::exec(*this);
113 }
114 
115 } // namespace sigproc
116 } // namespace astrotypes
117 } // namespace pss
std::size_t number_of_data_points() const
the number of data points in the file (i.e number of data points of nbits)
Definition: FileReader.cpp:62
std::enable_if< has_dimensions< DataType, units::Time, units::Frequency >::value, FileReader >::type & operator>>(DataType &data)
read in to the provided data object
boost::units::time_dimension Time
Definition: Time.h:42
Read in a sigproc file.
Definition: FileReader.h:41
void open(std::string const &file_name)
set the filename to read
Definition: FileReader.cpp:46
DimensionSize< Dimension > dimension() const
return the expected dimension of the data in the file
Definition: FileReader.cpp:110
A compile time dimesion tagging of size_t.
Definition: DimensionSize.h:39
void do_open(std::string const &file_name)
Definition: FileReader.cpp:53
boost::units::frequency_dimension Frequency
Definition: Frequency.h:45