Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 70433a5

Browse files
buckleygeerpcanal
authored andcommittedFeb 5, 2018
Fix LOGICAL type suport in TFITSIO
Solves a bug in the TFITSIO code when reading a FITS binary table that contained a column of type LOGICAL. The code in TFISTIO:LoadHDU only had tests for column types of strings or numeric so it was failing with the error “bad binary table datatype”
1 parent 55dff8c commit 70433a5

File tree

4 files changed

+561
-36
lines changed

4 files changed

+561
-36
lines changed
 

‎.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@
1717
*.jpg binary
1818
*.pdf binary
1919
*.root binary
20+
*.fits binary

‎graf2d/fitsio/src/TFITS.cxx

+76-36
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ several methods to manage them.
8585
#include "fitsio.h"
8686
#include <stdlib.h>
8787

88+
8889
ClassImp(TFITSHDU);
8990

9091
////////////////////////////////////////////////////////////////////////////////
@@ -380,6 +381,7 @@ Bool_t TFITSHDU::LoadHDU(TString& filepath_filter)
380381

381382
for (colnum = 0, cellindex = 0; colnum < fNColumns; colnum++) {
382383
fits_get_coltype(fp, colnum+1, &typecode, &repeat, &width, &status);
384+
383385
if (status) goto ERR;
384386

385387
if ((typecode == TDOUBLE) || (typecode == TSHORT) || (typecode == TLONG)
@@ -441,60 +443,89 @@ Bool_t TFITSHDU::LoadHDU(TString& filepath_filter)
441443

442444
fColumnsInfo[colnum].fDim = (Int_t) repeat;
443445

444-
double *array;
446+
double *array = 0;
447+
char *arrayl = 0;
445448

446449
if (repeat > 0) {
447-
array = new double [table_rows * repeat]; //Hope you got a big machine! Ask China otherwise :-)
448-
fits_read_col(fp, TDOUBLE, colnum+1, 1, 1, table_rows * repeat, &nulval, array, &anynul, &status);
449450

450-
if (status) {
451-
delete [] array;
452-
goto ERR;
451+
if (typecode == TLOGICAL) {
452+
arrayl = new char[table_rows * repeat];
453+
fits_read_col(fp, TLOGICAL, colnum + 1, 1, 1, table_rows * repeat, &nulval, arrayl, &anynul,
454+
&status);
455+
if (status) {
456+
delete[] arrayl;
457+
goto ERR;
458+
}
459+
} else {
460+
array = new double[table_rows * repeat]; // Hope you got a big machine! Ask China otherwise :-)
461+
fits_read_col(fp, TDOUBLE, colnum + 1, 1, 1, table_rows * repeat, &nulval, array, &anynul,
462+
&status);
463+
if (status) {
464+
delete[] array;
465+
goto ERR;
466+
}
453467
}
468+
454469
} else {
455-
//No elements: set dummy
456-
array = new double [table_rows];
470+
// No elements: set dummy
471+
array = new double[table_rows];
457472
for (long row = 0; row < table_rows; row++) {
458473
array[row] = 0.0;
459474
}
460475
}
461476

462-
//Save values
477+
// Save values
463478
if (repeat == 1) {
464-
//Scalar
465-
for (long row = 0; row < table_rows; row++) {
466-
fCells[cellindex++].fRealNumber = array[row];
479+
// Scalar
480+
if (typecode == TLOGICAL) {
481+
for (long row = 0; row < table_rows; row++) {
482+
int temp = (signed char)arrayl[row];
483+
fCells[cellindex++].fRealNumber = (double)temp;
484+
}
485+
delete[] arrayl;
486+
} else {
487+
for (long row = 0; row < table_rows; row++) {
488+
fCells[cellindex++].fRealNumber = array[row];
489+
}
490+
delete[] array;
467491
}
468492
} else if (repeat > 1) {
469-
//Vector
470-
for (long row = 0; row < table_rows; row++) {
471-
double *vec = new double [repeat];
472-
long offset = row * repeat;
473-
for (long component = 0; component < repeat; component++) {
474-
vec[component] = array[offset++];
493+
// Vector
494+
if (typecode == TLOGICAL) {
495+
for (long row = 0; row < table_rows; row++) {
496+
double *vec = new double[repeat];
497+
long offset = row * repeat;
498+
for (long component = 0; component < repeat; component++) {
499+
int temp = (signed char)arrayl[offset++];
500+
vec[component] = (double)temp;
501+
}
502+
fCells[cellindex++].fRealVector = vec;
503+
}
504+
delete[] arrayl;
505+
} else {
506+
for (long row = 0; row < table_rows; row++) {
507+
double *vec = new double[repeat];
508+
long offset = row * repeat;
509+
for (long component = 0; component < repeat; component++) {
510+
vec[component] = array[offset++];
511+
}
512+
fCells[cellindex++].fRealVector = vec;
475513
}
476-
fCells[cellindex++].fRealVector = vec;
514+
delete[] array;
477515
}
478516
}
479-
480-
delete [] array;
481-
482517
}
483-
484518
} else {
485519
Warning("LoadHDU", "error opening FITS file. Column type %d is currently not supported", typecode);
486520
}
487521
}
488522

489-
490-
491523
if (hdutype == ASCII_TBL) {
492-
//ASCII table
524+
// ASCII table
493525

494526
} else {
495-
//Binary table
527+
// Binary table
496528
}
497-
498529
}
499530

500531
// Close file
@@ -1058,13 +1089,16 @@ TVectorD* TFITSHDU::GetArrayRow(UInt_t row)
10581089
}
10591090

10601091
offset = W * row;
1061-
TVectorD *vec = new TVectorD(W);
1062-
double *v = vec->GetMatrixArray();
1092+
double *v = new double[W];
10631093

10641094
for (i = 0; i < W; i++) {
10651095
v[i] = fPixels->GetAt(offset+i);
10661096
}
10671097

1098+
TVectorD *vec = new TVectorD(W, v);
1099+
1100+
delete [] v;
1101+
10681102
return vec;
10691103
}
10701104

@@ -1094,13 +1128,15 @@ TVectorD* TFITSHDU::GetArrayColumn(UInt_t col)
10941128
return 0;
10951129
}
10961130

1097-
TVectorD *vec = new TVectorD(H);
1098-
double *v = vec->GetMatrixArray();
1131+
double *v = new double[H];
10991132

11001133
for (i = 0; i < H; i++) {
11011134
v[i] = fPixels->GetAt(W*i+col);
11021135
}
11031136

1137+
TVectorD *vec = new TVectorD(H, v);
1138+
1139+
delete [] v;
11041140

11051141
return vec;
11061142
}
@@ -1208,13 +1244,15 @@ TVectorD* TFITSHDU::GetTabRealVectorColumn(Int_t colnum)
12081244

12091245
Int_t offset = colnum * fNRows;
12101246

1211-
TVectorD *res = new TVectorD(fNRows);
1212-
Double_t *arr = res->GetMatrixArray();
1247+
Double_t *arr = new Double_t[fNRows];
12131248

12141249
for (Int_t row = 0; row < fNRows; row++) {
12151250
arr[row] = fCells[offset + row].fRealNumber;
12161251
}
12171252

1253+
TVectorD *res = new TVectorD();
1254+
res->Use(fNRows, arr);
1255+
12181256
return res;
12191257
}
12201258

@@ -1245,13 +1283,15 @@ TVectorD* TFITSHDU::GetTabRealVectorColumn(const char *colname)
12451283

12461284
Int_t offset = colnum * fNRows;
12471285

1248-
TVectorD *res = new TVectorD(fNRows);
1249-
Double_t *arr = res->GetMatrixArray();
1286+
Double_t *arr = new Double_t[fNRows];
12501287

12511288
for (Int_t row = 0; row < fNRows; row++) {
12521289
arr[row] = fCells[offset + row].fRealNumber;
12531290
}
12541291

1292+
TVectorD *res = new TVectorD();
1293+
res->Use(fNRows, arr);
1294+
12551295
return res;
12561296
}
12571297

‎tutorials/fitsio/FITS_tutorial7.C

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/// \file
2+
/// \ingroup tutorial_FITS
3+
/// Open a FITS file that contains a catalog of astronomical objects
4+
/// and dump some of its columns
5+
///
6+
/// \macro_code
7+
///
8+
/// \author Elizabeth Buckley-Geer
9+
10+
void FITS_tutorial7()
11+
{
12+
13+
printf("\n\n--------------------------------\n");
14+
printf("WELCOME TO FITS tutorial #7 !!!!\n");
15+
printf("--------------------------------\n");
16+
printf("We are going to open a table from a FITS file\n");
17+
printf("and print out three columns for some of the objects.\n");
18+
printf("This table contains a logical data type so this tutorial tests\n");
19+
printf("that we can read it correctly\n\n");
20+
21+
TString dir = gSystem->DirName(__FILE__);
22+
23+
// Open the table
24+
TFITSHDU *hdu = new TFITSHDU(dir + "/sample5.fits[1]");
25+
if (hdu == 0) {
26+
printf("ERROR: could not access the HDU\n");
27+
return;
28+
}
29+
30+
TVectorD *vec1;
31+
TVectorD *vec2;
32+
TVectorD *vec3;
33+
TVectorD *vec4;
34+
35+
// Read the ra, dec, flux_g and brick_primary columns
36+
37+
vec1 = hdu->GetTabRealVectorColumn("ra");
38+
vec2 = hdu->GetTabRealVectorColumn("dec");
39+
vec3 = hdu->GetTabRealVectorColumn("flux_g");
40+
vec4 = hdu->GetTabRealVectorColumn("brick_primary");
41+
42+
Double_t gflux, ra, dec, bp;
43+
44+
for (Int_t i = vec1->GetLwb(); i <= vec1->GetUpb(); i++) {
45+
46+
bp = (*vec4)[i];
47+
gflux = (*vec3)[i];
48+
ra = (*vec1)[i];
49+
dec = (*vec2)[i];
50+
51+
if (bp) {
52+
printf("RA %f DEC %f G-FLUX %f\n", ra, dec, gflux);
53+
}
54+
}
55+
56+
// Clean up
57+
58+
delete vec1;
59+
delete vec2;
60+
delete vec3;
61+
delete vec4;
62+
delete hdu;
63+
}

‎tutorials/fitsio/sample5.fits

+421
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.