-
Notifications
You must be signed in to change notification settings - Fork 10
/
group.c
6736 lines (5247 loc) · 181 KB
/
group.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, group.c, contains the grouping convention suport routines. */
/* 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. */
/* */
/* The group.c module of CFITSIO was written by Donald G. Jennings of */
/* the INTEGRAL Science Data Centre (ISDC) under NASA contract task */
/* 66002J6. The above copyright laws apply. Copyright guidelines of The */
/* University of Geneva might also apply. */
/* The following routines are designed to create, read, and manipulate */
/* FITS Grouping Tables as defined in the FITS Grouping Convention paper */
/* by Jennings, Pence, Folk and Schlesinger. The development of the */
/* grouping structure was partially funded under the NASA AISRP Program. */
#include "fitsio2.h"
#include "group.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#if defined(WIN32) || defined(__WIN32__)
#include <direct.h> /* defines the getcwd function on Windows PCs */
#endif
#if defined(unix) || defined(__unix__) || defined(__unix) || defined(HAVE_UNISTD_H)
#include <unistd.h> /* needed for getcwd prototype on unix machines */
#endif
#define HEX_ESCAPE '%'
/*---------------------------------------------------------------------------
Change record:
D. Jennings, 18/06/98, version 1.0 of group module delivered to B. Pence for
integration into CFITSIO 2.005
D. Jennings, 17/11/98, fixed bug in ffgtcpr(). Now use fits_find_nextkey()
correctly and insert auxiliary keyword records
directly before the TTYPE1 keyword in the copied
group table.
D. Jennings, 22/01/99, ffgmop() now looks for relative file paths when
the MEMBER_LOCATION information is given in a
grouping table.
D. Jennings, 01/02/99, ffgtop() now looks for relatve file paths when
the GRPLCn keyword value is supplied in the member
HDU header.
D. Jennings, 01/02/99, ffgtam() now trys to construct relative file paths
from the member's file to the group table's file
(and visa versa) when both the member's file and
group table file are of access type FILE://.
D. Jennings, 05/05/99, removed the ffgtcn() function; made obsolete by
fits_get_url().
D. Jennings, 05/05/99, updated entire module to handle partial URLs and
absolute URLs more robustly. Host dependent directory
paths are now converted to true URLs before being
read from/written to grouping tables.
D. Jennings, 05/05/99, added the following new functions (note, none of these
are directly callable by the application)
int fits_path2url()
int fits_url2path()
int fits_get_cwd()
int fits_get_url()
int fits_clean_url()
int fits_relurl2url()
int fits_encode_url()
int fits_unencode_url()
int fits_is_url_absolute()
-----------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
int ffgtcr(fitsfile *fptr, /* FITS file pointer */
char *grpname, /* name of the grouping table */
int grouptype, /* code specifying the type of
grouping table information:
GT_ID_ALL_URI 0 ==> defualt (all columns)
GT_ID_REF 1 ==> ID by reference
GT_ID_POS 2 ==> ID by position
GT_ID_ALL 3 ==> ID by ref. and position
GT_ID_REF_URI 11 ==> (1) + URI info
GT_ID_POS_URI 12 ==> (2) + URI info */
int *status )/* return status code */
/*
create a grouping table at the end of the current FITS file. This
function makes the last HDU in the file the CHDU, then calls the
fits_insert_group() function to actually create the new grouping table.
*/
{
int hdutype;
int hdunum;
if(*status != 0) return(*status);
*status = fits_get_num_hdus(fptr,&hdunum,status);
/* If hdunum is 0 then we are at the beginning of the file and
we actually haven't closed the first header yet, so don't do
anything more */
if (0 != hdunum) {
*status = fits_movabs_hdu(fptr,hdunum,&hdutype,status);
}
/* Now, the whole point of the above two fits_ calls was to get to
the end of file. Let's ignore errors at this point and keep
going since any error is likely to mean that we are already at the
EOF, or the file is fatally corrupted. If we are at the EOF then
the next fits_ call will be ok. If it's corrupted then the
next call will fail, but that's not big deal at this point.
*/
if (0 != *status ) *status = 0;
*status = fits_insert_group(fptr,grpname,grouptype,status);
return(*status);
}
/*---------------------------------------------------------------------------*/
int ffgtis(fitsfile *fptr, /* FITS file pointer */
char *grpname, /* name of the grouping table */
int grouptype, /* code specifying the type of
grouping table information:
GT_ID_ALL_URI 0 ==> defualt (all columns)
GT_ID_REF 1 ==> ID by reference
GT_ID_POS 2 ==> ID by position
GT_ID_ALL 3 ==> ID by ref. and position
GT_ID_REF_URI 11 ==> (1) + URI info
GT_ID_POS_URI 12 ==> (2) + URI info */
int *status) /* return status code */
/*
insert a grouping table just after the current HDU of the current FITS file.
This is the same as fits_create_group() only it allows the user to select
the place within the FITS file to add the grouping table.
*/
{
int tfields = 0;
int hdunum = 0;
int hdutype = 0;
int extver;
int i;
long pcount = 0;
char *ttype[6];
char *tform[6];
char ttypeBuff[102];
char tformBuff[54];
char extname[] = "GROUPING";
char keyword[FLEN_KEYWORD];
char keyvalue[FLEN_VALUE];
char comment[FLEN_COMMENT];
do
{
/* set up the ttype and tform character buffers */
for(i = 0; i < 6; ++i)
{
ttype[i] = ttypeBuff+(i*17);
tform[i] = tformBuff+(i*9);
}
/* define the columns required according to the grouptype parameter */
*status = ffgtdc(grouptype,0,0,0,0,0,0,ttype,tform,&tfields,status);
/* create the grouping table using the columns defined above */
*status = fits_insert_btbl(fptr,0,tfields,ttype,tform,NULL,
NULL,pcount,status);
if(*status != 0) continue;
/*
retrieve the hdu position of the new grouping table for
future use
*/
fits_get_hdu_num(fptr,&hdunum);
/*
add the EXTNAME and EXTVER keywords to the HDU just after the
TFIELDS keyword; for now the EXTVER value is set to 0, it will be
set to the correct value later on
*/
fits_read_keyword(fptr,"TFIELDS",keyvalue,comment,status);
fits_insert_key_str(fptr,"EXTNAME",extname,
"HDU contains a Grouping Table",status);
fits_insert_key_lng(fptr,"EXTVER",0,"Grouping Table vers. (this file)",
status);
/*
if the grpname parameter value was defined (Non NULL and non zero
length) then add the GRPNAME keyword and value
*/
if(grpname != NULL && strlen(grpname) > 0)
fits_insert_key_str(fptr,"GRPNAME",grpname,"Grouping Table name",
status);
/*
add the TNULL keywords and values for each integer column defined;
integer null values are zero (0) for the MEMBER_POSITION and
MEMBER_VERSION columns.
*/
for(i = 0; i < tfields && *status == 0; ++i)
{
if(fits_strcasecmp(ttype[i],"MEMBER_POSITION") == 0 ||
fits_strcasecmp(ttype[i],"MEMBER_VERSION") == 0)
{
snprintf(keyword,FLEN_KEYWORD,"TFORM%d",i+1);
*status = fits_read_key_str(fptr,keyword,keyvalue,comment,
status);
snprintf(keyword,FLEN_KEYWORD,"TNULL%d",i+1);
*status = fits_insert_key_lng(fptr,keyword,0,"Column Null Value",
status);
}
}
/*
determine the correct EXTVER value for the new grouping table
by finding the highest numbered grouping table EXTVER value
the currently exists
*/
for(extver = 1;
(fits_movnam_hdu(fptr,ANY_HDU,"GROUPING",extver,status)) == 0;
++extver);
if(*status == BAD_HDU_NUM) *status = 0;
/*
move back to the new grouping table HDU and update the EXTVER
keyword value
*/
fits_movabs_hdu(fptr,hdunum,&hdutype,status);
fits_modify_key_lng(fptr,"EXTVER",extver,"&",status);
}while(0);
return(*status);
}
/*---------------------------------------------------------------------------*/
int ffgtch(fitsfile *gfptr, /* FITS pointer to group */
int grouptype, /* code specifying the type of
grouping table information:
GT_ID_ALL_URI 0 ==> defualt (all columns)
GT_ID_REF 1 ==> ID by reference
GT_ID_POS 2 ==> ID by position
GT_ID_ALL 3 ==> ID by ref. and position
GT_ID_REF_URI 11 ==> (1) + URI info
GT_ID_POS_URI 12 ==> (2) + URI info */
int *status) /* return status code */
/*
Change the grouping table structure of the grouping table pointed to by
gfptr. The grouptype code specifies the new structure of the table. This
operation only adds or removes grouping table columns, it does not add
or delete group members (i.e., table rows). If the grouping table already
has the desired structure then no operations are performed and function
simply returns with a (0) success status code. If the requested structure
change creates new grouping table columns, then the column values for all
existing members will be filled with the appropriate null values.
*/
{
int xtensionCol, extnameCol, extverCol, positionCol, locationCol, uriCol;
int ncols = 0;
int colnum = 0;
int nrows = 0;
int grptype = 0;
int i,j;
long intNull = 0;
long tfields = 0;
char *tform[6];
char *ttype[6];
unsigned char charNull[1] = {'\0'};
char ttypeBuff[102];
char tformBuff[54];
char keyword[FLEN_KEYWORD];
char keyvalue[FLEN_VALUE];
char comment[FLEN_COMMENT];
if(*status != 0) return(*status);
do
{
/* set up the ttype and tform character buffers */
for(i = 0; i < 6; ++i)
{
ttype[i] = ttypeBuff+(i*17);
tform[i] = tformBuff+(i*9);
}
/* retrieve positions of all Grouping table reserved columns */
*status = ffgtgc(gfptr,&xtensionCol,&extnameCol,&extverCol,&positionCol,
&locationCol,&uriCol,&grptype,status);
if(*status != 0) continue;
/* determine the total number of grouping table columns */
*status = fits_read_key_lng(gfptr,"TFIELDS",&tfields,comment,status);
/* define grouping table columns to be added to the configuration */
*status = ffgtdc(grouptype,xtensionCol,extnameCol,extverCol,positionCol,
locationCol,uriCol,ttype,tform,&ncols,status);
/*
delete any grouping tables columns that exist but do not belong to
new desired configuration; note that we delete before creating new
columns for (file size) efficiency reasons
*/
switch(grouptype)
{
case GT_ID_ALL_URI:
/* no columns to be deleted in this case */
break;
case GT_ID_REF:
if(positionCol != 0)
{
*status = fits_delete_col(gfptr,positionCol,status);
--tfields;
if(uriCol > positionCol) --uriCol;
if(locationCol > positionCol) --locationCol;
}
if(uriCol != 0)
{
*status = fits_delete_col(gfptr,uriCol,status);
--tfields;
if(locationCol > uriCol) --locationCol;
}
if(locationCol != 0)
*status = fits_delete_col(gfptr,locationCol,status);
break;
case GT_ID_POS:
if(xtensionCol != 0)
{
*status = fits_delete_col(gfptr,xtensionCol,status);
--tfields;
if(extnameCol > xtensionCol) --extnameCol;
if(extverCol > xtensionCol) --extverCol;
if(uriCol > xtensionCol) --uriCol;
if(locationCol > xtensionCol) --locationCol;
}
if(extnameCol != 0)
{
*status = fits_delete_col(gfptr,extnameCol,status);
--tfields;
if(extverCol > extnameCol) --extverCol;
if(uriCol > extnameCol) --uriCol;
if(locationCol > extnameCol) --locationCol;
}
if(extverCol != 0)
{
*status = fits_delete_col(gfptr,extverCol,status);
--tfields;
if(uriCol > extverCol) --uriCol;
if(locationCol > extverCol) --locationCol;
}
if(uriCol != 0)
{
*status = fits_delete_col(gfptr,uriCol,status);
--tfields;
if(locationCol > uriCol) --locationCol;
}
if(locationCol != 0)
{
*status = fits_delete_col(gfptr,locationCol,status);
--tfields;
}
break;
case GT_ID_ALL:
if(uriCol != 0)
{
*status = fits_delete_col(gfptr,uriCol,status);
--tfields;
if(locationCol > uriCol) --locationCol;
}
if(locationCol != 0)
{
*status = fits_delete_col(gfptr,locationCol,status);
--tfields;
}
break;
case GT_ID_REF_URI:
if(positionCol != 0)
{
*status = fits_delete_col(gfptr,positionCol,status);
--tfields;
}
break;
case GT_ID_POS_URI:
if(xtensionCol != 0)
{
*status = fits_delete_col(gfptr,xtensionCol,status);
--tfields;
if(extnameCol > xtensionCol) --extnameCol;
if(extverCol > xtensionCol) --extverCol;
}
if(extnameCol != 0)
{
*status = fits_delete_col(gfptr,extnameCol,status);
--tfields;
if(extverCol > extnameCol) --extverCol;
}
if(extverCol != 0)
{
*status = fits_delete_col(gfptr,extverCol,status);
--tfields;
}
break;
default:
*status = BAD_OPTION;
ffpmsg("Invalid value for grouptype parameter specified (ffgtch)");
break;
}
/*
add all the new grouping table columns that were not there
previously but are called for by the grouptype parameter
*/
for(i = 0; i < ncols && *status == 0; ++i)
*status = fits_insert_col(gfptr,tfields+i+1,ttype[i],tform[i],status);
/*
add the TNULL keywords and values for each new integer column defined;
integer null values are zero (0) for the MEMBER_POSITION and
MEMBER_VERSION columns. Insert a null ("/0") into each new string
column defined: MEMBER_XTENSION, MEMBER_NAME, MEMBER_URI_TYPE and
MEMBER_LOCATION. Note that by convention a null string is the
TNULL value for character fields so no TNULL is required.
*/
for(i = 0; i < ncols && *status == 0; ++i)
{
if(fits_strcasecmp(ttype[i],"MEMBER_POSITION") == 0 ||
fits_strcasecmp(ttype[i],"MEMBER_VERSION") == 0)
{
/* col contains int data; set TNULL and insert 0 for each col */
*status = fits_get_colnum(gfptr,CASESEN,ttype[i],&colnum,
status);
snprintf(keyword,FLEN_KEYWORD,"TFORM%d",colnum);
*status = fits_read_key_str(gfptr,keyword,keyvalue,comment,
status);
snprintf(keyword,FLEN_KEYWORD,"TNULL%d",colnum);
*status = fits_insert_key_lng(gfptr,keyword,0,
"Column Null Value",status);
for(j = 1; j <= nrows && *status == 0; ++j)
*status = fits_write_col_lng(gfptr,colnum,j,1,1,&intNull,
status);
}
else if(fits_strcasecmp(ttype[i],"MEMBER_XTENSION") == 0 ||
fits_strcasecmp(ttype[i],"MEMBER_NAME") == 0 ||
fits_strcasecmp(ttype[i],"MEMBER_URI_TYPE") == 0 ||
fits_strcasecmp(ttype[i],"MEMBER_LOCATION") == 0)
{
/* new col contains character data; insert NULLs into each col */
*status = fits_get_colnum(gfptr,CASESEN,ttype[i],&colnum,
status);
for(j = 1; j <= nrows && *status == 0; ++j)
/* WILL THIS WORK FOR VAR LENTH CHAR COLS??????*/
*status = fits_write_col_byt(gfptr,colnum,j,1,1,charNull,
status);
}
}
}while(0);
return(*status);
}
/*---------------------------------------------------------------------------*/
int ffgtrm(fitsfile *gfptr, /* FITS file pointer to group */
int rmopt, /* code specifying if member
elements are to be deleted:
OPT_RM_GPT ==> remove only group table
OPT_RM_ALL ==> recursively remove members
and their members (if groups) */
int *status) /* return status code */
/*
remove a grouping table, and optionally all its members. Any groups
containing the grouping table are updated, and all members (if not
deleted) have their GRPIDn and GRPLCn keywords updated accordingly.
If the (deleted) members are members of another grouping table then those
tables are also updated. The CHDU of the FITS file pointed to by gfptr must
be positioned to the grouping table to be deleted.
*/
{
int hdutype;
long i;
long nmembers = 0;
HDUtracker HDU;
if(*status != 0) return(*status);
/*
remove the grouping table depending upon the rmopt parameter
*/
switch(rmopt)
{
case OPT_RM_GPT:
/*
for this option, the grouping table is deleted, but the member
HDUs remain; in this case we only have to remove each member from
the grouping table by calling fits_remove_member() with the
OPT_RM_ENTRY option
*/
/* get the number of members contained by this table */
*status = fits_get_num_members(gfptr,&nmembers,status);
/* loop over all grouping table members and remove them */
for(i = nmembers; i > 0 && *status == 0; --i)
*status = fits_remove_member(gfptr,i,OPT_RM_ENTRY,status);
break;
case OPT_RM_ALL:
/*
for this option the entire Group is deleted -- this includes all
members and their members (if grouping tables themselves). Call
the recursive form of this function to perform the removal.
*/
/* add the current grouping table to the HDUtracker struct */
HDU.nHDU = 0;
*status = fftsad(gfptr,&HDU,NULL,NULL);
/* call the recursive group remove function */
*status = ffgtrmr(gfptr,&HDU,status);
/* free the memory allocated to the HDUtracker struct */
for(i = 0; i < HDU.nHDU; ++i)
{
free(HDU.filename[i]);
free(HDU.newFilename[i]);
}
break;
default:
*status = BAD_OPTION;
ffpmsg("Invalid value for the rmopt parameter specified (ffgtrm)");
break;
}
/*
if all went well then unlink and delete the grouping table HDU
*/
*status = ffgmul(gfptr,0,status);
*status = fits_delete_hdu(gfptr,&hdutype,status);
return(*status);
}
/*---------------------------------------------------------------------------*/
int ffgtcp(fitsfile *infptr, /* input FITS file pointer */
fitsfile *outfptr, /* output FITS file pointer */
int cpopt, /* code specifying copy options:
OPT_GCP_GPT (0) ==> copy only grouping table
OPT_GCP_ALL (2) ==> recusrively copy members
and their members (if
groups) */
int *status) /* return status code */
/*
copy a grouping table, and optionally all its members, to a new FITS file.
If the cpopt is set to OPT_GCP_GPT (copy grouping table only) then the
existing members have their GRPIDn and GRPLCn keywords updated to reflect
the existance of the new group, since they now belong to another group. If
cpopt is set to OPT_GCP_ALL (copy grouping table and members recursively)
then the original members are not updated; the new grouping table is
modified to include only the copied member HDUs and not the original members.
Note that the recursive version of this function, ffgtcpr(), is called
to perform the group table copy. In the case of cpopt == OPT_GCP_GPT
ffgtcpr() does not actually use recursion.
*/
{
int i;
HDUtracker HDU;
if(*status != 0) return(*status);
/* make sure infptr and outfptr are not the same pointer */
if(infptr == outfptr) *status = IDENTICAL_POINTERS;
else
{
/* initialize the HDUtracker struct */
HDU.nHDU = 0;
*status = fftsad(infptr,&HDU,NULL,NULL);
/*
call the recursive form of this function to copy the grouping table.
If the cpopt is OPT_GCP_GPT then there is actually no recursion
performed
*/
*status = ffgtcpr(infptr,outfptr,cpopt,&HDU,status);
/* free memory allocated for the HDUtracker struct */
for(i = 0; i < HDU.nHDU; ++i)
{
free(HDU.filename[i]);
free(HDU.newFilename[i]);
}
}
return(*status);
}
/*---------------------------------------------------------------------------*/
int ffgtmg(fitsfile *infptr, /* FITS file ptr to source grouping table */
fitsfile *outfptr, /* FITS file ptr to target grouping table */
int mgopt, /* code specifying merge options:
OPT_MRG_COPY (0) ==> copy members to target
group, leaving source
group in place
OPT_MRG_MOV (1) ==> move members to target
group, source group is
deleted after merge */
int *status) /* return status code */
/*
merge two grouping tables by combining their members into a single table.
The source grouping table must be the CHDU of the fitsfile pointed to by
infptr, and the target grouping table must be the CHDU of the fitsfile to by
outfptr. All members of the source grouping table shall be copied to the
target grouping table. If the mgopt parameter is OPT_MRG_COPY then the source
grouping table continues to exist after the merge. If the mgopt parameter
is OPT_MRG_MOV then the source grouping table is deleted after the merge,
and all member HDUs are updated accordingly.
*/
{
long i ;
long nmembers = 0;
fitsfile *tmpfptr = NULL;
if(*status != 0) return(*status);
do
{
*status = fits_get_num_members(infptr,&nmembers,status);
for(i = 1; i <= nmembers && *status == 0; ++i)
{
*status = fits_open_member(infptr,i,&tmpfptr,status);
*status = fits_add_group_member(outfptr,tmpfptr,0,status);
if(*status == HDU_ALREADY_MEMBER) *status = 0;
if(tmpfptr != NULL)
{
fits_close_file(tmpfptr,status);
tmpfptr = NULL;
}
}
if(*status != 0) continue;
if(mgopt == OPT_MRG_MOV)
*status = fits_remove_group(infptr,OPT_RM_GPT,status);
}while(0);
if(tmpfptr != NULL)
{
fits_close_file(tmpfptr,status);
}
return(*status);
}
/*---------------------------------------------------------------------------*/
int ffgtcm(fitsfile *gfptr, /* FITS file pointer to grouping table */
int cmopt, /* code specifying compact options
OPT_CMT_MBR (1) ==> compact only direct
members (if groups)
OPT_CMT_MBR_DEL (11) ==> (1) + delete all
compacted groups */
int *status) /* return status code */
/*
"Compact" a group pointed to by the FITS file pointer gfptr. This
is achieved by flattening the tree structure of a group and its
(grouping table) members. All members HDUs of a grouping table which is
itself a member of the grouping table gfptr are added to gfptr. Optionally,
the grouping tables which are "compacted" are deleted. If the grouping
table contains no members that are themselves grouping tables then this
function performs a NOOP.
*/
{
long i;
long nmembers = 0;
char keyvalue[FLEN_VALUE];
char comment[FLEN_COMMENT];
fitsfile *mfptr = NULL;
if(*status != 0) return(*status);
do
{
if(cmopt != OPT_CMT_MBR && cmopt != OPT_CMT_MBR_DEL)
{
*status = BAD_OPTION;
ffpmsg("Invalid value for cmopt parameter specified (ffgtcm)");
continue;
}
/* reteive the number of grouping table members */
*status = fits_get_num_members(gfptr,&nmembers,status);
/*
loop over all the grouping table members; if the member is a
grouping table then merge its members with the parent grouping
table
*/
for(i = 1; i <= nmembers && *status == 0; ++i)
{
*status = fits_open_member(gfptr,i,&mfptr,status);
if(*status != 0) continue;
*status = fits_read_key_str(mfptr,"EXTNAME",keyvalue,comment,status);
/* if no EXTNAME keyword then cannot be a grouping table */
if(*status == KEY_NO_EXIST)
{
*status = 0;
continue;
}
prepare_keyvalue(keyvalue);
if(*status != 0) continue;
/* if EXTNAME == "GROUPING" then process member as grouping table */
if(fits_strcasecmp(keyvalue,"GROUPING") == 0)
{
/* merge the member (grouping table) into the grouping table */
*status = fits_merge_groups(mfptr,gfptr,OPT_MRG_COPY,status);
*status = fits_close_file(mfptr,status);
mfptr = NULL;
/*
remove the member from the grouping table now that all of
its members have been transferred; if cmopt is set to
OPT_CMT_MBR_DEL then remove and delete the member
*/
if(cmopt == OPT_CMT_MBR)
*status = fits_remove_member(gfptr,i,OPT_RM_ENTRY,status);
else
*status = fits_remove_member(gfptr,i,OPT_RM_MBR,status);
}
else
{
/* not a grouping table; just close the opened member */
*status = fits_close_file(mfptr,status);
mfptr = NULL;
}
}
}while(0);
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffgtvf(fitsfile *gfptr, /* FITS file pointer to group */
long *firstfailed, /* Member ID (if positive) of first failed
member HDU verify check or GRPID index
(if negitive) of first failed group
link verify check. */
int *status) /* return status code */
/*
check the integrity of a grouping table to make sure that all group members
are accessible and all the links to other grouping tables are valid. The
firstfailed parameter returns the member ID of the first member HDU to fail
verification if positive or the first group link to fail if negative;
otherwise firstfailed contains a return value of 0.
*/
{
long i;
long nmembers = 0;
long ngroups = 0;
char errstr[FLEN_VALUE];
fitsfile *fptr = NULL;
if(*status != 0) return(*status);
*firstfailed = 0;
do
{
/*
attempt to open all the members of the grouping table. We stop
at the first member which cannot be opened (which implies that it
cannot be located)
*/
*status = fits_get_num_members(gfptr,&nmembers,status);
for(i = 1; i <= nmembers && *status == 0; ++i)
{
*status = fits_open_member(gfptr,i,&fptr,status);
fits_close_file(fptr,status);
}
/*
if the status is non-zero from the above loop then record the
member index that caused the error
*/
if(*status != 0)
{
*firstfailed = i;
snprintf(errstr,FLEN_VALUE,"Group table verify failed for member %ld (ffgtvf)",
i);
ffpmsg(errstr);
continue;
}
/*
attempt to open all the groups linked to this grouping table. We stop
at the first group which cannot be opened (which implies that it
cannot be located)
*/
*status = fits_get_num_groups(gfptr,&ngroups,status);
for(i = 1; i <= ngroups && *status == 0; ++i)
{
*status = fits_open_group(gfptr,i,&fptr,status);
fits_close_file(fptr,status);
}
/*
if the status from the above loop is non-zero, then record the
GRPIDn index of the group that caused the failure
*/
if(*status != 0)
{
*firstfailed = -1*i;
snprintf(errstr,FLEN_VALUE,
"Group table verify failed for GRPID index %ld (ffgtvf)",i);
ffpmsg(errstr);
continue;
}
}while(0);
return(*status);
}
/*---------------------------------------------------------------------------*/
int ffgtop(fitsfile *mfptr, /* FITS file pointer to the member HDU */
int grpid, /* group ID (GRPIDn index) within member HDU */
fitsfile **gfptr, /* FITS file pointer to grouping table HDU */
int *status) /* return status code */
/*
open the grouping table that contains the member HDU. The member HDU must
be the CHDU of the FITS file pointed to by mfptr, and the grouping table
is identified by the Nth index number of the GRPIDn keywords specified in
the member HDU's header. The fitsfile gfptr pointer is positioned with the
appropriate FITS file with the grouping table as the CHDU. If the group
grouping table resides in a file other than the member then an attempt
is first made to open the file readwrite, and failing that readonly.
Note that it is possible for the GRPIDn/GRPLCn keywords in a member
header to be non-continuous, e.g., GRPID1, GRPID2, GRPID5, GRPID6. In
such cases, the grpid index value specified in the function call shall
identify the (grpid)th GRPID value. In the above example, if grpid == 3,
then the group specified by GRPID5 would be opened.
*/
{
int i;
int found;