source: Sophya/branches/Reza/AddOn/TAcq/minifits.cc@ 3537

Last change on this file since 3537 was 3537, checked in by ansari, 17 years ago

Ajout du module AddOn/TAcq (programmes test pour acquisition BAORadio) ds cvs sophya (sous-repertoire AddOn/) , Reza 22/10/2008

File size: 4.5 KB
Line 
1#include "minifits.h"
2
3// #include <iostream>
4
5#define MFITSHLEN 2880
6
7MiniFITSFile::MiniFITSFile()
8{
9 Init();
10}
11
12MiniFITSFile::MiniFITSFile(string const & nom, MiniFITS_Mode rwm)
13{
14 Init();
15 Open(nom, rwm);
16}
17
18MiniFITSFile::MiniFITSFile(const char* nom, MiniFITS_Mode rwm)
19{
20 Init();
21 Open(nom, rwm);
22}
23
24
25MiniFITSFile::~MiniFITSFile()
26{
27 Close();
28 delete[] header;
29}
30
31void MiniFITSFile::Init()
32{
33 fip = NULL;
34 rwmode = MF_Read;
35 dtype = MF_Byte;
36 nax1 = 1;
37 nax2 = 1;
38 totwsz = 0;
39 header = new char[MFITSHLEN];
40
41}
42
43void MiniFITSFile::Open(const char* nom, MiniFITS_Mode rwm)
44{
45 if (fip != NULL) throw MiniFITSException("MiniFITSFile::Open() - fip != NULL");
46 if (rwm == MF_Write) {
47 FillHeader();
48 fip = fopen(nom, "w");
49 if (fip == NULL)
50 throw MiniFITSException("MiniFITSFile::Open()/ failed fopen() for write");
51 fwrite(header, 1, MFITSHLEN, fip);
52 rwmode = MF_Write;
53 }
54 else {
55 fip = fopen(nom, "r");
56 if (fip == NULL)
57 throw MiniFITSException("MiniFITSFile::Open()/ failed fopen() for read");
58 fread(header, 1, MFITSHLEN, fip);
59 DecodeHeader();
60 rwmode = MF_Read;
61 }
62 return;
63}
64
65void MiniFITSFile::Close()
66{
67 if (fip) {
68 if (rwmode == MF_Write) {
69 // on remplit avec des zeros pour avoir une longueur multiple de 2880
70 size_t padsz = MFITSHLEN-(totwsz%MFITSHLEN);
71 for(size_t k=0; k<padsz; k++) header[k]=0;
72 fwrite(header, 1, padsz, fip);
73 // On reecrit l'entete
74 FillHeader();
75 fseek(fip, 0, SEEK_SET);
76 fwrite(header, 1, MFITSHLEN, fip);
77 }
78 fclose(fip);
79 }
80 fip = NULL;
81 return;
82}
83
84void MiniFITSFile::setDTypeNaxis(MiniFITS_DT dt, size_t na1, size_t na2)
85{
86 // Interdit si fichier ouvert en lecture ...
87 if ((fip!=NULL)&&(rwmode == MF_Read))
88 throw MiniFITSException("MiniFITSFile::setDTypeNaxis()/Error ReadOnly file");
89
90 dtype = dt;
91 nax1 = na1;
92 nax2 = na2;
93}
94
95string MiniFITSFile::DataTypeToString()
96{
97 if (dtype == MF_Byte) return "MF_Byte";
98 else if (dtype == MF_Int16) return "MF_Int16";
99 else if (dtype == MF_Float32) return "MF_Float32";
100 else return "Unknown??";
101}
102
103int MiniFITSFile::Write(void* data, size_t sz)
104{
105 fwrite(data, 1, sz, fip);
106 totwsz += sz;
107 return 0;
108}
109
110int MiniFITSFile::Read(void* data, size_t sz, size_t offset)
111{
112 fseek(fip, offset+MFITSHLEN, SEEK_SET);
113 fread(data, 1, sz, fip);
114 return 0;
115}
116
117void MiniFITSFile::FillHeader()
118{
119 for(int i=0; i<MFITSHLEN; i++) header[i]=' ';
120 strcpy(header, "SIMPLE = T / file does conform to FITS standard");
121 header[strlen(header)] = ' ';
122 int bpix = 8;
123 if (dtype == MF_Byte) bpix = 8;
124 else if (dtype == MF_Int16) bpix = 16;
125 else if (dtype == MF_Float32) bpix = -32;
126 char * buff = header+80;
127 sprintf(buff, "BITPIX = %20d / number of bits per data pixel", bpix);
128 buff[strlen(buff)] = ' ';
129 buff = header+160;
130 strcpy(buff, "NAXIS = 2 / number of data axes");
131 buff[strlen(buff)] = ' ';
132 buff = header+240;
133 sprintf(buff, "NAXIS1 = %20ld / number of bits per data pixel", (long)nax1);
134 buff[strlen(buff)] = ' ';
135 buff = header+320;
136 sprintf(buff, "NAXIS2 = %20ld / number of bits per data pixel", (long)nax2);
137 buff[strlen(buff)] = ' ';
138 buff = header+400;
139 strcpy(buff,"COMMENT BAO-Radio / MiniFITSFile ");
140 buff[strlen(buff)] = ' ';
141 buff = header+480;
142 strcpy(buff,"END");
143 buff[strlen(buff)] = ' ';
144
145 return;
146}
147
148
149void MiniFITSFile::DecodeHeader()
150{
151 // AMELIORER le decodage de l'entete, remplissage dtype, nax1, nax2
152 char * buff = header;
153 if (strncmp(buff, "SIMPLE =", 9) != 0)
154 throw MiniFITSException("MiniFITSFile::DecodeHeader()/Error - NO SIMPLE keyword");
155 bool fgokt=false;
156 bool fgok1=false;
157 bool fgok2=false;
158 for(int kh=80; kh<2800; kh+=80) {
159 buff = header+kh;
160 if (strncmp(buff, "NAXIS1 =", 9) == 0) {
161 nax1 = atol(buff+10);
162 fgok1 = true;
163// cout << " FOUND : NAXIS1= " << nax1 << endl;
164 }
165 else if (strncmp(buff, "NAXIS2 =", 9) == 0) {
166 nax2 = atol(buff+10);
167 fgok2 = true;
168// cout << " FOUND : NAXIS2= " << nax2 << endl;
169 }
170 else if (strncmp(buff, "BITPIX =", 9) == 0) {
171 int bpix = atoi(buff+10);
172 fgokt = true;
173 if (bpix == 8) dtype = MF_Byte;
174 else if (bpix == 16) dtype = MF_Int16;
175 else if (bpix == -32) dtype = MF_Float32;
176 else fgokt = false;
177// cout << " FOUND : bpix= " << bpix << endl;
178 }
179 }
180 if (!(fgok1&&fgok2&&fgokt))
181 throw MiniFITSException("MiniFITSFile::DecodeHeader()/Error- Missing/wrong NAXIS1/2,BITPIX");
182 return;
183}
184
Note: See TracBrowser for help on using the repository browser.