[2820] | 1 | #ifndef FITSBLKRW_H
|
---|
| 2 | #define FITSBLKRW_H
|
---|
| 3 |
|
---|
| 4 | #include "fitsinoutfile.h"
|
---|
| 5 |
|
---|
| 6 | namespace SOPHYA {
|
---|
| 7 |
|
---|
| 8 | /*!
|
---|
| 9 | \ingroup FitsIOServer
|
---|
| 10 | \brief Template class with static methods for handling bloc data
|
---|
[2846] | 11 | read from / write to fits filesz
|
---|
[2820] | 12 | */
|
---|
| 13 |
|
---|
| 14 | template <class T>
|
---|
| 15 | class FitsBlockRW {
|
---|
| 16 | public:
|
---|
| 17 | //! Write image HDU data to a fits file
|
---|
| 18 | static void WriteImageData(FitsInOutFile& fios, const T * d, size_t sz,
|
---|
[3167] | 19 | LONGLONG * fpixel=NULL)
|
---|
[2820] | 20 | {
|
---|
| 21 | int status = 0;
|
---|
[3167] | 22 | LONGLONG fpix[5] = {1,1,1,1,1};
|
---|
[2820] | 23 | if (fpixel == NULL) fpixel = fpix;
|
---|
| 24 | T * ncd = const_cast<T *>(d);
|
---|
[3167] | 25 | fits_write_pixll(fios.FitsPtr(), FitsTypes::DataType(d[0]), fpixel,
|
---|
[2820] | 26 | sz, ncd, &status);
|
---|
| 27 | if ( status ) {
|
---|
| 28 | fits_report_error(stderr, status);
|
---|
| 29 | char buff[32];
|
---|
| 30 | fits_get_errstatus(status, buff);
|
---|
| 31 | string msg = "FitsBlockRW<T>::WriteImageData() Error: " ;
|
---|
| 32 | msg += buff;
|
---|
| 33 | throw FitsIOException(msg);
|
---|
| 34 | }
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | //! Read image HDU data from a fits file
|
---|
| 38 | static void ReadImageData(FitsInOutFile& fios, T * d, size_t sz,
|
---|
[3167] | 39 | LONGLONG * fpixel=NULL)
|
---|
[2820] | 40 | {
|
---|
| 41 | int status = 0;
|
---|
[3167] | 42 | LONGLONG fpix[5] = {1,1,1,1,1};
|
---|
[2820] | 43 | if (fpixel == NULL) fpixel = fpix;
|
---|
| 44 | int anynul = 0;
|
---|
[3167] | 45 | fits_read_pixll(fios.FitsPtr(), FitsTypes::DataType(d[0]), fpixel,
|
---|
[2820] | 46 | sz, NULL, d, &anynul, &status);
|
---|
| 47 | if ( status ) {
|
---|
| 48 | fits_report_error(stderr, status);
|
---|
| 49 | char buff[32];
|
---|
| 50 | fits_get_errstatus(status, buff);
|
---|
| 51 | string msg = "FitsBlockRW<T>::ReadImageData() Error: " ;
|
---|
| 52 | msg += buff;
|
---|
| 53 | throw FitsIOException(msg);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 |
|
---|
| 59 | /*!
|
---|
| 60 | Write binary/ascii HDU data to a fits file.
|
---|
| 61 | See cfitsio function fits_write_col() for more detail.
|
---|
| 62 | \param colnum : table column number (starting from 1)
|
---|
| 63 | \param firstrow : the write operation starting row (starting from 1)
|
---|
| 64 | \param firstelem : the firstelem (for vector type columns)
|
---|
| 65 | \param d : pointer to data to be written
|
---|
| 66 | \param sz : number of data elements to be written
|
---|
| 67 | */
|
---|
[3167] | 68 | static void WriteColumnData(FitsInOutFile& fios, int colnum, LONGLONG firstrow,
|
---|
| 69 | LONGLONG firstelem, const T * d, size_t sz)
|
---|
[2820] | 70 | {
|
---|
| 71 | int status = 0;
|
---|
| 72 | T * ncd = const_cast<T *>(d);
|
---|
| 73 | fits_write_col(fios.FitsPtr(), FitsTypes::DataType(d[0]), colnum,
|
---|
| 74 | firstrow, firstelem, sz, ncd, &status);
|
---|
| 75 | if ( status ) {
|
---|
| 76 | fits_report_error(stderr, status);
|
---|
| 77 | char buff[32];
|
---|
| 78 | fits_get_errstatus(status, buff);
|
---|
| 79 | string msg = "FitsBlockRW<T>::WriteColumnData() Error: " ;
|
---|
| 80 | msg += buff;
|
---|
| 81 | throw FitsIOException(msg);
|
---|
| 82 | }
|
---|
| 83 | return;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | /*!
|
---|
| 87 | Read binary/ascii HDU data from a fits file.
|
---|
| 88 | See cfitsio function fits_read_col() for more detail.
|
---|
| 89 | \param colnum : table column number (starting from 1)
|
---|
| 90 | \param firstrow : the read operation starting point (row) (starting from 1)
|
---|
| 91 | \param firstelem : the firstelem (for vector type columns)
|
---|
| 92 | \param d : pointer to data to be written
|
---|
| 93 | \param sz : number of data elements to be read
|
---|
| 94 | */
|
---|
[3167] | 95 | static void ReadColumnData(FitsInOutFile& fios, int colnum, LONGLONG firstrow,
|
---|
| 96 | LONGLONG firstelem, T * d, size_t sz)
|
---|
[2820] | 97 | {
|
---|
| 98 | int status = 0;
|
---|
| 99 | int anynul = 0;
|
---|
| 100 | fits_read_col(fios.FitsPtr(), FitsTypes::DataType(d[0]), colnum,
|
---|
| 101 | firstrow, firstelem, sz, NULL, d, &anynul, &status);
|
---|
| 102 | if ( status ) {
|
---|
| 103 | fits_report_error(stderr, status);
|
---|
| 104 | char buff[32];
|
---|
| 105 | fits_get_errstatus(status, buff);
|
---|
| 106 | string msg = "FitsBlockRW<T>::ReadColumnData() Error: " ;
|
---|
| 107 | msg += buff;
|
---|
| 108 | throw FitsIOException(msg);
|
---|
| 109 | }
|
---|
| 110 | return;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 |
|
---|
| 114 | };
|
---|
| 115 |
|
---|
[2846] | 116 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
| 117 | class FitsBlockRW<std::string> {
|
---|
| 118 | public:
|
---|
| 119 | //! Write image HDU with string data type not supported (throws exception)
|
---|
| 120 | static void WriteImageData(FitsInOutFile& fios, const std::string * d, size_t sz,
|
---|
[3167] | 121 | LONGLONG * fpixel=NULL)
|
---|
[2846] | 122 | {
|
---|
| 123 | throw FitsIOException(
|
---|
| 124 | "FitsBlockRW<string>::WriteImageData() string data type Unsupported for image HDU");
|
---|
| 125 | }
|
---|
| 126 | //! Read image HDU with string data type not supported (throws exception)
|
---|
| 127 | static void ReadImageData(FitsInOutFile& fios, std::string * d, size_t sz,
|
---|
[3167] | 128 | LONGLONG * fpixel=NULL)
|
---|
[2846] | 129 | {
|
---|
| 130 | throw FitsIOException(
|
---|
| 131 | "FitsBlockRW<string>::ReadImageData() string data type Unsupported for image HDU");
|
---|
| 132 | }
|
---|
[2843] | 133 |
|
---|
[2846] | 134 | //! Write character string data to binary/ascii HDU data in a fits file.
|
---|
[3167] | 135 | static void WriteColumnData(FitsInOutFile& fios, int colnum, LONGLONG firstrow,
|
---|
| 136 | LONGLONG firstelem, const std::string * d, size_t sz)
|
---|
[2843] | 137 | {
|
---|
| 138 | int status = 0;
|
---|
[2846] | 139 | char sbuff[1024];
|
---|
| 140 | char * cp[4] = {sbuff, sbuff+256, sbuff+512, sbuff+768};
|
---|
| 141 | //cout << " --- Getting in WriteColumnData<string>() colnum=" << colnum << endl;
|
---|
[2843] | 142 | for(size_t kk=0; kk<sz; kk++) {
|
---|
[2846] | 143 | strncpy(sbuff, d[kk].c_str(), 1023);
|
---|
| 144 | // char * cp = const_cast<char *>(d[kk].c_str());
|
---|
| 145 | sbuff[1023] = '\0';
|
---|
[2843] | 146 | status = 0;
|
---|
[2846] | 147 | //cout <<"DBG-Write2Fits : appel a fits_write_col() kk=" << kk << " / sz=" << sz << endl;
|
---|
| 148 | fits_write_col(fios.FitsPtr(), FitsTypes::DataType(sbuff), colnum,
|
---|
| 149 | firstrow+kk, firstelem, 1, cp, &status);
|
---|
[2843] | 150 | if ( status ) {
|
---|
| 151 | fits_report_error(stderr, status);
|
---|
| 152 | char buff[32];
|
---|
| 153 | fits_get_errstatus(status, buff);
|
---|
[2846] | 154 | string msg = "FitsBlockRW<std::string>::WriteColumnData() Error: " ;
|
---|
[2843] | 155 | msg += buff;
|
---|
| 156 | sprintf(buff," kk=%ld",kk); msg += buff;
|
---|
| 157 | throw FitsIOException(msg);
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
| 160 | return;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
[2846] | 163 |
|
---|
| 164 | //! Read character string data to binary/ascii HDU data in a fits file.
|
---|
[3167] | 165 | static void ReadColumnData(FitsInOutFile& fios, int colnum, LONGLONG firstrow,
|
---|
| 166 | LONGLONG firstelem, std::string * d, size_t sz)
|
---|
[2843] | 167 | {
|
---|
| 168 | int status = 0;
|
---|
| 169 | int anynul = 0;
|
---|
[2846] | 170 | char sbuff[1024];
|
---|
| 171 | char * cp[4] = {sbuff, sbuff+256, sbuff+512, sbuff+768};
|
---|
| 172 | // cout << " --- Getting in ReadColumnData<string>() colnum=" << colnum << endl;
|
---|
[2843] | 173 | for(size_t kk=0; kk<sz; kk++) {
|
---|
[2846] | 174 | // cout <<"DBG-ReadFrFits : appel a fits_read_col() kk=" << kk << " / sz=" << sz << endl;
|
---|
| 175 | fits_read_col(fios.FitsPtr(), FitsTypes::DataType(sbuff), colnum,
|
---|
| 176 | firstrow+kk, firstelem, 1, NULL, cp, &anynul, &status);
|
---|
| 177 | sbuff[1023] = '\0'; d[kk] = sbuff;
|
---|
[2843] | 178 | if ( status ) {
|
---|
| 179 | fits_report_error(stderr, status);
|
---|
| 180 | char buff[32];
|
---|
| 181 | fits_get_errstatus(status, buff);
|
---|
[2846] | 182 | string msg = "FitsBlockRW<std::string>::ReadColumnData() Error: " ;
|
---|
[2843] | 183 | msg += buff;
|
---|
| 184 | sprintf(buff," kk=%ld",kk); msg += buff;
|
---|
| 185 | throw FitsIOException(msg);
|
---|
| 186 | }
|
---|
| 187 | }
|
---|
| 188 | return;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
[2846] | 191 | }; // Fin classe FitsBlockRW<std::string>
|
---|
| 192 |
|
---|
[2820] | 193 | } // Fin du namespace
|
---|
| 194 |
|
---|
| 195 | #endif
|
---|