source: Sophya/trunk/AddOn/TAcq/minifits.cc@ 3671

Last change on this file since 3671 was 3671, checked in by ansari, 16 years ago
  • Nettoyage complet des programmes d'acquisition
  • Ajout methode BRPaquet::CopyFrom() pour reduction de paquet
  • Introduction des classes BRParList , BRConfList , BRAcqConfig
  • Introduction du nouveau programme acquisition mfacq.cc (MultiFibresAcq)

Reza, 13/11/2009

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