casacore
Loading...
Searching...
No Matches
StokesIStManColumn.h
Go to the documentation of this file.
1#ifndef CASACORE_STOKES_I_ST_MAN_COLUMN_H_
2#define CASACORE_STOKES_I_ST_MAN_COLUMN_H_
3
4#include <casacore/tables/DataMan/StManColumn.h>
5
6#include <casacore/casa/Arrays/Array.h>
7#include <casacore/casa/Arrays/IPosition.h>
8
10
11#include <optional>
12
13namespace casacore {
14
15class StokesIStMan;
16
22template <typename T>
23void ExpandFromStokesI(T *data, size_t n) {
24 for (size_t i = 0; i != n; ++i) {
25 const size_t index = n - i - 1;
26 const T value = data[index];
27 data[index * 4] = value;
28 data[index * 4 + 1] = T();
29 data[index * 4 + 2] = T();
30 data[index * 4 + 3] = value;
31 }
32}
33
34template <>
35void ExpandFromStokesI(bool *data, size_t n) {
36 for (size_t i = 0; i != n; ++i) {
37 const size_t index = n - i - 1;
38 const bool value = data[index];
39 data[index * 4] = value;
40 data[index * 4 + 1] = value;
41 data[index * 4 + 2] = value;
42 data[index * 4 + 3] = value;
43 }
44}
45
53template <typename T>
54inline T *TransformToStokesI(const T *input, char *buffer, size_t n) {
55 for (size_t i = 0; i != n; ++i) {
56 // Placement new is used, because the lifetime of type T needs
57 // to be started.
58 new (&buffer[i * sizeof(T)]) T((input[i * 4] + input[i * 4 + 3]) * T(0.5));
59 if (input[i * 4 + 1] != T(0.0) || input[i * 4 + 2] != T(0.0))
60 throw std::runtime_error(
61 "Stokes-I stman cannot store data for which the 2nd and 3rd "
62 "correlation are non-zero");
63 // While we could also check whether pp == qq, this is a bit more
64 // complicated because of rounding inaccuracies. The above check should
65 // catch the most crucial misuse of the stman, so a pp == qq check is not
66 // performed.
67 }
68 return reinterpret_cast<T *>(buffer);
69}
70
71template <>
72inline bool *TransformToStokesI(const bool *input, char *buffer, size_t n) {
73 for (size_t i = 0; i != n; ++i) {
74 const bool a = input[i * 4];
75 const bool b = input[i * 4 + 1];
76 const bool c = input[i * 4 + 2];
77 const bool d = input[i * 4 + 3];
78 if (a != b || a != c || a != d)
79 throw std::runtime_error(
80 "Stokes-I stman cannot store boolean data for which not all "
81 "correlations are equal");
82 new (&buffer[i]) bool(a);
83 }
84 return reinterpret_cast<bool *>(buffer);
85}
86
92 public:
99 casacore::DataType dtype)
100 : casacore::StManColumn(dtype), parent_(parent), file_(file) {}
101
106 casacore::Bool isWritable() const final { return true; }
107
110 if (shape.size() != 2) {
111 throw std::runtime_error("StokesIStMan is used for a column with " +
112 std::to_string(shape.size()) +
113 " dimensions, but it can only be used for "
114 "columns with exactly 2 dimensions");
115 }
116 shape_ = shape;
117 updateStride();
118 }
119
124 const casacore::IPosition &shape() const { return shape_; }
125
132 casacore::Array<casacore::Complex> *dataPtr) final {
133 getArrayGeneric(rowNr, dataPtr);
134 }
135
137 casacore::Array<float> *dataPtr) final {
138 getArrayGeneric(rowNr, dataPtr);
139 }
140
142 casacore::Array<casacore::Bool> *dataPtr) final {
143 getArrayGeneric(rowNr, dataPtr);
144 }
145
152 casacore::uInt rowNr,
153 const casacore::Array<casacore::Complex> *dataPtr) final {
154 putArrayGeneric(rowNr, dataPtr);
155 }
157 casacore::uInt rowNr,
158 const casacore::Array<casacore::DComplex> *dataPtr) final {
159 putArrayGeneric(rowNr, dataPtr);
160 }
162 const casacore::Array<double> *dataPtr) final {
163 putArrayGeneric(rowNr, dataPtr);
164 }
166 const casacore::Array<float> *dataPtr) final {
167 putArrayGeneric(rowNr, dataPtr);
168 }
170 const casacore::Array<casacore::Bool> *dataPtr) final {
171 putArrayGeneric(rowNr, dataPtr);
172 }
173
174 void setOffset(uint64_t column_offset) { column_offset_ = column_offset; }
175 uint64_t getStoredSizeInBytes() const {
176 if (dtype() == casacore::TpBool) {
177 return (shape_[1] + 7) / 8;
178 } else {
179 const uint64_t type_size = SizeOfType(dtype());
180 return shape_[1] * type_size;
181 }
182 }
183
184 private:
185 StokesIStManColumn(const StokesIStManColumn &source) = delete;
186 void operator=(const StokesIStManColumn &source) = delete;
187
188 template <typename T>
190 bool ownership;
191 T *storage = dataPtr->getStorage(ownership);
192 const size_t n_values = shape_[1];
193 file_.Read(rowNr, column_offset_, storage, n_values);
194 ExpandFromStokesI(storage, n_values);
195 dataPtr->putStorage(storage, ownership);
196 }
197
198 template <typename T>
200 const casacore::Array<T> *dataPtr) {
201 bool ownership;
202 const T *storage = dataPtr->getStorage(ownership);
203 const size_t n_values = shape_[1];
204 buffer_.resize(n_values * sizeof(T));
205 T *buffer = TransformToStokesI(storage, buffer_.data(), n_values);
206 file_.Write(rowNr, column_offset_, buffer, n_values);
207 dataPtr->freeStorage(storage, ownership);
208 }
209 void updateStride();
210
215 std::vector<char> buffer_;
216};
217} // namespace casacore
218
219#include "StokesIStMan.h"
220
221namespace casacore {
222
226
227} // namespace casacore
228
229#endif
void freeStorage(const T *&storage, bool deleteIt) const
If deleteIt is set, delete "storage".
void putStorage(T *&storage, bool deleteAndCopy)
putStorage() is normally called after a call to getStorage() (cf).
T * getStorage(bool &deleteIt)
Generally use of this should be shunned, except to use a FORTRAN routine or something similar.
size_t size() const
Definition IPosition.h:570
void resize(size_t newSize, bool copy=true)
Old values are copied on resize if copy==true.
Base class for columns of the StokesIStMan.
void putArrayfloatV(casacore::uInt rowNr, const casacore::Array< float > *dataPtr) final
void putArrayDComplexV(casacore::uInt rowNr, const casacore::Array< casacore::DComplex > *dataPtr) final
void getArrayGeneric(casacore::uInt rowNr, casacore::Array< T > *dataPtr)
void getArrayfloatV(casacore::uInt rowNr, casacore::Array< float > *dataPtr) final
void getArrayComplexV(casacore::uInt rowNr, casacore::Array< casacore::Complex > *dataPtr) final
Read the values for a particular row.
void setOffset(uint64_t column_offset)
const casacore::IPosition & shape() const
void putArrayGeneric(casacore::uInt rowNr, const casacore::Array< T > *dataPtr)
StokesIStManColumn(StokesIStMan &parent, BufferedColumnarFile &file, casacore::DataType dtype)
Constructor, to be overloaded by subclass.
void getArrayBoolV(casacore::uInt rowNr, casacore::Array< casacore::Bool > *dataPtr) final
Get the array value in the given row.
casacore::IPosition shape(casacore::rownr_t) final
Get the shape of the item in the given row.
void putArraydoubleV(casacore::uInt rowNr, const casacore::Array< double > *dataPtr) final
void putArrayBoolV(casacore::uInt rowNr, const casacore::Array< casacore::Bool > *dataPtr) final
Put the array value into the given row.
StokesIStManColumn(const StokesIStManColumn &source)=delete
casacore::Bool isWritable() const final
Whether this column is writable.
void operator=(const StokesIStManColumn &source)=delete
void putArrayComplexV(casacore::uInt rowNr, const casacore::Array< casacore::Complex > *dataPtr) final
Write values into a particular row.
void setShapeColumn(const casacore::IPosition &shape) final
Set the dimensions of values in this column.
casacore::IPosition shape(casacore::uInt) final
Get the dimensions of the values in a particular row.
The Stokes I storage manager behaves like a full set of (4) polarizations but only stores the Stokes ...
uint64_t CalculateAndUpdateStride()
void Read(uint64_t row, uint64_t column_offset, float *data, uint64_t n)
Read one cell containing an array of floats.
void Write(uint64_t row, uint64_t column_offset, const float *data, uint64_t n)
Write one cell containing an array of floats.
this file contains all the compiler specific defines
Definition mainpage.dox:28
unsigned int uInt
Definition aipstype.h:49
T * TransformToStokesI(const T *input, char *buffer, size_t n)
Calculates for every set of 4 input values the Stokes-I values by doing out = 0.5 * (in_pp + in_qq),...
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
LatticeExprNode value(const LatticeExprNode &expr)
This function returns the value of the expression without a mask.
void ExpandFromStokesI(T *data, size_t n)
Expands n values from single Stokes I values to have 4 values, in place.
uInt64 rownr_t
Define the type of a row number in a table.
Definition aipsxtype.h:44