IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Feb 13, 2011, 12:33:05 PM (15 years ago)
Author:
eugene
Message:

adding some of the metadata needed by PSPS to output headers; skip models with few valid pixels (eg, only the edge is showing); only do the linear fit on pixels within the fit radius; modify psf-match auto-scaling process; enable multiple target psfs for matched-psf aperture photometry; speed up analysis of the radial apertures

Location:
trunk/psphot
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/psphot

  • trunk/psphot/src/psphotStackReadout.c

    r29936 r30624  
    11# include "psphotInternal.h"
    22
     3// we have 3 possible real filesets:
    34# define STACK_RAW "PSPHOT.STACK.INPUT.RAW"
    4 # define STACK_OUT "PSPHOT.STACK.OUTPUT.IMAGE"
     5# define STACK_CNV "PSPHOT.STACK.INPUT.CNV"
     6# define STACK_OUT "PSPHOT.STACK.OUTPUT.IMAGE"  /* the psf-matched image */
     7
     8// we have 3 files on which we operate:
     9// DET (detection image)       : nominally RAW (optionally CNV?)
     10// SRC (source analysis image) : nominally CNV (optionally RAW)
     11// OUT (psf-matched images)    : always OUT
     12
     13bool psphotStackVisualFilerule(pmConfig *config, const pmFPAview *view, const char *filerule) {
     14
     15    bool status = false;
     16
     17    int num = psphotFileruleCount(config, filerule);
     18
     19    // select the appropriate recipe information
     20    psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes, PSPHOT_RECIPE);
     21
     22    // loop over the available readouts
     23    for (int i = 0; i < num; i++) {
     24
     25        // find the currently selected readout
     26        pmFPAfile *file = pmFPAfileSelectSingle(config->files, filerule, i); // File of interest
     27        psAssert (file, "missing file?");
     28
     29        pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
     30        psAssert (readout, "missing readout?");
     31
     32        pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
     33        psAssert (detections, "missing detections?");
     34
     35        psArray *sources = detections->allSources;
     36        psAssert (sources, "missing sources?");
     37
     38        psphotVisualShowResidualImage (readout, true);
     39        psphotVisualShowObjectRegions (readout, recipe, sources);
     40    }
     41    return true;
     42}
    543
    644bool psphotStackReadout (pmConfig *config, const pmFPAview *view) {
     
    2058    PS_ASSERT_PTR_NON_NULL (breakPt, false);
    2159
     60    // XXX or do I set OUT to be a pmFPAfile pointing at the input of interest?
     61    bool useRaw = psMetadataLookupBool (NULL, recipe, "PSPHOT.STACK.USE.RAW");
     62    char *STACK_SRC = useRaw ? STACK_RAW : STACK_CNV;
     63    char *STACK_DET = STACK_RAW; // XXX optionally allow this to be CNV?
     64
    2265    // we have 3 relevant files: RAW, CNV, OUT
    2366
    2467    // set the photcode for each image
    25     if (!psphotAddPhotcode (config, view, STACK_OUT)) {
     68    if (!psphotAddPhotcode (config, view, STACK_SRC)) {
    2669        psError (PSPHOT_ERR_CONFIG, false, "trouble defining the photcode");
    2770        return false;
     
    3073    // Generate the mask and weight images
    3174    // XXX this should be done before we perform the convolutions
    32     if (!psphotSetMaskAndVariance (config, view, STACK_RAW)) {
    33         return psphotReadoutCleanup (config, view, STACK_OUT);
     75    if (!psphotSetMaskAndVariance (config, view, STACK_DET)) {
     76        return psphotReadoutCleanup (config, view, STACK_SRC);
    3477    }
    3578    if (!strcasecmp (breakPt, "NOTHING")) {
    36         return psphotReadoutCleanup (config, view, STACK_OUT);
     79        return psphotReadoutCleanup (config, view, STACK_SRC);
    3780    }
    3881
     
    4083    // XXX I think this is not defined correctly for an array of images.
    4184    // XXX probably need to subtract the model (same model?) for both RAW and OUT
    42     if (!psphotModelBackground (config, view, STACK_RAW)) {
    43         return psphotReadoutCleanup (config, view, STACK_OUT);
    44     }
    45     if (!psphotSubtractBackground (config, view, STACK_RAW)) {
    46         return psphotReadoutCleanup (config, view, STACK_OUT);
     85    if (!psphotModelBackground (config, view, STACK_DET)) {
     86        return psphotReadoutCleanup (config, view, STACK_SRC);
     87    }
     88    if (!psphotSubtractBackground (config, view, STACK_DET)) {
     89        return psphotReadoutCleanup (config, view, STACK_SRC);
    4790    }
    4891    if (!strcasecmp (breakPt, "BACKMDL")) {
    49         return psphotReadoutCleanup (config, view, STACK_OUT);
    50     }
    51 
    52     // load the psf model, if suppled.  FWHM_X,FWHM_Y,etc are determined and saved on
    53     // readout->analysis XXX this function currently only works with a single PSPHOT.INPUT
    54     if (!psphotLoadPSF (config, view, STACK_RAW)) {
    55         psError (PSPHOT_ERR_UNKNOWN, false, "error loading psf model");
    56         return psphotReadoutCleanup (config, view, STACK_OUT);
    57     }
    58 
    59     if (!psphotStackChisqImage(config, view, STACK_RAW, STACK_OUT)) {
     92        return psphotReadoutCleanup (config, view, STACK_SRC);
     93    }
     94
     95    if (!psphotStackChisqImage(config, view, STACK_DET, STACK_SRC)) {
    6096        psError (PSPHOT_ERR_UNKNOWN, false, "failure to generate chisq image");
    61         return psphotReadoutCleanup (config, view, STACK_OUT);
     97        return psphotReadoutCleanup (config, view, STACK_SRC);
    6298    }
    6399    if (!strcasecmp (breakPt, "CHISQ")) {
    64         return psphotReadoutCleanup (config, view, STACK_OUT);
     100        return psphotReadoutCleanup (config, view, STACK_SRC);
    65101    }
    66102
    67103    // find the detections (by peak and/or footprint) in the image.
    68104    // This finds the detections on Chisq image as well as the individuals
    69     if (!psphotFindDetections (config, view, STACK_RAW, true)) { // pass 1
     105    if (!psphotFindDetections (config, view, STACK_DET, true)) { // pass 1
    70106        // this only happens if we had an error in psphotFindDetections
    71107        psError (PSPHOT_ERR_UNKNOWN, false, "failure in peak analysis");
    72         return psphotReadoutCleanup (config, view, STACK_OUT);
    73     }
    74 
    75     // copy the detections from RAW to OUT
    76     if (!psphotCopySources (config, view, STACK_OUT, STACK_RAW)) {
    77         psError (PSPHOT_ERR_UNKNOWN, false, "failure in peak analysis");
    78         return psphotReadoutCleanup (config, view, STACK_OUT);
     108        return psphotReadoutCleanup (config, view, STACK_SRC);
     109    }
     110
     111    // copy the detections from DET to SRC
     112    if (strcmp(STACK_SRC, STACK_DET)) {
     113        if (!psphotCopySources (config, view, STACK_SRC, STACK_DET)) {
     114            psError (PSPHOT_ERR_UNKNOWN, false, "failure in peak analysis");
     115            return psphotReadoutCleanup (config, view, STACK_SRC);
     116        }
    79117    }
    80118
    81119    // construct sources and measure basic stats (saved on detections->newSources)
    82     // only run this on detections from the input images, not chisq image
    83     if (!psphotSourceStats (config, view, STACK_OUT, true)) { // pass 1
     120    if (!psphotSourceStats (config, view, STACK_SRC, true)) { // pass 1
    84121        psError(PSPHOT_ERR_UNKNOWN, false, "failure to generate sources");
    85         return psphotReadoutCleanup (config, view, STACK_OUT);
     122        return psphotReadoutCleanup (config, view, STACK_SRC);
     123    }
     124
     125    if (!strcasecmp (breakPt, "TEST1")) {
     126        return psphotReadoutCleanup (config, view, STACK_SRC);
    86127    }
    87128
    88129    // generate the objects (object unify the sources from the different images)
    89     psArray *objects = psphotMatchSources (config, view, STACK_OUT);
     130    // XXX this could just match the detections for the chisq image, and not bother measuring the
     131    // source stats in that case...
     132    psArray *objects = psphotMatchSources (config, view, STACK_SRC);
     133
     134    if (!strcasecmp (breakPt, "TEST2")) {
     135        psFree(objects);
     136        return psphotReadoutCleanup (config, view, STACK_SRC);
     137    }
    90138
    91139    // construct sources for the newly-generated sources (from other images)
    92     if (!psphotSourceStats (config, view, STACK_OUT, false)) { // pass 1
     140    if (!psphotSourceStats (config, view, STACK_SRC, false)) { // pass 1
     141        psFree(objects);
    93142        psError(PSPHOT_ERR_UNKNOWN, false, "failure to generate sources");
    94         return psphotReadoutCleanup (config, view, STACK_OUT);
     143        return psphotReadoutCleanup (config, view, STACK_SRC);
    95144    }
    96145
     
    98147    // if (!psphotDeblendSatstars (config, view)) {
    99148    //     psError (PSPHOT_ERR_UNKNOWN, false, "failed on satstar deblend analysis");
    100     //     return psphotReadoutCleanup (config, view, STACK_OUT);
     149    //     return psphotReadoutCleanup (config, view, STACK_SRC);
    101150    // }
    102151
     
    104153    // if (!psphotBasicDeblend (config, view)) {
    105154    //     psError (PSPHOT_ERR_UNKNOWN, false, "failed on deblend analysis");
    106     //     return psphotReadoutCleanup (config, view, STACK_OUT);
     155    //     return psphotReadoutCleanup (config, view, STACK_SRC);
    107156    // }
    108157
    109158    // classify sources based on moments, brightness
    110159    // only run this on detections from the input images, not chisq image
    111     if (!psphotRoughClass (config, view, STACK_OUT)) {
     160    if (!psphotRoughClass (config, view, STACK_SRC)) {
     161        psFree(objects);
    112162        psError (PSPHOT_ERR_UNKNOWN, false, "failed to determine rough classifications");
    113         return psphotReadoutCleanup (config, view, STACK_OUT);
     163        return psphotReadoutCleanup (config, view, STACK_SRC);
    114164    }
    115165    // if we were not supplied a PSF model, determine the IQ stats here (detections->newSources)
    116166    // only run this on detections from the input images, not chisq image
    117     if (!psphotImageQuality (config, view, STACK_OUT)) { // pass 1
     167    if (!psphotImageQuality (config, view, STACK_SRC)) { // pass 1
     168        psFree(objects);
    118169        psError (PSPHOT_ERR_UNKNOWN, false, "failed to measure image quality");
    119         return psphotReadoutCleanup (config, view, STACK_OUT);
     170        return psphotReadoutCleanup (config, view, STACK_SRC);
    120171    }
    121172    if (!strcasecmp (breakPt, "MOMENTS")) {
    122         return psphotReadoutCleanup (config, view, STACK_OUT);
     173        psFree(objects);
     174        return psphotReadoutCleanup (config, view, STACK_SRC);
    123175    }
    124176
    125177    // use bright stellar objects to measure PSF if we were supplied a PSF for any input file,
    126178    // this step is skipped
    127     if (!psphotChoosePSF (config, view, STACK_OUT)) { // pass 1
     179    if (!psphotChoosePSF (config, view, STACK_SRC, true)) { // pass 1
     180        psFree(objects);
    128181        psLogMsg ("psphot", 3, "failure to construct a psf model");
    129         return psphotReadoutCleanup (config, view, STACK_OUT);
     182        return psphotReadoutCleanup (config, view, STACK_SRC);
    130183    }
    131184    if (!strcasecmp (breakPt, "PSFMODEL")) {
    132         return psphotReadoutCleanup (config, view, STACK_OUT);
     185        psFree(objects);
     186        return psphotReadoutCleanup (config, view, STACK_SRC);
    133187    }
    134188
    135189    // construct an initial model for each object, set the radius to fitRadius, set circular fit mask
    136     psphotGuessModels (config, view, STACK_OUT);
     190    psphotGuessModels (config, view, STACK_SRC);
    137191
    138192    // merge the newly selected sources into the existing list
    139193    // NOTE: merge OLD and NEW
    140     psphotMergeSources (config, view, STACK_OUT);
     194    psphotMergeSources (config, view, STACK_SRC);
    141195
    142196    // linear PSF fit to source peaks, subtract the models from the image (in PSF mask)
    143197    psphotFitSourcesLinearStack (config, objects, FALSE);
     198    psphotStackVisualFilerule(config, view, STACK_SRC);
    144199
    145200    // identify CRs and extended sources
    146     psphotSourceSize (config, view, STACK_OUT, TRUE);
     201    psphotSourceSize (config, view, STACK_SRC, TRUE);
     202
     203    // XXX do we want to do a preliminary (unconvolved) model fit here, and then
     204    // do a second detection pass? (like standard psphot)
    147205
    148206    // measure aperture photometry corrections
    149     if (!psphotApResid (config, view, STACK_OUT)) {
     207    if (!psphotApResid (config, view, STACK_SRC)) {
    150208        psFree (objects);
    151209        psLogMsg ("psphot", 3, "failed on psphotApResid");
    152         return psphotReadoutCleanup (config, view, STACK_OUT);
     210        return psphotReadoutCleanup (config, view, STACK_SRC);
    153211    }
    154212
    155213    psphotStackObjectsUnifyPosition (objects);
    156214
    157     // measure circular, radial apertures (objects sorted by S/N)
    158     psphotRadialAperturesByObject (config, objects, view, STACK_OUT);
    159 
    160215    // measure elliptical apertures, petrosians (objects sorted by S/N)
    161     psphotExtendedSourceAnalysisByObject (config, objects, view, STACK_OUT); // pass 1 (detections->allSources)
     216    psphotExtendedSourceAnalysisByObject (config, objects, view, STACK_SRC); // pass 1 (detections->allSources)
    162217
    163218    // measure non-linear extended source models (exponential, deVaucouleur, Sersic) (sources sorted by S/N)
    164     psphotExtendedSourceFits (config, view, STACK_OUT); // pass 1 (detections->allSources)
     219    psphotExtendedSourceFits (config, view, STACK_SRC); // pass 1 (detections->allSources)
    165220
    166221    // calculate source magnitudes
    167     psphotMagnitudes(config, view, STACK_OUT);
     222    psphotMagnitudes(config, view, STACK_SRC);
     223
     224    // create source children for the OUT filerule (for radial aperture photometry)
     225    psArray *objectsRadial = psphotSourceChildrenByObject (config, view, STACK_OUT, objects);
     226    if (!objectsRadial) {
     227        psFree(objects);
     228        psError (PSPHOT_ERR_UNKNOWN, false, "failure in peak analysis");
     229        return psphotReadoutCleanup (config, view, STACK_SRC);
     230    }
     231
     232    bool smoothAgain = true;
     233    for (int nMatchedPSF = 0; smoothAgain; nMatchedPSF++) {
     234
     235        // re-measure the PSF for the smoothed image (using entries in 'allSources')
     236        psphotChoosePSF (config, view, STACK_OUT, false);
     237
     238        // this is necessary to update the models based on the new PSF
     239        psphotResetModels (config, view, STACK_OUT);
     240
     241        // this is necessary to get the right normalization for the new models
     242        psphotFitSourcesLinear (config, view, STACK_OUT, false);
     243
     244        // measure circular, radial apertures (objects sorted by S/N)
     245        psphotRadialAperturesByObject (config, objectsRadial, view, STACK_OUT, nMatchedPSF);
     246
     247        // replace the flux in the image so it is returned to its original state
     248        psphotReplaceAllSources (config, view, STACK_OUT);
     249
     250        // smooth to the next FWHM, or set 'smoothAgain' to false if no more
     251        psphotStackMatchPSFsNext(&smoothAgain, config, view, STACK_OUT, nMatchedPSF);
     252    }
    168253
    169254    if (0 && !psphotEfficiency(config, view, STACK_OUT)) {
     
    176261
    177262    // replace background in residual image
    178     psphotSkyReplace (config, view, STACK_RAW);
     263    psphotSkyReplace (config, view, STACK_DET);
    179264
    180265    // drop the references to the image pixels held by each source
    181     psphotSourceFreePixels (config, view, STACK_OUT);
    182 
    183     // remove chisq image from config->file:PSPHOT.INPUT (why?)
    184     psphotStackRemoveChisqFromInputs(config, STACK_RAW);
     266    // psphotSourceFreePixels (config, view, STACK_OUT);
     267    psphotSourceFreePixels (config, view, STACK_SRC);
     268
     269    // remove chisq image from config->file:PSPHOT.INPUT
     270    psphotStackRemoveChisqFromInputs(config, STACK_DET);
     271    if (strcmp(STACK_SRC, STACK_DET)) {
     272        psphotStackRemoveChisqFromInputs(config, STACK_SRC);
     273    }
    185274
    186275    psFree (objects);
     276    psFree (objectsRadial);
    187277
    188278    // create the exported-metadata and free local data
    189     return psphotReadoutCleanup (config, view, STACK_OUT);
     279    return psphotReadoutCleanup (config, view, STACK_SRC);
    190280}
    191281
     282/* here is the process:
     283
     284 * we have three(*) images:
     285 * RAW : unconvolved image stack
     286 * CNV : input convolved image
     287
     288 * OUT : psf-matched output image (there may be more than one of
     289 * these.  we will generate the first matched image by selecting the
     290 * target PSF and doing a full psf-maching process (as used by ppStack
     291 * and ppSub).  But, additional target output files should use a
     292 * simple gaussian convolution kernel determind from therms of the
     293 * current and the target).
     294
     295 * the output should be / could be one of the matched images, but not
     296 * all.  should we ensure the first gets written out, and ot save the
     297 * others (or only optionally).
     298
     299 * by default, we probably only sve the cmf ffile outputs.
     300
     301 * load the RAW image (unconvolved stacks)
     302 * add photcode to the output headers / readout->analysis
     303 * generate mask and variance image (this is probably never needed in
     304   practice: we always load an input mask & var.
     305 * generate & subtract a model background for ?? (RAW? CNV? OUT? all?)
     306 * load a PSF (probably not yet working)
     307
     308 * generate the CHISQ image from the RAW input images (why save on OUT?)
     309
     310 * find detections on RAW
     311
     312 * copy detections to OUT
     313
     314 * generate source stats (moments) for OUT
     315
     316 * match sources across inputs (on OUT?)
     317
     318 * generate source stats for the new constructions
     319
     320 * rough class (star, galaxy, cosmic, etc)
     321
     322 * Image quality
     323
     324 * generate PSF
     325
     326 * guess models
     327
     328 * merge sources (new -> old)
     329
     330 * linear fit to the psf
     331
     332 * find ApResid
     333
     334 * assign common positions
     335
     336 * radial apertures (** this should be on the PSF-matched images
     337
     338 * extended analysis (elliptical profile & petrosian)
     339
     340 * extended fits (sersic, etc)
     341
     342 * psphot magnitudes
     343
     344
     345 ******
     346
     347 the above is all wrong:  first, we should be doing the full
     348 morphology analysis (ExtendedAnalysis & ExtendedFits) on the CNV or
     349 RAW image (as desired optionally), etc.
     350
     351 In the discussion below, 'BST' (best) means optionally RAW or CNV
     352
     353 * detection : RAW & CHISQ (of RAW)
     354 * moments : used by psf analysis & classification (BST)
     355 * rough class : uses moments, not pixels
     356 * image quality : uses moments as well
     357 * generate PSF : (BST)
     358 * guess models (BST)
     359 * linear fit (BST)
     360 * find ApResid (BST) -- uses sources not pixels
     361 * extended analysis (BST)
     362 * extended fits (BST)
     363 * detection efficiency (BST)
     364 
     365 * somehow need to copy the sources so they point at the pixels on the
     366 * OUT image
     367
     368 * foreach target PSF
     369   * radial aperture
     370   * convolve to next target PSF
     371
     372   * somehow need to organize the output file to have the values from
     373   * the different PSFs in separate tables (with header info to
     374   * specify the size of that PSF)
     375
     376   */
Note: See TracChangeset for help on using the changeset viewer.