forked from dstndstn/astrometry.net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfvrf_head.c
3079 lines (2707 loc) · 90.6 KB
/
fvrf_head.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
992
993
994
995
996
997
998
999
1000
/*
This file is part of "fitsverify" and was imported from:
http://heasarc.gsfc.nasa.gov/docs/software/ftools/fitsverify/
*/
#include "fverify.h"
/*
the following are only needed if one calls wcslib
#include <wcslib/wcshdr.h>
#include <wcslib/wcsfix.h>
#include <wcslib/wcs.h>
#include <wcslib/getwcstab.h>
*/
static char **cards; /* array to store the keywords */
static int ncards; /* total number of the keywords */
static char **tmpkwds; /* An string array holding the keyword name.
It is sorted in alphabetical ascending order
and not includes the keywords before
the first non-reserved keyword and END
keyword. */
static char **ttype;
static char **tform;
static char **tunit;
static char temp[80];
static char *ptemp; /* it always pointed to the address of
temp */
static char snull[] = "";
static int curhdu; /* current HDU index */
static int curtype; /* current HDU type */
/******************************************************************************
* Function
* verify_fits
*
* DESCRIPTION:
* Verify individual fits file.
*
*******************************************************************************/
/* routine to verify individual fitsfile */
void verify_fits(char *infile, FILE *out)
{
char rootnam[FLEN_FILENAME] = ""; /* Input Fits file root name */
fitsfile *infits; /* input fits file pointer */
FitsHdu fitshdu; /* hdu information */
int hdutype;
int status = 0;
int i;
int len;
char *p;
char *pfile;
char xtension[80];
/* take out the leading and trailing space and skip the empty line*/
p = infile;
while(isspace((int)*p) )p++;
len = strlen(p);
pfile = p;
p += (len -1);
for (i = len - 1; i >= 0 && isspace((int)*p); i--) {*p = '\0'; p--;}
if(!strlen(pfile)) return;
#ifndef WEBTOOL
wrtout(out," ");
sprintf(comm,"File: %s",pfile);
wrtout(out,comm);
#endif
totalhdu = 0;
/* discard the extension, rowfilter... */
if(ffrtnm(pfile, rootnam, &status)) {
wrtserr(out,"",&status,2);
leave_early(out);
return;
}
if(fits_open_file(&infits, rootnam, READONLY, &status)) {
wrtserr(out,"",&status,2);
leave_early(out);
return;
}
/* get the total hdus */
if(fits_get_num_hdus(infits, &totalhdu, &status)) {
wrtserr(out,"",&status,2);
leave_early(out);
return;
}
/* initialize the report */
init_report(out,rootnam);
/*------------------ Hdu Loop --------------------------------*/
for (i = 1; i <= totalhdu; i++) {
/* move to the right hdu and do the CFITSIO test */
hdutype = -1;
if(fits_movabs_hdu(infits,i, &hdutype, &status) ) {
print_title(out,i, hdutype);
wrtferr(out,"",&status,2);
set_hdubasic(i,hdutype);
break;
}
if (i != 1 && hdutype == IMAGE_HDU) {
/* test if this is a tile compressed image in a binary table */
fits_read_key(infits, TSTRING, "XTENSION", xtension, NULL, &status);
if (!strcmp(xtension, "BINTABLE") )
print_title(out,i, BINARY_TBL);
else
print_title(out,i, hdutype);
}
else
print_title(out,i, hdutype);
init_hdu(infits,out,i,hdutype,
&fitshdu); /* initialize fitshdu */
test_hdu(infits,out,&fitshdu); /* test hdu header */
if(testdata)
test_data(infits,out,&fitshdu);
close_err(out); /* end of error report */
if(prhead)
print_header(out);
if(prstat)
print_summary(infits,out,&fitshdu);
close_hdu(&fitshdu); /* clear the fitshdu */
}
/* test the end of file */
test_end(infits,out);
/*------------------ Closing --------------------------------*/
/* closing the report*/
close_report(out);
/* close the input fitsfile */
fits_close_file(infits, &status);
}
void leave_early (FILE* out)
{
sprintf(comm,"**** Abort Verification: Fatal Error. ****");
wrtout(out,comm);
/* write the total number of errors and warnings to parfile*/
update_parfile(1,0);
}
void close_err(FILE* out)
{
int merr, mwrn;
num_err_wrn(&merr, &mwrn);
if(merr || mwrn ) wrtout(out," ");
return;
}
/*************************************************************
*
* init_hdu
*
* Initialize the FitsHdu, HduName and ttype, tform, tunit if
* the hdu is a table.
*
*
*************************************************************/
void init_hdu(fitsfile *infits, /* input fits file */
FILE* out, /* output ascii file */
int hdunum, /* hdu index */
int hdutype, /* hdutype */
FitsHdu *hduptr
)
{
int morekeys;
int i,j,k,m,n;
int status = 0;
FitsKey ** kwds;
char *p = 0;
int numusrkey;
LONGLONG lv,lu=0L;
FitsKey tmpkey;
hduptr->hdunum = hdunum;
hduptr->hdutype = hdutype;
/* curhdu and curtype are shared with print_title */
curhdu = hdunum; /* set the current hdu number */
curtype = hdutype; /* set the current hdu number */
/* check the null character in the header.(only the first one will
be recorded */
lv = 0;
lv = fits_null_check(infits, &status);
if (lv > 0) {
m = (lv - 1)/80 + 1;
n = lv - (m - 1) * 80;
sprintf(errmes,
"Byte #%d in Card#%d is a null(\\0).",n,m);
wrterr(out,errmes,1);
status = 0;
} else {
if (status) {
wrtserr(out,"",&status,1);
status = 0;
}
}
/* get the total number of keywords */
hduptr->nkeys = 0;
morekeys = 0;
if(fits_get_hdrspace(infits, &(hduptr->nkeys), &morekeys, &status))
wrtferr(out,"",&status,1);
(hduptr->nkeys)++; /* include END keyword */
/* read all the keywords */
ncards = hduptr->nkeys;
cards = (char **)malloc(sizeof(char *) * ncards );
for (i=0; i < ncards; i++) {
cards[i] = (char *)malloc(sizeof(char )* FLEN_CARD );
}
for (i=1; i <= ncards; i++) {
if(fits_read_record(infits, i, cards[i-1], &status))
wrtferr(out,"",&status,1);
}
/* Parse the XTENSION/SIMPLEX keyword */
fits_parse_card(out, 1, cards[0], tmpkey.kname,
&(tmpkey.ktype), tmpkey.kvalue,comm);
if( *(tmpkey.kvalue) == ' ') {
sprintf(errmes,
"Keyword #1, %s \"%s\" should not have leading space.",
tmpkey.kname,tmpkey.kvalue);
wrterr(out,errmes,1);
}
if(hdunum == 1) { /* SIMPLE should be logical T */
if(strcmp(tmpkey.kname,"SIMPLE"))
wrterr(out, "The 1st keyword of a primary array is not SIMPLE.",1);
if( !check_log(&tmpkey,out)|| strcmp(tmpkey.kvalue,"T"))
wrtwrn(out,
"SIMPLE != T indicates file may not conform to the FITS Standard.",0);
check_fixed_log(cards[0], out);
}
else {
if(strcmp(tmpkey.kname,"XTENSION"))
wrterr(out, "The 1st keyword of a extension is not XTENSION.",1);
check_str(&tmpkey,out);
check_fixed_str(cards[0], out);
/* Get the original string */
p = cards[0];
p +=10;
while (*p == ' ') p++;
p++; /* skip the quote */
if( strncmp(p,"TABLE ",8) &&
strncmp(p,"BINTABLE",8) &&
strncmp(p,"A3DTABLE",8) &&
strncmp(p,"IUEIMAGE",8) &&
strncmp(p,"FOREIGN ",8) &&
strncmp(p,"DUMP ",8) &&
strncmp(p,"IMAGE ",8) ) {
sprintf(errmes, "Unregistered XTENSION value \"%8.8s\".",p);
wrterr(out,errmes,1);
}
else {
if (p[8] != '\'') {
sprintf(errmes,
"Extra \'%c\' follows the XTENSION value \"%8.8s\".",p[8],p);
wrterr(out,errmes,1);
}
}
/* test if this is a tile compressed image, stored in a binary table */
/* If so then test the extension as binary table rather than an image */
if (!strncmp(p,"BINTABLE",8) && hduptr->hdutype == IMAGE_HDU) {
hduptr->hdutype = BINARY_TBL;
hduptr->istilecompressed = 1;
} else {
hduptr->istilecompressed = 0;
}
}
/* read the BITPIX keywords */
if(fits_read_key(infits, TINT, "BITPIX", &(hduptr->bitpix), NULL, &status))
wrtferr(out,"",&status,2);
check_fixed_int(cards[1], out);
/* Read and Parse the NAXIS */
hduptr->naxis = 0;
if(fits_read_key(infits, TINT, "NAXIS", &(hduptr->naxis), NULL, &status))
wrtferr(out,"",&status,2);
check_fixed_int(cards[2], out);
if(hduptr->naxis!=0)
hduptr->naxes = (LONGLONG *)malloc(hduptr->naxis*sizeof(LONGLONG));
for (i = 0; i < hduptr->naxis; i++) hduptr->naxes[i] = -1;
/* Parse the keywords NAXISn */
for (j = 3; j < 3 + hduptr->naxis; j++){
fits_parse_card(out, 1+j,cards[j], tmpkey.kname,
&(tmpkey.ktype), tmpkey.kvalue,comm);
p = tmpkey.kname+5;
if(!isdigit((int) *p))continue;
#if (USE_LL_SUFFIX == 1)
if(check_int(&tmpkey,out)) lu = strtoll(tmpkey.kvalue,NULL,10);
#else
if(check_int(&tmpkey,out)) lu = strtol(tmpkey.kvalue,NULL,10);
#endif
lv = strtol(p,NULL,10);
if(lv > hduptr->naxis && lv <= 0) {
sprintf(errmes,
"Keyword #%d, %s is not allowed (with n > NAXIS = %d).",
tmpkey.kindex,tmpkey.kname,hduptr->naxis);
wrterr(out,errmes,1);
}
else {
if(hduptr->naxes[lv-1] == -1) {
hduptr->naxes[lv-1] = lu;
}
else {
sprintf(errmes, "Keyword #%d, %s is duplicated.",
tmpkey.kindex,tmpkey.kname);
wrterr(out,errmes,1);
}
}
check_fixed_int(cards[j], out);
}
/* check all the NAXISn are there */
for (j = 0; j < hduptr->naxis; j++) {
if(hduptr->naxes[j] == -1) {
sprintf(errmes,
"Keyword NAXIS%d is not present or is out of order.", j+1);
wrterr(out,errmes,2);
}
}
/* get the column number */
hduptr->ncols = 1;
if(hduptr->hdutype == ASCII_TBL || hduptr->hdutype == BINARY_TBL) {
/* get the total number of columns */
if(fits_get_num_cols(infits, &(hduptr->ncols),&status))
wrtferr(out,"",&status,2);
}
/* parse the keywords after NAXISn and prepare the array for
sorting. We only check the keywords after the NAXISn */
n = hduptr->nkeys - 4 - hduptr->naxis ; /* excluding the SIMPLE/XTENSION,
BITPIX, NAXIS, NAXISn
and END */
hduptr->kwds = (FitsKey **)malloc(sizeof(FitsKey *)*n);
for (i= 0; i < n; i++)
hduptr->kwds[i] = (FitsKey *)malloc(sizeof(FitsKey));
kwds = hduptr->kwds;
k = 3 + hduptr->naxis; /* index of first keyword following NAXISn. */
m = hduptr->nkeys - 1; /* last key */
i = 0;
hduptr->use_longstr = 0;
for (j = k ; j < m; j++) {
kwds[i]->kindex = j+1; /* record number */
kwds[i]->goodkey=1;
if(fits_parse_card(out,1+j,cards[j], kwds[i]->kname,
&(kwds[i]->ktype), kwds[i]->kvalue,comm))
kwds[i]->goodkey=0;
if (kwds[i]->ktype == UNKNOWN && *(kwds[i]->kvalue) == 0)
{
sprintf(errmes,
"Keyword #%d, %s has a null value.",
j+1,kwds[i]->kname);
wrtwrn(out,errmes,0);
}
/* only count the non-commentary keywords */
if (!strcmp(kwds[i]->kname,"CONTINUE")) {
hduptr->use_longstr = 1;
}
if( strcmp(kwds[i]->kname,"COMMENT") &&
strcmp(kwds[i]->kname,"HISTORY") &&
strcmp(kwds[i]->kname,"HIERARCH") &&
strcmp(kwds[i]->kname,"CONTINUE") &&
strcmp(kwds[i]->kname,"") ) i++;
}
numusrkey = i;
hduptr->tkeys = i;
/* parse the END key */
fits_parse_card(out,m+1,cards[hduptr->nkeys-1],
tmpkey.kname,&(tmpkey.ktype),tmpkey.kvalue,comm) ;
/* sort the keyword in the ascending order of kname field*/
qsort(kwds, numusrkey, sizeof(FitsKey *), compkey);
/* store addresses of sorted keyword names in a working
array */
tmpkwds = (char **)malloc(sizeof(char*) * numusrkey);
for (i=0; i < numusrkey; i++) tmpkwds[i] = kwds[i]->kname;
/* Initialize the PCOUNT, GCOUNT and heap values */
hduptr->pcount = -99;
hduptr->gcount = -99;
hduptr->heap = -99;
/* set the random group flag (will be determined later) */
hduptr->isgroup = 0;
/* allocate memory for datamax and datamin (will determined later)*/
if(hduptr->ncols > 0) {
hduptr->datamax = (char **)calloc(hduptr->ncols, sizeof(char *));
hduptr->datamin = (char **)calloc(hduptr->ncols, sizeof(char *));
hduptr->tnull = (char **)calloc(hduptr->ncols, sizeof(char *));
for (i = 0; i < hduptr->ncols; i++) {
hduptr->datamax[i] = (char *)calloc(13,sizeof(char));
hduptr->datamin[i] = (char *)calloc(13,sizeof(char));
hduptr->tnull[i] = (char *)calloc(12,sizeof(char));
}
}
/* initialize the extension name and version */
strcpy(hduptr->extname,"");
hduptr->extver = -999;
return;
}
/*************************************************************
*
* test_hdu
*
* Test the HDU header
* This includes many tests of WCS keywords
*
*************************************************************/
void test_hdu(fitsfile *infits, /* input fits file */
FILE *out, /* output ascii file */
FitsHdu *hduptr
)
{
int status = 0;
FitsKey **kwds;
int numusrkey;
int hdunum;
char *p, *p2, *pname = 0;
int i,j,k,m,n, wcsaxes = 0, taxes;
int wcsaxesExists = 0, wcsaxesvalue = 0, wcsaxespos = 0, wcskeypos = 1000000000;
FitsKey *pkey;
int crota2_exists = 0, matrix_exists[2] = {0,0};
double dvalue;
/* floating WCS keywords */
char *cfltkeys[] = {"CRPIX", "CRVAL","CDELT","CROTA",
"CRDER","CSYER", "PV"};
int ncfltkeys = 7;
int keynum[] = {0,0,0,0,0,0,0}, nmax = 0;
/* floating non-indexed WCS keywords */
char *cfltnkeys[] = {"RESTFRQ", "RESTFREQ", "RESTWAV",
"OBSGEO-X", "OBSGEO-Y", "OBSGEO-Z",
"VELOSYS", "ZSOURCE", "VELANGL",
"LONPOLE", "LATPOLE"};
int ncfltnkeys = 11;
/* floating WCS keywords w/ underscore */
char *cflt_keys[] = {"PC","CD"};
int ncflt_keys = 2;
/* string WCS keywords */
char *cstrkeys[] = {"CTYPE", "CUNIT", "PS", "CNAME" };
int ncstrkeys = 4;
/* string RADESYS keywords with list of allowed values */
char *rastrkeys[] = {"RADESYS", "RADECSYS" };
int nrastrkeys = 2;
/* string spectral ref frame keywords with list of allowed values */
char *specstrkeys[] = {"SPECSYS", "SSYSOBS", "SSYSSRC" };
int nspecstrkeys = 3;
numusrkey = hduptr->tkeys;
kwds = hduptr->kwds;
/* find the extension name and version */
strcpy(temp,"EXTNAME");
ptemp = temp;
key_match(tmpkwds,numusrkey,&ptemp,1,&k,&n);
if(k> -1 ) {
if(kwds[k]->ktype == STR_KEY)
strcpy(hduptr->extname,kwds[k]->kvalue);
}
strcpy(temp,"EXTVER");
ptemp = temp;
key_match(tmpkwds,numusrkey,&ptemp,1,&k,&n);
if(k> -1 ) {
if(kwds[k]->ktype == INT_KEY)
hduptr->extver = (int) strtol(kwds[k]->kvalue,NULL,10);
}
/* set the HduName structure */
hdunum = hduptr->hdunum;
set_hduname(hdunum,hduptr->hdutype,hduptr->extname, hduptr->extver);
if(hduptr->hdunum == 1) {
test_prm(infits,out,hduptr);
}
else {
/* test the keywords specific to the hdutype*/
switch (hduptr->hdutype) {
case IMAGE_HDU:
test_img_ext(infits,out,hduptr);
break;
case ASCII_TBL:
test_asc_ext(infits,out,hduptr);
break;
case BINARY_TBL:
test_bin_ext(infits,out,hduptr);
break;
default:
break;
}
}
/* test the general keywords */
test_header(infits,out,hduptr);
/* test if CROTA2 exists; if so, then PCi_j must not exist */
strcpy(temp,"CROTA2");
ptemp = temp;
key_match(tmpkwds,numusrkey,&ptemp,1,&k,&n);
if (n == 1) {
pkey = hduptr->kwds[k];
crota2_exists = pkey->kindex;
}
strcpy(temp,"WCSAXES");
ptemp = temp;
/* first find the primary WCSAXES value, if it exists */
key_match(tmpkwds,numusrkey,&ptemp,1,&k,&n);
if (k >= 0) {
j = k;
if (check_int(kwds[j],out)) {
pkey = hduptr->kwds[j];
wcsaxesvalue = (int) strtol(pkey->kvalue,NULL,10);
nmax = wcsaxesvalue;
}
}
/* Check and find max value of the WCSAXESa keywords */
/* Use the max value when checking the range of the indexed WCS keywords. */
/* This is a less rigorous test than if one were to test the range of the */
/* keywords for each of the alternate WCS systems (A - Z) against the */
/* corresponding WCSAXESa keyword. */
key_match(tmpkwds,numusrkey,&ptemp,0,&k,&n);
for (j = k; j< n + k ; j++){
if (check_int(kwds[j],out)) {
pkey = hduptr->kwds[j];
taxes = (int) strtol(pkey->kvalue,NULL,10);
if (taxes > wcsaxes) wcsaxes = taxes;
wcsaxesExists = 1;
/* store highest index of any wcsaxes keyword */
/* (they must appear before other WCS keywords) */
if (pkey->kindex > wcsaxespos) wcsaxespos = pkey->kindex;
}
}
/* test datatype of reserved indexed floating point WCS keywords */
for (i = 0; i < ncfltkeys; i++) {
strcpy(temp,cfltkeys[i]);
ptemp = temp;
key_match(tmpkwds,numusrkey,&ptemp,0,&k,&n);
if(k < 0) continue;
for (j = k; j < k+n; j++) {
pkey = hduptr->kwds[j];
p = kwds[j]->kname;
p += strlen(temp);
if(!isdigit((int)*p)) continue;
if (!check_flt(pkey,out) )continue;
if (i == 2 ) { /* test that CDELTi != 0 */
dvalue = strtod(pkey->kvalue, NULL);
if (dvalue == 0.) {
sprintf( errmes,
"Keyword #%d, %s: must have non-zero value.",
pkey->kindex,pkey->kname);
wrterr(out,errmes,1);
}
}
if (i == 4 || i == 5 ) { /* test that CRDERi and CSYSERi are non-negative */
dvalue = strtod(pkey->kvalue, NULL);
if (dvalue < 0.) {
sprintf( errmes,
"Keyword #%d, %s: must have non-negative value: %s",
pkey->kindex,pkey->kname,pkey->kvalue);
wrterr(out,errmes,1);
}
}
m = (int)strtol(p,&p2,10);
if (wcsaxesExists) { /* WCSAXES keyword exists */
if (m < 1 || m > wcsaxes) {
sprintf( errmes,
"Keyword #%d, %s: index %d is not in range 1-%d (WCSAXES).",
pkey->kindex,pkey->kname,m,wcsaxes);
wrterr(out,errmes,1);
}
} else {
if (m < 1 || m > hduptr->naxis) {
sprintf( errmes,
"Keyword #%d, %s: index %d is not in range 1-%d (NAXIS).",
pkey->kindex,pkey->kname,m,hduptr->naxis);
wrtwrn(out,errmes,0);
}
}
/* count the number of each keyword */
if (*p2 == 0) { /* only test the primary set of WCS keywords */
keynum[i] = keynum[i] + 1;
if (m > nmax) nmax = m;
}
/* store lowest index of any wcs keyword */
if (pkey->kindex < wcskeypos) {
wcskeypos = pkey->kindex;
pname = pkey->kname;
}
}
}
if (wcsaxesvalue == 0) { /* limit value of nmax to the legal maximum */
if (nmax > hduptr->naxis)
nmax = hduptr->naxis;
} else {
if (nmax > wcsaxesvalue)
nmax = wcsaxesvalue;
}
if (keynum[0] < nmax) { /* test number of CRPIXi keywords */
sprintf( errmes,
"Some CRPIXi keywords appear to be missing; expected %d.",nmax);
wrtwrn(out,errmes,0);
}
if (keynum[1] < nmax) { /* test number of CRVALi keywords */
sprintf( errmes,
"Some CRVALi keywords appear to be missing; expected %d.",nmax);
wrtwrn(out,errmes,0);
}
/* test datatype of reserved non-indexed floating point WCS keywords */
for (i = 0; i < ncfltnkeys; i++) {
strcpy(temp,cfltnkeys[i]);
ptemp = temp;
key_match(tmpkwds,numusrkey,&ptemp,0,&k,&n);
if(k < 0) continue;
for (j = k; j < k+n; j++) {
pkey = hduptr->kwds[j];
if (!check_flt(pkey,out) )continue;
}
}
/* test datatype of reserved indexed floating point WCS keywords with "_" */
for (i = 0; i < ncflt_keys; i++) {
strcpy(temp,cflt_keys[i]);
ptemp = temp;
key_match(tmpkwds,numusrkey,&ptemp,0,&k,&n);
if(k < 0) continue;
for (j = k; j < k+n; j++) {
pkey = hduptr->kwds[j];
p = kwds[j]->kname;
p += strlen(temp);
if(!isdigit((int)*p)) continue;
p2 = strchr(p, '_'); /* 2 digits must be separated by a '_' */
if (!p2) continue;
if (!check_flt(pkey,out) )continue;
*p2 = '\0'; /* terminate string at the '_' */
/* test the first digit */
m = (int)strtol(p,NULL,10);
*p2 = '_'; /* replace the '_' */
if (wcsaxesExists) { /* WCSAXES keyword exists */
if (m < 1 || m > wcsaxes) {
sprintf( errmes,
"Keyword #%d, %s: 1st index %d is not in range 1-%d (WCSAXES).",
pkey->kindex,pkey->kname,m,wcsaxes);
wrterr(out,errmes,1);
}
} else {
if (m < 1 || m > hduptr->naxis) {
sprintf( errmes,
"Keyword #%d, %s: 1st index %d is not in range 1-%d (NAXIS).",
pkey->kindex,pkey->kname,m,hduptr->naxis);
wrtwrn(out,errmes,0);
}
}
/* test the second digit */
p = p2 + 1;
m = (int)strtol(p,&p2,10);
if (wcsaxesExists) { /* WCSAXES keyword exists */
if (m < 1 || m > wcsaxes) {
sprintf( errmes,
"Keyword #%d, %s: 2nd index %d is not in range 1-%d (WCSAXES).",
pkey->kindex,pkey->kname,m,wcsaxes);
wrterr(out,errmes,1);
}
} else {
if (m < 1 || m > hduptr->naxis) {
sprintf( errmes,
"Keyword #%d, %s: 2nd index %d is not in range 1-%d (NAXIS).",
pkey->kindex,pkey->kname,m,hduptr->naxis);
wrtwrn(out,errmes,0);
}
}
if (*p2 == 0) { /* no alternate suffix on the PC or CD name */
matrix_exists[i] = pkey->kindex;
}
/* store lowest index of any wcs keyword */
if (pkey->kindex < wcskeypos) {
wcskeypos = pkey->kindex;
pname = pkey->kname;
}
}
}
if (matrix_exists[0] > 0 && matrix_exists[1] > 0 ) {
sprintf( errmes,
"Keywords PCi_j (#%d) and CDi_j (#%d) are mutually exclusive.",
matrix_exists[0],matrix_exists[1]);
wrterr(out,errmes,1);
}
if (matrix_exists[0] > 0 && crota2_exists > 0 ) {
sprintf( errmes,
"Keywords PCi_j (#%d) and CROTA2 (#%d) are mutually exclusive.",
matrix_exists[0],crota2_exists);
wrterr(out,errmes,1);
}
/* test datatype of reserved indexed string WCS keywords */
for (i = 0; i < ncstrkeys; i++) {
strcpy(temp,cstrkeys[i]);
ptemp = temp;
keynum[i] = 0;
key_match(tmpkwds,numusrkey,&ptemp,0,&k,&n);
if(k < 0) continue;
for (j = k; j < k+n; j++) {
pkey = hduptr->kwds[j];
p = kwds[j]->kname;
p += strlen(temp);
if(!isdigit((int)*p)) continue;
if (!check_str(pkey,out) )continue;
m = (int)strtol(p,&p2,10);
if (wcsaxesExists) { /* WCSAXES keyword exists */
if (m < 1 || m > wcsaxes) {
sprintf( errmes,
"Keyword #%d, %s: index %d is not in range 1-%d (WCSAXES).",
pkey->kindex,pkey->kname,m,wcsaxes);
wrterr(out,errmes,1);
}
} else {
if (m < 1 || m > hduptr->naxis) {
sprintf( errmes,
"Keyword #%d, %s: index %d is not in range 1-%d (NAXIS).",
pkey->kindex,pkey->kname,m,hduptr->naxis);
wrtwrn(out,errmes,0);
}
}
if (*p2 == 0) { /* only test the primary set of WCS keywords */
keynum[i] = keynum[i] + 1;
}
/* store lowest index of any wcs keyword */
if (pkey->kindex < wcskeypos) {
wcskeypos = pkey->kindex;
pname = pkey->kname;
}
}
}
if (keynum[0] < nmax) {
sprintf( errmes,
"Some CTYPEi keywords appear to be missing; expected %d.",nmax);
wrtwrn(out,errmes,0);
}
if (wcskeypos < wcsaxespos) {
sprintf( errmes,
"WCSAXES keyword #%d appears after other WCS keyword %s #%d",
wcsaxespos, pname, wcskeypos);
wrterr(out,errmes,1);
}
/* test datatype and value of reserved RADECSYS WCS keywords */
for (i = 0; i < nrastrkeys; i++) {
strcpy(temp,rastrkeys[i]);
ptemp = temp;
keynum[i] = 0;
key_match(tmpkwds,numusrkey,&ptemp,0,&k,&n);
if(k < 0) continue;
for (j = k; j < k+n; j++) {
pkey = hduptr->kwds[j];
p = kwds[j]->kname;
p += strlen(temp);
if (!check_str(pkey,out) )continue;
if (strcmp(pkey->kvalue, "ICRS") && strcmp(pkey->kvalue, "FK5") &&
strcmp(pkey->kvalue, "FK4") && strcmp(pkey->kvalue, "FK4-NO-E") &&
strcmp(pkey->kvalue, "GAPPT")) {
sprintf( errmes,
"Keyword #%d, %s has non-allowed value: %s",
pkey->kindex,pkey->kname,pkey->kvalue);
wrtwrn(out,errmes,0);
}
}
}
/* test datatype and value of reserved spectral ref frame WCS keywords */
for (i = 0; i < nspecstrkeys; i++) {
strcpy(temp,specstrkeys[i]);
ptemp = temp;
keynum[i] = 0;
key_match(tmpkwds,numusrkey,&ptemp,0,&k,&n);
if(k < 0) continue;
for (j = k; j < k+n; j++) {
pkey = hduptr->kwds[j];
p = kwds[j]->kname;
p += strlen(temp);
if (!check_str(pkey,out) )continue;
if (strcmp(pkey->kvalue, "TOPOCENT") && strcmp(pkey->kvalue, "GEOCENTR") &&
strcmp(pkey->kvalue, "BARYCENT") && strcmp(pkey->kvalue, "HELIOCEN") &&
strcmp(pkey->kvalue, "LSRK") && strcmp(pkey->kvalue, "LSRD") &&
strcmp(pkey->kvalue, "GALACTOC") && strcmp(pkey->kvalue, "LOCALGRP") &&
strcmp(pkey->kvalue, "CMBDIPOL") && strcmp(pkey->kvalue, "SOURCE")) {
sprintf( errmes,
"Keyword #%d, %s has non-allowed value: %s",
pkey->kindex,pkey->kname,pkey->kvalue);
wrtwrn(out,errmes,0);
}
}
}
/* test the fill area */
if(testfill) {
if(ffchfl(infits,&status)) {
wrterr(out,
"The header fill area is not totally filled with blanks.",1);
}
}
return ;
}
/*************************************************************
*
* test_prm
*
* Test the primary array header
*
*
*************************************************************/
void test_prm(fitsfile *infits, /* input fits file */
FILE* out, /* output ascii file */
FitsHdu *hduptr /* hdu information structure */
)
{
int i,j,k,n;
FitsKey *pkey;
FitsKey **kwds;
int numusrkey;
char *p;
char *exlkey[] = {"XTENSION"};
int nexlkey = 1;
kwds = hduptr->kwds;
numusrkey = hduptr->tkeys;
/* The SIMPLE, BITPIX, NAXIS, and NAXISn keywords have been
checked in CFITSIO */
/* excluded keywords cannot be used. */
for (i = 0; i < nexlkey; i++) {
strcpy(temp,exlkey[i]);
ptemp = temp;
key_match(tmpkwds,numusrkey,&ptemp,1,&k,&n);
if( n > 0) {
pkey = hduptr->kwds[k];
sprintf(errmes,
"Keyword #%d, %s is not allowed in a primary array.",
pkey->kindex,exlkey[i]);
wrterr(out,errmes,1);
}
}
/* Check if Random Groups file */
strcpy(temp,"GROUPS");
ptemp = temp;
key_match(tmpkwds,numusrkey,&ptemp,1,&k,&n);
if(k > -1){
pkey = hduptr->kwds[k];
if(*(pkey->kvalue) == 'T' && hduptr->naxis > 0 && hduptr->naxes[0]==0) {
hduptr->isgroup = 1;
check_fixed_log(cards[pkey->kindex - 1], out);
}
}
/* check the position of the EXTEND */
/* the EXTEND keyword is no longer required if the file contains extensions */
if (hduptr->isgroup == 0) {
strcpy(temp,"EXTEND");
ptemp = temp;
key_match(tmpkwds,numusrkey,&ptemp,1,&k,&n);
if( k > 0) {
pkey = hduptr->kwds[k];
if(check_log(pkey,out) && *(pkey->kvalue)!='T' && totalhdu > 1) {
sprintf(errmes,"There are extensions but EXTEND = F.");
wrterr(out,errmes,1);
}
}
}
/* Check PCOUNT and GCOUNT keyword */
strcpy(temp,"PCOUNT");
ptemp = temp;
key_match(tmpkwds,numusrkey,&ptemp,1,&k,&n);
if(k > -1) {
pkey = hduptr->kwds[k];
/* Primary array cannot have PCOUNT */
if (!hduptr->isgroup ){
sprintf(errmes,