IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Mar 4, 2008, 3:10:25 PM (18 years ago)
Author:
eugene
Message:

merge changes from eam_branch_20080229: better flag definitions, cleanup footprint code (move into psphotFindDetections), push mask selection into called functions

File:
1 edited

Legend:

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

    r16250 r16820  
    99    psTimerStart ("psphotReadout");
    1010
    11     bool dump = (psTraceGetLevel("psphot") >= 6);
    12 
    1311    // select the current recipe
    1412    psMetadata *recipe = psMetadataLookupPtr (NULL, config->recipes, PSPHOT_RECIPE);
     
    1816    }
    1917
    20     // Interpret the mask values
    21     const char *maskValStr = psMetadataLookupStr(NULL, recipe, "MASKVAL"); // String with mask names
    22     if (!maskValStr) {
    23         psError(PSPHOT_ERR_CONFIG, false, "Missing recipe item: MASKVAL(STR)");
    24         return false;
    25     }
    26     psMaskType maskVal = pmConfigMask(maskValStr, config); // Mask values to mask against
    27     psMaskType maskMark = pmConfigMask("MARK", config); // Mask value for marking
    28     psMaskType maskSat = pmConfigMask("SAT", config); // Mask value for saturated pixels
    29     psMaskType maskBad = pmConfigMask("BAD", config); // Mask value for bad pixels
    30 
    31     // find the currently selected readout
    32     pmReadout  *readout = pmFPAfileThisReadout (config->files, view, "PSPHOT.INPUT");
    33     PS_ASSERT_PTR_NON_NULL (readout, false);
    34 
    35     // optional break-point for processing
    36     char *breakPt = psMetadataLookupStr (NULL, recipe, "BREAK_POINT");
    37     PS_ASSERT_PTR_NON_NULL (breakPt, false);
    38 
    39     // Use the new pmFootprints approach?
    40     const bool useFootprints = psMetadataLookupBool(NULL, recipe, "USE_FOOTPRINTS");
    41 
    42     // generate mask & weight images if they don't already exit
    43     if (!readout->mask) {
    44         if (!pmReadoutGenerateMask(readout, maskSat, maskBad)) {
    45             psError (PSPHOT_ERR_CONFIG, false, "trouble creating mask");
    46             return false;
    47         }
    48     }
    49     if (!readout->weight) {
    50         if (!pmReadoutGenerateWeight(readout, true)) {
    51             psError (PSPHOT_ERR_CONFIG, false, "trouble creating weight");
    52             return false;
    53         }
    54     }
    55 
    5618    // set the photcode for this image
    5719    if (!psphotAddPhotcode (recipe, config, view)) {
     
    6022    }
    6123
    62     // I have a valid mask, now mask in the analysis region of interest
    63     psphotMaskReadout (readout, recipe, maskBad);
    64 
    65     if (psTraceGetLevel("psphot") >= 5) {
    66         psphotSaveImage (NULL, readout->image,  "image.fits");
    67         psphotSaveImage (NULL, readout->mask,   "mask.fits");
    68         psphotSaveImage (NULL, readout->weight, "weight.fits");
    69     }
    70 
     24    // find the currently selected readout
     25    pmReadout  *readout = pmFPAfileThisReadout (config->files, view, "PSPHOT.INPUT");
     26    PS_ASSERT_PTR_NON_NULL (readout, false);
     27
     28    // optional break-point for processing
     29    char *breakPt = psMetadataLookupStr (NULL, recipe, "BREAK_POINT");
     30    PS_ASSERT_PTR_NON_NULL (breakPt, false);
     31
     32    // Generate the mask and weight images, including the user-defined analysis region of interest
     33    psphotSetMaskAndWeight (config, readout, recipe);
    7134    if (!strcasecmp (breakPt, "NOTHING")) {
    72         return psphotReadoutCleanup(config, readout, recipe, NULL, NULL);
     35        return psphotReadoutCleanup(config, readout, recipe, NULL, NULL, NULL);
    7336    }
    7437
    7538    // generate a background model (median, smoothed image)
    76     if (!psphotModelBackground (config, view, "PSPHOT.INPUT", maskVal)) {
    77         return psphotReadoutCleanup (config, readout, recipe, NULL, NULL);
    78     }
    79     if (!psphotSubtractBackground (config, view, "PSPHOT.INPUT", maskVal)) {
    80         return psphotReadoutCleanup (config, readout, recipe, NULL, NULL);
    81     }
    82 
     39    if (!psphotModelBackground (config, view, "PSPHOT.INPUT")) {
     40        return psphotReadoutCleanup (config, readout, recipe, NULL, NULL, NULL);
     41    }
     42    if (!psphotSubtractBackground (config, view, "PSPHOT.INPUT")) {
     43        return psphotReadoutCleanup (config, readout, recipe, NULL, NULL, NULL);
     44    }
    8345    if (!strcasecmp (breakPt, "BACKMDL")) {
    84         return psphotReadoutCleanup (config, readout, recipe, NULL, NULL);
    85     }
    86 
    87     // run a single-model test if desired
    88     psphotModelTest (config, view, recipe, maskVal, maskMark);
     46        return psphotReadoutCleanup (config, readout, recipe, NULL, NULL, NULL);
     47    }
     48
     49    // run a single-model test if desired (exits from here if test is run)
     50    psphotModelTest (config, view, recipe);
    8951
    9052    // load the psf model, if suppled.  FWHM_X,FWHM_Y,etc are saved in the recipe
     
    9456    bool havePSF = (psf != NULL);
    9557
    96     // find the peaks in the image.
    97 
    98     // XXX clean this up into a single function.  if psf is defined, we should treat this as
    99     // pass "2", not "1".  re-define this boolean to be "have PSF"
    100     psArray *peaks;
    101     psArray *footprints = NULL;
    102     if (useFootprints) {
    103        footprints = psphotFindPeaks (readout, recipe, useFootprints, 1, maskVal);
    104        int growRadius = psMetadataLookupS32(NULL, recipe, "FOOTPRINT_GROW_RADIUS");
    105        if (growRadius > 0) {
    106            psArray *tmp = pmGrowFootprintArray(footprints, growRadius);
    107            psFree(footprints);
    108            footprints = tmp;
    109        }
    110        psphotCullPeaks(readout->image, readout->weight, recipe, footprints);
    111 
    112        peaks = pmFootprintArrayToPeaks(footprints);
    113     } else {
    114        peaks = psphotFindPeaks (readout, recipe, useFootprints, 1, maskVal);
    115     }
    116 
    117     if (!peaks) {
    118         psLogMsg ("psphot", 3, "unable to find peaks in this image");
    119         return psphotReadoutCleanup (config, readout, recipe, NULL, NULL);
     58    // find the detections (by peak and/or footprint) in the image.
     59    pmDetections *detections = psphotFindDetections (NULL, readout, recipe);
     60    if (!detections) {
     61        psLogMsg ("psphot", 3, "unable to find detections in this image");
     62        return psphotReadoutCleanup (config, readout, recipe, detections, psf, NULL);
    12063    }
    12164
    12265    // construct sources and measure basic stats
    123     psArray *sources = psphotSourceStats (readout, recipe, peaks, maskVal, maskMark);
     66    psArray *sources = psphotSourceStats (readout, recipe, detections);
    12467    if (!sources) return false;
    125     psFree (peaks);
    126 
    12768    if (!strcasecmp (breakPt, "PEAKS")) {
    128         return psphotReadoutCleanup(config, readout, recipe, NULL, sources);
    129     }
    130 
     69        return psphotReadoutCleanup(config, readout, recipe, detections, psf, sources);
     70    }
     71
     72    // find blended neighbors of very saturated stars
     73    // XXX merge this with Basic Deblend?
    13174    psphotDeblendSatstars (sources, recipe);
    13275
     
    13477    if (!psphotBasicDeblend (sources, recipe)) {
    13578        psLogMsg ("psphot", 3, "failed on deblend analysis");
    136         return psphotReadoutCleanup (config, readout, recipe, NULL, sources);
     79        return psphotReadoutCleanup (config, readout, recipe, detections, psf, sources);
    13780    }
    13881
    13982    // classify sources based on moments, brightness
    140     if (!psphotRoughClass (sources, recipe, havePSF, maskSat)) {
     83    if (!psphotRoughClass (sources, recipe, havePSF)) {
    14184        psLogMsg ("psphot", 3, "failed to find a valid PSF clump for image");
    142         return psphotReadoutCleanup (config, readout, recipe, NULL, sources);
     85        return psphotReadoutCleanup (config, readout, recipe, detections, psf, sources);
    14386    }
    14487    if (!strcasecmp (breakPt, "MOMENTS")) {
    145         return psphotReadoutCleanup(config, readout, recipe, NULL, sources);
     88        return psphotReadoutCleanup(config, readout, recipe, detections, psf, sources);
    14689    }
    14790
     
    15194        // XXX if we do not have enough stars to generate the PSF, build one
    15295        // from the SEEING guess and model class
    153         psf = psphotChoosePSF (readout, sources, recipe, maskVal, maskMark);
     96        psf = psphotChoosePSF (readout, sources, recipe);
    15497        if (psf == NULL) {
    15598            psLogMsg ("psphot", 3, "failure to construct a psf model");
    156             return psphotReadoutCleanup (config, readout, recipe, psf, sources);
     99            return psphotReadoutCleanup (config, readout, recipe, detections, psf, sources);
    157100        }
    158101        havePSF = true;
    159102    }
    160 
    161     // XXX Test dump of PSF parameters across image field.
    162 
    163103    if (!strcasecmp (breakPt, "PSFMODEL")) {
    164         return psphotReadoutCleanup (config, readout, recipe, psf, sources);
     104        return psphotReadoutCleanup (config, readout, recipe, detections, psf, sources);
    165105    }
    166106
     
    169109
    170110    // construct an initial model for each object
    171     psphotGuessModels (readout, sources, recipe, psf, maskVal);
     111    psphotGuessModels (readout, sources, recipe, psf);
    172112
    173113    // XXX test output of models
    174114    // psphotTestSourceOutput (readout, sources, recipe, psf);
    175115
    176     if (dump) psphotSaveImage (NULL, readout->image,  "image.v0.fits");
    177 
    178     // linear PSF fit to peaks
    179     psphotFitSourcesLinear (readout, sources, recipe, psf, FALSE, maskVal);
    180     if (dump) psphotSaveImage (NULL, readout->image,  "image.v1.fits");
     116    // linear PSF fit to source peaks
     117    psphotFitSourcesLinear (readout, sources, recipe, psf, FALSE);
    181118
    182119    // XXX test the CR/EXT measurement
    183120    // XXX we need to call this here and after the second pass: option for starting number?
    184     psphotSourceSize (readout, sources, recipe);
    185 
     121    psphotSourceSize (readout, sources, recipe, 0);
    186122    if (!strcasecmp (breakPt, "ENSEMBLE")) {
    187123        goto finish;
     
    189125
    190126    // non-linear PSF and EXT fit to brighter sources
    191     psphotBlendFit (readout, sources, recipe, psf, maskVal);
    192     if (dump) psphotSaveImage (NULL, readout->image,  "image.v2.fits");
    193 
    194     // replace all sources
    195     psphotReplaceAll (sources, maskVal);
    196     if (dump) psphotSaveImage (NULL, readout->image,  "image.v3.fits");
    197 
    198     // linear PSF fit to remaining peaks
    199     psphotFitSourcesLinear (readout, sources, recipe, psf, TRUE, maskVal);
    200     if (dump) psphotSaveImage (NULL, readout->image,  "image.v4.fits");
     127    psphotBlendFit (readout, sources, recipe, psf);
     128
     129    // replace all sources (make this part of psphotFitSourcesLinear?)
     130    psphotReplaceAll (sources, recipe);
     131
     132    // linear fit to include all sources
     133    psphotFitSourcesLinear (readout, sources, recipe, psf, TRUE);
    201134    if (!strcasecmp (breakPt, "PASS1")) {
    202135        goto finish;
     
    210143
    211144    // add noise for subtracted objects
    212     psphotAddNoise (readout, sources, recipe, true, maskVal);
    213 
    214     // find the peaks in the image
    215     psArray *newPeaks;
    216     if (useFootprints) {
    217        psArray *newFootprints = psphotFindPeaks (readout, recipe, useFootprints, 2, maskVal);
    218 
    219        int growRadius = psMetadataLookupS32(NULL, recipe, "FOOTPRINT_GROW_RADIUS_2");
    220        if (growRadius > 0) {
    221            psArray *tmp = pmGrowFootprintArray(newFootprints, growRadius);
    222            psFree(newFootprints);
    223            newFootprints = tmp;
    224        }
    225 
    226        // merge in old peaks
    227        const int includePeaks = 0x0 | 0x2; // i.e. just from newFootprints
    228        psArray *mergedFootprints = pmMergeFootprintArrays(footprints, newFootprints, includePeaks);
    229        psFree(footprints);
    230        psFree(newFootprints);
    231 
    232        psphotCullPeaks(readout->image, readout->weight, recipe, mergedFootprints);
    233 
    234        newPeaks = pmFootprintArrayToPeaks(mergedFootprints);
    235 
    236        psFree(mergedFootprints);
    237     } else {
    238        newPeaks = psphotFindPeaks(readout, recipe, useFootprints, 2, maskVal);
    239     }
     145    psphotAddNoise (readout, sources, recipe);
     146
     147    detections = psphotFindDetections (detections, readout, recipe);
    240148
    241149    // remove noise for subtracted objects
    242     psphotAddNoise (readout, sources, recipe, false, maskVal);
    243 
    244     // define new sources based on the new peaks
    245     psArray *newSources = psphotSourceStats (readout, recipe, newPeaks, maskVal, maskMark);
    246     psFree (newPeaks);
     150    psphotSubNoise (readout, sources, recipe);
     151
     152    // define new sources based on only the new peaks
     153    psArray *newSources = psphotSourceStats (readout, recipe, detections);
    247154
    248155    // set source type
    249     if (!psphotRoughClass (newSources, recipe, havePSF, maskSat)) {
     156    if (!psphotRoughClass (newSources, recipe, havePSF)) {
    250157        psLogMsg ("psphot", 3, "failed to find a valid PSF clump for image");
    251         return psphotReadoutCleanup (config, readout, recipe, psf, sources);
     158        return psphotReadoutCleanup (config, readout, recipe, detections, psf, sources);
    252159    }
    253160
    254161    // create full input models
    255     psphotGuessModels (readout, newSources, recipe, psf, maskVal);
    256 
    257     // replace all sources
    258     psphotReplaceAll (sources, maskVal);
    259     if (dump) psphotSaveImage (NULL, readout->image,  "image.v5.fits");
    260 
    261     // merge the newly selected peaks into the existing list
     162    psphotGuessModels (readout, newSources, recipe, psf);
     163
     164    // replace all sources so fit below applies to all at once
     165    psphotReplaceAll (sources, recipe);
     166
     167    // merge the newly selected sources into the existing list
    262168    psphotMergeSources (sources, newSources);
    263169    psFree (newSources);
    264170
    265     // linear PSF fit to remaining peaks
    266     psphotFitSourcesLinear (readout, sources, recipe, psf, TRUE, maskVal);
    267     if (dump) psphotSaveImage (NULL, readout->image,  "image.v6.fits");
    268 
    269     psphotExtendedSources (readout, sources, recipe, maskVal);
     171    // linear fit to all sources
     172    psphotFitSourcesLinear (readout, sources, recipe, psf, TRUE);
     173
     174    // measure source size for the remaining sources
     175    psphotSourceSize (readout, sources, recipe, 0);
     176
     177    psphotExtendedSources (readout, sources, recipe);
    270178
    271179finish:
     
    273181    // plot positive sources
    274182    if (!havePSF) {
    275         // psphotSourcePlots (readout, sources, recipe, maskVal);
     183        // psphotSourcePlots (readout, sources, recipe);
    276184    }
    277185
    278186    // measure aperture photometry corrections
    279     if (!havePSF && !psphotApResid (readout, sources, recipe, psf, maskVal, maskMark)) {
    280         psTrace ("psphot", 4, "failure on psphotApResid");
    281         psError(PSPHOT_ERR_PHOTOM, false, "Measure aperture photometry corrections");
    282         return false;
     187    if (!havePSF && !psphotApResid (readout, sources, recipe, psf)) {
     188        psLogMsg ("psphot", 3, "failed on psphotApResid");
     189        return psphotReadoutCleanup (config, readout, recipe, detections, psf, sources);
    283190    }
    284191
    285192    // calculate source magnitudes
    286193    pmReadout *background = psphotSelectBackground (config, view, false);
    287     psphotMagnitudes(sources, recipe, psf, background, maskVal, maskMark);
     194    psphotMagnitudes(sources, recipe, psf, background);
    288195
    289196    // replace failed sources?
     
    297204
    298205    // create the exported-metadata and free local data
    299     return psphotReadoutCleanup(config, readout, recipe, psf, sources);
     206    return psphotReadoutCleanup(config, readout, recipe, detections, psf, sources);
    300207}
Note: See TracChangeset for help on using the changeset viewer.