| |
| |
|
|
| |
| |
| |
|
|
| #include <string.h> |
| #include <ctype.h> |
| #include <stdlib.h> |
| #include "fitsio2.h" |
| |
| int ffrsim(fitsfile *fptr, |
| int bitpix, |
| int naxis, |
| long *naxes, |
| int *status) |
| |
| |
| |
| { |
| 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, |
| int bitpix, |
| int naxis, |
| LONGLONG *naxes, |
| int *status) |
| |
| |
| |
| { |
| 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); |
|
|
| |
| 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) |
| return(*status); |
|
|
| |
| if (ffghprll(fptr, 99, &simple, &obitpix, &onaxis, onaxes, &pcount, |
| &gcount, &extend, status) > 0) |
| return(*status); |
|
|
| longbitpix = bitpix; |
|
|
| |
| if (longbitpix == USHORT_IMG) |
| longbitpix = SHORT_IMG; |
| else if (longbitpix == ULONG_IMG) |
| longbitpix = LONG_IMG; |
|
|
| |
|
|
| 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]; |
| } |
|
|
| |
|
|
| 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; |
|
|
| newsize = (newsize + pcount) * gcount * (abs(longbitpix) / 8); |
| newsize = (newsize + 2879) / 2880; |
|
|
| if (newsize > oldsize) |
| { |
| nblocks = (long) (newsize - oldsize); |
| if (ffiblk(fptr, nblocks, 1, status) > 0) |
| return(*status); |
| } |
| else if (oldsize > newsize) |
| { |
| nblocks = (long) (oldsize - newsize); |
| if (ffdblk(fptr, nblocks, status) > 0) |
| return(*status); |
| } |
|
|
| |
|
|
| strcpy(comment,"&"); |
|
|
| if (longbitpix != obitpix) |
| { |
| ffmkyj(fptr, "BITPIX", longbitpix, comment, status); |
| } |
|
|
| if (naxis != onaxis) |
| { |
| longval = naxis; |
| ffmkyj(fptr, "NAXIS", longval, comment, status); |
| } |
|
|
| |
| 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) |
| { |
| 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) |
| { |
| for (ii = naxis; ii < onaxis; ii++) |
| { |
| ffkeyn("NAXIS", ii+1, keyname, status); |
| ffdkey(fptr, keyname, status); |
| } |
| } |
|
|
| |
| 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); |
| } |
|
|
| |
| ffrdef(fptr, status); |
| return(*status); |
| } |
| |
| int ffirow(fitsfile *fptr, |
| LONGLONG firstrow, |
| |
| LONGLONG nrows, |
| int *status) |
| |
| |
| |
| |
| { |
| int tstatus; |
| LONGLONG naxis1, naxis2; |
| LONGLONG datasize, firstbyte, nshift, nbytes; |
| LONGLONG freespace; |
| long nblock; |
|
|
| if (*status > 0) |
| return(*status); |
|
|
| |
| 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) |
| 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); |
|
|
| |
| |
| 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); |
| } |
|
|
| |
| datasize = (fptr->Fptr)->heapstart + (fptr->Fptr)->heapsize; |
| freespace = ( ( (datasize + 2879) / 2880) * 2880) - datasize; |
| nshift = naxis1 * nrows; |
|
|
| if ( (freespace - nshift) < 0) |
| { |
| nblock = (long) ((nshift - freespace + 2879) / 2880); |
| ffiblk(fptr, nblock, 1, status); |
| } |
|
|
| firstbyte = naxis1 * firstrow; |
| nbytes = datasize - firstbyte; |
| firstbyte += ((fptr->Fptr)->datastart); |
|
|
| ffshft(fptr, firstbyte, nbytes, nshift, status); |
|
|
| |
| (fptr->Fptr)->heapstart += nshift; |
|
|
| |
| tstatus = 0; |
| ffmkyj(fptr, "THEAP", (fptr->Fptr)->heapstart, "&", &tstatus); |
|
|
| |
| ffmkyj(fptr, "NAXIS2", naxis2 + nrows, "&", status); |
| ((fptr->Fptr)->numrows) += nrows; |
| ((fptr->Fptr)->origrows) += nrows; |
|
|
| return(*status); |
| } |
| |
| int ffdrow(fitsfile *fptr, |
| LONGLONG firstrow, |
| LONGLONG nrows, |
| int *status) |
| |
| |
| |
| { |
| 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); |
| } |
| |
| 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); |
|
|
| ffgkyjj(fptr, "NAXIS1", &naxis1, comm, status); |
|
|
| |
|
|
| |
| 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; |
| |
| datasize = (fptr->Fptr)->heapstart + (fptr->Fptr)->heapsize; |
|
|
| firstbyte = naxis1 * (firstrow + nrows - 1); |
| nbytes = datasize - firstbyte; |
| firstbyte += ((fptr->Fptr)->datastart); |
|
|
| ffshft(fptr, firstbyte, nbytes, nshift * (-1), status); |
|
|
| freespace = ( ( (datasize + 2879) / 2880) * 2880) - datasize; |
| nblock = (long) ((nshift + freespace) / 2880); |
|
|
| |
| if (nblock > 0) |
| ffdblk(fptr, nblock, status); |
|
|
| |
| (fptr->Fptr)->heapstart -= nshift; |
|
|
| |
| tstatus = 0; |
| ffmkyj(fptr, "THEAP", (long)(fptr->Fptr)->heapstart, "&", &tstatus); |
|
|
| |
| ffmkyj(fptr, "NAXIS2", naxis2 - nrows, "&", status); |
| ((fptr->Fptr)->numrows) -= nrows; |
| ((fptr->Fptr)->origrows) -= nrows; |
|
|
| |
| |
| ffcmph(fptr, status); |
| return(*status); |
| } |
| |
| int ffdrrg(fitsfile *fptr, |
| char *ranges, |
| int *status) |
| |
| |
| |
| |
| |
| |
| |
| { |
| 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); |
| } |
| |
| 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); |
| } |
|
|
| |
| naxis2 = (fptr->Fptr)->numrows; |
|
|
| |
| 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); |
| } |
|
|
| |
| ffrwrg(ranges, naxis2, nranges, &nranges2, minrow, maxrow, status); |
| if (*status > 0 || nranges2 == 0) { |
| free(maxrow); |
| free(minrow); |
| return(*status); |
| } |
|
|
| |
| 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++; |
| } |
| } |
|
|
| |
| ffdrws(fptr, rowarray, nrows, status); |
| |
| free(rowarray); |
| free(maxrow); |
| free(minrow); |
| return(*status); |
| } |
| |
| int ffdrws(fitsfile *fptr, |
| long *rownum, |
| long nrows, |
| int *status) |
| |
| |
| |
| { |
| 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); |
|
|
| |
| 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); |
|
|
| ffgkyjj(fptr, "NAXIS1", &naxis1, comm, status); |
| ffgkyjj(fptr, "NAXIS2", &naxis2, comm, status); |
|
|
| |
| 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); |
|
|
| if (!buffer) |
| { |
| ffpmsg("malloc failed (ffdrws)"); |
| return(*status = MEMORY_ALLOCATION); |
| } |
|
|
| |
| insertpos = (fptr->Fptr)->datastart + ((rownum[0] - 1) * naxis1); |
| nextrowpos = insertpos + naxis1; |
| nextrow = rownum[0] + 1; |
|
|
| |
| for (ii = 1; ii < nrows; nextrow++, nextrowpos += naxis1) |
| { |
| if (nextrow < rownum[ii]) |
| { |
|
|
| ffmbyt(fptr, nextrowpos, REPORT_EOF, status); |
| ffgbyt(fptr, naxis1, buffer, status); |
|
|
| ffmbyt(fptr, insertpos, IGNORE_EOF, status); |
| ffpbyt(fptr, naxis1, buffer, status); |
|
|
| if (*status > 0) |
| { |
| ffpmsg("error while copying good rows in table (ffdrws)"); |
| free(buffer); |
| return(*status); |
| } |
| insertpos += naxis1; |
| } |
| else |
| { |
| ii++; |
| } |
| } |
|
|
| |
| while(nextrow <= naxis2) |
| { |
| ffmbyt(fptr, nextrowpos, REPORT_EOF, status); |
| ffgbyt(fptr, naxis1, buffer, status); |
|
|
| ffmbyt(fptr, insertpos, IGNORE_EOF, status); |
| ffpbyt(fptr, naxis1, buffer, status); |
|
|
| if (*status > 0) |
| { |
| ffpmsg("failed to copy remaining rows in table (ffdrws)"); |
| free(buffer); |
| return(*status); |
| } |
| insertpos += naxis1; |
| nextrowpos += naxis1; |
| nextrow++; |
| } |
| free(buffer); |
| |
| |
| ffdrow(fptr, naxis2 - nrows + 1, nrows, status); |
|
|
| |
| |
| ffcmph(fptr, status); |
| |
| return(*status); |
| } |
| |
| int ffdrwsll(fitsfile *fptr, |
| LONGLONG *rownum, |
| LONGLONG nrows, |
| int *status) |
| |
| |
| |
| { |
| 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); |
|
|
| |
| 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); |
|
|
| ffgkyjj(fptr, "NAXIS1", &naxis1, comm, status); |
| ffgkyjj(fptr, "NAXIS2", &naxis2, comm, status); |
|
|
| |
| 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); |
|
|
| if (!buffer) |
| { |
| ffpmsg("malloc failed (ffdrwsll)"); |
| return(*status = MEMORY_ALLOCATION); |
| } |
|
|
| |
| insertpos = (fptr->Fptr)->datastart + ((rownum[0] - 1) * naxis1); |
| nextrowpos = insertpos + naxis1; |
| nextrow = rownum[0] + 1; |
|
|
| |
| for (ii = 1; ii < nrows; nextrow++, nextrowpos += naxis1) |
| { |
| if (nextrow < rownum[ii]) |
| { |
|
|
| ffmbyt(fptr, nextrowpos, REPORT_EOF, status); |
| ffgbyt(fptr, naxis1, buffer, status); |
|
|
| ffmbyt(fptr, insertpos, IGNORE_EOF, status); |
| ffpbyt(fptr, naxis1, buffer, status); |
|
|
| if (*status > 0) |
| { |
| ffpmsg("error while copying good rows in table (ffdrws)"); |
| free(buffer); |
| return(*status); |
| } |
| insertpos += naxis1; |
| } |
| else |
| { |
| ii++; |
| } |
| } |
|
|
| |
| while(nextrow <= naxis2) |
| { |
| ffmbyt(fptr, nextrowpos, REPORT_EOF, status); |
| ffgbyt(fptr, naxis1, buffer, status); |
|
|
| ffmbyt(fptr, insertpos, IGNORE_EOF, status); |
| ffpbyt(fptr, naxis1, buffer, status); |
|
|
| if (*status > 0) |
| { |
| ffpmsg("failed to copy remaining rows in table (ffdrws)"); |
| free(buffer); |
| return(*status); |
| } |
| insertpos += naxis1; |
| nextrowpos += naxis1; |
| nextrow++; |
| } |
| free(buffer); |
| |
| |
| ffdrow(fptr, naxis2 - nrows + 1, nrows, status); |
|
|
| |
| |
| ffcmph(fptr, status); |
| |
| return(*status); |
| } |
| |
| int ffrwrg( |
| char *rowlist, |
| LONGLONG maxrows, |
| int maxranges, |
| int *numranges, |
| long *minrow, |
| long *maxrow, |
| int *status) |
| { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| 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++; |
| |
| while (*next != '\0') { |
|
|
| |
| if (*next == '-') { |
| minval = 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++; |
|
|
| |
| if (*next == '-') { |
| next++; |
| while (*next == ' ')next++; |
|
|
| if ( isdigit((int) *next) ) { |
| maxval = strtol(next, &next, 10); |
| } else if (*next == ',' || *next == '\0') { |
| maxval = (long) maxrows; |
| } else { |
| *status = RANGE_PARSE_ERROR; |
| ffpmsg("Syntax error in this row range list:"); |
| ffpmsg(rowlist); |
| return(*status); |
| } |
| } else if (*next == ',' || *next == '\0') { |
| maxval = minval; |
| } 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) { |
| if (maxval > maxrows) |
| maxval = (long) maxrows; |
|
|
| minrow[*numranges] = minval; |
| maxrow[*numranges] = maxval; |
|
|
| (*numranges)++; |
| } |
|
|
| while (*next == ' ')next++; |
| if (*next == ',') { |
| next++; |
| while (*next == ' ')next++; |
| } |
| } |
|
|
| if (*numranges == 0) { |
| minrow[0] = 1; |
| maxrow[0] = (long) maxrows; |
| *numranges = 1; |
| } |
|
|
| return(*status); |
| } |
| |
| int ffrwrgll( |
| char *rowlist, |
| LONGLONG maxrows, |
| int maxranges, |
| int *numranges, |
| LONGLONG *minrow, |
| LONGLONG *maxrow, |
| int *status) |
| { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| 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++; |
| |
| while (*next != '\0') { |
|
|
| |
| if (*next == '-') { |
| minval = 1; |
| } else if ( isdigit((int) *next) ) { |
|
|
| |
| |
|
|
| 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++; |
|
|
| |
| if (*next == '-') { |
| next++; |
| while (*next == ' ')next++; |
|
|
| if ( isdigit((int) *next) ) { |
|
|
| |
| |
|
|
| dvalue = strtod(next, &next); |
| maxval = (LONGLONG) (dvalue + 0.1); |
|
|
| } else if (*next == ',' || *next == '\0') { |
| maxval = maxrows; |
| } else { |
| *status = RANGE_PARSE_ERROR; |
| ffpmsg("Syntax error in this row range list:"); |
| ffpmsg(rowlist); |
| return(*status); |
| } |
| } else if (*next == ',' || *next == '\0') { |
| maxval = minval; |
| } 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) { |
| if (maxval > maxrows) |
| maxval = maxrows; |
|
|
| minrow[*numranges] = minval; |
| maxrow[*numranges] = maxval; |
|
|
| (*numranges)++; |
| } |
|
|
| while (*next == ' ')next++; |
| if (*next == ',') { |
| next++; |
| while (*next == ' ')next++; |
| } |
| } |
|
|
| if (*numranges == 0) { |
| minrow[0] = 1; |
| maxrow[0] = maxrows; |
| *numranges = 1; |
| } |
|
|
| return(*status); |
| } |
| |
| int fficol(fitsfile *fptr, |
| int numcol, |
| char *ttype, |
| char *tform, |
| int *status) |
| |
| |
| |
| |
| |
| { |
| char *name, *format; |
|
|
| name = ttype; |
| format = tform; |
|
|
| fficls(fptr, numcol, 1, &name, &format, status); |
| return(*status); |
| } |
| |
| int fficls(fitsfile *fptr, |
| int fstcol, |
| int ncols, |
| char **ttype, |
| char **tform, |
| int *status) |
| |
| |
| |
| |
| |
| { |
| int colnum, datacode, decims, tfields, tstatus, ii; |
| LONGLONG datasize, firstbyte, nbytes, nadd, naxis1, naxis2, freespace; |
| LONGLONG tbcol, firstcol, delbyte; |
| long nblock, width, repeat; |
| char tfm[FLEN_VALUE], keyname[FLEN_KEYWORD], comm[FLEN_COMMENT], *cptr; |
| tcolumn *colptr; |
|
|
| if (*status > 0) |
| return(*status); |
|
|
| 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) |
| return(*status); |
|
|
| if ((fptr->Fptr)->hdutype == IMAGE_HDU) |
| { |
| ffpmsg("Can only add columns to TABLE or BINTABLE extension (fficls)"); |
| return(*status = NOT_TABLE); |
| } |
|
|
| |
| tfields = (fptr->Fptr)->tfield; |
| if (fstcol < 1 ) |
| return(*status = BAD_COL_NUM); |
| else if (fstcol > tfields) |
| colnum = tfields + 1; |
| else |
| colnum = fstcol; |
|
|
| |
| delbyte = 0; |
| for (ii = 0; ii < ncols; ii++) |
| { |
| if (strlen(tform[ii]) > FLEN_VALUE-1) |
| { |
| ffpmsg("Column format string too long (fficls)"); |
| return (*status=BAD_TFORM); |
| } |
| strcpy(tfm, tform[ii]); |
| ffupch(tfm); |
|
|
| if ((fptr->Fptr)->hdutype == ASCII_TBL) |
| { |
| ffasfm(tfm, &datacode, &width, &decims, status); |
| delbyte += width + 1; |
| } |
| else |
| { |
| ffbnfm(tfm, &datacode, &repeat, &width, status); |
|
|
| if (datacode < 0) { |
| if (strchr(tfm, 'Q')) |
| delbyte += 16; |
| else |
| delbyte += 8; |
| } else if (datacode == 1) |
| delbyte += (repeat + 7) / 8; |
| else if (datacode == 16) |
| delbyte += repeat; |
| else |
| delbyte += (datacode / 10) * repeat; |
| } |
| } |
|
|
| if (*status > 0) |
| return(*status); |
|
|
| |
| |
| naxis1 = (fptr->Fptr)->rowlength; |
| naxis2 = (fptr->Fptr)->numrows; |
|
|
| |
| datasize = (fptr->Fptr)->heapstart + (fptr->Fptr)->heapsize; |
| freespace = ( ( (datasize + 2879) / 2880) * 2880) - datasize; |
| nadd = delbyte * naxis2; |
|
|
| if ( (freespace - nadd) < 0) |
| { |
| nblock = (long) ((nadd - freespace + 2879) / 2880); |
| if (ffiblk(fptr, nblock, 1, status) > 0) |
| return(*status); |
| } |
|
|
| |
| if ((fptr->Fptr)->heapsize > 0) |
| { |
| nbytes = (fptr->Fptr)->heapsize; |
|
|
| |
| firstbyte = (fptr->Fptr)->datastart + (fptr->Fptr)->heapstart; |
|
|
| if (ffshft(fptr, firstbyte, nbytes, nadd, status) > 0) |
| return(*status); |
| } |
|
|
| |
| (fptr->Fptr)->heapstart += nadd; |
|
|
| |
| tstatus = 0; |
| ffmkyj(fptr, "THEAP", (fptr->Fptr)->heapstart, "&", &tstatus); |
|
|
| |
| if (colnum > tfields) |
| firstcol = naxis1; |
| else |
| { |
| colptr = (fptr->Fptr)->tableptr; |
| colptr += (colnum - 1); |
| firstcol = colptr->tbcol; |
| } |
|
|
| |
| ffcins(fptr, naxis1, naxis2, delbyte, firstcol, status); |
|
|
| if ((fptr->Fptr)->hdutype == ASCII_TBL) |
| { |
| |
| for(ii = 0; ii < tfields; ii++) |
| { |
| ffkeyn("TBCOL", ii + 1, keyname, status); |
| ffgkyjj(fptr, keyname, &tbcol, comm, status); |
| if (tbcol > firstcol) |
| { |
| tbcol += delbyte; |
| ffmkyj(fptr, keyname, tbcol, "&", status); |
| } |
| } |
| } |
|
|
| |
| ffmkyj(fptr, "TFIELDS", tfields + ncols, "&", status); |
| ffmkyj(fptr, "NAXIS1", naxis1 + delbyte, "&", status); |
|
|
| |
| if(colnum <= tfields) |
| ffkshf(fptr, colnum, tfields, ncols, status); |
|
|
| |
| for (ii = 0; ii < ncols; ii++, colnum++) |
| { |
| strcpy(comm, "label for field"); |
| ffkeyn("TTYPE", colnum, keyname, status); |
| ffpkys(fptr, keyname, ttype[ii], comm, status); |
|
|
| strcpy(comm, "format of field"); |
| strcpy(tfm, tform[ii]); |
| ffupch(tfm); |
| ffkeyn("TFORM", colnum, keyname, status); |
|
|
| if (abs(datacode) == TSBYTE) |
| { |
| |
| cptr = tfm; |
| while (*cptr != 'S') |
| cptr++; |
|
|
| *cptr = 'B'; |
| ffpkys(fptr, keyname, tfm, comm, status); |
|
|
| |
| ffkeyn("TZERO", colnum, keyname, status); |
| strcpy(comm, "offset for signed bytes"); |
|
|
| ffpkyg(fptr, keyname, -128., 0, comm, status); |
|
|
| ffkeyn("TSCAL", colnum, keyname, status); |
| strcpy(comm, "data are not scaled"); |
| ffpkyg(fptr, keyname, 1., 0, comm, status); |
| } |
| else if (abs(datacode) == TUSHORT) |
| { |
| |
| cptr = tfm; |
| while (*cptr != 'U') |
| cptr++; |
|
|
| *cptr = 'I'; |
| ffpkys(fptr, keyname, tfm, comm, status); |
|
|
| |
| ffkeyn("TZERO", colnum, keyname, status); |
| strcpy(comm, "offset for unsigned integers"); |
|
|
| ffpkyg(fptr, keyname, 32768., 0, comm, status); |
|
|
| ffkeyn("TSCAL", colnum, keyname, status); |
| strcpy(comm, "data are not scaled"); |
| ffpkyg(fptr, keyname, 1., 0, comm, status); |
| } |
| else if (abs(datacode) == TULONG) |
| { |
| |
| cptr = tfm; |
| while (*cptr != 'V') |
| cptr++; |
|
|
| *cptr = 'J'; |
| ffpkys(fptr, keyname, tfm, comm, status); |
|
|
| |
| ffkeyn("TZERO", colnum, keyname, status); |
| strcpy(comm, "offset for unsigned integers"); |
|
|
| ffpkyg(fptr, keyname, 2147483648., 0, comm, status); |
|
|
| ffkeyn("TSCAL", colnum, keyname, status); |
| strcpy(comm, "data are not scaled"); |
| ffpkyg(fptr, keyname, 1., 0, comm, status); |
| } |
| else |
| { |
| ffpkys(fptr, keyname, tfm, comm, status); |
| } |
|
|
| if ((fptr->Fptr)->hdutype == ASCII_TBL) |
| { |
| if (colnum == tfields + 1) |
| tbcol = firstcol + 2; |
| else |
| tbcol = firstcol + 1; |
|
|
| strcpy(comm, "beginning column of field"); |
| ffkeyn("TBCOL", colnum, keyname, status); |
| ffpkyj(fptr, keyname, tbcol, comm, status); |
|
|
| |
| ffasfm(tfm, &datacode, &width, &decims, status); |
| firstcol += width + 1; |
| } |
| } |
| ffrdef(fptr, status); |
| return(*status); |
| } |
| |
| int ffmvec(fitsfile *fptr, |
| int colnum, |
| LONGLONG newveclen, |
| int *status) |
| |
| |
| |
| |
| { |
| int datacode, tfields, tstatus; |
| LONGLONG datasize, size, firstbyte, nbytes, nadd, ndelete; |
| LONGLONG naxis1, naxis2, firstcol, freespace; |
| LONGLONG width, delbyte, repeat; |
| long nblock; |
| char tfm[FLEN_VALUE], keyname[FLEN_KEYWORD], tcode[2]; |
| tcolumn *colptr; |
|
|
| if (*status > 0) |
| return(*status); |
|
|
| 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) |
| return(*status); |
|
|
| if ((fptr->Fptr)->hdutype != BINARY_TBL) |
| { |
| ffpmsg( |
| "Can only change vector length of a column in BINTABLE extension (ffmvec)"); |
| return(*status = NOT_TABLE); |
| } |
|
|
| |
| tfields = (fptr->Fptr)->tfield; |
| if (colnum < 1 || colnum > tfields) |
| return(*status = BAD_COL_NUM); |
|
|
| |
|
|
| colptr = (fptr->Fptr)->tableptr; |
| colptr += (colnum - 1); |
|
|
| datacode = colptr->tdatatype; |
| repeat = colptr->trepeat; |
| width = colptr->twidth; |
|
|
| if (datacode < 0) |
| { |
| ffpmsg( |
| "Can't modify vector length of variable length column (ffmvec)"); |
| return(*status = BAD_TFORM); |
| } |
|
|
| if (repeat == newveclen) |
| return(*status); |
|
|
| if (datacode == TSTRING) |
| width = 1; |
|
|
| naxis1 = (fptr->Fptr)->rowlength; |
| naxis2 = (fptr->Fptr)->numrows; |
|
|
| delbyte = (newveclen - repeat) * width; |
| if (datacode == TBIT) |
| delbyte = ((newveclen + 7) / 8) - ((repeat + 7) / 8); |
|
|
| if (delbyte > 0) |
| { |
| |
| datasize = (fptr->Fptr)->heapstart + (fptr->Fptr)->heapsize; |
| freespace = ( ( (datasize + 2879) / 2880) * 2880) - datasize; |
|
|
| nadd = (LONGLONG)delbyte * naxis2; |
|
|
| if ( (freespace - nadd) < 0) |
| { |
| nblock = (long) ((nadd - freespace + 2879) / 2880); |
| if (ffiblk(fptr, nblock, 1, status) > 0) |
| return(*status); |
| } |
|
|
| |
| if ((fptr->Fptr)->heapsize > 0) |
| { |
| nbytes = (fptr->Fptr)->heapsize; |
|
|
| |
| firstbyte = (fptr->Fptr)->datastart + (fptr->Fptr)->heapstart; |
|
|
| if (ffshft(fptr, firstbyte, nbytes, nadd, status) > 0) |
| return(*status); |
| } |
|
|
| |
| (fptr->Fptr)->heapstart += nadd; |
|
|
| |
| tstatus = 0; |
| ffmkyj(fptr, "THEAP", (fptr->Fptr)->heapstart, "&", &tstatus); |
|
|
| |
| |
| colptr = (fptr->Fptr)->tableptr; |
| colptr += (colnum - 1); |
|
|
| firstcol = colptr->tbcol + (repeat * width); |
|
|
| |
| ffcins(fptr, naxis1, naxis2, delbyte, firstcol, status); |
| } |
| else if (delbyte < 0) |
| { |
| |
| size = (fptr->Fptr)->heapstart + (fptr->Fptr)->heapsize; |
| freespace = ((size + 2879) / 2880) * 2880 - size - ((LONGLONG)delbyte * naxis2); |
| nblock = (long) (freespace / 2880); |
| firstcol = colptr->tbcol + (newveclen * width); |
|
|
| |
| ffcdel(fptr, naxis1, naxis2, -delbyte, firstcol, status); |
| |
| |
| firstbyte = (fptr->Fptr)->datastart + (fptr->Fptr)->heapstart; |
| ndelete = (LONGLONG)delbyte * naxis2; |
|
|
| |
| if ((fptr->Fptr)->heapsize > 0) |
| { |
| nbytes = (fptr->Fptr)->heapsize; |
| if (ffshft(fptr, firstbyte, nbytes, ndelete, status) > 0) |
| return(*status); |
| } |
|
|
| |
| if (nblock > 0) |
| ffdblk(fptr, nblock, status); |
|
|
| |
| (fptr->Fptr)->heapstart += ndelete; |
|
|
| |
| tstatus = 0; |
| ffmkyj(fptr, "THEAP", (fptr->Fptr)->heapstart, "&", &tstatus); |
| } |
|
|
| |
| if (datacode == TBIT) |
| strcpy(tcode,"X"); |
| else if (datacode == TBYTE) |
| strcpy(tcode,"B"); |
| else if (datacode == TLOGICAL) |
| strcpy(tcode,"L"); |
| else if (datacode == TSTRING) |
| strcpy(tcode,"A"); |
| else if (datacode == TSHORT) |
| strcpy(tcode,"I"); |
| else if (datacode == TLONG) |
| strcpy(tcode,"J"); |
| else if (datacode == TLONGLONG) |
| strcpy(tcode,"K"); |
| else if (datacode == TFLOAT) |
| strcpy(tcode,"E"); |
| else if (datacode == TDOUBLE) |
| strcpy(tcode,"D"); |
| else if (datacode == TCOMPLEX) |
| strcpy(tcode,"C"); |
| else if (datacode == TDBLCOMPLEX) |
| strcpy(tcode,"M"); |
|
|
| |
| |
|
|
| snprintf(tfm,FLEN_VALUE,"%.0f%s",(double) newveclen, tcode); |
|
|
| ffkeyn("TFORM", colnum, keyname, status); |
| ffmkys(fptr, keyname, tfm, "&", status); |
|
|
| ffmkyj(fptr, "NAXIS1", naxis1 + delbyte, "&", status); |
|
|
| ffrdef(fptr, status); |
| return(*status); |
| } |
| |
| int ffcpcl(fitsfile *infptr, |
| fitsfile *outfptr, |
| int incol, |
| int outcol, |
| int create_col, |
| int *status) |
| |
| |
| |
| { |
| int tstatus, colnum, typecode, otypecode, anynull; |
| int inHduType, outHduType; |
| long tfields, repeat, orepeat, width, owidth, nrows, outrows; |
| long inloop, outloop, maxloop, ndone, ntodo, npixels; |
| long firstrow, firstelem, ii; |
| char keyname[FLEN_KEYWORD], ttype[FLEN_VALUE], tform[FLEN_VALUE]; |
| char ttype_comm[FLEN_COMMENT],tform_comm[FLEN_COMMENT]; |
| char *lvalues = 0, nullflag, **strarray = 0; |
| char nulstr[] = {'\5', '\0'}; |
| double dnull = 0.l, *dvalues = 0; |
| float fnull = 0., *fvalues = 0; |
|
|
| if (*status > 0) |
| return(*status); |
|
|
| if (infptr->HDUposition != (infptr->Fptr)->curhdu) |
| { |
| ffmahd(infptr, (infptr->HDUposition) + 1, NULL, status); |
| } |
| else if ((infptr->Fptr)->datastart == DATA_UNDEFINED) |
| ffrdef(infptr, status); |
| inHduType = (infptr->Fptr)->hdutype; |
| |
| if (outfptr->HDUposition != (outfptr->Fptr)->curhdu) |
| { |
| ffmahd(outfptr, (outfptr->HDUposition) + 1, NULL, status); |
| } |
| else if ((outfptr->Fptr)->datastart == DATA_UNDEFINED) |
| ffrdef(outfptr, status); |
| outHduType = (outfptr->Fptr)->hdutype; |
| |
| if (*status > 0) |
| return(*status); |
|
|
| if (inHduType == IMAGE_HDU || outHduType == IMAGE_HDU) |
| { |
| ffpmsg |
| ("Can not copy columns to or from IMAGE HDUs (ffcpcl)"); |
| return(*status = NOT_TABLE); |
| } |
|
|
| if ( inHduType == BINARY_TBL && outHduType == ASCII_TBL) |
| { |
| ffpmsg |
| ("Copying from Binary table to ASCII table is not supported (ffcpcl)"); |
| return(*status = NOT_BTABLE); |
| } |
|
|
| |
| ffgtcl(infptr, incol, &typecode, &repeat, &width, status); |
|
|
| if (typecode < 0) |
| { |
| ffpmsg("Variable-length columns are not supported (ffcpcl)"); |
| return(*status = BAD_TFORM); |
| } |
|
|
| if (create_col) |
| { |
| tstatus = 0; |
| ffkeyn("TTYPE", incol, keyname, &tstatus); |
| ffgkys(infptr, keyname, ttype, ttype_comm, &tstatus); |
| ffkeyn("TFORM", incol, keyname, &tstatus); |
| |
| if (ffgkys(infptr, keyname, tform, tform_comm, &tstatus) ) |
| { |
| ffpmsg |
| ("Could not find TTYPE and TFORM keywords in input table (ffcpcl)"); |
| return(*status = NO_TFORM); |
| } |
|
|
| if (inHduType == ASCII_TBL && outHduType == BINARY_TBL) |
| { |
| |
| if (typecode == TSTRING) |
| ffnkey(width, "A", tform, status); |
|
|
| else if (typecode == TLONG) |
| strcpy(tform, "1J"); |
|
|
| else if (typecode == TSHORT) |
| strcpy(tform, "1I"); |
|
|
| else if (typecode == TFLOAT) |
| strcpy(tform,"1E"); |
|
|
| else if (typecode == TDOUBLE) |
| strcpy(tform,"1D"); |
| } |
|
|
| if (ffgkyj(outfptr, "TFIELDS", &tfields, 0, &tstatus)) |
| { |
| ffpmsg |
| ("Could not read TFIELDS keyword in output table (ffcpcl)"); |
| return(*status = NO_TFIELDS); |
| } |
|
|
| colnum = minvalue((int) tfields + 1, outcol); |
|
|
| |
| if (fficol(outfptr, colnum, ttype, tform, status) > 0) |
| { |
| ffpmsg |
| ("Could not append new column to output file (ffcpcl)"); |
| return(*status); |
| } |
|
|
| if ((infptr->Fptr == outfptr->Fptr) |
| && (infptr->HDUposition == outfptr->HDUposition) |
| && (colnum <= incol)) { |
| incol++; |
| } |
|
|
| |
| tstatus = 0; |
| ffkeyn("TTYPE", colnum, keyname, &tstatus); |
| ffmcom(outfptr, keyname, ttype_comm, &tstatus); |
| ffkeyn("TFORM", colnum, keyname, &tstatus); |
| ffmcom(outfptr, keyname, tform_comm, &tstatus); |
|
|
| |
|
|
| ffcpky(infptr, outfptr, incol, colnum, "TUNIT", status); |
| ffcpky(infptr, outfptr, incol, colnum, "TSCAL", status); |
| ffcpky(infptr, outfptr, incol, colnum, "TZERO", status); |
| ffcpky(infptr, outfptr, incol, colnum, "TDISP", status); |
| ffcpky(infptr, outfptr, incol, colnum, "TLMIN", status); |
| ffcpky(infptr, outfptr, incol, colnum, "TLMAX", status); |
| ffcpky(infptr, outfptr, incol, colnum, "TDIM", status); |
|
|
| |
| ffcpky(infptr, outfptr, incol, colnum, "TCTYP", status); |
| ffcpky(infptr, outfptr, incol, colnum, "TCUNI", status); |
| ffcpky(infptr, outfptr, incol, colnum, "TCRVL", status); |
| ffcpky(infptr, outfptr, incol, colnum, "TCRPX", status); |
| ffcpky(infptr, outfptr, incol, colnum, "TCDLT", status); |
| ffcpky(infptr, outfptr, incol, colnum, "TCROT", status); |
|
|
| if (inHduType == ASCII_TBL && outHduType == BINARY_TBL) |
| { |
| |
| if (typecode == TLONG || typecode == TSHORT) |
| { |
| |
| ffkeyn("TNULL", incol, keyname, &tstatus); |
| if (ffgkys(infptr, keyname, ttype, 0, &tstatus) <= 0) |
| { |
| ffkeyn("TNULL", colnum, keyname, &tstatus); |
| if (typecode == TLONG) |
| ffpkyj(outfptr, keyname, -9999999L, "Null value", status); |
| else |
| ffpkyj(outfptr, keyname, -32768L, "Null value", status); |
| } |
| } |
| } |
| else |
| { |
| ffcpky(infptr, outfptr, incol, colnum, "TNULL", status); |
| } |
|
|
| |
| if (ffrdef(outfptr, status) ) |
| return(*status); |
| } |
| else |
| { |
| colnum = outcol; |
| |
| ffgtcl(outfptr, outcol, &otypecode, &orepeat, &owidth, status); |
|
|
| if (orepeat != repeat) { |
| ffpmsg("Input and output vector columns must have same length (ffcpcl)"); |
| return(*status = BAD_TFORM); |
| } |
| } |
|
|
| ffgkyj(infptr, "NAXIS2", &nrows, 0, status); |
| ffgkyj(outfptr, "NAXIS2", &outrows, 0, status); |
| nrows = minvalue(nrows, outrows); |
|
|
| if (typecode == TBIT) |
| repeat = (repeat + 7) / 8; |
| else if (typecode == TSTRING && inHduType == BINARY_TBL) |
| repeat = repeat / width; |
|
|
| |
| ffgrsz(infptr, &inloop, status); |
| ffgrsz(outfptr, &outloop, status); |
|
|
| |
| maxloop = minvalue(inloop, outloop); |
| maxloop = maxvalue(1, maxloop / 2); |
| maxloop = minvalue(maxloop, nrows); |
| maxloop *= repeat; |
|
|
| |
| if (typecode == TLOGICAL) |
| { |
| lvalues = (char *) calloc(maxloop, sizeof(char) ); |
| if (!lvalues) |
| { |
| ffpmsg |
| ("malloc failed to get memory for logicals (ffcpcl)"); |
| return(*status = ARRAY_TOO_BIG); |
| } |
| } |
| else if (typecode == TSTRING) |
| { |
| |
| strarray = (char **) calloc(maxloop, sizeof(strarray)); |
|
|
| |
| for (ii = 0; ii < maxloop; ii++) |
| strarray[ii] = (char *) calloc(width+1, sizeof(char)); |
| } |
| else if (typecode == TCOMPLEX) |
| { |
| fvalues = (float *) calloc(maxloop * 2, sizeof(float) ); |
| if (!fvalues) |
| { |
| ffpmsg |
| ("malloc failed to get memory for complex (ffcpcl)"); |
| return(*status = ARRAY_TOO_BIG); |
| } |
| fnull = 0.; |
| } |
| else if (typecode == TDBLCOMPLEX) |
| { |
| dvalues = (double *) calloc(maxloop * 2, sizeof(double) ); |
| if (!dvalues) |
| { |
| ffpmsg |
| ("malloc failed to get memory for dbl complex (ffcpcl)"); |
| return(*status = ARRAY_TOO_BIG); |
| } |
| dnull = 0.; |
| } |
| else |
| { |
| dvalues = (double *) calloc(maxloop, sizeof(double) ); |
| if (!dvalues) |
| { |
| ffpmsg |
| ("malloc failed to get memory for doubles (ffcpcl)"); |
| return(*status = ARRAY_TOO_BIG); |
| } |
| dnull = -9.99991999E31; |
| } |
|
|
| npixels = nrows * repeat; |
| ntodo = minvalue(npixels, maxloop); |
| ndone = 0; |
|
|
| while (ntodo) |
| { |
| firstrow = ndone / repeat + 1; |
| firstelem = ndone - ((firstrow - 1) * repeat) + 1; |
|
|
| |
| if (typecode == TLOGICAL) |
| ffgcl(infptr, incol, firstrow, firstelem, ntodo, |
| lvalues, status); |
| else if (typecode == TSTRING) |
| ffgcvs(infptr, incol, firstrow, firstelem, ntodo, |
| nulstr, strarray, &anynull, status); |
|
|
| else if (typecode == TCOMPLEX) |
| ffgcvc(infptr, incol, firstrow, firstelem, ntodo, fnull, |
| fvalues, &anynull, status); |
|
|
| else if (typecode == TDBLCOMPLEX) |
| ffgcvm(infptr, incol, firstrow, firstelem, ntodo, dnull, |
| dvalues, &anynull, status); |
|
|
| else |
| ffgcvd(infptr, incol, firstrow, firstelem, ntodo, dnull, |
| dvalues, &anynull, status); |
|
|
| if (*status > 0) |
| { |
| ffpmsg("Error reading input copy of column (ffcpcl)"); |
| break; |
| } |
|
|
| |
| if (typecode == TLOGICAL) |
| { |
| nullflag = 2; |
|
|
| ffpcnl(outfptr, colnum, firstrow, firstelem, ntodo, |
| lvalues, nullflag, status); |
|
|
| } |
|
|
| else if (typecode == TSTRING) |
| { |
| if (anynull) |
| ffpcns(outfptr, colnum, firstrow, firstelem, ntodo, |
| strarray, nulstr, status); |
| else |
| ffpcls(outfptr, colnum, firstrow, firstelem, ntodo, |
| strarray, status); |
| } |
|
|
| else if (typecode == TCOMPLEX) |
| { |
| ffpclc(outfptr, colnum, firstrow, firstelem, ntodo, |
| fvalues, status); |
| } |
|
|
| else if (typecode == TDBLCOMPLEX) |
| { |
| ffpclm(outfptr, colnum, firstrow, firstelem, ntodo, |
| dvalues, status); |
| } |
|
|
| else |
| { |
| if (anynull) |
| ffpcnd(outfptr, colnum, firstrow, firstelem, ntodo, |
| dvalues, dnull, status); |
| else |
| ffpcld(outfptr, colnum, firstrow, firstelem, ntodo, |
| dvalues, status); |
| } |
|
|
| if (*status > 0) |
| { |
| ffpmsg("Error writing output copy of column (ffcpcl)"); |
| break; |
| } |
|
|
| npixels -= ntodo; |
| ndone += ntodo; |
| ntodo = minvalue(npixels, maxloop); |
| } |
|
|
| |
| if (typecode == TLOGICAL) |
| { |
| free(lvalues); |
| } |
| else if (typecode == TSTRING) |
| { |
| for (ii = 0; ii < maxloop; ii++) |
| free(strarray[ii]); |
|
|
| free(strarray); |
| } |
| else |
| { |
| free(dvalues); |
| } |
|
|
| return(*status); |
| } |
| |
| int ffccls(fitsfile *infptr, |
| fitsfile *outfptr, |
| int incol, |
| int outcol, |
| int ncols, |
| int create_col, |
| int *status) |
| |
| |
| |
| |
| |
| |
| { |
| int tstatus, colnum, typecode, otypecode, anynull; |
| int inHduType, outHduType; |
| long tfields, repeat, orepeat, width, owidth, nrows, outrows; |
| long inloop, outloop, maxloop, ndone, ntodo, npixels; |
| long firstrow, firstelem, ii; |
| char keyname[FLEN_KEYWORD], ttype[FLEN_VALUE], tform[FLEN_VALUE]; |
| char ttype_comm[FLEN_COMMENT],tform_comm[FLEN_COMMENT]; |
| char *lvalues = 0, nullflag, **strarray = 0; |
| char nulstr[] = {'\5', '\0'}; |
| double dnull = 0.l, *dvalues = 0; |
| float fnull = 0., *fvalues = 0; |
| int typecodes[1000]; |
| char *ttypes[1000], *tforms[1000], keyarr[1001][FLEN_CARD]; |
| int ikey = 0; |
| int icol, incol1, outcol1; |
|
|
| if (*status > 0) |
| return(*status); |
|
|
| if (infptr->HDUposition != (infptr->Fptr)->curhdu) |
| { |
| ffmahd(infptr, (infptr->HDUposition) + 1, NULL, status); |
| } |
| else if ((infptr->Fptr)->datastart == DATA_UNDEFINED) |
| ffrdef(infptr, status); |
| inHduType = (infptr->Fptr)->hdutype; |
| |
| if (outfptr->HDUposition != (outfptr->Fptr)->curhdu) |
| { |
| ffmahd(outfptr, (outfptr->HDUposition) + 1, NULL, status); |
| } |
| else if ((outfptr->Fptr)->datastart == DATA_UNDEFINED) |
| ffrdef(outfptr, status); |
| outHduType = (outfptr->Fptr)->hdutype; |
| |
| if (*status > 0) |
| return(*status); |
|
|
| if (inHduType == IMAGE_HDU || outHduType == IMAGE_HDU) |
| { |
| ffpmsg |
| ("Can not copy columns to or from IMAGE HDUs (ffccls)"); |
| return(*status = NOT_TABLE); |
| } |
|
|
| if ( (inHduType == BINARY_TBL && outHduType == ASCII_TBL) || |
| (inHduType == ASCII_TBL && outHduType == BINARY_TBL) ) |
| { |
| ffpmsg |
| ("Copying between Binary and ASCII tables is not supported (ffccls)"); |
| return(*status = NOT_BTABLE); |
| } |
|
|
| |
| |
| if ((infptr->Fptr == outfptr->Fptr) |
| && (infptr->HDUposition == outfptr->HDUposition)) |
| { |
| ffpmsg |
| ("Copying multiple columns in same HDU is not supported (ffccls)"); |
| return(*status = NOT_BTABLE); |
| } |
|
|
| |
| tstatus=0; |
| if (ffgkyj(outfptr, "TFIELDS", &tfields, 0, &tstatus)) |
| { |
| ffpmsg |
| ("Could not read TFIELDS keyword in output table (ffccls)"); |
| return(*status = NO_TFIELDS); |
| } |
|
|
| colnum = minvalue((int) tfields + 1, outcol); |
|
|
| |
| for (incol1 = incol, outcol1 = colnum, icol = 0; |
| icol < ncols; |
| icol++, incol1++, outcol1++) |
| { |
| ffgtcl(infptr, incol1, &typecode, &repeat, &width, status); |
|
|
| if (typecode < 0) |
| { |
| ffpmsg("Variable-length columns are not supported (ffccls)"); |
| return(*status = BAD_TFORM); |
| } |
|
|
| typecodes[icol] = typecode; |
|
|
| tstatus = 0; |
| ffkeyn("TTYPE", incol1, keyname, &tstatus); |
| ffgkys(infptr, keyname, ttype, ttype_comm, &tstatus); |
|
|
| ffkeyn("TFORM", incol1, keyname, &tstatus); |
| |
| if (ffgkys(infptr, keyname, tform, tform_comm, &tstatus) ) |
| { |
| ffpmsg |
| ("Could not find TTYPE and TFORM keywords in input table (ffccls)"); |
| return(*status = NO_TFORM); |
| } |
|
|
| |
| if ( create_col ) { |
| tforms[icol] = keyarr[ikey++]; |
| ttypes[icol] = keyarr[ikey++]; |
|
|
| strcpy(tforms[icol], tform); |
| strcpy(ttypes[icol], ttype); |
| } else { |
| |
| |
| ffgtcl(outfptr, outcol1, &otypecode, &orepeat, &owidth, status); |
|
|
| if (orepeat != repeat) { |
| ffpmsg("Input and output vector columns must have same length (ffccls)"); |
| return(*status = BAD_TFORM); |
| } |
| } |
| } |
|
|
| |
| |
| if (create_col) |
| { |
| |
| if (fficls(outfptr, colnum, ncols, ttypes, tforms, status) > 0) |
| { |
| ffpmsg |
| ("Could not append new columns to output file (ffccls)"); |
| return(*status); |
| } |
|
|
| |
| for (incol1 = incol, outcol1 = colnum, icol = 0; |
| icol < ncols; |
| icol++, incol1++, outcol1++) |
| { |
| |
| ffkeyn("TTYPE", incol1, keyname, status); |
| ffgkys(infptr, keyname, ttype, ttype_comm, status); |
| ffkeyn("TTYPE", outcol1, keyname, status); |
| ffmcom(outfptr, keyname, ttype_comm, status); |
| |
| ffkeyn("TFORM", incol1, keyname, status); |
| ffgkys(infptr, keyname, tform, tform_comm, status); |
| ffkeyn("TFORM", outcol1, keyname, status); |
| ffmcom(outfptr, keyname, tform_comm, status); |
| |
| |
| |
| ffcpky(infptr, outfptr, incol1, outcol1, "TUNIT", status); |
| ffcpky(infptr, outfptr, incol1, outcol1, "TSCAL", status); |
| ffcpky(infptr, outfptr, incol1, outcol1, "TZERO", status); |
| ffcpky(infptr, outfptr, incol1, outcol1, "TDISP", status); |
| ffcpky(infptr, outfptr, incol1, outcol1, "TLMIN", status); |
| ffcpky(infptr, outfptr, incol1, outcol1, "TLMAX", status); |
| ffcpky(infptr, outfptr, incol1, outcol1, "TDIM", status); |
|
|
| |
| ffcpky(infptr, outfptr, incol1, outcol1, "TCTYP", status); |
| ffcpky(infptr, outfptr, incol1, outcol1, "TCUNI", status); |
| ffcpky(infptr, outfptr, incol1, outcol1, "TCRVL", status); |
| ffcpky(infptr, outfptr, incol1, outcol1, "TCRPX", status); |
| ffcpky(infptr, outfptr, incol1, outcol1, "TCDLT", status); |
| ffcpky(infptr, outfptr, incol1, outcol1, "TCROT", status); |
| |
| ffcpky(infptr, outfptr, incol1, outcol1, "TNULL", status); |
|
|
| } |
|
|
| |
| if (ffrdef(outfptr, status) ) |
| return(*status); |
| } |
| |
| |
| |
| for (incol1 = incol, outcol1 = colnum, icol = 0; |
| icol < ncols; |
| icol++, incol1++, outcol1++) |
| { |
| ffcpcl(infptr, outfptr, incol1, outcol1, 0, status); |
| } |
|
|
| return(*status); |
| } |
| |
| int ffcprw(fitsfile *infptr, |
| fitsfile *outfptr, |
| LONGLONG firstrow, |
| LONGLONG nrows, |
| int *status) |
| |
| |
| |
| { |
| LONGLONG innaxis1, innaxis2, outnaxis1, outnaxis2, ii, jj; |
| unsigned char *buffer; |
|
|
| if (*status > 0) |
| return(*status); |
|
|
| if (infptr->HDUposition != (infptr->Fptr)->curhdu) |
| { |
| ffmahd(infptr, (infptr->HDUposition) + 1, NULL, status); |
| } |
| else if ((infptr->Fptr)->datastart == DATA_UNDEFINED) |
| ffrdef(infptr, status); |
|
|
| if (outfptr->HDUposition != (outfptr->Fptr)->curhdu) |
| { |
| ffmahd(outfptr, (outfptr->HDUposition) + 1, NULL, status); |
| } |
| else if ((outfptr->Fptr)->datastart == DATA_UNDEFINED) |
| ffrdef(outfptr, status); |
|
|
| if (*status > 0) |
| return(*status); |
|
|
| if ((infptr->Fptr)->hdutype == IMAGE_HDU || (outfptr->Fptr)->hdutype == IMAGE_HDU) |
| { |
| ffpmsg |
| ("Can not copy rows to or from IMAGE HDUs (ffcprw)"); |
| return(*status = NOT_TABLE); |
| } |
|
|
| if ( ((infptr->Fptr)->hdutype == BINARY_TBL && (outfptr->Fptr)->hdutype == ASCII_TBL) || |
| ((infptr->Fptr)->hdutype == ASCII_TBL && (outfptr->Fptr)->hdutype == BINARY_TBL) ) |
| { |
| ffpmsg |
| ("Copying rows between Binary and ASCII tables is not supported (ffcprw)"); |
| return(*status = NOT_BTABLE); |
| } |
|
|
| ffgkyjj(infptr, "NAXIS1", &innaxis1, 0, status); |
| ffgkyjj(infptr, "NAXIS2", &innaxis2, 0, status); |
| ffgkyjj(outfptr, "NAXIS1", &outnaxis1, 0, status); |
| ffgkyjj(outfptr, "NAXIS2", &outnaxis2, 0, status); |
|
|
| if (*status > 0) |
| return(*status); |
|
|
| if (outnaxis1 > innaxis1) { |
| ffpmsg |
| ("Input and output tables do not have same width (ffcprw)"); |
| return(*status = BAD_ROW_WIDTH); |
| } |
|
|
| if (firstrow + nrows - 1 > innaxis2) { |
| ffpmsg |
| ("Not enough rows in input table to copy (ffcprw)"); |
| return(*status = BAD_ROW_NUM); |
| } |
|
|
| |
| buffer = malloc( (size_t) innaxis1); |
| if (!buffer) { |
| ffpmsg |
| ("Unable to allocate memory (ffcprw)"); |
| return(*status = MEMORY_ALLOCATION); |
| } |
| |
| |
| jj = outnaxis2 + 1; |
| for (ii = firstrow; ii < firstrow + nrows; ii++) { |
| fits_read_tblbytes (infptr, ii, 1, innaxis1, buffer, status); |
| fits_write_tblbytes(outfptr, jj, 1, innaxis1, buffer, status); |
| jj++; |
| } |
|
|
| outnaxis2 += nrows; |
| fits_update_key(outfptr, TLONGLONG, "NAXIS2", &outnaxis2, 0, status); |
|
|
| free(buffer); |
| return(*status); |
| } |
| |
| int ffcpky(fitsfile *infptr, |
| fitsfile *outfptr, |
| int incol, |
| int outcol, |
| char *rootname, |
| int *status) |
| |
| |
| |
| { |
| int tstatus = 0; |
| char keyname[FLEN_KEYWORD]; |
| char value[FLEN_VALUE], comment[FLEN_COMMENT], card[FLEN_CARD]; |
|
|
| ffkeyn(rootname, incol, keyname, &tstatus); |
| if (ffgkey(infptr, keyname, value, comment, &tstatus) <= 0) |
| { |
| ffkeyn(rootname, outcol, keyname, &tstatus); |
| ffmkky(keyname, value, comment, card, status); |
| ffprec(outfptr, card, status); |
| } |
| return(*status); |
| } |
| |
| int ffdcol(fitsfile *fptr, |
| int colnum, |
| int *status) |
| |
| |
| |
| { |
| int ii, tstatus; |
| LONGLONG firstbyte, size, ndelete, nbytes, naxis1, naxis2, firstcol, delbyte, freespace; |
| LONGLONG tbcol; |
| long nblock, nspace; |
| char keyname[FLEN_KEYWORD], comm[FLEN_COMMENT]; |
| tcolumn *colptr, *nextcol; |
|
|
| if (*status > 0) |
| return(*status); |
|
|
| 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) |
| return(*status); |
|
|
| if ((fptr->Fptr)->hdutype == IMAGE_HDU) |
| { |
| ffpmsg |
| ("Can only delete column from TABLE or BINTABLE extension (ffdcol)"); |
| return(*status = NOT_TABLE); |
| } |
|
|
| if (colnum < 1 || colnum > (fptr->Fptr)->tfield ) |
| return(*status = BAD_COL_NUM); |
|
|
| colptr = (fptr->Fptr)->tableptr; |
| colptr += (colnum - 1); |
| firstcol = colptr->tbcol; |
|
|
| |
| if ((fptr->Fptr)->hdutype == ASCII_TBL) |
| { |
| delbyte = colptr->twidth; |
|
|
| if (colnum < (fptr->Fptr)->tfield) |
| { |
| nextcol = colptr + 1; |
| nspace = (long) ((nextcol->tbcol) - (colptr->tbcol) - delbyte); |
| if (nspace > 0) |
| delbyte++; |
| } |
| else if (colnum > 1) |
| { |
| nextcol = colptr - 1; |
| nspace = (long) ((colptr->tbcol) - (nextcol->tbcol) - (nextcol->twidth)); |
| if (nspace > 0) |
| { |
| delbyte++; |
| firstcol--; |
| } |
| } |
| } |
| else |
| { |
| if (colnum < (fptr->Fptr)->tfield) |
| { |
| nextcol = colptr + 1; |
| delbyte = (nextcol->tbcol) - (colptr->tbcol); |
| } |
| else |
| { |
| delbyte = ((fptr->Fptr)->rowlength) - (colptr->tbcol); |
| } |
| } |
|
|
| naxis1 = (fptr->Fptr)->rowlength; |
| naxis2 = (fptr->Fptr)->numrows; |
|
|
| |
| size = (fptr->Fptr)->heapstart + (fptr->Fptr)->heapsize; |
| freespace = ((LONGLONG)delbyte * naxis2) + ((size + 2879) / 2880) * 2880 - size; |
| nblock = (long) (freespace / 2880); |
|
|
| ffcdel(fptr, naxis1, naxis2, delbyte, firstcol, status); |
|
|
| |
| firstbyte = (fptr->Fptr)->datastart + (fptr->Fptr)->heapstart; |
| ndelete = (LONGLONG)delbyte * naxis2; |
|
|
| |
| if ((fptr->Fptr)->heapsize > 0) |
| { |
| nbytes = (fptr->Fptr)->heapsize; |
|
|
| if (ffshft(fptr, firstbyte, nbytes, -ndelete, status) > 0) |
| return(*status); |
| } |
|
|
| |
| if (nblock > 0) |
| ffdblk(fptr, nblock, status); |
|
|
| |
| (fptr->Fptr)->heapstart -= ndelete; |
|
|
| |
| tstatus = 0; |
| ffmkyj(fptr, "THEAP", (long)(fptr->Fptr)->heapstart, "&", &tstatus); |
|
|
| if ((fptr->Fptr)->hdutype == ASCII_TBL) |
| { |
| |
| for (ii = 1; ii <= (fptr->Fptr)->tfield; ii++) |
| { |
| ffkeyn("TBCOL", ii, keyname, status); |
| ffgkyjj(fptr, keyname, &tbcol, comm, status); |
| if (tbcol > firstcol) |
| { |
| tbcol = tbcol - delbyte; |
| ffmkyj(fptr, keyname, tbcol, "&", status); |
| } |
| } |
| } |
|
|
| |
| ffmkyj(fptr, "TFIELDS", ((fptr->Fptr)->tfield) - 1, "&", status); |
| ffmkyj(fptr, "NAXIS1", naxis1 - delbyte, "&", status); |
| |
| |
| |
| |
| ffkshf(fptr, colnum, (fptr->Fptr)->tfield, -1, status); |
|
|
| ffrdef(fptr, status); |
| return(*status); |
| } |
| |
| int ffcins(fitsfile *fptr, |
| LONGLONG naxis1, |
| LONGLONG naxis2, |
| LONGLONG ninsert, |
| LONGLONG bytepos, |
| int *status) |
| |
| |
| |
| { |
| unsigned char buffer[10000], cfill; |
| LONGLONG newlen, fbyte, nbytes, irow, nseg, ii; |
|
|
| if (*status > 0) |
| return(*status); |
|
|
| if (naxis2 == 0) |
| return(*status); |
|
|
| |
| if ((fptr->Fptr)->hdutype == ASCII_TBL) |
| cfill = 32; |
| else |
| cfill = 0; |
|
|
| newlen = naxis1 + ninsert; |
|
|
| if (newlen <= 10000) |
| { |
| |
| |
| |
|
|
| for (ii = 0; ii < ninsert; ii++) |
| buffer[ii] = cfill; |
|
|
| |
| fbyte = bytepos + 1; |
| nbytes = naxis1 - bytepos; |
| ffgtbb(fptr, naxis2, fbyte, nbytes, &buffer[ninsert], status); |
| (fptr->Fptr)->rowlength = newlen; |
|
|
| |
| nbytes += ninsert; |
| ffptbb(fptr, naxis2, fbyte, nbytes, buffer, status); |
| (fptr->Fptr)->rowlength = naxis1; |
|
|
| |
| for (irow = naxis2 - 1; irow > 0; irow--) |
| { |
| |
| ffgtbb(fptr, irow, fbyte, naxis1, &buffer[ninsert], status); |
| (fptr->Fptr)->rowlength = newlen; |
|
|
| |
| ffptbb(fptr, irow, fbyte, newlen, buffer, status); |
| (fptr->Fptr)->rowlength = naxis1; |
| } |
| } |
| else |
| { |
| |
| |
| |
| |
| |
|
|
| nbytes = naxis1 - bytepos; |
| nseg = (nbytes + 9999) / 10000; |
| fbyte = (nseg - 1) * 10000 + bytepos + 1; |
| nbytes = naxis1 - fbyte + 1; |
|
|
| for (ii = 0; ii < nseg; ii++) |
| { |
| ffgtbb(fptr, naxis2, fbyte, nbytes, buffer, status); |
| (fptr->Fptr)->rowlength = newlen; |
|
|
| ffptbb(fptr, naxis2, fbyte + ninsert, nbytes, buffer, status); |
| (fptr->Fptr)->rowlength = naxis1; |
|
|
| fbyte -= 10000; |
| nbytes = 10000; |
| } |
|
|
| |
| nseg = (naxis1 + 9999) / 10000; |
| for (irow = naxis2 - 1; irow > 0; irow--) |
| { |
| fbyte = (nseg - 1) * 10000 + bytepos + 1; |
| nbytes = naxis1 - (nseg - 1) * 10000; |
| for (ii = 0; ii < nseg; ii++) |
| { |
| |
| ffgtbb(fptr, irow, fbyte, nbytes, buffer, status); |
| (fptr->Fptr)->rowlength = newlen; |
|
|
| |
| ffptbb(fptr, irow, fbyte + ninsert, nbytes, buffer, status); |
| (fptr->Fptr)->rowlength = naxis1; |
|
|
| fbyte -= 10000; |
| nbytes = 10000; |
| } |
| } |
|
|
| |
| nbytes = minvalue(ninsert, 10000); |
| memset(buffer, cfill, (size_t) nbytes); |
|
|
| nseg = (ninsert + 9999) / 10000; |
| (fptr->Fptr)->rowlength = newlen; |
|
|
| for (irow = 1; irow <= naxis2; irow++) |
| { |
| fbyte = bytepos + 1; |
| nbytes = ninsert - ((nseg - 1) * 10000); |
| for (ii = 0; ii < nseg; ii++) |
| { |
| ffptbb(fptr, irow, fbyte, nbytes, buffer, status); |
| fbyte += nbytes; |
| nbytes = 10000; |
| } |
| } |
| (fptr->Fptr)->rowlength = naxis1; |
| } |
| return(*status); |
| } |
| |
| int ffcdel(fitsfile *fptr, |
| LONGLONG naxis1, |
| LONGLONG naxis2, |
| LONGLONG ndelete, |
| LONGLONG bytepos, |
| int *status) |
| |
| |
| { |
| unsigned char buffer[10000]; |
| LONGLONG i1, i2, ii, irow, nseg; |
| LONGLONG newlen, remain, nbytes; |
|
|
| if (*status > 0) |
| return(*status); |
|
|
| if (naxis2 == 0) |
| return(*status); |
|
|
| newlen = naxis1 - ndelete; |
|
|
| if (newlen <= 10000) |
| { |
| |
| |
| |
| i1 = bytepos + 1; |
| i2 = i1 + ndelete; |
| for (irow = 1; irow < naxis2; irow++) |
| { |
| ffgtbb(fptr, irow, i2, newlen, buffer, status); |
| (fptr->Fptr)->rowlength = newlen; |
|
|
| ffptbb(fptr, irow, i1, newlen, buffer, status); |
| (fptr->Fptr)->rowlength = naxis1; |
| } |
|
|
| |
| remain = naxis1 - (bytepos + ndelete); |
|
|
| if (remain > 0) |
| { |
| ffgtbb(fptr, naxis2, i2, remain, buffer, status); |
| (fptr->Fptr)->rowlength = newlen; |
|
|
| ffptbb(fptr, naxis2, i1, remain, buffer, status); |
| (fptr->Fptr)->rowlength = naxis1; |
| } |
| } |
| else |
| { |
| |
| |
| |
|
|
| nseg = (newlen + 9999) / 10000; |
| for (irow = 1; irow < naxis2; irow++) |
| { |
| i1 = bytepos + 1; |
| i2 = i1 + ndelete; |
|
|
| nbytes = newlen - (nseg - 1) * 10000; |
| for (ii = 0; ii < nseg; ii++) |
| { |
| ffgtbb(fptr, irow, i2, nbytes, buffer, status); |
| (fptr->Fptr)->rowlength = newlen; |
|
|
| ffptbb(fptr, irow, i1, nbytes, buffer, status); |
| (fptr->Fptr)->rowlength = naxis1; |
|
|
| i1 += nbytes; |
| i2 += nbytes; |
| nbytes = 10000; |
| } |
| } |
|
|
| |
| remain = naxis1 - (bytepos + ndelete); |
|
|
| if (remain > 0) |
| { |
| nseg = (remain + 9999) / 10000; |
| i1 = bytepos + 1; |
| i2 = i1 + ndelete; |
| nbytes = remain - (nseg - 1) * 10000; |
| for (ii = 0; ii < nseg; ii++) |
| { |
| ffgtbb(fptr, naxis2, i2, nbytes, buffer, status); |
| (fptr->Fptr)->rowlength = newlen; |
|
|
| ffptbb(fptr, naxis2, i1, nbytes, buffer, status); |
| (fptr->Fptr)->rowlength = naxis1; |
|
|
| i1 += nbytes; |
| i2 += nbytes; |
| nbytes = 10000; |
| } |
| } |
| } |
| return(*status); |
| } |
| |
| int ffkshf(fitsfile *fptr, |
| int colmin, |
| int colmax, |
| int incre, |
| int *status) |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| { |
| int nkeys, nmore, nrec, tstatus, i1; |
| long ivalue; |
| char rec[FLEN_CARD], q[FLEN_KEYWORD], newkey[FLEN_KEYWORD]; |
|
|
| ffghsp(fptr, &nkeys, &nmore, status); |
|
|
| |
|
|
| for (nrec = 9; nrec <= nkeys; nrec++) |
| { |
| ffgrec(fptr, nrec, rec, status); |
|
|
| if (rec[0] == 'T') |
| { |
| i1 = 0; |
| strncpy(q, &rec[1], 4); |
| if (!strncmp(q, "BCOL", 4) || !strncmp(q, "FORM", 4) || |
| !strncmp(q, "TYPE", 4) || !strncmp(q, "SCAL", 4) || |
| !strncmp(q, "UNIT", 4) || !strncmp(q, "NULL", 4) || |
| !strncmp(q, "ZERO", 4) || !strncmp(q, "DISP", 4) || |
| !strncmp(q, "LMIN", 4) || !strncmp(q, "LMAX", 4) || |
| !strncmp(q, "DMIN", 4) || !strncmp(q, "DMAX", 4) || |
| !strncmp(q, "CTYP", 4) || !strncmp(q, "CRPX", 4) || |
| !strncmp(q, "CRVL", 4) || !strncmp(q, "CDLT", 4) || |
| !strncmp(q, "CROT", 4) || !strncmp(q, "CUNI", 4) ) |
| i1 = 5; |
| else if (!strncmp(rec, "TDIM", 4) ) |
| i1 = 4; |
|
|
| if (i1) |
| { |
| |
| q[0] = '\0'; |
| strncat(q, &rec[i1], 8 - i1); |
|
|
| tstatus = 0; |
| ffc2ii(q, &ivalue, &tstatus); |
|
|
| if (tstatus == 0 && ivalue >= colmin && ivalue <= colmax) |
| { |
| if (incre <= 0 && ivalue == colmin) |
| { |
| ffdrec(fptr, nrec, status); |
| nkeys = nkeys - 1; |
| nrec = nrec - 1; |
| } |
| else |
| { |
| ivalue = ivalue + incre; |
| q[0] = '\0'; |
| strncat(q, rec, i1); |
| |
| ffkeyn(q, ivalue, newkey, status); |
| strncpy(rec, " ", 8); |
| i1 = strlen(newkey); |
| strncpy(rec, newkey, i1); |
| ffmrec(fptr, nrec, rec, status); |
| } |
| } |
| } |
| } |
| } |
| return(*status); |
| } |
| |
| int ffshft(fitsfile *fptr, |
| LONGLONG firstbyte, |
| LONGLONG nbytes, |
| LONGLONG nshift, |
| int *status) |
| |
| |
| |
| |
| |
| { |
| #define shftbuffsize 100000 |
| long ntomov; |
| LONGLONG ptr, ntodo; |
| char buffer[shftbuffsize]; |
|
|
| if (*status > 0) |
| return(*status); |
|
|
| ntodo = nbytes; |
|
|
| if (nshift > 0) |
| |
| ptr = firstbyte + nbytes; |
| else |
| |
| ptr = firstbyte; |
|
|
| while (ntodo) |
| { |
| |
| ntomov = (long) (minvalue(ntodo, shftbuffsize)); |
|
|
| if (nshift > 0) |
| ptr -= ntomov; |
|
|
| |
|
|
| ffmbyt(fptr, ptr, REPORT_EOF, status); |
| ffgbyt(fptr, ntomov, buffer, status); |
|
|
| |
| ffmbyt(fptr, ptr + nshift, IGNORE_EOF, status); |
| if (ffpbyt(fptr, ntomov, buffer, status) > 0) |
| { |
| ffpmsg("Error while shifting block (ffshft)"); |
| return(*status); |
| } |
|
|
| ntodo -= ntomov; |
| if (nshift < 0) |
| ptr += ntomov; |
| } |
|
|
| |
| if ((fptr->Fptr)->hdutype == ASCII_TBL) |
| memset(buffer, 32, shftbuffsize); |
| else |
| memset(buffer, 0, shftbuffsize); |
|
|
|
|
| if (nshift < 0) |
| { |
| ntodo = -nshift; |
| |
| ptr = firstbyte + nbytes + nshift; |
| } |
| else |
| { |
| ntodo = nshift; |
| |
| ptr = firstbyte; |
| } |
|
|
| ffmbyt(fptr, ptr, REPORT_EOF, status); |
|
|
| while (ntodo) |
| { |
| ntomov = (long) (minvalue(ntodo, shftbuffsize)); |
| ffpbyt(fptr, ntomov, buffer, status); |
| ntodo -= ntomov; |
| } |
| return(*status); |
| } |
|
|