This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
modkey.c
1842 lines (1550 loc) · 65.7 KB
/
modkey.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, modkey.c, contains routines that modify, insert, or update */
/* keywords in 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>
/* stddef.h is apparently needed to define size_t */
#include <ctype.h>
#include <stddef.h>
#include <stdlib.h>
#include "fitsio2.h"
/*--------------------------------------------------------------------------*/
int ffuky( fitsfile *fptr, /* I - FITS file pointer */
int datatype, /* I - datatype of the value */
const char *keyname,/* I - name of keyword to write */
void *value, /* I - keyword value */
const char *comm, /* I - keyword comment */
int *status) /* IO - error status */
/*
Update the keyword, value and comment in the FITS header.
The datatype is specified by the 2nd argument.
*/
{
if (*status > 0) /* inherit input status value if > 0 */
return(*status);
if (datatype == TSTRING)
{
ffukys(fptr, keyname, (char *) value, comm, status);
}
else if (datatype == TBYTE)
{
ffukyj(fptr, keyname, (LONGLONG) *(unsigned char *) value, comm, status);
}
else if (datatype == TSBYTE)
{
ffukyj(fptr, keyname, (LONGLONG) *(signed char *) value, comm, status);
}
else if (datatype == TUSHORT)
{
ffukyj(fptr, keyname, (LONGLONG) *(unsigned short *) value, comm, status);
}
else if (datatype == TSHORT)
{
ffukyj(fptr, keyname, (LONGLONG) *(short *) value, comm, status);
}
else if (datatype == TINT)
{
ffukyj(fptr, keyname, (LONGLONG) *(int *) value, comm, status);
}
else if (datatype == TUINT)
{
ffukyg(fptr, keyname, (double) *(unsigned int *) value, 0,
comm, status);
}
else if (datatype == TLOGICAL)
{
ffukyl(fptr, keyname, *(int *) value, comm, status);
}
else if (datatype == TULONG)
{
ffukyg(fptr, keyname, (double) *(unsigned long *) value, 0,
comm, status);
}
else if (datatype == TLONG)
{
ffukyj(fptr, keyname, (LONGLONG) *(long *) value, comm, status);
}
else if (datatype == TLONGLONG)
{
ffukyj(fptr, keyname, *(LONGLONG *) value, comm, status);
}
else if (datatype == TFLOAT)
{
ffukye(fptr, keyname, *(float *) value, -7, comm, status);
}
else if (datatype == TDOUBLE)
{
ffukyd(fptr, keyname, *(double *) value, -15, comm, status);
}
else if (datatype == TCOMPLEX)
{
ffukyc(fptr, keyname, (float *) value, -7, comm, status);
}
else if (datatype == TDBLCOMPLEX)
{
ffukym(fptr, keyname, (double *) value, -15, comm, status);
}
else
*status = BAD_DATATYPE;
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffukyu(fitsfile *fptr, /* I - FITS file pointer */
const char *keyname, /* I - keyword name */
const char *comm, /* I - keyword comment */
int *status) /* IO - error status */
{
int tstatus;
if (*status > 0) /* inherit input status value if > 0 */
return(*status);
tstatus = *status;
if (ffmkyu(fptr, keyname, comm, status) == KEY_NO_EXIST)
{
*status = tstatus;
ffpkyu(fptr, keyname, comm, status);
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffukys(fitsfile *fptr, /* I - FITS file pointer */
const char *keyname, /* I - keyword name */
const char *value, /* I - keyword value */
const char *comm, /* I - keyword comment */
int *status) /* IO - error status */
{
int tstatus;
if (*status > 0) /* inherit input status value if > 0 */
return(*status);
tstatus = *status;
if (ffmkys(fptr, keyname, value, comm, status) == KEY_NO_EXIST)
{
*status = tstatus;
ffpkys(fptr, keyname, value, comm, status);
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffukls(fitsfile *fptr, /* I - FITS file pointer */
const char *keyname, /* I - keyword name */
const char *value, /* I - keyword value */
const char *comm, /* I - keyword comment */
int *status) /* IO - error status */
{
/* update a long string keyword */
int tstatus;
char junk[FLEN_ERRMSG];
if (*status > 0) /* inherit input status value if > 0 */
return(*status);
tstatus = *status;
if (ffmkls(fptr, keyname, value, comm, status) == KEY_NO_EXIST)
{
/* since the ffmkls call failed, it wrote a bogus error message */
fits_read_errmsg(junk); /* clear the error message */
*status = tstatus;
ffpkls(fptr, keyname, value, comm, status);
}
return(*status);
}/*--------------------------------------------------------------------------*/
int ffukyl(fitsfile *fptr, /* I - FITS file pointer */
const char *keyname,/* I - keyword name */
int value, /* I - keyword value */
const char *comm, /* I - keyword comment */
int *status) /* IO - error status */
{
int tstatus;
if (*status > 0) /* inherit input status value if > 0 */
return(*status);
tstatus = *status;
if (ffmkyl(fptr, keyname, value, comm, status) == KEY_NO_EXIST)
{
*status = tstatus;
ffpkyl(fptr, keyname, value, comm, status);
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffukyj(fitsfile *fptr, /* I - FITS file pointer */
const char *keyname,/* I - keyword name */
LONGLONG value, /* I - keyword value */
const char *comm, /* I - keyword comment */
int *status) /* IO - error status */
{
int tstatus;
if (*status > 0) /* inherit input status value if > 0 */
return(*status);
tstatus = *status;
if (ffmkyj(fptr, keyname, value, comm, status) == KEY_NO_EXIST)
{
*status = tstatus;
ffpkyj(fptr, keyname, value, comm, status);
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffukyf(fitsfile *fptr, /* I - FITS file pointer */
const char *keyname,/* I - keyword name */
float value, /* I - keyword value */
int decim, /* I - no of decimals */
const char *comm, /* I - keyword comment */
int *status) /* IO - error status */
{
int tstatus;
if (*status > 0) /* inherit input status value if > 0 */
return(*status);
tstatus = *status;
if (ffmkyf(fptr, keyname, value, decim, comm, status) == KEY_NO_EXIST)
{
*status = tstatus;
ffpkyf(fptr, keyname, value, decim, comm, status);
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffukye(fitsfile *fptr, /* I - FITS file pointer */
const char *keyname,/* I - keyword name */
float value, /* I - keyword value */
int decim, /* I - no of decimals */
const char *comm, /* I - keyword comment */
int *status) /* IO - error status */
{
int tstatus;
if (*status > 0) /* inherit input status value if > 0 */
return(*status);
tstatus = *status;
if (ffmkye(fptr, keyname, value, decim, comm, status) == KEY_NO_EXIST)
{
*status = tstatus;
ffpkye(fptr, keyname, value, decim, comm, status);
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffukyg(fitsfile *fptr, /* I - FITS file pointer */
const char *keyname,/* I - keyword name */
double value, /* I - keyword value */
int decim, /* I - no of decimals */
const char *comm, /* I - keyword comment */
int *status) /* IO - error status */
{
int tstatus;
if (*status > 0) /* inherit input status value if > 0 */
return(*status);
tstatus = *status;
if (ffmkyg(fptr, keyname, value, decim, comm, status) == KEY_NO_EXIST)
{
*status = tstatus;
ffpkyg(fptr, keyname, value, decim, comm, status);
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffukyd(fitsfile *fptr, /* I - FITS file pointer */
const char *keyname,/* I - keyword name */
double value, /* I - keyword value */
int decim, /* I - no of decimals */
const char *comm, /* I - keyword comment */
int *status) /* IO - error status */
{
int tstatus;
if (*status > 0) /* inherit input status value if > 0 */
return(*status);
tstatus = *status;
if (ffmkyd(fptr, keyname, value, decim, comm, status) == KEY_NO_EXIST)
{
*status = tstatus;
ffpkyd(fptr, keyname, value, decim, comm, status);
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffukfc(fitsfile *fptr, /* I - FITS file pointer */
const char *keyname,/* I - keyword name */
float *value, /* I - keyword value */
int decim, /* I - no of decimals */
const char *comm, /* I - keyword comment */
int *status) /* IO - error status */
{
int tstatus;
if (*status > 0) /* inherit input status value if > 0 */
return(*status);
tstatus = *status;
if (ffmkfc(fptr, keyname, value, decim, comm, status) == KEY_NO_EXIST)
{
*status = tstatus;
ffpkfc(fptr, keyname, value, decim, comm, status);
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffukyc(fitsfile *fptr, /* I - FITS file pointer */
const char *keyname,/* I - keyword name */
float *value, /* I - keyword value */
int decim, /* I - no of decimals */
const char *comm, /* I - keyword comment */
int *status) /* IO - error status */
{
int tstatus;
if (*status > 0) /* inherit input status value if > 0 */
return(*status);
tstatus = *status;
if (ffmkyc(fptr, keyname, value, decim, comm, status) == KEY_NO_EXIST)
{
*status = tstatus;
ffpkyc(fptr, keyname, value, decim, comm, status);
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffukfm(fitsfile *fptr, /* I - FITS file pointer */
const char *keyname,/* I - keyword name */
double *value, /* I - keyword value */
int decim, /* I - no of decimals */
const char *comm, /* I - keyword comment */
int *status) /* IO - error status */
{
int tstatus;
if (*status > 0) /* inherit input status value if > 0 */
return(*status);
tstatus = *status;
if (ffmkfm(fptr, keyname, value, decim, comm, status) == KEY_NO_EXIST)
{
*status = tstatus;
ffpkfm(fptr, keyname, value, decim, comm, status);
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffukym(fitsfile *fptr, /* I - FITS file pointer */
const char *keyname,/* I - keyword name */
double *value, /* I - keyword value */
int decim, /* I - no of decimals */
const char *comm, /* I - keyword comment */
int *status) /* IO - error status */
{
int tstatus;
if (*status > 0) /* inherit input status value if > 0 */
return(*status);
tstatus = *status;
if (ffmkym(fptr, keyname, value, decim, comm, status) == KEY_NO_EXIST)
{
*status = tstatus;
ffpkym(fptr, keyname, value, decim, comm, status);
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffucrd(fitsfile *fptr, /* I - FITS file pointer */
const char *keyname,/* I - keyword name */
const char *card, /* I - card string value */
int *status) /* IO - error status */
{
int tstatus;
if (*status > 0) /* inherit input status value if > 0 */
return(*status);
tstatus = *status;
if (ffmcrd(fptr, keyname, card, status) == KEY_NO_EXIST)
{
*status = tstatus;
ffprec(fptr, card, status);
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffmrec(fitsfile *fptr, /* I - FITS file pointer */
int nkey, /* I - number of the keyword to modify */
const char *card, /* I - card string value */
int *status) /* IO - error status */
{
if (*status > 0) /* inherit input status value if > 0 */
return(*status);
ffmaky(fptr, nkey+1, status);
ffmkey(fptr, card, status);
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffmcrd(fitsfile *fptr, /* I - FITS file pointer */
const char *keyname, /* I - keyword name */
const char *card, /* I - card string value */
int *status) /* IO - error status */
{
char tcard[FLEN_CARD], valstring[FLEN_CARD], comm[FLEN_CARD], value[FLEN_CARD];
char nextcomm[FLEN_COMMENT];
int keypos, len;
if (*status > 0) /* inherit input status value if > 0 */
return(*status);
if (ffgcrd(fptr, keyname, tcard, status) > 0)
return(*status);
ffmkey(fptr, card, status);
/* calc position of keyword in header */
keypos = (int) ((((fptr->Fptr)->nextkey) - ((fptr->Fptr)->headstart[(fptr->Fptr)->curhdu])) / 80) + 1;
ffpsvc(tcard, valstring, comm, status);
if (*status > 0) /* inherit input status value if > 0 */
return(*status);
/* check for string value which may be continued over multiple keywords */
ffpmrk(); /* put mark on message stack; erase any messages after this */
ffc2s(valstring, value, status); /* remove quotes and trailing spaces */
if (*status == VALUE_UNDEFINED) {
ffcmrk(); /* clear any spurious error messages, back to the mark */
*status = 0;
} else {
len = strlen(value);
while (len && value[len - 1] == '&') /* ampersand used as continuation char */
{
ffgcnt(fptr, value, nextcomm, status);
if (*value)
{
ffdrec(fptr, keypos, status); /* delete the keyword */
len = strlen(value);
}
else /* a null valstring indicates no continuation */
len = 0;
}
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffmnam(fitsfile *fptr, /* I - FITS file pointer */
const char *oldname,/* I - existing keyword name */
const char *newname,/* I - new name for keyword */
int *status) /* IO - error status */
{
char comm[FLEN_COMMENT];
char value[FLEN_VALUE];
char card[FLEN_CARD];
if (*status > 0) /* inherit input status value if > 0 */
return(*status);
if (ffgkey(fptr, oldname, value, comm, status) > 0)
return(*status);
ffmkky(newname, value, comm, card, status); /* construct the card */
ffmkey(fptr, card, status); /* rewrite with new name */
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffmcom(fitsfile *fptr, /* I - FITS file pointer */
const char *keyname,/* I - keyword name */
const char *comm, /* I - keyword comment */
int *status) /* IO - error status */
{
char oldcomm[FLEN_COMMENT];
char value[FLEN_VALUE];
char card[FLEN_CARD];
if (*status > 0) /* inherit input status value if > 0 */
return(*status);
if (ffgkey(fptr, keyname, value, oldcomm, status) > 0)
return(*status);
ffmkky(keyname, value, comm, card, status); /* construct the card */
ffmkey(fptr, card, status); /* rewrite with new comment */
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffpunt(fitsfile *fptr, /* I - FITS file pointer */
const char *keyname,/* I - keyword name */
const char *unit, /* I - keyword unit string */
int *status) /* IO - error status */
/*
Write (put) the units string into the comment field of the existing keyword.
This routine uses a FITS convention in which the units are enclosed in
square brackets following the '/' comment field delimiter, e.g.:
KEYWORD = 12 / [kpc] comment string goes here
*/
{
char oldcomm[FLEN_COMMENT];
char newcomm[FLEN_COMMENT];
char value[FLEN_VALUE];
char card[FLEN_CARD];
char *loc;
size_t len;
if (*status > 0) /* inherit input status value if > 0 */
return(*status);
if (ffgkey(fptr, keyname, value, oldcomm, status) > 0)
return(*status);
/* copy the units string to the new comment string if not null */
if (*unit)
{
strcpy(newcomm, "[");
strncat(newcomm, unit, 45); /* max allowed length is about 45 chars */
strcat(newcomm, "] ");
len = strlen(newcomm);
len = FLEN_COMMENT - len - 1; /* amount of space left in the field */
}
else
{
newcomm[0] = '\0';
len = FLEN_COMMENT - 1;
}
if (oldcomm[0] == '[') /* check for existing units field */
{
loc = strchr(oldcomm, ']'); /* look for the closing bracket */
if (loc)
{
loc++;
while (*loc == ' ') /* skip any blank spaces */
loc++;
strncat(newcomm, loc, len); /* concat remainder of comment */
}
else
{
strncat(newcomm, oldcomm, len); /* append old comment onto new */
}
}
else
{
strncat(newcomm, oldcomm, len);
}
ffmkky(keyname, value, newcomm, card, status); /* construct the card */
ffmkey(fptr, card, status); /* rewrite with new units string */
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffmkyu(fitsfile *fptr, /* I - FITS file pointer */
const char *keyname,/* I - keyword name */
const char *comm, /* I - keyword comment */
int *status) /* IO - error status */
{
char valstring[FLEN_VALUE];
char oldcomm[FLEN_COMMENT];
char card[FLEN_CARD];
if (*status > 0) /* inherit input status value if > 0 */
return(*status);
if (ffgkey(fptr, keyname, valstring, oldcomm, status) > 0)
return(*status); /* get old comment */
strcpy(valstring," "); /* create a dummy value string */
if (!comm || comm[0] == '&') /* preserve the current comment string */
ffmkky(keyname, valstring, oldcomm, card, status);
else
ffmkky(keyname, valstring, comm, card, status);
ffmkey(fptr, card, status);
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffmkys(fitsfile *fptr, /* I - FITS file pointer */
const char *keyname, /* I - keyword name */
const char *value, /* I - keyword value */
const char *comm, /* I - keyword comment */
int *status) /* IO - error status */
{
/* NOTE: This routine does not support long continued strings */
/* It will correctly overwrite an existing long continued string, */
/* but it will not write a new long string. */
char oldval[FLEN_VALUE], valstring[FLEN_VALUE];
char oldcomm[FLEN_COMMENT];
char card[FLEN_CARD], nextcomm[FLEN_COMMENT];
int len, keypos;
if (*status > 0) /* inherit input status value if > 0 */
return(*status);
if (ffgkey(fptr, keyname, oldval, oldcomm, status) > 0)
return(*status); /* get old comment */
ffs2c(value, valstring, status); /* convert value to a string */
if (!comm || comm[0] == '&') /* preserve the current comment string */
ffmkky(keyname, valstring, oldcomm, card, status);
else
ffmkky(keyname, valstring, comm, card, status);
ffmkey(fptr, card, status); /* overwrite the previous keyword */
keypos = (int) (((((fptr->Fptr)->nextkey) - ((fptr->Fptr)->headstart[(fptr->Fptr)->curhdu])) / 80) + 1);
if (*status > 0)
return(*status);
/* check if old string value was continued over multiple keywords */
ffpmrk(); /* put mark on message stack; erase any messages after this */
ffc2s(oldval, valstring, status); /* remove quotes and trailing spaces */
if (*status == VALUE_UNDEFINED) {
ffcmrk(); /* clear any spurious error messages, back to the mark */
*status = 0;
} else {
len = strlen(valstring);
while (len && valstring[len - 1] == '&') /* ampersand is continuation char */
{
ffgcnt(fptr, valstring, nextcomm, status);
if (*valstring)
{
ffdrec(fptr, keypos, status); /* delete the continuation */
len = strlen(valstring);
}
else /* a null valstring indicates no continuation */
len = 0;
}
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffmkls( fitsfile *fptr, /* I - FITS file pointer */
const char *keyname, /* I - name of keyword to write */
const char *value, /* I - keyword value */
const char *incomm, /* I - keyword comment */
int *status) /* IO - error status */
/*
Modify the value and optionally the comment of a long string keyword.
This routine supports the
HEASARC long string convention and can modify arbitrarily long string
keyword values. The value is continued over multiple keywords that
have the name COMTINUE without an equal sign in column 9 of the card.
This routine also supports simple string keywords which are less than
69 characters in length.
This routine is not very efficient, so it should be used sparingly.
*/
{
char valstring[FLEN_VALUE];
char card[FLEN_CARD], tmpkeyname[FLEN_CARD];
char comm[FLEN_COMMENT];
char tstring[FLEN_VALUE], *cptr;
char *longval;
int next, remain, vlen, nquote, nchar, namelen, contin, tstatus = -1;
int nkeys, keypos;
if (*status > 0) /* inherit input status value if > 0 */
return(*status);
if (!incomm || incomm[0] == '&') /* preserve the old comment string */
{
ffghps(fptr, &nkeys, &keypos, status); /* save current position */
if (ffgkls(fptr, keyname, &longval, comm, status) > 0)
return(*status); /* keyword doesn't exist */
free(longval); /* don't need the old value */
/* move back to previous position to ensure that we delete */
/* the right keyword in case there are more than one keyword */
/* with this same name. */
ffgrec(fptr, keypos - 1, card, status);
} else {
/* copy the input comment string */
strncpy(comm, incomm, FLEN_COMMENT-1);
comm[FLEN_COMMENT-1] = '\0';
}
/* delete the old keyword */
if (ffdkey(fptr, keyname, status) > 0)
return(*status); /* keyword doesn't exist */
ffghps(fptr, &nkeys, &keypos, status); /* save current position */
/* now construct the new keyword, and insert into header */
remain = strlen(value); /* number of characters to write out */
next = 0; /* pointer to next character to write */
/* count the number of single quote characters in the string */
nquote = 0;
cptr = strchr(value, '\''); /* search for quote character */
while (cptr) /* search for quote character */
{
nquote++; /* increment no. of quote characters */
cptr++; /* increment pointer to next character */
cptr = strchr(cptr, '\''); /* search for another quote char */
}
strncpy(tmpkeyname, keyname, 80);
tmpkeyname[80] = '\0';
cptr = tmpkeyname;
while(*cptr == ' ') /* skip over leading spaces in name */
cptr++;
/* determine the number of characters that will fit on the line */
/* Note: each quote character is expanded to 2 quotes */
namelen = strlen(cptr);
if (namelen <= 8 && (fftkey(cptr, &tstatus) <= 0) )
{
/* This a normal 8-character FITS keyword */
nchar = 68 - nquote; /* max of 68 chars fit in a FITS string value */
}
else
{
nchar = 80 - nquote - namelen - 5;
}
contin = 0;
while (remain > 0)
{
if (nchar > FLEN_VALUE-1)
{
ffpmsg("longstr keyword value is too long (ffmkls)");
return (*status=BAD_KEYCHAR);
}
strncpy(tstring, &value[next], nchar); /* copy string to temp buff */
tstring[nchar] = '\0';
ffs2c(tstring, valstring, status); /* put quotes around the string */
if (remain > nchar) /* if string is continued, put & as last char */
{
vlen = strlen(valstring);
nchar -= 1; /* outputting one less character now */
if (valstring[vlen-2] != '\'')
valstring[vlen-2] = '&'; /* over write last char with & */
else
{ /* last char was a pair of single quotes, so over write both */
valstring[vlen-3] = '&';
valstring[vlen-1] = '\0';
}
}
if (contin) /* This is a CONTINUEd keyword */
{
ffmkky("CONTINUE", valstring, comm, card, status); /* make keyword */
strncpy(&card[8], " ", 2); /* overwrite the '=' */
}
else
{
ffmkky(keyname, valstring, comm, card, status); /* make keyword */
}
ffirec(fptr, keypos, card, status); /* insert the keyword */
keypos++; /* next insert position */
contin = 1;
remain -= nchar;
next += nchar;
nchar = 68 - nquote;
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffmkyl(fitsfile *fptr, /* I - FITS file pointer */
const char *keyname, /* I - keyword name */
int value, /* I - keyword value */
const char *comm, /* I - keyword comment */
int *status) /* IO - error status */
{
char valstring[FLEN_VALUE];
char oldcomm[FLEN_COMMENT];
char card[FLEN_CARD];
if (*status > 0) /* inherit input status value if > 0 */
return(*status);
if (ffgkey(fptr, keyname, valstring, oldcomm, status) > 0)
return(*status); /* get old comment */
ffl2c(value, valstring, status); /* convert value to a string */
if (!comm || comm[0] == '&') /* preserve the current comment string */
ffmkky(keyname, valstring, oldcomm, card, status);
else
ffmkky(keyname, valstring, comm, card, status);
ffmkey(fptr, card, status);
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffmkyj(fitsfile *fptr, /* I - FITS file pointer */
const char *keyname, /* I - keyword name */
LONGLONG value, /* I - keyword value */
const char *comm, /* I - keyword comment */
int *status) /* IO - error status */
{
char valstring[FLEN_VALUE];
char oldcomm[FLEN_COMMENT];
char card[FLEN_CARD];
if (*status > 0) /* inherit input status value if > 0 */
return(*status);
if (ffgkey(fptr, keyname, valstring, oldcomm, status) > 0)
return(*status); /* get old comment */
ffi2c(value, valstring, status); /* convert value to a string */
if (!comm || comm[0] == '&') /* preserve the current comment string */
ffmkky(keyname, valstring, oldcomm, card, status);
else
ffmkky(keyname, valstring, comm, card, status);
ffmkey(fptr, card, status);
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffmkyf(fitsfile *fptr, /* I - FITS file pointer */
const char *keyname, /* I - keyword name */
float value, /* I - keyword value */
int decim, /* I - no of decimals */
const char *comm, /* I - keyword comment */
int *status) /* IO - error status */
{
char valstring[FLEN_VALUE];
char oldcomm[FLEN_COMMENT];
char card[FLEN_CARD];
if (*status > 0) /* inherit input status value if > 0 */
return(*status);
if (ffgkey(fptr, keyname, valstring, oldcomm, status) > 0)
return(*status); /* get old comment */
ffr2f(value, decim, valstring, status); /* convert value to a string */
if (!comm || comm[0] == '&') /* preserve the current comment string */
ffmkky(keyname, valstring, oldcomm, card, status);
else
ffmkky(keyname, valstring, comm, card, status);
ffmkey(fptr, card, status);
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffmkye(fitsfile *fptr, /* I - FITS file pointer */
const char *keyname, /* I - keyword name */
float value, /* I - keyword value */
int decim, /* I - no of decimals */
const char *comm, /* I - keyword comment */
int *status) /* IO - error status */
{
char valstring[FLEN_VALUE];
char oldcomm[FLEN_COMMENT];
char card[FLEN_CARD];
if (*status > 0) /* inherit input status value if > 0 */
return(*status);
if (ffgkey(fptr, keyname, valstring, oldcomm, status) > 0)
return(*status); /* get old comment */
ffr2e(value, decim, valstring, status); /* convert value to a string */
if (!comm || comm[0] == '&') /* preserve the current comment string */
ffmkky(keyname, valstring, oldcomm, card, status);
else
ffmkky(keyname, valstring, comm, card, status);
ffmkey(fptr, card, status);
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffmkyg(fitsfile *fptr, /* I - FITS file pointer */
const char *keyname, /* I - keyword name */
double value, /* I - keyword value */
int decim, /* I - no of decimals */
const char *comm, /* I - keyword comment */
int *status) /* IO - error status */
{
char valstring[FLEN_VALUE];
char oldcomm[FLEN_COMMENT];
char card[FLEN_CARD];
if (*status > 0) /* inherit input status value if > 0 */
return(*status);
if (ffgkey(fptr, keyname, valstring, oldcomm, status) > 0)
return(*status); /* get old comment */
ffd2f(value, decim, valstring, status); /* convert value to a string */
if (!comm || comm[0] == '&') /* preserve the current comment string */
ffmkky(keyname, valstring, oldcomm, card, status);
else
ffmkky(keyname, valstring, comm, card, status);
ffmkey(fptr, card, status);
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffmkyd(fitsfile *fptr, /* I - FITS file pointer */
const char *keyname, /* I - keyword name */
double value, /* I - keyword value */
int decim, /* I - no of decimals */
const char *comm, /* I - keyword comment */
int *status) /* IO - error status */
{
char valstring[FLEN_VALUE];
char oldcomm[FLEN_COMMENT];
char card[FLEN_CARD];
if (*status > 0) /* inherit input status value if > 0 */
return(*status);
if (ffgkey(fptr, keyname, valstring, oldcomm, status) > 0)
return(*status); /* get old comment */
ffd2e(value, decim, valstring, status); /* convert value to a string */
if (!comm || comm[0] == '&') /* preserve the current comment string */
ffmkky(keyname, valstring, oldcomm, card, status);
else
ffmkky(keyname, valstring, comm, card, status);
ffmkey(fptr, card, status);
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffmkfc(fitsfile *fptr, /* I - FITS file pointer */
const char *keyname, /* I - keyword name */
float *value, /* I - keyword value */
int decim, /* I - no of decimals */
const char *comm, /* I - keyword comment */
int *status) /* IO - error status */
{
char valstring[FLEN_VALUE], tmpstring[FLEN_VALUE];
char oldcomm[FLEN_COMMENT];
char card[FLEN_CARD];
if (*status > 0) /* inherit input status value if > 0 */
return(*status);
if (ffgkey(fptr, keyname, valstring, oldcomm, status) > 0)
return(*status); /* get old comment */
strcpy(valstring, "(" );
ffr2f(value[0], decim, tmpstring, status); /* convert to string */
if (strlen(tmpstring)+3 > FLEN_VALUE-1)
{
ffpmsg("complex key value too long (ffmkfc)");
return(*status=BAD_F2C);
}
strcat(valstring, tmpstring);
strcat(valstring, ", ");
ffr2f(value[1], decim, tmpstring, status); /* convert to string */
if (strlen(valstring) + strlen(tmpstring)+1 > FLEN_VALUE-1)