IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jan 11, 2012, 6:57:13 AM (15 years ago)
Author:
eugene
Message:

finish basic coding for model variance weighting

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20111122/psphot/src/psphotFitSourcesLinear.c

    r33071 r33086  
    1212static bool SetBorderMatrixElements (psSparseBorder *border, pmReadout *readout, psArray *sources, bool constant_weights, int SKY_FIT_ORDER, psImageMaskType markVal);
    1313
     14bool psphotGenerateModelVariance (pmConfig *config, const pmFPAview *view, int index, psMetadata *recipe, pmReadout *readout, psArray *sources);
     15pmSourceFitVarMode psphotGetFitVarMode (psMetadata *recipe);
     16bool psphotFreeModelVariance (pmReadout *readout, psArray *sources);
     17
    1418// for now, let's store the detections on the readout->analysis for each readout
    1519bool psphotFitSourcesLinear (pmConfig *config, const pmFPAview *view, const char *filerule, bool final)
     
    2933        return false;
    3034    }
     35    // MODEL_VAR requires 2 passes -- in the first, we get the rough fluxes; in the second, we
     36    // use the flux to define the model variance before fitting the objects.  Other modes only
     37    // do a single pass.
    3138    pmSourceFitVarMode fitVarModePass1 = (fitVarMode == PM_SOURCE_PHOTFIT_MODEL_VAR) ? PM_SOURCE_PHOTFIT_CONST : fitVarMode;
    3239
     
    6673        // re-run the fit against that variance.
    6774        if (fitVarMode == PM_SOURCE_PHOTFIT_MODEL_VAR) {
    68             // generate the model variance image
    69             if (!psphotGenerateModelVariance (recipe, readout, sources, psf, final, fitVarMode)) {
     75            // generate the model variance image & source pointers
     76            if (!psphotGenerateModelVariance (config, view, i, recipe, readout, sources)) {
    7077                psError (PSPHOT_ERR_CONFIG, false, "failed to fit sources (linear) for %s entry %d", filerule, i);
    7178                return false;
    7279            }
     80
     81            // replace all sources (use TMPF_SUBTRACTED as test)
     82            psphotReplaceAllSourcesReadout (config, view, filerule, i, recipe, false);
    7383
    7484            // rerun fit with correct fitVarMode
     
    7787                return false;
    7888            }
     89
     90            // free the model variance image & source pointers
     91            if (!psphotFreeModelVariance (readout, sources)) {
     92                psError (PSPHOT_ERR_CONFIG, false, "failed to fit sources (linear) for %s entry %d", filerule, i);
     93                return false;
     94            }
    7995        }
    8096
     
    86102}
    87103
     104// look up the fit variance mode from the recipe; older recipes do not have the value
     105// 'LINEAR_FIT_VARIANCE_MODE'; in those cases, look for 'CONSTANT_PHOTOMETRIC_WEIGHTS' as a boolean and
     106// set the value to either CONST or IMAGE_VAR
    88107pmSourceFitVarMode psphotGetFitVarMode (psMetadata *recipe) {
    89108
     
    96115            psAbort("You must provide a value for LINEAR_FIT_VARIANCE_MODE or CONSTANT_PHOTOMETRIC_WEIGHTS");
    97116        }
    98         pmsourceFitVarMode fitVarMode = CONSTANT_PHOTOMETRIC_WEIGHTS ? PM_SOURCE_PHOTFIT_CONST : PM_SOURCE_PHOTFIT_IMAGE_VAR;
     117        pmSourceFitVarMode fitVarMode = CONSTANT_PHOTOMETRIC_WEIGHTS ? PM_SOURCE_PHOTFIT_CONST : PM_SOURCE_PHOTFIT_IMAGE_VAR;
    99118        return fitVarMode;
    100119    }
     
    109128    }
    110129    psError (PSPHOT_ERR_CONFIG, false, "Invalid value for LINEAR_FIT_VARIANCE_MODE (%s)", fitVarModeString);
    111     return PM_SOURCE_PHOTFIT_MODEL_NONE;
     130    return PM_SOURCE_PHOTFIT_NONE;
    112131}
    113132
     
    182201        // psAssert (source->peak, "source without peak??");
    183202        // psAssert (source->peak->footprint, "peak without footprint??");
    184 
    185         // XXX TEST
    186         if (source->mode2 & PM_SOURCE_MODE2_MATCHED) {
    187             fprintf (stderr, "forced photometry on supplied source\n");
    188         }
    189203
    190204        // turn this bit off and turn it on again if we pass this test
     
    381395
    382396    // set the sky, sky_x, sky_y components of border matrix
    383     SetBorderMatrixElements (border, readout, fitSources, CONSTANT_PHOTOMETRIC_WEIGHTS, SKY_FIT_ORDER, markVal);
     397    SetBorderMatrixElements (border, readout, fitSources, (fitVarMode == PM_SOURCE_PHOTFIT_CONST), SKY_FIT_ORDER, markVal);
    384398
    385399    psSparseConstraint constraint;
     
    540554}
    541555
    542 bool psphotGenerateModelVariance (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe) {
     556bool psphotGenerateModelVariance (pmConfig *config, const pmFPAview *view, int index, psMetadata *recipe, pmReadout *readout, psArray *sources) {
     557
     558    bool status = false;
     559    psRegion fullRegion = psRegionSet (0, 0, 0, 0);
     560
     561    // bit-masks to test for good/bad pixels
     562    psImageMaskType maskVal = psMetadataLookupImageMask(&status, recipe, "MASK.PSPHOT");
     563    assert (maskVal);
     564
     565    // bit-mask to mark pixels not used in analysis
     566    psImageMaskType markVal = psMetadataLookupImageMask(&status, recipe, "MARK.PSPHOT");
     567    assert (markVal);
     568
     569    // maskVal is used to test for rejected pixels, and must include markVal
     570    maskVal |= markVal;
    543571
    544572    // create a model variance image
    545573    psImage *modelVar = psImageCopy (NULL, readout->variance, PS_TYPE_F32);
    546574
    547     // find the pmFPAfile for the readout (background model is saved on PSPHOT.BACKMDL regardless of 'filename')
    548     pmFPAfile *backModellFile = pmFPAfileSelectSingle(config->files, "PSPHOT.BACKMDL", index); // File of interest
     575    // find the pmFPAfile for the readout (background model is saved on PSPHOT.BACKMDL regardless of 'filerule')
     576    pmFPAfile *backModelFile = pmFPAfileSelectSingle(config->files, "PSPHOT.BACKMDL", index); // File of interest
    549577    assert (backModelFile);
    550578
     
    554582
    555583    // find the binning information
    556     psImageBinning *backBinning = psMetadataLookupPtr(&status, model->analysis, "PSPHOT.BACKGROUND.BINNING");
     584    psImageBinning *backBinning = psMetadataLookupPtr(&status, backModel->analysis, "PSPHOT.BACKGROUND.BINNING");
    557585    assert (backBinning);
    558586
     
    572600        pmSource *source = sources->data[i];
    573601
    574         // XXX which sources need to be injected?
    575         // skip non-astronomical objects (very likely defects)
    576         if (source->type == PM_SOURCE_TYPE_DEFECT) continue;
    577         if (source->type == PM_SOURCE_TYPE_SATURATED) continue;
    578      
    579         // do not include CRs in the full ensemble fit
    580         if (source->mode & PM_SOURCE_MODE_CR_LIMIT) continue;
    581        
    582         // do not include MOMENTS_FAILURES in the fit
    583         if (source->mode & PM_SOURCE_MODE_MOMENTS_FAILURE) continue;
    584 
    585         // XXX need to identify the region appropriate for the source
     602        // skip sources which were not fitted already
     603        if (!(source->mode & PM_SOURCE_MODE_LINEAR_FIT)) continue;
     604
     605        // pixel region appropriate for the source
     606        psRegion region = psRegionForImage (source->pixels, fullRegion);
    586607
    587608        // define the source->modelVar pixels (view on modelVar image)
    588609        psAssert (!source->modelVar, "programming error : modelVar should be NULL here");
     610        psAssert (source->modelFlux, "programming error : modelFlux should not be NULL here");
    589611        source->modelVar = psImageSubset(readout->image, region);
    590612
    591613        // add the source model to the model variance image
    592         pmSourceAdd (source, PM_MODEL_OP_XXX, maskVal);
     614        pmSourceAdd (source, PM_MODEL_OP_MODELVAR, maskVal);
    593615    }
    594616
     
    599621    // XXX for a test:
    600622    psphotSaveImage (NULL, modelVar, "model.var.fits");
     623    psphotSaveImage (NULL, readout->image, "image.var.fits");
     624
     625    // we save the model variance for future reference
     626    psMetadataAddImage(readout->analysis, PS_LIST_TAIL, "PSPHOT.MODEL.VAR", PS_META_REPLACE, "model variance", modelVar);
     627    psFree (modelVar);
    601628
    602629    return true;
    603630}
    604631
    605 bool psphotFreeModelVariance (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe) {
     632bool psphotFreeModelVariance (pmReadout *readout, psArray *sources) {
     633
     634    bool status = false;
    606635
    607636    // find the binning information
    608     psImage *modelVar = psMetadataLookupPtr(&status, model->analysis, "PSPHOT.MODEL.VAR");
     637    psImage *modelVar = psMetadataLookupPtr(&status, readout->analysis, "PSPHOT.MODEL.VAR");
    609638    assert (modelVar);
    610639
Note: See TracChangeset for help on using the changeset viewer.