IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Aug 9, 2007, 2:11:07 PM (19 years ago)
Author:
Paul Price
Message:

Adding support for doing subtraction in multiple regions with independent solutions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ppSub/src/ppSubReadout.c

    r14365 r14457  
    1010#include "ppSub.h"
    1111
    12 #define TESTING
     12//#define TESTING
    1313#define WCS_TOLERANCE 0.001             // Tolerance for WCS
    1414
     
    5252    int size = psMetadataLookupS32(NULL, config->arguments, "KERNEL.SIZE"); // Kernel half-size
    5353    int order = psMetadataLookupS32(NULL, config->arguments, "SPATIAL.ORDER"); // Spatial polynomial order
     54    float regionSize = psMetadataLookupF32(NULL, config->arguments, "REGION.SIZE"); // Size of iso-kernel regs
    5455    float spacing = psMetadataLookupF32(NULL, config->arguments, "STAMP.SPACING"); // Typical stamp spacing
    5556    int footprint = psMetadataLookupS32(NULL, config->arguments, "STAMP.FOOTPRINT"); // Stamp half-size
     
    119120    memCheck("kernels");
    120121
    121     int numRejected = -1;               // Number of rejected stamps in each iteration
    122     for (int i = 0; i < iter && numRejected != 0; i++) {
    123         stamps = pmSubtractionFindStamps(stamps, refRO->image, subMask, threshold, spacing);
    124         if (!stamps) {
    125             psError(PS_ERR_UNKNOWN, false, "Unable to find stamps on reference image.");
    126             goto ERROR;
    127         }
    128 
    129         memCheck("  find stamps");
    130 
    131         if (!pmSubtractionCalculateEquation(stamps, refRO->image, inRO->image, inRO->weight,
    132                                             kernels, footprint)) {
    133             psError(PS_ERR_UNKNOWN, false, "Unable to calculate least-squares equation.");
    134             goto ERROR;
    135          }
    136 
    137         memCheck("  calculate equation");
    138 
    139         solution = pmSubtractionSolveEquation(solution, stamps);
    140         if (!solution) {
    141             psError(PS_ERR_UNKNOWN, false, "Unable to calculate least-squares equation.");
    142             goto ERROR;
    143         }
    144 
    145         memCheck("  solve equation");
    146 
    147         numRejected = pmSubtractionRejectStamps(stamps, refRO->image, inRO->image, subMask,
    148                                                 solution, footprint, rej, kernels);
    149         if (numRejected < 0) {
    150             psError(PS_ERR_UNKNOWN, false, "Unable to reject stamps.");
    151             goto ERROR;
    152         }
    153         psLogMsg("ppSub", PS_LOG_INFO, "%d stamps rejected on iteration %d.", numRejected, i);
    154 
    155         memCheck("  reject stamps");
    156     }
    157 
    158     if (numRejected > 0) {
    159         solution = pmSubtractionSolveEquation(solution, stamps);
    160         if (!solution) {
    161             psError(PS_ERR_UNKNOWN, false, "Unable to calculate least-squares equation.");
    162             goto ERROR;
    163         }
    164     }
    165     psFree(stamps);
    166     stamps = NULL;
    167 
    168     memCheck("solution");
     122    int xRegions = 1, yRegions = 1;     // Number of iso-kernel regions
     123    float xRegionSize = 0, yRegionSize = 0; // Size of iso-kernel regions
     124    psRegion *region = NULL;            // Iso-kernel region
     125    if (isfinite(regionSize)) {
     126        xRegions = numCols / regionSize + 1;
     127        yRegions = numRows / regionSize + 1;
     128        xRegionSize = (float)numCols / (float)xRegions;
     129        yRegionSize = (float)numRows / (float)yRegions;
     130        region = psRegionAlloc(NAN, NAN, NAN, NAN);
     131    }
     132
     133    psImage *convImage = NULL, *convWeight = NULL, *convMask = NULL; // Convolved images
     134
     135    // Iterate over iso-kernel regions
     136    for (int j = 0; j < yRegions; j++) {
     137        for (int i = 0; i < xRegions; i++) {
     138            psTrace("ppSub", 1, "Subtracting region %d of %d...\n",
     139                    j * xRegions + i + 1, xRegions * yRegions);
     140            if (region) {
     141                *region = psRegionSet((int)(i * xRegionSize), (int)((i + 1) * xRegionSize),
     142                                      (int)(j * yRegionSize), (int)((j + 1) * yRegionSize));
     143                psString string = psRegionToString(*region);
     144                psTrace("ppSub", 3, "Iso-kernel region: %s out of %d,%d\n", string, numCols, numRows);
     145                psFree(string);
     146            }
     147
     148            int numRejected = -1;               // Number of rejected stamps in each iteration
     149            for (int k = 0; k < iter && numRejected != 0; k++) {
     150                psTrace("ppSub", 2, "Iteration %d...\n", k);
     151                psTrace("ppSub", 3, "Finding stamps...\n");
     152                stamps = pmSubtractionFindStamps(stamps, refRO->image, subMask, region, threshold, spacing);
     153                if (!stamps) {
     154                    psError(PS_ERR_UNKNOWN, false, "Unable to find stamps on reference image.");
     155                    goto ERROR;
     156                }
     157
     158                memCheck("  find stamps");
     159
     160                psTrace("ppSub", 3, "Calculating equation...\n");
     161                if (!pmSubtractionCalculateEquation(stamps, refRO->image, inRO->image, inRO->weight,
     162                                                    kernels, footprint)) {
     163                    psError(PS_ERR_UNKNOWN, false, "Unable to calculate least-squares equation.");
     164                    goto ERROR;
     165                }
     166
     167                memCheck("  calculate equation");
     168
     169                psTrace("ppSub", 3, "Solving equation...\n");
     170                solution = pmSubtractionSolveEquation(solution, stamps);
     171                if (!solution) {
     172                    psError(PS_ERR_UNKNOWN, false, "Unable to calculate least-squares equation.");
     173                    goto ERROR;
     174                }
     175
     176                memCheck("  solve equation");
     177
     178                psTrace("ppSub", 3, "Rejecting stamps...\n");
     179                numRejected = pmSubtractionRejectStamps(stamps, refRO->image, inRO->image, subMask,
     180                                                        solution, footprint, rej, kernels);
     181                if (numRejected < 0) {
     182                    psError(PS_ERR_UNKNOWN, false, "Unable to reject stamps.");
     183                    goto ERROR;
     184                }
     185                psLogMsg("ppSub", PS_LOG_INFO, "%d stamps rejected on iteration %d.", numRejected, k);
     186
     187                memCheck("  reject stamps");
     188            }
     189
     190            if (numRejected > 0) {
     191                psTrace("ppSub", 3, "Solving equation...\n");
     192                solution = pmSubtractionSolveEquation(solution, stamps);
     193                if (!solution) {
     194                    psError(PS_ERR_UNKNOWN, false, "Unable to calculate least-squares equation.");
     195                    goto ERROR;
     196                }
     197            }
     198            psFree(stamps);
     199            stamps = NULL;
     200
     201            memCheck("solution");
    169202
    170203#ifdef TESTING
    171     {
    172         // Generate image with convolution kernels
    173         int fullSize = 2 * size + 1 + 1;    // Full size of kernel
    174         psImage *convKernels = psImageAlloc(5 * fullSize - 1, 5 * fullSize - 1, PS_TYPE_F32);
    175         psImageInit(convKernels, NAN);
    176         for (int j = -2; j <= 2; j++) {
    177             for (int i = -2; i <= 2; i++) {
    178                 psImage *kernel = pmSubtractionKernelImage(solution, kernels, (float)i / 2.0,
    179                                                            (float)j / 2.0); // Image of the kernel
    180                 if (!kernel) {
    181                     psError(PS_ERR_UNKNOWN, false, "Unable to generate kernel image.");
    182                     psFree(convKernels);
    183                     goto ERROR;
    184                 }
    185 
    186                 if (psImageOverlaySection(convKernels, kernel, (i + 2) * fullSize,
    187                                           (j + 2) * fullSize, "=") == 0) {
    188                     psError(PS_ERR_UNKNOWN, false, "Unable to overlay kernel image.");
    189                     psFree(kernel);
    190                     psFree(convKernels);
    191                     goto ERROR;
    192                 }
    193                 psFree(kernel);
    194             }
    195         }
    196 
    197         // XXX What do we do with this image?
    198 
    199         psFits *kernelFile = psFitsOpen("kernel.fits", "w");
    200         (void)psFitsWriteImage(kernelFile, NULL, convKernels, 0, NULL);
    201         psFitsClose(kernelFile);
    202 
    203         psFree(convKernels);
    204     }
    205 
    206     {
    207         // Generate images of the kernel components
    208         psMetadata *header = psMetadataAlloc(); // Header
    209         for (int i = 0; i < solution->n; i++) {
    210             psString name = NULL;       // Header keyword
    211             psStringAppend(&name, "SOLN%04d", i);
    212             psMetadataAddF64(header, PS_LIST_TAIL, name, 0, NULL, solution->data.F64[i]);
    213             psFree(name);
    214         }
    215         psArray *kernelImages = pmSubtractionKernelSolutions(solution, kernels, 0.0, 0.0);
    216         psFits *kernelFile = psFitsOpen("kernels.fits", "w");
    217         (void)psFitsWriteImageCube(kernelFile, header, kernelImages, NULL);
    218         psFitsClose(kernelFile);
    219         psFree(kernelImages);
    220         psFree(header);
    221     }
     204            psTrace("ppSub", 2, "Generating diagnostics...\n");
     205            {
     206                // Generate image with convolution kernels
     207                int fullSize = 2 * size + 1 + 1;    // Full size of kernel
     208                psImage *convKernels = psImageAlloc(5 * fullSize - 1, 5 * fullSize - 1, PS_TYPE_F32);
     209                psImageInit(convKernels, NAN);
     210                for (int j = -2; j <= 2; j++) {
     211                    for (int i = -2; i <= 2; i++) {
     212                        psImage *kernel = pmSubtractionKernelImage(solution, kernels, (float)i / 2.0,
     213                                                                   (float)j / 2.0); // Image of the kernel
     214                        if (!kernel) {
     215                            psError(PS_ERR_UNKNOWN, false, "Unable to generate kernel image.");
     216                            psFree(convKernels);
     217                            goto ERROR;
     218                        }
     219
     220                        if (psImageOverlaySection(convKernels, kernel, (i + 2) * fullSize,
     221                                                  (j + 2) * fullSize, "=") == 0) {
     222                            psError(PS_ERR_UNKNOWN, false, "Unable to overlay kernel image.");
     223                            psFree(kernel);
     224                            psFree(convKernels);
     225                            goto ERROR;
     226                        }
     227                        psFree(kernel);
     228                    }
     229                }
     230
     231                // XXX What do we do with this image?
     232
     233                psFits *kernelFile = psFitsOpen("kernel.fits", "w");
     234                (void)psFitsWriteImage(kernelFile, NULL, convKernels, 0, NULL);
     235                psFitsClose(kernelFile);
     236
     237                psFree(convKernels);
     238            }
     239
     240            {
     241                // Generate images of the kernel components
     242                psMetadata *header = psMetadataAlloc(); // Header
     243                for (int i = 0; i < solution->n; i++) {
     244                    psString name = NULL;       // Header keyword
     245                    psStringAppend(&name, "SOLN%04d", i);
     246                    psMetadataAddF64(header, PS_LIST_TAIL, name, 0, NULL, solution->data.F64[i]);
     247                    psFree(name);
     248                }
     249                psArray *kernelImages = pmSubtractionKernelSolutions(solution, kernels, 0.0, 0.0);
     250                psFits *kernelFile = psFitsOpen("kernels.fits", "w");
     251                (void)psFitsWriteImageCube(kernelFile, header, kernelImages, NULL);
     252                psFitsClose(kernelFile);
     253                psFree(kernelImages);
     254                psFree(header);
     255            }
    222256#endif
    223257
    224     memCheck("diag outputs");
    225 
    226     psImage *convImage = NULL, *convWeight = NULL, *convMask = NULL; // Convolved images
    227     if (!pmSubtractionConvolve(&convImage, &convWeight, &convMask, refRO->image, refRO->weight, subMask,
    228                                maskBlank, solution, kernels)) {
    229         psError(PS_ERR_UNKNOWN, false, "Unable to convolve reference image.");
    230         goto ERROR;
    231     }
     258            memCheck("diag outputs");
     259
     260            psTrace("ppSub", 2, "Convolving...\n");
     261            if (!pmSubtractionConvolve(&convImage, &convWeight, &convMask, refRO->image, refRO->weight,
     262                                       subMask, maskBlank, region, solution, kernels)) {
     263                psError(PS_ERR_UNKNOWN, false, "Unable to convolve reference image.");
     264                goto ERROR;
     265            }
     266
     267            psFree(solution);
     268            solution = NULL;
     269        }
     270    }
     271
     272    psFree(region);
     273    region = NULL;
    232274    psFree(subMask);
    233275    subMask = NULL;
    234276
     277    if (!pmSubtractionBorder(convImage, convWeight, convMask, kernels, maskBlank)) {
     278        psError(PS_ERR_UNKNOWN, false, "Unable to set border region of convolved image.");
     279        goto ERROR;
     280    }
     281
    235282    psFree(kernels);
    236     psFree(solution);
    237283
    238284    memCheck("convolution");
     
    265311        for (int x = 0; x < outRO->image->numCols; x++) {
    266312            if (isnan(outRO->image->data.F32[y][x]) && !(outRO->mask->data.U8[y][x] & maskBlank)) {
    267                 printf("%d %d --> %d\n", x, y, outRO->mask->data.U8[y][x]);
     313                printf("Unmasked NAN at %d %d --> %d\n", x, y, outRO->mask->data.U8[y][x]);
    268314            }
    269315        }
     
    315361
    316362ERROR:
     363    psFree(region);
    317364    psFree(subMask);
    318365    psFree(kernels);
Note: See TracChangeset for help on using the changeset viewer.