-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfileio.c
8010 lines (6864 loc) · 257 KB
/
cfileio.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, cfileio.c, contains the low-level file access 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. */
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <errno.h>
#include <stddef.h> /* apparently needed to define size_t */
#include "fitsio2.h"
#include "group.h"
#ifdef CFITSIO_HAVE_CURL
#include <curl/curl.h>
#endif
#define MAX_PREFIX_LEN 20 /* max length of file type prefix (e.g. 'http://') */
#define MAX_DRIVERS 31 /* max number of file I/O drivers */
typedef struct /* structure containing pointers to I/O driver functions */
{ char prefix[MAX_PREFIX_LEN];
int (*init)(void);
int (*shutdown)(void);
int (*setoptions)(int option);
int (*getoptions)(int *options);
int (*getversion)(int *version);
int (*checkfile)(char *urltype, char *infile, char *outfile);
int (*open)(char *filename, int rwmode, int *driverhandle);
int (*create)(char *filename, int *drivehandle);
int (*truncate)(int drivehandle, LONGLONG size);
int (*close)(int drivehandle);
int (*remove)(char *filename);
int (*size)(int drivehandle, LONGLONG *size);
int (*flush)(int drivehandle);
int (*seek)(int drivehandle, LONGLONG offset);
int (*read)(int drivehandle, void *buffer, long nbytes);
int (*write)(int drivehandle, void *buffer, long nbytes);
} fitsdriver;
fitsdriver driverTable[MAX_DRIVERS]; /* allocate driver tables */
FITSfile *FptrTable[NMAXFILES]; /* this table of Fptr pointers is */
/* used by fits_already_open */
int need_to_initialize = 1; /* true if CFITSIO has not been initialized */
int no_of_drivers = 0; /* number of currently defined I/O drivers */
static int pixel_filter_helper(fitsfile **fptr, char *outfile,
char *expr, int *status);
static int find_quote(char **string);
static int find_doublequote(char **string);
static int find_paren(char **string);
static int find_bracket(char **string);
static int find_curlybracket(char **string);
static int standardize_path(char *fullpath, int *status);
int comma2semicolon(char *string);
#ifdef _REENTRANT
pthread_mutex_t Fitsio_InitLock = PTHREAD_MUTEX_INITIALIZER;
#endif
/*--------------------------------------------------------------------------*/
int fitsio_init_lock(void)
{
int status = 0;
#ifdef _REENTRANT
static int need_to_init = 1;
pthread_mutexattr_t mutex_init;
FFLOCK1(Fitsio_InitLock);
if (need_to_init) {
/* Init the main fitsio lock here since we need a a recursive lock */
status = pthread_mutexattr_init(&mutex_init);
if (status) {
ffpmsg("pthread_mutexattr_init failed (fitsio_init_lock)");
return(status);
}
#ifdef __GLIBC__
status = pthread_mutexattr_settype(&mutex_init,
PTHREAD_MUTEX_RECURSIVE_NP);
#else
status = pthread_mutexattr_settype(&mutex_init,
PTHREAD_MUTEX_RECURSIVE);
#endif
if (status) {
ffpmsg("pthread_mutexattr_settype failed (fitsio_init_lock)");
return(status);
}
status = pthread_mutex_init(&Fitsio_Lock,&mutex_init);
if (status) {
ffpmsg("pthread_mutex_init failed (fitsio_init_lock)");
return(status);
}
need_to_init = 0;
}
FFUNLOCK1(Fitsio_InitLock);
#endif
return(status);
}
/*--------------------------------------------------------------------------*/
int ffomem(fitsfile **fptr, /* O - FITS file pointer */
const char *name, /* I - name of file to open */
int mode, /* I - 0 = open readonly; 1 = read/write */
void **buffptr, /* I - address of memory pointer */
size_t *buffsize, /* I - size of buffer, in bytes */
size_t deltasize, /* I - increment for future realloc's */
void *(*mem_realloc)(void *p, size_t newsize), /* function */
int *status) /* IO - error status */
/*
Open an existing FITS file in core memory. This is a specialized version
of ffopen.
*/
{
int ii, driver, handle, hdutyp, slen, movetotype, extvers, extnum;
char extname[FLEN_VALUE];
LONGLONG filesize;
char urltype[MAX_PREFIX_LEN], infile[FLEN_FILENAME], outfile[FLEN_FILENAME];
char extspec[FLEN_FILENAME], rowfilter[FLEN_FILENAME];
char binspec[FLEN_FILENAME], colspec[FLEN_FILENAME];
char imagecolname[FLEN_VALUE], rowexpress[FLEN_FILENAME];
char *url, errmsg[FLEN_ERRMSG];
char *hdtype[3] = {"IMAGE", "TABLE", "BINTABLE"};
if (*status > 0)
return(*status);
*fptr = 0; /* initialize null file pointer */
if (need_to_initialize) /* this is called only once */
{
*status = fits_init_cfitsio();
if (*status > 0)
return(*status);
}
url = (char *) name;
while (*url == ' ') /* ignore leading spaces in the file spec */
url++;
/* parse the input file specification */
fits_parse_input_url(url, urltype, infile, outfile, extspec,
rowfilter, binspec, colspec, status);
strcpy(urltype, "memkeep://"); /* URL type for pre-existing memory file */
*status = urltype2driver(urltype, &driver);
if (*status > 0)
{
ffpmsg("could not find driver for pre-existing memory file: (ffomem)");
return(*status);
}
/* call driver routine to open the memory file */
FFLOCK; /* lock this while searching for vacant handle */
*status = mem_openmem( buffptr, buffsize,deltasize,
mem_realloc, &handle);
FFUNLOCK;
if (*status > 0)
{
ffpmsg("failed to open pre-existing memory file: (ffomem)");
return(*status);
}
/* get initial file size */
*status = (*driverTable[driver].size)(handle, &filesize);
if (*status > 0)
{
(*driverTable[driver].close)(handle); /* close the file */
ffpmsg("failed get the size of the memory file: (ffomem)");
return(*status);
}
/* allocate fitsfile structure and initialize = 0 */
*fptr = (fitsfile *) calloc(1, sizeof(fitsfile));
if (!(*fptr))
{
(*driverTable[driver].close)(handle); /* close the file */
ffpmsg("failed to allocate structure for following file: (ffomem)");
ffpmsg(url);
return(*status = MEMORY_ALLOCATION);
}
/* allocate FITSfile structure and initialize = 0 */
(*fptr)->Fptr = (FITSfile *) calloc(1, sizeof(FITSfile));
if (!((*fptr)->Fptr))
{
(*driverTable[driver].close)(handle); /* close the file */
ffpmsg("failed to allocate structure for following file: (ffomem)");
ffpmsg(url);
free(*fptr);
*fptr = 0;
return(*status = MEMORY_ALLOCATION);
}
slen = strlen(url) + 1;
slen = maxvalue(slen, 32); /* reserve at least 32 chars */
((*fptr)->Fptr)->filename = (char *) malloc(slen); /* mem for file name */
if ( !(((*fptr)->Fptr)->filename) )
{
(*driverTable[driver].close)(handle); /* close the file */
ffpmsg("failed to allocate memory for filename: (ffomem)");
ffpmsg(url);
free((*fptr)->Fptr);
free(*fptr);
*fptr = 0; /* return null file pointer */
return(*status = MEMORY_ALLOCATION);
}
/* mem for headstart array */
((*fptr)->Fptr)->headstart = (LONGLONG *) calloc(1001, sizeof(LONGLONG));
if ( !(((*fptr)->Fptr)->headstart) )
{
(*driverTable[driver].close)(handle); /* close the file */
ffpmsg("failed to allocate memory for headstart array: (ffomem)");
ffpmsg(url);
free( ((*fptr)->Fptr)->filename);
free((*fptr)->Fptr);
free(*fptr);
*fptr = 0; /* return null file pointer */
return(*status = MEMORY_ALLOCATION);
}
/* mem for file I/O buffers */
((*fptr)->Fptr)->iobuffer = (char *) calloc(NIOBUF, IOBUFLEN);
if ( !(((*fptr)->Fptr)->iobuffer) )
{
(*driverTable[driver].close)(handle); /* close the file */
ffpmsg("failed to allocate memory for iobuffer array: (ffomem)");
ffpmsg(url);
free( ((*fptr)->Fptr)->headstart); /* free memory for headstart array */
free( ((*fptr)->Fptr)->filename);
free((*fptr)->Fptr);
free(*fptr);
*fptr = 0; /* return null file pointer */
return(*status = MEMORY_ALLOCATION);
}
/* initialize the ageindex array (relative age of the I/O buffers) */
/* and initialize the bufrecnum array as being empty */
for (ii = 0; ii < NIOBUF; ii++) {
((*fptr)->Fptr)->ageindex[ii] = ii;
((*fptr)->Fptr)->bufrecnum[ii] = -1;
}
/* store the parameters describing the file */
((*fptr)->Fptr)->MAXHDU = 1000; /* initial size of headstart */
((*fptr)->Fptr)->filehandle = handle; /* file handle */
((*fptr)->Fptr)->driver = driver; /* driver number */
strcpy(((*fptr)->Fptr)->filename, url); /* full input filename */
((*fptr)->Fptr)->filesize = filesize; /* physical file size */
((*fptr)->Fptr)->logfilesize = filesize; /* logical file size */
((*fptr)->Fptr)->writemode = mode; /* read-write mode */
((*fptr)->Fptr)->datastart = DATA_UNDEFINED; /* unknown start of data */
((*fptr)->Fptr)->curbuf = -1; /* undefined current IO buffer */
((*fptr)->Fptr)->open_count = 1; /* structure is currently used once */
((*fptr)->Fptr)->validcode = VALIDSTRUC; /* flag denoting valid structure */
((*fptr)->Fptr)->noextsyntax = 0; /* extended syntax can be used in filename */
ffldrc(*fptr, 0, REPORT_EOF, status); /* load first record */
fits_store_Fptr( (*fptr)->Fptr, status); /* store Fptr address */
if (ffrhdu(*fptr, &hdutyp, status) > 0) /* determine HDU structure */
{
ffpmsg(
"ffomem could not interpret primary array header of file: (ffomem)");
ffpmsg(url);
if (*status == UNKNOWN_REC)
ffpmsg("This does not look like a FITS file.");
ffclos(*fptr, status);
*fptr = 0; /* return null file pointer */
}
/* ---------------------------------------------------------- */
/* move to desired extension, if specified as part of the URL */
/* ---------------------------------------------------------- */
imagecolname[0] = '\0';
rowexpress[0] = '\0';
if (*extspec)
{
/* parse the extension specifier into individual parameters */
ffexts(extspec, &extnum,
extname, &extvers, &movetotype, imagecolname, rowexpress, status);
if (*status > 0)
return(*status);
if (extnum)
{
ffmahd(*fptr, extnum + 1, &hdutyp, status);
}
else if (*extname) /* move to named extension, if specified */
{
ffmnhd(*fptr, movetotype, extname, extvers, status);
}
if (*status > 0)
{
ffpmsg("ffomem could not move to the specified extension:");
if (extnum > 0)
{
snprintf(errmsg, FLEN_ERRMSG,
" extension number %d doesn't exist or couldn't be opened.",extnum);
ffpmsg(errmsg);
}
else
{
snprintf(errmsg, FLEN_ERRMSG,
" extension with EXTNAME = %s,", extname);
ffpmsg(errmsg);
if (extvers)
{
snprintf(errmsg, FLEN_ERRMSG,
" and with EXTVERS = %d,", extvers);
ffpmsg(errmsg);
}
if (movetotype != ANY_HDU)
{
snprintf(errmsg, FLEN_ERRMSG,
" and with XTENSION = %s,", hdtype[movetotype]);
ffpmsg(errmsg);
}
ffpmsg(" doesn't exist or couldn't be opened.");
}
return(*status);
}
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffdkopn(fitsfile **fptr, /* O - FITS file pointer */
const char *name, /* I - full name of file to open */
int mode, /* I - 0 = open readonly; 1 = read/write */
int *status) /* IO - error status */
/*
Open an existing FITS file on magnetic disk with either readonly or
read/write access. The routine does not support CFITSIO's extended
filename syntax and simply uses the entire input 'name' string as
the name of the file.
*/
{
if (*status > 0)
return(*status);
*status = OPEN_DISK_FILE;
ffopen(fptr, name, mode, status);
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffdopn(fitsfile **fptr, /* O - FITS file pointer */
const char *name, /* I - full name of file to open */
int mode, /* I - 0 = open readonly; 1 = read/write */
int *status) /* IO - error status */
/*
Open an existing FITS file with either readonly or read/write access. and
move to the first HDU that contains 'interesting' data, if the primary
array contains a null image (i.e., NAXIS = 0).
*/
{
if (*status > 0)
return(*status);
*status = SKIP_NULL_PRIMARY;
ffopen(fptr, name, mode, status);
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffeopn(fitsfile **fptr, /* O - FITS file pointer */
const char *name, /* I - full name of file to open */
int mode, /* I - 0 = open readonly; 1 = read/write */
char *extlist, /* I - list of 'good' extensions to move to */
int *hdutype, /* O - type of extension that is moved to */
int *status) /* IO - error status */
/*
Open an existing FITS file with either readonly or read/write access. and
if the primary array contains a null image (i.e., NAXIS = 0) then attempt to
move to the first extension named in the extlist of extension names. If
none are found, then simply move to the 2nd extension.
*/
{
int hdunum, naxis = 0, thdutype, gotext=0;
char *ext, *textlist;
char *saveptr;
if (*status > 0)
return(*status);
if (ffopen(fptr, name, mode, status) > 0)
return(*status);
fits_get_hdu_num(*fptr, &hdunum);
fits_get_hdu_type(*fptr, &thdutype, status);
if (hdunum == 1 && thdutype == IMAGE_HDU) {
fits_get_img_dim(*fptr, &naxis, status);
}
/* We are in the "default" primary extension */
/* look through the extension list */
if( (hdunum == 1) && (naxis == 0) ){
if( extlist ){
gotext = 0;
textlist = malloc(strlen(extlist) + 1);
if (!textlist) {
*status = MEMORY_ALLOCATION;
return(*status);
}
strcpy(textlist, extlist);
for(ext=(char *)ffstrtok(textlist, " ",&saveptr); ext != NULL;
ext=(char *)ffstrtok(NULL," ",&saveptr)){
fits_movnam_hdu(*fptr, ANY_HDU, ext, 0, status);
if( *status == 0 ){
gotext = 1;
break;
} else {
*status = 0;
}
}
free(textlist);
}
if( !gotext ){
/* if all else fails, move to extension #2 and hope for the best */
fits_movabs_hdu(*fptr, 2, &thdutype, status);
}
}
if (hdutype) {
fits_get_hdu_type(*fptr, hdutype, status);
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int fftopn(fitsfile **fptr, /* O - FITS file pointer */
const char *name, /* I - full name of file to open */
int mode, /* I - 0 = open readonly; 1 = read/write */
int *status) /* IO - error status */
/*
Open an existing FITS file with either readonly or read/write access. and
move to the first HDU that contains 'interesting' table (not an image).
*/
{
int hdutype;
if (*status > 0)
return(*status);
*status = SKIP_IMAGE;
ffopen(fptr, name, mode, status);
if (ffghdt(*fptr, &hdutype, status) <= 0) {
if (hdutype == IMAGE_HDU)
*status = NOT_TABLE;
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffiopn(fitsfile **fptr, /* O - FITS file pointer */
const char *name, /* I - full name of file to open */
int mode, /* I - 0 = open readonly; 1 = read/write */
int *status) /* IO - error status */
/*
Open an existing FITS file with either readonly or read/write access. and
move to the first HDU that contains 'interesting' image (not an table).
*/
{
int hdutype;
if (*status > 0)
return(*status);
*status = SKIP_TABLE;
ffopen(fptr, name, mode, status);
if (ffghdt(*fptr, &hdutype, status) <= 0) {
if (hdutype != IMAGE_HDU)
*status = NOT_IMAGE;
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffopentest(int soname, /* I - CFITSIO shared library version */
/* application program (fitsio.h file) */
fitsfile **fptr, /* O - FITS file pointer */
const char *name, /* I - full name of file to open */
int mode, /* I - 0 = open readonly; 1 = read/write */
int *status) /* IO - error status */
/*
Open an existing FITS file with either readonly or read/write access.
First test that the SONAME of fitsio.h used to build the CFITSIO library
is the same as was used in compiling the application program that
links to the library.
*/
{
if (soname != CFITSIO_SONAME)
{
printf("\nERROR: Mismatch in the CFITSIO_SONAME value in the fitsio.h include file\n");
printf("that was used to build the CFITSIO library, and the value in the include file\n");
printf("that was used when compiling the application program:\n");
printf(" Version used to build the CFITSIO library = %d\n",CFITSIO_SONAME);
printf(" Version included by the application program = %d\n",soname);
printf("\nFix this by recompiling and then relinking this application program \n");
printf("with the CFITSIO library.\n");
*status = FILE_NOT_OPENED;
return(*status);
}
/* now call the normal file open routine */
ffopen(fptr, name, mode, status);
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffopen(fitsfile **fptr, /* O - FITS file pointer */
const char *name, /* I - full name of file to open */
int mode, /* I - 0 = open readonly; 1 = read/write */
int *status) /* IO - error status */
/*
Open an existing FITS file with either readonly or read/write access.
*/
{
fitsfile *newptr;
int ii, driver, hdutyp, hdunum, slen, writecopy, isopen;
LONGLONG filesize;
long rownum, nrows, goodrows;
int extnum, extvers, handle, movetotype, tstatus = 0, only_one = 0;
char urltype[MAX_PREFIX_LEN], infile[FLEN_FILENAME], outfile[FLEN_FILENAME];
char origurltype[MAX_PREFIX_LEN], extspec[FLEN_FILENAME];
char extname[FLEN_VALUE], rowfilter[FLEN_FILENAME], tblname[FLEN_VALUE];
char imagecolname[FLEN_VALUE], rowexpress[FLEN_FILENAME];
char binspec[FLEN_FILENAME], colspec[FLEN_FILENAME], pixfilter[FLEN_FILENAME];
char histfilename[FLEN_FILENAME];
char filtfilename[FLEN_FILENAME], compspec[FLEN_FILENAME];
char wtcol[FLEN_VALUE];
char minname[4][FLEN_VALUE], maxname[4][FLEN_VALUE];
char binname[4][FLEN_VALUE];
char *url;
double minin[4], maxin[4], binsizein[4], weight;
int imagetype, naxis = 1, haxis, recip;
int skip_null = 0, skip_image = 0, skip_table = 0, open_disk_file = 0;
char colname[4][FLEN_VALUE];
char errmsg[FLEN_ERRMSG];
char *hdtype[3] = {"IMAGE", "TABLE", "BINTABLE"};
char *rowselect = 0;
if (*status > 0)
return(*status);
if (*status == SKIP_NULL_PRIMARY)
{
/* this special status value is used as a flag by ffdopn to tell */
/* ffopen to skip over a null primary array when opening the file. */
skip_null = 1;
*status = 0;
}
else if (*status == SKIP_IMAGE)
{
/* this special status value is used as a flag by fftopn to tell */
/* ffopen to move to 1st significant table when opening the file. */
skip_image = 1;
*status = 0;
}
else if (*status == SKIP_TABLE)
{
/* this special status value is used as a flag by ffiopn to tell */
/* ffopen to move to 1st significant image when opening the file. */
skip_table = 1;
*status = 0;
}
else if (*status == OPEN_DISK_FILE)
{
/* this special status value is used as a flag by ffdkopn to tell */
/* ffopen to not interpret the input filename using CFITSIO's */
/* extended filename syntax, and simply open the specified disk file */
open_disk_file = 1;
*status = 0;
}
*fptr = 0; /* initialize null file pointer */
writecopy = 0; /* have we made a write-able copy of the input file? */
if (need_to_initialize) { /* this is called only once */
*status = fits_init_cfitsio();
}
if (*status > 0)
return(*status);
url = (char *) name;
while (*url == ' ') /* ignore leading spaces in the filename */
url++;
if (*url == '\0')
{
ffpmsg("Name of file to open is blank. (ffopen)");
return(*status = FILE_NOT_OPENED);
}
if (open_disk_file)
{
/* treat the input URL literally as the name of the file to open */
/* and don't try to parse the URL using the extended filename syntax */
if (strlen(url) > FLEN_FILENAME - 1) {
ffpmsg("Name of file to open is too long. (ffopen)");
return(*status = FILE_NOT_OPENED);
}
strcpy(infile,url);
strcpy(urltype, "file://");
outfile[0] = '\0';
extspec[0] = '\0';
binspec[0] = '\0';
colspec[0] = '\0';
rowfilter[0] = '\0';
pixfilter[0] = '\0';
compspec[0] = '\0';
}
else
{
/* parse the input file specification */
/* NOTE: This routine tests that all the strings do not */
/* overflow the standard buffer sizes (FLEN_FILENAME, etc.) */
/* therefore in general we do not have to worry about buffer */
/* overflow of any of the returned strings. */
/* call the newer version of this parsing routine that supports 'compspec' */
ffifile2(url, urltype, infile, outfile, extspec,
rowfilter, binspec, colspec, pixfilter, compspec, status);
}
if (*status > 0)
{
ffpmsg("could not parse the input filename: (ffopen)");
ffpmsg(url);
return(*status);
}
imagecolname[0] = '\0';
rowexpress[0] = '\0';
if (*extspec)
{
slen = strlen(extspec);
if (extspec[slen - 1] == '#') { /* special symbol to mean only copy this extension */
extspec[slen - 1] = '\0';
only_one = 1;
}
/* parse the extension specifier into individual parameters */
ffexts(extspec, &extnum,
extname, &extvers, &movetotype, imagecolname, rowexpress, status);
if (*status > 0)
return(*status);
}
/*-------------------------------------------------------------------*/
/* special cases: */
/*-------------------------------------------------------------------*/
histfilename[0] = '\0';
filtfilename[0] = '\0';
if (*outfile && (*binspec || *imagecolname || *pixfilter))
{
/* if binspec or imagecolumn are specified, then the */
/* output file name is intended for the final image, */
/* and not a copy of the input file. */
strcpy(histfilename, outfile);
outfile[0] = '\0';
}
else if (*outfile && (*rowfilter || *colspec))
{
/* if rowfilter or colspece are specified, then the */
/* output file name is intended for the filtered file */
/* and not a copy of the input file. */
strcpy(filtfilename, outfile);
outfile[0] = '\0';
}
/*-------------------------------------------------------------------*/
/* check if this same file is already open, and if so, attach to it */
/*-------------------------------------------------------------------*/
FFLOCK;
if (fits_already_open(fptr, url, urltype, infile, extspec, rowfilter,
binspec, colspec, mode, open_disk_file, &isopen, status) > 0)
{
FFUNLOCK;
return(*status);
}
FFUNLOCK;
if (isopen) {
goto move2hdu;
}
/* get the driver number corresponding to this urltype */
*status = urltype2driver(urltype, &driver);
if (*status > 0)
{
ffpmsg("could not find driver for this file: (ffopen)");
ffpmsg(urltype);
ffpmsg(url);
return(*status);
}
/*-------------------------------------------------------------------
deal with all those messy special cases which may require that
a different driver be used:
- is disk file compressed?
- are ftp:, gsiftp:, or http: files compressed?
- has user requested that a local copy be made of
the ftp or http file?
-------------------------------------------------------------------*/
if (driverTable[driver].checkfile)
{
strcpy(origurltype,urltype); /* Save the urltype */
/* 'checkfile' may modify the urltype, infile and outfile strings */
*status = (*driverTable[driver].checkfile)(urltype, infile, outfile);
if (*status)
{
ffpmsg("checkfile failed for this file: (ffopen)");
ffpmsg(url);
return(*status);
}
if (strcmp(origurltype, urltype)) /* did driver changed on us? */
{
*status = urltype2driver(urltype, &driver);
if (*status > 0)
{
ffpmsg("could not change driver for this file: (ffopen)");
ffpmsg(url);
ffpmsg(urltype);
return(*status);
}
}
}
/* call appropriate driver to open the file */
if (driverTable[driver].open)
{
FFLOCK; /* lock this while searching for vacant handle */
*status = (*driverTable[driver].open)(infile, mode, &handle);
FFUNLOCK;
if (*status > 0)
{
ffpmsg("failed to find or open the following file: (ffopen)");
ffpmsg(url);
return(*status);
}
}
else
{
ffpmsg("cannot open an existing file of this type: (ffopen)");
ffpmsg(url);
return(*status = FILE_NOT_OPENED);
}
/* get initial file size */
*status = (*driverTable[driver].size)(handle, &filesize);
if (*status > 0)
{
(*driverTable[driver].close)(handle); /* close the file */
ffpmsg("failed get the size of the following file: (ffopen)");
ffpmsg(url);
return(*status);
}
/* allocate fitsfile structure and initialize = 0 */
*fptr = (fitsfile *) calloc(1, sizeof(fitsfile));
if (!(*fptr))
{
(*driverTable[driver].close)(handle); /* close the file */
ffpmsg("failed to allocate structure for following file: (ffopen)");
ffpmsg(url);
return(*status = MEMORY_ALLOCATION);
}
/* allocate FITSfile structure and initialize = 0 */
(*fptr)->Fptr = (FITSfile *) calloc(1, sizeof(FITSfile));
if (!((*fptr)->Fptr))
{
(*driverTable[driver].close)(handle); /* close the file */
ffpmsg("failed to allocate structure for following file: (ffopen)");
ffpmsg(url);
free(*fptr);
*fptr = 0;
return(*status = MEMORY_ALLOCATION);
}
slen = strlen(url) + 1;
slen = maxvalue(slen, 32); /* reserve at least 32 chars */
((*fptr)->Fptr)->filename = (char *) malloc(slen); /* mem for file name */
if ( !(((*fptr)->Fptr)->filename) )
{
(*driverTable[driver].close)(handle); /* close the file */
ffpmsg("failed to allocate memory for filename: (ffopen)");
ffpmsg(url);
free((*fptr)->Fptr);
free(*fptr);
*fptr = 0; /* return null file pointer */
return(*status = MEMORY_ALLOCATION);
}
/* mem for headstart array */
((*fptr)->Fptr)->headstart = (LONGLONG *) calloc(1001, sizeof(LONGLONG));
if ( !(((*fptr)->Fptr)->headstart) )
{
(*driverTable[driver].close)(handle); /* close the file */
ffpmsg("failed to allocate memory for headstart array: (ffopen)");
ffpmsg(url);
free( ((*fptr)->Fptr)->filename);
free((*fptr)->Fptr);
free(*fptr);
*fptr = 0; /* return null file pointer */
return(*status = MEMORY_ALLOCATION);
}
/* mem for file I/O buffers */
((*fptr)->Fptr)->iobuffer = (char *) calloc(NIOBUF, IOBUFLEN);
if ( !(((*fptr)->Fptr)->iobuffer) )
{
(*driverTable[driver].close)(handle); /* close the file */
ffpmsg("failed to allocate memory for iobuffer array: (ffopen)");
ffpmsg(url);
free( ((*fptr)->Fptr)->headstart); /* free memory for headstart array */
free( ((*fptr)->Fptr)->filename);
free((*fptr)->Fptr);
free(*fptr);
*fptr = 0; /* return null file pointer */
return(*status = MEMORY_ALLOCATION);
}
/* initialize the ageindex array (relative age of the I/O buffers) */
/* and initialize the bufrecnum array as being empty */
for (ii = 0; ii < NIOBUF; ii++) {
((*fptr)->Fptr)->ageindex[ii] = ii;
((*fptr)->Fptr)->bufrecnum[ii] = -1;
}
/* store the parameters describing the file */
((*fptr)->Fptr)->MAXHDU = 1000; /* initial size of headstart */
((*fptr)->Fptr)->filehandle = handle; /* file handle */
((*fptr)->Fptr)->driver = driver; /* driver number */
strcpy(((*fptr)->Fptr)->filename, url); /* full input filename */
((*fptr)->Fptr)->filesize = filesize; /* physical file size */
((*fptr)->Fptr)->logfilesize = filesize; /* logical file size */
((*fptr)->Fptr)->writemode = mode; /* read-write mode */
((*fptr)->Fptr)->datastart = DATA_UNDEFINED; /* unknown start of data */
((*fptr)->Fptr)->curbuf = -1; /* undefined current IO buffer */
((*fptr)->Fptr)->open_count = 1; /* structure is currently used once */
((*fptr)->Fptr)->validcode = VALIDSTRUC; /* flag denoting valid structure */
((*fptr)->Fptr)->only_one = only_one; /* flag denoting only copy single extension */
((*fptr)->Fptr)->noextsyntax = open_disk_file; /* true if extended syntax is disabled */
ffldrc(*fptr, 0, REPORT_EOF, status); /* load first record */
fits_store_Fptr( (*fptr)->Fptr, status); /* store Fptr address */
if (ffrhdu(*fptr, &hdutyp, status) > 0) /* determine HDU structure */
{
ffpmsg(
"ffopen could not interpret primary array header of file: ");
ffpmsg(url);
if (*status == UNKNOWN_REC)
ffpmsg("This does not look like a FITS file.");
ffclos(*fptr, status);
*fptr = 0; /* return null file pointer */
return(*status);
}
/* ------------------------------------------------------------- */
/* At this point, the input file has been opened. If outfile was */
/* specified, then we have opened a copy of the file, not the */
/* original file so it is safe to modify it if necessary */
/* ------------------------------------------------------------- */
if (*outfile)
writecopy = 1;
move2hdu:
/* ---------------------------------------------------------- */
/* move to desired extension, if specified as part of the URL */
/* ---------------------------------------------------------- */
if (*extspec)
{
if (extnum) /* extension number was specified */
{
ffmahd(*fptr, extnum + 1, &hdutyp, status);
}
else if (*extname) /* move to named extension, if specified */
{
ffmnhd(*fptr, movetotype, extname, extvers, status);
}
if (*status > 0) /* clean up after error */
{
ffpmsg("ffopen could not move to the specified extension:");
if (extnum > 0)
{
snprintf(errmsg, FLEN_ERRMSG,
" extension number %d doesn't exist or couldn't be opened.",extnum);
ffpmsg(errmsg);
}
else
{
snprintf(errmsg, FLEN_ERRMSG,
" extension with EXTNAME = %s,", extname);
ffpmsg(errmsg);
if (extvers)
{
snprintf(errmsg, FLEN_ERRMSG,
" and with EXTVERS = %d,", extvers);
ffpmsg(errmsg);
}
if (movetotype != ANY_HDU)
{
snprintf(errmsg, FLEN_ERRMSG,
" and with XTENSION = %s,", hdtype[movetotype]);
ffpmsg(errmsg);
}
ffpmsg(" doesn't exist or couldn't be opened.");
}
ffclos(*fptr, status);
*fptr = 0; /* return null file pointer */
return(*status);
}
}
else if (skip_null || skip_image || skip_table ||
(*imagecolname || *colspec || *rowfilter || *binspec))
{
/* ------------------------------------------------------------------