summaryrefslogtreecommitdiffstats
path: root/tksao/fitsy++/hdu.C
blob: 27f1a5964ea558c8be0efba258289ded80f51de7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// Copyright (C) 1999-2016
// Smithsonian Astrophysical Observatory, Cambridge, MA, USA
// For conditions of distribution and use, see copyright notice in "copyright"

#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;

#include "hdu.h"
#include "head.h"

FitsHDU::FitsHDU(FitsHead* head)
{
  extname_ = head->getString("EXTNAME");
  // trim any spaces at end
  if (extname_) {
    for (int ii=strlen(extname_)-1; ii>=0; ii--) {
      if (extname_[ii] == ' ')
	extname_[ii] = '\0';
      else
	break;
    }
  }

  extver_ = head->getInteger("EXTVER", 0);

  bitpix_ = head->getInteger("BITPIX", 0);
  naxes_ = head->getInteger("NAXIS", 0);
  if (naxes_>FTY_MAXAXES)
    naxes_ = FTY_MAXAXES;

  // init naxis_
  for(int i=0; i<FTY_MAXAXES; i++)
    naxis_[i] = 0;

  for(int i=0; i<naxes_; i++)
    naxis_[i] = head->getInteger(keycat("NAXIS",(i+1)), 0);

  // special case: 1D image
  if (naxis_[0]>0 && naxis_[1]==0)
    naxis_[1] = 1;

  realbytes_ = 0;
  heapbytes_ = head->getInteger("PCOUNT",0);
  allbytes_ = 0;
  padbytes_ = 0;
  databytes_ = 0;
  datablocks_ = 0;
}

FitsHDU::~FitsHDU()
{
  if (extname_)
    delete [] extname_;
}

char* FitsHDU::keycat(const char* name, int i)
{
  ostringstream str;
  str << name << i << ends;
  memcpy(keybuf,str.str().c_str(),str.str().length());
  return keybuf;
}

void FitsHDU::updateCards(FitsHead* head)
{
  head->setInteger("BITPIX", bitpix_, NULL);
  head->setInteger("NAXIS", naxes_, NULL);

  for (int i=1; i<=naxes_; i++)
      head->setInteger(keycat("NAXIS", i), naxis_[i-1], NULL);
}

// FitsImageHDU

FitsImageHDU::FitsImageHDU(FitsHead* head) : FitsHDU(head)
{
  size_t imgpixels = (size_t)naxis_[0]*naxis_[1];
  imgbytes_ = imgpixels * (abs(bitpix_)/8);

  size_t realpixels;
  if (naxes_>0) {
    realpixels = 1;
    for (int i=0; i<naxes_; i++ )
      realpixels *= naxis_[i];
  }
  else
    realpixels = 0;

  realbytes_ = realpixels * (abs(bitpix_)/8);
  allbytes_ = realbytes_ + heapbytes_;
  datablocks_ = (allbytes_ + (FTY_BLOCK-1))/FTY_BLOCK;
  databytes_ = datablocks_ * FTY_BLOCK;
  padbytes_ = databytes_ - allbytes_;

  bzero_ = head->getReal("BZERO", 0.0);
  bscale_ = head->getReal("BSCALE", 1.0);
  hasblank_ = head->find("BLANK") ? 1:0;
  blank_ = head->getInteger("BLANK", 0);
}

void FitsImageHDU::updateCards(FitsHead* head)
{
  FitsHDU::updateCards(head);
  if (blank_)
    if (bitpix_ > 0)
      head->setInteger("BLANK", blank_, NULL);

  if (bzero_)
    head->setReal("BZERO", bzero_ , 7, NULL);
  if (bscale_ != 1)
    head->setReal("BSCALE", bscale_, 7, NULL);
}

// FitsTableHDU

FitsTableHDU::FitsTableHDU(FitsHead* head) : FitsHDU(head)
{
  tfields_ = head->getInteger("TFIELDS", 0);
  cols_ = NULL;

  // number of rows * width of row in bytes
  realbytes_ = (size_t)naxis_[0]*naxis_[1]; 
  allbytes_ = realbytes_ + heapbytes_;
  datablocks_ = (allbytes_ + (FTY_BLOCK-1))/FTY_BLOCK;
  databytes_ = datablocks_ * FTY_BLOCK;
  padbytes_ = databytes_ - allbytes_;
}

FitsTableHDU::~FitsTableHDU()
{
  if (cols_) {
    for (int i=0; i<tfields_; i++)
      if (cols_[i])
	delete cols_[i];

    delete [] cols_;
  }
}

char* FitsTableHDU::list()
{
  ostringstream str;
  for (int i=0; i<tfields_; i++)
    if (cols_[i])
      str << cols_[i]->ttype() << ' ';
  str << ends;
  return dupstr(str.str().c_str());
}

FitsColumn* FitsTableHDU::find(const char* name)
{
  char* n = toUpper(name);
  // trim any spaces
  char* nn = n;
  while (*nn)
    nn++;
  nn--;
  while (*nn == ' ')
    *nn-- = '\0';

  for (int i=0; i<tfields_; i++) {
    if (cols_[i]) {

      char* t = toUpper(cols_[i]->ttype());
      // trim any spaces
      char* tt=t;
      while (*tt)
	tt++;
      tt--;
      while (*tt == ' ')
	*tt-- = '\0';

      if (!strncmp(n,t,strlen(n)) && strlen(n)==strlen(t)) {
	delete [] n;
	delete [] t;
	return cols_[i];
      }

      delete [] t;
    }
  }

  delete [] n;
  return NULL;
}

FitsColumn* FitsTableHDU::find(int i)
{
  if (i>=0 && i<tfields_)
    return cols_[i];

  return NULL;
}

Vector FitsTableHDU::dimension(const char* name)
{
  FitsColumn* col = find(name);
  return col ? col->dimension() : Vector();
}

FitsAsciiTableHDU::FitsAsciiTableHDU(FitsHead* head) : FitsTableHDU(head)
{
  cols_ = new FitsColumn*[tfields_];

  size_t offset = 0;
  for (int i=0; i<tfields_; i++) {
    char* tform = head->getString(keycat("TFORM",i+1));
    char type = 'F';
    if (tform) {
      string x(tform);
      istringstream str(x);
      str >> type;
    }

    switch (type) {
    case 'A':
      cols_[i] = new FitsAsciiColumnStr(head, i+1, offset);
      break;
    case 'I':
      cols_[i] = new FitsAsciiColumnT<int>(head, i+1, offset);
      break;
    case 'F':
      cols_[i] = new FitsAsciiColumnT<float>(head, i+1, offset);
      break;
    case 'E':
      cols_[i] = new FitsAsciiColumnT<float>(head, i+1, offset);
      break;
    case 'D':
      cols_[i] = new FitsAsciiColumnT<double>(head, i+1, offset);
      break;
    }

    delete [] tform;
    if (cols_[i])
      offset += cols_[i]->width();
  }
}

FitsBinTableHDU::FitsBinTableHDU(FitsHead* head) : FitsTableHDU(head)
{
  cols_ = new FitsColumn*[tfields_];

  int offset =0;
  for (int i=0; i<tfields_; i++) {
    char* tform = head->getString(keycat("TFORM",i+1));
    int repeat;
    char type = 'J';
    if (tform) {
      string x(tform);
      istringstream str(x);
      if (isalpha(tform[0]))
	str >> type;
      else
	str >> repeat >> type;
    }

    switch (type) {
    case 'L':
      cols_[i] = new FitsBinColumnLogical(head, i+1, offset);
      break;
    case 'X':
      cols_[i] = new FitsBinColumnBit(head, i+1, offset);
      break;
    case 'B':
      cols_[i] = new FitsBinColumnT<unsigned char>(head, i+1, offset);
      break;
    case 'I':
      cols_[i] = new FitsBinColumnT<short>(head, i+1, offset);
      break;
    case 'U':
      cols_[i] = new FitsBinColumnT<unsigned short>(head, i+1, offset);
      break;
    case 'J':
      cols_[i] = new FitsBinColumnT<int>(head, i+1, offset);
      break;
    case 'V':
      cols_[i] = new FitsBinColumnT<unsigned int>(head, i+1, offset);
      break;
    case 'K':
      cols_[i] = new FitsBinColumnT<long long>(head, i+1, offset);
      break;
    case 'A':
      cols_[i] = new FitsBinColumnStr(head, i+1, offset);
      break;
    case 'E':
      cols_[i] = new FitsBinColumnT<float>(head, i+1, offset);
      break;
    case 'D':
      cols_[i] = new FitsBinColumnT<double>(head, i+1, offset);
      break;
    case 'C':
      cols_[i] = NULL;
      internalError("Fitsy++ hdu single precision complex column type not supported");
      break;
    case 'M':
      cols_[i] = NULL;
      internalError("Fitsy++ hdu double precision complex column type not supported");
      break;
    case 'P':
      cols_[i] = new FitsBinColumnArrayP(head, i+1, offset);
      break;
    case 'Q':
      cols_[i] = new FitsBinColumnArrayQ(head, i+1, offset);
      break;

    default:
      cols_[i] = NULL;
      internalError("Fitsy++ hdu unknown table column type");
      break;
    }

    delete [] tform;
    if (cols_[i])
      offset += cols_[i]->width();
  }
}