IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Feb 10, 2010, 7:36:29 PM (16 years ago)
Author:
eugene
Message:

updates from eam_branches/20091201 (substantially changes to the psphotReadout APIs to support future stack photometry; improvements to the CR masking code)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psphot/src/psphotMergeSources.c

    r25983 r26894  
    55                         PM_SOURCE_MODE_CR_LIMIT | PM_SOURCE_MODE_EXT_LIMIT) // Mask to apply for PSF sources
    66
    7 // merge the externally supplied sources with the existing sources.  mark them as having
    8 // mode PM_SOURCE_MODE_EXTERNAL
    9 bool psphotLoadExtSources (pmConfig *config, const pmFPAview *view, psArray *sources) {
    10 
    11     psArray *extSourcesCMF = NULL;
     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
     25// add newly selected sources to the existing list of sources
     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;
     55
     56    for (int i = 0; i < newSources->n; i++) {
     57        pmSource *source = newSources->data[i];
     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;
    1277    psArray *extSourcesTXT = NULL;
    1378
     79    // find the currently selected readout
     80    pmFPAfile *file = pmFPAfileSelectSingle(config->files, "PSPHOT.INPUT", 0); // File of interest
     81    psAssert (file, "missing file?");
     82
     83    pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
     84    psAssert (readout, "missing readout?");
     85
     86    pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
     87    psAssert (detections, "missing detections?");
     88
     89    // XXX allSources or newSources?
     90    psArray *sources = detections->allSources;
     91    psAssert (sources, "missing sources?");
     92
    1493    // load data from input CMF file:
    15     {
    16         pmReadout *readoutCMF = pmFPAfileThisReadout (config->files, view, "PSPHOT.INPUT.CMF");
    17         if (!readoutCMF) goto loadTXT;
    18 
    19         extSourcesCMF = psMetadataLookupPtr (NULL, readoutCMF->analysis, "PSPHOT.SOURCES");
    20         if (extSourcesCMF) {
    21             for (int i = 0; i < extSourcesCMF->n; i++) {
    22                 pmSource *source = extSourcesCMF->data[i];
    23                 source->mode |= PM_SOURCE_MODE_EXTERNAL;
    24 
    25                 // the supplied peak flux needs to be re-normalized
    26                 source->peak->flux = 1.0;
    27                 source->peak->value = 1.0;
    28 
    29                 // drop the loaded source modelPSF
    30                 psFree (source->modelPSF);
    31                 source->modelPSF = NULL;
    32             }
    33             psphotMergeSources (sources, extSourcesCMF);
    34         }
     94    {
     95        pmReadout *readoutCMF = pmFPAfileThisReadout (config->files, view, "PSPHOT.INPUT.CMF");
     96        if (!readoutCMF) goto loadTXT;
     97
     98        extCMF = psMetadataLookupPtr (NULL, readoutCMF->analysis, "PSPHOT.DETECTIONS");
     99        if (extCMF) {
     100            for (int i = 0; i < extCMF->allSources->n; i++) {
     101                pmSource *source = extCMF->allSources->data[i];
     102                source->mode |= PM_SOURCE_MODE_EXTERNAL;
     103
     104                // the supplied peak flux needs to be re-normalized
     105                source->peak->flux = 1.0;
     106                source->peak->value = 1.0;
     107
     108                // drop the loaded source modelPSF
     109                psFree (source->modelPSF);
     110                source->modelPSF = NULL;
     111
     112                psArrayAdd (detections->allSources, 100, source);
     113            }
     114        }
    35115    }
    36116
     
    38118
    39119    // load data from input TXT file:
    40     {
    41         pmChip *chipTXT = pmFPAfileThisChip (config->files, view, "PSPHOT.INPUT");
    42         if (!chipTXT) goto finish;
    43 
    44         extSourcesTXT = psMetadataLookupPtr (NULL, chipTXT->analysis, "PSPHOT.SOURCES.TEXT");
    45         if (extSourcesTXT) {
    46             for (int i = 0; i < extSourcesTXT->n; i++) {
    47                 pmSource *source = extSourcesTXT->data[i];
    48                 source->mode |= PM_SOURCE_MODE_EXTERNAL;
    49 
    50                 // drop the loaded source modelPSF
    51                 psFree (source->modelPSF);
    52                 source->modelPSF = NULL;
    53             }
    54             psphotMergeSources (sources, extSourcesTXT);
    55         }
     120    {
     121        pmChip *chipTXT = pmFPAfileThisChip (config->files, view, "PSPHOT.INPUT");
     122        if (!chipTXT) goto finish;
     123
     124        extSourcesTXT = psMetadataLookupPtr (NULL, chipTXT->analysis, "PSPHOT.SOURCES.TEXT");
     125        if (extSourcesTXT) {
     126            for (int i = 0; i < extSourcesTXT->n; i++) {
     127                pmSource *source = extSourcesTXT->data[i];
     128                source->mode |= PM_SOURCE_MODE_EXTERNAL;
     129
     130                // drop the loaded source modelPSF
     131                psFree (source->modelPSF);
     132                source->modelPSF = NULL;
     133
     134                psArrayAdd (detections->allSources, 100, source);
     135            }
     136        }
    56137    }
    57138
    58139finish:
    59140
    60     if (!extSourcesTXT && !extSourcesTXT) {
     141    if (!extCMF && !extSourcesTXT) {
    61142        psLogMsg ("psphot", 3, "no external sources for this readout");
    62143        return true;
    63144    }
    64145
    65     int nCMF = extSourcesCMF ? extSourcesCMF->n : 0;
     146    int nCMF = extCMF        ? extCMF->allSources->n        : 0;
    66147    int nTXT = extSourcesTXT ? extSourcesTXT->n : 0;
    67148
    68     psLogMsg ("psphot", 3, "%d external sources (%d cmf, %d text) merged to yield %ld total sources",
    69               nCMF + nTXT, nCMF, nTXT, sources->n);
    70     return true;
    71 }
    72 
    73 // add newly selected sources to the existing list of sources
    74 bool psphotMergeSources (psArray *oldSources, psArray *newSources) {
    75 
    76     for (int i = 0; i < newSources->n; i++) {
    77         pmSource *source = newSources->data[i];
    78         psArrayAdd (oldSources, 100, source);
    79     }
     149    psLogMsg ("psphot", 3, "%d external sources (%d cmf, %d text) merged to yield %ld total sources",
     150              nCMF + nTXT, nCMF, nTXT, sources->n);
    80151    return true;
    81152}
    82153
    83154// extract the input sources corresponding to this readout
     155// XXX this function needs to be updated to work with the new context of pshot inputs
    84156psArray *psphotLoadPSFSources (pmConfig *config, const pmFPAview *view) {
     157
     158    bool status;
    85159
    86160    // find the currently selected readout
     
    91165    }
    92166
    93     psArray *sources = psMetadataLookupPtr (NULL, readout->analysis, "PSPHOT.SOURCES");
     167    pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
     168    if (!detections) {
     169        psLogMsg ("psphot", 3, "no psf sources for this readout");
     170        return NULL;
     171    }
     172
     173    psArray *sources = detections->allSources;
    94174    if (!sources) {
    95175        psLogMsg ("psphot", 3, "no psf sources for this readout");
     176        return NULL;
    96177    }
    97178
     
    99180}
    100181
     182// this function is used to fix sources which were loaded externally, but have passed from
     183// psphotDetectionsFromSources to psphotSourceStats and are now stored on
     184// detections->newSources.
     185bool psphotRepairLoadedSources (pmConfig *config, const pmFPAview *view) {
     186
     187    // find the currently selected readout
     188    pmFPAfile *file = pmFPAfileSelectSingle(config->files, "PSPHOT.INPUT", 0); // File of interest
     189    psAssert (file, "missing file?");
     190
     191    pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
     192    psAssert (readout, "missing readout?");
     193
     194    pmDetections *detections = psMetadataLookupPtr (NULL, readout->analysis, "PSPHOT.DETECTIONS");
     195    if (!detections) {
     196        psError(PSPHOT_ERR_CONFIG, false, "missing detections");
     197        return false;
     198    }
     199
     200    psArray *sources = detections->newSources;
     201    psAssert (sources, "missing sources?");
     202
     203    // peak flux is wrong : set based on previous image
     204    // use the peak measured in the moments analysis:
     205    for (int i = 0; i < sources->n; i++) {
     206      pmSource *source = sources->data[i];
     207      source->peak->flux = source->moments->Peak;
     208    }
     209
     210    return true;
     211}
     212
    101213// generate the detection structure for the supplied array of sources
    102 pmDetections *psphotDetectionsFromSources (pmConfig *config, psArray *sources) {
     214// XXX this currently assumes there is a single input file
     215bool psphotDetectionsFromSources (pmConfig *config, const pmFPAview *view, psArray *sources) {
     216
     217    // find the currently selected readout
     218    pmFPAfile *file = pmFPAfileSelectSingle(config->files, "PSPHOT.INPUT", 0); // File of interest
     219    psAssert (file, "missing file?");
     220
     221    pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
     222    psAssert (readout, "missing readout?");
    103223
    104224    pmDetections *detections = pmDetectionsAlloc();
     
    113233    float snMin = psMetadataLookupF32(NULL, recipe, "MOMENTS_SN_MIN");
    114234    if (!isfinite(snMin)) {
    115         return NULL;
     235        return false;
    116236    }
    117237
     
    121241
    122242        if (source->mode & PSF_SOURCE_MASK || !isfinite(source->psfMag)) {
     243            continue;
     244        }
     245
     246        // use the existing peak information, otherwise generate a new peak
     247        if (source->peak) {
     248            source->peak->assigned = false; // So the moments will be measured
     249            psArrayAdd (detections->peaks, 100, source->peak);
    123250            continue;
    124251        }
     
    133260        peak->flux = flux; // this are being set wrong, but does it matter?
    134261
    135         if (isfinite (source->errMag) && (source->errMag > 0.0)) {
    136           peak->SN = 1.0 / source->errMag;
    137         } else {
    138           peak->SN = 0.0;
    139         }
     262        if (isfinite (source->errMag) && (source->errMag > 0.0)) {
     263          peak->SN = 1.0 / source->errMag;
     264        } else {
     265          peak->SN = 0.0;
     266        }
    140267
    141268        psArrayAdd (detections->peaks, 100, peak);
     
    145272    psLogMsg ("psphot", 3, "%ld PSF sources loaded", detections->peaks->n);
    146273
    147     return detections;
     274    // save detections on the readout->analysis
     275    if (!psMetadataAddPtr (readout->analysis, PS_LIST_TAIL, "PSPHOT.DETECTIONS", PS_META_REPLACE | PS_DATA_UNKNOWN, "psphot detectinos", detections)) {
     276        psError (PSPHOT_ERR_CONFIG, false, "problem saving detections on readout");
     277        return false;
     278    }
     279    psFree (detections);
     280    return true;
    148281}
    149282
     
    174307        peak->flux = flux; // this are being set wrong, but does it matter?
    175308
    176         if (isfinite (source->errMag) && (source->errMag > 0.0)) {
    177           peak->SN = 1.0 / source->errMag;
    178         } else {
    179           peak->SN = 0.0;
    180         }
     309        if (isfinite (source->errMag) && (source->errMag > 0.0)) {
     310          peak->SN = 1.0 / source->errMag;
     311        } else {
     312          peak->SN = 0.0;
     313        }
    181314
    182315        source->peak = peak;
     
    187320    return true;
    188321}
     322
     323bool psphotCheckExtSources (pmConfig *config, const pmFPAview *view) {
     324
     325    bool status;
     326
     327    psMetadata *recipe = psMetadataLookupPtr (NULL, config->recipes, PSPHOT_RECIPE);
     328    psAssert (recipe, "missing recipe");
     329
     330    // find the currently selected readout
     331    pmFPAfile *file = pmFPAfileSelectSingle(config->files, "PSPHOT.INPUT", 0); // File of interest
     332    psAssert (file, "missing file?");
     333
     334    pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
     335    psAssert (readout, "missing readout?");
     336
     337    pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
     338    psAssert (detections, "missing detections?");
     339
     340    // XXX allSources of newSources?
     341    psArray *sources = detections->allSources;
     342    psAssert (sources, "missing sources?");
     343
     344    if (sources->n) {
     345        // the user wants to make the psf from these stars; define them as psf stars:
     346        for (int i = 0; i < sources->n; i++) {
     347            pmSource *source = sources->data[i];
     348            source->mode |= PM_SOURCE_MODE_PSFSTAR;
     349        }
     350        // force psphotChoosePSF to use all loaded sources
     351        psMetadataAddS32 (recipe, PS_LIST_TAIL, "PSF_MAX_NSTARS", PS_META_REPLACE, "max number of sources for PSF model", sources->n);
     352
     353        // measure stats of externally specified sources
     354        if (!psphotSourceStatsUpdate (sources, config, readout)) {
     355            psError(PSPHOT_ERR_CONFIG, false, "failure to measure stats of existing sources");
     356            return false;
     357        }
     358    } else {
     359
     360        // find the detections (by peak and/or footprint) in the image.
     361        if (!psphotFindDetections (config, view, true)) {
     362            psError(PSPHOT_ERR_CONFIG, false, "unable to find detections in this image");
     363            return psphotReadoutCleanup (config, view);
     364        }
     365
     366        // construct sources and measure basic stats
     367        psphotSourceStats (config, view, true);
     368
     369        // find blended neighbors of very saturated stars
     370        psphotDeblendSatstars (config, view);
     371
     372        // mark blended peaks PS_SOURCE_BLEND
     373        if (!psphotBasicDeblend (config, view)) {
     374            psLogMsg ("psphot", 3, "failed on deblend analysis");
     375            return psphotReadoutCleanup (config, view);
     376        }
     377
     378        // classify sources based on moments, brightness
     379        if (!psphotRoughClass (config, view)) {
     380            psLogMsg ("psphot", 3, "failed to find a valid PSF clump for image");
     381            return psphotReadoutCleanup (config, view);
     382        }
     383    }
     384
     385    return true;
     386}
Note: See TracChangeset for help on using the changeset viewer.