| |
| #include <string.h> |
| #include <ctype.h> |
| #include <math.h> |
| #include <stdlib.h> |
| #include "fitsio2.h" |
|
|
| typedef struct { |
| union { |
| char *b; |
| short *i; |
| int *j; |
| float *r; |
| double *d; |
| } hist; |
|
|
| fitsfile *tblptr; |
|
|
| int haxis, hcolnum[4], himagetype; |
| long haxis1, haxis2, haxis3, haxis4; |
| double amin1, amin2, amin3, amin4; |
| double maxbin1, maxbin2, maxbin3, maxbin4; |
| double binsize1, binsize2, binsize3, binsize4; |
| int wtrecip, wtcolnum; |
| double weight; |
| char *rowselector; |
|
|
| } histType; |
|
|
| |
| int ffbins(char *binspec, |
| int *imagetype, |
| int *histaxis, |
| char colname[4][FLEN_VALUE], |
| double *minin, |
| double *maxin, |
| double *binsizein, |
| char minname[4][FLEN_VALUE], |
| char maxname[4][FLEN_VALUE], |
| char binname[4][FLEN_VALUE], |
| double *wt, |
| char *wtname, |
| int *recip, |
| int *status) |
| { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| int ii, slen, defaulttype; |
| char *ptr, tmpname[FLEN_VALUE], *file_expr = NULL; |
| double dummy; |
|
|
| if (*status > 0) |
| return(*status); |
|
|
| |
| *histaxis = 2; |
| *imagetype = TINT; |
| defaulttype = 1; |
| *wt = 1.; |
| *recip = 0; |
| *wtname = '\0'; |
|
|
| |
| for (ii = 0; ii < 4; ii++) |
| { |
| *colname[ii] = '\0'; |
| *minname[ii] = '\0'; |
| *maxname[ii] = '\0'; |
| *binname[ii] = '\0'; |
| minin[ii] = DOUBLENULLVALUE; |
| maxin[ii] = DOUBLENULLVALUE; |
| binsizein[ii] = DOUBLENULLVALUE; |
| } |
|
|
| ptr = binspec + 3; |
|
|
| if (*ptr == 'i' ) |
| { |
| *imagetype = TSHORT; |
| defaulttype = 0; |
| ptr++; |
| } |
| else if (*ptr == 'j' ) |
| { |
| defaulttype = 0; |
| ptr ++; |
| } |
| else if (*ptr == 'r' ) |
| { |
| *imagetype = TFLOAT; |
| defaulttype = 0; |
| ptr ++; |
| } |
| else if (*ptr == 'd' ) |
| { |
| *imagetype = TDOUBLE; |
| defaulttype = 0; |
| ptr ++; |
| } |
| else if (*ptr == 'b' ) |
| { |
| *imagetype = TBYTE; |
| defaulttype = 0; |
| ptr ++; |
| } |
|
|
| if (*ptr == '\0') |
| return(*status); |
| else if (*ptr != ' ') |
| { |
| ffpmsg("binning specification syntax error:"); |
| ffpmsg(binspec); |
| return(*status = URL_PARSE_ERROR); |
| } |
|
|
| while (*ptr == ' ') |
| ptr++; |
|
|
| if (*ptr == '\0') |
| return(*status); |
|
|
| |
|
|
| if( *ptr=='@' ) { |
| if( ffimport_file( ptr+1, &file_expr, status ) ) return(*status); |
| ptr = file_expr; |
| while (*ptr == ' ') |
| ptr++; |
| } |
|
|
| if (*ptr == '(' ) |
| { |
| |
| |
|
|
| for (ii = 0; ii < 4; ii++) |
| { |
| ptr++; |
| while (*ptr == ' ') |
| ptr++; |
|
|
| slen = strcspn(ptr, " ,)"); |
| strncat(colname[ii], ptr, slen); |
|
|
| ptr += slen; |
| while (*ptr == ' ') |
| ptr++; |
|
|
| if (*ptr == ')' ) |
| { |
| *histaxis = ii + 1; |
| break; |
| } |
| } |
|
|
| if (ii == 4) |
| { |
| ffpmsg( |
| "binning specification has too many column names or is missing closing ')':"); |
| ffpmsg(binspec); |
| if( file_expr ) free( file_expr ); |
| return(*status = URL_PARSE_ERROR); |
| } |
|
|
| ptr++; |
| while (*ptr == ' ') |
| ptr++; |
|
|
| if (*ptr == '\0') { |
| if( file_expr ) free( file_expr ); |
| return(*status); |
| } |
|
|
| else if (*ptr != '=') |
| { |
| ffpmsg("illegal binning specification in URL:"); |
| ffpmsg(" an equals sign '=' must follow the column names"); |
| ffpmsg(binspec); |
| if( file_expr ) free( file_expr ); |
| return(*status = URL_PARSE_ERROR); |
| } |
|
|
| ptr++; |
| while (*ptr == ' ') |
| ptr++; |
|
|
| |
| ffbinr(&ptr, tmpname, minin, |
| maxin, binsizein, minname[0], |
| maxname[0], binname[0], status); |
| if (*status > 0) |
| { |
| ffpmsg("illegal binning specification in URL:"); |
| ffpmsg(binspec); |
| if( file_expr ) free( file_expr ); |
| return(*status); |
| } |
|
|
| for (ii = 1; ii < *histaxis; ii++) |
| { |
| minin[ii] = minin[0]; |
| maxin[ii] = maxin[0]; |
| binsizein[ii] = binsizein[0]; |
| strcpy(minname[ii], minname[0]); |
| strcpy(maxname[ii], maxname[0]); |
| strcpy(binname[ii], binname[0]); |
| } |
|
|
| while (*ptr == ' ') |
| ptr++; |
|
|
| if (*ptr == ';') |
| goto getweight; |
|
|
| if (*ptr != '\0') |
| { |
| ffpmsg("illegal syntax after binning range specification in URL:"); |
| ffpmsg(binspec); |
| if( file_expr ) free( file_expr ); |
| return(*status = URL_PARSE_ERROR); |
| } |
|
|
| return(*status); |
| } |
|
|
| |
| |
| |
| |
|
|
| for (ii = 0; ii < 4; ii++) |
| { |
| ffbinr(&ptr, colname[ii], &minin[ii], |
| &maxin[ii], &binsizein[ii], minname[ii], |
| maxname[ii], binname[ii], status); |
|
|
| if (*status > 0) |
| { |
| ffpmsg("illegal syntax in binning range specification in URL:"); |
| ffpmsg(binspec); |
| if( file_expr ) free( file_expr ); |
| return(*status); |
| } |
|
|
| if (*ptr == '\0' || *ptr == ';') |
| break; |
|
|
| if (*ptr == ' ') |
| { |
| while (*ptr == ' ') |
| ptr++; |
|
|
| if (*ptr == '\0' || *ptr == ';') |
| break; |
|
|
| if (*ptr == ',') |
| ptr++; |
| } |
| else if (*ptr == ',') |
| { |
| ptr++; |
| } |
| else |
| { |
| ffpmsg("illegal characters following binning specification in URL:"); |
| ffpmsg(binspec); |
| if( file_expr ) free( file_expr ); |
| return(*status = URL_PARSE_ERROR); |
| } |
| } |
|
|
| if (ii == 4) |
| { |
| |
| ffpmsg("illegal binning specification in URL:"); |
| ffpmsg("apparently greater than 4 histogram dimensions"); |
| ffpmsg(binspec); |
| return(*status = URL_PARSE_ERROR); |
| } |
| else |
| *histaxis = ii + 1; |
|
|
| |
| |
|
|
| if (*histaxis == 1 && *colname[0] == '\0' && |
| minin[0] == DOUBLENULLVALUE && maxin[0] == DOUBLENULLVALUE) |
| { |
| *histaxis = 2; |
| binsizein[1] = binsizein[0]; |
| } |
|
|
| getweight: |
| if (*ptr == ';') |
| { |
| ptr++; |
| |
| while (*ptr == ' ') |
| ptr++; |
|
|
| recip = 0; |
| if (*ptr == '/') |
| { |
| *recip = 1; |
| ptr++; |
|
|
| while (*ptr == ' ') |
| ptr++; |
| } |
|
|
| |
| |
|
|
| ffbinr(&ptr, wtname, &dummy, &dummy, wt, tmpname, |
| tmpname, tmpname, status); |
|
|
| if (*status > 0) |
| { |
| ffpmsg("illegal binning weight specification in URL:"); |
| ffpmsg(binspec); |
| if( file_expr ) free( file_expr ); |
| return(*status); |
| } |
|
|
| |
| |
|
|
| if ( (defaulttype && *wt != 1.0) || (defaulttype && *wtname) ) |
| *imagetype = TFLOAT; |
| } |
|
|
| while (*ptr == ' ') |
| ptr++; |
|
|
| if (*ptr != '\0') |
| { |
| ffpmsg("illegal syntax after binning weight specification in URL:"); |
| ffpmsg(binspec); |
| *status = URL_PARSE_ERROR; |
| } |
|
|
| if( file_expr ) free( file_expr ); |
| return(*status); |
| } |
| |
| int ffbinr(char **ptr, |
| char *colname, |
| double *minin, |
| double *maxin, |
| double *binsizein, |
| char *minname, |
| char *maxname, |
| char *binname, |
| int *status) |
| |
| |
| |
| |
| { |
| int slen, isanumber=0; |
| char *token=0; |
|
|
| if (*status > 0) |
| return(*status); |
|
|
| slen = fits_get_token2(ptr, " ,=:;", &token, &isanumber, status); |
|
|
| if ((*status) || (slen == 0 && (**ptr == '\0' || **ptr == ',' || **ptr == ';')) ) |
| return(*status); |
| |
| if (!isanumber && **ptr != ':') |
| { |
| |
| |
| |
| |
| |
| if (token) |
| { |
| if (strlen(token) > FLEN_VALUE-1) |
| { |
| ffpmsg("column name too long (ffbinr)"); |
| free(token); |
| return(*status=PARSE_SYNTAX_ERR); |
| } |
| if (token[0] == '#' && isdigit((int) token[1]) ) |
| { |
| |
| strcpy(colname, token+1); |
| } |
| else |
| strcpy(colname, token); |
| free(token); |
| token=0; |
| } |
| while (**ptr == ' ') |
| (*ptr)++; |
|
|
| if (**ptr != '=') |
| return(*status); |
| |
| (*ptr)++; |
|
|
| while (**ptr == ' ') |
| (*ptr)++; |
|
|
| |
| slen = fits_get_token2(ptr, " ,:;", &token, &isanumber, status); |
| if (*status) |
| return(*status); |
| } |
|
|
| if (**ptr != ':') |
| { |
| |
| |
| if (token) |
| { |
| if (!isanumber) |
| { |
| if (strlen(token) > FLEN_VALUE-1) |
| { |
| ffpmsg("binname too long (ffbinr)"); |
| free(token); |
| return(*status=PARSE_SYNTAX_ERR); |
| } |
| strcpy(binname, token); |
| } |
| else |
| *binsizein = strtod(token, NULL); |
|
|
| free(token); |
| } |
| |
| return(*status); |
| } |
| else |
| { |
| |
| if (slen) |
| { |
| if (!isanumber) |
| { |
| if (strlen(token) > FLEN_VALUE-1) |
| { |
| ffpmsg("minname too long (ffbinr)"); |
| free(token); |
| return(*status=PARSE_SYNTAX_ERR); |
| } |
| strcpy(minname, token); |
| } |
| else |
| *minin = strtod(token, NULL); |
| free(token); |
| token=0; |
| } |
| } |
|
|
| (*ptr)++; |
| slen = fits_get_token2(ptr, " ,:;", &token, &isanumber, status); |
| if (*status) |
| return(*status); |
|
|
| |
| if (slen) |
| { |
| if (!isanumber) |
| { |
| if (strlen(token) > FLEN_VALUE-1) |
| { |
| ffpmsg("maxname too long (ffbinr)"); |
| free(token); |
| return(*status=PARSE_SYNTAX_ERR); |
| } |
| strcpy(maxname, token); |
| } |
| else |
| *maxin = strtod(token, NULL); |
| free(token); |
| token=0; |
| } |
|
|
| if (**ptr != ':') |
| { |
| free(token); |
| return(*status); |
| } |
|
|
| (*ptr)++; |
| slen = fits_get_token2(ptr, " ,:;", &token, &isanumber, status); |
| if (*status) |
| return(*status); |
|
|
| |
| if (slen) |
| { |
| if (!isanumber) |
| { |
| if (strlen(token) > FLEN_VALUE-1) |
| { |
| ffpmsg("binname too long (ffbinr)"); |
| free(token); |
| return(*status=PARSE_SYNTAX_ERR); |
| } |
| strcpy(binname, token); |
| } |
| else |
| *binsizein = strtod(token, NULL); |
| free(token); |
| } |
|
|
| return(*status); |
| } |
| |
| int ffhist2(fitsfile **fptr, |
| |
| char *outfile, |
| int imagetype, |
| int naxis, |
| char colname[4][FLEN_VALUE], |
| double *minin, |
| double *maxin, |
| double *binsizein, |
| char minname[4][FLEN_VALUE], |
| char maxname[4][FLEN_VALUE], |
| char binname[4][FLEN_VALUE], |
| double weightin, |
| char wtcol[FLEN_VALUE], |
| int recip, |
| char *selectrow, |
| |
| |
| |
| |
| |
| int *status) |
| { |
| fitsfile *histptr; |
| int bitpix, colnum[4], wtcolnum; |
| long haxes[4]; |
| double amin[4], amax[4], binsize[4], weight; |
|
|
| if (*status > 0) |
| return(*status); |
|
|
| if (naxis > 4) |
| { |
| ffpmsg("histogram has more than 4 dimensions"); |
| return(*status = BAD_DIMEN); |
| } |
|
|
| |
| if ((*fptr)->HDUposition != ((*fptr)->Fptr)->curhdu) |
| ffmahd(*fptr, ((*fptr)->HDUposition) + 1, NULL, status); |
|
|
| if (imagetype == TBYTE) |
| bitpix = BYTE_IMG; |
| else if (imagetype == TSHORT) |
| bitpix = SHORT_IMG; |
| else if (imagetype == TINT) |
| bitpix = LONG_IMG; |
| else if (imagetype == TFLOAT) |
| bitpix = FLOAT_IMG; |
| else if (imagetype == TDOUBLE) |
| bitpix = DOUBLE_IMG; |
| else |
| return(*status = BAD_DATATYPE); |
|
|
| |
| |
| |
|
|
| if (fits_calc_binningd( |
| *fptr, naxis, colname, minin, maxin, binsizein, minname, maxname, binname, |
| colnum, haxes, amin, amax, binsize, status) > 0) |
| { |
| ffpmsg("failed to determine binning parameters"); |
| return(*status); |
| } |
| |
| |
| if (*wtcol) |
| { |
| |
| if (ffgky(*fptr, TDOUBLE, wtcol, &weight, NULL, status) ) |
| { |
| |
| *status = 0; |
|
|
| |
| if (ffgcno(*fptr, CASEINSEN, wtcol, &wtcolnum, status) > 0) |
| { |
| ffpmsg( |
| "keyword or column for histogram weights doesn't exist: "); |
| ffpmsg(wtcol); |
| return(*status); |
| } |
|
|
| weight = DOUBLENULLVALUE; |
| } |
| } |
| else |
| weight = (double) weightin; |
|
|
| if (weight <= 0. && weight != DOUBLENULLVALUE) |
| { |
| ffpmsg("Illegal histogramming weighting factor <= 0."); |
| return(*status = URL_PARSE_ERROR); |
| } |
|
|
| if (recip && weight != DOUBLENULLVALUE) |
| |
| weight = (double) (1.0 / weight); |
|
|
| |
| if (fits_create_file(&histptr, outfile, status) > 0) |
| { |
| ffpmsg("failed to create temp output file for histogram"); |
| return(*status); |
| } |
|
|
| |
| if (ffcrim(histptr, bitpix, naxis, haxes, status) > 0) |
| { |
| ffpmsg("failed to create output histogram FITS image"); |
| return(*status); |
| } |
|
|
| |
| if (fits_copy_pixlist2image(*fptr, histptr, 9, naxis, colnum, status) > 0) |
| { |
| ffpmsg("failed to copy pixel list keywords to new histogram header"); |
| return(*status); |
| } |
|
|
| |
| fits_write_keys_histo(*fptr, histptr, naxis, colnum, status); |
| |
| |
| fits_rebin_wcsd(histptr, naxis, amin, binsize, status); |
| |
| |
| if (fits_make_histd(*fptr, histptr, bitpix, naxis, haxes, colnum, amin, amax, |
| binsize, weight, wtcolnum, recip, selectrow, status) > 0) |
| { |
| ffpmsg("failed to calculate new histogram values"); |
| return(*status); |
| } |
| |
| |
| ffclos(*fptr, status); |
| *fptr = histptr; |
|
|
| return(*status); |
| } |
| |
|
|
| |
| |
| fitsfile *ffhist3(fitsfile *fptr, |
| char *outfile, |
| int imagetype, |
| int naxis, |
| char colname[4][FLEN_VALUE], |
| double *minin, |
| double *maxin, |
| double *binsizein, |
| char minname[4][FLEN_VALUE], |
| char maxname[4][FLEN_VALUE], |
| char binname[4][FLEN_VALUE], |
| double weightin, |
| char wtcol[FLEN_VALUE], |
| int recip, |
| char *selectrow, |
| |
| |
| |
| |
| |
| int *status) |
| { |
| fitsfile *histptr; |
| int bitpix, colnum[4], wtcolnum; |
| long haxes[4]; |
| double amin[4], amax[4], binsize[4], weight; |
|
|
| if (*status > 0) |
| return(NULL); |
|
|
| if (naxis > 4) |
| { |
| ffpmsg("histogram has more than 4 dimensions"); |
| *status = BAD_DIMEN; |
| return(NULL); |
| } |
|
|
| |
| if ((fptr)->HDUposition != ((fptr)->Fptr)->curhdu) |
| ffmahd(fptr, ((fptr)->HDUposition) + 1, NULL, status); |
|
|
| if (imagetype == TBYTE) |
| bitpix = BYTE_IMG; |
| else if (imagetype == TSHORT) |
| bitpix = SHORT_IMG; |
| else if (imagetype == TINT) |
| bitpix = LONG_IMG; |
| else if (imagetype == TFLOAT) |
| bitpix = FLOAT_IMG; |
| else if (imagetype == TDOUBLE) |
| bitpix = DOUBLE_IMG; |
| else{ |
| *status = BAD_DATATYPE; |
| return(NULL); |
| } |
| |
| |
| |
|
|
| if (fits_calc_binningd( |
| fptr, naxis, colname, minin, maxin, binsizein, minname, maxname, binname, |
| colnum, haxes, amin, amax, binsize, status) > 0) |
| { |
| ffpmsg("failed to determine binning parameters"); |
| return(NULL); |
| } |
| |
| |
| if (*wtcol) |
| { |
| |
| if (fits_read_key(fptr, TDOUBLE, wtcol, &weight, NULL, status) ) |
| { |
| |
| *status = 0; |
|
|
| |
| if (ffgcno(fptr, CASEINSEN, wtcol, &wtcolnum, status) > 0) |
| { |
| ffpmsg( |
| "keyword or column for histogram weights doesn't exist: "); |
| ffpmsg(wtcol); |
| return(NULL); |
| } |
|
|
| weight = DOUBLENULLVALUE; |
| } |
| } |
| else |
| weight = (double) weightin; |
|
|
| if (weight <= 0. && weight != DOUBLENULLVALUE) |
| { |
| ffpmsg("Illegal histogramming weighting factor <= 0."); |
| *status = URL_PARSE_ERROR; |
| return(NULL); |
| } |
|
|
| if (recip && weight != DOUBLENULLVALUE) |
| |
| weight = (double) (1.0 / weight); |
|
|
| |
| if (fits_create_file(&histptr, outfile, status) > 0) |
| { |
| ffpmsg("failed to create temp output file for histogram"); |
| return(NULL); |
| } |
|
|
| |
| if (ffcrim(histptr, bitpix, naxis, haxes, status) > 0) |
| { |
| ffpmsg("failed to create output histogram FITS image"); |
| return(NULL); |
| } |
|
|
| |
| if (fits_copy_pixlist2image(fptr, histptr, 9, naxis, colnum, status) > 0) |
| { |
| ffpmsg("failed to copy pixel list keywords to new histogram header"); |
| return(NULL); |
| } |
|
|
| |
| fits_write_keys_histo(fptr, histptr, naxis, colnum, status); |
| |
| |
| fits_rebin_wcsd(histptr, naxis, amin, binsize, status); |
| |
| |
| if (fits_make_histd(fptr, histptr, bitpix, naxis, haxes, colnum, amin, amax, |
| binsize, weight, wtcolnum, recip, selectrow, status) > 0) |
| { |
| ffpmsg("failed to calculate new histogram values"); |
| return(NULL); |
| } |
| |
| return(histptr); |
| } |
| |
| int ffhist(fitsfile **fptr, |
| |
| char *outfile, |
| int imagetype, |
| int naxis, |
| char colname[4][FLEN_VALUE], |
| double *minin, |
| double *maxin, |
| double *binsizein, |
| char minname[4][FLEN_VALUE], |
| char maxname[4][FLEN_VALUE], |
| char binname[4][FLEN_VALUE], |
| double weightin, |
| char wtcol[FLEN_VALUE], |
| int recip, |
| char *selectrow, |
| |
| |
| |
| |
| |
| int *status) |
| { |
| int ii, datatype, repeat, imin, imax, ibin, bitpix, tstatus, use_datamax = 0; |
| long haxes[4]; |
| fitsfile *histptr; |
| char errmsg[FLEN_ERRMSG], keyname[FLEN_KEYWORD], card[FLEN_CARD]; |
| tcolumn *colptr; |
| iteratorCol imagepars[1]; |
| int n_cols = 1, nkeys; |
| long offset = 0; |
| long n_per_loop = -1; |
| histType histData; |
| |
| double amin[4], amax[4], binsize[4], maxbin[4]; |
| double datamin = DOUBLENULLVALUE, datamax = DOUBLENULLVALUE; |
| char svalue[FLEN_VALUE]; |
| double dvalue; |
| char cpref[4][FLEN_VALUE]; |
| char *cptr; |
|
|
| if (*status > 0) |
| return(*status); |
|
|
| if (naxis > 4) |
| { |
| ffpmsg("histogram has more than 4 dimensions"); |
| return(*status = BAD_DIMEN); |
| } |
|
|
| |
| if ((*fptr)->HDUposition != ((*fptr)->Fptr)->curhdu) |
| ffmahd(*fptr, ((*fptr)->HDUposition) + 1, NULL, status); |
|
|
| histData.tblptr = *fptr; |
| histData.himagetype = imagetype; |
| histData.haxis = naxis; |
| histData.rowselector = selectrow; |
|
|
| if (imagetype == TBYTE) |
| bitpix = BYTE_IMG; |
| else if (imagetype == TSHORT) |
| bitpix = SHORT_IMG; |
| else if (imagetype == TINT) |
| bitpix = LONG_IMG; |
| else if (imagetype == TFLOAT) |
| bitpix = FLOAT_IMG; |
| else if (imagetype == TDOUBLE) |
| bitpix = DOUBLE_IMG; |
| else |
| return(*status = BAD_DATATYPE); |
|
|
| |
| |
|
|
| tstatus = 0; |
| ffgky(*fptr, TSTRING, "CPREF", cpref[0], NULL, &tstatus); |
|
|
| if (!tstatus) |
| { |
| |
| cptr = cpref[0]; |
|
|
| |
| while (*cptr != ',' && *cptr != '\0') |
| cptr++; |
|
|
| if (*cptr != '\0') |
| { |
| *cptr = '\0'; |
| cptr++; |
| while (*cptr == ' ') |
| cptr++; |
|
|
| strcpy(cpref[1], cptr); |
| cptr = cpref[1]; |
|
|
| |
| while (*cptr != ',' && *cptr != '\0') |
| cptr++; |
|
|
| if (*cptr != '\0') |
| { |
| *cptr = '\0'; |
| cptr++; |
| while (*cptr == ' ') |
| cptr++; |
|
|
| strcpy(cpref[2], cptr); |
| cptr = cpref[2]; |
|
|
| |
| while (*cptr != ',' && *cptr != '\0') |
| cptr++; |
|
|
| if (*cptr != '\0') |
| { |
| *cptr = '\0'; |
| cptr++; |
| while (*cptr == ' ') |
| cptr++; |
|
|
| strcpy(cpref[3], cptr); |
|
|
| } |
| } |
| } |
| } |
|
|
| for (ii = 0; ii < naxis; ii++) |
| { |
|
|
| |
|
|
| if (*minname[ii]) |
| { |
| if (ffgky(*fptr, TDOUBLE, minname[ii], &minin[ii], NULL, status) ) |
| { |
| ffpmsg("error reading histogramming minimum keyword"); |
| ffpmsg(minname[ii]); |
| return(*status); |
| } |
| } |
|
|
| if (*maxname[ii]) |
| { |
| if (ffgky(*fptr, TDOUBLE, maxname[ii], &maxin[ii], NULL, status) ) |
| { |
| ffpmsg("error reading histogramming maximum keyword"); |
| ffpmsg(maxname[ii]); |
| return(*status); |
| } |
| } |
|
|
| if (*binname[ii]) |
| { |
| if (ffgky(*fptr, TDOUBLE, binname[ii], &binsizein[ii], NULL, status) ) |
| { |
| ffpmsg("error reading histogramming binsize keyword"); |
| ffpmsg(binname[ii]); |
| return(*status); |
| } |
| } |
|
|
| if (binsizein[ii] == 0.) |
| { |
| ffpmsg("error: histogram binsize = 0"); |
| return(*status = ZERO_SCALE); |
| } |
|
|
| if (*colname[ii] == '\0') |
| { |
| strcpy(colname[ii], cpref[ii]); |
| if (*colname[ii] == '\0') |
| { |
| if (ii == 0) |
| strcpy(colname[ii], "X"); |
| else if (ii == 1) |
| strcpy(colname[ii], "Y"); |
| else if (ii == 2) |
| strcpy(colname[ii], "Z"); |
| else if (ii == 3) |
| strcpy(colname[ii], "T"); |
| } |
| } |
|
|
| |
| if (ffgcno(*fptr, CASEINSEN, colname[ii], histData.hcolnum+ii, status) |
| > 0) |
| { |
| strcpy(errmsg, "column for histogram axis doesn't exist: "); |
| strncat(errmsg, colname[ii], FLEN_ERRMSG-strlen(errmsg)-1); |
| ffpmsg(errmsg); |
| return(*status); |
| } |
|
|
| colptr = ((*fptr)->Fptr)->tableptr; |
| colptr += (histData.hcolnum[ii] - 1); |
|
|
| repeat = (int) colptr->trepeat; |
| if (repeat > 1) |
| { |
| strcpy(errmsg, "Can't bin a vector column: "); |
| strncat(errmsg, colname[ii],FLEN_ERRMSG-strlen(errmsg)-1); |
| ffpmsg(errmsg); |
| return(*status = BAD_DATATYPE); |
| } |
|
|
| |
| fits_get_coltype(*fptr, histData.hcolnum[ii], &datatype, |
| NULL, NULL, status); |
|
|
| if (datatype < 0 || datatype == TSTRING) |
| { |
| strcpy(errmsg, "Inappropriate datatype; can't bin this column: "); |
| strncat(errmsg, colname[ii],FLEN_ERRMSG-strlen(errmsg)-1); |
| ffpmsg(errmsg); |
| return(*status = BAD_DATATYPE); |
| } |
|
|
| |
| |
| |
| if (minin[ii] == DOUBLENULLVALUE) |
| { |
| ffkeyn("TLMIN", histData.hcolnum[ii], keyname, status); |
| if (ffgky(*fptr, TDOUBLE, keyname, amin+ii, NULL, status) > 0) |
| { |
| |
| *status = 0; |
| if (fits_get_col_minmax(*fptr, histData.hcolnum[ii], amin+ii, &datamax, status) > 0) |
| { |
| strcpy(errmsg, "Error calculating datamin and datamax for column: "); |
| strncat(errmsg, colname[ii],FLEN_ERRMSG-strlen(errmsg)-1); |
| ffpmsg(errmsg); |
| return(*status); |
| } |
| } |
| } |
| else |
| { |
| amin[ii] = (double) minin[ii]; |
| } |
|
|
| if (maxin[ii] == DOUBLENULLVALUE) |
| { |
| ffkeyn("TLMAX", histData.hcolnum[ii], keyname, status); |
| if (ffgky(*fptr, TDOUBLE, keyname, &amax[ii], NULL, status) > 0) |
| { |
| *status = 0; |
| if(datamax != DOUBLENULLVALUE) |
| { |
| amax[ii] = datamax; |
| } |
| else |
| { |
| |
| if (fits_get_col_minmax(*fptr, histData.hcolnum[ii], &datamin, &amax[ii], status) > 0) |
| { |
| strcpy(errmsg, "Error calculating datamin and datamax for column: "); |
| strncat(errmsg, colname[ii],FLEN_ERRMSG-strlen(errmsg)-1); |
| ffpmsg(errmsg); |
| return(*status); |
| } |
| } |
| } |
| use_datamax = 1; |
| |
| } |
| else |
| { |
| amax[ii] = (double) maxin[ii]; |
| } |
|
|
| |
| if (binsizein[ii] == DOUBLENULLVALUE) |
| { |
| tstatus = 0; |
| ffkeyn("TDBIN", histData.hcolnum[ii], keyname, &tstatus); |
|
|
| if (ffgky(*fptr, TDOUBLE, keyname, binsizein + ii, NULL, &tstatus) > 0) |
| { |
| |
| binsizein[ii] = (amax[ii] - amin[ii]) / 10. ; |
| if (binsizein[ii] > 1.) |
| binsizein[ii] = 1.; |
| } |
| } |
|
|
| if ( (amin[ii] > amax[ii] && binsizein[ii] > 0. ) || |
| (amin[ii] < amax[ii] && binsizein[ii] < 0. ) ) |
| binsize[ii] = (double) -binsizein[ii]; |
| else |
| binsize[ii] = (double) binsizein[ii]; |
|
|
| ibin = (int) binsize[ii]; |
| imin = (int) amin[ii]; |
| imax = (int) amax[ii]; |
|
|
| |
| |
| |
|
|
| if (datatype <= TLONG && (double) imin == amin[ii] && |
| (double) imax == amax[ii] && |
| (double) ibin == binsize[ii] ) |
| { |
| |
| |
| |
|
|
| haxes[ii] = (imax - imin) / ibin + 1; |
| |
| maxbin[ii] = (double) (haxes[ii] + 1.); |
|
|
| if (amin[ii] < amax[ii]) |
| { |
| amin[ii] = (double) (amin[ii] - 0.5); |
| amax[ii] = (double) (amax[ii] + 0.5); |
| } |
| else |
| { |
| amin[ii] = (double) (amin[ii] + 0.5); |
| amax[ii] = (double) (amax[ii] - 0.5); |
| } |
| } |
| else if (use_datamax) |
| { |
| |
| |
| |
| |
| |
|
|
| maxbin[ii] = (amax[ii] - amin[ii]) / binsize[ii]; |
| haxes[ii] = (long) (maxbin[ii] + 1); |
| } |
| else |
| { |
| |
| |
| |
| maxbin[ii] = (amax[ii] - amin[ii]) / binsize[ii]; |
| haxes[ii] = (long) maxbin[ii]; |
|
|
| if (amin[ii] < amax[ii]) |
| { |
| if (amin[ii] + (haxes[ii] * binsize[ii]) < amax[ii]) |
| haxes[ii]++; |
| } |
| else |
| { |
| if (amin[ii] + (haxes[ii] * binsize[ii]) > amax[ii]) |
| haxes[ii]++; |
| } |
| } |
| } |
|
|
| |
| if (*wtcol) |
| { |
| |
| if (ffgky(*fptr, TDOUBLE, wtcol, &histData.weight, NULL, status) ) |
| { |
| |
| *status = 0; |
|
|
| |
| if (ffgcno(*fptr, CASEINSEN, wtcol, &histData.wtcolnum, status) > 0) |
| { |
| ffpmsg( |
| "keyword or column for histogram weights doesn't exist: "); |
| ffpmsg(wtcol); |
| return(*status); |
| } |
|
|
| histData.weight = DOUBLENULLVALUE; |
| } |
| } |
| else |
| histData.weight = (double) weightin; |
|
|
| if (histData.weight <= 0. && histData.weight != DOUBLENULLVALUE) |
| { |
| ffpmsg("Illegal histogramming weighting factor <= 0."); |
| return(*status = URL_PARSE_ERROR); |
| } |
|
|
| if (recip && histData.weight != DOUBLENULLVALUE) |
| |
| histData.weight = (double) (1.0 / histData.weight); |
|
|
| histData.wtrecip = recip; |
| |
| |
| if (ffinit(&histptr, outfile, status) > 0) |
| { |
| ffpmsg("failed to create temp output file for histogram"); |
| return(*status); |
| } |
|
|
| if (ffcrim(histptr, bitpix, histData.haxis, haxes, status) > 0) |
| { |
| ffpmsg("failed to create primary array histogram in temp file"); |
| ffclos(histptr, status); |
| return(*status); |
| } |
|
|
| |
| fits_get_hdrspace(*fptr, &nkeys, NULL, status); |
| for (ii = 1; ii <= nkeys; ii++) |
| { |
| fits_read_record(*fptr, ii, card, status); |
| if (fits_get_keyclass(card) >= 120) |
| fits_write_record(histptr, card, status); |
| } |
|
|
| |
| |
| |
|
|
| histData.amin1 = amin[0]; |
| histData.maxbin1 = maxbin[0]; |
| histData.binsize1 = binsize[0]; |
| histData.haxis1 = haxes[0]; |
|
|
| if (histData.haxis > 1) |
| { |
| histData.amin2 = amin[1]; |
| histData.maxbin2 = maxbin[1]; |
| histData.binsize2 = binsize[1]; |
| histData.haxis2 = haxes[1]; |
|
|
| if (histData.haxis > 2) |
| { |
| histData.amin3 = amin[2]; |
| histData.maxbin3 = maxbin[2]; |
| histData.binsize3 = binsize[2]; |
| histData.haxis3 = haxes[2]; |
|
|
| if (histData.haxis > 3) |
| { |
| histData.amin4 = amin[3]; |
| histData.maxbin4 = maxbin[3]; |
| histData.binsize4 = binsize[3]; |
| histData.haxis4 = haxes[3]; |
| } |
| } |
| } |
|
|
| |
| fits_iter_set_file(imagepars, histptr); |
| fits_iter_set_datatype(imagepars, imagetype); |
| fits_iter_set_iotype(imagepars, OutputCol); |
|
|
| |
| if (fits_iterate_data(n_cols, imagepars, offset, n_per_loop, |
| ffwritehisto, (void*)&histData, status) ) |
| return(*status); |
|
|
| |
| |
| for (ii = 0; ii < histData.haxis; ii++) |
| { |
| |
| tstatus = 0; |
| ffkeyn("TCTYP", histData.hcolnum[ii], keyname, &tstatus); |
| ffgky(*fptr, TSTRING, keyname, svalue, NULL, &tstatus); |
| if (tstatus) |
| { |
| tstatus = 0; |
| ffkeyn("TTYPE", histData.hcolnum[ii], keyname, &tstatus); |
| ffgky(*fptr, TSTRING, keyname, svalue, NULL, &tstatus); |
| } |
|
|
| if (!tstatus) |
| { |
| ffkeyn("CTYPE", ii + 1, keyname, &tstatus); |
| ffpky(histptr, TSTRING, keyname, svalue, "Coordinate Type", &tstatus); |
| } |
| else |
| tstatus = 0; |
|
|
| |
| ffkeyn("TCUNI", histData.hcolnum[ii], keyname, &tstatus); |
| ffgky(*fptr, TSTRING, keyname, svalue, NULL, &tstatus); |
| if (tstatus) |
| { |
| tstatus = 0; |
| ffkeyn("TUNIT", histData.hcolnum[ii], keyname, &tstatus); |
| ffgky(*fptr, TSTRING, keyname, svalue, NULL, &tstatus); |
| } |
|
|
| if (!tstatus) |
| { |
| ffkeyn("CUNIT", ii + 1, keyname, &tstatus); |
| ffpky(histptr, TSTRING, keyname, svalue, "Coordinate Units", &tstatus); |
| } |
| else |
| tstatus = 0; |
|
|
| |
| ffkeyn("TCRPX", histData.hcolnum[ii], keyname, &tstatus); |
| ffgky(*fptr, TDOUBLE, keyname, &dvalue, NULL, &tstatus); |
| if (tstatus) |
| { |
| dvalue = 1.0; |
| tstatus = 0; |
| } |
| else |
| { |
| |
| dvalue = (dvalue - amin[ii]) / binsize[ii] + .5; |
| } |
|
|
| ffkeyn("CRPIX", ii + 1, keyname, &tstatus); |
| ffpky(histptr, TDOUBLE, keyname, &dvalue, "Reference Pixel", &tstatus); |
|
|
| |
| ffkeyn("TCRVL", histData.hcolnum[ii], keyname, &tstatus); |
| ffgky(*fptr, TDOUBLE, keyname, &dvalue, NULL, &tstatus); |
| if (tstatus) |
| { |
| |
| dvalue = amin[ii] + binsize[ii]/2.; |
| tstatus = 0; |
| } |
|
|
| ffkeyn("CRVAL", ii + 1, keyname, &tstatus); |
| ffpky(histptr, TDOUBLE, keyname, &dvalue, "Reference Value", &tstatus); |
|
|
| |
| ffkeyn("TCDLT", histData.hcolnum[ii], keyname, &tstatus); |
| ffgky(*fptr, TDOUBLE, keyname, &dvalue, NULL, &tstatus); |
| if (tstatus) |
| { |
| dvalue = 1.0; |
| tstatus = 0; |
| } |
|
|
| dvalue = dvalue * binsize[ii]; |
| ffkeyn("CDELT", ii + 1, keyname, &tstatus); |
| ffpky(histptr, TDOUBLE, keyname, &dvalue, "Pixel size", &tstatus); |
|
|
| |
| |
| if (ii == 1) |
| { |
| ffkeyn("TCROT", histData.hcolnum[ii], keyname, &tstatus); |
| ffgky(*fptr, TDOUBLE, keyname, &dvalue, NULL, &tstatus); |
| if (!tstatus && dvalue != 0.) |
| { |
| ffkeyn("CROTA", ii + 1, keyname, &tstatus); |
| ffpky(histptr, TDOUBLE, keyname, &dvalue, |
| "Rotation angle", &tstatus); |
| } |
| else |
| { |
| |
| |
| tstatus = 0; |
| ffkeyn("TCROT", histData.hcolnum[0], keyname, &tstatus); |
| ffgky(*fptr, TDOUBLE, keyname, &dvalue, NULL, &tstatus); |
| if (!tstatus && dvalue != 0.) |
| { |
| dvalue *= -1.; |
| ffkeyn("CROTA", ii + 1, keyname, &tstatus); |
| ffpky(histptr, TDOUBLE, keyname, &dvalue, |
| "Rotation angle", &tstatus); |
| } |
| } |
| } |
| } |
|
|
| |
| |
| |
|
|
| if (histData.haxis == 2) { |
|
|
| |
| tstatus = 0; |
| ffkeyn("TP", histData.hcolnum[0], card, &tstatus); |
| strcat(card,"_"); |
| ffkeyn(card, histData.hcolnum[0], keyname, &tstatus); |
| ffgky(*fptr, TDOUBLE, keyname, &dvalue, card, &tstatus); |
| if (!tstatus) |
| ffpky(histptr, TDOUBLE, "PC1_1", &dvalue, card, &tstatus); |
|
|
| tstatus = 0; |
| keyname[1] = 'C'; |
| ffgky(*fptr, TDOUBLE, keyname, &dvalue, card, &tstatus); |
| if (!tstatus) { |
| dvalue *= binsize[0]; |
| ffpky(histptr, TDOUBLE, "CD1_1", &dvalue, card, &tstatus); |
| } |
|
|
| |
| tstatus = 0; |
| ffkeyn("TP", histData.hcolnum[0], card, &tstatus); |
| strcat(card,"_"); |
| ffkeyn(card, histData.hcolnum[1], keyname, &tstatus); |
| ffgky(*fptr, TDOUBLE, keyname, &dvalue, card, &tstatus); |
| if (!tstatus) |
| ffpky(histptr, TDOUBLE, "PC1_2", &dvalue, card, &tstatus); |
| |
| tstatus = 0; |
| keyname[1] = 'C'; |
| ffgky(*fptr, TDOUBLE, keyname, &dvalue, card, &tstatus); |
| if (!tstatus) { |
| dvalue *= binsize[0]; |
| ffpky(histptr, TDOUBLE, "CD1_2", &dvalue, card, &tstatus); |
| } |
| |
| |
| tstatus = 0; |
| ffkeyn("TP", histData.hcolnum[1], card, &tstatus); |
| strcat(card,"_"); |
| ffkeyn(card, histData.hcolnum[0], keyname, &tstatus); |
| ffgky(*fptr, TDOUBLE, keyname, &dvalue, card, &tstatus); |
| if (!tstatus) |
| ffpky(histptr, TDOUBLE, "PC2_1", &dvalue, card, &tstatus); |
| |
| tstatus = 0; |
| keyname[1] = 'C'; |
| ffgky(*fptr, TDOUBLE, keyname, &dvalue, card, &tstatus); |
| if (!tstatus) { |
| dvalue *= binsize[1]; |
| ffpky(histptr, TDOUBLE, "CD2_1", &dvalue, card, &tstatus); |
| } |
| |
| |
| tstatus = 0; |
| ffkeyn("TP", histData.hcolnum[1], card, &tstatus); |
| strcat(card,"_"); |
| ffkeyn(card, histData.hcolnum[1], keyname, &tstatus); |
| ffgky(*fptr, TDOUBLE, keyname, &dvalue, card, &tstatus); |
| if (!tstatus) |
| ffpky(histptr, TDOUBLE, "PC2_2", &dvalue, card, &tstatus); |
| |
| tstatus = 0; |
| keyname[1] = 'C'; |
| ffgky(*fptr, TDOUBLE, keyname, &dvalue, card, &tstatus); |
| if (!tstatus) { |
| dvalue *= binsize[1]; |
| ffpky(histptr, TDOUBLE, "CD2_2", &dvalue, card, &tstatus); |
| } |
| } |
| |
| |
| ffclos(*fptr, status); |
| *fptr = histptr; |
|
|
| return(*status); |
| } |
| |
| |
| int fits_calc_binning( |
| fitsfile *fptr, |
| int naxis, |
| char colname[4][FLEN_VALUE], |
| double *minin, |
| double *maxin, |
| double *binsizein, |
| char minname[4][FLEN_VALUE], |
| char maxname[4][FLEN_VALUE], |
| char binname[4][FLEN_VALUE], |
| |
| |
| |
| int *colnum, |
| long *haxes, |
| float *amin, |
| float *amax, |
| float *binsize, |
| int *status) |
| { |
| double amind[4], amaxd[4], binsized[4]; |
|
|
| fits_calc_binningd(fptr, naxis, colname, minin, maxin, binsizein, minname, maxname, binname, |
| colnum, haxes, amind, amaxd, binsized, status); |
|
|
| |
| if (*status == 0) { |
| int i, naxis1 = 4; |
| if (naxis < naxis1) naxis1 = naxis; |
| for (i=0; i<naxis1; i++) { |
| amin[i] = (float) amind[i]; |
| amax[i] = (float) amaxd[i]; |
| binsize[i] = (float) binsized[i]; |
| } |
| } |
|
|
| return (*status); |
| } |
|
|
| |
| int fits_calc_binningd( |
| fitsfile *fptr, |
| int naxis, |
| char colname[4][FLEN_VALUE], |
| double *minin, |
| double *maxin, |
| double *binsizein, |
| char minname[4][FLEN_VALUE], |
| char maxname[4][FLEN_VALUE], |
| char binname[4][FLEN_VALUE], |
| |
| |
| |
| int *colnum, |
| long *haxes, |
| double *amin, |
| double *amax, |
| double *binsize, |
| int *status) |
| |
| |
| |
| |
| { |
| tcolumn *colptr; |
| char *cptr, cpref[4][FLEN_VALUE]; |
| char errmsg[FLEN_ERRMSG], keyname[FLEN_KEYWORD]; |
| int tstatus, ii; |
| int datatype, repeat, imin, imax, ibin, use_datamax = 0; |
| double datamin, datamax; |
|
|
| |
| |
| if (*status > 0) |
| return(*status); |
|
|
| if (naxis > 4) |
| { |
| ffpmsg("histograms with more than 4 dimensions are not supported"); |
| return(*status = BAD_DIMEN); |
| } |
|
|
| |
| if ((fptr)->HDUposition != ((fptr)->Fptr)->curhdu) |
| ffmahd(fptr, ((fptr)->HDUposition) + 1, NULL, status); |
| |
| |
| |
| |
|
|
| *cpref[0] = '\0'; |
| *cpref[1] = '\0'; |
| *cpref[2] = '\0'; |
| *cpref[3] = '\0'; |
|
|
| tstatus = 0; |
| ffgky(fptr, TSTRING, "CPREF", cpref[0], NULL, &tstatus); |
|
|
| if (!tstatus) |
| { |
| |
| cptr = cpref[0]; |
|
|
| |
| while (*cptr != ',' && *cptr != '\0') |
| cptr++; |
|
|
| if (*cptr != '\0') |
| { |
| *cptr = '\0'; |
| cptr++; |
| while (*cptr == ' ') |
| cptr++; |
|
|
| strcpy(cpref[1], cptr); |
| cptr = cpref[1]; |
|
|
| |
| while (*cptr != ',' && *cptr != '\0') |
| cptr++; |
|
|
| if (*cptr != '\0') |
| { |
| *cptr = '\0'; |
| cptr++; |
| while (*cptr == ' ') |
| cptr++; |
|
|
| strcpy(cpref[2], cptr); |
| cptr = cpref[2]; |
|
|
| |
| while (*cptr != ',' && *cptr != '\0') |
| cptr++; |
|
|
| if (*cptr != '\0') |
| { |
| *cptr = '\0'; |
| cptr++; |
| while (*cptr == ' ') |
| cptr++; |
|
|
| strcpy(cpref[3], cptr); |
|
|
| } |
| } |
| } |
| } |
|
|
| |
| |
|
|
| for (ii = 0; ii < naxis; ii++) |
| { |
|
|
| |
| |
| |
| |
| |
| |
| |
| if (*colname[ii] == '\0') |
| { |
| strcpy(colname[ii], cpref[ii]); |
| if (*colname[ii] == '\0') |
| { |
| if (ii == 0) |
| strcpy(colname[ii], "X"); |
| else if (ii == 1) |
| strcpy(colname[ii], "Y"); |
| else if (ii == 2) |
| strcpy(colname[ii], "Z"); |
| else if (ii == 3) |
| strcpy(colname[ii], "T"); |
| } |
| } |
|
|
| |
| if (ffgcno(fptr, CASEINSEN, colname[ii], colnum+ii, status) |
| > 0) |
| { |
| strcpy(errmsg, "column for histogram axis doesn't exist: "); |
| strncat(errmsg, colname[ii],FLEN_ERRMSG-strlen(errmsg)-1); |
| ffpmsg(errmsg); |
| return(*status); |
| } |
|
|
| |
| |
|
|
| colptr = ((fptr)->Fptr)->tableptr; |
| colptr += (colnum[ii] - 1); |
|
|
| repeat = (int) colptr->trepeat; |
| if (repeat > 1) |
| { |
| strcpy(errmsg, "Can't bin a vector column: "); |
| strncat(errmsg, colname[ii],FLEN_ERRMSG-strlen(errmsg)-1); |
| ffpmsg(errmsg); |
| return(*status = BAD_DATATYPE); |
| } |
|
|
| |
| fits_get_coltype(fptr, colnum[ii], &datatype, |
| NULL, NULL, status); |
|
|
| if (datatype < 0 || datatype == TSTRING) |
| { |
| strcpy(errmsg, "Inappropriate datatype; can't bin this column: "); |
| strncat(errmsg, colname[ii],FLEN_ERRMSG-strlen(errmsg)-1); |
| ffpmsg(errmsg); |
| return(*status = BAD_DATATYPE); |
| } |
|
|
| |
| |
|
|
| datamin = DOUBLENULLVALUE; |
| datamax = DOUBLENULLVALUE; |
| |
| if (*minname[ii]) |
| { |
| if (ffgky(fptr, TDOUBLE, minname[ii], &minin[ii], NULL, status) ) |
| { |
| ffpmsg("error reading histogramming minimum keyword"); |
| ffpmsg(minname[ii]); |
| return(*status); |
| } |
| } |
|
|
| if (minin[ii] != DOUBLENULLVALUE) |
| { |
| amin[ii] = (double) minin[ii]; |
| } |
| else |
| { |
| ffkeyn("TLMIN", colnum[ii], keyname, status); |
| if (ffgky(fptr, TDOUBLE, keyname, amin+ii, NULL, status) > 0) |
| { |
| |
| *status = 0; |
| if (fits_get_col_minmax(fptr, colnum[ii], amin+ii, &datamax, status) > 0) |
| { |
| strcpy(errmsg, "Error calculating datamin and datamax for column: "); |
| strncat(errmsg, colname[ii],FLEN_ERRMSG-strlen(errmsg)-1); |
| ffpmsg(errmsg); |
| return(*status); |
| } |
| } |
| } |
|
|
| |
| |
|
|
| if (*maxname[ii]) |
| { |
| if (ffgky(fptr, TDOUBLE, maxname[ii], &maxin[ii], NULL, status) ) |
| { |
| ffpmsg("error reading histogramming maximum keyword"); |
| ffpmsg(maxname[ii]); |
| return(*status); |
| } |
| } |
|
|
| if (maxin[ii] != DOUBLENULLVALUE) |
| { |
| amax[ii] = (double) maxin[ii]; |
| } |
| else |
| { |
| ffkeyn("TLMAX", colnum[ii], keyname, status); |
| if (ffgky(fptr, TDOUBLE, keyname, &amax[ii], NULL, status) > 0) |
| { |
| *status = 0; |
| if(datamax != DOUBLENULLVALUE) |
| { |
| amax[ii] = datamax; |
| } |
| else |
| { |
| |
| if (fits_get_col_minmax(fptr, colnum[ii], &datamin, &amax[ii], status) > 0) |
| { |
| strcpy(errmsg, "Error calculating datamin and datamax for column: "); |
| strncat(errmsg, colname[ii],FLEN_ERRMSG-strlen(errmsg)-1); |
| ffpmsg(errmsg); |
| return(*status); |
| } |
| } |
| } |
| use_datamax = 1; |
| |
| } |
|
|
|
|
| |
| |
|
|
| if (*binname[ii]) |
| { |
| if (ffgky(fptr, TDOUBLE, binname[ii], &binsizein[ii], NULL, status) ) |
| { |
| ffpmsg("error reading histogramming binsize keyword"); |
| ffpmsg(binname[ii]); |
| return(*status); |
| } |
| } |
|
|
| if (binsizein[ii] == 0.) |
| { |
| ffpmsg("error: histogram binsize = 0"); |
| return(*status = ZERO_SCALE); |
| } |
|
|
| |
| if (binsizein[ii] != DOUBLENULLVALUE) |
| { |
| binsize[ii] = (double) binsizein[ii]; |
| } |
| else |
| { |
| tstatus = 0; |
| ffkeyn("TDBIN", colnum[ii], keyname, &tstatus); |
|
|
| if (ffgky(fptr, TDOUBLE, keyname, binsizein + ii, NULL, &tstatus) > 0) |
| { |
| |
| binsize[ii] = (amax[ii] - amin[ii]) / 10.F ; |
| if (binsize[ii] > 1.) |
| binsize[ii] = 1.; |
| } |
| } |
|
|
| |
| |
| if ( (amin[ii] > amax[ii] && binsize[ii] > 0. ) || |
| (amin[ii] < amax[ii] && binsize[ii] < 0. ) ) |
| binsize[ii] = -binsize[ii]; |
|
|
|
|
| ibin = (int) binsize[ii]; |
| imin = (int) amin[ii]; |
| imax = (int) amax[ii]; |
|
|
| |
| |
| |
|
|
| if (datatype <= TLONG && (double) imin == amin[ii] && |
| (double) imax == amax[ii] && |
| (double) ibin == binsize[ii] ) |
| { |
| |
| |
| |
|
|
| haxes[ii] = (imax - imin) / ibin + 1; |
| |
| if (amin[ii] < amax[ii]) |
| { |
| amin[ii] = (double) (amin[ii] - 0.5); |
| amax[ii] = (double) (amax[ii] + 0.5); |
| } |
| else |
| { |
| amin[ii] = (double) (amin[ii] + 0.5); |
| amax[ii] = (double) (amax[ii] - 0.5); |
| } |
| } |
| else if (use_datamax) |
| { |
| |
| |
| |
| |
| |
|
|
| haxes[ii] = (long) (((amax[ii] - amin[ii]) / binsize[ii]) + 1.); |
| } |
| else |
| { |
| |
| |
| |
| haxes[ii] = (long) ((amax[ii] - amin[ii]) / binsize[ii]); |
|
|
| if (amin[ii] < amax[ii]) |
| { |
| if (amin[ii] + (haxes[ii] * binsize[ii]) < amax[ii]) |
| haxes[ii]++; |
| } |
| else |
| { |
| if (amin[ii] + (haxes[ii] * binsize[ii]) > amax[ii]) |
| haxes[ii]++; |
| } |
| } |
| } |
|
|
| return(*status); |
| } |
| |
| int fits_write_keys_histo( |
| fitsfile *fptr, |
| fitsfile *histptr, |
| int naxis, |
| int *colnum, |
| int *status) |
| { |
| |
| |
|
|
| int ii, tstatus; |
| char keyname[FLEN_KEYWORD], svalue[FLEN_VALUE]; |
| double dvalue; |
| |
| if (*status > 0) |
| return(*status); |
|
|
| for (ii = 0; ii < naxis; ii++) |
| { |
| |
| tstatus = 0; |
| ffkeyn("CTYPE", ii+1, keyname, &tstatus); |
| ffgky(histptr, TSTRING, keyname, svalue, NULL, &tstatus); |
| |
| if (!tstatus) continue; |
| |
| |
| tstatus = 0; |
| ffkeyn("TTYPE", colnum[ii], keyname, &tstatus); |
| ffgky(fptr, TSTRING, keyname, svalue, NULL, &tstatus); |
|
|
| if (!tstatus) |
| { |
| ffkeyn("CTYPE", ii + 1, keyname, &tstatus); |
| ffpky(histptr, TSTRING, keyname, svalue, "Coordinate Type", &tstatus); |
| } |
|
|
| |
| tstatus = 0; |
| ffkeyn("TUNIT", colnum[ii], keyname, &tstatus); |
| ffgky(fptr, TSTRING, keyname, svalue, NULL, &tstatus); |
|
|
| if (!tstatus) |
| { |
| ffkeyn("CUNIT", ii + 1, keyname, &tstatus); |
| ffpky(histptr, TSTRING, keyname, svalue, "Coordinate Units", &tstatus); |
| } |
|
|
| |
| dvalue = 1.0; |
| tstatus = 0; |
| ffkeyn("CRPIX", ii + 1, keyname, &tstatus); |
| ffpky(histptr, TDOUBLE, keyname, &dvalue, "Reference Pixel", &tstatus); |
|
|
| |
| dvalue = 1.0; |
| tstatus = 0; |
| ffkeyn("CRVAL", ii + 1, keyname, &tstatus); |
| ffpky(histptr, TDOUBLE, keyname, &dvalue, "Reference Value", &tstatus); |
|
|
| |
| dvalue = 1.0; |
| tstatus = 0; |
| dvalue = 1.; |
| ffkeyn("CDELT", ii + 1, keyname, &tstatus); |
| ffpky(histptr, TDOUBLE, keyname, &dvalue, "Pixel size", &tstatus); |
|
|
| } |
| return(*status); |
| } |
| |
| int fits_rebin_wcs( |
| fitsfile *fptr, |
| int naxis, |
| float *amin, |
| float *binsize, |
| int *status) |
| { |
| double amind[4], binsized[4]; |
|
|
| |
| if (*status == 0) { |
| int i, naxis1 = 4; |
| if (naxis < naxis1) naxis1 = naxis; |
| for (i=0; i<naxis1; i++) { |
| amind[i] = (double) amin[i]; |
| binsized[i] = (double) binsize[i]; |
| } |
|
|
| fits_rebin_wcsd(fptr, naxis, amind, binsized, status); |
| } |
|
|
|
|
| return (*status); |
| } |
|
|
| |
| int fits_rebin_wcsd( |
| fitsfile *fptr, |
| int naxis, |
| double *amin, |
| double *binsize, |
| int *status) |
| { |
| |
| |
|
|
| int ii, jj, tstatus, reset ; |
| char keyname[FLEN_KEYWORD], svalue[FLEN_VALUE]; |
| double dvalue; |
| |
| if (*status > 0) |
| return(*status); |
| |
| for (ii = 0; ii < naxis; ii++) |
| { |
| reset = 0; |
| tstatus = 0; |
| ffkeyn("CRVAL", ii + 1, keyname, &tstatus); |
| |
| ffgky(fptr, TDOUBLE, keyname, &dvalue, NULL, &tstatus); |
| if (!tstatus && dvalue == 1.0) |
| reset = 1; |
|
|
| tstatus = 0; |
| |
| ffkeyn("CRPIX", ii + 1, keyname, &tstatus); |
|
|
| |
| ffgky(fptr, TDOUBLE, keyname, &dvalue, NULL, &tstatus); |
|
|
| if (!tstatus) |
| { |
| if (dvalue != 1.0) |
| reset = 0; |
|
|
| |
| dvalue = (dvalue - amin[ii]) / ((double) binsize[ii]) + .5; |
|
|
| fits_modify_key_dbl(fptr, keyname, dvalue, -14, NULL, &tstatus); |
| } else { |
| reset = 0; |
| } |
|
|
| |
| tstatus = 0; |
| ffkeyn("CDELT", ii + 1, keyname, &tstatus); |
|
|
| |
| ffgky(fptr, TDOUBLE, keyname, &dvalue, NULL, &tstatus); |
|
|
| if (!tstatus) |
| { |
| if (dvalue != 1.0) |
| reset = 0; |
|
|
| |
| dvalue = dvalue * binsize[ii]; |
|
|
| fits_modify_key_dbl(fptr, keyname, dvalue, -14, NULL, &tstatus); |
| } |
| else |
| { |
| reset = 0; |
|
|
| for (jj = 0; jj < naxis; jj++) |
| { |
| tstatus = 0; |
| ffkeyn("CD", jj + 1, svalue, &tstatus); |
| strcat(svalue,"_"); |
| ffkeyn(svalue, ii + 1, keyname, &tstatus); |
|
|
| |
| ffgky(fptr, TDOUBLE, keyname, &dvalue, NULL, &tstatus); |
|
|
| if (!tstatus) |
| { |
| |
| dvalue = dvalue * binsize[ii]; |
|
|
| fits_modify_key_dbl(fptr, keyname, dvalue, -14, NULL, &tstatus); |
| } |
| } |
| } |
|
|
| if (reset) { |
| |
| |
| |
| |
| dvalue = 1.0; |
| ffkeyn("CRPIX", ii + 1, keyname, &tstatus); |
| fits_modify_key_dbl(fptr, keyname, dvalue, -14, NULL, &tstatus); |
|
|
| ffkeyn("CRVAL", ii + 1, keyname, &tstatus); |
| dvalue = amin[ii] + (binsize[ii] / 2.0); |
| fits_modify_key_dbl(fptr, keyname, dvalue, -14, NULL, &tstatus); |
| } |
|
|
| } |
| return(*status); |
| } |
| |
| |
| int fits_make_hist(fitsfile *fptr, |
| fitsfile *histptr, |
| int bitpix, |
| int naxis, |
| long *naxes, |
| int *colnum, |
| float *amin, |
| float *amax, |
| float *binsize, |
| float weight, |
| int wtcolnum, |
| int recip, |
| char *selectrow, |
| |
| |
| |
| |
| |
| int *status) |
| { |
| double amind[4], amaxd[4], binsized[4], weightd; |
|
|
| |
| if (*status == 0) { |
| int i, naxis1 = 4; |
| if (naxis < naxis1) naxis1 = naxis; |
| for (i=0; i<naxis1; i++) { |
| amind[i] = (double) amin[i]; |
| amaxd[i] = (double) amax[i]; |
| binsized[i] = (double) binsize[i]; |
| } |
|
|
| weightd = (double) weight; |
|
|
| fits_make_histd(fptr, histptr, bitpix, naxis, naxes, colnum, |
| amind, amaxd, binsized, weight, wtcolnum, recip, |
| selectrow, status); |
| } |
|
|
| return (*status); |
| } |
|
|
| |
| int fits_make_histd(fitsfile *fptr, |
| fitsfile *histptr, |
| int bitpix, |
| int naxis, |
| long *naxes, |
| int *colnum, |
| double *amin, |
| double *amax, |
| double *binsize, |
| double weight, |
| int wtcolnum, |
| int recip, |
| char *selectrow, |
| |
| |
| |
| |
| |
| int *status) |
| { |
| int ii, imagetype, datatype; |
| int n_cols = 1; |
| long imin, imax, ibin; |
| long offset = 0; |
| long n_per_loop = -1; |
| double taxes[4], tmin[4], tmax[4], tbin[4], maxbin[4]; |
| histType histData; |
| iteratorCol imagepars[1]; |
|
|
| |
| |
| if (*status > 0) |
| return(*status); |
|
|
| if (naxis > 4) |
| { |
| ffpmsg("histogram has more than 4 dimensions"); |
| return(*status = BAD_DIMEN); |
| } |
|
|
| if (bitpix == BYTE_IMG) |
| imagetype = TBYTE; |
| else if (bitpix == SHORT_IMG) |
| imagetype = TSHORT; |
| else if (bitpix == LONG_IMG) |
| imagetype = TINT; |
| else if (bitpix == FLOAT_IMG) |
| imagetype = TFLOAT; |
| else if (bitpix == DOUBLE_IMG) |
| imagetype = TDOUBLE; |
| else |
| return(*status = BAD_DATATYPE); |
|
|
| |
| if ((fptr)->HDUposition != ((fptr)->Fptr)->curhdu) |
| ffmahd(fptr, ((fptr)->HDUposition) + 1, NULL, status); |
|
|
| histData.weight = weight; |
| histData.wtcolnum = wtcolnum; |
| histData.wtrecip = recip; |
| histData.tblptr = fptr; |
| histData.himagetype = imagetype; |
| histData.haxis = naxis; |
| histData.rowselector = selectrow; |
|
|
| for (ii = 0; ii < naxis; ii++) |
| { |
| taxes[ii] = (double) naxes[ii]; |
| tmin[ii] = amin[ii]; |
| tmax[ii] = amax[ii]; |
| if ( (amin[ii] > amax[ii] && binsize[ii] > 0. ) || |
| (amin[ii] < amax[ii] && binsize[ii] < 0. ) ) |
| tbin[ii] = -binsize[ii]; |
| else |
| tbin[ii] = binsize[ii]; |
| |
| imin = (long) tmin[ii]; |
| imax = (long) tmax[ii]; |
| ibin = (long) tbin[ii]; |
| |
| |
| fits_get_coltype(fptr, colnum[ii], &datatype, NULL, NULL, status); |
|
|
| if (datatype <= TLONG && (double) imin == tmin[ii] && |
| (double) imax == tmax[ii] && |
| (double) ibin == tbin[ii] ) |
| { |
| |
| |
| |
|
|
| maxbin[ii] = (taxes[ii] + 1.F); |
|
|
| if (tmin[ii] < tmax[ii]) |
| { |
| tmin[ii] = tmin[ii] - 0.5F; |
| tmax[ii] = tmax[ii] + 0.5F; |
| } |
| else |
| { |
| tmin[ii] = tmin[ii] + 0.5F; |
| tmax[ii] = tmax[ii] - 0.5F; |
| } |
| } else { |
| maxbin[ii] = (tmax[ii] - tmin[ii]) / tbin[ii]; |
| } |
| } |
|
|
| |
| |
| |
|
|
| histData.hcolnum[0] = colnum[0]; |
| histData.amin1 = tmin[0]; |
| histData.maxbin1 = maxbin[0]; |
| histData.binsize1 = tbin[0]; |
| histData.haxis1 = (long) taxes[0]; |
|
|
| if (histData.haxis > 1) |
| { |
| histData.hcolnum[1] = colnum[1]; |
| histData.amin2 = tmin[1]; |
| histData.maxbin2 = maxbin[1]; |
| histData.binsize2 = tbin[1]; |
| histData.haxis2 = (long) taxes[1]; |
|
|
| if (histData.haxis > 2) |
| { |
| histData.hcolnum[2] = colnum[2]; |
| histData.amin3 = tmin[2]; |
| histData.maxbin3 = maxbin[2]; |
| histData.binsize3 = tbin[2]; |
| histData.haxis3 = (long) taxes[2]; |
|
|
| if (histData.haxis > 3) |
| { |
| histData.hcolnum[3] = colnum[3]; |
| histData.amin4 = tmin[3]; |
| histData.maxbin4 = maxbin[3]; |
| histData.binsize4 = tbin[3]; |
| histData.haxis4 = (long) taxes[3]; |
| } |
| } |
| } |
|
|
| |
| fits_iter_set_file(imagepars, histptr); |
| fits_iter_set_datatype(imagepars, imagetype); |
| fits_iter_set_iotype(imagepars, OutputCol); |
|
|
| |
| fits_iterate_data(n_cols, imagepars, offset, n_per_loop, |
| ffwritehisto, (void*)&histData, status); |
| |
| return(*status); |
| } |
| |
| int fits_get_col_minmax(fitsfile *fptr, int colnum, double *datamin, |
| double *datamax, int *status) |
| |
| |
| |
| { |
| int anynul; |
| long nrows, ntodo, firstrow, ii; |
| double array[1000], nulval; |
|
|
| ffgky(fptr, TLONG, "NAXIS2", &nrows, NULL, status); |
|
|
| firstrow = 1; |
| nulval = DOUBLENULLVALUE; |
| *datamin = 9.0E36; |
| *datamax = -9.0E36; |
|
|
| while(nrows) |
| { |
| ntodo = minvalue(nrows, 100); |
| ffgcv(fptr, TDOUBLE, colnum, firstrow, 1, ntodo, &nulval, array, |
| &anynul, status); |
|
|
| for (ii = 0; ii < ntodo; ii++) |
| { |
| if (array[ii] != nulval) |
| { |
| *datamin = minvalue(*datamin, array[ii]); |
| *datamax = maxvalue(*datamax, array[ii]); |
| } |
| } |
|
|
| nrows -= ntodo; |
| firstrow += ntodo; |
| } |
| return(*status); |
| } |
| |
| int ffwritehisto(long totaln, long pixoffset, long firstn, long nvalues, |
| int narrays, iteratorCol *imagepars, void *userPointer) |
| |
| |
| |
| |
| |
| { |
| iteratorCol colpars[5]; |
| int ii, status = 0, ncols; |
| long rows_per_loop = 0, offset = 0; |
| histType *histData; |
|
|
| histData = (histType *)userPointer; |
|
|
| |
|
|
| switch( histData->himagetype ) { |
| case TBYTE: |
| histData->hist.b = (char * ) fits_iter_get_array(imagepars); |
| break; |
| case TSHORT: |
| histData->hist.i = (short * ) fits_iter_get_array(imagepars); |
| break; |
| case TINT: |
| histData->hist.j = (int * ) fits_iter_get_array(imagepars); |
| break; |
| case TFLOAT: |
| histData->hist.r = (float * ) fits_iter_get_array(imagepars); |
| break; |
| case TDOUBLE: |
| histData->hist.d = (double *) fits_iter_get_array(imagepars); |
| break; |
| } |
|
|
| |
| for (ii = 0; ii < histData->haxis; ii++) |
| { |
| fits_iter_set_by_num(&colpars[ii], histData->tblptr, |
| histData->hcolnum[ii], TDOUBLE, InputCol); |
| } |
| ncols = histData->haxis; |
|
|
| if (histData->weight == DOUBLENULLVALUE) |
| { |
| fits_iter_set_by_num(&colpars[histData->haxis], histData->tblptr, |
| histData->wtcolnum, TDOUBLE, InputCol); |
| ncols = histData->haxis + 1; |
| } |
|
|
| |
|
|
| |
| |
| |
| FFLOCK; |
| fits_iterate_data(ncols, colpars, offset, rows_per_loop, |
| ffcalchist, (void*)histData, &status); |
| FFUNLOCK; |
|
|
| return(status); |
| } |
| |
| int ffcalchist(long totalrows, long offset, long firstrow, long nrows, |
| int ncols, iteratorCol *colpars, void *userPointer) |
| |
| |
| |
| { |
| long ii, ipix, iaxisbin; |
| double pix, axisbin; |
| static double *col1, *col2, *col3, *col4; |
| static double *wtcol; |
| static long incr2, incr3, incr4; |
| static histType histData; |
| static char *rowselect; |
|
|
| |
| if (firstrow == 1) |
| { |
|
|
| |
| |
|
|
| histData = *(histType*)userPointer; |
| rowselect = histData.rowselector; |
|
|
| |
| col1 = (double *) fits_iter_get_array(&colpars[0]); |
| if (histData.haxis > 1) |
| { |
| col2 = (double *) fits_iter_get_array(&colpars[1]); |
| incr2 = histData.haxis1; |
|
|
| if (histData.haxis > 2) |
| { |
| col3 = (double *) fits_iter_get_array(&colpars[2]); |
| incr3 = incr2 * histData.haxis2; |
|
|
| if (histData.haxis > 3) |
| { |
| col4 = (double *) fits_iter_get_array(&colpars[3]); |
| incr4 = incr3 * histData.haxis3; |
| } |
| } |
| } |
|
|
| if (ncols > histData.haxis) |
| { |
| wtcol = (double *) fits_iter_get_array(&colpars[histData.haxis]); |
| } |
| } |
|
|
| |
| for (ii = 1; ii <= nrows; ii++) |
| { |
| if (rowselect) |
| { |
| if (*rowselect) |
| { |
| rowselect++; |
| } |
| else |
| { |
| rowselect++; |
| continue; |
| } |
| } |
|
|
| if (col1[ii] == DOUBLENULLVALUE) |
| continue; |
|
|
| pix = (col1[ii] - histData.amin1) / histData.binsize1; |
| ipix = (long) (pix + 1.); |
|
|
| |
| if (ipix < 1 || ipix > histData.haxis1 || pix > histData.maxbin1) |
| continue; |
|
|
| if (histData.haxis > 1) |
| { |
| if (col2[ii] == DOUBLENULLVALUE) |
| continue; |
|
|
| axisbin = (col2[ii] - histData.amin2) / histData.binsize2; |
| iaxisbin = (long) axisbin; |
|
|
| if (axisbin < 0. || iaxisbin >= histData.haxis2 || axisbin > histData.maxbin2) |
| continue; |
|
|
| ipix += (iaxisbin * incr2); |
|
|
| if (histData.haxis > 2) |
| { |
| if (col3[ii] == DOUBLENULLVALUE) |
| continue; |
|
|
| axisbin = (col3[ii] - histData.amin3) / histData.binsize3; |
| iaxisbin = (long) axisbin; |
| if (axisbin < 0. || iaxisbin >= histData.haxis3 || axisbin > histData.maxbin3) |
| continue; |
|
|
| ipix += (iaxisbin * incr3); |
| |
| if (histData.haxis > 3) |
| { |
| if (col4[ii] == DOUBLENULLVALUE) |
| continue; |
|
|
| axisbin = (col4[ii] - histData.amin4) / histData.binsize4; |
| iaxisbin = (long) axisbin; |
| if (axisbin < 0. || iaxisbin >= histData.haxis4 || axisbin > histData.maxbin4) |
| continue; |
|
|
| ipix += (iaxisbin * incr4); |
|
|
| } |
| } |
| } |
|
|
| |
| if (histData.weight != DOUBLENULLVALUE) |
| { |
| if (histData.himagetype == TINT) |
| histData.hist.j[ipix] += (int) histData.weight; |
| else if (histData.himagetype == TSHORT) |
| histData.hist.i[ipix] += (short) histData.weight; |
| else if (histData.himagetype == TFLOAT) |
| histData.hist.r[ipix] += histData.weight; |
| else if (histData.himagetype == TDOUBLE) |
| histData.hist.d[ipix] += histData.weight; |
| else if (histData.himagetype == TBYTE) |
| histData.hist.b[ipix] += (char) histData.weight; |
| } |
| else if (histData.wtrecip) |
| { |
| if (histData.himagetype == TINT) |
| histData.hist.j[ipix] += (int) (1./wtcol[ii]); |
| else if (histData.himagetype == TSHORT) |
| histData.hist.i[ipix] += (short) (1./wtcol[ii]); |
| else if (histData.himagetype == TFLOAT) |
| histData.hist.r[ipix] += (float) (1./wtcol[ii]); |
| else if (histData.himagetype == TDOUBLE) |
| histData.hist.d[ipix] += 1./wtcol[ii]; |
| else if (histData.himagetype == TBYTE) |
| histData.hist.b[ipix] += (char) (1./wtcol[ii]); |
| } |
| else |
| { |
| if (histData.himagetype == TINT) |
| histData.hist.j[ipix] += (int) wtcol[ii]; |
| else if (histData.himagetype == TSHORT) |
| histData.hist.i[ipix] += (short) wtcol[ii]; |
| else if (histData.himagetype == TFLOAT) |
| histData.hist.r[ipix] += wtcol[ii]; |
| else if (histData.himagetype == TDOUBLE) |
| histData.hist.d[ipix] += wtcol[ii]; |
| else if (histData.himagetype == TBYTE) |
| histData.hist.b[ipix] += (char) wtcol[ii]; |
| } |
|
|
| } |
|
|
| return(0); |
| } |
|
|
|
|