summaryrefslogtreecommitdiffstats
path: root/fitsy++/block.C
blob: b519e510716ca070806f97323b63511a64aee09a (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
// Copyright (C) 1999-2018
// Smithsonian Astrophysical Observatory, Cambridge, MA, USA
// For conditions of distribution and use, see copyright notice in "copyright"

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

#include "block.h"
#include "util.h"

FitsBlock::FitsBlock(FitsFile* fits, Vector& block)
{
  FitsHead* srcHead = fits->head();

  width_ = srcHead->naxis(0)/block[0];
  height_ = srcHead->naxis(1)/block[1];
  if (width_<1)
    width_ = 1;
  if (height_<1)
    height_ = 1;
  size_ = (size_t)width_*height_;

  primary_ = fits->primary();
  ext_ = fits->ext();
  inherit_ = fits->inherit();

  bitpix_ = srcHead->hdu()->bitpix();
  switch (bitpix_) {
  case -64:
    if (!(data_ = new double[size_]))
      return;
    dataSize_ = size_*sizeof(double);
    break;
  default:
    bitpix_ = -32;
    if (!(data_ = new float[size_]))
      return;
    dataSize_ = size_*sizeof(float);
    break;
  }
  dataSkip_ = 0;
  memset(data_, 0, dataSize_);

  initHeader(fits, block);
    
  // made it this far, must be valid
  byteswap_ = 0;
  endian_ = lsb() ? LITTLE : BIG;
  valid_ = 1;
}

FitsBlock::~FitsBlock()
{
  if (data_)
    delete [] (char*)data_;
}

void FitsBlock::initHeader(FitsFile* fits, Vector& block)
{
  head_ = new FitsHead(*(fits->head()));

  // change bitpix
  switch (bitpix_) {
  case -64:
    head_->setInteger("BITPIX", -64, "");
    break;
  default:
    head_->setInteger("BITPIX", -32, "");
    break;
  }

  // change width/height
  head_->setInteger("NAXIS1", width_, "");
  head_->setInteger("NAXIS2", height_, "");

  // IRAF
  initLTMV(block);
  initCCDSUM(block);
  initKeySEC("BIASSEC",block);
  initKeySEC("DATASEC",block);
  initKeySEC("TRIMSEC",block);

  // WCS blocking done later in FitsImage::resetWCS()

  // clear cards
  if (head_->find("BZERO"))
    head_->carddel("BZERO");
  if (head_->find("BSCALE"))
    head_->carddel("BSCALE");
  if (head_->find("DATASUM"))
    head_->carddel("DATASUM");
  if (head_->find("CHECKSUM"))
    head_->carddel("CHECKSUM");
  if (head_->find("DATAMIN"))
    head_->carddel("DATAMIN");
  if (head_->find("DATAMAX"))
    head_->carddel("DATAMAX");
  if (head_->find("DATAMEAN"))
    head_->carddel("DATAMEAN");
  if (head_->find("GOODMIN"))
    head_->carddel("GOODMIN");
  if (head_->find("GOODMAX"))
    head_->carddel("GOODMAX");
  if (head_->find("IRAF-MIN"))
    head_->carddel("IRAF-MIN");
  if (head_->find("IRAF-MAX"))
    head_->carddel("IRAF-MAX");

  head_->updateHDU();
}

void FitsBlock::initCCDSUM(Vector& block)
{
  if (head_->find("CCDSUM")) {
    char* val = head_->getString("CCDSUM");
    float xx,yy;
    istringstream istr(val);
    istr >> xx >> yy;

    xx *= block[0];
    yy *= block[1];

    ostringstream ostr;
    ostr << xx << ' ' << yy << ends;
    head_->setString("CCDSUM", ostr.str().c_str(), "");
  }
}

void FitsBlock::initKeySEC(const char* key, Vector& block)
{
  if (head_->find(key)) {
    char* sec = head_->getString(key);
    Vector ll,ur;
    parseSection(sec,&ll,&ur);
    Matrix mm = Translate(-1,-1) *
      Scale(1/block[0],1/block[1]) *
      Translate(1,1);

    Vector nll = ll*mm;
    Vector nur = ur*mm;
    ostringstream ostr;
    ostr << '[' << int(nll[0]) << ':' << int(nur[0])
	 << ',' << int(nll[1]) << ':' << int(nur[1]) << ']' << ends;
    head_->setString(key, ostr.str().c_str(), "");
  }
}

void FitsBlock::initLTMV(Vector& block)
{
  // always do LTMV
  double ltv1 = head_->getReal("LTV1", 0);
  double ltv2 = head_->getReal("LTV2", 0);
  double ltm11 = head_->getReal("LTM1_1", 1);
  double ltm12 = head_->getReal("LTM1_2", 0);
  double ltm21 = head_->getReal("LTM2_1", 0);
  double ltm22 = head_->getReal("LTM2_2", 1);

  Matrix mm(ltm11,ltm12,ltm21,ltm22,ltv1,ltv2);
  Matrix im = mm * 
    Translate(-.5,-.5) * 
    Scale(1./block[0],1./block[1]) * 
    Translate(.5,.5);

  if (head_->find("LTV1"))
    head_->setReal("LTV1", im.matrix(2,0), 8, "");
  else
    head_->appendReal("LTV1", im.matrix(2,0), 8, "");

  if (head_->find("LTV2"))
    head_->setReal("LTV2", im.matrix(2,1), 8, "");
  else
    head_->appendReal("LTV2", im.matrix(2,1), 8, "");

  if (head_->find("LTM1_1"))
    head_->setReal("LTM1_1", ltm11/block[0], 8, "");
  else
    head_->appendReal("LTM1_1", ltm11/block[0], 8, "");

  if (head_->find("LTM1_2"))
    head_->setReal("LTM1_2", ltm12, 8, "");
  else
    head_->appendReal("LTM1_2", ltm12, 8, "");

  if (head_->find("LTM2_1"))
    head_->setReal("LTM2_1", ltm21, 8, "");
  else
    head_->appendReal("LTM2_1", ltm21, 8, "");

  if (head_->find("LTM2_2"))
    head_->setReal("LTM2_2", ltm22/block[1], 8, "");
  else
    head_->appendReal("LTM2_2", ltm22/block[1], 8, "");
}