IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

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:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/psphot

  • 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
Note: See TracChangeset for help on using the changeset viewer.