casacore
Loading...
Searching...
No Matches
AntennaPairFile.h
Go to the documentation of this file.
1#ifndef CASACORE_ANTENNA_PAIR_FILE_H_
2#define CASACORE_ANTENNA_PAIR_FILE_H_
3
5
6#include <cassert>
7#include <limits>
8#include <stdexcept>
9#include <string>
10#include <vector>
11
12namespace casacore {
13
25 public:
26 AntennaPairFile() noexcept = default;
27
29 : file_(std::move(source.file_)),
31 data_(source.data_) {
32 source.rows_in_pattern_ = 0;
33 source.data_.clear();
34 }
35
36 ~AntennaPairFile() noexcept { Close(); }
37
39 Close();
40 file_ = std::move(rhs.file_);
41 rows_in_pattern_ = rhs.rows_in_pattern_;
42 data_ = rhs.data_;
43 rhs.rows_in_pattern_ = 0;
44 rhs.data_.clear();
45 return *this;
46 }
47
51 static AntennaPairFile CreateNew(const std::string& filename) {
52 return AntennaPairFile(filename);
53 }
54
59 static AntennaPairFile OpenExisting(const std::string& filename) {
60 return AntennaPairFile(filename, true);
61 }
62
63 void WriteAntenna1(uint64_t row, int32_t antenna1) {
64 WriteAntenna<0>(row, antenna1);
65 }
66
67 void WriteAntenna2(uint64_t row, int32_t antenna2) {
68 WriteAntenna<1>(row, antenna2);
69 }
70
71 void WritePair(uint64_t row, int32_t antenna1, int32_t antenna2) {
72 WriteAntenna<0>(row, antenna1);
73 WriteAntenna<1>(row, antenna2);
74 }
75
76 int32_t ReadAntenna1(uint64_t row) { return ReadAntenna<0>(row); }
77
78 int32_t ReadAntenna2(uint64_t row) { return ReadAntenna<1>(row); }
79
80 void Close() {
81 if (file_.IsOpen()) {
82 if (rows_in_pattern_ == 0) {
84 WriteData();
85 }
86 file_.Close();
88 data_.clear();
89 }
90 }
91
92 const std::string& Filename() const { return file_.Filename(); }
93
98 uint64_t NRowsInPattern() const { return rows_in_pattern_; }
99
100 private:
104 AntennaPairFile(const std::string& filename)
106 sizeof(int32_t) * 2)) {}
107
112 AntennaPairFile(const std::string& filename, bool /*open existing*/)
114 ReadHeader();
115 ReadData();
116 }
117
118 static bool HasUnsetAntenna(const std::array<int32_t, 2>& pair) {
119 return pair[0] == kUnsetAntenna || pair[1] == kUnsetAntenna;
120 }
121
125 template <size_t AntennaNumber>
126 void WriteAntenna(uint64_t row, int32_t antenna) {
127 static_assert(AntennaNumber == 0 || AntennaNumber == 1);
128 if (rows_in_pattern_ == 0) {
129 const bool has_unfinished_row =
130 !data_.empty() && HasUnsetAntenna(data_.back());
131 if (has_unfinished_row) {
132 if (row >= data_.size())
133 throw std::runtime_error(
134 "Incorrect writing order for AntennaPairFile (in unfinished "
135 "pair, row=" +
136 std::to_string(row) + ")");
137 const bool is_rewrite = row < data_.size() - 1 ||
138 data_.back()[AntennaNumber] != kUnsetAntenna;
139 if (is_rewrite) {
140 if (data_[row][AntennaNumber] != antenna)
141 throw std::runtime_error(
142 "Antenna " + std::to_string(AntennaNumber) + " value in row " +
143 std::to_string(row) + " is rewritten with a different value");
144 } else {
145 data_.back()[AntennaNumber] = antenna;
146 if (data_.back() == data_.front() && data_.size() > 1) {
147 // This row is the same as the first row, so this row is the first
148 // row of the next repeated pattern, and we have discovered the size
149 // of the pattern.
150 data_.pop_back();
151 rows_in_pattern_ = data_.size();
152 WriteHeader();
153 WriteData();
154 }
155 }
156 } else {
157 if (row > data_.size())
158 throw std::runtime_error(
159 "Incorrect writing order for AntennaPairFile (in new pair, row=" +
160 std::to_string(row) + ")");
161 if (row == data_.size()) {
162 if constexpr (AntennaNumber == 0)
163 data_.emplace_back(std::array<int32_t, 2>{antenna, kUnsetAntenna});
164 else
165 data_.emplace_back(std::array<int32_t, 2>{kUnsetAntenna, antenna});
166 } else {
167 // This is a rewrite of an already written value
168 if (data_[row][AntennaNumber] != antenna)
169 throw std::runtime_error(
170 "Antenna " + std::to_string(AntennaNumber) + " value in row " +
171 std::to_string(row) + " is rewritten with a different value");
172 }
173 }
174 } else {
175 const std::array<int32_t, 2>& pair = data_[row % rows_in_pattern_];
176 if (pair[AntennaNumber] != antenna)
177 throw std::runtime_error(
178 "Error writing to AntennaPairFile, row " + std::to_string(row) +
179 ": the antenna pairs do not follow a consistent pattern");
180 }
181 }
182
183 template <size_t AntennaNumber>
184 int32_t ReadAntenna(uint64_t row) {
185 static_assert(AntennaNumber == 0 || AntennaNumber == 1);
186 int32_t antenna;
187 if (rows_in_pattern_ == 0) {
188 if (row >= data_.size())
189 throw std::runtime_error(
190 "Invalid read of antenna pair: requested row is beyond the number "
191 "of written rows, and writing of antenna pattern not finished");
192 antenna = data_[row][AntennaNumber];
193 if (antenna == kUnsetAntenna)
194 throw std::runtime_error(
195 "Trying to read antenna value that has not been written yet");
196 return antenna;
197 } else {
198 return data_[row % rows_in_pattern_][AntennaNumber];
199 }
200 }
201
202 void ReadHeader() {
203 unsigned char data[kHeaderSize];
204 file_.ReadHeader(data);
205 if (!std::equal(data, data + 8, kMagicHeaderTag)) {
206 throw std::runtime_error(
207 "The Antenna-pair columnar file header does not have the expected "
208 "tag for antenna columns: the measurement set may be damaged");
209 }
210 rows_in_pattern_ = reinterpret_cast<uint64_t&>(data[8]);
211 }
212
213 void WriteHeader() {
214 unsigned char data[kHeaderSize];
215 std::copy_n(kMagicHeaderTag, 8, data);
216 reinterpret_cast<uint64_t&>(data[8]) = rows_in_pattern_;
217 file_.WriteHeader(data);
218 }
219
220 void ReadData() {
221 data_.resize(file_.NRows());
222 for (uint64_t row = 0; row != data_.size(); ++row) {
223 file_.Read(row, 0, data_[row].data(), 2);
224 }
225 }
226
227 void WriteData() {
228 // Note that rows_in_pattern_ might still be zero if the pattern has not
229 // been finished. This would happen if MS is partially written, reopened and
230 // written further. This is rather unlikely to happen, but it is easy to
231 // support.
232 for (uint64_t row = 0; row != data_.size(); ++row) {
233 file_.Write(row, 0, data_[row].data(), 2);
234 }
235 }
236
242 constexpr static size_t kHeaderSize = 16;
243 constexpr static const char kMagicHeaderTag[8] = "AntPair";
244 constexpr static int32_t kUnsetAntenna = std::numeric_limits<int32_t>::min();
249 uint64_t rows_in_pattern_ = 0;
250 std::vector<std::array<int32_t, 2>> data_;
251};
252
253} // namespace casacore
254
255#endif
This class is able to store the combination of ANTENNA1 and ANTENNA2 columns in a compressed manner.
int32_t ReadAntenna1(uint64_t row)
AntennaPairFile & operator=(AntennaPairFile &&rhs)
void WriteAntenna(uint64_t row, int32_t antenna)
static AntennaPairFile OpenExisting(const std::string &filename)
Open an already existing antenna-pair file from disk with the given filename.
int32_t ReadAntenna(uint64_t row)
std::vector< std::array< int32_t, 2 > > data_
uint64_t NRowsInPattern() const
The number of rows that form one repeating pattern.
void WritePair(uint64_t row, int32_t antenna1, int32_t antenna2)
const std::string & Filename() const
static bool HasUnsetAntenna(const std::array< int32_t, 2 > &pair)
static constexpr size_t kHeaderSize
The header: char[8] "AntPair\0" uint64_t rows_per_block.
AntennaPairFile() noexcept=default
AntennaPairFile(const std::string &filename, bool)
Open an existing file from disk.
BufferedColumnarFile file_
static AntennaPairFile CreateNew(const std::string &filename)
Create a new antenna-pair file on disk with the given filename.
AntennaPairFile(const std::string &filename)
Create a new file on disk.
void WriteAntenna1(uint64_t row, int32_t antenna1)
uint64_t rows_in_pattern_
This value remains zero until the repeating pattern was found.
void WriteAntenna2(uint64_t row, int32_t antenna2)
static constexpr int32_t kUnsetAntenna
int32_t ReadAntenna2(uint64_t row)
const std::string & Filename() const
this file contains all the compiler specific defines
Definition mainpage.dox:28
Define real & complex conjugation for non-complex types and put comparisons into std namespace.
Definition Complex.h:350