Changeset 28795
- Timestamp:
- Jul 30, 2010, 9:37:39 AM (16 years ago)
- Location:
- branches/eam_branches/ipp-20100621/psLib/src
- Files:
-
- 5 edited
-
fits/psFitsImage.c (modified) (1 diff)
-
fits/psFitsTable.c (modified) (19 diffs)
-
imageops/psImageCovariance.c (modified) (15 diffs)
-
imageops/psImageInterpolate.c (modified) (4 diffs)
-
math/psMinimizePowell.c (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branches/ipp-20100621/psLib/src/fits/psFitsImage.c
r27313 r28795 415 415 if (fits_read_subset(fits->fd, info->fitsDatatype, info->firstPixel, info->lastPixel, 416 416 info->increment, nullValue, output->data.V[0], &anynull, &status) != 0) { 417 psFitsError(status, true, "Reading FITS file failed.");417 psFitsError(status, true, "Reading FITS file %s failed.", fits->fd->Fptr->filename); 418 418 return false; 419 419 } -
branches/eam_branches/ipp-20100621/psLib/src/fits/psFitsTable.c
r28216 r28795 39 39 40 40 int status = 0; // CFITSIO status 41 42 // Check for empty table, which looks like an image 43 int hdutype; // Type of HDU 44 fits_get_hdu_type(fits->fd, &hdutype, &status); 45 if (psFitsError(status, true, "Could not determine the HDU type.")) { 46 return -1; 47 } 48 if (hdutype == IMAGE_HDU) { 49 // It could be an empty table 50 int naxis = 0; // Dimensions of image 51 if (fits_get_img_dim(fits->fd, &naxis, &status) != 0) { 52 psFitsError(status, true, "Unable to determine dimension for table."); 53 return -1; 54 } 55 if (naxis != 0) { 56 psFitsError(PS_ERR_BAD_FITS, true, "Current FITS HDU is not a table."); 57 return -1; 58 } 59 return 0; 60 } 61 41 62 long numRows; // Number of rows 42 63 if (fits_get_num_rows(fits->fd, &numRows, &status)) { … … 48 69 } 49 70 71 // Check if the FITS file is an empty table 72 static bool emptyTableCheck(const psFits *fits // FITS file 73 ) 74 { 75 PS_ASSERT_FITS_NON_NULL(fits, false); 76 77 if (psFitsGetExtNum(fits) == 0 && !psFitsMoveExtNum(fits, 1, false)) { 78 psError(PS_ERR_IO, false, "Unable to move to first extension to read table."); 79 return false; 80 } 81 82 int status = 0; // CFITSIO status 83 int hdutype; // Type of HDU 84 fits_get_hdu_type(fits->fd, &hdutype, &status); 85 if (status) { 86 psFitsError(status, true, "Could not determine the HDU type."); 87 return false; 88 } 89 if (hdutype == IMAGE_HDU) { 90 // It could be an empty table 91 int naxis = 0; // Dimensions of image 92 if (fits_get_img_dim(fits->fd, &naxis, &status) != 0) { 93 psFitsError(status, true, "Unable to determine dimension for table."); 94 return false; 95 } 96 if (naxis != 0) { 97 psFitsError(PS_ERR_BAD_FITS, true, "Current FITS HDU is not a table."); 98 return false; 99 } 100 return true; 101 } 102 103 return false; 104 } 105 50 106 // Check the FITS file in preparation for reading a table 51 107 static bool readTableCheck(const psFits *fits // FITS file 52 108 ) 53 109 { 54 PS_ASSERT_FITS_NON_NULL(fits, NULL);110 PS_ASSERT_FITS_NON_NULL(fits, false); 55 111 56 112 if (psFitsGetExtNum(fits) == 0 && !psFitsMoveExtNum(fits, 1, false)) { … … 58 114 return false; 59 115 } 60 61 116 62 117 // check that we are positioned on a table HDU … … 64 119 int hdutype; // Type of HDU 65 120 fits_get_hdu_type(fits->fd, &hdutype, &status); 66 if (psFitsError(status, true, "Could not determine the HDU type.")) { 121 if (status) { 122 psFitsError(status, true, "Could not determine the HDU type."); 67 123 return false; 68 124 } 69 125 if (hdutype != ASCII_TBL && hdutype != BINARY_TBL) { 70 psError(PS_ERR_IO, true, _("Current FITS HDU is not a table."));71 126 return false; 72 127 } … … 82 137 83 138 if (!readTableCheck(fits)) { 139 if (emptyTableCheck(fits)) { 140 return psMetadataAlloc(); 141 } 142 psError(PS_ERR_BAD_FITS, true, "Extension is not a table"); 84 143 return NULL; 85 144 } … … 91 150 fits_get_num_rows(fits->fd, &numRows, &status); 92 151 fits_get_num_cols(fits->fd, &numCols, &status); 93 if (status != 0) {152 if (status) { 94 153 psFitsError(status, true, "Failed to determine the size of the current HDU table."); 95 154 return NULL; … … 204 263 205 264 if (!readTableCheck(fits)) { 265 if (emptyTableCheck(fits)) { 266 return psArrayAlloc(0); 267 } 268 psError(PS_ERR_BAD_FITS, true, "Extension is not a table"); 206 269 return NULL; 207 270 } … … 251 314 252 315 if (!readTableCheck(fits)) { 316 psError(PS_ERR_BAD_FITS, true, "Extension is not a table"); 253 317 return NULL; 254 318 } … … 295 359 296 360 if (!readTableCheck(fits)) { 361 if (emptyTableCheck(fits)) { 362 return psArrayAlloc(0); 363 } 364 psError(PS_ERR_BAD_FITS, true, "Extension is not a table"); 297 365 return NULL; 298 366 } … … 704 772 705 773 if (!readTableCheck(fits)) { 774 psError(PS_ERR_BAD_FITS, true, "Extension is not a table"); 706 775 return NULL; 707 776 } … … 753 822 754 823 if (!readTableCheck(fits)) { 824 if (emptyTableCheck(fits)) { 825 return psMetadataAlloc(); 826 } 827 psError(PS_ERR_BAD_FITS, true, "Extension is not a table"); 755 828 return NULL; 756 829 } … … 762 835 fits_get_num_rows(fits->fd, &numRows, &status); 763 836 fits_get_num_cols(fits->fd, &numCols, &status); 764 if (psFitsError(status, true, "Failed to determine the size of the current HDU table.")) { 837 if (status) { 838 psFitsError(status, true, "Failed to determine the size of the current HDU table."); 765 839 return NULL; 766 840 } … … 768 842 int hdutype; // Type of HDU: need to distinguish ASCII and binary tables 769 843 fits_get_hdu_type(fits->fd, &hdutype, &status); 770 if (psFitsError(status, true, "Could not determine the HDU type.")) { 844 if (status) { 845 psFitsError(status, true, "Could not determine the HDU type."); 771 846 return false; 772 847 } … … 786 861 long repeat; // Number of repeats 787 862 fits_get_eqcoltype(fits->fd, col, &cfitsioType, &repeat, NULL, &status); 788 if (psFitsError(status, true, "Could not determine the column data for %s", name)) { 863 if (status) { 864 psFitsError(status, true, "Could not determine the column data for %s", name); 789 865 psFree(table); 790 866 return false; … … 805 881 } 806 882 fits_read_col_str(fits->fd, col, 1, 1, numRows, "", (char**)array->data, NULL, &status); 807 if (psFitsError(status, true, "Failed to read column %s", name)) { 883 if (status) { 884 psFitsError(status, true, "Failed to read column %s", name); 808 885 psFree(array); 809 886 psFree(table); … … 822 899 fits_read_col(fits->fd, cfitsioType, col, 1, 1, numRows, NULL, 823 900 vector->data.U8, NULL, &status); 824 if (psFitsError(status, true, "Failed to read column %s", name)) { 901 if (status) { 902 psFitsError(status, true, "Failed to read column %s", name); 825 903 psFree(vector); 826 904 psFree(table); … … 933 1011 psFree(names); 934 1012 psFree(types); 935 if (psFitsError(status, true, "Unable to create FITS table with %d columns and %ld rows", 936 numCols, numRows)) { 1013 if (status) { 1014 psFitsError(status, true, "Unable to create FITS table with %d columns and %ld rows", 1015 numCols, numRows); 937 1016 psFree(columns); 938 1017 return false; … … 986 1065 } 987 1066 // Check error status from writing column 988 if (psFitsError(status, true, "Unable to write column %ld of FITS table", col)) { 1067 if (status) { 1068 psFitsError(status, true, "Unable to write column %ld of FITS table", col); 989 1069 psFree(columns); 990 1070 return false; … … 997 1077 // hurt. 998 1078 ffrdef(fits->fd, &status); 999 if (psFitsError(status, true, "Could not re-scan HDU.")) { 1079 if (status) { 1080 psFitsError(status, true, "Could not re-scan HDU."); 1000 1081 return false; 1001 1082 } -
branches/eam_branches/ipp-20100621/psLib/src/imageops/psImageCovariance.c
r28405 r28795 34 34 static float imageCovarianceCalculate(const psKernel *covar, // Original covariance matrix 35 35 const psKernel *kernel, // Convolution kernel 36 int x, int y // Coordinates in output covariance matrix 36 int x, int y, // Coordinates in output covariance matrix 37 float scale // Scale to apply 37 38 ) 38 39 { … … 83 84 } 84 85 85 return s um;86 return scale * sum; 86 87 } 87 88 … … 91 92 PS_ASSERT_THREAD_JOB_NON_NULL(job, false); 92 93 psAssert(job->args, "No job arguments"); 93 psAssert(job->args->n == 5, "Wrong number of job arguments: %ld", job->args->n);94 psAssert(job->args->n == 6, "Wrong number of job arguments: %ld", job->args->n); 94 95 95 96 psKernel *out = job->args->data[0]; // Output covariance matrix … … 98 99 int x = PS_SCALAR_VALUE(job->args->data[3], S32); // x coordinate in output covariance matrix 99 100 int y = PS_SCALAR_VALUE(job->args->data[4], S32); // y coordinate in output covariance matrix 100 101 out->kernel[y][x] = imageCovarianceCalculate(covar, kernel, x, y); 101 float scale = PS_SCALAR_VALUE(job->args->data[5], F32); // Scaling to apply 102 103 out->kernel[y][x] = imageCovarianceCalculate(covar, kernel, x, y, scale); 102 104 103 105 return true; … … 127 129 128 130 // Check for non-finite elements 131 double sumKernel = 0.0, sumKernel2 = 0.0; // Sum of the kernel 129 132 for (int y = kernel->yMin; y <= kernel->yMax; y++) { 130 133 for (int x = kernel->xMin; x <= kernel->xMax; x++) { … … 135 138 return NULL; 136 139 } 140 sumKernel += kernel->kernel[y][x]; 141 sumKernel2 += PS_SQR(kernel->kernel[y][x]); 137 142 } 138 143 } … … 155 160 int yMin = kernel->yMin - kernel->yMax + covar->yMin, yMax = kernel->yMax - kernel->yMin + covar->yMax; 156 161 psKernel *out = psKernelAlloc(xMin, xMax, yMin, yMax); // Covariance matrix for output 162 float scale = 1.0 / sumKernel2; // Scaling to apply 157 163 158 164 for (int y = yMin; y <= yMax; y++) { … … 165 171 PS_ARRAY_ADD_SCALAR(job->args, x, PS_TYPE_S32); 166 172 PS_ARRAY_ADD_SCALAR(job->args, y, PS_TYPE_S32); 173 PS_ARRAY_ADD_SCALAR(job->args, scale, PS_TYPE_F32); 167 174 if (!psThreadJobAddPending(job)) { 168 175 psFree(covar); … … 170 177 } 171 178 } else { 172 out->kernel[y][x] = imageCovarianceCalculate(covar, kernel, x, y); 173 } 174 } 175 } 176 psFree(covar); 179 out->kernel[y][x] = imageCovarianceCalculate(covar, kernel, x, y, scale); 180 } 181 } 182 } 177 183 178 184 if (threaded && !psThreadPoolWait(true)) { … … 180 186 return false; 181 187 } 188 189 psFree(covar); 182 190 183 191 return out; … … 194 202 195 203 // Check for non-finite elements 204 double sumKernel2 = 0.0; // Sum of the squared kernel 196 205 for (int y = kernel->yMin; y <= kernel->yMax; y++) { 197 206 for (int x = kernel->xMin; x <= kernel->xMax; x++) { … … 202 211 return NAN; 203 212 } 213 sumKernel2 += PS_SQR(kernel->kernel[y][x]); 204 214 } 205 215 } … … 215 225 } 216 226 217 float factor = imageCovarianceCalculate(covar, kernel, 0, 0); // Covariance factor 227 float scale = 1.0 / sumKernel2; // Scale to apply 228 float factor = imageCovarianceCalculate(covar, kernel, 0, 0, scale); // Covariance factor 218 229 psFree(covar); 219 230 return factor; … … 338 349 } 339 350 } 340 psFree(covar);341 342 351 if (threaded && !psThreadPoolWait(true)) { 343 352 psError(PS_ERR_UNKNOWN, false, "Error waiting for threads."); 344 353 return false; 345 354 } 355 psFree(covar); 346 356 347 357 return out; … … 616 626 if (set && !threaded) { 617 627 { 618 psThreadTask *task = psThreadTaskAlloc("PSLIB_IMAGE_COVARIANCE_CALCULATE", 5);628 psThreadTask *task = psThreadTaskAlloc("PSLIB_IMAGE_COVARIANCE_CALCULATE", 6); 619 629 task->function = &imageCovarianceCalculateThread; 620 630 psThreadTaskAdd(task); -
branches/eam_branches/ipp-20100621/psLib/src/imageops/psImageInterpolate.c
r26892 r28795 427 427 428 428 // Determine the result of the interpolation after all the math has been done 429 #define INTERPOLATE_RESULT() \ 430 psImageInterpolateStatus status = PS_INTERPOLATE_STATUS_ERROR; /* Status of interpolation */ \ 431 *imageValue = sumKernel > 0 ? sumImage / sumKernel : interp->badImage; \ 432 if (wantVariance) { \ 433 *varianceValue = sumVariance / (sumKernel2 - sumBad); \ 434 } \ 435 if (sumKernel == 0.0) { \ 436 /* No kernel contributions */ \ 437 if (haveMask && maskValue) { \ 438 *maskValue |= interp->badMask; \ 439 } \ 440 status = PS_INTERPOLATE_STATUS_BAD; \ 441 } else if (sumBad == 0) { \ 442 /* Completely good pixel */ \ 443 status = PS_INTERPOLATE_STATUS_GOOD; \ 444 } else if (sumBad < PS_SQR(interp->poorFrac) * sumKernel2) { \ 445 /* Some pixels masked: poor pixel */ \ 446 if (haveMask && maskValue) { \ 447 *maskValue |= interp->poorMask; \ 448 } \ 449 status = PS_INTERPOLATE_STATUS_POOR; \ 450 } else { \ 451 /* Many pixels (or a few important pixels) masked: bad pixel */ \ 452 if (haveMask && maskValue) { \ 453 *maskValue |= interp->badMask; \ 454 } \ 455 status = PS_INTERPOLATE_STATUS_BAD; \ 456 } 429 static psImageInterpolateStatus interpolateResult(const psImageInterpolation *interp, 430 double *imageValue, double *varianceValue, 431 psImageMaskType *maskValue, 432 double sumImage, double sumVariance, double sumBad, 433 double sumKernel, double sumKernel2, 434 bool wantVariance, bool haveMask) 435 { 436 *imageValue = sumKernel > 0 ? sumImage / sumKernel : interp->badImage; 437 if (wantVariance) { 438 if (sumBad > 0) { 439 sumVariance *= sumKernel2 / (sumKernel2 - sumBad); 440 } 441 *varianceValue = sumVariance / PS_SQR(sumKernel); 442 } 443 if (sumKernel == 0.0) { 444 // No kernel contributions at all 445 if (haveMask && maskValue) { 446 *maskValue |= interp->badMask; 447 } 448 return PS_INTERPOLATE_STATUS_BAD; 449 } 450 if (sumBad == 0) { 451 // Completely good pixel 452 return PS_INTERPOLATE_STATUS_GOOD; 453 } 454 if (sumBad < PS_SQR(interp->poorFrac) * sumKernel2) { 455 // Some pixels masked: poor pixel 456 if (haveMask && maskValue) { 457 *maskValue |= interp->poorMask; 458 } 459 return PS_INTERPOLATE_STATUS_POOR; 460 } 461 // Many pixels (or a few important pixels) masked: bad pixel 462 if (haveMask && maskValue) { 463 *maskValue |= interp->badMask; 464 } 465 return PS_INTERPOLATE_STATUS_BAD; 466 } 457 467 458 468 // Interpolation engine for separable interpolation kernels … … 703 713 } 704 714 705 INTERPOLATE_RESULT();706 707 715 psFree(xKernelNew); 708 716 psFree(yKernelNew); … … 710 718 psFree(yKernel2New); 711 719 712 return status; 720 return interpolateResult(interp, imageValue, varianceValue, maskValue, sumImage, sumVariance, sumBad, 721 sumKernel, sumKernel2, wantVariance, haveMask); 713 722 } 714 723 … … 861 870 } 862 871 863 INTERPOLATE_RESULT(); 864 865 return status; 872 return interpolateResult(interp, imageValue, varianceValue, maskValue, sumImage, sumVariance, sumBad, 873 sumKernel, sumKernel2, wantVariance, haveMask); 866 874 } 867 875 -
branches/eam_branches/ipp-20100621/psLib/src/math/psMinimizePowell.c
r28655 r28795 83 83 f(param + b * line) < f(param + c * line) 84 84 a < b < c 85 85 86 86 Algorithm: 87 87 88 88 XXX completely ad hoc: 89 89 start with the user-supplied starting parameter and 90 90 call that b. Calculate a/c as a fractional amount smaller/larger than b. 91 91 Repeat this process until a local minimum is found. 92 92 93 93 XXX: new algorithm: 94 94 start at x=0, expand in one direction until the function … … 96 96 increases, or x is too large. If thst does not work, expand in the other 97 97 direction. 98 98 99 99 XXX: output bracket vector should be an input as well. 100 100 *****************************************************************************/ … … 335 335 along that vector and returns the offset along that vector at which the 336 336 minimum is determined. 337 337 338 338 XXX: This routine is not very efficient in terms of total evaluations of the 339 339 function. … … 491 491 points at which the function is varied are in the argument "coords" which is 492 492 a psArray of psVectors: each vector represents a different coordinate. 493 493 494 494 XXX: We do not use Brent's method. 495 495 *****************************************************************************/ … … 709 709 This routine is to be used with the psMinimizeChi2Powell() function below. 710 710 and the psMinimizePowell() function above. 711 711 712 712 The basic idea is calculate chi-squared for a set of params/coords/errors. 713 713 This functions uses global variables to receive the function pointer, the … … 764 764 This routine must minimize the chi-squared match of a set of data points and 765 765 values for a possibly multi-dimensional function. 766 766 767 767 The basic idea is to use the psMinimizePowell() function defined above. In 768 768 order to do so, we defined above a function myPowellChi2Func() which takes … … 780 780 psMinimizeChi2PowellFunc model) 781 781 { 782 PS_ASSERT_VECTOR_NON_NULL(params, false); 783 PS_ASSERT_VECTOR_TYPE(params, PS_TYPE_F32, false); 784 PS_ASSERT_ARRAY_NON_NULL(coords, false); 785 PS_ASSERT_VECTOR_NON_NULL(value, false); 786 PS_ASSERT_VECTOR_TYPE(value, PS_TYPE_F32, false); 787 PS_ASSERT_VECTORS_SIZE_EQUAL(coords, value, false); 788 782 789 // Generate extended version of coords array, so we can pass in extra data to the chi^2 function 783 790 psArray *newCoords = psArrayAlloc(coords->n + 3); … … 791 798 newCoords->data[coords->n + 2] = &model; 792 799 793 bool success = psMinimizePowell(min, params, constraint->paramMask, newCoords, myPowellChi2Func); 800 bool success = psMinimizePowell(min, params, constraint ? constraint->paramMask : NULL, 801 newCoords, myPowellChi2Func); 794 802 795 803 newCoords->data[coords->n - 1] = NULL; // We can't free the array with a function pointer on it
Note:
See TracChangeset
for help on using the changeset viewer.
