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