IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jan 11, 2012, 11:19:21 PM (15 years ago)
Author:
mhuber
Message:

merged latest r33093 trunk changes to meh_branches/ppsub_test

Location:
branches/meh_branches/ppsub_test
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/meh_branches/ppsub_test

  • branches/meh_branches/ppsub_test/psphot

  • branches/meh_branches/ppsub_test/psphot/src

  • branches/meh_branches/ppsub_test/psphot/src/psphotStackReadout.c

    r31154 r33098  
    4444bool psphotStackReadout (pmConfig *config, const pmFPAview *view) {
    4545
     46    psArray *objects = NULL; // used below after 'pass1finish' label
     47
     48    // measure the total elapsed time in psphotReadout.  dtime is the elapsed time used jointly
     49    // by the multiple threads, not the total time used by all threads.
    4650    psTimerStart ("psphotReadout");
    4751
     
    5660    // optional break-point for processing
    5761    char *breakPt = psMetadataLookupStr (NULL, recipe, "BREAK_POINT");
    58     PS_ASSERT_PTR_NON_NULL (breakPt, false);
    59 
    60     // XXX or do I set OUT to be a pmFPAfile pointing at the input of interest?
     62    psAssert (breakPt, "configuration error: set BREAK_POINT");
     63
     64    // we have 3 relevant files: RAW (unconvolved), CNV (convolved stack), OUT (psf-matched stack)
     65    // select which image (RAW or CNV) is used for analysis (RAW always used for detection)
    6166    bool useRaw = psMetadataLookupBool (NULL, recipe, "PSPHOT.STACK.USE.RAW");
    6267    char *STACK_SRC = useRaw ? STACK_RAW : STACK_CNV;
    63     char *STACK_DET = STACK_RAW; // XXX optionally allow this to be CNV?
    64 
    65     // we have 3 relevant files: RAW, CNV, OUT
     68    char *STACK_DET = STACK_RAW;
    6669
    6770    // set the photcode for each image
     
    7174    }
    7275
    73     // Generate the mask and weight images
    74     // XXX this should be done before we perform the convolutions
     76    // Generate the mask and weight images (if not supplied) and set mask bits
    7577    if (!psphotSetMaskAndVariance (config, view, STACK_DET)) {
    7678        return psphotReadoutCleanup (config, view, STACK_SRC);
     
    8183
    8284    // generate a background model (median, smoothed image)
    83     // XXX I think this is not defined correctly for an array of images.
    84     // XXX probably need to subtract the model (same model?) for both RAW and OUT
    8585    if (!psphotModelBackground (config, view, STACK_DET)) {
    8686        return psphotReadoutCleanup (config, view, STACK_SRC);
     
    8989        return psphotReadoutCleanup (config, view, STACK_SRC);
    9090    }
     91    if (!psphotSubtractBackground (config, view, STACK_SRC)) {
     92        return psphotReadoutCleanup (config, view, STACK_SRC);
     93    }
    9194    if (!strcasecmp (breakPt, "BACKMDL")) {
    9295        return psphotReadoutCleanup (config, view, STACK_SRC);
    9396    }
    9497
     98    // also make the chisq detection image
    9599    if (!psphotStackChisqImage(config, view, STACK_DET, STACK_SRC)) {
    96100        psError (PSPHOT_ERR_UNKNOWN, false, "failure to generate chisq image");
     
    109113    }
    110114
    111     // copy the detections from DET to SRC
     115    // If DET and SRC are different images, copy the detections from DET to SRC.  This 'copy'
     116    // is just a copy of the container pointer; the sources on both DET and SRC are the same
     117    // memory objects
    112118    if (strcmp(STACK_SRC, STACK_DET)) {
    113119        if (!psphotCopySources (config, view, STACK_SRC, STACK_DET)) {
     
    122128        return psphotReadoutCleanup (config, view, STACK_SRC);
    123129    }
    124 
    125     if (!strcasecmp (breakPt, "TEST1")) {
    126         return psphotReadoutCleanup (config, view, STACK_SRC);
    127     }
    128 
     130    if (!strcasecmp (breakPt, "PEAKS")) {
     131        return psphotReadoutCleanup (config, view, STACK_SRC);
     132    }
     133    // psphotDumpTest (config, view, STACK_SRC);
    129134    psMemDump("sourcestats");
    130 
    131     // generate the objects (object unify the sources from the different images)
    132     // XXX this could just match the detections for the chisq image, and not bother measuring the
    133     // source stats in that case...
    134     psArray *objects = psphotMatchSources (config, view, STACK_SRC);
    135 
    136     psMemDump("matchsources");
    137 
    138     if (!strcasecmp (breakPt, "TEST2")) {
    139         psFree(objects);
    140         return psphotReadoutCleanup (config, view, STACK_SRC);
    141     }
    142 
    143     // construct sources for the newly-generated sources (from other images)
    144     if (!psphotSourceStats (config, view, STACK_SRC, false)) { // pass 1
    145         psFree(objects);
    146         psError(PSPHOT_ERR_UNKNOWN, false, "failure to generate sources");
    147         return psphotReadoutCleanup (config, view, STACK_SRC);
    148     }
    149 
    150     psMemDump("sourcestats");
    151 
    152     // find blended neighbors of very saturated stars (detections->newSources)
    153     // if (!psphotDeblendSatstars (config, view)) {
    154     //     psError (PSPHOT_ERR_UNKNOWN, false, "failed on satstar deblend analysis");
    155     //     return psphotReadoutCleanup (config, view, STACK_SRC);
    156     // }
    157 
    158     // mark blended peaks PS_SOURCE_BLEND (detections->newSources)
    159     // if (!psphotBasicDeblend (config, view)) {
    160     //     psError (PSPHOT_ERR_UNKNOWN, false, "failed on deblend analysis");
    161     //     return psphotReadoutCleanup (config, view, STACK_SRC);
    162     // }
    163135
    164136    // classify sources based on moments, brightness
    165137    // only run this on detections from the input images, not chisq image
    166138    if (!psphotRoughClass (config, view, STACK_SRC)) {
    167         psFree(objects);
    168139        psError (PSPHOT_ERR_UNKNOWN, false, "failed to determine rough classifications");
    169140        return psphotReadoutCleanup (config, view, STACK_SRC);
    170141    }
     142
    171143    // if we were not supplied a PSF model, determine the IQ stats here (detections->newSources)
    172144    // only run this on detections from the input images, not chisq image
    173145    if (!psphotImageQuality (config, view, STACK_SRC)) { // pass 1
    174         psFree(objects);
    175146        psError (PSPHOT_ERR_UNKNOWN, false, "failed to measure image quality");
    176147        return psphotReadoutCleanup (config, view, STACK_SRC);
    177148    }
    178149    if (!strcasecmp (breakPt, "MOMENTS")) {
    179         psFree(objects);
    180         return psphotReadoutCleanup (config, view, STACK_SRC);
    181     }
    182 
    183     // use bright stellar objects to measure PSF if we were supplied a PSF for any input file,
    184     // this step is skipped
     150        return psphotReadoutCleanup (config, view, STACK_SRC);
     151    }
     152
     153    // use bright stellar objects to measure PSF
    185154    if (!psphotChoosePSF (config, view, STACK_SRC, true)) { // pass 1
    186         psFree(objects);
    187155        psLogMsg ("psphot", 3, "failure to construct a psf model");
    188156        return psphotReadoutCleanup (config, view, STACK_SRC);
    189157    }
    190158    if (!strcasecmp (breakPt, "PSFMODEL")) {
    191         psFree(objects);
    192         return psphotReadoutCleanup (config, view, STACK_SRC);
    193     }
    194 
    195     // construct an initial model for each object, set the radius to fitRadius, set circular fit mask
    196     psphotGuessModels (config, view, STACK_SRC);
     159        return psphotReadoutCleanup (config, view, STACK_SRC);
     160    }
    197161
    198162    // merge the newly selected sources into the existing list
     
    200164    psphotMergeSources (config, view, STACK_SRC);
    201165
     166    // Construct an initial model for each object, set the radius to fitRadius, set circular
     167    // fit mask.  NOTE: only applied to sources without guess models
     168    psphotGuessModels (config, view, STACK_SRC);
     169
    202170    // linear PSF fit to source peaks, subtract the models from the image (in PSF mask)
    203     psphotFitSourcesLinearStack (config, objects, FALSE);
     171    psphotFitSourcesLinear (config, view, STACK_SRC, false);
    204172    psphotStackVisualFilerule(config, view, STACK_SRC);
    205173
     174    // measure the radial profiles to the sky
     175    psphotRadialProfileWings (config, view, STACK_SRC);
     176
     177    // re-measure the kron mags with models subtracted.  this pass starts with a circular
     178    // window of size PSF_MOMENTS_RADIUS (same window used to measure the psf-scale moments)
     179    // but iterates to an appropriately larger size
     180    psphotKronIterate(config, view, STACK_SRC);
     181
    206182    // identify CRs and extended sources
    207     psphotSourceSize (config, view, STACK_SRC, TRUE);
    208 
    209     // XXX do we want to do a preliminary (unconvolved) model fit here, and then
    210     // do a second detection pass? (like standard psphot)
    211 
    212     // measure aperture photometry corrections
    213     if (!psphotApResid (config, view, STACK_SRC)) {
    214         psFree (objects);
    215         psLogMsg ("psphot", 3, "failed on psphotApResid");
    216         return psphotReadoutCleanup (config, view, STACK_SRC);
    217     }
     183    psphotSourceSize (config, view, STACK_SRC, true);
     184
     185    // non-linear PSF and EXT fit to brighter sources
     186    // replace model flux, adjust mask as needed, fit, subtract the models (full stamp)
     187    psphotBlendFit (config, view, STACK_SRC); // pass 1 (detections->allSources)
     188
     189    // replace all sources (do NOT ignore subtraction state)
     190    psphotReplaceAllSources (config, view, STACK_SRC, false); // pass 1 (detections->allSources)
     191
     192    // if we only do one pass, skip to extended source analysis
     193    if (!strcasecmp (breakPt, "PASS1")) goto pass1finish;
     194
     195    // linear fit to include all sources (subtract again)
     196    // NOTE : apply to ALL sources (extended + psf)
     197    // NOTE 2 : this function subtracts the models from the given filerule (SRC), not DET
     198    psphotFitSourcesLinear (config, view, STACK_SRC, true); // pass 2 (detections->allSources)
     199
     200    // NOTE: possibly re-measure background model here with objects subtracted / or masked
     201
     202    // NOTE: this block performs the 2nd pass low-significance PSF detection stage
     203    {
     204        // if DET and SRC are different images, generate children sources for all sources in
     205        // the SRC image.  This operation replaces the existing DETECTION container on DET
     206        // which is currently a view to the one on SRC).  children sources go to
     207        // det->allSources
     208        if (strcmp(STACK_SRC, STACK_DET)) {
     209            psphotSourceChildren (config, view, STACK_DET, STACK_SRC);
     210
     211            //  subtract all sources from DET (this will subtract using the psf model for SRC, which
     212            //  will somewhat oversubtract the sources -- this is OK
     213            psphotRemoveAllSources (config, view, STACK_DET, false); // ignore subtraction state for sources
     214        }
     215
     216        // add noise for subtracted objects
     217        psphotAddNoise (config, view, STACK_DET); // pass 1 (detections->allSources)
     218
     219        // find fainter sources
     220        // NOTE: finds new peaks and new footprints, OLD and FULL set are saved on detections
     221        psphotFindDetections (config, view, STACK_DET, false); // pass 2 (detections->peaks, detections->footprints)
     222
     223        // remove noise for subtracted objects (ie, return to normal noise level)
     224        // NOTE: this needs to operate only on the OLD sources
     225        psphotSubNoise (config, view, STACK_DET); // pass 1 (detections->allSources)
     226
     227        // if DET and SRC are different images, copy the detections from DET to SRC
     228        // (this operation just ensures the metadata container has a view on SRC as well
     229        if (strcmp(STACK_SRC, STACK_DET)) {
     230            // replace all sources in DET
     231            psphotReplaceAllSources (config, view, STACK_DET, false); // ignore subtraction state for sources
     232
     233            // copy the newly detected peaks from DET to SRC so SourceStats below can operate on them
     234            if (!psphotCopyPeaks (config, view, STACK_SRC, STACK_DET)) {
     235                psError (PSPHOT_ERR_UNKNOWN, false, "failure in peak analysis");
     236                return psphotReadoutCleanup (config, view, STACK_SRC);
     237            }
     238        }
     239
     240        // define new sources based on only the new peaks & measure moments
     241        // NOTE: new sources are saved on detections->newSources
     242        psphotSourceStats (config, view, STACK_SRC, false); // pass 2 (detections->newSources)
     243
     244        // set source type
     245        // NOTE: apply only to detections->newSources
     246        if (!psphotRoughClass (config, view, STACK_SRC)) { // pass 2 (detections->newSources)
     247            psLogMsg ("psphot", 3, "failed to find a valid PSF clump for image");
     248            return psphotReadoutCleanup (config, view, STACK_SRC);
     249        }
     250
     251        // replace all sources so fit below applies to all at once
     252        // NOTE: apply only to OLD sources (which have been subtracted)
     253        psphotReplaceAllSources (config, view, STACK_SRC, false); // pass 2
     254
     255        // merge the newly selected sources into the existing list
     256        // NOTE: merge OLD and NEW
     257        // XXX check on free of sources...
     258        psphotMergeSources (config, view, STACK_SRC); // (detections->newSources + detections->allSources -> detections->allSources)
     259
     260        // Construct an initial model for each object, set the radius to fitRadius, set circular
     261        // fit mask.  NOTE: only applied to sources without guess models
     262        psphotGuessModels (config, view, STACK_SRC);
     263    }
     264
     265pass1finish:
     266
     267    // generate the objects (objects unify the sources from the different images) NOTE: could
     268    // this just match the detections for the chisq image, and not bother measuring the source
     269    // stats in that case...?
     270    objects = psphotMatchSources (config, view, STACK_SRC);
     271    psMemDump("matchsources");
     272
     273    // Construct an initial model for each object, set the radius to fitRadius, set circular
     274    // fit mask.  NOTE: only applied to sources without guess models
     275    psphotGuessModels (config, view, STACK_SRC);
     276
     277    psphotStackObjectsUnifyPosition (objects);
     278
     279    psphotStackObjectsSelectForAnalysis (config, view, STACK_SRC, objects);
     280
     281    // NOTE: apply to ALL sources
     282    psphotFitSourcesLinear (config, view, STACK_SRC, true); // pass 3 (detections->allSources)
     283
     284    // measure the radial profiles to the sky (only measures new objects)
     285    psphotRadialProfileWings (config, view, STACK_SRC);
     286
     287    // re-measure the kron mags with models subtracted
     288    // psphotKronMasked(config, view, STACK_SRC);
     289    psphotKronIterate(config, view, STACK_SRC);
     290
     291    // measure source size for the remaining sources
     292    // NOTE: applies only to NEW (unmeasured) sources
     293    psphotSourceSize (config, view, STACK_SRC, false); // pass 2 (detections->allSources)
    218294
    219295    psMemDump("psfstats");
    220296
    221     psphotStackObjectsUnifyPosition (objects);
    222 
    223297    // measure elliptical apertures, petrosians (objects sorted by S/N)
    224     psphotExtendedSourceAnalysisByObject (config, objects, view, STACK_SRC); // pass 1 (detections->allSources)
     298    // psphotExtendedSourceAnalysisByObject (config, objects, view, STACK_SRC); // pass 1 (detections->allSources)
     299    psphotExtendedSourceAnalysis (config, view, STACK_SRC); // pass 1 (detections->allSources)
    225300
    226301    // measure non-linear extended source models (exponential, deVaucouleur, Sersic) (sources sorted by S/N)
    227302    psphotExtendedSourceFits (config, view, STACK_SRC); // pass 1 (detections->allSources)
    228 
    229     // calculate source magnitudes
    230     psphotMagnitudes(config, view, STACK_SRC);
    231303
    232304    // create source children for the OUT filerule (for radial aperture photometry)
     
    238310    }
    239311
     312    // measure circular, radial apertures (objects sorted by S/N)
     313    // this forces photometry on the undetected sources from other images
     314    psphotRadialApertures (config, view, STACK_SRC, 0); // XXX entry 0 == unmatched?
    240315    psMemDump("extmeas");
    241316
    242     bool smoothAgain = true;
    243     for (int nMatchedPSF = 0; smoothAgain; nMatchedPSF++) {
     317    int nRadialEntries = psphotStackMatchPSFsEntries(config, view, STACK_OUT);
     318
     319    for (int entry = 1; entry < nRadialEntries; entry++) {
     320        // NOTE: entry 0 is the unmatched image set
    244321
    245322        // re-measure the PSF for the smoothed image (using entries in 'allSources')
     
    253330
    254331        // measure circular, radial apertures (objects sorted by S/N)
    255         psphotRadialAperturesByObject (config, objectsRadial, view, STACK_OUT, nMatchedPSF);
     332        // entry 0 == unmatched? pass entry + 1?
     333        psphotRadialApertures (config, view, STACK_OUT, entry);
    256334
    257335        // replace the flux in the image so it is returned to its original state
    258         psphotReplaceAllSources (config, view, STACK_OUT);
     336        psphotReplaceAllSources (config, view, STACK_OUT, false);
    259337
    260338        // smooth to the next FWHM, or set 'smoothAgain' to false if no more
    261         psphotStackMatchPSFsNext(&smoothAgain, config, view, STACK_OUT, nMatchedPSF);
     339        psphotStackMatchPSFsNext(config, view, STACK_OUT, entry);
    262340        psMemDump("matched");
    263341    }
    264342
    265     if (0 && !psphotEfficiency(config, view, STACK_OUT)) {
     343    // measure aperture photometry corrections
     344    if (!psphotApResid (config, view, STACK_SRC)) {
     345        psFree (objects);
     346        psLogMsg ("psphot", 3, "failed on psphotApResid");
     347        return psphotReadoutCleanup (config, view, STACK_SRC);
     348    }
     349
     350    // calculate source magnitudes
     351    psphotMagnitudes(config, view, STACK_SRC);
     352
     353    // XXX NOTE: this function wants to have the PSF of the image, but we (so far) only measure the
     354    // PSF of the SRC image.  can we fake it by generating the PSF for DET as well (up above)?
     355    if (false && !psphotEfficiency(config, view, STACK_DET)) {
    266356        psErrorStackPrint(stderr, "Unable to determine detection efficiencies from fake sources");
    267357        psErrorClear();
Note: See TracChangeset for help on using the changeset viewer.