forked from esheldon/fitsio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffers.c
1377 lines (1158 loc) · 52 KB
/
buffers.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, buffers.c, contains the core set of FITSIO routines */
/* that use or manage the internal set of IO buffers. */
/* 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 "fitsio2.h"
/*--------------------------------------------------------------------------*/
int ffmbyt(fitsfile *fptr, /* I - FITS file pointer */
LONGLONG bytepos, /* I - byte position in file to move to */
int err_mode, /* I - 1=ignore error, 0 = return error */
int *status) /* IO - error status */
{
/*
Move to the input byte location in the file. When writing to a file, a move
may sometimes be made to a position beyond the current EOF. The err_mode
parameter determines whether such conditions should be returned as an error
or simply ignored.
*/
long record;
if (*status > 0)
return(*status);
if (bytepos < 0)
return(*status = NEG_FILE_POS);
if (fptr->HDUposition != (fptr->Fptr)->curhdu)
ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);
record = (long) (bytepos / IOBUFLEN); /* zero-indexed record number */
/* if this is not the current record, then load it */
if ( ((fptr->Fptr)->curbuf < 0) ||
(record != (fptr->Fptr)->bufrecnum[(fptr->Fptr)->curbuf]))
ffldrc(fptr, record, err_mode, status);
if (*status <= 0)
(fptr->Fptr)->bytepos = bytepos; /* save new file position */
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffpbyt(fitsfile *fptr, /* I - FITS file pointer */
LONGLONG nbytes, /* I - number of bytes to write */
void *buffer, /* I - buffer containing the bytes to write */
int *status) /* IO - error status */
/*
put (write) the buffer of bytes to the output FITS file, starting at
the current file position. Write large blocks of data directly to disk;
write smaller segments to intermediate IO buffers to improve efficiency.
*/
{
int ii, nbuff;
LONGLONG filepos;
long recstart, recend;
long ntodo, bufpos, nspace, nwrite;
char *cptr;
if (*status > 0)
return(*status);
if (fptr->HDUposition != (fptr->Fptr)->curhdu)
ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);
if (nbytes > LONG_MAX) {
ffpmsg("Number of bytes to write is greater than LONG_MAX (ffpbyt).");
*status = WRITE_ERROR;
return(*status);
}
ntodo = (long) nbytes;
cptr = (char *)buffer;
if ((fptr->Fptr)->curbuf < 0) /* no current data buffer for this file */
{ /* so reload the last one that was used */
ffldrc(fptr, (long) (((fptr->Fptr)->bytepos) / IOBUFLEN), REPORT_EOF, status);
}
if (nbytes >= MINDIRECT)
{
/* write large blocks of data directly to disk instead of via buffers */
/* first, fill up the current IO buffer before flushing it to disk */
nbuff = (fptr->Fptr)->curbuf; /* current IO buffer number */
filepos = (fptr->Fptr)->bytepos; /* save the write starting position */
recstart = (fptr->Fptr)->bufrecnum[nbuff]; /* starting record */
recend = (long) ((filepos + nbytes - 1) / IOBUFLEN); /* ending record */
/* bufpos is the starting position within the IO buffer */
bufpos = (long) (filepos - ((LONGLONG)recstart * IOBUFLEN));
nspace = IOBUFLEN - bufpos; /* amount of space left in the buffer */
if (nspace)
{ /* fill up the IO buffer */
memcpy((fptr->Fptr)->iobuffer + (nbuff * IOBUFLEN) + bufpos, cptr, nspace);
ntodo -= nspace; /* decrement remaining number of bytes */
cptr += nspace; /* increment user buffer pointer */
filepos += nspace; /* increment file position pointer */
(fptr->Fptr)->dirty[nbuff] = TRUE; /* mark record as having been modified */
}
for (ii = 0; ii < NIOBUF; ii++) /* flush any affected buffers to disk */
{
if ((fptr->Fptr)->bufrecnum[ii] >= recstart
&& (fptr->Fptr)->bufrecnum[ii] <= recend )
{
if ((fptr->Fptr)->dirty[ii]) /* flush modified buffer to disk */
ffbfwt(fptr->Fptr, ii, status);
(fptr->Fptr)->bufrecnum[ii] = -1; /* disassociate buffer from the file */
}
}
/* move to the correct write position */
if ((fptr->Fptr)->io_pos != filepos)
ffseek(fptr->Fptr, filepos);
nwrite = ((ntodo - 1) / IOBUFLEN) * IOBUFLEN; /* don't write last buff */
ffwrite(fptr->Fptr, nwrite, cptr, status); /* write the data */
ntodo -= nwrite; /* decrement remaining number of bytes */
cptr += nwrite; /* increment user buffer pointer */
(fptr->Fptr)->io_pos = filepos + nwrite; /* update the file position */
if ((fptr->Fptr)->io_pos >= (fptr->Fptr)->filesize) /* at the EOF? */
{
(fptr->Fptr)->filesize = (fptr->Fptr)->io_pos; /* increment file size */
/* initialize the current buffer with the correct fill value */
if ((fptr->Fptr)->hdutype == ASCII_TBL)
memset((fptr->Fptr)->iobuffer + (nbuff * IOBUFLEN), 32, IOBUFLEN); /* blank fill */
else
memset((fptr->Fptr)->iobuffer + (nbuff * IOBUFLEN), 0, IOBUFLEN); /* zero fill */
}
else
{
/* read next record */
ffread(fptr->Fptr, IOBUFLEN, (fptr->Fptr)->iobuffer + (nbuff * IOBUFLEN), status);
(fptr->Fptr)->io_pos += IOBUFLEN;
}
/* copy remaining bytes from user buffer into current IO buffer */
memcpy((fptr->Fptr)->iobuffer + (nbuff * IOBUFLEN), cptr, ntodo);
(fptr->Fptr)->dirty[nbuff] = TRUE; /* mark record as having been modified */
(fptr->Fptr)->bufrecnum[nbuff] = recend; /* record number */
(fptr->Fptr)->logfilesize = maxvalue((fptr->Fptr)->logfilesize,
(LONGLONG)(recend + 1) * IOBUFLEN);
(fptr->Fptr)->bytepos = filepos + nwrite + ntodo;
}
else
{
/* bufpos is the starting position in IO buffer */
bufpos = (long) ((fptr->Fptr)->bytepos - ((LONGLONG)(fptr->Fptr)->bufrecnum[(fptr->Fptr)->curbuf] *
IOBUFLEN));
nspace = IOBUFLEN - bufpos; /* amount of space left in the buffer */
while (ntodo)
{
nwrite = minvalue(ntodo, nspace);
/* copy bytes from user's buffer to the IO buffer */
memcpy((fptr->Fptr)->iobuffer + ((fptr->Fptr)->curbuf * IOBUFLEN) + bufpos, cptr, nwrite);
ntodo -= nwrite; /* decrement remaining number of bytes */
cptr += nwrite;
(fptr->Fptr)->bytepos += nwrite; /* increment file position pointer */
(fptr->Fptr)->dirty[(fptr->Fptr)->curbuf] = TRUE; /* mark record as modified */
if (ntodo) /* load next record into a buffer */
{
ffldrc(fptr, (long) ((fptr->Fptr)->bytepos / IOBUFLEN), IGNORE_EOF, status);
bufpos = 0;
nspace = IOBUFLEN;
}
}
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffpbytoff(fitsfile *fptr, /* I - FITS file pointer */
long gsize, /* I - size of each group of bytes */
long ngroups, /* I - number of groups to write */
long offset, /* I - size of gap between groups */
void *buffer, /* I - buffer to be written */
int *status) /* IO - error status */
/*
put (write) the buffer of bytes to the output FITS file, with an offset
between each group of bytes. This function combines ffmbyt and ffpbyt
for increased efficiency.
*/
{
int bcurrent;
long ii, bufpos, nspace, nwrite, record;
char *cptr, *ioptr;
if (*status > 0)
return(*status);
if (fptr->HDUposition != (fptr->Fptr)->curhdu)
ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);
if ((fptr->Fptr)->curbuf < 0) /* no current data buffer for this file */
{ /* so reload the last one that was used */
ffldrc(fptr, (long) (((fptr->Fptr)->bytepos) / IOBUFLEN), REPORT_EOF, status);
}
cptr = (char *)buffer;
bcurrent = (fptr->Fptr)->curbuf; /* number of the current IO buffer */
record = (fptr->Fptr)->bufrecnum[bcurrent]; /* zero-indexed record number */
bufpos = (long) ((fptr->Fptr)->bytepos - ((LONGLONG)record * IOBUFLEN)); /* start pos */
nspace = IOBUFLEN - bufpos; /* amount of space left in buffer */
ioptr = (fptr->Fptr)->iobuffer + (bcurrent * IOBUFLEN) + bufpos;
for (ii = 1; ii < ngroups; ii++) /* write all but the last group */
{
/* copy bytes from user's buffer to the IO buffer */
nwrite = minvalue(gsize, nspace);
memcpy(ioptr, cptr, nwrite);
cptr += nwrite; /* increment buffer pointer */
if (nwrite < gsize) /* entire group did not fit */
{
(fptr->Fptr)->dirty[bcurrent] = TRUE; /* mark record as having been modified */
record++;
ffldrc(fptr, record, IGNORE_EOF, status); /* load next record */
bcurrent = (fptr->Fptr)->curbuf;
ioptr = (fptr->Fptr)->iobuffer + (bcurrent * IOBUFLEN);
nwrite = gsize - nwrite;
memcpy(ioptr, cptr, nwrite);
cptr += nwrite; /* increment buffer pointer */
ioptr += (offset + nwrite); /* increment IO buffer pointer */
nspace = IOBUFLEN - offset - nwrite; /* amount of space left */
}
else
{
ioptr += (offset + nwrite); /* increment IO bufer pointer */
nspace -= (offset + nwrite);
}
if (nspace <= 0) /* beyond current record? */
{
(fptr->Fptr)->dirty[bcurrent] = TRUE;
record += ((IOBUFLEN - nspace) / IOBUFLEN); /* new record number */
ffldrc(fptr, record, IGNORE_EOF, status);
bcurrent = (fptr->Fptr)->curbuf;
bufpos = (-nspace) % IOBUFLEN; /* starting buffer pos */
nspace = IOBUFLEN - bufpos;
ioptr = (fptr->Fptr)->iobuffer + (bcurrent * IOBUFLEN) + bufpos;
}
}
/* now write the last group */
nwrite = minvalue(gsize, nspace);
memcpy(ioptr, cptr, nwrite);
cptr += nwrite; /* increment buffer pointer */
if (nwrite < gsize) /* entire group did not fit */
{
(fptr->Fptr)->dirty[bcurrent] = TRUE; /* mark record as having been modified */
record++;
ffldrc(fptr, record, IGNORE_EOF, status); /* load next record */
bcurrent = (fptr->Fptr)->curbuf;
ioptr = (fptr->Fptr)->iobuffer + (bcurrent * IOBUFLEN);
nwrite = gsize - nwrite;
memcpy(ioptr, cptr, nwrite);
}
(fptr->Fptr)->dirty[bcurrent] = TRUE; /* mark record as having been modified */
(fptr->Fptr)->bytepos = (fptr->Fptr)->bytepos + (ngroups * gsize)
+ (ngroups - 1) * offset;
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffgbyt(fitsfile *fptr, /* I - FITS file pointer */
LONGLONG nbytes, /* I - number of bytes to read */
void *buffer, /* O - buffer to read into */
int *status) /* IO - error status */
/*
get (read) the requested number of bytes from the file, starting at
the current file position. Read large blocks of data directly from disk;
read smaller segments via intermediate IO buffers to improve efficiency.
*/
{
int ii;
LONGLONG filepos;
long recstart, recend, ntodo, bufpos, nspace, nread;
char *cptr;
if (*status > 0)
return(*status);
if (fptr->HDUposition != (fptr->Fptr)->curhdu)
ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);
cptr = (char *)buffer;
if (nbytes >= MINDIRECT)
{
/* read large blocks of data directly from disk instead of via buffers */
filepos = (fptr->Fptr)->bytepos; /* save the read starting position */
/* note that in this case, ffmbyt has not been called, and so */
/* bufrecnum[(fptr->Fptr)->curbuf] does not point to the intended */
/* output buffer */
recstart = (long) (filepos / IOBUFLEN); /* starting record */
recend = (long) ((filepos + nbytes - 1) / IOBUFLEN); /* ending record */
for (ii = 0; ii < NIOBUF; ii++) /* flush any affected buffers to disk */
{
if ((fptr->Fptr)->dirty[ii] &&
(fptr->Fptr)->bufrecnum[ii] >= recstart && (fptr->Fptr)->bufrecnum[ii] <= recend)
{
ffbfwt(fptr->Fptr, ii, status); /* flush modified buffer to disk */
}
}
/* move to the correct read position */
if ((fptr->Fptr)->io_pos != filepos)
ffseek(fptr->Fptr, filepos);
ffread(fptr->Fptr, (long) nbytes, cptr, status); /* read the data */
(fptr->Fptr)->io_pos = filepos + nbytes; /* update the file position */
}
else
{
/* read small chucks of data using the IO buffers for efficiency */
if ((fptr->Fptr)->curbuf < 0) /* no current data buffer for this file */
{ /* so reload the last one that was used */
ffldrc(fptr, (long) (((fptr->Fptr)->bytepos) / IOBUFLEN), REPORT_EOF, status);
}
/* bufpos is the starting position in IO buffer */
bufpos = (long) ((fptr->Fptr)->bytepos - ((LONGLONG)(fptr->Fptr)->bufrecnum[(fptr->Fptr)->curbuf] *
IOBUFLEN));
nspace = IOBUFLEN - bufpos; /* amount of space left in the buffer */
ntodo = (long) nbytes;
while (ntodo)
{
nread = minvalue(ntodo, nspace);
/* copy bytes from IO buffer to user's buffer */
memcpy(cptr, (fptr->Fptr)->iobuffer + ((fptr->Fptr)->curbuf * IOBUFLEN) + bufpos, nread);
ntodo -= nread; /* decrement remaining number of bytes */
cptr += nread;
(fptr->Fptr)->bytepos += nread; /* increment file position pointer */
if (ntodo) /* load next record into a buffer */
{
ffldrc(fptr, (long) ((fptr->Fptr)->bytepos / IOBUFLEN), REPORT_EOF, status);
bufpos = 0;
nspace = IOBUFLEN;
}
}
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffgbytoff(fitsfile *fptr, /* I - FITS file pointer */
long gsize, /* I - size of each group of bytes */
long ngroups, /* I - number of groups to read */
long offset, /* I - size of gap between groups (may be < 0) */
void *buffer, /* I - buffer to be filled */
int *status) /* IO - error status */
/*
get (read) the requested number of bytes from the file, starting at
the current file position. This function combines ffmbyt and ffgbyt
for increased efficiency.
*/
{
int bcurrent;
long ii, bufpos, nspace, nread, record;
char *cptr, *ioptr;
if (*status > 0)
return(*status);
if (fptr->HDUposition != (fptr->Fptr)->curhdu)
ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);
if ((fptr->Fptr)->curbuf < 0) /* no current data buffer for this file */
{ /* so reload the last one that was used */
ffldrc(fptr, (long) (((fptr->Fptr)->bytepos) / IOBUFLEN), REPORT_EOF, status);
}
cptr = (char *)buffer;
bcurrent = (fptr->Fptr)->curbuf; /* number of the current IO buffer */
record = (fptr->Fptr)->bufrecnum[bcurrent]; /* zero-indexed record number */
bufpos = (long) ((fptr->Fptr)->bytepos - ((LONGLONG)record * IOBUFLEN)); /* start pos */
nspace = IOBUFLEN - bufpos; /* amount of space left in buffer */
ioptr = (fptr->Fptr)->iobuffer + (bcurrent * IOBUFLEN) + bufpos;
for (ii = 1; ii < ngroups; ii++) /* read all but the last group */
{
/* copy bytes from IO buffer to the user's buffer */
nread = minvalue(gsize, nspace);
memcpy(cptr, ioptr, nread);
cptr += nread; /* increment buffer pointer */
if (nread < gsize) /* entire group did not fit */
{
record++;
ffldrc(fptr, record, REPORT_EOF, status); /* load next record */
bcurrent = (fptr->Fptr)->curbuf;
ioptr = (fptr->Fptr)->iobuffer + (bcurrent * IOBUFLEN);
nread = gsize - nread;
memcpy(cptr, ioptr, nread);
cptr += nread; /* increment buffer pointer */
ioptr += (offset + nread); /* increment IO buffer pointer */
nspace = IOBUFLEN - offset - nread; /* amount of space left */
}
else
{
ioptr += (offset + nread); /* increment IO bufer pointer */
nspace -= (offset + nread);
}
if (nspace <= 0 || nspace > IOBUFLEN) /* beyond current record? */
{
if (nspace <= 0)
{
record += ((IOBUFLEN - nspace) / IOBUFLEN); /* new record number */
bufpos = (-nspace) % IOBUFLEN; /* starting buffer pos */
}
else
{
record -= ((nspace - 1 ) / IOBUFLEN); /* new record number */
bufpos = IOBUFLEN - (nspace % IOBUFLEN); /* starting buffer pos */
}
ffldrc(fptr, record, REPORT_EOF, status);
bcurrent = (fptr->Fptr)->curbuf;
nspace = IOBUFLEN - bufpos;
ioptr = (fptr->Fptr)->iobuffer + (bcurrent * IOBUFLEN) + bufpos;
}
}
/* now read the last group */
nread = minvalue(gsize, nspace);
memcpy(cptr, ioptr, nread);
cptr += nread; /* increment buffer pointer */
if (nread < gsize) /* entire group did not fit */
{
record++;
ffldrc(fptr, record, REPORT_EOF, status); /* load next record */
bcurrent = (fptr->Fptr)->curbuf;
ioptr = (fptr->Fptr)->iobuffer + (bcurrent * IOBUFLEN);
nread = gsize - nread;
memcpy(cptr, ioptr, nread);
}
(fptr->Fptr)->bytepos = (fptr->Fptr)->bytepos + (ngroups * gsize)
+ (ngroups - 1) * offset;
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffldrc(fitsfile *fptr, /* I - FITS file pointer */
long record, /* I - record number to be loaded */
int err_mode, /* I - 1=ignore EOF, 0 = return EOF error */
int *status) /* IO - error status */
{
/*
low-level routine to load a specified record from a file into
a physical buffer, if it is not already loaded. Reset all
pointers to make this the new current record for that file.
Update ages of all the physical buffers.
*/
int ibuff, nbuff;
LONGLONG rstart;
/* check if record is already loaded in one of the buffers */
/* search from youngest to oldest buffer for efficiency */
if (fptr->HDUposition != (fptr->Fptr)->curhdu)
ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);
for (ibuff = NIOBUF - 1; ibuff >= 0; ibuff--)
{
nbuff = (fptr->Fptr)->ageindex[ibuff];
if (record == (fptr->Fptr)->bufrecnum[nbuff]) {
goto updatebuf; /* use 'goto' for efficiency */
}
}
/* record is not already loaded */
rstart = (LONGLONG)record * IOBUFLEN;
if ( !err_mode && (rstart >= (fptr->Fptr)->logfilesize) ) /* EOF? */
return(*status = END_OF_FILE);
if (ffwhbf(fptr, &nbuff) < 0) /* which buffer should we reuse? */
return(*status = TOO_MANY_FILES);
if ((fptr->Fptr)->dirty[nbuff])
ffbfwt(fptr->Fptr, nbuff, status); /* write dirty buffer to disk */
if (rstart >= (fptr->Fptr)->filesize) /* EOF? */
{
/* initialize an empty buffer with the correct fill value */
if ((fptr->Fptr)->hdutype == ASCII_TBL)
memset((fptr->Fptr)->iobuffer + (nbuff * IOBUFLEN), 32, IOBUFLEN); /* blank fill */
else
memset((fptr->Fptr)->iobuffer + (nbuff * IOBUFLEN), 0, IOBUFLEN); /* zero fill */
(fptr->Fptr)->logfilesize = maxvalue((fptr->Fptr)->logfilesize,
rstart + IOBUFLEN);
(fptr->Fptr)->dirty[nbuff] = TRUE; /* mark record as having been modified */
}
else /* not EOF, so read record from disk */
{
if ((fptr->Fptr)->io_pos != rstart)
ffseek(fptr->Fptr, rstart);
ffread(fptr->Fptr, IOBUFLEN, (fptr->Fptr)->iobuffer + (nbuff * IOBUFLEN), status);
(fptr->Fptr)->io_pos = rstart + IOBUFLEN; /* set new IO position */
}
(fptr->Fptr)->bufrecnum[nbuff] = record; /* record number contained in buffer */
updatebuf:
(fptr->Fptr)->curbuf = nbuff; /* this is the current buffer for this file */
if (ibuff < 0)
{
/* find the current position of the buffer in the age index */
for (ibuff = 0; ibuff < NIOBUF; ibuff++)
if ((fptr->Fptr)->ageindex[ibuff] == nbuff)
break;
}
/* increment the age of all the buffers that were younger than it */
for (ibuff++; ibuff < NIOBUF; ibuff++)
(fptr->Fptr)->ageindex[ibuff - 1] = (fptr->Fptr)->ageindex[ibuff];
(fptr->Fptr)->ageindex[NIOBUF - 1] = nbuff; /* this is now the youngest buffer */
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffwhbf(fitsfile *fptr, /* I - FITS file pointer */
int *nbuff) /* O - which buffer to use */
{
/*
decide which buffer to (re)use to hold a new file record
*/
return(*nbuff = (fptr->Fptr)->ageindex[0]); /* return oldest buffer */
}
/*--------------------------------------------------------------------------*/
int ffflus(fitsfile *fptr, /* I - FITS file pointer */
int *status) /* IO - error status */
/*
Flush all the data in the current FITS file to disk. This ensures that if
the program subsequently dies, the disk FITS file will be closed correctly.
*/
{
int hdunum, hdutype;
if (*status > 0)
return(*status);
ffghdn(fptr, &hdunum); /* get the current HDU number */
if (ffchdu(fptr,status) > 0) /* close out the current HDU */
ffpmsg("ffflus could not close the current HDU.");
ffflsh(fptr, FALSE, status); /* flush any modified IO buffers to disk */
if (ffgext(fptr, hdunum - 1, &hdutype, status) > 0) /* reopen HDU */
ffpmsg("ffflus could not reopen the current HDU.");
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffflsh(fitsfile *fptr, /* I - FITS file pointer */
int clearbuf, /* I - also clear buffer contents? */
int *status) /* IO - error status */
{
/*
flush all dirty IO buffers associated with the file to disk
*/
int ii;
/*
no need to move to a different HDU
if (fptr->HDUposition != (fptr->Fptr)->curhdu)
ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);
*/
for (ii = 0; ii < NIOBUF; ii++)
{
/* flush modified buffer to disk */
if ((fptr->Fptr)->bufrecnum[ii] >= 0 &&(fptr->Fptr)->dirty[ii])
ffbfwt(fptr->Fptr, ii, status);
if (clearbuf)
(fptr->Fptr)->bufrecnum[ii] = -1; /* set contents of buffer as undefined */
}
if (*status != READONLY_FILE)
ffflushx(fptr->Fptr); /* flush system buffers to disk */
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffbfeof(fitsfile *fptr, /* I - FITS file pointer */
int *status) /* IO - error status */
{
/*
clear any buffers beyond the end of file
*/
int ii;
for (ii = 0; ii < NIOBUF; ii++)
{
if ( (LONGLONG) (fptr->Fptr)->bufrecnum[ii] * IOBUFLEN >= fptr->Fptr->filesize)
{
(fptr->Fptr)->bufrecnum[ii] = -1; /* set contents of buffer as undefined */
}
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffbfwt(FITSfile *Fptr, /* I - FITS file pointer */
int nbuff, /* I - which buffer to write */
int *status) /* IO - error status */
{
/*
write contents of buffer to file; If the position of the buffer
is beyond the current EOF, then the file may need to be extended
with fill values, and/or with the contents of some of the other
i/o buffers.
*/
int ii,ibuff;
long jj, irec, minrec, nloop;
LONGLONG filepos;
static char zeros[IOBUFLEN]; /* initialized to zero by default */
if (!(Fptr->writemode) )
{
ffpmsg("Error: trying to write to READONLY file.");
if (Fptr->driver == 8) { /* gzip compressed file */
ffpmsg("Cannot write to a GZIP or COMPRESS compressed file.");
}
Fptr->dirty[nbuff] = FALSE; /* reset buffer status to prevent later probs */
*status = READONLY_FILE;
return(*status);
}
filepos = (LONGLONG)Fptr->bufrecnum[nbuff] * IOBUFLEN;
if (filepos <= Fptr->filesize)
{
/* record is located within current file, so just write it */
/* move to the correct write position */
if (Fptr->io_pos != filepos)
ffseek(Fptr, filepos);
ffwrite(Fptr, IOBUFLEN, Fptr->iobuffer + (nbuff * IOBUFLEN), status);
Fptr->io_pos = filepos + IOBUFLEN;
if (filepos == Fptr->filesize) /* appended new record? */
Fptr->filesize += IOBUFLEN; /* increment the file size */
Fptr->dirty[nbuff] = FALSE;
}
else /* if record is beyond the EOF, append any other records */
/* and/or insert fill values if necessary */
{
/* move to EOF */
if (Fptr->io_pos != Fptr->filesize)
ffseek(Fptr, Fptr->filesize);
ibuff = NIOBUF; /* initialize to impossible value */
while(ibuff != nbuff) /* repeat until requested buffer is written */
{
minrec = (long) (Fptr->filesize / IOBUFLEN);
/* write lowest record beyond the EOF first */
irec = Fptr->bufrecnum[nbuff]; /* initially point to the requested buffer */
ibuff = nbuff;
for (ii = 0; ii < NIOBUF; ii++)
{
if (Fptr->bufrecnum[ii] >= minrec &&
Fptr->bufrecnum[ii] < irec)
{
irec = Fptr->bufrecnum[ii]; /* found a lower record */
ibuff = ii;
}
}
filepos = (LONGLONG)irec * IOBUFLEN; /* byte offset of record in file */
/* append 1 or more fill records if necessary */
if (filepos > Fptr->filesize)
{
nloop = (long) ((filepos - (Fptr->filesize)) / IOBUFLEN);
for (jj = 0; jj < nloop && !(*status); jj++)
ffwrite(Fptr, IOBUFLEN, zeros, status);
/*
ffseek(Fptr, filepos);
*/
Fptr->filesize = filepos; /* increment the file size */
}
/* write the buffer itself */
ffwrite(Fptr, IOBUFLEN, Fptr->iobuffer + (ibuff * IOBUFLEN), status);
Fptr->dirty[ibuff] = FALSE;
Fptr->filesize += IOBUFLEN; /* increment the file size */
} /* loop back if more buffers need to be written */
Fptr->io_pos = Fptr->filesize; /* currently positioned at EOF */
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffgrsz( fitsfile *fptr, /* I - FITS file pionter */
long *ndata, /* O - optimal amount of data to access */
int *status) /* IO - error status */
/*
Returns an optimal value for the number of rows in a binary table
or the number of pixels in an image that should be read or written
at one time for maximum efficiency. Accessing more data than this
may cause excessive flushing and rereading of buffers to/from disk.
*/
{
int typecode, bytesperpixel;
/* There are NIOBUF internal buffers available each IOBUFLEN bytes long. */
if (fptr->HDUposition != (fptr->Fptr)->curhdu)
ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);
else if ((fptr->Fptr)->datastart == DATA_UNDEFINED)
if ( ffrdef(fptr, status) > 0) /* rescan header to get hdu struct */
return(*status);
if ((fptr->Fptr)->hdutype == IMAGE_HDU ) /* calc pixels per buffer size */
{
/* image pixels are in column 2 of the 'table' */
ffgtcl(fptr, 2, &typecode, NULL, NULL, status);
bytesperpixel = typecode / 10;
*ndata = ((NIOBUF - 1) * IOBUFLEN) / bytesperpixel;
}
else /* calc number of rows that fit in buffers */
{
*ndata = (long) (((NIOBUF - 1) * IOBUFLEN) / maxvalue(1,
(fptr->Fptr)->rowlength));
*ndata = maxvalue(1, *ndata);
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffgtbb(fitsfile *fptr, /* I - FITS file pointer */
LONGLONG firstrow, /* I - starting row (1 = first row) */
LONGLONG firstchar, /* I - starting byte in row (1=first) */
LONGLONG nchars, /* I - number of bytes to read */
unsigned char *values, /* I - array of bytes to read */
int *status) /* IO - error status */
/*
read a consecutive string of bytes from an ascii or binary table.
This will span multiple rows of the table if nchars + firstchar is
greater than the length of a row.
*/
{
LONGLONG bytepos, endrow;
if (*status > 0 || nchars <= 0)
return(*status);
else if (firstrow < 1)
return(*status=BAD_ROW_NUM);
else if (firstchar < 1)
return(*status=BAD_ELEM_NUM);
if (fptr->HDUposition != (fptr->Fptr)->curhdu)
ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);
/* check that we do not exceed number of rows in the table */
endrow = ((firstchar + nchars - 2) / (fptr->Fptr)->rowlength) + firstrow;
if (endrow > (fptr->Fptr)->numrows)
{
ffpmsg("attempt to read past end of table (ffgtbb)");
return(*status=BAD_ROW_NUM);
}
/* move the i/o pointer to the start of the sequence of characters */
bytepos = (fptr->Fptr)->datastart +
((fptr->Fptr)->rowlength * (firstrow - 1)) +
firstchar - 1;
ffmbyt(fptr, bytepos, REPORT_EOF, status);
ffgbyt(fptr, nchars, values, status); /* read the bytes */
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffgi1b(fitsfile *fptr, /* I - FITS file pointer */
LONGLONG byteloc, /* I - position within file to start reading */
long nvals, /* I - number of pixels to read */
long incre, /* I - byte increment between pixels */
unsigned char *values, /* O - returned array of values */
int *status) /* IO - error status */
/*
get (read) the array of values from the FITS file, doing machine dependent
format conversion (e.g. byte-swapping) if necessary.
*/
{
LONGLONG postemp;
if (incre == 1) /* read all the values at once (contiguous bytes) */
{
if (nvals < MINDIRECT) /* read normally via IO buffers */
{
ffmbyt(fptr, byteloc, REPORT_EOF, status);
ffgbyt(fptr, nvals, values, status);
}
else /* read directly from disk, bypassing IO buffers */
{
postemp = (fptr->Fptr)->bytepos; /* store current file position */
(fptr->Fptr)->bytepos = byteloc; /* set to the desired position */
ffgbyt(fptr, nvals, values, status);
(fptr->Fptr)->bytepos = postemp; /* reset to original position */
}
}
else /* have to read each value individually (not contiguous ) */
{
ffmbyt(fptr, byteloc, REPORT_EOF, status);
ffgbytoff(fptr, 1, nvals, incre - 1, values, status);
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffgi2b(fitsfile *fptr, /* I - FITS file pointer */
LONGLONG byteloc, /* I - position within file to start reading */
long nvals, /* I - number of pixels to read */
long incre, /* I - byte increment between pixels */
short *values, /* O - returned array of values */
int *status) /* IO - error status */
/*
get (read) the array of values from the FITS file, doing machine dependent
format conversion (e.g. byte-swapping) if necessary.
*/
{
LONGLONG postemp;
if (incre == 2) /* read all the values at once (contiguous bytes) */
{
if (nvals * 2 < MINDIRECT) /* read normally via IO buffers */
{
ffmbyt(fptr, byteloc, REPORT_EOF, status);
ffgbyt(fptr, nvals * 2, values, status);
}
else /* read directly from disk, bypassing IO buffers */
{
postemp = (fptr->Fptr)->bytepos; /* store current file position */
(fptr->Fptr)->bytepos = byteloc; /* set to the desired position */
ffgbyt(fptr, nvals * 2, values, status);
(fptr->Fptr)->bytepos = postemp; /* reset to original position */
}
}
else /* have to read each value individually (not contiguous ) */
{
ffmbyt(fptr, byteloc, REPORT_EOF, status);
ffgbytoff(fptr, 2, nvals, incre - 2, values, status);
}
#if BYTESWAPPED
ffswap2(values, nvals); /* reverse order of bytes in each value */
#endif
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffgi4b(fitsfile *fptr, /* I - FITS file pointer */
LONGLONG byteloc, /* I - position within file to start reading */
long nvals, /* I - number of pixels to read */
long incre, /* I - byte increment between pixels */
INT32BIT *values, /* O - returned array of values */
int *status) /* IO - error status */
/*
get (read) the array of values from the FITS file, doing machine dependent
format conversion (e.g. byte-swapping) if necessary.
*/
{
LONGLONG postemp;
if (incre == 4) /* read all the values at once (contiguous bytes) */
{
if (nvals * 4 < MINDIRECT) /* read normally via IO buffers */
{
ffmbyt(fptr, byteloc, REPORT_EOF, status);
ffgbyt(fptr, nvals * 4, values, status);
}
else /* read directly from disk, bypassing IO buffers */
{
postemp = (fptr->Fptr)->bytepos; /* store current file position */
(fptr->Fptr)->bytepos = byteloc; /* set to the desired position */
ffgbyt(fptr, nvals * 4, values, status);
(fptr->Fptr)->bytepos = postemp; /* reset to original position */
}
}
else /* have to read each value individually (not contiguous ) */
{
ffmbyt(fptr, byteloc, REPORT_EOF, status);
ffgbytoff(fptr, 4, nvals, incre - 4, values, status);
}
#if BYTESWAPPED
ffswap4(values, nvals); /* reverse order of bytes in each value */
#endif
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffgi8b(fitsfile *fptr, /* I - FITS file pointer */
LONGLONG byteloc, /* I - position within file to start reading */
long nvals, /* I - number of pixels to read */
long incre, /* I - byte increment between pixels */
long *values, /* O - returned array of values */
int *status) /* IO - error status */
/*
get (read) the array of values from the FITS file, doing machine dependent
format conversion (e.g. byte-swapping) if necessary.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
This routine reads 'nvals' 8-byte integers into 'values'.
This works both on platforms that have sizeof(long) = 64, and 32,
as long as 'values' has been allocated to large enough to hold
8 * nvals bytes of data.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
*/
{
LONGLONG postemp;
if (incre == 8) /* read all the values at once (contiguous bytes) */
{
if (nvals * 8 < MINDIRECT) /* read normally via IO buffers */
{
ffmbyt(fptr, byteloc, REPORT_EOF, status);
ffgbyt(fptr, nvals * 8, values, status);
}
else /* read directly from disk, bypassing IO buffers */
{
postemp = (fptr->Fptr)->bytepos; /* store current file position */
(fptr->Fptr)->bytepos = byteloc; /* set to the desired position */
ffgbyt(fptr, nvals * 8, values, status);
(fptr->Fptr)->bytepos = postemp; /* reset to original position */
}
}
else /* have to read each value individually (not contiguous ) */
{
ffmbyt(fptr, byteloc, REPORT_EOF, status);
ffgbytoff(fptr, 8, nvals, incre - 8, values, status);
}
#if BYTESWAPPED
ffswap8((double *) values, nvals); /* reverse bytes in each value */
#endif
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffgr4b(fitsfile *fptr, /* I - FITS file pointer */
LONGLONG byteloc, /* I - position within file to start reading */
long nvals, /* I - number of pixels to read */
long incre, /* I - byte increment between pixels */
float *values, /* O - returned array of values */
int *status) /* IO - error status */
/*
get (read) the array of values from the FITS file, doing machine dependent
format conversion (e.g. byte-swapping) if necessary.
*/
{
LONGLONG postemp;