-
Notifications
You must be signed in to change notification settings - Fork 10
/
getkey.c
3747 lines (3222 loc) · 131 KB
/
getkey.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, getkey.c, contains routines that read keywords from */
/* a FITS header. */
/* 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 <string.h>
#include <limits.h>
#include <stdlib.h>
#include <ctype.h>
/* stddef.h is apparently needed to define size_t */
#include <stddef.h>
#include "fitsio2.h"
/*--------------------------------------------------------------------------*/
int ffghsp(fitsfile *fptr, /* I - FITS file pointer */
int *nexist, /* O - number of existing keywords in header */
int *nmore, /* O - how many more keywords will fit */
int *status) /* IO - error status */
/*
returns the number of existing keywords (not counting the END keyword)
and the number of more keyword that will fit in the current header
without having to insert more FITS blocks.
*/
{
if (*status > 0)
return(*status);
if (fptr->HDUposition != (fptr->Fptr)->curhdu)
ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);
if (nexist)
*nexist = (int) (( ((fptr->Fptr)->headend) -
((fptr->Fptr)->headstart[(fptr->Fptr)->curhdu]) ) / 80);
if ((fptr->Fptr)->datastart == DATA_UNDEFINED)
{
if (nmore)
*nmore = -1; /* data not written yet, so room for any keywords */
}
else
{
/* calculate space available between the data and the END card */
if (nmore)
*nmore = (int) (((fptr->Fptr)->datastart - (fptr->Fptr)->headend) / 80 - 1);
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffghps(fitsfile *fptr, /* I - FITS file pointer */
int *nexist, /* O - number of existing keywords in header */
int *position, /* O - position of next keyword to be read */
int *status) /* IO - error status */
/*
return the number of existing keywords and the position of the next
keyword that will be read.
*/
{
if (*status > 0)
return(*status);
if (fptr->HDUposition != (fptr->Fptr)->curhdu)
ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);
if (nexist)
*nexist = (int) (( ((fptr->Fptr)->headend) - ((fptr->Fptr)->headstart[(fptr->Fptr)->curhdu]) ) / 80);
if (position)
*position = (int) (( ((fptr->Fptr)->nextkey) - ((fptr->Fptr)->headstart[(fptr->Fptr)->curhdu]) ) / 80 + 1);
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffnchk(fitsfile *fptr, /* I - FITS file pointer */
int *status) /* IO - error status */
/*
function returns the position of the first null character (ASCII 0), if
any, in the current header. Null characters are illegal, but the other
CFITSIO routines that read the header will not detect this error, because
the null gets interpreted as a normal end of string character.
*/
{
long ii, nblock;
LONGLONG bytepos;
int length, nullpos;
char block[2881];
if (*status > 0)
return(*status);
if (fptr->HDUposition != (fptr->Fptr)->curhdu)
ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);
if ((fptr->Fptr)->datastart == DATA_UNDEFINED)
{
return(0); /* Don't check a file that is just being created. */
/* It cannot contain nulls since CFITSIO wrote it. */
}
else
{
/* calculate number of blocks in the header */
nblock = (long) (( (fptr->Fptr)->datastart -
(fptr->Fptr)->headstart[(fptr->Fptr)->curhdu] ) / 2880);
}
bytepos = (fptr->Fptr)->headstart[(fptr->Fptr)->curhdu];
ffmbyt(fptr, bytepos, REPORT_EOF, status); /* move to read pos. */
block[2880] = '\0';
for (ii = 0; ii < nblock; ii++)
{
if (ffgbyt(fptr, 2880, block, status) > 0)
return(0); /* read error of some sort */
length = strlen(block);
if (length != 2880)
{
nullpos = (ii * 2880) + length + 1;
return(nullpos);
}
}
return(0);
}
/*--------------------------------------------------------------------------*/
int ffmaky(fitsfile *fptr, /* I - FITS file pointer */
int nrec, /* I - one-based keyword number to move to */
int *status) /* IO - error status */
{
/*
move pointer to the specified absolute keyword position. E.g. this keyword
will then be read by the next call to ffgnky.
*/
if (fptr->HDUposition != (fptr->Fptr)->curhdu)
ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);
(fptr->Fptr)->nextkey = (fptr->Fptr)->headstart[(fptr->Fptr)->curhdu] + ( (nrec - 1) * 80);
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffmrky(fitsfile *fptr, /* I - FITS file pointer */
int nmove, /* I - relative number of keywords to move */
int *status) /* IO - error status */
{
/*
move pointer to the specified keyword position relative to the current
position. E.g. this keyword will then be read by the next call to ffgnky.
*/
if (fptr->HDUposition != (fptr->Fptr)->curhdu)
ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);
(fptr->Fptr)->nextkey += (nmove * 80);
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffgnky(fitsfile *fptr, /* I - FITS file pointer */
char *card, /* O - card string */
int *status) /* IO - error status */
/*
read the next keyword from the header - used internally by cfitsio
*/
{
int jj, nrec;
LONGLONG bytepos, endhead;
char message[FLEN_ERRMSG];
if (*status > 0)
return(*status);
if (fptr->HDUposition != (fptr->Fptr)->curhdu)
ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);
card[0] = '\0'; /* make sure card is terminated, even affer read error */
/*
Check that nextkey points to a legal keyword position. Note that headend
is the current end of the header, i.e., the position where a new keyword
would be appended, however, if there are more than 1 FITS block worth of
blank keywords at the end of the header (36 keywords per 2880 byte block)
then the actual physical END card must be located at a starting position
which is just 2880 bytes prior to the start of the data unit.
*/
bytepos = (fptr->Fptr)->nextkey;
endhead = maxvalue( ((fptr->Fptr)->headend), ((fptr->Fptr)->datastart - 2880) );
/* nextkey must be < endhead and > than headstart */
if (bytepos > endhead ||
bytepos < (fptr->Fptr)->headstart[(fptr->Fptr)->curhdu] )
{
nrec= (int) ((bytepos - (fptr->Fptr)->headstart[(fptr->Fptr)->curhdu]) / 80 + 1);
snprintf(message, FLEN_ERRMSG,"Cannot get keyword number %d. It does not exist.",
nrec);
ffpmsg(message);
return(*status = KEY_OUT_BOUNDS);
}
ffmbyt(fptr, bytepos, REPORT_EOF, status); /* move to read pos. */
card[80] = '\0'; /* make sure card is terminate, even if ffgbyt fails */
if (ffgbyt(fptr, 80, card, status) <= 0)
{
(fptr->Fptr)->nextkey += 80; /* increment pointer to next keyword */
/* strip off trailing blanks with terminated string */
jj = 79;
while (jj >= 0 && card[jj] == ' ')
jj--;
card[jj + 1] = '\0';
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffgnxk( fitsfile *fptr, /* I - FITS file pointer */
char **inclist, /* I - list of included keyword names */
int ninc, /* I - number of names in inclist */
char **exclist, /* I - list of excluded keyword names */
int nexc, /* I - number of names in exclist */
char *card, /* O - first matching keyword */
int *status) /* IO - error status */
/*
Return the next keyword that matches one of the names in inclist
but does not match any of the names in exclist. The search
goes from the current position to the end of the header, only.
Wild card characters may be used in the name lists ('*', '?' and '#').
*/
{
int casesn, match, exact, namelen;
long ii, jj;
char keybuf[FLEN_CARD], keyname[FLEN_KEYWORD];
card[0] = '\0';
if (*status > 0)
return(*status);
casesn = FALSE;
/* get next card, and return with an error if hit end of header */
while( ffgcrd(fptr, "*", keybuf, status) <= 0)
{
ffgknm(keybuf, keyname, &namelen, status); /* get the keyword name */
/* does keyword match any names in the include list? */
for (ii = 0; ii < ninc; ii++)
{
ffcmps(inclist[ii], keyname, casesn, &match, &exact);
if (match)
{
/* does keyword match any names in the exclusion list? */
jj = -1;
while ( ++jj < nexc )
{
ffcmps(exclist[jj], keyname, casesn, &match, &exact);
if (match)
break;
}
if (jj >= nexc)
{
/* not in exclusion list, so return this keyword */
strcat(card, keybuf);
return(*status);
}
}
}
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffgky( fitsfile *fptr, /* I - FITS file pointer */
int datatype, /* I - datatype of the value */
const char *keyname, /* I - name of keyword to read */
void *value, /* O - keyword value */
char *comm, /* O - keyword comment */
int *status) /* IO - error status */
/*
Read (get) the keyword value and comment from the FITS header.
Reads a keyword value with the datatype specified by the 2nd argument.
*/
{
LONGLONG longval;
ULONGLONG ulongval;
double doubleval;
if (*status > 0) /* inherit input status value if > 0 */
return(*status);
if (datatype == TSTRING)
{
ffgkys(fptr, keyname, (char *) value, comm, status);
}
else if (datatype == TBYTE)
{
if (ffgkyjj(fptr, keyname, &longval, comm, status) <= 0)
{
if (longval > UCHAR_MAX || longval < 0)
*status = NUM_OVERFLOW;
else
*(unsigned char *) value = (unsigned char) longval;
}
}
else if (datatype == TSBYTE)
{
if (ffgkyjj(fptr, keyname, &longval, comm, status) <= 0)
{
if (longval > 127 || longval < -128)
*status = NUM_OVERFLOW;
else
*(signed char *) value = (signed char) longval;
}
}
else if (datatype == TUSHORT)
{
if (ffgkyjj(fptr, keyname, &longval, comm, status) <= 0)
{
if (longval > (unsigned short) USHRT_MAX || longval < 0)
*status = NUM_OVERFLOW;
else
*(unsigned short *) value = (unsigned short) longval;
}
}
else if (datatype == TSHORT)
{
if (ffgkyjj(fptr, keyname, &longval, comm, status) <= 0)
{
if (longval > SHRT_MAX || longval < SHRT_MIN)
*status = NUM_OVERFLOW;
else
*(short *) value = (short) longval;
}
}
else if (datatype == TUINT)
{
if (ffgkyjj(fptr, keyname, &longval, comm, status) <= 0)
{
if (longval > (unsigned int) UINT_MAX || longval < 0)
*status = NUM_OVERFLOW;
else
*(unsigned int *) value = longval;
}
}
else if (datatype == TINT)
{
if (ffgkyjj(fptr, keyname, &longval, comm, status) <= 0)
{
if (longval > INT_MAX || longval < INT_MIN)
*status = NUM_OVERFLOW;
else
*(int *) value = longval;
}
}
else if (datatype == TLOGICAL)
{
ffgkyl(fptr, keyname, (int *) value, comm, status);
}
else if (datatype == TULONG)
{
if (ffgkyujj(fptr, keyname, &ulongval, comm, status) <= 0)
{
if (ulongval > ULONG_MAX)
*status = NUM_OVERFLOW;
else
*(unsigned long *) value = ulongval;
}
}
else if (datatype == TLONG)
{
if (ffgkyjj(fptr, keyname, &longval, comm, status) <= 0)
{
if (longval > LONG_MAX || longval < LONG_MIN)
*status = NUM_OVERFLOW;
else
*(int *) value = longval;
}
ffgkyj(fptr, keyname, (long *) value, comm, status);
}
else if (datatype == TULONGLONG)
{
ffgkyujj(fptr, keyname, (ULONGLONG *) value, comm, status);
}
else if (datatype == TLONGLONG)
{
ffgkyjj(fptr, keyname, (LONGLONG *) value, comm, status);
}
else if (datatype == TFLOAT)
{
ffgkye(fptr, keyname, (float *) value, comm, status);
}
else if (datatype == TDOUBLE)
{
ffgkyd(fptr, keyname, (double *) value, comm, status);
}
else if (datatype == TCOMPLEX)
{
ffgkyc(fptr, keyname, (float *) value, comm, status);
}
else if (datatype == TDBLCOMPLEX)
{
ffgkym(fptr, keyname, (double *) value, comm, status);
}
else
*status = BAD_DATATYPE;
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffgkey( fitsfile *fptr, /* I - FITS file pointer */
const char *keyname, /* I - name of keyword to read */
char *keyval, /* O - keyword value */
char *comm, /* O - keyword comment */
int *status) /* IO - error status */
/*
Read (get) the named keyword, returning the keyword value and comment.
The value is just the literal string of characters in the value field
of the keyword. In the case of a string valued keyword, the returned
value includes the leading and closing quote characters. The value may be
up to 70 characters long, and the comment may be up to 72 characters long.
If the keyword has no value (no equal sign in column 9) then a null value
is returned.
*/
{
char card[FLEN_CARD];
keyval[0] = '\0';
if (comm)
comm[0] = '\0';
if (*status > 0)
return(*status);
if (ffgcrd(fptr, keyname, card, status) > 0) /* get the 80-byte card */
return(*status);
ffpsvc(card, keyval, comm, status); /* parse the value and comment */
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffgrec( fitsfile *fptr, /* I - FITS file pointer */
int nrec, /* I - number of keyword to read */
char *card, /* O - keyword card */
int *status) /* IO - error status */
/*
Read (get) the nrec-th keyword, returning the entire keyword card up to
80 characters long. The first keyword in the header has nrec = 1, not 0.
The returned card value is null terminated with any trailing blank
characters removed. If nrec = 0, then this routine simply moves the
current header pointer to the top of the header.
*/
{
if (*status > 0)
return(*status);
if (nrec == 0)
{
ffmaky(fptr, 1, status); /* simply move to beginning of header */
if (card)
card[0] = '\0'; /* and return null card */
}
else if (nrec > 0)
{
ffmaky(fptr, nrec, status);
ffgnky(fptr, card, status);
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffgcrd( fitsfile *fptr, /* I - FITS file pointer */
const char *name, /* I - name of keyword to read */
char *card, /* O - keyword card */
int *status) /* IO - error status */
/*
Read (get) the named keyword, returning the entire keyword card up to
80 characters long.
The returned card value is null terminated with any trailing blank
characters removed.
If the input name contains wild cards ('?' matches any single char
and '*' matches any sequence of chars, # matches any string of decimal
digits) then the search ends once the end of header is reached and does
not automatically resume from the top of the header.
*/
{
int nkeys, nextkey, ntodo, namelen, namelen_limit, namelenminus1, cardlen;
int ii = 0, jj, kk, wild, match, exact, hier = 0;
char keyname[FLEN_KEYWORD], cardname[FLEN_KEYWORD];
char *ptr1, *ptr2, *gotstar;
if (*status > 0)
return(*status);
*keyname = '\0';
while (name[ii] == ' ') /* skip leading blanks in name */
ii++;
strncat(keyname, &name[ii], FLEN_KEYWORD - 1);
namelen = strlen(keyname);
while (namelen > 0 && keyname[namelen - 1] == ' ')
namelen--; /* ignore trailing blanks in name */
keyname[namelen] = '\0'; /* terminate the name */
for (ii=0; ii < namelen; ii++)
keyname[ii] = toupper(keyname[ii]); /* make upper case */
if (FSTRNCMP("HIERARCH", keyname, 8) == 0)
{
if (namelen == 8)
{
/* special case: just looking for any HIERARCH keyword */
hier = 1;
}
else
{
/* ignore the leading HIERARCH and look for the 'real' name */
/* starting with first non-blank character following HIERARCH */
ptr1 = keyname;
ptr2 = &keyname[8];
while(*ptr2 == ' ')
ptr2++;
namelen = 0;
while(*ptr2)
{
*ptr1 = *ptr2;
ptr1++;
ptr2++;
namelen++;
}
*ptr1 = '\0';
}
}
/* does input name contain wild card chars? ('?', '*', or '#') */
/* wild cards are currently not supported with HIERARCH keywords */
namelen_limit = namelen;
gotstar = 0;
if (namelen < 9 &&
(strchr(keyname,'?') || (gotstar = strchr(keyname,'*')) ||
strchr(keyname,'#')) )
{
wild = 1;
/* if we found a '*' wild card in the name, there might be */
/* more than one. Support up to 2 '*' in the template. */
/* Thus we need to compare keywords whose names have at least */
/* namelen - 2 characters. */
if (gotstar)
namelen_limit -= 2;
}
else
wild = 0;
ffghps(fptr, &nkeys, &nextkey, status); /* get no. keywords and position */
namelenminus1 = maxvalue(namelen - 1, 1);
ntodo = nkeys - nextkey + 1; /* first, read from next keyword to end */
for (jj=0; jj < 2; jj++)
{
for (kk = 0; kk < ntodo; kk++)
{
ffgnky(fptr, card, status); /* get next keyword */
if (hier)
{
if (FSTRNCMP("HIERARCH", card, 8) == 0)
return(*status); /* found a HIERARCH keyword */
}
else
{
ffgknm(card, cardname, &cardlen, status); /* get the keyword name */
if (cardlen >= namelen_limit) /* can't match if card < name */
{
/* if there are no wild cards, lengths must be the same */
if (!( !wild && cardlen != namelen) )
{
for (ii=0; ii < cardlen; ii++)
{
/* make sure keyword is in uppercase */
if (cardname[ii] > 96)
{
/* This assumes the ASCII character set in which */
/* upper case characters start at ASCII(97) */
/* Timing tests showed that this is 20% faster */
/* than calling the isupper function. */
cardname[ii] = toupper(cardname[ii]); /* make upper case */
}
}
if (wild)
{
ffcmps(keyname, cardname, 1, &match, &exact);
if (match)
return(*status); /* found a matching keyword */
}
else if (keyname[namelenminus1] == cardname[namelenminus1])
{
/* test the last character of the keyword name first, on */
/* the theory that it is less likely to match then the first */
/* character since many keywords begin with 'T', for example */
if (FSTRNCMP(keyname, cardname, namelenminus1) == 0)
{
return(*status); /* found the matching keyword */
}
}
else if (namelen == 0 && cardlen == 0)
{
/* matched a blank keyword */
return(*status);
}
}
}
}
}
if (wild || jj == 1)
break; /* stop at end of header if template contains wildcards */
ffmaky(fptr, 1, status); /* reset pointer to beginning of header */
ntodo = nextkey - 1; /* number of keyword to read */
}
return(*status = KEY_NO_EXIST); /* couldn't find the keyword */
}
/*--------------------------------------------------------------------------*/
int ffgstr( fitsfile *fptr, /* I - FITS file pointer */
const char *string, /* I - string to match */
char *card, /* O - keyword card */
int *status) /* IO - error status */
/*
Read (get) the next keyword record that contains the input character string,
returning the entire keyword card up to 80 characters long.
The returned card value is null terminated with any trailing blank
characters removed.
*/
{
int nkeys, nextkey, ntodo, stringlen;
int jj, kk;
if (*status > 0)
return(*status);
stringlen = strlen(string);
if (stringlen > 80) {
return(*status = KEY_NO_EXIST); /* matching string is too long to exist */
}
ffghps(fptr, &nkeys, &nextkey, status); /* get no. keywords and position */
ntodo = nkeys - nextkey + 1; /* first, read from next keyword to end */
for (jj=0; jj < 2; jj++)
{
for (kk = 0; kk < ntodo; kk++)
{
ffgnky(fptr, card, status); /* get next keyword */
if (strstr(card, string) != 0) {
return(*status); /* found the matching string */
}
}
ffmaky(fptr, 1, status); /* reset pointer to beginning of header */
ntodo = nextkey - 1; /* number of keyword to read */
}
return(*status = KEY_NO_EXIST); /* couldn't find the keyword */
}
/*--------------------------------------------------------------------------*/
int ffgknm( char *card, /* I - keyword card */
char *name, /* O - name of the keyword */
int *length, /* O - length of the keyword name */
int *status) /* IO - error status */
/*
Return the name of the keyword, and the name length. This supports the
ESO HIERARCH convention where keyword names may be > 8 characters long.
*/
{
char *ptr1, *ptr2;
int ii, namelength;
namelength = FLEN_KEYWORD - 1;
*name = '\0';
*length = 0;
/* support for ESO HIERARCH keywords; find the '=' */
if (FSTRNCMP(card, "HIERARCH ", 9) == 0)
{
ptr2 = strchr(card, '=');
if (!ptr2) /* no value indicator ??? */
{
/* this probably indicates an error, so just return FITS name */
strcat(name, "HIERARCH");
*length = 8;
return(*status);
}
/* find the start and end of the HIERARCH name */
ptr1 = &card[9];
while (*ptr1 == ' ') /* skip spaces */
ptr1++;
strncat(name, ptr1, ptr2 - ptr1);
ii = ptr2 - ptr1;
while (ii > 0 && name[ii - 1] == ' ') /* remove trailing spaces */
ii--;
name[ii] = '\0';
*length = ii;
}
else
{
for (ii = 0; ii < namelength; ii++)
{
/* look for string terminator, or a blank */
if (*(card+ii) != ' ' && *(card+ii) != '=' && *(card+ii) !='\0')
{
*(name+ii) = *(card+ii);
}
else
{
name[ii] = '\0';
*length = ii;
return(*status);
}
}
/* if we got here, keyword is namelength characters long */
name[namelength] = '\0';
*length = namelength;
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffgunt( fitsfile *fptr, /* I - FITS file pointer */
const char *keyname, /* I - name of keyword to read */
char *unit, /* O - keyword units */
int *status) /* IO - error status */
/*
Read (get) the units string from the comment field of the existing
keyword. This routine uses a local FITS convention (not defined in the
official FITS standard) in which the units are enclosed in
square brackets following the '/' comment field delimiter, e.g.:
KEYWORD = 12 / [kpc] comment string goes here
*/
{
char valstring[FLEN_VALUE];
char comm[FLEN_COMMENT];
char *loc;
if (*status > 0)
return(*status);
ffgkey(fptr, keyname, valstring, comm, status); /* read the keyword */
if (comm[0] == '[')
{
loc = strchr(comm, ']'); /* find the closing bracket */
if (loc)
*loc = '\0'; /* terminate the string */
strcpy(unit, &comm[1]); /* copy the string */
}
else
unit[0] = '\0';
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffgkys( fitsfile *fptr, /* I - FITS file pointer */
const char *keyname, /* I - name of keyword to read */
char *value, /* O - keyword value */
char *comm, /* O - keyword comment */
int *status) /* IO - error status */
/*
Get KeYword with a String value:
Read (get) a simple string valued keyword. The returned value may be up to
68 chars long ( + 1 null terminator char). The routine does not support the
HEASARC convention for continuing long string values over multiple keywords.
The ffgkls routine may be used to read long continued strings. The returned
comment string may be up to 69 characters long (including null terminator).
*/
{
char valstring[FLEN_VALUE];
if (*status > 0)
return(*status);
ffgkey(fptr, keyname, valstring, comm, status); /* read the keyword */
value[0] = '\0';
ffc2s(valstring, value, status); /* remove quotes from string */
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffgksl( fitsfile *fptr, /* I - FITS file pointer */
const char *keyname, /* I - name of keyword to read */
int *length, /* O - length of the string value */
int *status) /* IO - error status */
/*
Get the length of the keyword value string.
This routine explicitly supports the CONTINUE convention for long string values.
*/
{
int dummy=0;
if (*status > 0)
return(*status);
ffgkcsl(fptr, keyname, length, &dummy, status);
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffgkcsl( fitsfile *fptr, /* I - FITS file pointer */
const char *keyname, /* I - name of keyword to read */
int *length, /* O - length of the string value */
int *comlength, /* O - length of comment string */
int *status) /* IO - error status */
/*
Get the length of the keyword value string and comment string.
This routine explicitly supports the CONTINUE convention for long string values.
*/
{
if (*status > 0)
return(*status);
ffglkut(fptr, keyname, 0, 0, 0, (char *)0, length, (char *)0,
comlength, status);
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffgkls( fitsfile *fptr, /* I - FITS file pointer */
const char *keyname, /* I - name of keyword to read */
char **value, /* O - pointer to keyword value */
char *comm, /* O - keyword comment (may be NULL) */
int *status) /* IO - error status */
/*
This is the original routine for reading long string keywords that use
the CONTINUE keyword convention. In 2016 a new routine called
ffgsky / fits_read_string_key was added, which may provide a more
convenient user interface for most applications.
Get Keyword with possible Long String value:
Read (get) the named keyword, returning the value and comment.
The returned value string may be arbitrarily long (by using the HEASARC
convention for continuing long string values over multiple keywords) so
this routine allocates the required memory for the returned string value.
It is up to the calling routine to free the memory once it is finished
with the value string. The returned comment string may be up to 69
characters long.
*/
{
char valstring[FLEN_VALUE], nextcomm[FLEN_COMMENT], card[FLEN_CARD];
int contin, commSpace = 0, addCommDelim=0, keynum=0;
size_t len;
if (*status > 0)
return(*status);
*value = NULL; /* initialize a null pointer in case of error */
card[0] = '\0';
if (comm)
comm[0] = '\0';
ffgcrd(fptr, keyname, card, status);
if (*status > 0)
return(*status);
if (strlen(card) < FLEN_CARD-1)
addCommDelim=1;
ffpsvc(card,valstring, comm, status);
if (*status > 0)
return(*status);
if (comm)
{
/* remaining space in comment string */
commSpace = FLEN_COMMENT-1 - strlen(comm);
}
if (!valstring[0]) /* null value string? */
{
*value = (char *) malloc(1); /* allocate and return a null string */
**value = '\0';
}
else
{
/* allocate space, plus 1 for null */
*value = (char *) malloc(strlen(valstring) + 1);
ffc2s(valstring, *value, status); /* convert string to value */
len = strlen(*value);
/* If last character is a & then value may be continued on next keyword */
contin = 1;
while (contin)
{
if (len && *(*value+len-1) == '&') /* is last char an ampersand? */
{
valstring[0] = '\0';
nextcomm[0] = '\0';
ffgcnt(fptr, valstring, nextcomm, status);
if (*valstring || *nextcomm) /* If either valstring or nextcom
is filled, this must be a CONTINUE line */
{
*(*value+len-1) = '\0'; /* erase the trailing & char */
if (*valstring)
{
len += strlen(valstring) - 1;
*value = (char *) realloc(*value, len + 1); /* increase size */
strcat(*value, valstring); /* append the continued chars */
}
if (*nextcomm)
{
if ((commSpace > 0) && (*nextcomm != 0))
{
/* If in here, input 'comm' cannot be 0 */
/* concantenate comment strings (if any) */
if (strlen(comm) && addCommDelim)
{
strcat(comm, " ");
commSpace -=1;
}
strncat(comm, nextcomm, commSpace);
commSpace = FLEN_COMMENT-1 - strlen(comm);
}
}
/* Determine if a space delimiter is needed for next
comment concatenation (if any). Assume it is if card length
of the most recently read keyword is less than max.
keynum is 1-based. */
ffghps(fptr,0,&keynum,status);
ffgrec(fptr, keynum-1, card, status);
addCommDelim = (strlen(card) < FLEN_CARD-1) ? 1 : 0;
}
else
{
contin = 0;
}
}
else
{
contin = 0;
}
}
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffgsky( fitsfile *fptr, /* I - FITS file pointer */
const char *keyname, /* I - name of keyword to read */
int firstchar, /* I - first character of string to return */
int maxchar, /* I - maximum length of string to return */
/* (string will be null terminated) */
char *value, /* O - pointer to keyword value */
int *valuelen, /* O - total length of the keyword value string */
/* The returned 'value' string may only */
/* contain a piece of the total string, depending */
/* on the value of firstchar and maxchar */
char *comm, /* O - keyword comment (may be NULL) */
int *status) /* IO - error status */
/*
Read and return the value of the specified string-valued keyword.
This new routine was added in 2016 to provide a more convenient user
interface than the older ffgkls routine.
Read a string keyword, returning up to 'naxchars' characters of the value
starting with the 'firstchar' character.
The input 'value' string must be allocated at least 1 char bigger to
allow for the terminating null character.
This routine may be used to read continued string keywords that use
the CONTINUE keyword convention, as well as normal string keywords
that are contained within a single header record.
This routine differs from the ffkls routine in that it does not
internally allocate memory for the returned value string, and consequently
the calling routine does not need to call fffree to free the memory.
*/