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