| 1 | #include <stdlib.h>
 | 
|---|
| 2 | #include <string.h>
 | 
|---|
| 3 | #include "minifits.h"
 | 
|---|
| 4 | 
 | 
|---|
| 5 | // #include <iostream>
 | 
|---|
| 6 | 
 | 
|---|
| 7 | //////////////////////////////////////////////////////////////////////
 | 
|---|
| 8 | ///     Classe  MiniFITSException
 | 
|---|
| 9 | //////////////////////////////////////////////////////////////////////
 | 
|---|
| 10 | 
 | 
|---|
| 11 | 
 | 
|---|
| 12 | /* --Methode-- */
 | 
|---|
| 13 | MiniFITSException::MiniFITSException(const char * m)   throw()
 | 
|---|
| 14 | {
 | 
|---|
| 15 |   if (m!=NULL) { 
 | 
|---|
| 16 |     strncpy(msg_, m, MFEX_MAXMSGLEN-1);
 | 
|---|
| 17 |     msg_[MFEX_MAXMSGLEN-1] = '\0';
 | 
|---|
| 18 |   }
 | 
|---|
| 19 |   else msg_[0] = '\0';
 | 
|---|
| 20 | }
 | 
|---|
| 21 | 
 | 
|---|
| 22 | /* --Methode-- */
 | 
|---|
| 23 | MiniFITSException::MiniFITSException(const string& m)   throw()
 | 
|---|
| 24 | {
 | 
|---|
| 25 |   strncpy(msg_, m.c_str(), MFEX_MAXMSGLEN-1);
 | 
|---|
| 26 |   msg_[MFEX_MAXMSGLEN-1] = '\0';
 | 
|---|
| 27 | }
 | 
|---|
| 28 | 
 | 
|---|
| 29 | /* --Methode-- */
 | 
|---|
| 30 | MiniFITSException::~MiniFITSException()  throw()
 | 
|---|
| 31 | {
 | 
|---|
| 32 | }
 | 
|---|
| 33 | 
 | 
|---|
| 34 | /* --Methode-- */
 | 
|---|
| 35 | const char* MiniFITSException::what() const throw()
 | 
|---|
| 36 | {
 | 
|---|
| 37 |   return msg_;
 | 
|---|
| 38 | }
 | 
|---|
| 39 | 
 | 
|---|
| 40 | /* --Methode-- */
 | 
|---|
| 41 | string const MiniFITSException::Msg() const 
 | 
|---|
| 42 | {
 | 
|---|
| 43 |   return (string(msg_));
 | 
|---|
| 44 | }
 | 
|---|
| 45 | 
 | 
|---|
| 46 | //////////////////////////////////////////////////////////////////////
 | 
|---|
| 47 | ///     Classe  MiniFITSFile
 | 
|---|
| 48 | //////////////////////////////////////////////////////////////////////
 | 
|---|
| 49 | 
 | 
|---|
| 50 | #define MFITSHLEN 2880
 | 
|---|
| 51 | 
 | 
|---|
| 52 | /* --Methode-- */
 | 
|---|
| 53 | MiniFITSFile::MiniFITSFile()
 | 
|---|
| 54 | {
 | 
|---|
| 55 |   Init();
 | 
|---|
| 56 | }
 | 
|---|
| 57 | 
 | 
|---|
| 58 | /* --Methode-- */
 | 
|---|
| 59 | MiniFITSFile::MiniFITSFile(string const & nom, MiniFITS_Mode rwm)
 | 
|---|
| 60 | {
 | 
|---|
| 61 |   Init();
 | 
|---|
| 62 |   Open(nom, rwm);
 | 
|---|
| 63 | }
 | 
|---|
| 64 | 
 | 
|---|
| 65 | /* --Methode-- */
 | 
|---|
| 66 | MiniFITSFile::MiniFITSFile(const char* nom, MiniFITS_Mode rwm)
 | 
|---|
| 67 | {
 | 
|---|
| 68 |   Init();
 | 
|---|
| 69 |   Open(nom, rwm);
 | 
|---|
| 70 | }
 | 
|---|
| 71 | 
 | 
|---|
| 72 | 
 | 
|---|
| 73 | /* --Methode-- */
 | 
|---|
| 74 | MiniFITSFile::~MiniFITSFile()
 | 
|---|
| 75 | {
 | 
|---|
| 76 |   Close();
 | 
|---|
| 77 |   delete[] header;
 | 
|---|
| 78 | }
 | 
|---|
| 79 | 
 | 
|---|
| 80 | /* --Methode-- */
 | 
|---|
| 81 | void MiniFITSFile::Init()
 | 
|---|
| 82 | {
 | 
|---|
| 83 |   fip = NULL;
 | 
|---|
| 84 |   rwmode = MF_Read;
 | 
|---|
| 85 |   dtype = MF_Byte;
 | 
|---|
| 86 |   nax1 = 1;
 | 
|---|
| 87 |   nax2 = 1; 
 | 
|---|
| 88 |   totwsz = 0;
 | 
|---|
| 89 |   header = new char[MFITSHLEN];
 | 
|---|
| 90 |   for(int i=0; i<MFITSHLEN; i++)  header[i]=' ';
 | 
|---|
| 91 |   nkeya_ = 0;
 | 
|---|
| 92 | }
 | 
|---|
| 93 | 
 | 
|---|
| 94 | /* --Methode-- */
 | 
|---|
| 95 | void MiniFITSFile::Open(const char* nom, MiniFITS_Mode rwm)
 | 
|---|
| 96 | {
 | 
|---|
| 97 |   if (fip != NULL) throw MiniFITSException("MiniFITSFile::Open() - fip != NULL");
 | 
|---|
| 98 |   if (rwm == MF_Write) {
 | 
|---|
| 99 |     FillHeader();
 | 
|---|
| 100 |     fip = fopen(nom, "w");
 | 
|---|
| 101 |     if (fip == NULL) 
 | 
|---|
| 102 |       throw MiniFITSException("MiniFITSFile::Open()/ failed fopen() for write");
 | 
|---|
| 103 |     fwrite(header, 1, MFITSHLEN, fip);
 | 
|---|
| 104 |     rwmode = MF_Write;
 | 
|---|
| 105 |   }
 | 
|---|
| 106 |   else {
 | 
|---|
| 107 |     fip = fopen(nom, "r");
 | 
|---|
| 108 |     if (fip == NULL) 
 | 
|---|
| 109 |       throw MiniFITSException("MiniFITSFile::Open()/ failed fopen() for read");
 | 
|---|
| 110 |     fread(header, 1, MFITSHLEN, fip);
 | 
|---|
| 111 |     DecodeHeader();
 | 
|---|
| 112 |     rwmode = MF_Read;
 | 
|---|
| 113 |   }
 | 
|---|
| 114 |   return;
 | 
|---|
| 115 | }
 | 
|---|
| 116 | 
 | 
|---|
| 117 | /* --Methode-- */
 | 
|---|
| 118 | void MiniFITSFile::Close()
 | 
|---|
| 119 | {
 | 
|---|
| 120 |   if (fip) { 
 | 
|---|
| 121 |         if (rwmode == MF_Write)  {  
 | 
|---|
| 122 |           // on remplit avec des zeros pour avoir une longueur multiple de 2880 
 | 
|---|
| 123 |           size_t padsz = MFITSHLEN-(totwsz%MFITSHLEN);
 | 
|---|
| 124 |           char zeros[160];
 | 
|---|
| 125 |           for(size_t k=0; k<160; k++) zeros[k]=0;
 | 
|---|
| 126 |           while(padsz>160) {
 | 
|---|
| 127 |             fwrite(zeros, 1, 160, fip);
 | 
|---|
| 128 |             padsz-=160;
 | 
|---|
| 129 |           }
 | 
|---|
| 130 |           if (padsz>0)  fwrite(zeros, 1, padsz, fip);
 | 
|---|
| 131 |           // On reecrit l'entete 
 | 
|---|
| 132 |           FillHeader();
 | 
|---|
| 133 |           fseek(fip, 0, SEEK_SET);
 | 
|---|
| 134 |           fwrite(header, 1, MFITSHLEN, fip);
 | 
|---|
| 135 |         }       
 | 
|---|
| 136 |         fclose(fip);
 | 
|---|
| 137 |   }
 | 
|---|
| 138 |   fip = NULL;
 | 
|---|
| 139 |   return;
 | 
|---|
| 140 | }
 | 
|---|
| 141 | 
 | 
|---|
| 142 | /* --Methode-- */
 | 
|---|
| 143 | void MiniFITSFile::setDTypeNaxis(MiniFITS_DT dt, size_t na1, size_t na2)
 | 
|---|
| 144 | {
 | 
|---|
| 145 |   // Interdit si fichier ouvert en lecture ...
 | 
|---|
| 146 |   if ((fip!=NULL)&&(rwmode == MF_Read))  
 | 
|---|
| 147 |      throw MiniFITSException("MiniFITSFile::setDTypeNaxis()/Error ReadOnly file");
 | 
|---|
| 148 | 
 | 
|---|
| 149 |   dtype = dt;
 | 
|---|
| 150 |   nax1 = na1; 
 | 
|---|
| 151 |   nax2 = na2;
 | 
|---|
| 152 | }
 | 
|---|
| 153 | 
 | 
|---|
| 154 | /* --Methode-- */
 | 
|---|
| 155 | string MiniFITSFile::DataTypeToString()
 | 
|---|
| 156 | {
 | 
|---|
| 157 |   if (dtype == MF_Byte) return "MF_Byte";
 | 
|---|
| 158 |   else if (dtype == MF_Int16) return "MF_Int16";
 | 
|---|
| 159 |   else if (dtype == MF_Float32) return "MF_Float32";
 | 
|---|
| 160 |   else return "Unknown??";
 | 
|---|
| 161 | }
 | 
|---|
| 162 | 
 | 
|---|
| 163 | /* --Methode-- */
 | 
|---|
| 164 | int MiniFITSFile::Write(void* data, size_t sz)
 | 
|---|
| 165 | {
 | 
|---|
| 166 |   fwrite(data, 1, sz, fip);
 | 
|---|
| 167 |   totwsz += sz;
 | 
|---|
| 168 |   return 0;
 | 
|---|
| 169 | }
 | 
|---|
| 170 | 
 | 
|---|
| 171 | /* --Methode-- */
 | 
|---|
| 172 | int MiniFITSFile::Read(void* data, size_t sz, size_t offset)
 | 
|---|
| 173 | {
 | 
|---|
| 174 |   fseek(fip, offset+MFITSHLEN, SEEK_SET);
 | 
|---|
| 175 |   fread(data, 1, sz, fip);
 | 
|---|
| 176 |   return 0;
 | 
|---|
| 177 | }
 | 
|---|
| 178 | 
 | 
|---|
| 179 | /* --Methode-- */
 | 
|---|
| 180 | void MiniFITSFile::FillHeader()
 | 
|---|
| 181 | {
 | 
|---|
| 182 |   strcpy(header, "SIMPLE  =                    T / file does conform to FITS standard");
 | 
|---|
| 183 |   header[strlen(header)] = ' ';
 | 
|---|
| 184 |   int bpix = 8;
 | 
|---|
| 185 |   if (dtype == MF_Byte) bpix = 8;
 | 
|---|
| 186 |   else if (dtype == MF_Int16) bpix = 16;
 | 
|---|
| 187 |   else if (dtype == MF_Float32) bpix = -32;
 | 
|---|
| 188 |   char * buff = header+80;
 | 
|---|
| 189 |   sprintf(buff, "BITPIX  = %20d /   number of bits per data pixel", bpix);
 | 
|---|
| 190 |   buff[strlen(buff)] = ' ';
 | 
|---|
| 191 |   buff = header+160;
 | 
|---|
| 192 |   strcpy(buff, "NAXIS   =                    2 / number of data axes");
 | 
|---|
| 193 |   buff[strlen(buff)] = ' ';
 | 
|---|
| 194 |   buff = header+240;
 | 
|---|
| 195 |   sprintf(buff, "NAXIS1  = %20ld /   number of bits per data pixel", (long)nax1);
 | 
|---|
| 196 |   buff[strlen(buff)] = ' ';
 | 
|---|
| 197 |   buff = header+320;
 | 
|---|
| 198 |   sprintf(buff, "NAXIS2  = %20ld /   number of bits per data pixel", (long)nax2);
 | 
|---|
| 199 |   buff[strlen(buff)] = ' ';
 | 
|---|
| 200 |   buff = header+400+nkeya_*80;
 | 
|---|
| 201 |   strcpy(buff,"COMMENT  BAO-Radio / MiniFITSFile ");
 | 
|---|
| 202 |   buff[strlen(buff)] = ' ';
 | 
|---|
| 203 |   buff = header+480+nkeya_*80;
 | 
|---|
| 204 |   strcpy(buff,"END");
 | 
|---|
| 205 |   buff[strlen(buff)] = ' ';
 | 
|---|
| 206 | 
 | 
|---|
| 207 |   return;
 | 
|---|
| 208 | }
 | 
|---|
| 209 | 
 | 
|---|
| 210 | /* --Methode-- */
 | 
|---|
| 211 | int MiniFITSFile::AddKeyI(const char* key, long val, const char* comm)
 | 
|---|
| 212 | {
 | 
|---|
| 213 |   if (nkeya_ >= 29) return 0;
 | 
|---|
| 214 |   char cle[10];
 | 
|---|
| 215 |   strncpy(cle,key,8);
 | 
|---|
| 216 |   cle[8]='=';
 | 
|---|
| 217 |   for(int i=0;i<8;i++) 
 | 
|---|
| 218 |     if (cle[i]=='\0') cle[i]=' ';
 | 
|---|
| 219 |   cle[9]='\0';
 | 
|---|
| 220 |   char* buff=header+400+nkeya_*80;
 | 
|---|
| 221 |   if (comm!=NULL) {
 | 
|---|
| 222 |     char tcom[50];
 | 
|---|
| 223 |     strncpy(tcom,comm,48);  
 | 
|---|
| 224 |     tcom[48]='\0';
 | 
|---|
| 225 |     sprintf(buff,"%s %20ld / %s", cle, val, tcom);
 | 
|---|
| 226 |   }
 | 
|---|
| 227 |   else  sprintf(buff,"%s %20ld / ", cle, val);
 | 
|---|
| 228 |   buff[strlen(buff)]=' ';
 | 
|---|
| 229 |   nkeya_++;
 | 
|---|
| 230 |   return nkeya_;
 | 
|---|
| 231 | }
 | 
|---|
| 232 | 
 | 
|---|
| 233 | /* --Methode-- */
 | 
|---|
| 234 | int MiniFITSFile::AddKeyD(const char* key, double val, const char* comm)
 | 
|---|
| 235 | {
 | 
|---|
| 236 |   if (nkeya_ >= 29) return 0;
 | 
|---|
| 237 |   char cle[10];
 | 
|---|
| 238 |   strncpy(cle,key,8);
 | 
|---|
| 239 |   cle[8]='=';
 | 
|---|
| 240 |   for(int i=0;i<8;i++) 
 | 
|---|
| 241 |     if (cle[i]=='\0') cle[i]=' ';
 | 
|---|
| 242 |   cle[9]='\0';
 | 
|---|
| 243 |   char* buff=header+400+nkeya_*80;
 | 
|---|
| 244 |   if (comm!=NULL) {
 | 
|---|
| 245 |     char tcom[50];
 | 
|---|
| 246 |     strncpy(tcom,comm,48);  
 | 
|---|
| 247 |     tcom[48]='\0';
 | 
|---|
| 248 |     sprintf(buff,"%s %20lg / %s", cle, val, tcom);
 | 
|---|
| 249 |   }
 | 
|---|
| 250 |   else  sprintf(buff,"%s %20ld / ", cle, val);
 | 
|---|
| 251 |   buff[strlen(buff)] = ' ';
 | 
|---|
| 252 |   nkeya_++;
 | 
|---|
| 253 |   return nkeya_;
 | 
|---|
| 254 | }
 | 
|---|
| 255 | 
 | 
|---|
| 256 | /* --Methode-- */
 | 
|---|
| 257 | int MiniFITSFile::AddKeyS(const char* key, const char* val, const char* comm)
 | 
|---|
| 258 | {
 | 
|---|
| 259 |   if (nkeya_ >= 29) return 0;
 | 
|---|
| 260 |   char cle[10];
 | 
|---|
| 261 |   strncpy(cle,key,8);
 | 
|---|
| 262 |   cle[8]='=';
 | 
|---|
| 263 |   for(int i=0;i<8;i++) 
 | 
|---|
| 264 |     if (cle[i]=='\0') cle[i]=' ';
 | 
|---|
| 265 |   cle[9]='\0';
 | 
|---|
| 266 |   char tcom[72];
 | 
|---|
| 267 |   tcom[0]='\'';
 | 
|---|
| 268 |   strncpy(tcom+1,val,65);
 | 
|---|
| 269 |   int l=strlen(tcom);
 | 
|---|
| 270 |   strcpy(tcom+l,"' / ");
 | 
|---|
| 271 |   l+=4;
 | 
|---|
| 272 |   if ((l<70)&&(comm!=NULL)) strncpy(tcom+l,comm,70-l);
 | 
|---|
| 273 |   tcom[70]='\0';
 | 
|---|
| 274 |   char* buff=header+400+nkeya_*80;
 | 
|---|
| 275 |   sprintf(buff,"%s %s", cle, tcom);
 | 
|---|
| 276 |   buff[strlen(buff)] = ' ';
 | 
|---|
| 277 |   nkeya_++;
 | 
|---|
| 278 |   return nkeya_;
 | 
|---|
| 279 | }
 | 
|---|
| 280 | 
 | 
|---|
| 281 | /* --Methode-- */
 | 
|---|
| 282 | void MiniFITSFile::DecodeHeader()
 | 
|---|
| 283 | {
 | 
|---|
| 284 |   // AMELIORER le decodage de l'entete, remplissage dtype, nax1, nax2
 | 
|---|
| 285 |   char * buff = header;
 | 
|---|
| 286 |   if (strncmp(buff, "SIMPLE  =", 9) != 0) 
 | 
|---|
| 287 |      throw MiniFITSException("MiniFITSFile::DecodeHeader()/Error - NO SIMPLE keyword");
 | 
|---|
| 288 |   bool fgokt=false;
 | 
|---|
| 289 |   bool fgok1=false;
 | 
|---|
| 290 |   bool fgok2=false;
 | 
|---|
| 291 |   for(int kh=80; kh<2800; kh+=80) {
 | 
|---|
| 292 |         buff = header+kh;
 | 
|---|
| 293 |         if (strncmp(buff, "NAXIS1  =", 9) == 0)  {
 | 
|---|
| 294 |           nax1 = atol(buff+10);
 | 
|---|
| 295 |           fgok1 = true;
 | 
|---|
| 296 | //        cout << " FOUND : NAXIS1= " << nax1 << endl;
 | 
|---|
| 297 |         } 
 | 
|---|
| 298 |         else if (strncmp(buff, "NAXIS2  =", 9) == 0)  {
 | 
|---|
| 299 |           nax2 = atol(buff+10);
 | 
|---|
| 300 |           fgok2 = true;
 | 
|---|
| 301 | //        cout << " FOUND : NAXIS2= " << nax2 << endl;            
 | 
|---|
| 302 |         } 
 | 
|---|
| 303 |         else if (strncmp(buff, "BITPIX  =", 9) == 0)  {
 | 
|---|
| 304 |           int bpix = atoi(buff+10);
 | 
|---|
| 305 |           fgokt = true;
 | 
|---|
| 306 |           if (bpix == 8)  dtype = MF_Byte;
 | 
|---|
| 307 |           else if (bpix == 16)  dtype = MF_Int16;
 | 
|---|
| 308 |           else if (bpix == -32)  dtype = MF_Float32;
 | 
|---|
| 309 |           else fgokt = false;
 | 
|---|
| 310 | //        cout << " FOUND : bpix= " << bpix << endl;              
 | 
|---|
| 311 |         } 
 | 
|---|
| 312 |   }
 | 
|---|
| 313 |   if (!(fgok1&&fgok2&&fgokt))  
 | 
|---|
| 314 |      throw MiniFITSException("MiniFITSFile::DecodeHeader()/Error- Missing/wrong NAXIS1/2,BITPIX");
 | 
|---|
| 315 |   return;
 | 
|---|
| 316 | }
 | 
|---|
| 317 | 
 | 
|---|