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