DP3
BlobArray.h
Go to the documentation of this file.
1 // BlobArray.h: Blob handling for arrays
2 //
3 // Copyright (C) 2020 ASTRON (Netherlands Institute for Radio Astronomy)
4 // SPDX-License-Identifier: GPL-3.0-or-later
5 
6 #ifndef LOFAR_BLOB_BLOBARRAY_H
7 #define LOFAR_BLOB_BLOBARRAY_H
8 
9 #include "BlobOStream.h"
10 #include "BlobIStream.h"
11 #include <vector>
12 #if defined(HAVE_BLITZ)
13 #include <blitz/array.h>
14 #endif
15 #if defined(HAVE_AIPSPP)
16 #include <casacore/casa/Arrays/Array.h>
17 #endif
18 
19 namespace dp3 {
20 namespace blob {
21 
24 
43 
49 template <typename T>
50 BlobOStream& putBlobArray(BlobOStream& bs, const T* data, const uint64_t* shape,
51  uint16_t ndim, bool fortranOrder);
52 template <typename T>
53 BlobOStream& putBlobVector(BlobOStream& bs, const T* data, uint64_t size);
55 
66 template <typename T>
67 uint64_t setSpaceBlobArray1(BlobOStream& bs, bool useBlobHeader, uint64_t size0,
68  unsigned int alignment = 0);
69 template <typename T>
70 uint64_t setSpaceBlobArray2(BlobOStream& bs, bool useBlobHeader, uint64_t size0,
71  uint64_t size1, bool fortranOrder,
72  unsigned int alignment = 0);
73 template <typename T>
74 uint64_t setSpaceBlobArray3(BlobOStream& bs, bool useBlobHeader, uint64_t size0,
75  uint64_t size1, uint64_t size2, bool fortranOrder,
76  unsigned int alignment = 0);
77 template <typename T>
78 uint64_t setSpaceBlobArray4(BlobOStream& bs, bool useBlobHeader, uint64_t size0,
79  uint64_t size1, uint64_t size2, uint64_t size3,
80  bool fortranOrder, unsigned int alignment = 0);
81 template <typename T>
82 uint64_t setSpaceBlobArray(BlobOStream& bs, bool useBlobHeader,
83  const std::vector<uint64_t>& shape,
84  bool fortranOrder, unsigned int alignment = 0);
85 template <typename T>
86 uint64_t setSpaceBlobArray(BlobOStream& bs, bool useBlobHeader,
87  const uint64_t* shape, uint16_t ndim,
88  bool fortranOrder, unsigned int alignment = 0);
90 
91 #if defined(HAVE_BLITZ)
93 template <typename T, unsigned int N>
94 BlobOStream& operator<<(BlobOStream&, const blitz::Array<T, N>&);
95 
100 template <typename T, unsigned int N>
101 BlobIStream& operator>>(BlobIStream&, blitz::Array<T, N>&);
102 #endif
103 
104 #if defined(HAVE_AIPSPP)
106 template <typename T>
107 BlobOStream& operator<<(BlobOStream&, const casacore::Array<T>&);
108 
112 template <typename T>
113 BlobIStream& operator>>(BlobIStream&, casacore::Array<T>&);
114 
117 BlobOStream& operator<<(BlobOStream&, const casacore::IPosition&);
118 BlobIStream& operator>>(BlobIStream&, casacore::IPosition&);
120 #endif
121 
124 BlobOStream& operator<<(BlobOStream&, const std::vector<bool>&);
125 template <typename T>
126 BlobOStream& operator<<(BlobOStream&, const std::vector<T>&);
128 
133 BlobIStream& operator>>(BlobIStream&, std::vector<bool>&);
134 template <typename T>
135 BlobIStream& operator>>(BlobIStream&, std::vector<T>&);
137 
142 template <typename T>
144  std::vector<uint64_t>& shape, bool fortranOrder);
145 
155 template <typename T>
156 uint64_t getSpaceBlobArray(BlobIStream& bs, bool useBlobHeader,
157  std::vector<uint64_t>& shape, bool fortranOrder);
158 
160 template <typename T>
161 inline uint64_t setSpaceBlobArray1(BlobOStream& bs, bool useBlobHeader,
162  uint64_t size0, unsigned int alignment) {
163  return setSpaceBlobArray<T>(bs, useBlobHeader, &size0, 1, true, alignment);
164 }
165 
167 template <typename T>
168 inline BlobOStream& operator<<(BlobOStream& bs, const std::vector<T>& vec) {
169  return putBlobVector(bs, vec.empty() ? nullptr : &(vec[0]), vec.size());
170 }
171 
173 template <typename T>
174 inline BlobOStream& putBlobVector(BlobOStream& bs, const T* vec,
175  uint64_t size) {
176  return putBlobArray(bs, vec, &size, 1, true);
177 }
178 
182 uint64_t putBlobArrayHeader(BlobOStream& bs, bool useBlobHeader,
183  const std::string& headerName,
184  const uint64_t* shape, uint16_t ndim,
185  bool fortranOrder, unsigned int alignment);
186 
190 inline unsigned int getBlobArrayStart(BlobIStream& bs, bool& fortranOrder,
191  uint16_t& ndim) {
192  unsigned char nalign;
193  bs >> fortranOrder >> nalign >> ndim;
194  return nalign;
195 }
196 
198 void convertArrayHeader(common::DataFormat, char* header, bool useBlobHeader);
199 
203 uint64_t getBlobArrayShape(BlobIStream& bs, uint64_t* shape, unsigned int ndim,
204  bool swapAxes, unsigned int nalign);
205 
208 template <typename T>
209 void putBlobArrayData(BlobOStream& bs, const T* data, uint64_t nr);
210 
212 template <typename T>
213 void getBlobArrayData(BlobIStream& bs, T* data, uint64_t nr);
214 
216 #define BLOBARRAY_PUTGET_SPEC(TP) \
217  template <> \
218  inline void putBlobArrayData(BlobOStream& bs, const TP* data, uint64_t nr) { \
219  bs.put(data, nr); \
220  } \
221  template <> \
222  inline void getBlobArrayData(BlobIStream& bs, TP* data, uint64_t nr) { \
223  bs.get(data, nr); \
224  }
226 BLOBARRAY_PUTGET_SPEC(int8_t)
227 BLOBARRAY_PUTGET_SPEC(uint8_t)
228 BLOBARRAY_PUTGET_SPEC(int16_t)
229 BLOBARRAY_PUTGET_SPEC(uint16_t)
230 BLOBARRAY_PUTGET_SPEC(int32_t)
231 BLOBARRAY_PUTGET_SPEC(uint32_t)
232 BLOBARRAY_PUTGET_SPEC(int64_t)
233 BLOBARRAY_PUTGET_SPEC(uint64_t)
235 BLOBARRAY_PUTGET_SPEC(double)
236 BLOBARRAY_PUTGET_SPEC(std::complex<float>)
237 BLOBARRAY_PUTGET_SPEC(std::complex<double>)
238 BLOBARRAY_PUTGET_SPEC(std::string)
239 
240 
242 } // namespace blob
243 } // namespace dp3
244 
245 #include "BlobArray.tcc"
246 
247 using dp3::blob::operator<<;
248 using dp3::blob::operator>>;
258 
259 #endif
Input stream for a blob.
Definition: BlobIStream.h:43
Output stream for a blob.
Definition: BlobOStream.h:40
uint64_t size() const
Get the total size.
Definition: BlobOStream.h:54
void getBlobArrayData(BlobIStream &bs, T *data, uint64_t nr)
Helper function to get an array of data.
uint64_t setSpaceBlobArray4(BlobOStream &bs, bool useBlobHeader, uint64_t size0, uint64_t size1, uint64_t size2, uint64_t size3, bool fortranOrder, unsigned int alignment=0)
uint64_t getSpaceBlobArray(BlobIStream &bs, bool useBlobHeader, std::vector< uint64_t > &shape, bool fortranOrder)
BlobOStream & operator<<(BlobOStream &, const std::vector< bool > &)
void convertArrayHeader(common::DataFormat, char *header, bool useBlobHeader)
Convert the array header data.
BlobOStream & putBlobVector(BlobOStream &bs, const T *data, uint64_t size)
Put a C-style vector of values as an array.
Definition: BlobArray.h:174
BlobOStream & putBlobArray(BlobOStream &bs, const T *data, const uint64_t *shape, uint16_t ndim, bool fortranOrder)
unsigned int getBlobArrayStart(BlobIStream &bs, bool &fortranOrder, uint16_t &ndim)
Definition: BlobArray.h:190
uint64_t getBlobArrayShape(BlobIStream &bs, uint64_t *shape, unsigned int ndim, bool swapAxes, unsigned int nalign)
void putBlobArrayData(BlobOStream &bs, const T *data, uint64_t nr)
uint64_t setSpaceBlobArray1(BlobOStream &bs, bool useBlobHeader, uint64_t size0, unsigned int alignment=0)
Reserve space for a 1-dim array of the given size.
Definition: BlobArray.h:161
uint64_t putBlobArrayHeader(BlobOStream &bs, bool useBlobHeader, const std::string &headerName, const uint64_t *shape, uint16_t ndim, bool fortranOrder, unsigned int alignment)
BlobIStream & getBlobArray(BlobIStream &bs, T *&arr, std::vector< uint64_t > &shape, bool fortranOrder)
uint64_t setSpaceBlobArray(BlobOStream &bs, bool useBlobHeader, const std::vector< uint64_t > &shape, bool fortranOrder, unsigned int alignment=0)
uint64_t setSpaceBlobArray2(BlobOStream &bs, bool useBlobHeader, uint64_t size0, uint64_t size1, bool fortranOrder, unsigned int alignment=0)
#define BLOBARRAY_PUTGET_SPEC(TP)
Specializations for the standard types (including complex and string).
Definition: BlobArray.h:216
BlobIStream & operator>>(BlobIStream &, std::vector< bool > &)
uint64_t setSpaceBlobArray3(BlobOStream &bs, bool useBlobHeader, uint64_t size0, uint64_t size1, uint64_t size2, bool fortranOrder, unsigned int alignment=0)
DataFormat
This file defines an enum for the possible machine data formats.
Definition: DataFormat.h:24
This file has generic helper routines for testing steps.
Definition: AntennaConfig.h:53