| |
| |
|
|
| |
| |
| |
|
|
| #include <stdlib.h> |
| #include <string.h> |
| #include "fitsio2.h" |
| |
| int ffgcvl( fitsfile *fptr, |
| int colnum, |
| LONGLONG firstrow, |
| LONGLONG firstelem, |
| LONGLONG nelem, |
| char nulval, |
| char *array, |
| int *anynul, |
| int *status) |
| |
| |
| |
| |
| |
| { |
| char cdummy; |
|
|
| ffgcll( fptr, colnum, firstrow, firstelem, nelem, 1, nulval, array, |
| &cdummy, anynul, status); |
|
|
| return(*status); |
| } |
| |
| int ffgcl( fitsfile *fptr, |
| int colnum, |
| LONGLONG firstrow, |
| LONGLONG firstelem, |
| LONGLONG nelem, |
| char *array, |
| int *status) |
| |
| |
| |
| |
| |
| |
| { |
| char nulval = 0; |
| int anynul; |
|
|
| ffgcvl( fptr, colnum, firstrow, firstelem, nelem, nulval, array, |
| &anynul, status); |
|
|
| return(*status); |
| } |
| |
| int ffgcfl( fitsfile *fptr, |
| int colnum, |
| LONGLONG firstrow, |
| LONGLONG firstelem, |
| LONGLONG nelem, |
| char *array, |
| char *nularray, |
| int *anynul, |
| int *status) |
| |
| |
| |
| { |
| char nulval = 0; |
|
|
| ffgcll( fptr, colnum, firstrow, firstelem, nelem, 2, nulval, array, |
| nularray, anynul, status); |
|
|
| return(*status); |
| } |
| |
| int ffgcll( fitsfile *fptr, |
| int colnum, |
| LONGLONG firstrow, |
| LONGLONG firstelem, |
| LONGLONG nelem, |
| int nultyp, |
| |
| |
| char nulval, |
| char *array, |
| char *nularray, |
| int *anynul, |
| int *status) |
| |
| |
| |
| { |
| double dtemp; |
| int tcode, maxelem, hdutype, ii, nulcheck; |
| long twidth, incre; |
| long ntodo; |
| LONGLONG repeat, startpos, elemnum, readptr, tnull, rowlen, rownum, remain, next; |
| double scale, zero; |
| char tform[20]; |
| char message[FLEN_ERRMSG]; |
| char snull[20]; |
| unsigned char buffer[DBUFFSIZE], *buffptr; |
|
|
| if (*status > 0 || nelem == 0) |
| return(*status); |
|
|
| if (anynul) |
| *anynul = 0; |
|
|
| if (nultyp == 2) |
| memset(nularray, 0, (size_t) nelem); |
|
|
| |
| |
| |
| if (ffgcprll( fptr, colnum, firstrow, firstelem, nelem, 0, &scale, &zero, |
| tform, &twidth, &tcode, &maxelem, &startpos, &elemnum, &incre, |
| &repeat, &rowlen, &hdutype, &tnull, snull, status) > 0) |
| return(*status); |
|
|
| if (tcode != TLOGICAL) |
| return(*status = NOT_LOGICAL_COL); |
| |
| |
| |
| |
| nulcheck = nultyp; |
|
|
| if (nultyp == 1 && nulval == 0) |
| nulcheck = 0; |
|
|
| |
| |
| |
|
|
| remain = nelem; |
| next = 0; |
| rownum = 0; |
| ntodo = (long) remain; |
|
|
| while (ntodo) |
| { |
| |
| |
| |
| |
| ntodo = (long) minvalue(ntodo, maxelem); |
| ntodo = (long) minvalue(ntodo, (repeat - elemnum)); |
|
|
| readptr = startpos + (rowlen * rownum) + (elemnum * incre); |
|
|
| ffgi1b(fptr, readptr, ntodo, incre, buffer, status); |
|
|
| |
| buffptr = buffer; |
| for (ii = 0; ii < ntodo; ii++, next++, buffptr++) |
| { |
| if (*buffptr == 'T') |
| array[next] = 1; |
| else if (*buffptr =='F') |
| array[next] = 0; |
| else if (*buffptr == 0) |
| { |
| array[next] = nulval; |
| if (anynul) |
| *anynul = 1; |
|
|
| if (nulcheck == 2) |
| { |
| nularray[next] = 1; |
| } |
| } |
| else |
| { |
| if (*buffptr == 1) { |
| |
| |
| |
| array[next] = 49; |
| } else { |
| array[next] = (char) *buffptr; |
| } |
| } |
| } |
|
|
| if (*status > 0) |
| { |
| dtemp = (double) next; |
| snprintf(message,FLEN_ERRMSG, |
| "Error reading elements %.0f thruough %.0f of logical array (ffgcl).", |
| dtemp+1., dtemp + ntodo); |
| ffpmsg(message); |
| return(*status); |
| } |
|
|
| |
| |
| |
| remain -= ntodo; |
| if (remain) |
| { |
| elemnum += ntodo; |
|
|
| if (elemnum == repeat) |
| { |
| elemnum = 0; |
| rownum++; |
| } |
| } |
| ntodo = (long) remain; |
|
|
| } |
|
|
| return(*status); |
| } |
| |
| int ffgcx( fitsfile *fptr, |
| int colnum, |
| LONGLONG frow, |
| LONGLONG fbit, |
| LONGLONG nbit, |
| char *larray, |
| int *status) |
| |
| |
| |
| |
| |
| |
| { |
| LONGLONG bstart; |
| long offset, ndone, ii, repeat, bitloc, fbyte; |
| LONGLONG rstart, estart; |
| int tcode, descrp; |
| unsigned char cbuff; |
| static unsigned char onbit[8] = {128, 64, 32, 16, 8, 4, 2, 1}; |
| tcolumn *colptr; |
|
|
| if (*status > 0) |
| return(*status); |
|
|
| |
| if (nbit < 1) |
| return(*status); |
| else if (frow < 1) |
| return(*status = BAD_ROW_NUM); |
| else if (fbit < 1) |
| return(*status = BAD_ELEM_NUM); |
|
|
| |
| 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); |
|
|
| fbyte = (long) ((fbit + 7) / 8); |
| bitloc = (long) (fbit - 1 - ((fbit - 1) / 8 * 8)); |
| ndone = 0; |
| rstart = frow - 1; |
| estart = fbyte - 1; |
|
|
| colptr = (fptr->Fptr)->tableptr; |
| colptr += (colnum - 1); |
|
|
| tcode = colptr->tdatatype; |
|
|
| if (abs(tcode) > TBYTE) |
| return(*status = NOT_LOGICAL_COL); |
|
|
| if (tcode > 0) |
| { |
| descrp = FALSE; |
| |
| repeat = (long) colptr->trepeat; |
|
|
| if (tcode == TBIT) |
| repeat = (repeat + 7) / 8; |
|
|
| if (fbyte > repeat) |
| return(*status = BAD_ELEM_NUM); |
|
|
| |
| bstart = (fptr->Fptr)->datastart + ((fptr->Fptr)->rowlength * rstart) + |
| colptr->tbcol + estart; |
| } |
| else |
| { |
| descrp = TRUE; |
| |
| |
|
|
| ffgdes(fptr, colnum, frow, &repeat, &offset, status); |
|
|
| if (tcode == -TBIT) |
| repeat = (repeat + 7) / 8; |
|
|
| if ((fbit + nbit + 6) / 8 > repeat) |
| return(*status = BAD_ELEM_NUM); |
|
|
| |
| bstart = (fptr->Fptr)->datastart + offset + (fptr->Fptr)->heapstart + estart; |
| } |
|
|
| |
| if (ffmbyt(fptr, bstart, REPORT_EOF, status) > 0) |
| return(*status); |
|
|
| |
| while (1) |
| { |
| if (ffgbyt(fptr, 1, &cbuff, status) > 0) |
| return(*status); |
|
|
| for (ii = bitloc; (ii < 8) && (ndone < nbit); ii++, ndone++) |
| { |
| if(cbuff & onbit[ii]) |
| larray[ndone] = TRUE; |
| else |
| larray[ndone] = FALSE; |
| } |
|
|
| if (ndone == nbit) |
| return(*status); |
|
|
| |
| if (!descrp) |
| { |
| estart++; |
| if (estart == repeat) |
| { |
| |
| estart = 0; |
| rstart = rstart + 1; |
| bstart = (fptr->Fptr)->datastart + ((fptr->Fptr)->rowlength * rstart) + |
| colptr->tbcol; |
|
|
| ffmbyt(fptr, bstart, REPORT_EOF, status); |
| } |
| } |
| bitloc = 0; |
| } |
| } |
| |
| int ffgcxui(fitsfile *fptr, |
| int colnum, |
| LONGLONG firstrow, |
| LONGLONG nrows, |
| long input_first_bit, |
| int input_nbits, |
| unsigned short *array, |
| int *status) |
| |
| |
| |
| |
| |
| |
| { |
| int ii, firstbit, nbits, bytenum, startbit, numbits, endbit; |
| int firstbyte, lastbyte, nbytes, rshift, lshift; |
| unsigned short colbyte[5]; |
| tcolumn *colptr; |
| char message[FLEN_ERRMSG]; |
|
|
| if (*status > 0 || nrows == 0) |
| return(*status); |
|
|
| |
| if (firstrow < 1) |
| { |
| snprintf(message,FLEN_ERRMSG, "Starting row number is less than 1: %ld (ffgcxui)", |
| (long) firstrow); |
| ffpmsg(message); |
| return(*status = BAD_ROW_NUM); |
| } |
| else if (input_first_bit < 1) |
| { |
| snprintf(message,FLEN_ERRMSG, "Starting bit number is less than 1: %ld (ffgcxui)", |
| input_first_bit); |
| ffpmsg(message); |
| return(*status = BAD_ELEM_NUM); |
| } |
| else if (input_nbits > 16) |
| { |
| snprintf(message, FLEN_ERRMSG,"Number of bits to read is > 16: %d (ffgcxui)", |
| input_nbits); |
| ffpmsg(message); |
| return(*status = BAD_ELEM_NUM); |
| } |
|
|
| |
| 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("This is not a binary table extension (ffgcxui)"); |
| return(*status = NOT_BTABLE); |
| } |
|
|
| if (colnum > (fptr->Fptr)->tfield) |
| { |
| snprintf(message, FLEN_ERRMSG,"Specified column number is out of range: %d (ffgcxui)", |
| colnum); |
| ffpmsg(message); |
| snprintf(message, FLEN_ERRMSG," There are %d columns in this table.", |
| (fptr->Fptr)->tfield ); |
| ffpmsg(message); |
|
|
| return(*status = BAD_COL_NUM); |
| } |
|
|
| colptr = (fptr->Fptr)->tableptr; |
| colptr += (colnum - 1); |
|
|
| if (abs(colptr->tdatatype) > TBYTE) |
| { |
| ffpmsg("Can only read bits from X or B type columns. (ffgcxui)"); |
| return(*status = NOT_LOGICAL_COL); |
| } |
|
|
| firstbyte = (input_first_bit - 1 ) / 8 + 1; |
| lastbyte = (input_first_bit + input_nbits - 2) / 8 + 1; |
| nbytes = lastbyte - firstbyte + 1; |
|
|
| if (colptr->tdatatype == TBIT && |
| input_first_bit + input_nbits - 1 > (long) colptr->trepeat) |
| { |
| ffpmsg("Too many bits. Tried to read past width of column (ffgcxui)"); |
| return(*status = BAD_ELEM_NUM); |
| } |
| else if (colptr->tdatatype == TBYTE && lastbyte > (long) colptr->trepeat) |
| { |
| ffpmsg("Too many bits. Tried to read past width of column (ffgcxui)"); |
| return(*status = BAD_ELEM_NUM); |
| } |
|
|
| for (ii = 0; ii < nrows; ii++) |
| { |
| |
| if (ffgcvui(fptr, colnum, firstrow+ii, firstbyte, nbytes, 0, |
| colbyte, NULL, status) > 0) |
| { |
| ffpmsg("Error reading bytes from column (ffgcxui)"); |
| return(*status); |
| } |
|
|
| firstbit = (input_first_bit - 1) % 8; |
| nbits = input_nbits; |
|
|
| array[ii] = 0; |
|
|
| |
| while(nbits) |
| { |
| bytenum = firstbit / 8; |
|
|
| startbit = firstbit % 8; |
| numbits = minvalue(nbits, 8 - startbit); |
| endbit = startbit + numbits - 1; |
|
|
| rshift = 7 - endbit; |
| lshift = nbits - numbits; |
|
|
| array[ii] = ((colbyte[bytenum] >> rshift) << lshift) | array[ii]; |
|
|
| nbits -= numbits; |
| firstbit += numbits; |
| } |
| } |
|
|
| return(*status); |
| } |
|
|
| |
| int ffgcxuk(fitsfile *fptr, |
| int colnum, |
| LONGLONG firstrow, |
| LONGLONG nrows, |
| long input_first_bit, |
| int input_nbits, |
| unsigned int *array, |
| int *status) |
| |
| |
| |
| |
| |
| |
| { |
| int ii, firstbit, nbits, bytenum, startbit, numbits, endbit; |
| int firstbyte, lastbyte, nbytes, rshift, lshift; |
| unsigned int colbyte[5]; |
| tcolumn *colptr; |
| char message[FLEN_ERRMSG]; |
|
|
| if (*status > 0 || nrows == 0) |
| return(*status); |
|
|
| |
| if (firstrow < 1) |
| { |
| snprintf(message, FLEN_ERRMSG,"Starting row number is less than 1: %ld (ffgcxuk)", |
| (long) firstrow); |
| ffpmsg(message); |
| return(*status = BAD_ROW_NUM); |
| } |
| else if (input_first_bit < 1) |
| { |
| snprintf(message, FLEN_ERRMSG,"Starting bit number is less than 1: %ld (ffgcxuk)", |
| input_first_bit); |
| ffpmsg(message); |
| return(*status = BAD_ELEM_NUM); |
| } |
| else if (input_nbits > 32) |
| { |
| snprintf(message, FLEN_ERRMSG,"Number of bits to read is > 32: %d (ffgcxuk)", |
| input_nbits); |
| ffpmsg(message); |
| return(*status = BAD_ELEM_NUM); |
| } |
|
|
| |
| 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("This is not a binary table extension (ffgcxuk)"); |
| return(*status = NOT_BTABLE); |
| } |
|
|
| if (colnum > (fptr->Fptr)->tfield) |
| { |
| snprintf(message, FLEN_ERRMSG,"Specified column number is out of range: %d (ffgcxuk)", |
| colnum); |
| ffpmsg(message); |
| snprintf(message, FLEN_ERRMSG," There are %d columns in this table.", |
| (fptr->Fptr)->tfield ); |
| ffpmsg(message); |
|
|
| return(*status = BAD_COL_NUM); |
| } |
|
|
| colptr = (fptr->Fptr)->tableptr; |
| colptr += (colnum - 1); |
|
|
| if (abs(colptr->tdatatype) > TBYTE) |
| { |
| ffpmsg("Can only read bits from X or B type columns. (ffgcxuk)"); |
| return(*status = NOT_LOGICAL_COL); |
| } |
|
|
| firstbyte = (input_first_bit - 1 ) / 8 + 1; |
| lastbyte = (input_first_bit + input_nbits - 2) / 8 + 1; |
| nbytes = lastbyte - firstbyte + 1; |
|
|
| if (colptr->tdatatype == TBIT && |
| input_first_bit + input_nbits - 1 > (long) colptr->trepeat) |
| { |
| ffpmsg("Too many bits. Tried to read past width of column (ffgcxuk)"); |
| return(*status = BAD_ELEM_NUM); |
| } |
| else if (colptr->tdatatype == TBYTE && lastbyte > (long) colptr->trepeat) |
| { |
| ffpmsg("Too many bits. Tried to read past width of column (ffgcxuk)"); |
| return(*status = BAD_ELEM_NUM); |
| } |
|
|
| for (ii = 0; ii < nrows; ii++) |
| { |
| |
| if (ffgcvuk(fptr, colnum, firstrow+ii, firstbyte, nbytes, 0, |
| colbyte, NULL, status) > 0) |
| { |
| ffpmsg("Error reading bytes from column (ffgcxuk)"); |
| return(*status); |
| } |
|
|
| firstbit = (input_first_bit - 1) % 8; |
| nbits = input_nbits; |
|
|
| array[ii] = 0; |
|
|
| |
| while(nbits) |
| { |
| bytenum = firstbit / 8; |
|
|
| startbit = firstbit % 8; |
| numbits = minvalue(nbits, 8 - startbit); |
| endbit = startbit + numbits - 1; |
|
|
| rshift = 7 - endbit; |
| lshift = nbits - numbits; |
|
|
| array[ii] = ((colbyte[bytenum] >> rshift) << lshift) | array[ii]; |
|
|
| nbits -= numbits; |
| firstbit += numbits; |
| } |
| } |
|
|
| return(*status); |
| } |
|
|