IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 25027 for branches/pap/ppSub


Ignore:
Timestamp:
Aug 7, 2009, 4:08:25 PM (17 years ago)
Author:
Paul Price
Message:

Merging trunk (r25026) to get up-to-date on old branch.

Location:
branches/pap
Files:
1 deleted
12 edited
2 copied

Legend:

Unmodified
Added
Removed
  • branches/pap

  • branches/pap/ppSub

    • Property svn:mergeinfo deleted
  • branches/pap/ppSub/src/Makefile.am

    r23808 r25027  
    3030        ppSubVersion.c                  \
    3131        ppSubBackground.c               \
     32        ppSubVarianceRescale.c          \
    3233        ppSubCamera.c                   \
    3334        ppSubData.c                     \
     
    4445        ppSubReadoutStats.c             \
    4546        ppSubReadoutSubtract.c          \
    46         ppSubSetMasks.c
     47        ppSubSetMasks.c                 \
     48        ppSubThreshold.c
    4749
    4850ppSubKernel_CPPFLAGS = $(PSLIB_CFLAGS) $(PSMODULE_CFLAGS) $(PPSUB_CFLAGS)
  • branches/pap/ppSub/src/ppSub.h

    r23753 r25027  
    2424
    2525#define PPSUB_RECIPE "PPSUB"            /// Name of the recipe to use
     26#define WCS_TOLERANCE 0.001             // Tolerance for WCS
    2627
    2728// Output files, for activation/deactivation
     
    7778    );
    7879
     80/// Threshold low pixels in image
     81bool ppSubLowThreshold(ppSubData *data  ///< Processing data
     82    );
     83
    7984/// Generate the output readout and pass the kernel info to the header
    8085bool ppSubDefineOutput(const char *name,///< Name of output to define
     
    98103/// Higher-order background subtraction
    99104bool ppSubBackground(pmConfig *config   ///< Configuration
     105    );
     106
     107/// Perform Variance correction (rescale within a modest range)
     108bool ppSubVarianceRescale(pmConfig *config   ///< Configuration
    100109    );
    101110
  • branches/pap/ppSub/src/ppSubArguments.c

    r23853 r25027  
    8484    psMetadataAddS32(arguments, PS_LIST_TAIL, "-threads", 0, "Number of threads", 0);
    8585    psMetadataAddStr(arguments, PS_LIST_TAIL, "-dumpconfig", 0, "file to dump configuration to", NULL);
     86    psMetadataAddS32(arguments, PS_LIST_TAIL, "-convolve", 0, "Image to convolve [1 or 2]", 0);
    8687    psMetadataAddBool(arguments, PS_LIST_TAIL, "-photometry", 0, "Perform photometry?", NULL);
    8788    psMetadataAddBool(arguments, PS_LIST_TAIL, "-inverse", 0, "Generate inverse subtractions?", NULL);
  • branches/pap/ppSub/src/ppSubCamera.c

    r23753 r25027  
    163163
    164164
     165    // Now that the camera has been determined, we can read the recipe
     166    psMetadata *recipe = psMetadataLookupMetadata(NULL, config->recipes, PPSUB_RECIPE); // Recipe for ppSim
     167    if (!recipe) {
     168        psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to find recipe %s", PPSUB_RECIPE);
     169        return false;
     170    }
     171    if (psMetadataLookupBool(NULL, config->arguments, "-photometry")) {
     172        psMetadataAddBool(recipe, PS_LIST_TAIL, "PHOTOMETRY", PS_META_REPLACE,
     173                          "Perform photometry?", true);
     174    }
     175    if (psMetadataLookupBool(NULL, config->arguments, "-inverse")) {
     176        psMetadataAddBool(recipe, PS_LIST_TAIL, "INVERSE", PS_META_REPLACE,
     177                          "Generate inverse subtractions?", true);
     178    }
     179
     180    data->inverse = psMetadataLookupBool(NULL, recipe, "INVERSE");
     181    data->photometry = psMetadataLookupBool(NULL, recipe, "PHOTOMETRY");
     182
     183    bool mdok;                          // Status of MD lookup
     184    bool saveConv = psMetadataLookupBool(&mdok, recipe, "SAVE.CONVOLVED"); // Save convolved images?
     185
    165186    // Convolved input image
    166187    pmFPAfile *inConvImage = defineOutputFile(config, input, true, "PPSUB.INPUT.CONV", PM_FPA_FILE_IMAGE);
     
    171192        return false;
    172193    }
     194    if (saveConv) {
     195        inConvImage->save = true;
     196        inConvMask->save = true;
     197    }
    173198    if (inVar) {
    174199        pmFPAfile *inConvVar = defineOutputFile(config, inConvImage, false, "PPSUB.INPUT.CONV.VARIANCE",
     
    177202            psError(PS_ERR_UNKNOWN, false, "Unable to define output files");
    178203            return false;
     204        }
     205        if (saveConv) {
     206            inConvVar->save = true;
    179207        }
    180208    }
     
    188216        return false;
    189217    }
     218    if (saveConv) {
     219        refConvImage->save = true;
     220        refConvMask->save = true;
     221    }
    190222    if (refVar) {
    191223        pmFPAfile *refConvVar = defineOutputFile(config, refConvImage, false, "PPSUB.REF.CONV.VARIANCE",
     
    195227            return false;
    196228        }
    197     }
    198 
    199 
    200     // Now that the camera has been determined, we can read the recipe
    201     psMetadata *recipe = psMetadataLookupMetadata(NULL, config->recipes, PPSUB_RECIPE); // Recipe for ppSim
    202     if (!recipe) {
    203         psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to find recipe %s", PPSUB_RECIPE);
    204         return false;
    205     }
    206     if (psMetadataLookupBool(NULL, config->arguments, "-photometry")) {
    207         psMetadataAddBool(recipe, PS_LIST_TAIL, "PHOTOMETRY", PS_META_REPLACE,
    208                           "Perform photometry?", true);
    209     }
    210     if (psMetadataLookupBool(NULL, config->arguments, "-inverse")) {
    211         psMetadataAddBool(recipe, PS_LIST_TAIL, "INVERSE", PS_META_REPLACE,
    212                           "Generate inverse subtractions?", true);
    213     }
    214 
    215     data->inverse = psMetadataLookupBool(NULL, recipe, "INVERSE");
    216     data->photometry = psMetadataLookupBool(NULL, recipe, "PHOTOMETRY");
    217 
     229        if (saveConv) {
     230            refConvVar->save = true;
     231        }
     232    }
    218233
    219234    // Output image
  • branches/pap/ppSub/src/ppSubKernel.c

    r21360 r25027  
    2929    }
    3030
    31     psTraceSetLevel("psModules.imcombine", 7);
     31    (void) psTraceSetLevel("psModules.imcombine", 7);
    3232
    3333    const char *inName = argv[1];       // Input file name
  • branches/pap/ppSub/src/ppSubLoop.c

    r23753 r25027  
    6565    }
    6666
     67    if (!ppSubLowThreshold(data)) {
     68        psError(PS_ERR_UNKNOWN, false, "Unable to threshold images.");
     69        return false;
     70    }
     71
    6772    // Set up subtraction files
    6873    if (!ppSubFilesIterateDown(config, PPSUB_FILES_SUB)) {
     
    95100    if (!ppSubBackground(config)) {
    96101        psError(PS_ERR_UNKNOWN, false, "Unable to subtract background.");
     102        return false;
     103    }
     104
     105    // Perform Variance correction (rescale within a modest range)
     106    if (!ppSubVarianceRescale(config)) {
     107        psError(PS_ERR_UNKNOWN, false, "Unable to rescale variance.");
    97108        return false;
    98109    }
  • branches/pap/ppSub/src/ppSubMatchPSFs.c

    r23758 r25027  
    1818#include <pslib.h>
    1919#include <psmodules.h>
    20 #include <psphot.h>
    2120
    2221#include "ppSub.h"
     22
     23// Normalise a region on an image
     24static void normaliseRegion(psImage *image, // Image to normalise
     25                            const psRegion *region, // Region of image to normalise
     26                            float norm  // Normalisation
     27                            )
     28{
     29    if (!image) {
     30        return;
     31    }
     32    psAssert(region, "Expect region");
     33    psImage *subImage = psImageSubset(image, *region); // Sub-image
     34    psBinaryOp(subImage, subImage, "*", psScalarAlloc(norm, PS_TYPE_F32));
     35    psFree(subImage);
     36    return;
     37}
     38
    2339
    2440bool ppSubMatchPSFs(ppSubData *data)
     
    6076    pmReadout *inSourceRO = pmFPAfileThisReadout(config->files, view, "PPSUB.INPUT.SOURCES");
    6177    pmReadout *refSourceRO = pmFPAfileThisReadout(config->files, view, "PPSUB.REF.SOURCES");
    62     psArray *inSources = psMetadataLookupPtr(&mdok, inSourceRO->analysis, "PSPHOT.SOURCES"); // Source list
    63     psArray *refSources = psMetadataLookupPtr(&mdok, refSourceRO->analysis, "PSPHOT.SOURCES"); // Source list
     78    psArray *inSources = inSourceRO ? psMetadataLookupPtr(&mdok, inSourceRO->analysis, "PSPHOT.SOURCES") :
     79        NULL; // Source list from input image
     80    psArray *refSources = refSourceRO ? psMetadataLookupPtr(&mdok, refSourceRO->analysis, "PSPHOT.SOURCES") :
     81        NULL ; // Source list from reference image
    6482
    6583    psArray *sources = NULL;            // Merged list of sources
     
    92110    float spacing = psMetadataLookupF32(NULL, recipe, "STAMP.SPACING"); // Typical stamp spacing
    93111    float threshold = psMetadataLookupF32(NULL, recipe, "STAMP.THRESHOLD"); // Threshold for stmps
    94     const char *stampsName = psMetadataLookupStr(&mdok, config->arguments, "STAMPS"); // Filename for stamps
    95112
    96113    const char *typeStr = psMetadataLookupStr(NULL, recipe, "KERNEL.TYPE"); // Kernel type
     
    135152
    136153    bool dual = psMetadataLookupBool(&mdok, recipe, "DUAL"); // Dual convolution?
    137     pmSubtractionMode subMode = dual ? PM_SUBTRACTION_MODE_DUAL : PM_SUBTRACTION_MODE_UNSURE; // Subtracn mode
     154    pmSubtractionMode subMode;          // Subtraction mode
     155    if (dual) {
     156        subMode = PM_SUBTRACTION_MODE_DUAL;
     157    } else {
     158        int convolve = psMetadataLookupS32(NULL, config->arguments, "-convolve"); // Image number to convolve
     159        switch (convolve) {
     160          case 0:
     161            subMode = PM_SUBTRACTION_MODE_UNSURE;
     162            break;
     163          case 1:
     164            subMode = PM_SUBTRACTION_MODE_1;
     165            break;
     166          case 2:
     167            subMode = PM_SUBTRACTION_MODE_2;
     168            break;
     169          default:
     170            psErrorStackPrint(stderr, "Invalid value for -convolve");
     171            return false;
     172        }
     173    }
    138174
    139175    int threads = psMetadataLookupS32(NULL, config->arguments, "-threads"); // Number of threads
     
    149185    } else {
    150186        success = pmSubtractionMatch(inConv, refConv, inRO, refRO, footprint, stride, regionSize,
    151                                      spacing, threshold, sources, stampsName, type, size, order,
     187                                     spacing, threshold, sources, data->stamps, type, size, order,
    152188                                     widths, orders, inner, ringsOrder, binning, penalty, optimum,
    153189                                     optWidths, optOrder, optThresh, iter, rej, sys, maskVal,
     
    165201            ppSubDataQuality(data, error, PPSUB_FILES_ALL);
    166202            return true;
     203        } else if (error == PM_ERR_SMALL_AREA) {
     204            psErrorStackPrint(stderr, "Insufficient area for PSF matching");
     205            psWarning("Insufficient area for PSF matching --- suspect bad data quality.");
     206            ppSubDataQuality(data, error, PPSUB_FILES_ALL);
     207            return true;
    167208        } else {
    168209            psError(PS_ERR_UNKNOWN, false, "Unable to match images.");
     
    171212    }
    172213
     214    // Need to be careful with the normalisation
     215    // We will normalise everything to the normalisation of the *input* image
     216    {
     217        // Since the entries are MULTI, we have to retrieve them differently
     218        psMetadataIterator *regIter = psMetadataIteratorAlloc(inConv->analysis, PS_LIST_HEAD,
     219                                                              "^" PM_SUBTRACTION_ANALYSIS_REGION "$");
     220        psMetadataIterator *modeIter = psMetadataIteratorAlloc(inConv->analysis, PS_LIST_HEAD,
     221                                                              "^" PM_SUBTRACTION_ANALYSIS_MODE "$");
     222        psMetadataIterator *normIter = psMetadataIteratorAlloc(inConv->analysis, PS_LIST_HEAD,
     223                                                              "^" PM_SUBTRACTION_ANALYSIS_NORM "$");
     224        psMetadataItem *regItem;        // Item with region
     225        while ((regItem = psMetadataGetAndIncrement(regIter))) {
     226            psAssert(regItem->type == PS_DATA_REGION && regItem->data.V, "Expect region type");
     227            psRegion *region = regItem->data.V; // Region of interest
     228            psMetadataItem *modeItem = psMetadataGetAndIncrement(modeIter); // Item with mode
     229            psAssert(modeItem && modeItem->type == PS_TYPE_S32, "Expect subtraction mode");
     230            pmSubtractionMode mode = modeItem->data.S32; // Subtraction mode
     231            psMetadataItem *normItem = psMetadataGetAndIncrement(normIter); // Item with normalisation
     232            psAssert(normItem && normItem->type == PS_TYPE_F32 && isfinite(normItem->data.F32),
     233                     "Expect normalisation");
     234            float norm = normItem->data.F32; // Normalisation
     235
     236            switch (mode) {
     237              case PM_SUBTRACTION_MODE_1: // Convolved the input to match template
     238              case PM_SUBTRACTION_MODE_DUAL: // Convolved both; template should have flux conserved
     239                psLogMsg("ppSub", PS_LOG_INFO, "Correcting image for normalisation of %f\n", norm);
     240                normaliseRegion(inConv->image, region, 1.0 / norm);
     241                normaliseRegion(refConv->image, region, 1.0 / norm);
     242                normaliseRegion(inConv->variance, region, 1.0 / PS_SQR(norm));
     243                normaliseRegion(refConv->variance, region, 1.0 / PS_SQR(norm));
     244                break;
     245              case PM_SUBTRACTION_MODE_2:       // Convolved the template to match input
     246                // We're already happy!
     247                psLogMsg("ppSub", PS_LOG_INFO, "Image normalisation is correct\n");
     248                break;
     249              default:
     250                psAbort("Invalid subtraction mode: %x", mode);
     251            }
     252        }
     253        psFree(regIter);
     254        psFree(modeIter);
     255        psFree(normIter);
     256    }
     257
     258
    173259    pmConceptsCopyFPA(inConv->parent->parent->parent, inRO->parent->parent->parent, true, true);
    174260    pmConceptsCopyFPA(refConv->parent->parent->parent, refRO->parent->parent->parent, true, true);
  • branches/pap/ppSub/src/ppSubReadoutInverse.c

    r23763 r25027  
    77
    88#include "ppSub.h"
    9 
    109
    1110bool ppSubReadoutInverse(pmConfig *config)
     
    2423    invRO->data_exists = invRO->parent->data_exists = invRO->parent->parent->data_exists = true;
    2524
    26     pmConceptsCopyFPA(invRO->parent->parent->parent, outRO->parent->parent->parent, true, true);
     25    // Get concepts from reference
     26    pmFPAfile *refFile = psMetadataLookupPtr(NULL, config->files, "PPSUB.REF.CONV"); // File with concepts
     27    pmFPA *invFPA = invRO->parent->parent->parent; // Inverse FPA
     28    pmConceptsCopyFPA(invFPA, refFile->fpa, true, true);
     29
     30    // Get astrometry from (forward) subtraction
     31    pmChip *outChip = outRO->parent->parent;       // Output chip
     32    pmFPA *outFPA = outChip->parent;               // Output FPA
     33    pmChip *invChip = invRO->parent->parent; // Inverse chip
     34    pmHDU *invHDU = invFPA->hdu;          // Inverse HDU
     35    if (!pmAstromWriteWCS(invHDU->header, outFPA, outChip, WCS_TOLERANCE)) {
     36        psError(PS_ERR_UNKNOWN, false, "Unable to write WCS astrometry to PPSUB.INVERSE.");
     37        return false;
     38    }
     39    // Read from newly written astrometry so that it exists in the "inverse" FPA (for sources)
     40    if (!pmAstromReadWCS(invFPA, invChip, invHDU->header, 1.0)) {
     41        psError(PS_ERR_UNKNOWN, false, "Unable to read WCS astrometry.");
     42        return false;
     43    }
    2744
    2845    return true;
  • branches/pap/ppSub/src/ppSubReadoutPhotometry.c

    r23938 r25027  
    6565    // equivalent to the minuend image.
    6666    pmReadout *inRO = pmFPAfileThisReadout(config->files, view, name); // Readout with image and sources
     67    if (psMetadataLookup(inRO->analysis, "PSPHOT.SOURCES")) {
     68        psMetadataRemoveKey(inRO->analysis, "PSPHOT.SOURCES");
     69    }
    6770
    6871    pmFPAfile *photFile = psMetadataLookupPtr(&mdok, config->files, "PSPHOT.INPUT"); // Photometry file
     
    9295    }
    9396
     97    // If no sources were found, there's no error,  but we want to trigger 'bad quality'
     98    psArray *sources = psMetadataLookupPtr(NULL, photRO->analysis, "PSPHOT.SOURCES"); // Sources
     99    if (!sources) {
     100        ppSubDataQuality(data, PSPHOT_ERR_DATA, PPSUB_FILES_PHOT_SUB | PPSUB_FILES_PHOT_INV);
     101    }
     102
    94103    if (data->stats) {
    95         psArray *sources = psMetadataLookupPtr(NULL, photRO->analysis, "PSPHOT.SOURCES"); // Sources
    96104        bool mdok;
    97105        int numSources = psMetadataLookupS32(&mdok, data->stats, "NUM_SOURCES"); // Number of sources
  • branches/pap/ppSub/src/ppSubReadoutSubtract.c

    r23740 r25027  
    2020
    2121#include "ppSub.h"
    22 
    23 #define WCS_TOLERANCE 0.001             // Tolerance for WCS
    2422
    2523bool ppSubReadoutSubtract(pmConfig *config)
     
    4644        subtrahend = pmFPAfileThisReadout(config->files, view, "PPSUB.REF.CONV");
    4745    }
    48 
    49 // XXX this should be replaced by SAVE options to the inConv and refConf pmFPAfiles
    50 #ifdef TESTING
    51     {
    52         pmReadoutMaskApply(minuend, maskVal);
    53         psFits *fits = psFitsOpen("minuend.fits", "w");
    54         psFitsWriteImage(fits, NULL, minuend->image, 0, NULL);
    55         psFitsClose(fits);
    56     }
    57     {
    58         pmReadoutMaskApply(subtrahend, maskVal);
    59         psFits *fits = psFitsOpen("subtrahend.fits", "w");
    60         psFitsWriteImage(fits, NULL, subtrahend->image, 0, NULL);
    61             psFitsClose(fits);
    62     }
    63 #endif
    6446
    6547    // Do the actual subtraction
Note: See TracChangeset for help on using the changeset viewer.