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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
|
// Copyright (C) 1999-2018
// Smithsonian Astrophysical Observatory, Cambridge, MA, USA
// For conditions of distribution and use, see copyright notice in "copyright"
#include <string.h>
#include <ctype.h>
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;
#include "hist.h"
#include "util.h"
#include "fitsy.h"
#include "filter.h"
#ifdef __CYGWIN__
// limit size, cygwin pipe to 64K / (4 args x 4 bytes)
#define FILTERSIZE 2048
#else
#define FILTERSIZE 65536
#endif
#define MULTWCS 27
char *gerrorstring(void);
static const char* reservedKeywords[] = {
"SIMPLE","XTENSION","ZSIMPLE","ZTENSION","ZIMAGE","ZTABLE",
"BITPIX","ZBITPIX",
"NAXIS","ZNAXIS",
"NAXIS.","ZNAXIS.",
"END",
"PCOUNT","ZPCOUNT",
"GCOUNT","ZGCOUNT",
"CHECKSUM","ZHECKSUM",
"DATASUM","ZDATASUM",
/* "OBJECT", */
"INHERIT",
"BSCALE",
"BZERO","TZERO.",
/* "BUNIT", */ "TUNIT.",
"BLANK",
"DATAMAX","TDMAX.","TLMAX.",
"DATAMIN","TDMIN.","TLMIN.",
"BLOCKED","ZBLOCKED",
"EXTENDED","ZEXTEND",
"EXTNAME","ZNAME.",
"EXTVER",
"EXTLEVEL",
"TFIELDS",
"TFORM.","ZFORM.",
"TBCOL.",
"ZCMPTYPE","ZCTYP.","ZTILELEN","ZTILE.","ZVAL.",
"ZMASKCMP","ZQUANTIZ","ZDITHER0","ZTHEAP",
"TSCAL.","TNULL.","TTYPE.","TDISP.", "TDIM.","THEAP",
"FZTILELN","FZALGOR","FZALG.",
"PTYPE.","PSCAL.","PZERO.",
"WCSAXES.","WCSAX.?",
/* "CTYPE.?", */ ".CTYP.",".CTY.?","TCTYP.","TCTY.?",
/* "CUNIT.?", */ ".CUNI.",".CUN.?","TCUNI.","TCUN.?",
/* "CRVAL.?", */ ".CRVL.",".CRV.?","TCRVL.","TCRV.?",
/* "CDELT.?", */ ".CDLT.",".CDE.?","TCDLT.","TCDE.?",
/* "CRPIX.?", */ ".CRPX.",".CRP.?","TCRPX.","TCRP.?",
/* "CROTA.", */ ".CROT.","TCROT.",
/* "PC._.?", */ "..PC.?","TPC._.?","TP._.?",
/* "CD._.?", */ "..CD.?","TCD._.?","TC._.?",
"PV._.?",".PV._.?",".V._.?","TPV._.?","TV._.?",
".V._X?",
"PS._.?",".PS._.?",".S._.?","TPS._.?","TS._.?",
"WCSNAME?","WCSN.?","WCS.?","TWCS.?",
"CNAME.?",".CNA.?","TCNA.?",
"CRDER.?",".CRD.?","TCRD.?",
"CSYER.?",".CSY.?","TCSY.?",
"WCST.?",
"WCSX.?",
/* "LONPOLE?", */ "LONP.?",
/* "LATPOLE?", */ "LATP.?",
/* "EQUINOX?", */ "EQUI.?",
/* "EPOCH", */
/* "RADESYS", */ "REDE.?",
"RESTFRQ?","RFRQ.?",
"RESTWAV?","RWAV.?",
"SPECSYS?","SPEC.?",
"SSYSOBS?","SOBS.?",
"SSYSSRC?","SSRC.?",
"OBSGEO-X","OBSGX.",
"OBSGEO-Y","OBSGY.",
"OBSGEO-Z","OBSGZ.",
"VELOSYS?","VSYS.?",
"ZSOURCE?","ZSOU.?",
"VELANGL?","VANG.?",
/* "DATE",*/
/* "DATE-OBS", */ "DOBS.",
/* "MJD-OBS", */ "MJDOB.",
"BEPOCH",
"JEPOCH",
/* "DATE-AVE", */ "DAVG.",
/* "MJD-AVG", */ "MJDA.",
/* "DATE-BEG", */
/* "MJD-BEG", */
/* "TSTART", "TSTOP", */
/* "DATE-END", */
/* "MJD-END", */
"XPOSURE",
"TELAPSE",
/* "TIMESYS",*/
/* "MJDREF",*/
/* "JDREF",*/
/* "DATEREF",*/
"TREFPOS","TRPOS.",
"TREFDIR","TRDIR.",
"PLEPHEM",
/* "TIMEUNIT",*/
/* "TIMEOFFS",*/
/* "TIMSYER",*/
/* "TIMRDER",*/
/* "TIMEDEL",*/
/* "TIMEPIXR",*/
"CZPHS",".CZPH.",".CZP.?","TCZPH.","TCZP.?",
"CPERI",".CPER.",".CPR.?","TCPER.","TCPR.?"
};
FitsHist::FitsHist(FitsFile* fits, int w, int h, int d,
Matrix& m, Function func, Vector block)
: width_(w), height_(h), depth_(d)
{
size_ = (size_t)width_*height_*depth_;
xcol_ = NULL;
ycol_ = NULL;
zcol_ = NULL;
fitsy_ = NULL;
filter_ = NULL;
valid_ = 0;
if (!initHeader(fits))
return;
// we need to translate by another .5 for the offset from Data to Image
Matrix mm = m * Translate(.5,.5);
initLTMV(mm);
initWCS(fits, mm, block);
initFilter(fits);
bin(fits, m, func, block);
if (byteswap_)
swap();
deleteFilter();
valid_ = 1;
}
FitsHist::~FitsHist()
{
if (data_)
delete [] (float*)data_;
}
int FitsHist::initHeader(FitsFile* fits)
{
FitsHead* srcHead = fits->head();
FitsTableHDU* srcHDU = (FitsTableHDU*)(srcHead->hdu());
// make sure we have a table with columns, X, Y
if (!fits->isBinTable())
return 0;
// make sure we have rows and cols
if (!srcHDU->width() || !srcHDU->rows())
return 0;
// get X column
if (fits->pBinX())
xcol_ = srcHDU->find(fits->pBinX());
if (!xcol_)
return 0;
// get Y column
if (fits->pBinY())
ycol_ = srcHDU->find(fits->pBinY());
if (!ycol_)
return 0;
// get Z column (if specified)
if (fits->pBinZ() && depth_ > 1)
zcol_ = srcHDU->find(fits->pBinZ());
else
zcol_ = NULL;
// create header
head_ = new FitsHead(width_, height_, depth_, -32);
if (!head_->isValid())
return 0;
// now screen other KEYWORDS for addition
char* cc = srcHead->first();
while (cc) {
if (screenKeyword(cc))
head_->cardins(cc, (char*)NULL);
cc = srcHead->next();
}
// MJD-OBJ deprecated
double rr = srcHead->getReal("MJD_OBS",0);
if (rr)
head_->appendReal("MJD-OBS", rr, 10, NULL);
// we added cards
head_->updateHDU();
return 1;
}
static int mstrcmp(const char* s1, const char* s2)
{
const char* p1 = s1;
const char* p2 = s2;
while (*p1 != '\0') {
if (*p1 == '?')
return 0;
if (*p2=='\0')
return 1;
if (*p1 != '.') {
if (*p2>*p1)
return -1;
if (*p2<*p1)
return 1;
}
else {
if (*p2<'0' || *p2>'9')
return 1;
p2++;
if (*p2<'0' || *p2>'9')
p2--;
}
p1++;
p2++;
}
if (*p2 != '\0')
return -1;
return 0;
}
int FitsHist::screenKeyword(const char* cc)
{
int cnt= sizeof(reservedKeywords)/sizeof(const char*);
char key[9];
memset(key,0,9);
strncpy(key,cc,8);
for (int ii=8; ii>=0; ii--)
if (key[ii]==' ')
key[ii] = '\0';
const char** ptr = reservedKeywords;
for (int ii=0; ii<cnt; ii++) {
if (!mstrcmp(*ptr,key))
return 0;
ptr++;
}
return 1;
}
void FitsHist::initFilter(FitsFile* fits)
{
FitsHead* srcHead = fits->head();
const char* filtstr = fits->pFilter();
if (filtstr && *filtstr) {
ostringstream str;
str << "bincols=(" << fits->pBinX() << ',' << fits->pBinY() << ')';
if (byteswap_)
str << ",convert=true";
str << ends;
if (!(fitsy_ = ft_headinit(srcHead->cards(), srcHead->headbytes())))
internalError("Fitsy++ hist bad filter head");
else {
if (!(filter_ = FilterOpen((FITSHead)fitsy_, (char*)filtstr,
(char*)str.str().c_str()))){
internalError("Fitsy++ hist unable to build filter");
}
}
}
}
void FitsHist::deleteFilter()
{
if (filter_) {
FilterClose((Filter)filter_);
filter_ = NULL;
}
if (fitsy_) {
ft_headfree((FITSHead)fitsy_,0);
fitsy_ = NULL;
}
}
void FitsHist::bin(FitsFile* fits, Matrix& m, Function func, Vector block)
{
FitsHead* srcHead = fits->head();
FitsTableHDU* srcHDU = (FitsTableHDU*)(srcHead->hdu());
// create image space
float* dest = new float[size_];
memset(dest, 0, size_*sizeof(float));
// bin it up
char* ptr = (char*)fits->data();
int rowlen = srcHDU->width();
int rows = srcHDU->rows();
// third dimension
double zmin;
double zlength;
if (zcol_) {
zmin = zcol_->getMin();
zlength = zcol_->getMax() - zcol_->getMin();
}
// filter
int goodincr = 0;
int goodindex = FILTERSIZE;
int* good = NULL;
if (filter_)
good = new int[FILTERSIZE];
// matrix
register double m00 = m.matrix(0,0);
register double m10 = m.matrix(1,0);
register double m20 = m.matrix(2,0);
register double m01 = m.matrix(0,1);
register double m11 = m.matrix(1,1);
register double m21 = m.matrix(2,1);
for (int ii=0; ii<rows; ii++, ptr+=rowlen, goodindex++) {
// incr filter block, if needed
if (good && (goodindex>=FILTERSIZE)) {
// for memory models that support internal paging
// need at lease FILTERSIZE rows
ptr = fits->page(ptr, rowlen*FILTERSIZE);
int diff = srcHDU->rows() - (goodincr * FILTERSIZE);
if (FilterEvents((Filter)filter_, ptr, srcHDU->width(),
(diff<FILTERSIZE) ? diff : FILTERSIZE, good)) {
goodincr++;
goodindex = 0;
}
else {
if (good)
delete [] good;
good = NULL;
internalError("Fitsy++ hist filter failed");
}
}
else {
// for memory models that support internal paging
// all we need is just one row
ptr = fits->page(ptr, rowlen);
}
if (!good || (good && good[goodindex])) {
register double x = xcol_->value(ptr);
register double y = ycol_->value(ptr);
register double X = x*m00 + y*m10 + m20;
register double Y = x*m01 + y*m11 + m21;
if (X >= 0 && X < width_ && Y >= 0 && Y < height_) {
if (!zcol_)
dest[((int)Y)*width_ + (int)X]++;
else {
int zz = (int)((zcol_->value(ptr)-zmin)/zlength*depth_);
if (zz>=0 && zz<depth_)
dest[(zz*width_*height_) + ((int)Y)*width_ + (int)X]++;
}
}
}
}
// for memory models that support internal paging
fits->resetpage();
// Average
if (func==AVERAGE)
for (size_t kk=0; kk<size_; kk++)
dest[kk] /= (block[0]*block[1]);
if (good)
delete [] good;
data_ = dest;
dataSize_ = size_;
dataSkip_ = 0;
}
void FitsHist::swap()
{
if (!data_)
return;
// we now need to byteswap back to native form
float* dest = (float*)data_;
for (size_t ii=0; ii<size_; ii++) {
const char* p = (char*)(dest+ii);
union {
char c[4];
float f;
} u;
u.c[3] = *p++;
u.c[2] = *p++;
u.c[1] = *p++;
u.c[0] = *p;
dest[ii] = u.f;
}
}
void FitsHist::initLTMV(Matrix& m)
{
head_->appendReal("LTM1_1", m[0][0], 10, NULL);
head_->appendReal("LTM1_2", m[0][1], 10, NULL);
head_->appendReal("LTM2_1", m[1][0], 10, NULL);
head_->appendReal("LTM2_2", m[1][1], 10, NULL);
head_->appendReal("LTV1" , m[2][0], 10, NULL);
head_->appendReal("LTV2" , m[2][1], 10, NULL);
}
void FitsHist::mapWCSString(FitsHead* head, char* w,
const char* out, const char* prim)
{
ostringstream istr;
istr << prim << xcol_->index() << w << ends;
if (head->find(istr.str().c_str())) {
char* str = head->getString(istr.str().c_str());
head_->appendString(out, str, NULL);
}
}
void FitsHist::mapWCSString(FitsHead* head, char* w,
const char* out, const char* prim, const char* alt)
{
ostringstream istr1, istr2;
if (!w[0]) {
istr1 << prim << xcol_->index() << w << ends;
istr2 << prim << ycol_->index() << w << ends;
}
else {
istr1 << alt << xcol_->index() << w << ends;
istr2 << alt << ycol_->index() << w << ends;
}
ostringstream ostr1, ostr2;
ostr1 << out << "1" << w << ends;
ostr2 << out << "2" << w << ends;
if (head->find(istr1.str().c_str())) {
char* cc1 = head->getString(istr1.str().c_str());
head_->appendString(ostr1.str().c_str(), cc1, NULL);
}
if (head->find(istr2.str().c_str())) {
char* cc2 = head->getString(istr2.str().c_str());
head_->appendString(ostr2.str().c_str(), cc2, NULL);
}
}
void FitsHist::mapWCSReal(FitsHead* head, const char* out, const char* in)
{
ostringstream istr;
istr << in << xcol_->index() << ends;
if (head->find(istr.str().c_str())) {
float cc = head->getReal(istr.str().c_str(), 0);
head_->appendReal(out, cc, 10, NULL);
}
}
void FitsHist::mapWCSReal(FitsHead* head, char* w,
const char* out, const char* in)
{
ostringstream istr;
istr << in << xcol_->index() << w << ends;
if (head->find(istr.str().c_str())) {
float cc = head->getReal(istr.str().c_str(), 0);
head_->appendReal(out, cc, 10, NULL);
}
}
void FitsHist::mapWCSReal(FitsHead* head, char* w,
const char* out, const char* prim, const char* alt,
Matrix mm)
{
ostringstream istr1, istr2;
if (!w[0]) {
istr1 << prim << xcol_->index() << w << ends;
istr2 << prim << ycol_->index() << w << ends;
}
else {
istr1 << alt << xcol_->index() << w << ends;
istr2 << alt << ycol_->index() << w << ends;
}
ostringstream ostr1, ostr2;
ostr1 << out << "1" << w << ends;
ostr2 << out << "2" << w << ends;
if (head->find(istr1.str().c_str()) ||
head->find(istr2.str().c_str())) {
float cc1 = head->getReal(istr1.str().c_str(),0);
float cc2 = head->getReal(istr2.str().c_str(),0);
Vector cc = Vector(cc1,cc2) * mm;
head_->appendReal(ostr1.str().c_str(), cc[0], 10, NULL);
head_->appendReal(ostr2.str().c_str(), cc[1], 10, NULL);
}
}
void FitsHist::mapWCSMatrix(FitsHead* head, char* w,
const char* out, const char* in,
Vector vv)
{
ostringstream istr1, istr2, istr3, istr4;
istr1 << in << xcol_->index() << "_" << xcol_->index() << w << ends;
istr2 << in << xcol_->index() << "_" << ycol_->index() << w << ends;
istr3 << in << ycol_->index() << "_" << xcol_->index() << w << ends;
istr4 << in << ycol_->index() << "_" << ycol_->index() << w << ends;
ostringstream ostr1, ostr2, ostr3, ostr4;
ostr1 << out << "1_1" << w << ends;
ostr2 << out << "1_2" << w << ends;
ostr3 << out << "2_1" << w << ends;
ostr4 << out << "2_2" << w << ends;
if (head->find(istr1.str().c_str()) ||
head->find(istr2.str().c_str()) ||
head->find(istr3.str().c_str()) ||
head->find(istr4.str().c_str())) {
float cc11 = head->getReal(istr1.str().c_str(), 0);
float cc12 = head->getReal(istr2.str().c_str(), 0);
float cc21 = head->getReal(istr3.str().c_str(), 0);
float cc22 = head->getReal(istr4.str().c_str(), 0);
Matrix cc = Matrix(cc11*vv[0], cc12*vv[0],
cc21*vv[1], cc22*vv[1],
0, 0);
head_->appendReal(ostr1.str().c_str(), cc[0][0], 10, NULL);
head_->appendReal(ostr2.str().c_str(), cc[0][1], 10, NULL);
head_->appendReal(ostr3.str().c_str(), cc[1][0], 10, NULL);
head_->appendReal(ostr4.str().c_str(), cc[1][1], 10, NULL);
}
}
void FitsHist::mapWCSVector(FitsHead* head, char* w,
const char* out, const char* in)
{
for (int ii=0; ii<=9; ii++) {
ostringstream istr1, istr2;
istr1 << in << xcol_->index() << "_" << ii << w << ends;
istr2 << in << ycol_->index() << "_" << ii << w << ends;
ostringstream ostr1, ostr2;
ostr1 << out << "1_" << ii << ends;
ostr2 << out << "2_" << ii << ends;
if (head->find(istr1.str().c_str()) ||
head->find(istr2.str().c_str())) {
float cc1 = head->getReal(istr1.str().c_str(), 0);
float cc2 = head->getReal(istr2.str().c_str(), 0);
head_->appendReal(ostr1.str().c_str(), cc1, 10, NULL);
head_->appendReal(ostr2.str().c_str(), cc2, 10, NULL);
}
}
}
void FitsHist::initWCS(FitsFile* fits, Matrix& mm, Vector block)
{
FitsHead* srcHead = fits->head();
char w[2];
w[1] = '\0';
for (int i=0; i<MULTWCS; i++) {
if (!i)
w[0] = '\0';
else
w[0] = '@'+i;
mapWCSString(srcHead, w, "CTYPE", "TCTYP", "TCTY");
mapWCSString(srcHead, w, "CUNIT", "TCUNI", "TCUN");
mapWCSReal(srcHead, w, "CRVAL", "TCRVL", "TCRV", Matrix());
mapWCSReal(srcHead, w, "CDELT", "TCDLT", "TCDE", Scale(block));
mapWCSReal(srcHead, w, "CRPIX", "TCRPX", "TCRP", mm);
mapWCSReal(srcHead, w, "CROTA", "TCROT", "TCRO", Matrix());
mapWCSMatrix(srcHead, w, "PC", "TP", Vector(1,1));
mapWCSMatrix(srcHead, w, "CD", "TC", block);
mapWCSVector(srcHead, w, "PV", "TV");
mapWCSVector(srcHead, w, "PS", "TS");
mapWCSString(srcHead, w, "WCSNAME", "TWCS");
// alt
mapWCSMatrix(srcHead, w, "PC", "TPC", Vector(1,1));
mapWCSMatrix(srcHead, w, "CD", "TCD", block);
mapWCSVector(srcHead, w, "PV", "TPV");
mapWCSVector(srcHead, w, "PS", "TPS");
mapWCSString(srcHead, w, "WCSNAME", "WCS");
mapWCSReal(srcHead, w, "LONPOLE", "LONP");
mapWCSReal(srcHead, w, "LATPOLE", "LATP");
if (!head_->find("EQUINOX"))
mapWCSReal(srcHead, w, "EQUINOX", "EQUI");
if (!head_->find("MJD-OBS"))
mapWCSReal(srcHead, "MJD-OBS", "MJDOB");
if (!head_->find("RADESYS"))
mapWCSString(srcHead, w, "RADESYS", "RADE");
// general
mapWCSString(srcHead, w, "BUNIT", "TUNIT");
}
}
FitsHistNext::FitsHistNext(FitsFile* prev)
{
primary_ = prev->primary();
managePrimary_ = 0;
head_ = prev->head();
manageHead_ = 0;
FitsImageHDU* hdu = (FitsImageHDU*)head_->hdu();
data_ = (char*)prev->data() + hdu->imgbytes();
dataSize_ = 0;
dataSkip_ = 0;
ext_ = prev->ext();
inherit_ = prev->inherit();
byteswap_ = prev->byteswap();
endian_ = prev->endian();
valid_ = 1;
return;
}
|