| 1 |
/* |
| 2 |
* This file is part of libcxxsupport. |
| 3 |
* |
| 4 |
* libcxxsupport is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
* |
| 9 |
* libcxxsupport is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with libcxxsupport; if not, write to the Free Software |
| 16 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 |
*/ |
| 18 |
|
| 19 |
/* |
| 20 |
* libcxxsupport is being developed at the Max-Planck-Institut fuer Astrophysik |
| 21 |
* and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt |
| 22 |
* (DLR). |
| 23 |
*/ |
| 24 |
|
| 25 |
/*! \file fitshandle.h |
| 26 |
* Declaration of the FITS I/O helper class used by LevelS |
| 27 |
* |
| 28 |
* Copyright (C) 2002-2014 Max-Planck-Society |
| 29 |
* \author Martin Reinecke |
| 30 |
*/ |
| 31 |
|
| 32 |
#ifndef PLANCK_FITSHANDLE_H |
| 33 |
#define PLANCK_FITSHANDLE_H |
| 34 |
|
| 35 |
#include <string> |
| 36 |
#include <vector> |
| 37 |
#include "arr.h" |
| 38 |
#include "datatypes.h" |
| 39 |
#include "safe_cast.h" |
| 40 |
|
| 41 |
/*! \defgroup fitsgroup FITS-related functionality */ |
| 42 |
/*! \{ */ |
| 43 |
|
| 44 |
/*! Class containing information about a single column in a FITS table. */ |
| 45 |
class fitscolumn |
| 46 |
{ |
| 47 |
private: |
| 48 |
std::string name_, unit_; |
| 49 |
int64 repcount_; |
| 50 |
PDT type_; |
| 51 |
|
| 52 |
public: |
| 53 |
fitscolumn(); |
| 54 |
/*! Creates a \a fitscolumn with name \a nm, unit \a un, a repetition |
| 55 |
count of \a rc, and a Planck type of \a tp. */ |
| 56 |
fitscolumn (const std::string &nm, const std::string &un, int64 rc, PDT tp); |
| 57 |
~fitscolumn(); |
| 58 |
|
| 59 |
/*! Returns the column name. */ |
| 60 |
const std::string &name() const {return name_;} |
| 61 |
/*! Returns the column unit string. */ |
| 62 |
const std::string &unit() const {return unit_;} |
| 63 |
/*! Returns the repetition count of the column. */ |
| 64 |
int64 repcount() const {return repcount_;} |
| 65 |
/*! Returns the Planck type of the column. */ |
| 66 |
PDT type() const {return type_;} |
| 67 |
}; |
| 68 |
|
| 69 |
/*! Class for performing I/O from/to FITS files. */ |
| 70 |
class fitshandle |
| 71 |
{ |
| 72 |
private: |
| 73 |
enum { INVALID = -4711 }; |
| 74 |
|
| 75 |
mutable int status; |
| 76 |
void *fptr; |
| 77 |
int hdutype_, bitpix_; |
| 78 |
std::vector<int64> axes_; |
| 79 |
std::vector<fitscolumn> columns_; |
| 80 |
int64 nrows_; |
| 81 |
|
| 82 |
void check_errors() const; |
| 83 |
|
| 84 |
void clean_data(); |
| 85 |
void clean_all(); |
| 86 |
|
| 87 |
bool connected() const { return (hdutype_!=INVALID); } |
| 88 |
bool table_hdu (tsize col) const; |
| 89 |
bool image_hdu () const; |
| 90 |
|
| 91 |
void init_image(); |
| 92 |
void init_asciitab(); |
| 93 |
void init_bintab(); |
| 94 |
void init_data(); |
| 95 |
|
| 96 |
void read_col (int colnum, void *data, int64 ndata, PDT type, |
| 97 |
int64 offset) const; |
| 98 |
void write_col (int colnum, const void *data, int64 ndata, PDT type, |
| 99 |
int64 offset); |
| 100 |
|
| 101 |
void getKeyHelper(const std::string &name) const; |
| 102 |
|
| 103 |
public: |
| 104 |
/*! the list of modes in which a \a fitshandle can be opened. */ |
| 105 |
enum openmethod { CREATE, /*!< the file must not yet exist */ |
| 106 |
OPEN /*!< the file must already exist */ |
| 107 |
}; |
| 108 |
|
| 109 |
/*! \name File-level access and manipulation. */ |
| 110 |
/*! \{ */ |
| 111 |
|
| 112 |
/*! Creates an unconnected \a fitshandle. */ |
| 113 |
fitshandle (); |
| 114 |
/*! Performs all necessary cleanups. */ |
| 115 |
~fitshandle(); |
| 116 |
|
| 117 |
/*! Connects to the file \a fname. */ |
| 118 |
void open (const std::string &fname); |
| 119 |
/*! Creates the file \a fname and connects to it. */ |
| 120 |
void create (const std::string &fname); |
| 121 |
/*! Closes the current file. */ |
| 122 |
void close () { clean_all(); } |
| 123 |
/*! Deletes the file with name \a name. */ |
| 124 |
static void delete_file (const std::string &name); |
| 125 |
/*! Returns the name of the connected file. */ |
| 126 |
std::string fileName() const; |
| 127 |
/*! Jumps to the HDU with the absolute number \a hdu. */ |
| 128 |
void goto_hdu (int hdu); |
| 129 |
/*! Returns the number of HDUs in the file. */ |
| 130 |
int num_hdus () const; |
| 131 |
/*! Asserts that the PDMTYPE of the current HDU is \a pdmtype. */ |
| 132 |
void assert_pdmtype (const std::string &pdmtype) const; |
| 133 |
/*! Inserts a binary table described by \a cols. |
| 134 |
The HDU has the name \a extname. */ |
| 135 |
void insert_bintab (const std::vector<fitscolumn> &cols, |
| 136 |
const std::string &extname="xtension"); |
| 137 |
/*! Inserts an ASCII table described by \a cols. The width of the |
| 138 |
columns is chosen automatically, in a way that avoids truncation. |
| 139 |
The HDU has the name \a extname. */ |
| 140 |
void insert_asctab (const std::vector<fitscolumn> &cols, |
| 141 |
const std::string &extname="xtension"); |
| 142 |
/*! Inserts a FITS image with the type given by \a type and dimensions |
| 143 |
given by \a Axes. */ |
| 144 |
void insert_image (PDT type, const std::vector<int64> &Axes); |
| 145 |
/*! Inserts a 2D FITS image with the type given by \a type, whose |
| 146 |
contents are given in \a data. */ |
| 147 |
template<typename T> |
| 148 |
void insert_image (PDT type, const arr2<T> &data); |
| 149 |
|
| 150 |
/*! Computes the checksum for the current HDU and writes it into the |
| 151 |
header. */ |
| 152 |
void write_checksum(); |
| 153 |
|
| 154 |
/*! \} */ |
| 155 |
|
| 156 |
/*! \name Information about the current HDU */ |
| 157 |
/*! \{ */ |
| 158 |
|
| 159 |
/*! Returns the dimensions of the current image. */ |
| 160 |
const std::vector<int64> &axes() const; |
| 161 |
/*! Returns the name of column \a #i. */ |
| 162 |
const std::string &colname(int i) const; |
| 163 |
/*! Returns the unit of column \a #i. */ |
| 164 |
const std::string &colunit(int i) const; |
| 165 |
/*! Returns repetition count of column \a #i. */ |
| 166 |
int64 repcount(int i) const; |
| 167 |
/*! Returns the Planck type for column \a #i. */ |
| 168 |
PDT coltype(int i) const; |
| 169 |
/*! Returns the number of columns in the current table. */ |
| 170 |
int ncols() const; |
| 171 |
/*! Returns the number of rows in the current table. */ |
| 172 |
int64 nrows() const; |
| 173 |
/*! Returns the total number of elements (nrows*repcount) |
| 174 |
in column \a #i. */ |
| 175 |
int64 nelems(int i) const; |
| 176 |
/*! Returns the number of elements that should be read/written in a single |
| 177 |
call for optimum performance. This depends on the current HDU. */ |
| 178 |
int64 efficientChunkSize(int i) const; |
| 179 |
|
| 180 |
/*! \} */ |
| 181 |
|
| 182 |
/*! \name Keyword-handling methods */ |
| 183 |
/*! \{ */ |
| 184 |
|
| 185 |
/*! Returns a list of all user-defined keys in the current HDU |
| 186 |
in \a keys. */ |
| 187 |
void get_all_keys (std::vector<std::string> &keys) const; |
| 188 |
|
| 189 |
void set_key_void (const std::string &key, const void *value, PDT type, |
| 190 |
const std::string &comment=""); |
| 191 |
/*! Updates \a key with \a value and \a comment. */ |
| 192 |
template<typename T> void set_key (const std::string &name, |
| 193 |
const T &value, const std::string &comment="") |
| 194 |
{ set_key_void (name, &value, planckType<T>(), comment); } |
| 195 |
/*! Deletes \a key from the header. */ |
| 196 |
void delete_key (const std::string &name); |
| 197 |
/*! Adds \a comment as a comment line. */ |
| 198 |
void add_comment (const std::string &comment); |
| 199 |
void get_key_void (const std::string &key, void *value, PDT type) const; |
| 200 |
/*! Reads the value belonging to \a key and returns it in \a value. */ |
| 201 |
template<typename T> void get_key (const std::string &name, T &value) const |
| 202 |
{ get_key_void (name,&value,planckType<T>()); } |
| 203 |
/*! Returms the value belonging to \a key. */ |
| 204 |
template<typename T> T get_key (const std::string &name) const |
| 205 |
{ T tmp; get_key(name, tmp); return tmp; } |
| 206 |
/*! Returns \a true if \a key is present, else \a false. */ |
| 207 |
bool key_present (const std::string &name) const; |
| 208 |
|
| 209 |
/*! \} */ |
| 210 |
|
| 211 |
/*! \name Methods for table data I/O */ |
| 212 |
/*! \{ */ |
| 213 |
|
| 214 |
void read_column_raw_void |
| 215 |
(int colnum, void *data, PDT type, int64 num, int64 offset=0) const; |
| 216 |
/*! Copies \a num elements from column \a colnum to the memory pointed |
| 217 |
to by \a data, starting at offset \a offset in the column. */ |
| 218 |
template<typename T> void read_column_raw |
| 219 |
(int colnum, T *data, int64 num, int64 offset=0) const |
| 220 |
{ read_column_raw_void (colnum, data, planckType<T>(), num, offset); } |
| 221 |
/*! Fills \a data with elements from column \a colnum, |
| 222 |
starting at offset \a offset in the column. */ |
| 223 |
template<typename T> void read_column |
| 224 |
(int colnum, arr<T> &data, int64 offset=0) const |
| 225 |
{ read_column_raw (colnum, &(data[0]), data.size(), offset); } |
| 226 |
/*! Fills \a data with elements from column \a colnum, |
| 227 |
starting at offset \a offset in the column. */ |
| 228 |
template<typename T> void read_column |
| 229 |
(int colnum, std::vector<T> &data, int64 offset=0) const |
| 230 |
{ read_column_raw (colnum, &(data[0]), data.size(), offset); } |
| 231 |
/*! Reads the element \a #offset from column \a colnum into \a data. */ |
| 232 |
template<typename T> void read_column |
| 233 |
(int colnum, T &data, int64 offset=0) const |
| 234 |
{ read_column_raw (colnum, &data, 1, offset); } |
| 235 |
/*! Reads the whole column \a colnum into \a data (which is resized |
| 236 |
accordingly). */ |
| 237 |
template<typename T> void read_entire_column |
| 238 |
(int colnum, arr<T> &data) const |
| 239 |
{ |
| 240 |
data.alloc(safe_cast<tsize>(nelems(colnum))); |
| 241 |
read_column (colnum, data); |
| 242 |
} |
| 243 |
/*! Reads the whole column \a colnum into \a data (which is resized |
| 244 |
accordingly). */ |
| 245 |
template<typename T> void read_entire_column |
| 246 |
(int colnum, std::vector<T> &data) const |
| 247 |
{ |
| 248 |
data.resize(safe_cast<tsize>(nelems(colnum))); |
| 249 |
read_column (colnum, data); |
| 250 |
} |
| 251 |
|
| 252 |
void write_column_raw_void |
| 253 |
(int colnum, const void *data, PDT type, int64 num, int64 offset=0); |
| 254 |
/*! Copies \a num elements from the memory pointed to by \a data to the |
| 255 |
column \a colnum, starting at offset \a offset in the column. */ |
| 256 |
template<typename T> void write_column_raw |
| 257 |
(int colnum, const T *data, int64 num, int64 offset=0) |
| 258 |
{ write_column_raw_void (colnum, data, planckType<T>(), num, offset); } |
| 259 |
/*! Copies all elements from \a data to the |
| 260 |
column \a colnum, starting at offset \a offset in the column. */ |
| 261 |
template<typename T> void write_column |
| 262 |
(int colnum, const arr<T> &data, int64 offset=0) |
| 263 |
{ write_column_raw (colnum, &(data[0]), data.size(), offset); } |
| 264 |
/*! Copies all elements from \a data to the |
| 265 |
column \a colnum, starting at offset \a offset in the column. */ |
| 266 |
template<typename T> void write_column |
| 267 |
(int colnum, const std::vector<T> &data, int64 offset=0) |
| 268 |
{ write_column_raw (colnum, &(data[0]), data.size(), offset); } |
| 269 |
/*! Copies \a data to the column \a colnum, at the position \a offset. */ |
| 270 |
template<typename T> void write_column |
| 271 |
(int colnum, const T &data, int64 offset=0) |
| 272 |
{ write_column_raw (colnum, &data, 1, offset); } |
| 273 |
|
| 274 |
/*! \} */ |
| 275 |
|
| 276 |
/*! \name Methods for image data I/O */ |
| 277 |
/*! \{ */ |
| 278 |
|
| 279 |
/*! Reads the current image into \a data, which is resized accordingly. */ |
| 280 |
template<typename T> void read_image (arr2<T> &data) const; |
| 281 |
/*! Reads the current image into \a data, which is resized accordingly. */ |
| 282 |
template<typename T> void read_image (arr3<T> &data) const; |
| 283 |
/*! Reads a partial image, whose dimensions are given by the dimensions |
| 284 |
of \a data, into data. The starting pixel indices are given by |
| 285 |
\a xl and \a yl. */ |
| 286 |
template<typename T> void read_subimage |
| 287 |
(arr2<T> &data, int xl, int yl) const; |
| 288 |
void read_subimage_void (void *data, PDT type, tsize ndata, int64 offset=0) |
| 289 |
const; |
| 290 |
/*! Fills \a data with values from the image, starting at the offset |
| 291 |
\a offset in the image. The image is treated as a one-dimensional |
| 292 |
array. */ |
| 293 |
template<typename T> void read_subimage (arr<T> &data, int64 offset=0) |
| 294 |
const |
| 295 |
{ read_subimage_void (&data[0],planckType<T>(),data.size(),offset); } |
| 296 |
|
| 297 |
void write_image2D_void (const void *data, PDT type, tsize s1, |
| 298 |
tsize s2); |
| 299 |
/*! Writes \a data into the current image. \a data must have the same |
| 300 |
dimensions as specified in the HDU. */ |
| 301 |
template<typename T> void write_image (const arr2<T> &data) |
| 302 |
{ |
| 303 |
write_image2D_void (&data[0][0],planckType<T>(),data.size1(), |
| 304 |
data.size2()); |
| 305 |
} |
| 306 |
|
| 307 |
void write_subimage_void (const void *data, PDT type, tsize sz, |
| 308 |
int64 offset); |
| 309 |
/*! Copies \a data to the image, starting at the offset |
| 310 |
\a offset in the image. The image is treated as a one-dimensional |
| 311 |
array. */ |
| 312 |
template<typename T> void write_subimage (const arr<T> &data, |
| 313 |
int64 offset=0) |
| 314 |
{ write_subimage_void(&data[0],planckType<T>(),data.size(),offset); } |
| 315 |
|
| 316 |
/*! \} */ |
| 317 |
}; |
| 318 |
|
| 319 |
/*! \} */ |
| 320 |
|
| 321 |
#endif |