IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 32695 for trunk/psphot


Ignore:
Timestamp:
Nov 17, 2011, 2:17:35 PM (15 years ago)
Author:
eugene
Message:

fix psphotStack 2nd pass (subtract 1st pass sources from the correct image); fix PS color plotting; plug a leak in psphotStack

Location:
trunk
Files:
14 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/psphot

  • trunk/psphot/src/psphot.h

    r32633 r32695  
    108108bool            psphotBlendFit_Threaded (psThreadJob *job);
    109109
    110 bool            psphotReplaceAllSources (pmConfig *config, const pmFPAview *view, const char *filerule);
    111 bool            psphotReplaceAllSourcesReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe);
     110bool            psphotReplaceAllSources (pmConfig *config, const pmFPAview *view, const char *filerule, bool ignoreState);
     111bool            psphotReplaceAllSourcesReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe, bool ignoreState);
    112112
    113113bool            psphotAddNoise (pmConfig *config, const pmFPAview *view, const char *filerule);
     
    176176
    177177// in psphotReplaceUnfit.c:
    178 bool            psphotRemoveAllSources (const psArray *sources, const psMetadata *recipe);
     178bool            psphotRemoveAllSourcesByArray (const psArray *sources, const psMetadata *recipe);
     179bool            psphotRemoveAllSources (pmConfig *config, const pmFPAview *view, const char *filerule, bool ignoreState);
     180bool            psphotRemoveAllSourcesReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe, bool ignoreState);
    179181bool            psphotReplaceUnfitSources (psArray *sources, psImageMaskType maskVal);
    180182
     
    453455psArray *psphotSourceChildrenByObject (pmConfig *config, const pmFPAview *view, const char *filerule, psArray *objectsSrc);
    454456
     457bool psphotSourceParents (pmConfig *config, const pmFPAview *view, const char *ruleOut, const char *ruleSrc);
     458bool psphotSourceParentsReadout (pmConfig *config, const pmFPAview *view, const char *ruleOut, const char *ruleSrc, int index);
     459
     460bool psphotCopyPeaks (pmConfig *config, const pmFPAview *view, const char *ruleOut, const char *ruleSrc);
     461bool psphotCopyPeaksReadout (pmConfig *config, const pmFPAview *view, const char *ruleOut, const char *ruleSrc, int index);
     462
    455463bool psphotSersicModelClassGuessPCM (pmPCMdata *pcm, pmSource *source);
    456464void psphotSersicModelClassInit ();
  • trunk/psphot/src/psphotBlendFit.c

    r32348 r32695  
    236236        pmSource *source = sources->data[i];
    237237
    238 # define TEST_X -420.0
    239 # define TEST_Y 300.0
     238# if (0)
     239# define TEST_X 34
     240# define TEST_Y 28
    240241   
    241242        if ((fabs(source->peak->xf - TEST_X) < 5) && (fabs(source->peak->yf - TEST_Y) < 5)) {
    242             fprintf (stderr, "test galaxy\n");
     243            fprintf (stderr, "test object\n");
    243244        }
    244245
    245246# undef TEST_X
    246247# undef TEST_Y
     248# endif
    247249
    248250        // skip non-astronomical objects (very likely defects)
  • trunk/psphot/src/psphotEfficiency.c

    r32348 r32695  
    255255
    256256    // remove all sources, adding noise for subtracted sources
    257     psphotRemoveAllSources(realSources, recipe);
     257    psphotRemoveAllSourcesByArray(realSources, recipe);
    258258
    259259#if TESTING
  • trunk/psphot/src/psphotFake.c

    r26894 r32695  
    166166
    167167    // remove all sources, adding noise for subtracted sources
    168     psphotRemoveAllSources(realSources, recipe);
     168    psphotRemoveAllSourcesByArray(realSources, recipe);
    169169    psphotAddNoise(readout, realSources, recipe);
    170170
  • trunk/psphot/src/psphotFitSourcesLinear.c

    r32633 r32695  
    342342    psLogMsg ("psphot.ensemble", PS_LOG_MINUTIA, "solve matrix: %f sec (%d elements)\n", psTimerMark ("psphot.linear"), sparse->Nelem);
    343343
    344     // XXXX **** philosophical question:
    345     // we measure bright objects in three passes: 1) linear fit; 2) non-linear fit; 3) linear fit:
    346     // should retain the chisq and errors from the intermediate non-linear fit?
    347     // the non-linear fit provides better values for the position errors, and for
    348     // extended sources, the shape errors
     344    // Philosophical question: we measure bright objects in three passes: 1) linear fit; 2)
     345    // non-linear fit; 3) linear fit: should we retain the chisq and errors from the
     346    // intermediate non-linear fit?  the non-linear fit provides better values for the position
     347    // errors, and for extended sources, the shape errors
    349348
    350349    // adjust I0 for fitSources and subtract
  • trunk/psphot/src/psphotMergeSources.c

    r31452 r32695  
    520520}
    521521
     522// copy the newPeaks from the detections of one pmFPAfile to another
     523bool psphotCopyPeaks (pmConfig *config, const pmFPAview *view, const char *ruleOut, const char *ruleSrc)
     524{
     525    bool status = true;
     526
     527    int num = psphotFileruleCount(config, ruleSrc);
     528
     529    // skip the chisq image because it is a duplicate of the detection version
     530    int chisqNum = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.CHISQ.NUM");
     531    if (!status) chisqNum = -1;
     532
     533    // loop over the available readouts
     534    for (int i = 0; i < num; i++) {
     535        if (i == chisqNum) continue; // skip chisq image
     536        if (!psphotCopyPeaksReadout (config, view, ruleOut, ruleSrc, i)) {
     537            psError (PSPHOT_ERR_CONFIG, false, "failed to copy peaks from %s to %s entry %d", ruleSrc, ruleOut, i);
     538            return false;
     539        }
     540    }
     541    return true;
     542}
     543
     544// add newly detected peaks to the existing list of sources
     545bool psphotCopyPeaksReadout (pmConfig *config, const pmFPAview *view, const char *ruleOut, const char *ruleSrc, int index) {
     546
     547    bool status;
     548
     549    // find the currently selected readout
     550    pmFPAfile *fileSrc = pmFPAfileSelectSingle(config->files, ruleSrc, index); // File of interest
     551    psAssert (fileSrc, "missing file?");
     552
     553    pmReadout *readoutSrc = pmFPAviewThisReadout(view, fileSrc->fpa);
     554    psAssert (readoutSrc, "missing readout?");
     555
     556    pmDetections *detections = psMetadataLookupPtr (&status, readoutSrc->analysis, "PSPHOT.DETECTIONS");
     557    psAssert (detections, "missing detections?");
     558
     559    // find the currently selected readout
     560    pmFPAfile *fileOut = pmFPAfileSelectSingle(config->files, ruleOut, index); // File of interest
     561    psAssert (fileOut, "missing file?");
     562
     563    pmReadout *readoutOut = pmFPAviewThisReadout(view, fileOut->fpa);
     564    psAssert (readoutOut, "missing readout?");
     565
     566    // generate a new detection structure for the output filerule
     567    pmDetections *detectionsOut = psMetadataLookupPtr (&status, readoutOut->analysis, "PSPHOT.DETECTIONS");
     568    psAssert (detectionsOut, "missing PSPHOT.DETECTIONS?");
     569
     570    psAssert (detectionsOut->peaks, "programming error");
     571    psAssert (!detectionsOut->oldPeaks, "programming error");
     572    psAssert (detections->peaks, "programming error");
     573
     574    // save the OUT existing peaks on oldPeaks
     575    detectionsOut->oldPeaks = detectionsOut->peaks;
     576    detectionsOut->peaks = psArrayAllocEmpty(detections->peaks->n);
     577
     578    for (int i = 0; i < detections->peaks->n; i++) {
     579        psAssert (detections->peaks->data[i], "programming error");
     580        pmPeak *peak = pmPeakAlloc (0, 0, 0.0, PM_PEAK_LONE);
     581        pmPeakCopy(peak, detections->peaks->data[i]);
     582        psArrayAdd (detectionsOut->peaks, 100, peak);
     583        psFree (peak);
     584    }
     585    return true;
     586}
     587
     588// create source parents children from ruleSrc for ruleOut for orphans
     589bool psphotSourceParents (pmConfig *config, const pmFPAview *view, const char *ruleOut, const char *ruleSrc)
     590{
     591    bool status = true;
     592
     593    int num = psphotFileruleCount(config, ruleSrc);
     594
     595    // skip the chisq image because it is a duplicate of the detection version
     596    int chisqNum = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.CHISQ.NUM");
     597    if (!status) chisqNum = -1;
     598
     599    // loop over the available readouts
     600    for (int i = 0; i < num; i++) {
     601        if (i == chisqNum) continue; // skip chisq image
     602        if (!psphotSourceParentsReadout (config, view, ruleOut, ruleSrc, i)) {
     603            psError (PSPHOT_ERR_CONFIG, false, "failed to copy sources from %s to %s entry %d", ruleSrc, ruleOut, i);
     604            return false;
     605        }
     606    }
     607    return true;
     608}
     609
     610// create source parents from ruleSrc for ruleOut for orphaned children for this readout. 
     611bool psphotSourceParentsReadout (pmConfig *config, const pmFPAview *view, const char *ruleOut, const char *ruleSrc, int index) {
     612
     613    bool status;
     614    int nParents = 0;
     615    int nNonOrphans = 0;
     616
     617    // find the currently selected readout
     618    pmFPAfile *fileSrc = pmFPAfileSelectSingle(config->files, ruleSrc, index); // File of interest
     619    psAssert (fileSrc, "missing file?");
     620
     621    pmReadout *readoutSrc = pmFPAviewThisReadout(view, fileSrc->fpa);
     622    psAssert (readoutSrc, "missing readout?");
     623
     624    pmDetections *detectionsSrc = psMetadataLookupPtr (&status, readoutSrc->analysis, "PSPHOT.DETECTIONS");
     625    psAssert (detectionsSrc, "missing detections?");
     626
     627    psArray *sourcesSrc = detectionsSrc->allSources;
     628    psAssert (sourcesSrc, "missing sources?");
     629
     630    // find the currently selected readout
     631    pmFPAfile *fileOut = pmFPAfileSelectSingle(config->files, ruleOut, index); // File of interest
     632    psAssert (fileOut, "missing file?");
     633
     634    pmReadout *readoutOut = pmFPAviewThisReadout(view, fileOut->fpa);
     635    psAssert (readoutOut, "missing readout?");
     636
     637    // generate a new detection structure for the output filerule
     638    pmDetections *detectionsOut = psMetadataLookupPtr (&status, readoutOut->analysis, "PSPHOT.DETECTIONS");
     639    psAssert (detectionsOut, "missing PSPHOT.DETECTIONS?");
     640
     641    // loop over the sources, redefine their pixels to point at the new filerule image,
     642    // copy the source data, and add a reference back to the original source
     643   
     644    // copy the sources from sourceSrcs to the new detection structure
     645    for (int i = 0; i < sourcesSrc->n; i++) {
     646      pmSource *sourceSrc = sourcesSrc->data[i];
     647      if (sourceSrc->parent) {
     648          nNonOrphans ++;
     649          continue; // Not an orphan
     650      }
     651
     652      pmSource *sourceOut = pmSourceCopy(sourceSrc);
     653      sourceOut->parent = sourceSrc;
     654     
     655      // keep the original source flags
     656      sourceOut->seq      = sourceSrc->seq;
     657      sourceOut->type     = sourceSrc->type;
     658      sourceOut->mode     = sourceSrc->mode;
     659      sourceOut->mode2    = sourceSrc->mode2;
     660      sourceOut->tmpFlags = sourceSrc->tmpFlags;
     661
     662      // does this copy all model data? (NO)
     663      sourceOut->modelPSF = pmModelCopy(sourceSrc->modelPSF);
     664      sourceOut->modelEXT = pmModelCopy(sourceSrc->modelEXT);
     665
     666      if (sourceSrc->modelFits) {
     667          sourceOut->modelFits = psArrayAlloc(sourceSrc->modelFits->n);
     668          for (int j = 0; j < sourceSrc->modelFits->n; j++) {
     669              sourceOut->modelFits->data[j] = pmModelCopy(sourceSrc->modelFits->data[j]);
     670          }
     671      }
     672
     673      // drop the references to the original image pixels:
     674      pmSourceFreePixels (sourceOut);
     675
     676      // allocate image, weight, mask for the new image for each peak
     677      if (sourceOut->modelPSF) {
     678        pmSourceRedefinePixels (sourceOut, readoutOut, sourceOut->peak->x, sourceOut->peak->y, sourceOut->modelPSF->fitRadius);
     679      }
     680
     681      // child sources have not been subtracted in this image, but this flag may be raised if
     682      // they were subtracted in the parent's image
     683      sourceOut->tmpFlags &= ~PM_SOURCE_TMPF_SUBTRACTED;
     684
     685      nParents ++;
     686      psArrayAdd (detectionsOut->allSources, 100, sourceOut);
     687      psFree (sourceOut);
     688    }
     689    psLogMsg ("psphot", 3, "%d parents created, %d unorphaned children, %ld input vs %ld output", nParents, nNonOrphans, sourcesSrc->n, detectionsOut->allSources->n);
     690
     691    return true;
     692}
     693
    522694// create source children from ruleSrc for ruleOut
    523695bool psphotSourceChildren (pmConfig *config, const pmFPAview *view, const char *ruleOut, const char *ruleSrc)
     
    542714}
    543715
    544 // create source children from ruleSrc for ruleOut for this entry.  XXX currently, this is only
     716// Create source children from ruleSrc for ruleOut for this entry.  Currently, this is only
    545717// used by psphotStackReadout (sources go on allSources so that psphotChoosePSF can be called
    546 // repeatedly)
     718// repeatedly).
    547719bool psphotSourceChildrenReadout (pmConfig *config, const pmFPAview *view, const char *ruleOut, const char *ruleSrc, int index) {
    548720
     
    569741    psAssert (readoutOut, "missing readout?");
    570742
    571     // generate a new detection structure for the output filerule
    572     pmDetections *detectionsOut = psMetadataLookupPtr (&status, readoutOut->analysis, "PSPHOT.DETECTIONS");
    573     if (!detectionsOut) {
    574         detectionsOut = pmDetectionsAlloc();
    575         detectionsOut->allSources = psArrayAllocEmpty (100);
    576         // save detections on the readout->analysis
    577         if (!psMetadataAddPtr (readoutOut->analysis, PS_LIST_TAIL, "PSPHOT.DETECTIONS", PS_META_REPLACE | PS_DATA_UNKNOWN, "psphot detections", detectionsOut)) {
    578             psError (PSPHOT_ERR_CONFIG, false, "problem saving detections on readout");
    579             return false;
    580         }
    581         psFree(detectionsOut); // a copy remains on the analysis metadata
     743    // replace any existing DETECTION container on readoutOut->analysis with the new one
     744    pmDetections *detectionsOut = pmDetectionsAlloc();
     745    detectionsOut->allSources = psArrayAllocEmpty (100);
     746    if (!psMetadataAddPtr (readoutOut->analysis, PS_LIST_TAIL, "PSPHOT.DETECTIONS", PS_META_REPLACE | PS_DATA_UNKNOWN, "psphot detections", detectionsOut)) {
     747        psError (PSPHOT_ERR_CONFIG, false, "problem saving detections on readout");
     748        return false;
    582749    }
    583750
     
    593760     
    594761      // keep the original source flags
     762      sourceOut->seq      = sourceSrc->seq;
    595763      sourceOut->type     = sourceSrc->type;
    596764      sourceOut->mode     = sourceSrc->mode;
     
    612780      pmSourceFreePixels (sourceOut);
    613781
     782      // XXX do we need to skip the Chisq image sources?
     783
    614784      // allocate image, weight, mask for the new image for each peak
    615       if (sourceOut->modelPSF) {
    616         pmSourceRedefinePixels (sourceOut, readoutOut, sourceOut->peak->x, sourceOut->peak->y, sourceOut->modelPSF->fitRadius);
    617       }
     785      pmSourceRedefinePixels (sourceOut, readoutOut, sourceOut->peak->x, sourceOut->peak->y, sourceOut->windowRadius);
    618786
    619787      // child sources have not been subtracted in this image, but this flag may be raised if
     
    624792      psFree (sourceOut);
    625793    }
    626     psLogMsg ("psphot", 3, "%ld known sources supplied", detectionsOut->allSources->n);
    627 
    628 
    629     return true;
    630 }
    631 
    632 // create source children associated with 'filerule' from the objectsSrc.  XXX currently, this
    633 // is only used by psphotStackReadout (sources go on allSources so that psphotChoosePSF can be
    634 // called repeatedly)
     794    psLogMsg ("psphot", 3, "created %ld children", detectionsOut->allSources->n);
     795
     796    psFree(detectionsOut); // a copy remains on the analysis metadata
     797
     798    return true;
     799}
     800
     801// create source children associated with 'filerule' from the objectsSrc.  returns a new object
     802// array containing the child sources.  XXX currently, this is only used by psphotStackReadout
     803// (sources go on allSources so that psphotChoosePSF can be called repeatedly)
    635804psArray *psphotSourceChildrenByObject (pmConfig *config, const pmFPAview *view, const char *filerule, psArray *objectsSrc) {
    636805
     
    652821        psAssert (readout, "missing readout?");
    653822
     823        // create DETECTIONS containers for each image, in case one lacks it
    654824        pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
    655825        if (!detections) {
     
    665835            psAssert (detections, "missing detections?");
    666836        }
     837
     838        // we need to save the new sources on the detection arrays of the appropriate image
    667839        detArrays->data[i] = psMemIncrRefCounter(detections);
    668840        readouts->data[i] = psMemIncrRefCounter(readout);
     
    695867
    696868            pmSource *sourceOut = pmSourceCopy(sourceSrc);
     869            sourceOut->parent = sourceSrc;
     870
     871            // save on the output object array at the same location
    697872            objectOut->sources->data[i] = sourceOut;
    698 
    699             sourceOut->parent = sourceSrc;
    700873
    701874            // keep the original source flags and sequence ID (if set)
     
    720893            pmSourceFreePixels (sourceOut);
    721894
    722             // set the output readotu
     895            // set the output readout
    723896            int index = sourceOut->imageID;
    724897            if (index >= readouts->n) continue; // skip the sources generated by the chisq image
  • trunk/psphot/src/psphotOutput.c

    r32348 r32695  
    407407    return true;
    408408}
     409
     410// for now, let's store the detections on the readout->analysis for each readout
     411bool psphotDumpTest (pmConfig *config, const pmFPAview *view, const char *filerule)
     412{
     413    static int npass = 0;
     414    char filename[64];
     415
     416    // XXX dump tests are disabled unless this is commented out:
     417    return true;
     418
     419    bool status = true;
     420
     421    int num = psphotFileruleCount(config, filerule);
     422
     423    snprintf (filename, 64, "testdump.%02d.dat", npass);
     424    FILE *f = fopen (filename, "w");
     425
     426    // loop over the available readouts
     427    for (int i = 0; i < num; i++) {
     428
     429        // find the currently selected readout
     430        pmFPAfile *file = pmFPAfileSelectSingle(config->files, filerule, i); // File of interest
     431        psAssert (file, "missing file?");
     432
     433        pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
     434        psAssert (readout, "missing readout?");
     435
     436        pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
     437        psAssert (detections, "missing detections?");
     438
     439        psArray *sources = detections->newSources ? detections->newSources : detections->allSources;
     440        psAssert (sources, "missing sources?");
     441
     442        if (detections->newSources) {
     443            fprintf (f, "## --- from new sources ---\n");
     444        } else {
     445            fprintf (f, "## --- from all sources ---\n");
     446        }
     447
     448        for (int i = 0; i < sources->n; i++) {
     449            pmSource *source = sources->data[i];
     450            if (!source) continue;
     451
     452            pmPeak *peak = source->peak;
     453            if (!peak) continue;
     454
     455            // XXX only dump a given region
     456            // if (peak->xf < 20) continue;
     457            // if (peak->yf < 20) continue;
     458            // if (peak->xf > 40) continue;
     459            // if (peak->yf > 70) continue;
     460
     461            float Msum = source->moments ? source->moments->Sum : NAN;
     462            float Mx   = source->moments ? source->moments->Mx : NAN;
     463            float My   = source->moments ? source->moments->My : NAN;
     464            // float Npix = source->moments ? source->moments->nPixels : NAN;
     465            float Io = source->modelPSF ? source->modelPSF->params->data.F32[PM_PAR_I0] : NAN;
     466            fprintf (f, "%d %f %f  : %f %f : %f %f\n", source->imageID, peak->xf, peak->yf, Mx, My, Msum, Io);
     467        }
     468    }
     469    fclose (f);
     470    npass ++;
     471
     472    return true;
     473}
     474
     475# if (0)
    409476bool psphotDumpTest (pmConfig *config, const pmFPAview *view, const char *filerule, char *filename) {
    410477
     
    449516    return true;
    450517}
    451 
     518# endif
  • trunk/psphot/src/psphotReadout.c

    r32633 r32695  
    99}
    1010
    11 // for now, let's store the detections on the readout->analysis for each readout
    12 bool psphotDumpChisqs (pmConfig *config, const pmFPAview *view, const char *filerule)
    13 {
    14     static int npass = 0;
    15     char filename[64];
    16 
    17     return true;
    18 
    19     bool status = true;
    20 
    21     int num = psphotFileruleCount(config, filerule);
    22 
    23     snprintf (filename, 64, "chisq.%02d.dat", npass);
    24     FILE *f = fopen (filename, "w");
    25 
    26     // loop over the available readouts
    27     for (int i = 0; i < num; i++) {
    28 
    29         // find the currently selected readout
    30         pmFPAfile *file = pmFPAfileSelectSingle(config->files, filerule, i); // File of interest
    31         psAssert (file, "missing file?");
    32 
    33         pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
    34         psAssert (readout, "missing readout?");
    35 
    36         pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
    37         psAssert (detections, "missing detections?");
    38 
    39         psArray *sources = detections->allSources;
    40         psAssert (sources, "missing sources?");
    41 
    42         for (int i = 0; i < sources->n; i++) {
    43             pmSource *source = sources->data[i];
    44             if (!source) continue;
    45 
    46             pmModel *model = pmSourceGetModel (NULL, source);
    47             if (!model) continue;
    48        
    49             if (source->mode & PM_SOURCE_MODE_NONLINEAR_FIT) {
    50                 fprintf (f, "%f %f %f %d %d %f  1 NONLINEAR\n", model->mag, model->params->data.F32[1], model->chisq, model->nDOF, model->nPix, model->chisqNorm);
    51             } else {
    52                 fprintf (f, "%f %f %f %d %d %f  0 LINEAR\n", model->mag, model->params->data.F32[1], model->chisq, model->nDOF, model->nPix, model->chisqNorm);
    53             }
    54         }
    55     }
    56     fclose (f);
    57     npass ++;
    58 
    59     return true;
    60 }
    61 
    6211bool psphotReadout(pmConfig *config, const pmFPAview *view, const char *filerule) {
    6312
     
    188137    // linear PSF fit to source peaks, subtract the models from the image (in PSF mask)
    189138    psphotFitSourcesLinear (config, view, filerule, false); // pass 1 (detections->allSources)
    190     psphotDumpChisqs (config, view, filerule);
    191139
    192140    // measure the radial profiles to the sky
     
    208156    // replace model flux, adjust mask as needed, fit, subtract the models (full stamp)
    209157    psphotBlendFit (config, view, filerule); // pass 1 (detections->allSources)
    210     psphotDumpChisqs (config, view, filerule);
    211158
    212159    // replace all sources
    213     psphotReplaceAllSources (config, view, filerule); // pass 1 (detections->allSources)
     160    psphotReplaceAllSources (config, view, filerule, false); // pass 1 (detections->allSources)
    214161
    215162    // linear fit to include all sources (subtract again)
    216163    // NOTE : apply to ALL sources (extended + psf)
    217164    psphotFitSourcesLinear (config, view, filerule, true); // pass 2 (detections->allSources)
    218     psphotDumpChisqs (config, view, filerule);
    219165
    220166    // if we only do one pass, skip to extended source analysis
     
    253199        // replace all sources so fit below applies to all at once
    254200        // NOTE: apply only to OLD sources (which have been subtracted)
    255         psphotReplaceAllSources (config, view, filerule); // pass 2
     201        psphotReplaceAllSources (config, view, filerule, false); // pass 2
    256202
    257203        // merge the newly selected sources into the existing list
     
    262208        // NOTE: apply to ALL sources
    263209        psphotFitSourcesLinear (config, view, filerule, true); // pass 3 (detections->allSources)
    264         psphotDumpChisqs (config, view, filerule);
    265210    }
    266211
     
    295240        // replace all sources so fit below applies to all at once
    296241        // NOTE: apply only to OLD sources (which have been subtracted)
    297         psphotReplaceAllSources (config, view, filerule); // pass 2
     242        psphotReplaceAllSources (config, view, filerule, false); // pass 2
    298243
    299244        // merge the newly selected sources into the existing list
  • trunk/psphot/src/psphotReplaceUnfit.c

    r31452 r32695  
    99
    1010    for (int i = 0; i < sources->n; i++) {
    11       source = sources->data[i];
    12 
    13       // replace other sources?
    14       if (source->mode & PM_SOURCE_MODE_FAIL) goto replace;
    15       continue;
     11        source = sources->data[i];
     12
     13        // replace other sources?
     14        if (source->mode & PM_SOURCE_MODE_FAIL) goto replace;
     15        continue;
    1616
    1717    replace:
     
    2323
    2424// for now, let's store the detections on the readout->analysis for each readout
    25 bool psphotReplaceAllSources (pmConfig *config, const pmFPAview *view, const char *filerule)
     25bool psphotReplaceAllSources (pmConfig *config, const pmFPAview *view, const char *filerule, bool ignoreState)
    2626{
    2727    bool status = true;
     
    3535    // loop over the available readouts
    3636    for (int i = 0; i < num; i++) {
    37         if (!psphotReplaceAllSourcesReadout (config, view, filerule, i, recipe)) {
     37        if (!psphotReplaceAllSourcesReadout (config, view, filerule, i, recipe, ignoreState)) {
    3838            psError (PSPHOT_ERR_CONFIG, false, "failed to replace all sources for %s entry %d", filerule, i);
    3939            return false;
     
    4343}
    4444
    45 bool psphotReplaceAllSourcesReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe) {
    46 
    47     bool status;
    48     pmSource *source;
     45bool psphotReplaceAllSourcesReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe, bool ignoreState) {
     46
     47    bool status;
    4948
    5049    psTimerStart ("psphot.replace");
     
    7574
    7675    for (int i = 0; i < sources->n; i++) {
    77       source = sources->data[i];
    78 
    79       // replace other sources?
    80       if (!(source->tmpFlags & PM_SOURCE_TMPF_SUBTRACTED)) continue;
    81 
    82       pmSourceAdd (source, PM_MODEL_OP_FULL, maskVal);
     76        pmSource *source = sources->data[i];
     77
     78        if (ignoreState) {
     79            // rely on the type of source to decide if we subtract it or not
     80
     81            // skip non-astronomical objects (very likely defects)
     82            if (source->type == PM_SOURCE_TYPE_DEFECT) continue;
     83            if (source->type == PM_SOURCE_TYPE_SATURATED) continue;
     84     
     85            // do not include CRs in the full ensemble fit
     86            if (source->mode & PM_SOURCE_MODE_CR_LIMIT) continue;
     87       
     88            // do not include MOMENTS_FAILURES in the fit
     89            if (source->mode & PM_SOURCE_MODE_MOMENTS_FAILURE) continue;
     90        } else {
     91            // if we respect the state, do not replace unsubtracted sources
     92            if (!(source->tmpFlags & PM_SOURCE_TMPF_SUBTRACTED)) continue;
     93        }
     94
     95        pmSourceAdd (source, PM_MODEL_OP_FULL, maskVal);
    8396    }
    8497
     
    88101}
    89102
    90 bool psphotRemoveAllSources (const psArray *sources, const psMetadata *recipe) {
     103// for now, let's store the detections on the readout->analysis for each readout
     104bool psphotRemoveAllSources (pmConfig *config, const pmFPAview *view, const char *filerule, bool ignoreState)
     105{
     106    bool status = true;
     107
     108    // select the appropriate recipe information
     109    psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes, PSPHOT_RECIPE);
     110    psAssert (recipe, "missing recipe?");
     111
     112    int num = psphotFileruleCount(config, filerule);
     113
     114    // loop over the available readouts
     115    for (int i = 0; i < num; i++) {
     116        if (!psphotRemoveAllSourcesReadout (config, view, filerule, i, recipe, ignoreState)) {
     117            psError (PSPHOT_ERR_CONFIG, false, "failed to replace all sources for %s entry %d", filerule, i);
     118            return false;
     119        }
     120    }
     121    return true;
     122}
     123
     124bool psphotRemoveAllSourcesReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe, bool ignoreState) {
     125
     126    bool status;
     127
     128    psTimerStart ("psphot.replace");
     129
     130    // find the currently selected readout
     131    pmFPAfile *file = pmFPAfileSelectSingle(config->files, filename, index); // File of interest
     132    psAssert (file, "missing file?");
     133
     134    pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
     135    psAssert (readout, "missing readout?");
     136
     137    pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
     138    psAssert (detections, "missing detections?");
     139
     140    psArray *sources = detections->allSources;
     141    psAssert (sources, "missing sources?");
     142
     143    // user-defined masks to test for good/bad pixels (build from recipe list if not yet set)
     144    psImageMaskType maskVal = psMetadataLookupImageMask(&status, recipe, "MASK.PSPHOT"); // Mask value for bad pixels
     145    psAssert (maskVal, "missing mask value?");
     146
     147    // bit-mask to mark pixels not used in analysis
     148    psImageMaskType markVal = psMetadataLookupImageMask(&status, recipe, "MARK.PSPHOT");
     149    assert (markVal);
     150
     151    // maskVal is used to test for rejected pixels, and must include markVal
     152    maskVal |= markVal;
     153
     154    for (int i = 0; i < sources->n; i++) {
     155        pmSource *source = sources->data[i];
     156
     157        if (ignoreState) {
     158            // rely on the type of source to decide if we subtract it or not
     159
     160            // skip non-astronomical objects (very likely defects)
     161            if (source->type == PM_SOURCE_TYPE_DEFECT) continue;
     162            if (source->type == PM_SOURCE_TYPE_SATURATED) continue;
     163     
     164            // do not include CRs in the full ensemble fit
     165            if (source->mode & PM_SOURCE_MODE_CR_LIMIT) continue;
     166       
     167            // do not include MOMENTS_FAILURES in the fit
     168            if (source->mode & PM_SOURCE_MODE_MOMENTS_FAILURE) continue;
     169        } else {
     170            // if we respect the state, only remove unsubtracted sources
     171            if ((source->tmpFlags & PM_SOURCE_TMPF_SUBTRACTED)) continue;
     172        }
     173
     174        pmSourceSub (source, PM_MODEL_OP_FULL, maskVal);
     175    }
     176
     177    psphotVisualShowImage(readout);
     178    psLogMsg ("psphot.replace", PS_LOG_INFO, "replaced models for %ld objects: %f sec\n", sources->n, psTimerMark ("psphot.replace"));
     179    return true;
     180}
     181
     182bool psphotRemoveAllSourcesByArray (const psArray *sources, const psMetadata *recipe) {
    91183
    92184    bool status;
     
    100192
    101193    for (int i = 0; i < sources->n; i++) {
    102       source = sources->data[i];
    103 
    104       // replace other sources?
    105       if (source->tmpFlags & PM_SOURCE_TMPF_SUBTRACTED) continue;
    106 
    107       pmSourceSub (source, PM_MODEL_OP_FULL, maskVal);
     194        source = sources->data[i];
     195
     196        // replace other sources?
     197        if (source->tmpFlags & PM_SOURCE_TMPF_SUBTRACTED) continue;
     198
     199        pmSourceSub (source, PM_MODEL_OP_FULL, maskVal);
    108200    }
    109201    psLogMsg ("psphot.replace", PS_LOG_INFO, "replaced models for %ld objects: %f sec\n", sources->n, psTimerMark ("psphot.replace"));
     
    158250
    159251    for (int i = 0; i < sources->n; i++) {
    160       source = sources->data[i];
    161 
    162       // sources have not yet been subtracted in this image (but this flag may be raised)
    163       source->tmpFlags &= ~PM_SOURCE_TMPF_SUBTRACTED;
    164       if (!source->modelPSF) continue;
    165 
    166       float Xo = source->modelPSF->params->data.F32[PM_PAR_XPOS];
    167       float Yo = source->modelPSF->params->data.F32[PM_PAR_YPOS];
    168       float radius = source->modelPSF->fitRadius;
    169 
    170       // force a redefine to this image
    171       pmSourceFreePixels(source);
    172       pmSourceRedefinePixels (source, readout, Xo, Yo, radius);
     252        source = sources->data[i];
     253
     254        // sources have not yet been subtracted in this image (but this flag may be raised)
     255        source->tmpFlags &= ~PM_SOURCE_TMPF_SUBTRACTED;
     256        if (!source->modelPSF) continue;
     257
     258        float Xo = source->modelPSF->params->data.F32[PM_PAR_XPOS];
     259        float Yo = source->modelPSF->params->data.F32[PM_PAR_YPOS];
     260        float radius = source->modelPSF->fitRadius;
     261
     262        // force a redefine to this image
     263        pmSourceFreePixels(source);
     264        pmSourceRedefinePixels (source, readout, Xo, Yo, radius);
    173265    }
    174266    return true;
     
    229321
    230322    for (int i = 0; i < sources->n; i++) {
    231       source = sources->data[i];
    232 
    233       // *** we need to cache the 'best' model, and we have 3 cases:
    234       // 1) model is the psf model --> generate from the new psf
    235       // 2) model is an unconvolved extended model --> just cache the copy (not perfect)
    236       // 3) model is a convolved extended model --> re-generate
    237 
    238       // use the 'best' model to cache the model (PSF or EXT : EXT may point at one of modelFits
    239       bool isPSF = false;
    240       pmModel *model = pmSourceGetModel(&isPSF, source);
    241       if (!model) continue;
    242 
    243       float radius = model->fitRadius; // save for future use below
    244 
    245       // regenerate the PSF if the model is a PSF, or if we need the PSF for a PCM
    246       if (isPSF || model->isPCM) {
    247           // the guess central intensity comes from the peak:
    248           float Io = source->peak->rawFlux;
    249           float Xo = source->modelPSF->params->data.F32[PM_PAR_XPOS];
    250           float Yo = source->modelPSF->params->data.F32[PM_PAR_YPOS];
    251 
    252           // generate a model for this object with Io = 1.0
    253           pmModel *modelPSF = pmModelFromPSFforXY(psf, Xo, Yo, Io);
    254           if (modelPSF == NULL) {
    255               psWarning ("Failed to determine PSF model for source at (%f,%f), skipping", Xo, Yo);
    256               continue;
    257           }
    258 
    259           // set the source PSF model
    260           psFree (source->modelPSF);
    261           source->modelPSF = modelPSF;
    262           source->modelPSF->fitRadius = radius;
    263       }
    264 
    265       if (model->isPCM) {
    266           psAssert(false, "this section is not complete");
    267 
    268           pmSourceCachePSF (source, maskVal);
    269 
    270           psKernel *psfKernel = pmPCMkernelFromPSF(source, psfSize);
    271           if (!psfKernel) {
    272               psWarning ("no psf kernel");
    273           }
    274 
    275           // generate an image of the right size
    276           psImage *rawModelFlux = psImageCopy (NULL, source->pixels, PS_TYPE_F32);
    277           psImageInit (rawModelFlux, 0.0);
     323        source = sources->data[i];
     324
     325        // *** we need to cache the 'best' model, and we have 3 cases:
     326        // 1) model is the psf model --> generate from the new psf
     327        // 2) model is an unconvolved extended model --> just cache the copy (not perfect)
     328        // 3) model is a convolved extended model --> re-generate
     329
     330        // use the 'best' model to cache the model (PSF or EXT : EXT may point at one of modelFits
     331        bool isPSF = false;
     332        pmModel *model = pmSourceGetModel(&isPSF, source);
     333        if (!model) continue;
     334
     335        float radius = model->fitRadius; // save for future use below
     336
     337        // regenerate the PSF if the model is a PSF, or if we need the PSF for a PCM
     338        if (isPSF || model->isPCM) {
     339            // the guess central intensity comes from the peak:
     340            float Io = source->peak->rawFlux;
     341            float Xo = source->modelPSF->params->data.F32[PM_PAR_XPOS];
     342            float Yo = source->modelPSF->params->data.F32[PM_PAR_YPOS];
     343
     344            // generate a model for this object with Io = 1.0
     345            pmModel *modelPSF = pmModelFromPSFforXY(psf, Xo, Yo, Io);
     346            if (modelPSF == NULL) {
     347                psWarning ("Failed to determine PSF model for source at (%f,%f), skipping", Xo, Yo);
     348                continue;
     349            }
     350
     351            // set the source PSF model
     352            psFree (source->modelPSF);
     353            source->modelPSF = modelPSF;
     354            source->modelPSF->fitRadius = radius;
     355        }
     356
     357        if (model->isPCM) {
     358            psAssert(false, "this section is not complete");
     359
     360            pmSourceCachePSF (source, maskVal);
     361
     362            psKernel *psfKernel = pmPCMkernelFromPSF(source, psfSize);
     363            if (!psfKernel) {
     364                psWarning ("no psf kernel");
     365            }
     366
     367            // generate an image of the right size
     368            psImage *rawModelFlux = psImageCopy (NULL, source->pixels, PS_TYPE_F32);
     369            psImageInit (rawModelFlux, 0.0);
    278370         
    279           // insert the model image normalized to 1.0
    280           pmModelAdd (rawModelFlux, NULL, model, PM_MODEL_OP_FULL | PM_MODEL_OP_NORM, maskVal);
    281 
    282           psImageConvolveFFT (source->modelFlux, rawModelFlux, NULL, 0, psfKernel);
     371            // insert the model image normalized to 1.0
     372            pmModelAdd (rawModelFlux, NULL, model, PM_MODEL_OP_FULL | PM_MODEL_OP_NORM, maskVal);
     373
     374            psImageConvolveFFT (source->modelFlux, rawModelFlux, NULL, 0, psfKernel);
    283375         
    284           psFree (psfKernel);
    285           psFree (rawModelFlux);
    286       }
    287 
    288       pmSourceCacheModel (source, maskVal);  // ALLOC x14 (!)
     376            psFree (psfKernel);
     377            psFree (rawModelFlux);
     378        }
     379
     380        pmSourceCacheModel (source, maskVal);  // ALLOC x14 (!)
    289381    }
    290382
  • trunk/psphot/src/psphotSourceFits.c

    r32633 r32695  
    257257        okDBL  = psphotEvalDBL (tmpSrc, DBL->data[0]);
    258258        okDBL &= psphotEvalDBL (tmpSrc, DBL->data[1]);
     259        okDBL = false; // XXX this is failing badly...
    259260        // XXX should I keep / save the flags set in the eval functions?
    260261
  • trunk/psphot/src/psphotStackChisqImage.c

    r31154 r32695  
    1313    psAssert (chisqFile, "missing chisq image FPA?");
    1414
     15    // the readout containing the chisq image is generated in the first pass of this loop and
     16    // used by the successive passes
    1517    pmReadout *chiReadout = NULL;
    1618
  • trunk/psphot/src/psphotStackReadout.c

    r32633 r32695  
    8282    }
    8383
    84     // XXX I think this is not defined correctly for an array of images.
    85     // XXX I probably need to subtract the model (same model?) for both RAW and OUT.
    86     // XXX But, probably not a problem in practice since the stacks are constructed with 0.0 mean level.
    87 
    8884    // generate a background model (median, smoothed image)
    8985    if (!psphotModelBackground (config, view, STACK_DET)) {
     
    9187    }
    9288    if (!psphotSubtractBackground (config, view, STACK_DET)) {
     89        return psphotReadoutCleanup (config, view, STACK_SRC);
     90    }
     91    if (!psphotSubtractBackground (config, view, STACK_SRC)) {
    9392        return psphotReadoutCleanup (config, view, STACK_SRC);
    9493    }
     
    114113    }
    115114
    116     // if DET and SRC are different images, 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
    117118    if (strcmp(STACK_SRC, STACK_DET)) {
    118119        if (!psphotCopySources (config, view, STACK_SRC, STACK_DET)) {
     
    130131        return psphotReadoutCleanup (config, view, STACK_SRC);
    131132    }
     133    // psphotDumpTest (config, view, STACK_SRC);
    132134    psMemDump("sourcestats");
    133135
     
    166168
    167169    // linear PSF fit to source peaks, subtract the models from the image (in PSF mask)
    168     // XXX why do this as a stack operation?
    169     // psphotFitSourcesLinearStack (config, objects, false);
    170170    psphotFitSourcesLinear (config, view, STACK_SRC, false);
    171171    psphotStackVisualFilerule(config, view, STACK_SRC);
     
    186186    psphotBlendFit (config, view, STACK_SRC); // pass 1 (detections->allSources)
    187187
    188     // replace all sources
    189     psphotReplaceAllSources (config, view, STACK_SRC); // pass 1 (detections->allSources)
     188    // replace all sources (do NOT ignore subtraction state)
     189    psphotReplaceAllSources (config, view, STACK_SRC, false); // pass 1 (detections->allSources)
    190190
    191191    // if we only do one pass, skip to extended source analysis
     
    194194    // linear fit to include all sources (subtract again)
    195195    // NOTE : apply to ALL sources (extended + psf)
     196    // NOTE 2 : this function subtracts the models from the given filerule (SRC), not DET
    196197    psphotFitSourcesLinear (config, view, STACK_SRC, true); // pass 2 (detections->allSources)
    197198
     
    200201    // NOTE: this block performs the 2nd pass low-significance PSF detection stage
    201202    {
     203        // if DET and SRC are different images, generate children sources for all sources in
     204        // the SRC image.  This operation replaces the existing DETECTION container on DET
     205        // which is currently a view to the one on SRC).  children sources go to
     206        // det->allSources
     207        if (strcmp(STACK_SRC, STACK_DET)) {
     208            psphotSourceChildren (config, view, STACK_DET, STACK_SRC);
     209
     210            //  subtract all sources from DET (this will subtract using the psf model for SRC, which
     211            //  will somewhat oversubtract the sources -- this is OK
     212            psphotRemoveAllSources (config, view, STACK_DET, false); // ignore subtraction state for sources
     213        }
     214
    202215        // add noise for subtracted objects
    203216        psphotAddNoise (config, view, STACK_DET); // pass 1 (detections->allSources)
     
    212225
    213226        // if DET and SRC are different images, copy the detections from DET to SRC
     227        // (this operation just ensures the metadata container has a view on SRC as well
    214228        if (strcmp(STACK_SRC, STACK_DET)) {
    215             // XXX how does this handle 1st vs 2nd pass sources?
    216             if (!psphotCopySources (config, view, STACK_SRC, STACK_DET)) {
     229            // replace all sources in DET
     230            psphotReplaceAllSources (config, view, STACK_DET, false); // ignore subtraction state for sources
     231
     232            // copy the newly detected peaks from DET to SRC so SourceStats below can operate on them
     233            if (!psphotCopyPeaks (config, view, STACK_SRC, STACK_DET)) {
    217234                psError (PSPHOT_ERR_UNKNOWN, false, "failure in peak analysis");
    218235                return psphotReadoutCleanup (config, view, STACK_SRC);
     
    237254        // replace all sources so fit below applies to all at once
    238255        // NOTE: apply only to OLD sources (which have been subtracted)
    239         psphotReplaceAllSources (config, view, STACK_SRC); // pass 2
     256        psphotReplaceAllSources (config, view, STACK_SRC, false); // pass 2
    240257
    241258        // merge the newly selected sources into the existing list
     
    312329
    313330        // replace the flux in the image so it is returned to its original state
    314         psphotReplaceAllSources (config, view, STACK_OUT);
     331        psphotReplaceAllSources (config, view, STACK_OUT, false);
    315332
    316333        // smooth to the next FWHM, or set 'smoothAgain' to false if no more
Note: See TracChangeset for help on using the changeset viewer.