IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jun 19, 2012, 5:24:19 PM (14 years ago)
Author:
mhuber
Message:

merging latest r34040 trunk changes to branch

Location:
branches/meh_branches/ppstack_test
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/meh_branches/ppstack_test

  • branches/meh_branches/ppstack_test/psphot

  • branches/meh_branches/ppstack_test/psphot/src

  • branches/meh_branches/ppstack_test/psphot/src/psphotFitSourcesLinear.c

    r33415 r34041  
    1212static bool SetBorderMatrixElements (psSparseBorder *border, pmReadout *readout, psArray *sources, bool constant_weights, int SKY_FIT_ORDER, psImageMaskType markVal);
    1313
     14# if (HAVE_MODEL_VAR)
     15bool psphotGenerateModelVariance (pmConfig *config, const pmFPAview *view, pmFPAfile *file, int index, psMetadata *recipe, pmReadout *readout, psArray *sources);
     16pmSourceFitVarMode psphotGetFitVarMode (psMetadata *recipe);
     17bool psphotFreeModelVariance (pmReadout *readout, psArray *sources);
     18# endif
     19
    1420// for now, let's store the detections on the readout->analysis for each readout
    1521bool psphotFitSourcesLinear (pmConfig *config, const pmFPAview *view, const char *filerule, bool final)
     
    2430    assert (recipe);
    2531
     32# if (HAVE_MODEL_VAR)
     33    pmSourceFitVarMode fitVarMode = psphotGetFitVarMode (recipe);
     34    if (!fitVarMode) {
     35        psError (PSPHOT_ERR_CONFIG, true, "failed to get LINEAR_FIT_VARIANCE_MODE");
     36        return false;
     37    }
     38    // MODEL_VAR requires 2 passes -- in the first, we get the rough fluxes; in the second, we
     39    // use the flux to define the model variance before fitting the objects.  Other modes only
     40    // do a single pass.
     41    pmSourceFitVarMode fitVarModePass1 = (fitVarMode == PM_SOURCE_PHOTFIT_MODEL_VAR) ? PM_SOURCE_PHOTFIT_CONST : fitVarMode;
     42# endif
     43
    2644    int num = psphotFileruleCount(config, filerule);
    2745
     
    5068        psAssert (psf, "missing psf?");
    5169
    52         if (!psphotFitSourcesLinearReadout (recipe, readout, sources, psf, final)) {
     70# if (HAVE_MODEL_VAR)
     71        if (!psphotFitSourcesLinearReadout (recipe, readout, sources, psf, final, fitVarModePass1))
     72# else
     73        if (!psphotFitSourcesLinearReadout (recipe, readout, sources, psf, final))
     74# endif
     75        {
    5376            psError (PSPHOT_ERR_CONFIG, false, "failed to fit sources (linear) for %s entry %d", filerule, i);
    5477            return false;
    5578        }
     79
     80# if (HAVE_MODEL_VAR)
     81        // the MODEL_VAR weighting scheme requires knowledge of the model fluxes to generate the variance
     82        // after we have determined the initial set of fits, then we can generate the variance image and
     83        // re-run the fit against that variance.
     84        if (fitVarMode == PM_SOURCE_PHOTFIT_MODEL_VAR) {
     85            // generate the model variance image & source pointers
     86            if (!psphotGenerateModelVariance (config, view, file, i, recipe, readout, sources)) {
     87                psError (PSPHOT_ERR_CONFIG, false, "failed to fit sources (linear) for %s entry %d", filerule, i);
     88                return false;
     89            }
     90
     91            // replace all sources (use TMPF_SUBTRACTED as test)
     92            psphotReplaceAllSourcesReadout (config, view, filerule, i, recipe, false);
     93
     94            // rerun fit with correct fitVarMode
     95            if (!psphotFitSourcesLinearReadout (recipe, readout, sources, psf, final, fitVarMode)) {
     96                psError (PSPHOT_ERR_CONFIG, false, "failed to fit sources (linear) for %s entry %d", filerule, i);
     97                return false;
     98            }
     99
     100            // free the model variance image & source pointers
     101            if (!psphotFreeModelVariance (readout, sources)) {
     102                psError (PSPHOT_ERR_CONFIG, false, "failed to fit sources (linear) for %s entry %d", filerule, i);
     103                return false;
     104            }
     105        }
     106# endif
    56107
    57108        psphotVisualShowResidualImage (readout, (num > 0));
     
    62113}
    63114
    64 bool psphotFitSourcesLinearReadout (psMetadata *recipe, pmReadout *readout, psArray *sources, pmPSF *psf, bool final) {
    65 
     115# if (HAVE_MODEL_VAR)
     116// look up the fit variance mode from the recipe; older recipes do not have the value
     117// 'LINEAR_FIT_VARIANCE_MODE'; in those cases, look for 'CONSTANT_PHOTOMETRIC_WEIGHTS' as a boolean and
     118// set the value to either CONST or IMAGE_VAR
     119pmSourceFitVarMode psphotGetFitVarMode (psMetadata *recipe) {
     120
     121    bool status = false;
     122
     123    char *fitVarModeString = psMetadataLookupStr(&status, recipe, "LINEAR_FIT_VARIANCE_MODE");
     124    if (!status) {
     125        bool CONSTANT_PHOTOMETRIC_WEIGHTS = psMetadataLookupBool(&status, recipe, "CONSTANT_PHOTOMETRIC_WEIGHTS");
     126        if (!status) {
     127            psAbort("You must provide a value for LINEAR_FIT_VARIANCE_MODE or CONSTANT_PHOTOMETRIC_WEIGHTS");
     128        }
     129        pmSourceFitVarMode fitVarMode = CONSTANT_PHOTOMETRIC_WEIGHTS ? PM_SOURCE_PHOTFIT_CONST : PM_SOURCE_PHOTFIT_IMAGE_VAR;
     130        return fitVarMode;
     131    }
     132    if (!strcasecmp(fitVarModeString, "CONSTANT") || !strcasecmp(fitVarModeString, "CONST")) {
     133        return PM_SOURCE_PHOTFIT_CONST;
     134    }
     135    if (!strcasecmp(fitVarModeString, "IMAGE") || !strcasecmp(fitVarModeString, "IMAGE_VAR")) {
     136        return PM_SOURCE_PHOTFIT_IMAGE_VAR;
     137    }
     138    if (!strcasecmp(fitVarModeString, "MODEL") || !strcasecmp(fitVarModeString, "MODEL_VAR")) {
     139        return PM_SOURCE_PHOTFIT_MODEL_VAR;
     140    }
     141    psError (PSPHOT_ERR_CONFIG, false, "Invalid value for LINEAR_FIT_VARIANCE_MODE (%s)", fitVarModeString);
     142    return PM_SOURCE_PHOTFIT_NONE;
     143}
     144# endif
     145
     146# if (HAVE_MODEL_VAR)
     147bool psphotFitSourcesLinearReadout (psMetadata *recipe, pmReadout *readout, psArray *sources, pmPSF *psf, bool final, pmSourceFitVarMode fitVarMode)
     148# else
     149bool psphotFitSourcesLinearReadout (psMetadata *recipe, pmReadout *readout, psArray *sources, pmPSF *psf, bool final)
     150# endif
     151{
    66152    bool status;
    67153    float x;
     
    99185    if (psRegionIsNaN (AnalysisRegion)) psAbort("analysis region mis-defined");
    100186
     187# if (!HAVE_MODEL_VAR)
    101188    bool CONSTANT_PHOTOMETRIC_WEIGHTS =
    102189        psMetadataLookupBool(&status, recipe, "CONSTANT_PHOTOMETRIC_WEIGHTS");
     
    104191        psAbort("You must provide a value for the BOOL recipe CONSTANT_PHOTOMETRIC_WEIGHTS");
    105192    }
     193# endif
    106194    int SKY_FIT_ORDER = psMetadataLookupS32(&status, recipe, "SKY_FIT_ORDER");
    107195    if (!status) {
     
    231319    psSparseBorder *border = psSparseBorderAlloc (sparse, nBorder);
    232320
     321# if (HAVE_MODEL_VAR)
     322    // if fitVarMode is MODEL_VAR, then we need to generate the model image variance
     323    // XXX we have two possibilities here:
     324
     325    // 1) do 2 passes, where in the first case we use the CONST weighting, and in the second
     326    // use the fitted model values to define the model
     327
     328    // 2) do a single pass, and use the model guess to define the model variance (but do I trust the Model Guess?)
     329# endif
     330
    233331    // fill out the sparse matrix elements and border elements (B)
    234332    // SRCi is the current source of interest
     
    238336
    239337        // diagonal elements of the sparse matrix (auto-cross-product)
     338# if (HAVE_MODEL_VAR)
     339        f = pmSourceModelDotModel (SRCi, SRCi, fitVarMode, covarFactor, maskVal);
     340# else
    240341        f = pmSourceModelDotModel (SRCi, SRCi, CONSTANT_PHOTOMETRIC_WEIGHTS, covarFactor, maskVal);
     342# endif
    241343        psSparseMatrixElement (sparse, i, i, f);
    242344
     345# if (HAVE_MODEL_VAR)
     346        // if we have used CONSTANT errors, then we need to calculate the value of the parameter error
     347        if (fitVarMode != PM_SOURCE_PHOTFIT_IMAGE_VAR) {
     348            float var = pmSourceModelDotModel (SRCi, SRCi, PM_SOURCE_PHOTFIT_IMAGE_VAR, covarFactor, maskVal);
     349            errors->data.F32[i] = 1.0 / sqrt(var);
     350        } else {
     351            errors->data.F32[i] = 1.0 / sqrt(f);
     352        }
     353# else
    243354        // the formal error depends on the weighting scheme
    244355        if (CONSTANT_PHOTOMETRIC_WEIGHTS) {
     
    248359            errors->data.F32[i] = 1.0 / sqrt(f);
    249360        }
    250 
     361# endif
    251362
    252363        // find the image x model value
     364# if (HAVE_MODEL_VAR)
     365        f = pmSourceDataDotModel (SRCi, SRCi, fitVarMode, covarFactor, maskVal);
     366# else
    253367        f = pmSourceDataDotModel (SRCi, SRCi, CONSTANT_PHOTOMETRIC_WEIGHTS, covarFactor, maskVal);
     368# endif
    254369        psSparseVectorElement (sparse, i, f);
    255370
     
    257372        switch (SKY_FIT_ORDER) {
    258373          case 1:
     374# if (HAVE_MODEL_VAR)
     375            f = pmSourceModelWeight (SRCi, 1, fitVarMode, covarFactor, maskVal);
     376            psSparseBorderElementB (border, i, 1, f);
     377            f = pmSourceModelWeight (SRCi, 2, fitVarMode, covarFactor, maskVal);
     378            psSparseBorderElementB (border, i, 2, f);
     379# else
    259380            f = pmSourceModelWeight (SRCi, 1, CONSTANT_PHOTOMETRIC_WEIGHTS, covarFactor, maskVal);
    260381            psSparseBorderElementB (border, i, 1, f);
    261382            f = pmSourceModelWeight (SRCi, 2, CONSTANT_PHOTOMETRIC_WEIGHTS, covarFactor, maskVal);
    262383            psSparseBorderElementB (border, i, 2, f);
     384# endif
    263385
    264386          case 0:
     387# if (HAVE_MODEL_VAR)
     388            f = pmSourceModelWeight (SRCi, 0, fitVarMode, covarFactor, maskVal);
     389# else
    265390            f = pmSourceModelWeight (SRCi, 0, CONSTANT_PHOTOMETRIC_WEIGHTS, covarFactor, maskVal);
     391# endif
    266392            psSparseBorderElementB (border, i, 0, f);
    267393            break;
     
    283409
    284410            // got an overlap; calculate cross-product and add to output array
     411# if (HAVE_MODEL_VAR)
     412            f = pmSourceModelDotModel (SRCi, SRCj, fitVarMode, covarFactor, maskVal);
     413# else
    285414            f = pmSourceModelDotModel (SRCi, SRCj, CONSTANT_PHOTOMETRIC_WEIGHTS, covarFactor, maskVal);
     415# endif
    286416            psSparseMatrixElement (sparse, j, i, f);
    287417        }
     
    321451
    322452    // set the sky, sky_x, sky_y components of border matrix
     453# if (HAVE_MODEL_VAR)
     454    SetBorderMatrixElements (border, readout, fitSources, (fitVarMode == PM_SOURCE_PHOTFIT_CONST), SKY_FIT_ORDER, markVal);
     455# else
    323456    SetBorderMatrixElements (border, readout, fitSources, CONSTANT_PHOTOMETRIC_WEIGHTS, SKY_FIT_ORDER, markVal);
     457# endif
    324458
    325459    psSparseConstraint constraint;
     
    479613    return true;
    480614}
     615
     616# if (HAVE_MODEL_VAR)
     617bool psphotModelBackgroundReadout(psImage *model,  // Model image
     618                                  psImage *modelStdev, // Model stdev image
     619                                  psMetadata *analysis, // Analysis metadata for outputs
     620                                  pmReadout *readout, // Readout for which to generate a background model
     621                                  psImageBinning *binning, // Binning parameters
     622                                  const pmConfig *config,// Configuration
     623                                  bool useVarianceImage
     624    );
     625
     626bool psphotGenerateModelVariance (pmConfig *config, const pmFPAview *view, pmFPAfile *file, int index, psMetadata *recipe, pmReadout *readout, psArray *sources) {
     627
     628    bool status = false;
     629    psRegion fullRegion = psRegionSet (0, 0, 0, 0);
     630
     631    // bit-masks to test for good/bad pixels
     632    psImageMaskType maskVal = psMetadataLookupImageMask(&status, recipe, "MASK.PSPHOT");
     633    assert (maskVal);
     634
     635    // bit-mask to mark pixels not used in analysis
     636    psImageMaskType markVal = psMetadataLookupImageMask(&status, recipe, "MARK.PSPHOT");
     637    assert (markVal);
     638
     639    // maskVal is used to test for rejected pixels, and must include markVal
     640    maskVal |= markVal;
     641
     642    // create a model variance image
     643    psImage *modelVar = psImageCopy (NULL, readout->variance, PS_TYPE_F32);
     644
     645    // find the binning information
     646    psImageBinning *backBinning = psphotBackgroundBinning (modelVar, config);
     647    assert (backBinning);
     648   
     649    psImage *varModel = psImageAlloc(backBinning->nXruff, backBinning->nYruff, PS_TYPE_F32); // Background model
     650    psImage *varModelStdev = psImageAlloc(backBinning->nXruff, backBinning->nYruff, PS_TYPE_F32); // Background model
     651
     652    if (!psphotModelBackgroundReadout(varModel, varModelStdev, NULL, readout, backBinning, config, true)) {
     653        psError(PS_ERR_UNKNOWN, false, "Unable to generate background model");
     654        psFree (varModel);
     655        psFree (varModelStdev);
     656        return false;
     657    }
     658
     659    // linear interpolation to full-scale
     660    if (!psImageUnbin (modelVar, varModel, backBinning)) {
     661        psError (PSPHOT_ERR_PROG, true, "inconsistent sizes for unbinning");
     662        psFree (varModel);
     663        psFree (varModelStdev);
     664        return false;
     665    }
     666
     667    psFree (varModel);
     668    psFree (varModelStdev);
     669
     670    // XXX for a test:
     671    psphotSaveImage (NULL, modelVar, "model.bck.fits");
     672
     673    // insert all of the source models
     674    for (int i = 0; i < sources->n; i++) {
     675
     676        // source of interest
     677        pmSource *source = sources->data[i];
     678
     679        // skip sources which were not fitted already
     680        if (!(source->mode & PM_SOURCE_MODE_LINEAR_FIT)) continue;
     681
     682        // pixel region appropriate for the source
     683        psRegion region = psRegionForImage (source->pixels, fullRegion);
     684
     685        // define the source->modelVar pixels (view on modelVar image)
     686        psAssert (!source->modelVar, "programming error : modelVar should be NULL here");
     687        psAssert (source->modelFlux, "programming error : modelFlux should not be NULL here");
     688        psAssert (source->modelFlux->data.F32, "programming error : modelFlux should not be NULL here");
     689        source->modelVar = psImageSubset(modelVar, region);
     690
     691        // add the source model to the model variance image
     692        pmSourceAdd (source, PM_MODEL_OP_MODELVAR, maskVal);
     693    }
     694
     695    // XXX for a test:
     696    psphotSaveImage (NULL, modelVar, "model.var.fits");
     697    psphotSaveImage (NULL, readout->variance, "image.var.fits");
     698
     699    // we save the model variance for future reference
     700    psMetadataAddImage(readout->analysis, PS_LIST_TAIL, "PSPHOT.MODEL.VAR", PS_META_REPLACE, "model variance", modelVar);
     701    psFree (modelVar);
     702
     703    return true;
     704}
     705
     706bool psphotFreeModelVariance (pmReadout *readout, psArray *sources) {
     707
     708    bool status = false;
     709
     710    // find the binning information
     711    psImage *modelVar = psMetadataLookupPtr(&status, readout->analysis, "PSPHOT.MODEL.VAR");
     712    assert (modelVar);
     713
     714    psMetadataRemoveKey (readout->analysis, "PSPHOT.MODEL.VAR");
     715
     716    // clear modelVar pointers for all of the source models
     717    for (int i = 0; i < sources->n; i++) {
     718
     719        // source of interest
     720        pmSource *source = sources->data[i];
     721        psFree (source->modelVar);
     722    }
     723
     724    return true;
     725}
     726# endif
Note: See TracChangeset for help on using the changeset viewer.