IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 3, 2010, 8:41:49 AM (16 years ago)
Author:
eugene
Message:

updates from trunk

Location:
branches/tap_branches
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/tap_branches

  • branches/tap_branches/psphot

  • branches/tap_branches/psphot/src

    • Property svn:ignore
      •  

        old new  
        1919psphotMomentsStudy
        2020psphotPetrosianStudy
         21psphotForced
         22psphotMakePSF
         23psphotStack
  • branches/tap_branches/psphot/src/psphotMergeSources.c

    r21519 r27838  
    55                         PM_SOURCE_MODE_CR_LIMIT | PM_SOURCE_MODE_EXT_LIMIT) // Mask to apply for PSF sources
    66
     7// for now, let's store the detections on the readout->analysis for each readout
     8bool psphotMergeSources (pmConfig *config, const pmFPAview *view)
     9{
     10    bool status = true;
     11
     12    int num = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
     13    psAssert (status, "programming error: must define PSPHOT.INPUT.NUM");
     14
     15    // loop over the available readouts
     16    for (int i = 0; i < num; i++) {
     17        if (!psphotMergeSourcesReadout (config, view, "PSPHOT.INPUT", i)) {
     18            psError (PSPHOT_ERR_CONFIG, false, "failed to merge sources for PSPHOT.INPUT entry %d", i);
     19            return false;
     20        }
     21    }
     22    return true;
     23}
     24
    725// add newly selected sources to the existing list of sources
    8 bool psphotMergeSources (psArray *oldSources, psArray *newSources) {
     26bool psphotMergeSourcesReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index) {
     27
     28    bool status;
     29
     30    // find the currently selected readout
     31    pmFPAfile *file = pmFPAfileSelectSingle(config->files, filename, index); // File of interest
     32    psAssert (file, "missing file?");
     33
     34    pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
     35    psAssert (readout, "missing readout?");
     36
     37    pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
     38    psAssert (detections, "missing detections?");
     39
     40    psArray *newSources = detections->newSources;
     41    psAssert (newSources, "missing sources?");
     42
     43    // XXX TEST:
     44    if (detections->allSources) {
     45        psphotMaskCosmicRayFootprintCheck(detections->allSources);
     46    }
     47    if (detections->newSources) {
     48        psphotMaskCosmicRayFootprintCheck(detections->newSources);
     49    }
     50
     51    if (!detections->allSources) {
     52        detections->allSources = psArrayAllocEmpty(newSources->n);
     53    }
     54    psArray *allSources = detections->allSources;
    955
    1056    for (int i = 0; i < newSources->n; i++) {
    1157        pmSource *source = newSources->data[i];
    12         psArrayAdd (oldSources, 100, source);
    13     }
    14     return true;
    15 }
    16 
    17 // merge the externally supplied sources with the existing sources.  mark them as having
    18 // mode PM_SOURCE_MODE_EXTERNAL
    19 bool psphotLoadExtSources (pmConfig *config, const pmFPAview *view, psArray *sources) {
    20 
    21     // find the currently selected readout
    22     pmReadout  *readout = pmFPAfileThisReadout (config->files, view, "PSPHOT.INPUT.CMF");
    23     if (!readout) {
    24         psLogMsg ("psphot", 3, "no external sources supplied");
    25         return true;
    26     }
    27 
    28     psArray *extSources = psMetadataLookupPtr (NULL, readout->analysis, "PSPHOT.SOURCES");
    29     if (!extSources) {
     58        psArrayAdd (allSources, 100, source);
     59    }
     60
     61    psFree (detections->newSources);
     62    detections->newSources = NULL;
     63
     64    return true;
     65}
     66
     67// Merge the externally supplied sources with the existing sources.  Mark them as having mode
     68// PM_SOURCE_MODE_EXTERNAL.
     69
     70// XXX This function needs to be updated to loop over set of input files.  At the moment, we
     71// only expect a single entry for PSPHOT.INPUT.CMF and PSPHOT.SOURCES.TEXT, so we can only
     72// associate input sources with a single entry for PSPHOT.INPUT
     73bool psphotLoadExtSources (pmConfig *config, const pmFPAview *view) {
     74
     75    bool status;
     76    pmDetections *extCMF = NULL;
     77    psArray *extSourcesTXT = NULL;
     78    int index = 0;
     79
     80    // find the currently selected readout
     81    pmFPAfile *file = pmFPAfileSelectSingle(config->files, "PSPHOT.INPUT", index); // File of interest
     82    psAssert (file, "missing file?");
     83
     84    pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
     85    psAssert (readout, "missing readout?");
     86
     87    pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
     88    if (!detections) {
     89        detections = pmDetectionsAlloc();
     90        detections->newSources = psArrayAllocEmpty (100);
     91        // save detections on the readout->analysis
     92        if (!psMetadataAddPtr (readout->analysis, PS_LIST_TAIL, "PSPHOT.DETECTIONS", PS_META_REPLACE | PS_DATA_UNKNOWN, "psphot detections", detections)) {
     93            psError (PSPHOT_ERR_CONFIG, false, "problem saving detections on readout");
     94            return false;
     95        }
     96    } else {
     97        psMemIncrRefCounter(detections); // so we can free the detections below
     98    }
     99
     100    psArray *sources = detections->newSources;
     101    psAssert (sources, "missing sources?");
     102
     103    // load data from input CMF file:
     104    {
     105        pmReadout *readoutCMF = pmFPAfileThisReadout (config->files, view, "PSPHOT.INPUT.CMF");
     106        if (!readoutCMF) goto loadTXT;
     107
     108        extCMF = psMetadataLookupPtr (NULL, readoutCMF->analysis, "PSPHOT.DETECTIONS");
     109        if (extCMF) {
     110            for (int i = 0; i < extCMF->allSources->n; i++) {
     111                pmSource *source = extCMF->allSources->data[i];
     112                source->mode |= PM_SOURCE_MODE_EXTERNAL;
     113
     114                // the supplied peak flux needs to be re-normalized
     115                source->peak->flux = 1.0;
     116                source->peak->value = 1.0;
     117
     118                // drop the loaded source modelPSF
     119                psFree (source->modelPSF);
     120                source->modelPSF = NULL;
     121                source->imageID = index;
     122
     123                psArrayAdd (detections->newSources, 100, source);
     124            }
     125        }
     126    }
     127
     128loadTXT:
     129
     130    // load data from input TXT file:
     131    {
     132        pmChip *chipTXT = pmFPAfileThisChip (config->files, view, "PSPHOT.INPUT");
     133        if (!chipTXT) goto finish;
     134
     135        extSourcesTXT = psMetadataLookupPtr (NULL, chipTXT->analysis, "PSPHOT.SOURCES.TEXT");
     136        if (extSourcesTXT) {
     137            for (int i = 0; i < extSourcesTXT->n; i++) {
     138                pmSource *source = extSourcesTXT->data[i];
     139                source->mode |= PM_SOURCE_MODE_EXTERNAL;
     140
     141                // drop the loaded source modelPSF
     142                psFree (source->modelPSF);
     143                source->modelPSF = NULL;
     144                source->imageID = index;
     145
     146                psArrayAdd (detections->newSources, 100, source);
     147            }
     148        }
     149    }
     150
     151finish:
     152
     153    psFree (detections);
     154
     155    if (!extCMF && !extSourcesTXT) {
    30156        psLogMsg ("psphot", 3, "no external sources for this readout");
    31157        return true;
    32158    }
    33159
    34     for (int i = 0; i < extSources->n; i++) {
    35         pmSource *source = extSources->data[i];
    36         source->mode |= PM_SOURCE_MODE_EXTERNAL;
    37         pmModel *model = source->modelPSF;
    38 
    39         float xpos = model->params->data.F32[PM_PAR_XPOS];
    40         float ypos = model->params->data.F32[PM_PAR_YPOS];
    41 
    42         source->peak = pmPeakAlloc(xpos, ypos, 1.0, PM_PEAK_LONE);
    43         source->peak->xf = xpos;
    44         source->peak->yf = ypos;
    45         source->peak->flux = 1.0;
    46 
    47         // drop the loaded source modelPSF
    48         psFree (source->modelPSF);
    49         source->modelPSF = NULL;
    50     }
    51 
    52     psphotMergeSources (sources, extSources);
    53     psLogMsg ("psphot", 3, "%ld external sources merged to yield %ld total sources", extSources->n, sources->n);
    54 
     160    int nCMF = extCMF        ? extCMF->allSources->n        : 0;
     161    int nTXT = extSourcesTXT ? extSourcesTXT->n : 0;
     162
     163    psLogMsg ("psphot", 3, "%d external sources (%d cmf, %d text) merged to yield %ld total sources",
     164              nCMF + nTXT, nCMF, nTXT, sources->n);
    55165    return true;
    56166}
    57167
    58168// extract the input sources corresponding to this readout
     169// XXX this function needs to be updated to work with the new context of pshot inputs
    59170psArray *psphotLoadPSFSources (pmConfig *config, const pmFPAview *view) {
     171
     172    bool status;
    60173
    61174    // find the currently selected readout
     
    66179    }
    67180
    68     psArray *sources = psMetadataLookupPtr (NULL, readout->analysis, "PSPHOT.SOURCES");
     181    pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
     182    if (!detections) {
     183        psLogMsg ("psphot", 3, "no psf sources for this readout");
     184        return NULL;
     185    }
     186
     187    psArray *sources = detections->allSources;
    69188    if (!sources) {
    70189        psLogMsg ("psphot", 3, "no psf sources for this readout");
     190        return NULL;
    71191    }
    72192
     
    74194}
    75195
     196// this function is used to fix sources which were loaded externally, but have passed from
     197// psphotDetectionsFromSources to psphotSourceStats and are now stored on
     198// detections->newSources.
     199bool psphotRepairLoadedSources (pmConfig *config, const pmFPAview *view) {
     200
     201    // find the currently selected readout
     202    pmFPAfile *file = pmFPAfileSelectSingle(config->files, "PSPHOT.INPUT", 0); // File of interest
     203    psAssert (file, "missing file?");
     204
     205    pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
     206    psAssert (readout, "missing readout?");
     207
     208    pmDetections *detections = psMetadataLookupPtr (NULL, readout->analysis, "PSPHOT.DETECTIONS");
     209    if (!detections) {
     210        psError(PSPHOT_ERR_CONFIG, false, "missing detections");
     211        return false;
     212    }
     213
     214    psArray *sources = detections->newSources;
     215    psAssert (sources, "missing sources?");
     216
     217    // peak flux is wrong : set based on previous image
     218    // use the peak measured in the moments analysis:
     219    for (int i = 0; i < sources->n; i++) {
     220      pmSource *source = sources->data[i];
     221      source->peak->flux = source->moments->Peak;
     222    }
     223
     224    return true;
     225}
     226
    76227// generate the detection structure for the supplied array of sources
    77 pmDetections *psphotDetectionsFromSources (pmConfig *config, psArray *sources) {
     228// XXX this currently assumes there is a single input file
     229bool psphotDetectionsFromSources (pmConfig *config, const pmFPAview *view, psArray *sources) {
     230
     231    // find the currently selected readout
     232    pmFPAfile *file = pmFPAfileSelectSingle(config->files, "PSPHOT.INPUT", 0); // File of interest
     233    psAssert (file, "missing file?");
     234
     235    pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
     236    psAssert (readout, "missing readout?");
    78237
    79238    pmDetections *detections = pmDetectionsAlloc();
     
    88247    float snMin = psMetadataLookupF32(NULL, recipe, "MOMENTS_SN_MIN");
    89248    if (!isfinite(snMin)) {
    90         return NULL;
     249        return false;
    91250    }
    92251
     
    96255
    97256        if (source->mode & PSF_SOURCE_MASK || !isfinite(source->psfMag)) {
     257            continue;
     258        }
     259
     260        // use the existing peak information, otherwise generate a new peak
     261        if (source->peak) {
     262            source->peak->assigned = false; // So the moments will be measured
     263            psArrayAdd (detections->peaks, 100, source->peak);
    98264            continue;
    99265        }
     
    108274        peak->flux = flux; // this are being set wrong, but does it matter?
    109275
    110         if (isfinite (source->errMag) && (source->errMag > 0.0)) {
    111           peak->SN = 1.0 / source->errMag;
    112         } else {
    113           peak->SN = 0.0;
    114         }
     276        if (isfinite (source->errMag) && (source->errMag > 0.0)) {
     277          peak->SN = 1.0 / source->errMag;
     278        } else {
     279          peak->SN = 0.0;
     280        }
    115281
    116282        psArrayAdd (detections->peaks, 100, peak);
     
    120286    psLogMsg ("psphot", 3, "%ld PSF sources loaded", detections->peaks->n);
    121287
    122     return detections;
     288    // save detections on the readout->analysis
     289    if (!psMetadataAddPtr (readout->analysis, PS_LIST_TAIL, "PSPHOT.DETECTIONS", PS_META_REPLACE | PS_DATA_UNKNOWN, "psphot detectinos", detections)) {
     290        psError (PSPHOT_ERR_CONFIG, false, "problem saving detections on readout");
     291        return false;
     292    }
     293    psFree (detections);
     294    return true;
    123295}
    124296
     
    149321        peak->flux = flux; // this are being set wrong, but does it matter?
    150322
    151         if (isfinite (source->errMag) && (source->errMag > 0.0)) {
    152           peak->SN = 1.0 / source->errMag;
    153         } else {
    154           peak->SN = 0.0;
    155         }
     323        if (isfinite (source->errMag) && (source->errMag > 0.0)) {
     324          peak->SN = 1.0 / source->errMag;
     325        } else {
     326          peak->SN = 0.0;
     327        }
    156328
    157329        source->peak = peak;
     
    162334    return true;
    163335}
     336
     337bool psphotCheckExtSources (pmConfig *config, const pmFPAview *view) {
     338
     339    bool status;
     340
     341    psMetadata *recipe = psMetadataLookupPtr (NULL, config->recipes, PSPHOT_RECIPE);
     342    psAssert (recipe, "missing recipe");
     343
     344    // find the currently selected readout
     345    pmFPAfile *file = pmFPAfileSelectSingle(config->files, "PSPHOT.INPUT", 0); // File of interest
     346    psAssert (file, "missing file?");
     347
     348    pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
     349    psAssert (readout, "missing readout?");
     350
     351    pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
     352    psAssert (detections, "missing detections?");
     353
     354    // XXX allSources of newSources?
     355    psArray *sources = detections->allSources;
     356    psAssert (sources, "missing sources?");
     357
     358    if (sources->n) {
     359        // the user wants to make the psf from these stars; define them as psf stars:
     360        for (int i = 0; i < sources->n; i++) {
     361            pmSource *source = sources->data[i];
     362            source->mode |= PM_SOURCE_MODE_PSFSTAR;
     363        }
     364        // force psphotChoosePSF to use all loaded sources
     365        psMetadataAddS32 (recipe, PS_LIST_TAIL, "PSF_MAX_NSTARS", PS_META_REPLACE, "max number of sources for PSF model", sources->n);
     366
     367        // measure stats of externally specified sources
     368        if (!psphotSourceStatsUpdate (sources, config, readout)) {
     369            psError(PSPHOT_ERR_CONFIG, false, "failure to measure stats of existing sources");
     370            return false;
     371        }
     372    } else {
     373
     374        // find the detections (by peak and/or footprint) in the image.
     375        if (!psphotFindDetections (config, view, true)) {
     376            psError(PSPHOT_ERR_CONFIG, false, "unable to find detections in this image");
     377            return psphotReadoutCleanup (config, view);
     378        }
     379
     380        // construct sources and measure basic stats
     381        psphotSourceStats (config, view, true);
     382
     383        // find blended neighbors of very saturated stars
     384        psphotDeblendSatstars (config, view);
     385
     386        // mark blended peaks PS_SOURCE_BLEND
     387        if (!psphotBasicDeblend (config, view)) {
     388            psLogMsg ("psphot", 3, "failed on deblend analysis");
     389            return psphotReadoutCleanup (config, view);
     390        }
     391
     392        // classify sources based on moments, brightness
     393        if (!psphotRoughClass (config, view)) {
     394            psLogMsg ("psphot", 3, "failed to find a valid PSF clump for image");
     395            return psphotReadoutCleanup (config, view);
     396        }
     397    }
     398
     399    return true;
     400}
Note: See TracChangeset for help on using the changeset viewer.