-
Notifications
You must be signed in to change notification settings - Fork 1
/
getcols.c
992 lines (860 loc) · 33.1 KB
/
getcols.c
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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
/* This file, getcols.c, contains routines that read data elements from */
/* a FITS image or table, with a character string datatype. */
/* The FITSIO software was written by William Pence at the High Energy */
/* Astrophysic Science Archive Research Center (HEASARC) at the NASA */
/* Goddard Space Flight Center. */
#include <stdlib.h>
#include <string.h>
/* stddef.h is apparently needed to define size_t */
#include <stddef.h>
#include <ctype.h>
#include "fitsio2.h"
/*--------------------------------------------------------------------------*/
int ffgcvs( fitsfile *fptr, /* I - FITS file pointer */
int colnum, /* I - number of column to read (1 = 1st col) */
LONGLONG firstrow, /* I - first row to read (1 = 1st row) */
LONGLONG firstelem, /* I - first vector element to read (1 = 1st) */
LONGLONG nelem, /* I - number of strings to read */
char *nulval, /* I - string for null pixels */
char **array, /* O - array of values that are read */
int *anynul, /* O - set to 1 if any values are null; else 0 */
int *status) /* IO - error status */
/*
Read an array of string values from a column in the current FITS HDU.
Any undefined pixels will be set equal to the value of 'nulval' unless
nulval = null in which case no checks for undefined pixels will be made.
*/
{
char cdummy[2];
ffgcls(fptr, colnum, firstrow, firstelem, nelem, 1, nulval,
array, cdummy, anynul, status);
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffgcfs( fitsfile *fptr, /* I - FITS file pointer */
int colnum, /* I - number of column to read (1 = 1st col) */
LONGLONG firstrow, /* I - first row to read (1 = 1st row) */
LONGLONG firstelem, /* I - first vector element to read (1 = 1st) */
LONGLONG nelem, /* I - number of strings to read */
char **array, /* O - array of values that are read */
char *nularray, /* O - array of flags = 1 if nultyp = 2 */
int *anynul, /* O - set to 1 if any values are null; else 0 */
int *status) /* IO - error status */
/*
Read an array of string values from a column in the current FITS HDU.
Nularray will be set = 1 if the corresponding array pixel is undefined,
otherwise nularray will = 0.
*/
{
char dummy[2];
ffgcls(fptr, colnum, firstrow, firstelem, nelem, 2, dummy,
array, nularray, anynul, status);
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffgcls( fitsfile *fptr, /* I - FITS file pointer */
int colnum, /* I - number of column to read (1 = 1st col) */
LONGLONG firstrow, /* I - first row to read (1 = 1st row) */
LONGLONG firstelem, /* I - first vector element to read (1 = 1st) */
LONGLONG nelem, /* I - number of strings to read */
int nultyp, /* I - null value handling code: */
/* 1: set undefined pixels = nulval */
/* 2: set nularray=1 for undefined pixels */
char *nulval, /* I - value for null pixels if nultyp = 1 */
char **array, /* O - array of values that are read */
char *nularray, /* O - array of flags = 1 if nultyp = 2 */
int *anynul, /* O - set to 1 if any values are null; else 0 */
int *status) /* IO - error status */
/*
Read an array of string values from a column in the current FITS HDU.
Returns a formated string value, regardless of the datatype of the column
*/
{
int tcode, hdutype, tstatus, scaled, intcol, dwidth, nulwidth, ll, dlen;
int equivtype;
long ii, jj;
tcolumn *colptr;
char message[FLEN_ERRMSG], *carray, keyname[FLEN_KEYWORD];
char cform[20], dispfmt[20], tmpstr[400], *flgarray, tmpnull[80];
unsigned char byteval;
float *earray;
double *darray, tscale = 1.0;
LONGLONG *llarray;
ULONGLONG *ullarray;
if (*status > 0 || nelem == 0) /* inherit input status value if > 0 */
return(*status);
/* reset position to the correct HDU if necessary */
if (fptr->HDUposition != (fptr->Fptr)->curhdu)
ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);
/* rescan header if data structure is undefined */
else if ((fptr->Fptr)->datastart == DATA_UNDEFINED)
if ( ffrdef(fptr, status) > 0)
return(*status);
if (colnum < 1 || colnum > (fptr->Fptr)->tfield)
{
snprintf(message, FLEN_ERRMSG,"Specified column number is out of range: %d",
colnum);
ffpmsg(message);
return(*status = BAD_COL_NUM);
}
/* get equivalent dataype of column (only needed for TLONGLONG columns) */
ffeqtyll(fptr, colnum, &equivtype, NULL, NULL, status);
if (equivtype < 0) equivtype = abs(equivtype);
colptr = (fptr->Fptr)->tableptr; /* point to first column */
colptr += (colnum - 1); /* offset to correct column structure */
tcode = abs(colptr->tdatatype);
intcol = 0;
if (tcode == TSTRING)
{
/* simply call the string column reading routine */
ffgcls2(fptr, colnum, firstrow, firstelem, nelem, nultyp, nulval,
array, nularray, anynul, status);
}
else if (tcode == TLOGICAL)
{
/* allocate memory for the array of logical values */
carray = (char *) malloc((size_t) nelem);
/* call the logical column reading routine */
ffgcll(fptr, colnum, firstrow, firstelem, nelem, nultyp, *nulval,
carray, nularray, anynul, status);
if (*status <= 0)
{
/* convert logical values to "T", "F", or "N" (Null) */
for (ii = 0; ii < nelem; ii++)
{
if (carray[ii] == 1)
strcpy(array[ii], "T");
else if (carray[ii] == 0)
strcpy(array[ii], "F");
else /* undefined values = 2 */
strcpy(array[ii],"N");
}
}
free(carray); /* free the memory */
}
else if (tcode == TCOMPLEX)
{
/* allocate memory for the array of double values */
earray = (float *) calloc((size_t) (nelem * 2), sizeof(float) );
ffgcle(fptr, colnum, firstrow, (firstelem - 1) * 2 + 1, nelem * 2,
1, 1, FLOATNULLVALUE, earray, nularray, anynul, status);
if (*status <= 0)
{
/* determine the format for the output strings */
ffgcdw(fptr, colnum, &dwidth, status);
dwidth = (dwidth - 3) / 2;
/* use the TDISPn keyword if it exists */
ffkeyn("TDISP", colnum, keyname, status);
tstatus = 0;
cform[0] = '\0';
if (ffgkys(fptr, keyname, dispfmt, NULL, &tstatus) == 0)
{
/* convert the Fortran style format to a C style format */
ffcdsp(dispfmt, cform);
/* Special case: TDISPn='Aw' disallowed for numeric types */
if (dispfmt[0] == 'A') {
cform[0] = 0;
/* Special case: if the output is intended to be represented
as an integer, but we read it as a double, we need to
set intcol = 1 so it is printed as an integer */
} else if ((dispfmt[0] == 'I') || (dispfmt[0] == 'i') ||
(dispfmt[0] == 'O') || (dispfmt[0] == 'o') ||
(dispfmt[0] == 'Z') || (dispfmt[0] == 'z')) {
intcol = 1;
}
}
if (!cform[0])
strcpy(cform, "%14.6E");
/* write the formated string for each value: "(real,imag)" */
jj = 0;
for (ii = 0; ii < nelem; ii++)
{
strcpy(array[ii], "(");
/* test for null value */
if (earray[jj] == FLOATNULLVALUE)
{
strcpy(tmpstr, "NULL");
if (nultyp == 2)
nularray[ii] = 1;
}
else if (intcol)
{
snprintf(tmpstr, 400,cform, (int) earray[jj]);
}
else
{
snprintf(tmpstr, 400,cform, earray[jj]);
}
strncat(array[ii], tmpstr, dwidth);
strcat(array[ii], ",");
jj++;
/* test for null value */
if (earray[jj] == FLOATNULLVALUE)
{
strcpy(tmpstr, "NULL");
if (nultyp == 2)
nularray[ii] = 1;
}
else if (intcol)
{
snprintf(tmpstr, 400,cform, (int) earray[jj]);
}
else
{
snprintf(tmpstr, 400,cform, earray[jj]);
}
strncat(array[ii], tmpstr, dwidth);
strcat(array[ii], ")");
jj++;
}
}
free(earray); /* free the memory */
}
else if (tcode == TDBLCOMPLEX)
{
/* allocate memory for the array of double values */
darray = (double *) calloc((size_t) (nelem * 2), sizeof(double) );
ffgcld(fptr, colnum, firstrow, (firstelem - 1) * 2 + 1, nelem * 2,
1, 1, DOUBLENULLVALUE, darray, nularray, anynul, status);
if (*status <= 0)
{
/* determine the format for the output strings */
ffgcdw(fptr, colnum, &dwidth, status);
dwidth = (dwidth - 3) / 2;
/* use the TDISPn keyword if it exists */
ffkeyn("TDISP", colnum, keyname, status);
tstatus = 0;
cform[0] = '\0';
if (ffgkys(fptr, keyname, dispfmt, NULL, &tstatus) == 0)
{
/* convert the Fortran style format to a C style format */
ffcdsp(dispfmt, cform);
/* Special case: TDISPn='Aw' disallowed for numeric types */
if (dispfmt[0] == 'A') {
cform[0] = 0;
/* Special case: if the output is intended to be represented
as an integer, but we read it as a double, we need to
set intcol = 1 so it is printed as an integer */
} else if ((dispfmt[0] == 'I') || (dispfmt[0] == 'i') ||
(dispfmt[0] == 'O') || (dispfmt[0] == 'o') ||
(dispfmt[0] == 'Z') || (dispfmt[0] == 'z')) {
intcol = 1;
}
}
if (!cform[0])
strcpy(cform, "%23.15E");
/* write the formated string for each value: "(real,imag)" */
jj = 0;
for (ii = 0; ii < nelem; ii++)
{
strcpy(array[ii], "(");
/* test for null value */
if (darray[jj] == DOUBLENULLVALUE)
{
strcpy(tmpstr, "NULL");
if (nultyp == 2)
nularray[ii] = 1;
}
else if (intcol)
{
snprintf(tmpstr, 400,cform, (int) darray[jj]);
}
else
{
snprintf(tmpstr, 400,cform, darray[jj]);
}
strncat(array[ii], tmpstr, dwidth);
strcat(array[ii], ",");
jj++;
/* test for null value */
if (darray[jj] == DOUBLENULLVALUE)
{
strcpy(tmpstr, "NULL");
if (nultyp == 2)
nularray[ii] = 1;
}
else if (intcol)
{
snprintf(tmpstr, 400,cform, (int) darray[jj]);
}
else
{
snprintf(tmpstr, 400,cform, darray[jj]);
}
strncat(array[ii], tmpstr, dwidth);
strcat(array[ii], ")");
jj++;
}
}
free(darray); /* free the memory */
}
else if (tcode == TLONGLONG && equivtype == TLONGLONG)
{
/* allocate memory for the array of LONGLONG values */
llarray = (LONGLONG *) calloc((size_t) nelem, sizeof(LONGLONG) );
flgarray = (char *) calloc((size_t) nelem, sizeof(char) );
dwidth = 20; /* max width of displayed long long integer value */
if (ffgcfjj(fptr, colnum, firstrow, firstelem, nelem,
llarray, flgarray, anynul, status) > 0)
{
free(flgarray);
free(llarray);
return(*status);
}
/* write the formated string for each value */
if (nulval) {
strncpy(tmpnull, nulval,79);
tmpnull[79]='\0'; /* In case len(nulval) >= 79 */
nulwidth = strlen(tmpnull);
} else {
strcpy(tmpnull, " ");
nulwidth = 1;
}
for (ii = 0; ii < nelem; ii++)
{
if ( flgarray[ii] )
{
*array[ii] = '\0';
if (dwidth < nulwidth)
strncat(array[ii], tmpnull, dwidth);
else
sprintf(array[ii],"%*s",dwidth,tmpnull);
if (nultyp == 2)
nularray[ii] = 1;
}
else
{
#if defined(_MSC_VER)
/* Microsoft Visual C++ 6.0 uses '%I64d' syntax for 8-byte integers */
snprintf(tmpstr, 400,"%20I64d", llarray[ii]);
#elif (USE_LL_SUFFIX == 1)
snprintf(tmpstr, 400,"%20lld", llarray[ii]);
#else
snprintf(tmpstr, 400,"%20ld", llarray[ii]);
#endif
*array[ii] = '\0';
strncat(array[ii], tmpstr, 20);
}
}
free(flgarray);
free(llarray); /* free the memory */
}
else if (tcode == TLONGLONG && equivtype == TULONGLONG)
{
/* allocate memory for the array of ULONGLONG values */
ullarray = (ULONGLONG *) calloc((size_t) nelem, sizeof(ULONGLONG) );
flgarray = (char *) calloc((size_t) nelem, sizeof(char) );
dwidth = 20; /* max width of displayed unsigned long long integer value */
if (ffgcfujj(fptr, colnum, firstrow, firstelem, nelem,
ullarray, flgarray, anynul, status) > 0)
{
free(flgarray);
free(ullarray);
return(*status);
}
/* write the formated string for each value */
if (nulval) {
strncpy(tmpnull, nulval, 79);
tmpnull[79]='\0'; /* In case len(nulval) >= 79 */
nulwidth = strlen(tmpnull);
} else {
strcpy(tmpnull, " ");
nulwidth = 1;
}
for (ii = 0; ii < nelem; ii++)
{
if ( flgarray[ii] )
{
*array[ii] = '\0';
if (dwidth < nulwidth)
strncat(array[ii], tmpnull, dwidth);
else
sprintf(array[ii],"%*s",dwidth,tmpnull);
if (nultyp == 2)
nularray[ii] = 1;
}
else
{
#if defined(_MSC_VER)
/* Microsoft Visual C++ 6.0 uses '%I64d' syntax for 8-byte integers */
snprintf(tmpstr, 400, "%20I64u", ullarray[ii]);
#elif (USE_LL_SUFFIX == 1)
snprintf(tmpstr, 400, "%20llu", ullarray[ii]);
#else
snprintf(tmpstr, 400, "%20lu", ullarray[ii]);
#endif
*array[ii] = '\0';
strncat(array[ii], tmpstr, 20);
}
}
free(flgarray);
free(ullarray); /* free the memory */
}
else
{
/* allocate memory for the array of double values */
darray = (double *) calloc((size_t) nelem, sizeof(double) );
/* read all other numeric type columns as doubles */
if (ffgcld(fptr, colnum, firstrow, firstelem, nelem, 1, nultyp,
DOUBLENULLVALUE, darray, nularray, anynul, status) > 0)
{
free(darray);
return(*status);
}
/* determine the format for the output strings */
ffgcdw(fptr, colnum, &dwidth, status);
/* check if column is scaled */
ffkeyn("TSCAL", colnum, keyname, status);
tstatus = 0;
scaled = 0;
if (ffgkyd(fptr, keyname, &tscale, NULL, &tstatus) == 0)
{
if (tscale != 1.0)
scaled = 1; /* yes, this is a scaled column */
}
intcol = 0;
if (tcode <= TLONG && !scaled)
intcol = 1; /* this is an unscaled integer column */
/* use the TDISPn keyword if it exists */
ffkeyn("TDISP", colnum, keyname, status);
tstatus = 0;
cform[0] = '\0';
if (ffgkys(fptr, keyname, dispfmt, NULL, &tstatus) == 0)
{
/* convert the Fortran style TDISPn to a C style format */
ffcdsp(dispfmt, cform);
/* Special case: TDISPn='Aw' disallowed for numeric types */
if (dispfmt[0] == 'A') {
cform[0] = 0;
/* Special case: if the output is intended to be represented
as an integer, but we read it as a double, we need to
set intcol = 1 so it is printed as an integer */
} else if ((dispfmt[0] == 'I') || (dispfmt[0] == 'i') ||
(dispfmt[0] == 'O') || (dispfmt[0] == 'o') ||
(dispfmt[0] == 'Z') || (dispfmt[0] == 'z')) {
intcol = 1;
}
}
if (!cform[0])
{
/* no TDISPn keyword; use TFORMn instead */
ffkeyn("TFORM", colnum, keyname, status);
ffgkys(fptr, keyname, dispfmt, NULL, status);
if (scaled && tcode <= TSHORT)
{
/* scaled short integer column == float */
strcpy(cform, "%#14.6G");
}
else if (scaled && tcode == TLONG)
{
/* scaled long integer column == double */
strcpy(cform, "%#23.15G");
}
else if (scaled && tcode == TLONGLONG)
{
/* scaled long long integer column == double */
strcpy(cform, "%#23.15G");
}
else
{
ffghdt(fptr, &hdutype, status);
if (hdutype == ASCII_TBL)
{
/* convert the Fortran style TFORMn to a C style format */
ffcdsp(dispfmt, cform);
}
else
{
/* this is a binary table, need to convert the format */
if (tcode == TBIT) { /* 'X' */
strcpy(cform, "%4d");
} else if (tcode == TBYTE) { /* 'B' */
strcpy(cform, "%4d");
} else if (tcode == TSHORT) { /* 'I' */
strcpy(cform, "%6d");
} else if (tcode == TLONG) { /* 'J' */
strcpy(cform, "%11.0f");
intcol = 0; /* needed to support unsigned int */
} else if (tcode == TFLOAT) { /* 'E' */
strcpy(cform, "%#14.6G");
} else if (tcode == TDOUBLE) { /* 'D' */
strcpy(cform, "%#23.15G");
}
}
}
}
if (nulval) {
strncpy(tmpnull, nulval,79);
tmpnull[79]='\0';
nulwidth = strlen(tmpnull);
} else {
strcpy(tmpnull, " ");
nulwidth = 1;
}
/* write the formated string for each value */
for (ii = 0; ii < nelem; ii++)
{
if (tcode == TBIT)
{
byteval = (unsigned char) darray[ii];
for (ll=0; ll < 8; ll++)
{
if ( ((unsigned char) (byteval << ll)) >> 7 )
*(array[ii] + ll) = '1';
else
*(array[ii] + ll) = '0';
}
*(array[ii] + 8) = '\0';
}
/* test for null value */
else if ( (nultyp == 1 && darray[ii] == DOUBLENULLVALUE) ||
(nultyp == 2 && nularray[ii]) )
{
*array[ii] = '\0';
if (dwidth < nulwidth)
strncat(array[ii], tmpnull, dwidth);
else
sprintf(array[ii],"%*s",dwidth,tmpnull);
}
else
{
if (intcol) {
snprintf(tmpstr, 400,cform, (int) darray[ii]);
} else {
snprintf(tmpstr, 400,cform, darray[ii]);
}
/* fill field with '*' if number is too wide */
dlen = strlen(tmpstr);
if (dlen > dwidth) {
memset(tmpstr, '*', dwidth);
}
*array[ii] = '\0';
strncat(array[ii], tmpstr, dwidth);
}
}
free(darray); /* free the memory */
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffgcdw( fitsfile *fptr, /* I - FITS file pointer */
int colnum, /* I - number of column (1 = 1st col) */
int *width, /* O - display width */
int *status) /* IO - error status */
/*
Get Column Display Width.
*/
{
tcolumn *colptr;
char *cptr;
char message[FLEN_ERRMSG], keyname[FLEN_KEYWORD], dispfmt[20];
int tcode, hdutype, tstatus, scaled;
double tscale;
if (*status > 0) /* inherit input status value if > 0 */
return(*status);
if (fptr->HDUposition != (fptr->Fptr)->curhdu)
ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);
if (colnum < 1 || colnum > (fptr->Fptr)->tfield)
{
snprintf(message, FLEN_ERRMSG,"Specified column number is out of range: %d",
colnum);
ffpmsg(message);
return(*status = BAD_COL_NUM);
}
colptr = (fptr->Fptr)->tableptr; /* point to first column */
colptr += (colnum - 1); /* offset to correct column structure */
tcode = abs(colptr->tdatatype);
/* use the TDISPn keyword if it exists */
ffkeyn("TDISP", colnum, keyname, status);
*width = 0;
tstatus = 0;
if (ffgkys(fptr, keyname, dispfmt, NULL, &tstatus) == 0)
{
/* parse TDISPn get the display width */
cptr = dispfmt;
while(*cptr == ' ') /* skip leading blanks */
cptr++;
if (*cptr == 'A' || *cptr == 'a' ||
*cptr == 'I' || *cptr == 'i' ||
*cptr == 'O' || *cptr == 'o' ||
*cptr == 'Z' || *cptr == 'z' ||
*cptr == 'F' || *cptr == 'f' ||
*cptr == 'E' || *cptr == 'e' ||
*cptr == 'D' || *cptr == 'd' ||
*cptr == 'G' || *cptr == 'g')
{
while(!isdigit((int) *cptr) && *cptr != '\0') /* find 1st digit */
cptr++;
*width = atoi(cptr);
if (tcode >= TCOMPLEX)
*width = (2 * (*width)) + 3;
}
}
if (*width == 0)
{
/* no valid TDISPn keyword; use TFORMn instead */
ffkeyn("TFORM", colnum, keyname, status);
ffgkys(fptr, keyname, dispfmt, NULL, status);
/* check if column is scaled */
ffkeyn("TSCAL", colnum, keyname, status);
tstatus = 0;
scaled = 0;
if (ffgkyd(fptr, keyname, &tscale, NULL, &tstatus) == 0)
{
if (tscale != 1.0)
scaled = 1; /* yes, this is a scaled column */
}
if (scaled && tcode <= TSHORT)
{
/* scaled short integer col == float; default format is 14.6G */
*width = 14;
}
else if (scaled && tcode == TLONG)
{
/* scaled long integer col == double; default format is 23.15G */
*width = 23;
}
else if (scaled && tcode == TLONGLONG)
{
/* scaled long long integer col == double; default format is 23.15G */
*width = 23;
}
else
{
ffghdt(fptr, &hdutype, status); /* get type of table */
if (hdutype == ASCII_TBL)
{
/* parse TFORMn get the display width */
cptr = dispfmt;
while(!isdigit((int) *cptr) && *cptr != '\0') /* find 1st digit */
cptr++;
*width = atoi(cptr);
}
else
{
/* this is a binary table */
if (tcode == TBIT) /* 'X' */
*width = 8;
else if (tcode == TBYTE) /* 'B' */
*width = 4;
else if (tcode == TSHORT) /* 'I' */
*width = 6;
else if (tcode == TLONG) /* 'J' */
*width = 11;
else if (tcode == TLONGLONG) /* 'K' */
*width = 20;
else if (tcode == TFLOAT) /* 'E' */
*width = 14;
else if (tcode == TDOUBLE) /* 'D' */
*width = 23;
else if (tcode == TCOMPLEX) /* 'C' */
*width = 31;
else if (tcode == TDBLCOMPLEX) /* 'M' */
*width = 49;
else if (tcode == TLOGICAL) /* 'L' */
*width = 1;
else if (tcode == TSTRING) /* 'A' */
{
int typecode;
long int repeat = 0, rwidth = 0;
int gstatus = 0;
/* Deal with possible vector string with repeat / width by parsing
the TFORM=rAw keyword */
if (ffgtcl(fptr, colnum, &typecode, &repeat, &rwidth, &gstatus) == 0 &&
rwidth >= 1 && rwidth < repeat) {
*width = rwidth;
} else {
/* Hmmm, we couldn't parse the TFORM keyword by standard, so just do
simple parsing */
cptr = dispfmt;
while(!isdigit((int) *cptr) && *cptr != '\0')
cptr++;
*width = atoi(cptr);
}
if (*width < 1)
*width = 1; /* default is at least 1 column */
}
}
}
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffgcls2 ( fitsfile *fptr, /* I - FITS file pointer */
int colnum, /* I - number of column to read (1 = 1st col) */
LONGLONG firstrow, /* I - first row to read (1 = 1st row) */
LONGLONG firstelem, /* I - first vector element to read (1 = 1st) */
LONGLONG nelem, /* I - number of strings to read */
int nultyp, /* I - null value handling code: */
/* 1: set undefined pixels = nulval */
/* 2: set nularray=1 for undefined pixels */
char *nulval, /* I - value for null pixels if nultyp = 1 */
char **array, /* O - array of values that are read */
char *nularray, /* O - array of flags = 1 if nultyp = 2 */
int *anynul, /* O - set to 1 if any values are null; else 0 */
int *status) /* IO - error status */
/*
Read an array of string values from a column in the current FITS HDU.
*/
{
double dtemp;
long nullen;
int tcode, maxelem, hdutype, nulcheck;
long twidth, incre;
long ii, jj, ntodo;
LONGLONG repeat, startpos, elemnum, readptr, tnull, rowlen, rownum, remain, next;
double scale, zero;
char tform[20];
char message[FLEN_ERRMSG];
char snull[20]; /* the FITS null value */
tcolumn *colptr;
double cbuff[DBUFFSIZE / sizeof(double)]; /* align cbuff on word boundary */
char *buffer, *arrayptr;
if (*status > 0 || nelem == 0) /* inherit input status value if > 0 */
return(*status);
if (fptr->HDUposition != (fptr->Fptr)->curhdu)
ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);
if (anynul)
*anynul = 0;
if (nultyp == 2)
memset(nularray, 0, (size_t) nelem); /* initialize nullarray */
/*---------------------------------------------------*/
/* Check input and get parameters about the column: */
/*---------------------------------------------------*/
if (colnum < 1 || colnum > (fptr->Fptr)->tfield)
{
snprintf(message, FLEN_ERRMSG,"Specified column number is out of range: %d",
colnum);
ffpmsg(message);
return(*status = BAD_COL_NUM);
}
colptr = (fptr->Fptr)->tableptr; /* point to first column */
colptr += (colnum - 1); /* offset to correct column structure */
tcode = colptr->tdatatype;
if (tcode == -TSTRING) /* variable length column in a binary table? */
{
/* only read a single string; ignore value of firstelem */
if (ffgcprll( fptr, colnum, firstrow, 1, 1, 0, &scale, &zero,
tform, &twidth, &tcode, &maxelem, &startpos, &elemnum, &incre,
&repeat, &rowlen, &hdutype, &tnull, snull, status) > 0)
return(*status);
remain = 1;
twidth = (long) repeat;
}
else if (tcode == TSTRING)
{
if (ffgcprll( fptr, colnum, firstrow, firstelem, nelem, 0, &scale, &zero,
tform, &twidth, &tcode, &maxelem, &startpos, &elemnum, &incre,
&repeat, &rowlen, &hdutype, &tnull, snull, status) > 0)
return(*status);
/* if string length is greater than a FITS block (2880 char) then must */
/* only read 1 string at a time, to force reading by ffgbyt instead of */
/* ffgbytoff (ffgbytoff can't handle this case) */
if (twidth > IOBUFLEN) {
maxelem = 1;
incre = twidth;
repeat = 1;
}
remain = nelem;
}
else
return(*status = NOT_ASCII_COL);
nullen = strlen(snull); /* length of the undefined pixel string */
if (nullen == 0)
nullen = 1;
/*------------------------------------------------------------------*/
/* Decide whether to check for null values in the input FITS file: */
/*------------------------------------------------------------------*/
nulcheck = nultyp; /* by default check for null values in the FITS file */
if (nultyp == 1 && nulval == 0)
nulcheck = 0; /* calling routine does not want to check for nulls */
else if (nultyp == 1 && nulval && nulval[0] == 0)
nulcheck = 0; /* calling routine does not want to check for nulls */
else if (snull[0] == ASCII_NULL_UNDEFINED)
nulcheck = 0; /* null value string in ASCII table not defined */
else if (nullen > twidth)
nulcheck = 0; /* null value string is longer than width of column */
/* thus impossible for any column elements to = null */
/*---------------------------------------------------------------------*/
/* Now read the strings one at a time from the FITS column. */
/*---------------------------------------------------------------------*/
next = 0; /* next element in array to be read */
rownum = 0; /* row number, relative to firstrow */
while (remain)
{
/* limit the number of pixels to process at one time to the number that
will fit in the buffer space or to the number of pixels that remain
in the current vector, which ever is smaller.
*/
ntodo = (long) minvalue(remain, maxelem);
ntodo = (long) minvalue(ntodo, (repeat - elemnum));
readptr = startpos + ((LONGLONG)rownum * rowlen) + (elemnum * incre);
ffmbyt(fptr, readptr, REPORT_EOF, status); /* move to read position */
/* read the array of strings from the FITS file into the buffer */
if (incre == twidth)
ffgbyt(fptr, ntodo * twidth, cbuff, status);
else
ffgbytoff(fptr, twidth, ntodo, incre - twidth, cbuff, status);
/* copy from the buffer into the user's array of strings */
/* work backwards from last char of last string to 1st char of 1st */
buffer = ((char *) cbuff) + (ntodo * twidth) - 1;
for (ii = (long) (next + ntodo - 1); ii >= next; ii--)
{
arrayptr = array[ii] + twidth - 1;
for (jj = twidth - 1; jj > 0; jj--) /* ignore trailing blanks */
{
if (*buffer == ' ')
{
buffer--;
arrayptr--;
}
else
break;
}
*(arrayptr + 1) = 0; /* write the string terminator */
for (; jj >= 0; jj--) /* copy the string itself */
{
*arrayptr = *buffer;
buffer--;
arrayptr--;
}
/* check if null value is defined, and if the */
/* column string is identical to the null string */
if (nulcheck && !strncmp(snull, array[ii], nullen) )
{
*anynul = 1; /* this is a null value */
if (nultyp == 1) {
if (nulval)
strcpy(array[ii], nulval);
else
strcpy(array[ii], " ");
} else
nularray[ii] = 1;
}
}
if (*status > 0) /* test for error during previous read operation */
{
dtemp = (double) next;
snprintf(message,FLEN_ERRMSG,
"Error reading elements %.0f thru %.0f of data array (ffpcls).",
dtemp+1., dtemp+ntodo);
ffpmsg(message);
return(*status);
}
/*--------------------------------------------*/
/* increment the counters for the next loop */
/*--------------------------------------------*/
next += ntodo;
remain -= ntodo;
if (remain)
{
elemnum += ntodo;
if (elemnum == repeat) /* completed a row; start on next row */
{
elemnum = 0;
rownum++;
}
}
} /* End of main while Loop */
return(*status);
}