-
Notifications
You must be signed in to change notification settings - Fork 1
/
editcol.c
3229 lines (2771 loc) · 109 KB
/
editcol.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, editcol.c, contains the set of FITSIO routines that */
/* insert or delete rows or columns in a table or resize an image */
/* 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 <ctype.h>
#include <stdlib.h>
#include "fitsio2.h"
/*--------------------------------------------------------------------------*/
int ffrsim(fitsfile *fptr, /* I - FITS file pointer */
int bitpix, /* I - bits per pixel */
int naxis, /* I - number of axes in the array */
long *naxes, /* I - size of each axis */
int *status) /* IO - error status */
/*
resize an existing primary array or IMAGE extension.
*/
{
LONGLONG tnaxes[99];
int ii;
if (*status > 0)
return(*status);
for (ii = 0; (ii < naxis) && (ii < 99); ii++)
tnaxes[ii] = naxes[ii];
ffrsimll(fptr, bitpix, naxis, tnaxes, status);
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffrsimll(fitsfile *fptr, /* I - FITS file pointer */
int bitpix, /* I - bits per pixel */
int naxis, /* I - number of axes in the array */
LONGLONG *naxes, /* I - size of each axis */
int *status) /* IO - error status */
/*
resize an existing primary array or IMAGE extension.
*/
{
int ii, simple, obitpix, onaxis, extend, nmodify;
long nblocks, longval;
long pcount, gcount, longbitpix;
LONGLONG onaxes[99], newsize, oldsize;
char comment[FLEN_COMMENT], keyname[FLEN_KEYWORD], message[FLEN_ERRMSG];
if (*status > 0)
return(*status);
/* reset position to the correct HDU if necessary */
if (fptr->HDUposition != (fptr->Fptr)->curhdu)
{
ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);
}
/* rescan header if data structure is undefined */
else if ((fptr->Fptr)->datastart == DATA_UNDEFINED)
if ( ffrdef(fptr, status) > 0)
return(*status);
/* get current image size parameters */
if (ffghprll(fptr, 99, &simple, &obitpix, &onaxis, onaxes, &pcount,
&gcount, &extend, status) > 0)
return(*status);
longbitpix = bitpix;
/* test for the 4 special cases that represent unsigned integers
or signed bytes */
if (longbitpix == USHORT_IMG)
longbitpix = SHORT_IMG;
else if (longbitpix == ULONG_IMG)
longbitpix = LONG_IMG;
else if (longbitpix == SBYTE_IMG)
longbitpix = BYTE_IMG;
else if (longbitpix == ULONGLONG_IMG)
longbitpix = LONGLONG_IMG;
/* test that the new values are legal */
if (longbitpix != BYTE_IMG && longbitpix != SHORT_IMG &&
longbitpix != LONG_IMG && longbitpix != LONGLONG_IMG &&
longbitpix != FLOAT_IMG && longbitpix != DOUBLE_IMG)
{
snprintf(message, FLEN_ERRMSG,
"Illegal value for BITPIX keyword: %d", bitpix);
ffpmsg(message);
return(*status = BAD_BITPIX);
}
if (naxis < 0 || naxis > 999)
{
snprintf(message, FLEN_ERRMSG,
"Illegal value for NAXIS keyword: %d", naxis);
ffpmsg(message);
return(*status = BAD_NAXIS);
}
if (naxis == 0)
newsize = 0;
else
newsize = 1;
for (ii = 0; ii < naxis; ii++)
{
if (naxes[ii] < 0)
{
snprintf(message, FLEN_ERRMSG,
"Illegal value for NAXIS%d keyword: %.0f", ii + 1, (double) (naxes[ii]));
ffpmsg(message);
return(*status = BAD_NAXES);
}
newsize *= naxes[ii]; /* compute new image size, in pixels */
}
/* compute size of old image, in bytes */
if (onaxis == 0)
oldsize = 0;
else
{
oldsize = 1;
for (ii = 0; ii < onaxis; ii++)
oldsize *= onaxes[ii];
oldsize = (oldsize + pcount) * gcount * (abs(obitpix) / 8);
}
oldsize = (oldsize + 2879) / 2880; /* old size, in blocks */
newsize = (newsize + pcount) * gcount * (labs(longbitpix) / 8);
newsize = (newsize + 2879) / 2880; /* new size, in blocks */
if (newsize > oldsize) /* have to insert new blocks for image */
{
nblocks = (long) (newsize - oldsize);
if (ffiblk(fptr, nblocks, 1, status) > 0)
return(*status);
}
else if (oldsize > newsize) /* have to delete blocks from image */
{
nblocks = (long) (oldsize - newsize);
if (ffdblk(fptr, nblocks, status) > 0)
return(*status);
}
/* now update the header keywords */
strcpy(comment,"&"); /* special value to leave comments unchanged */
if (longbitpix != obitpix)
{ /* update BITPIX value */
ffmkyj(fptr, "BITPIX", longbitpix, comment, status);
}
if (naxis != onaxis)
{ /* update NAXIS value */
longval = naxis;
ffmkyj(fptr, "NAXIS", longval, comment, status);
}
/* modify the existing NAXISn keywords */
nmodify = minvalue(naxis, onaxis);
for (ii = 0; ii < nmodify; ii++)
{
ffkeyn("NAXIS", ii+1, keyname, status);
ffmkyj(fptr, keyname, naxes[ii], comment, status);
}
if (naxis > onaxis) /* insert additional NAXISn keywords */
{
strcpy(comment,"length of data axis");
for (ii = onaxis; ii < naxis; ii++)
{
ffkeyn("NAXIS", ii+1, keyname, status);
ffikyj(fptr, keyname, naxes[ii], comment, status);
}
}
else if (onaxis > naxis) /* delete old NAXISn keywords */
{
for (ii = naxis; ii < onaxis; ii++)
{
ffkeyn("NAXIS", ii+1, keyname, status);
ffdkey(fptr, keyname, status);
}
}
/* Update the BSCALE and BZERO keywords, if an unsigned integer image
or a signed byte image. */
if (bitpix == USHORT_IMG)
{
strcpy(comment, "offset data range to that of unsigned short");
ffukyg(fptr, "BZERO", 32768., 0, comment, status);
strcpy(comment, "default scaling factor");
ffukyg(fptr, "BSCALE", 1.0, 0, comment, status);
}
else if (bitpix == ULONG_IMG)
{
strcpy(comment, "offset data range to that of unsigned long");
ffukyg(fptr, "BZERO", 2147483648., 0, comment, status);
strcpy(comment, "default scaling factor");
ffukyg(fptr, "BSCALE", 1.0, 0, comment, status);
}
else if (bitpix == ULONGLONG_IMG)
{
strcpy(comment, "offset data range to that of unsigned long long");
ffukyg(fptr, "BZERO", 9223372036854775808., 0, comment, status);
strcpy(comment, "default scaling factor");
ffukyg(fptr, "BSCALE", 1.0, 0, comment, status);
}
else if (bitpix == SBYTE_IMG)
{
strcpy(comment, "offset data range to that of signed byte");
ffukyg(fptr, "BZERO", -128., 0, comment, status);
strcpy(comment, "default scaling factor");
ffukyg(fptr, "BSCALE", 1.0, 0, comment, status);
}
/* re-read the header, to make sure structures are updated */
ffrdef(fptr, status);
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffirow(fitsfile *fptr, /* I - FITS file pointer */
LONGLONG firstrow, /* I - insert space AFTER this row */
/* 0 = insert space at beginning of table */
LONGLONG nrows, /* I - number of rows to insert */
int *status) /* IO - error status */
/*
insert NROWS blank rows immediated after row firstrow (1 = first row).
Set firstrow = 0 to insert space at the beginning of the table.
*/
{
int tstatus;
LONGLONG naxis1, naxis2;
LONGLONG datasize, firstbyte, nshift, nbytes;
LONGLONG freespace;
long nblock;
if (*status > 0)
return(*status);
/* reset position to the correct HDU if necessary */
if (fptr->HDUposition != (fptr->Fptr)->curhdu)
{
ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);
}
/* rescan header if data structure is undefined */
else if ((fptr->Fptr)->datastart == DATA_UNDEFINED)
if ( ffrdef(fptr, status) > 0)
return(*status);
if ((fptr->Fptr)->hdutype == IMAGE_HDU)
{
ffpmsg("Can only add rows to TABLE or BINTABLE extension (ffirow)");
return(*status = NOT_TABLE);
}
if (nrows < 0 )
return(*status = NEG_BYTES);
else if (nrows == 0)
return(*status); /* no op, so just return */
/* get the current size of the table */
/* use internal structure since NAXIS2 keyword may not be up to date */
naxis1 = (fptr->Fptr)->rowlength;
naxis2 = (fptr->Fptr)->numrows;
if (firstrow > naxis2)
{
ffpmsg(
"Insert position greater than the number of rows in the table (ffirow)");
return(*status = BAD_ROW_NUM);
}
else if (firstrow < 0)
{
ffpmsg("Insert position is less than 0 (ffirow)");
return(*status = BAD_ROW_NUM);
}
/* current data size */
datasize = (fptr->Fptr)->heapstart + (fptr->Fptr)->heapsize;
freespace = ( ( (datasize + 2879) / 2880) * 2880) - datasize;
nshift = naxis1 * nrows; /* no. of bytes to add to table */
if ( (freespace - nshift) < 0) /* not enough existing space? */
{
nblock = (long) ((nshift - freespace + 2879) / 2880); /* number of blocks */
ffiblk(fptr, nblock, 1, status); /* insert the blocks */
}
firstbyte = naxis1 * firstrow; /* relative insert position */
nbytes = datasize - firstbyte; /* no. of bytes to shift down */
firstbyte += ((fptr->Fptr)->datastart); /* absolute insert position */
if (nshift > 0) { /* nshift may be zero if naxis1 == naxis2 == 0 */
ffshft(fptr, firstbyte, nbytes, nshift, status); /* shift rows and heap */
}
/* update the heap starting address */
(fptr->Fptr)->heapstart += nshift;
/* update the THEAP keyword if it exists */
tstatus = 0;
ffmkyj(fptr, "THEAP", (fptr->Fptr)->heapstart, "&", &tstatus);
/* update the NAXIS2 keyword */
ffmkyj(fptr, "NAXIS2", naxis2 + nrows, "&", status);
((fptr->Fptr)->numrows) += nrows;
((fptr->Fptr)->origrows) += nrows;
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffdrow(fitsfile *fptr, /* I - FITS file pointer */
LONGLONG firstrow, /* I - first row to delete (1 = first) */
LONGLONG nrows, /* I - number of rows to delete */
int *status) /* IO - error status */
/*
delete NROWS rows from table starting with firstrow (1 = first row of table).
*/
{
int tstatus;
LONGLONG naxis1, naxis2;
LONGLONG datasize, firstbyte, nbytes, nshift;
LONGLONG freespace;
long nblock;
char comm[FLEN_COMMENT];
if (*status > 0)
return(*status);
if (fptr->HDUposition != (fptr->Fptr)->curhdu)
{
ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);
}
/* rescan header if data structure is undefined */
else if ((fptr->Fptr)->datastart == DATA_UNDEFINED)
if ( ffrdef(fptr, status) > 0)
return(*status);
if ((fptr->Fptr)->hdutype == IMAGE_HDU)
{
ffpmsg("Can only delete rows in TABLE or BINTABLE extension (ffdrow)");
return(*status = NOT_TABLE);
}
if (nrows < 0 )
return(*status = NEG_BYTES);
else if (nrows == 0)
return(*status); /* no op, so just return */
ffgkyjj(fptr, "NAXIS1", &naxis1, comm, status); /* get the current */
/* ffgkyj(fptr, "NAXIS2", &naxis2, comm, status);*/ /* size of the table */
/* the NAXIS2 keyword may not be up to date, so use the structure value */
naxis2 = (fptr->Fptr)->numrows;
if (firstrow > naxis2)
{
ffpmsg(
"Delete position greater than the number of rows in the table (ffdrow)");
return(*status = BAD_ROW_NUM);
}
else if (firstrow < 1)
{
ffpmsg("Delete position is less than 1 (ffdrow)");
return(*status = BAD_ROW_NUM);
}
else if (firstrow + nrows - 1 > naxis2)
{
ffpmsg("No. of rows to delete exceeds size of table (ffdrow)");
return(*status = BAD_ROW_NUM);
}
nshift = naxis1 * nrows; /* no. of bytes to delete from table */
/* cur size of data */
datasize = (fptr->Fptr)->heapstart + (fptr->Fptr)->heapsize;
firstbyte = naxis1 * (firstrow + nrows - 1); /* relative del pos */
nbytes = datasize - firstbyte; /* no. of bytes to shift up */
firstbyte += ((fptr->Fptr)->datastart); /* absolute delete position */
ffshft(fptr, firstbyte, nbytes, nshift * (-1), status); /* shift data */
freespace = ( ( (datasize + 2879) / 2880) * 2880) - datasize;
nblock = (long) ((nshift + freespace) / 2880); /* number of blocks */
/* delete integral number blocks */
if (nblock > 0)
ffdblk(fptr, nblock, status);
/* update the heap starting address */
(fptr->Fptr)->heapstart -= nshift;
/* update the THEAP keyword if it exists */
tstatus = 0;
ffmkyj(fptr, "THEAP", (long)(fptr->Fptr)->heapstart, "&", &tstatus);
/* update the NAXIS2 keyword */
ffmkyj(fptr, "NAXIS2", naxis2 - nrows, "&", status);
((fptr->Fptr)->numrows) -= nrows;
((fptr->Fptr)->origrows) -= nrows;
/* Update the heap data, if any. This will remove any orphaned data */
/* that was only pointed to by the rows that have been deleted */
ffcmph(fptr, status);
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffdrrg(fitsfile *fptr, /* I - FITS file pointer to table */
char *ranges, /* I - ranges of rows to delete (1 = first) */
int *status) /* IO - error status */
/*
delete the ranges of rows from the table (1 = first row of table).
The 'ranges' parameter typically looks like:
'10-20, 30 - 40, 55' or '50-'
and gives a list of rows or row ranges separated by commas.
*/
{
char *cptr;
int nranges, nranges2, ii;
long *minrow, *maxrow, nrows, *rowarray, jj, kk;
LONGLONG naxis2;
if (*status > 0)
return(*status);
if (fptr->HDUposition != (fptr->Fptr)->curhdu)
{
ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);
}
/* rescan header if data structure is undefined */
else if ((fptr->Fptr)->datastart == DATA_UNDEFINED)
if ( ffrdef(fptr, status) > 0)
return(*status);
if ((fptr->Fptr)->hdutype == IMAGE_HDU)
{
ffpmsg("Can only delete rows in TABLE or BINTABLE extension (ffdrrg)");
return(*status = NOT_TABLE);
}
/* the NAXIS2 keyword may not be up to date, so use the structure value */
naxis2 = (fptr->Fptr)->numrows;
/* find how many ranges were specified ( = no. of commas in string + 1) */
cptr = ranges;
for (nranges = 1; (cptr = strchr(cptr, ',')); nranges++)
cptr++;
minrow = calloc(nranges, sizeof(long));
maxrow = calloc(nranges, sizeof(long));
if (!minrow || !maxrow) {
*status = MEMORY_ALLOCATION;
ffpmsg("failed to allocate memory for row ranges (ffdrrg)");
if (maxrow) free(maxrow);
if (minrow) free(minrow);
return(*status);
}
/* parse range list into array of range min and max values */
ffrwrg(ranges, naxis2, nranges, &nranges2, minrow, maxrow, status);
if (*status > 0 || nranges2 == 0) {
free(maxrow);
free(minrow);
return(*status);
}
/* determine total number or rows to delete */
nrows = 0;
for (ii = 0; ii < nranges2; ii++) {
nrows = nrows + maxrow[ii] - minrow[ii] + 1;
}
rowarray = calloc(nrows, sizeof(long));
if (!rowarray) {
*status = MEMORY_ALLOCATION;
ffpmsg("failed to allocate memory for row array (ffdrrg)");
return(*status);
}
for (kk = 0, ii = 0; ii < nranges2; ii++) {
for (jj = minrow[ii]; jj <= maxrow[ii]; jj++) {
rowarray[kk] = jj;
kk++;
}
}
/* delete the rows */
ffdrws(fptr, rowarray, nrows, status);
free(rowarray);
free(maxrow);
free(minrow);
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffdrws(fitsfile *fptr, /* I - FITS file pointer */
long *rownum, /* I - list of rows to delete (1 = first) */
long nrows, /* I - number of rows to delete */
int *status) /* IO - error status */
/*
delete the list of rows from the table (1 = first row of table).
*/
{
LONGLONG naxis1, naxis2, insertpos, nextrowpos;
long ii, nextrow;
char comm[FLEN_COMMENT];
unsigned char *buffer;
if (*status > 0)
return(*status);
if (fptr->HDUposition != (fptr->Fptr)->curhdu)
ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);
/* rescan header if data structure is undefined */
if ((fptr->Fptr)->datastart == DATA_UNDEFINED)
if ( ffrdef(fptr, status) > 0)
return(*status);
if ((fptr->Fptr)->hdutype == IMAGE_HDU)
{
ffpmsg("Can only delete rows in TABLE or BINTABLE extension (ffdrws)");
return(*status = NOT_TABLE);
}
if (nrows < 0 )
return(*status = NEG_BYTES);
else if (nrows == 0)
return(*status); /* no op, so just return */
ffgkyjj(fptr, "NAXIS1", &naxis1, comm, status); /* row width */
ffgkyjj(fptr, "NAXIS2", &naxis2, comm, status); /* number of rows */
/* check that input row list is in ascending order */
for (ii = 1; ii < nrows; ii++)
{
if (rownum[ii - 1] >= rownum[ii])
{
ffpmsg("row numbers are not in increasing order (ffdrws)");
return(*status = BAD_ROW_NUM);
}
}
if (rownum[0] < 1)
{
ffpmsg("first row to delete is less than 1 (ffdrws)");
return(*status = BAD_ROW_NUM);
}
else if (rownum[nrows - 1] > naxis2)
{
ffpmsg("last row to delete exceeds size of table (ffdrws)");
return(*status = BAD_ROW_NUM);
}
buffer = (unsigned char *) malloc( (size_t) naxis1); /* buffer for one row */
if (!buffer)
{
ffpmsg("malloc failed (ffdrws)");
return(*status = MEMORY_ALLOCATION);
}
/* byte location to start of first row to delete, and the next row */
insertpos = (fptr->Fptr)->datastart + ((rownum[0] - 1) * naxis1);
nextrowpos = insertpos + naxis1;
nextrow = rownum[0] + 1;
/* work through the list of rows to delete */
for (ii = 1; ii < nrows; nextrow++, nextrowpos += naxis1)
{
if (nextrow < rownum[ii])
{ /* keep this row, so copy it to the new position */
ffmbyt(fptr, nextrowpos, REPORT_EOF, status);
ffgbyt(fptr, naxis1, buffer, status); /* read the bytes */
ffmbyt(fptr, insertpos, IGNORE_EOF, status);
ffpbyt(fptr, naxis1, buffer, status); /* write the bytes */
if (*status > 0)
{
ffpmsg("error while copying good rows in table (ffdrws)");
free(buffer);
return(*status);
}
insertpos += naxis1;
}
else
{ /* skip over this row since it is in the list */
ii++;
}
}
/* finished with all the rows to delete; copy remaining rows */
while(nextrow <= naxis2)
{
ffmbyt(fptr, nextrowpos, REPORT_EOF, status);
ffgbyt(fptr, naxis1, buffer, status); /* read the bytes */
ffmbyt(fptr, insertpos, IGNORE_EOF, status);
ffpbyt(fptr, naxis1, buffer, status); /* write the bytes */
if (*status > 0)
{
ffpmsg("failed to copy remaining rows in table (ffdrws)");
free(buffer);
return(*status);
}
insertpos += naxis1;
nextrowpos += naxis1;
nextrow++;
}
free(buffer);
/* now delete the empty rows at the end of the table */
ffdrow(fptr, naxis2 - nrows + 1, nrows, status);
/* Update the heap data, if any. This will remove any orphaned data */
/* that was only pointed to by the rows that have been deleted */
ffcmph(fptr, status);
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffdrwsll(fitsfile *fptr, /* I - FITS file pointer */
LONGLONG *rownum, /* I - list of rows to delete (1 = first) */
LONGLONG nrows, /* I - number of rows to delete */
int *status) /* IO - error status */
/*
delete the list of rows from the table (1 = first row of table).
*/
{
LONGLONG insertpos, nextrowpos;
LONGLONG naxis1, naxis2, ii, nextrow;
char comm[FLEN_COMMENT];
unsigned char *buffer;
if (*status > 0)
return(*status);
if (fptr->HDUposition != (fptr->Fptr)->curhdu)
ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);
/* rescan header if data structure is undefined */
if ((fptr->Fptr)->datastart == DATA_UNDEFINED)
if ( ffrdef(fptr, status) > 0)
return(*status);
if ((fptr->Fptr)->hdutype == IMAGE_HDU)
{
ffpmsg("Can only delete rows in TABLE or BINTABLE extension (ffdrws)");
return(*status = NOT_TABLE);
}
if (nrows < 0 )
return(*status = NEG_BYTES);
else if (nrows == 0)
return(*status); /* no op, so just return */
ffgkyjj(fptr, "NAXIS1", &naxis1, comm, status); /* row width */
ffgkyjj(fptr, "NAXIS2", &naxis2, comm, status); /* number of rows */
/* check that input row list is in ascending order */
for (ii = 1; ii < nrows; ii++)
{
if (rownum[ii - 1] >= rownum[ii])
{
ffpmsg("row numbers are not in increasing order (ffdrws)");
return(*status = BAD_ROW_NUM);
}
}
if (rownum[0] < 1)
{
ffpmsg("first row to delete is less than 1 (ffdrws)");
return(*status = BAD_ROW_NUM);
}
else if (rownum[nrows - 1] > naxis2)
{
ffpmsg("last row to delete exceeds size of table (ffdrws)");
return(*status = BAD_ROW_NUM);
}
buffer = (unsigned char *) malloc( (size_t) naxis1); /* buffer for one row */
if (!buffer)
{
ffpmsg("malloc failed (ffdrwsll)");
return(*status = MEMORY_ALLOCATION);
}
/* byte location to start of first row to delete, and the next row */
insertpos = (fptr->Fptr)->datastart + ((rownum[0] - 1) * naxis1);
nextrowpos = insertpos + naxis1;
nextrow = rownum[0] + 1;
/* work through the list of rows to delete */
for (ii = 1; ii < nrows; nextrow++, nextrowpos += naxis1)
{
if (nextrow < rownum[ii])
{ /* keep this row, so copy it to the new position */
ffmbyt(fptr, nextrowpos, REPORT_EOF, status);
ffgbyt(fptr, naxis1, buffer, status); /* read the bytes */
ffmbyt(fptr, insertpos, IGNORE_EOF, status);
ffpbyt(fptr, naxis1, buffer, status); /* write the bytes */
if (*status > 0)
{
ffpmsg("error while copying good rows in table (ffdrws)");
free(buffer);
return(*status);
}
insertpos += naxis1;
}
else
{ /* skip over this row since it is in the list */
ii++;
}
}
/* finished with all the rows to delete; copy remaining rows */
while(nextrow <= naxis2)
{
ffmbyt(fptr, nextrowpos, REPORT_EOF, status);
ffgbyt(fptr, naxis1, buffer, status); /* read the bytes */
ffmbyt(fptr, insertpos, IGNORE_EOF, status);
ffpbyt(fptr, naxis1, buffer, status); /* write the bytes */
if (*status > 0)
{
ffpmsg("failed to copy remaining rows in table (ffdrws)");
free(buffer);
return(*status);
}
insertpos += naxis1;
nextrowpos += naxis1;
nextrow++;
}
free(buffer);
/* now delete the empty rows at the end of the table */
ffdrow(fptr, naxis2 - nrows + 1, nrows, status);
/* Update the heap data, if any. This will remove any orphaned data */
/* that was only pointed to by the rows that have been deleted */
ffcmph(fptr, status);
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffrwrg(
char *rowlist, /* I - list of rows and row ranges */
LONGLONG maxrows, /* I - number of rows in the table */
int maxranges, /* I - max number of ranges to be returned */
int *numranges, /* O - number ranges returned */
long *minrow, /* O - first row in each range */
long *maxrow, /* O - last row in each range */
int *status) /* IO - status value */
{
/*
parse the input list of row ranges, returning the number of ranges,
and the min and max row value in each range.
The only characters allowed in the input rowlist are
decimal digits, minus sign, and comma (and non-significant spaces)
Example:
list = "10-20, 30-35,50"
would return numranges = 3, minrow[] = {10, 30, 50}, maxrow[] = {20, 35, 50}
error is returned if min value of range is > max value of range or if the
ranges are not monotonically increasing.
*/
char *next;
long minval, maxval;
if (*status > 0)
return(*status);
if (maxrows <= 0 ) {
*status = RANGE_PARSE_ERROR;
ffpmsg("Input maximum range value is <= 0 (fits_parse_ranges)");
return(*status);
}
next = rowlist;
*numranges = 0;
while (*next == ' ')next++; /* skip spaces */
while (*next != '\0') {
/* find min value of next range; *next must be '-' or a digit */
if (*next == '-') {
minval = 1; /* implied minrow value = 1 */
} else if ( isdigit((int) *next) ) {
minval = strtol(next, &next, 10);
} else {
*status = RANGE_PARSE_ERROR;
ffpmsg("Syntax error in this row range list:");
ffpmsg(rowlist);
return(*status);
}
while (*next == ' ')next++; /* skip spaces */
/* find max value of next range; *next must be '-', or ',' */
if (*next == '-') {
next++;
while (*next == ' ')next++; /* skip spaces */
if ( isdigit((int) *next) ) {
maxval = strtol(next, &next, 10);
} else if (*next == ',' || *next == '\0') {
maxval = (long) maxrows; /* implied max value */
} else {
*status = RANGE_PARSE_ERROR;
ffpmsg("Syntax error in this row range list:");
ffpmsg(rowlist);
return(*status);
}
} else if (*next == ',' || *next == '\0') {
maxval = minval; /* only a single integer in this range */
} else {
*status = RANGE_PARSE_ERROR;
ffpmsg("Syntax error in this row range list:");
ffpmsg(rowlist);
return(*status);
}
if (*numranges + 1 > maxranges) {
*status = RANGE_PARSE_ERROR;
ffpmsg("Overflowed maximum number of ranges (fits_parse_ranges)");
return(*status);
}
if (minval < 1 ) {
*status = RANGE_PARSE_ERROR;
ffpmsg("Syntax error in this row range list: row number < 1");
ffpmsg(rowlist);
return(*status);
}
if (maxval < minval) {
*status = RANGE_PARSE_ERROR;
ffpmsg("Syntax error in this row range list: min > max");
ffpmsg(rowlist);
return(*status);
}
if (*numranges > 0) {
if (minval <= maxrow[(*numranges) - 1]) {
*status = RANGE_PARSE_ERROR;
ffpmsg("Syntax error in this row range list. Range minimum is");
ffpmsg(" less than or equal to previous range maximum");
ffpmsg(rowlist);
return(*status);
}
}
if (minval <= maxrows) { /* ignore range if greater than maxrows */
if (maxval > maxrows)
maxval = (long) maxrows;
minrow[*numranges] = minval;
maxrow[*numranges] = maxval;
(*numranges)++;
}
while (*next == ' ')next++; /* skip spaces */
if (*next == ',') {
next++;
while (*next == ' ')next++; /* skip more spaces */
}
}
if (*numranges == 0) { /* a null string was entered */
minrow[0] = 1;
maxrow[0] = (long) maxrows;
*numranges = 1;
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffrwrgll(
char *rowlist, /* I - list of rows and row ranges */
LONGLONG maxrows, /* I - number of rows in the list */
int maxranges, /* I - max number of ranges to be returned */
int *numranges, /* O - number ranges returned */
LONGLONG *minrow, /* O - first row in each range */
LONGLONG *maxrow, /* O - last row in each range */
int *status) /* IO - status value */
{
/*
parse the input list of row ranges, returning the number of ranges,
and the min and max row value in each range.
The only characters allowed in the input rowlist are
decimal digits, minus sign, and comma (and non-significant spaces)
Example:
list = "10-20, 30-35,50"
would return numranges = 3, minrow[] = {10, 30, 50}, maxrow[] = {20, 35, 50}
error is returned if min value of range is > max value of range or if the
ranges are not monotonically increasing.
*/
char *next;
LONGLONG minval, maxval;
double dvalue;
if (*status > 0)
return(*status);
if (maxrows <= 0 ) {
*status = RANGE_PARSE_ERROR;
ffpmsg("Input maximum range value is <= 0 (fits_parse_ranges)");
return(*status);
}
next = rowlist;
*numranges = 0;
while (*next == ' ')next++; /* skip spaces */
while (*next != '\0') {
/* find min value of next range; *next must be '-' or a digit */
if (*next == '-') {
minval = 1; /* implied minrow value = 1 */
} else if ( isdigit((int) *next) ) {
/* read as a double, because the string to LONGLONG function */
/* is platform dependent (strtoll, strtol, _atoI64) */
dvalue = strtod(next, &next);
minval = (LONGLONG) (dvalue + 0.1);
} else {
*status = RANGE_PARSE_ERROR;
ffpmsg("Syntax error in this row range list:");
ffpmsg(rowlist);
return(*status);
}
while (*next == ' ')next++; /* skip spaces */
/* find max value of next range; *next must be '-', or ',' */
if (*next == '-') {
next++;
while (*next == ' ')next++; /* skip spaces */
if ( isdigit((int) *next) ) {
/* read as a double, because the string to LONGLONG function */
/* is platform dependent (strtoll, strtol, _atoI64) */
dvalue = strtod(next, &next);
maxval = (LONGLONG) (dvalue + 0.1);
} else if (*next == ',' || *next == '\0') {
maxval = maxrows; /* implied max value */
} else {
*status = RANGE_PARSE_ERROR;
ffpmsg("Syntax error in this row range list:");
ffpmsg(rowlist);
return(*status);
}
} else if (*next == ',' || *next == '\0') {
maxval = minval; /* only a single integer in this range */
} else {
*status = RANGE_PARSE_ERROR;
ffpmsg("Syntax error in this row range list:");
ffpmsg(rowlist);
return(*status);
}
if (*numranges + 1 > maxranges) {
*status = RANGE_PARSE_ERROR;
ffpmsg("Overflowed maximum number of ranges (fits_parse_ranges)");
return(*status);
}