IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Sep 24, 2013, 12:09:27 PM (13 years ago)
Author:
eugene
Message:

first version of psphotFullForce which builds

Location:
branches/eam_branches/ipp-20130904/psphot/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20130904/psphot/src

    • Property svn:ignore
      •  

        old new  
        2424psphotModelTest
        2525psphotMinimal
         26psphotFullForce
  • branches/eam_branches/ipp-20130904/psphot/src/psphotGalaxyShape.c

    r36146 r36148  
    11# include "psphotInternal.h"
    22
    3 bool psphotGalaxyShape (pmConfig *config, const pmFPAview *view, const char *filerule, int pass)
     3bool psphotGalaxyShape (pmConfig *config, const pmFPAview *view, const char *filerule)
    44{
    55    bool status = true;
     
    3838        // psAssert (psf, "missing psf?");
    3939
    40         if (!psphotGalaxyShapeReadout (config, recipe, view, filerule, readout, sources, psf, i, pass)) {
     40        if (!psphotGalaxyShapeReadout (config, recipe, view, filerule, readout, sources, psf)) {
    4141            psError (PSPHOT_ERR_CONFIG, false, "failed to measure magnitudes for %s entry %d", filerule, i);
    4242            return false;
     
    4646}
    4747
    48 bool psphotGalaxyShapeReadout(pmConfig *config, psMetadata *recipe, const pmFPAview *view, const char * filerule, pmReadout *readout, psArray *sources, pmPSF *psf, int index, int pass) {
     48bool psphotGalaxyShapeReadout(pmConfig *config, psMetadata *recipe, const pmFPAview *view, const char * filerule, pmReadout *readout, psArray *sources, pmPSF *psf) {
    4949
    5050    bool status = false;
     
    5555    }
    5656
    57     psTimerStart ("psphot.kron");
     57    psTimerStart ("psphot.galaxy");
    5858
    5959    // determine the number of allowed threads
     
    6363    }
    6464
    65     bool KRON_APPLY_WEIGHT = psMetadataLookupBool (&status, recipe, "KRON_APPLY_WEIGHT");
    66     if (!status) {
    67         KRON_APPLY_WEIGHT = true;
    68     }
    69 
    70     bool KRON_APPLY_WINDOW = psMetadataLookupBool (&status, recipe, "KRON_APPLY_WINDOW");
    71     if (!status) {
    72         KRON_APPLY_WINDOW = false;
    73     }
    74 
    7565    // bit-masks to test for good/bad pixels
    7666    psImageMaskType maskVal = psMetadataLookupImageMask(&status, recipe, "MASK.PSPHOT");
     
    8373    // maskVal is used to test for rejected pixels, and must include markVal
    8474    maskVal |= markVal;
     75
     76    // what fraction of the PSF is used? (radius in pixels : 2 -> 5x5 box)
     77    int psfSize = psMetadataLookupS32 (&status, recipe, "PCM_BOX_SIZE");
     78    assert (status);
     79
     80    // source fitting parameters for extended source fits
     81    int fitIter = psMetadataLookupS32(&status, recipe, "EXT_FIT_ITER"); // Max number of fit iterations
     82    assert (status && fitIter > 0);
     83
     84    float fitMinTol = psMetadataLookupF32 (&status, recipe, "EXT_FIT_MIN_TOL"); // Fit tolerance
     85    if (!status || !isfinite(fitMinTol) || fitMinTol <= 0) {
     86        fitMinTol = psMetadataLookupF32 (&status, recipe, "PSF_FIT_TOL"); // Fit tolerance
     87        if (!status || !isfinite(fitMinTol) || fitMinTol <= 0) {
     88            psAbort("PSF_FIT_MIN_TOL (and PSF_FIT_TOL) not defined or positive");
     89        }
     90    }
     91
     92    float fitMaxTol = psMetadataLookupF32 (&status, recipe, "EXT_FIT_MAX_TOL"); // Fit tolerance
     93    if (!status || !isfinite(fitMaxTol) || fitMaxTol <= 0) {
     94        fitMaxTol = 1.0;
     95    }
     96
     97    bool chisqConvergence = psMetadataLookupBool (&status, recipe, "LMM_FIT_CHISQ_CONVERGENCE"); // Fit tolerance
     98    if (!status) {
     99        // default to the old method (chisqConvergence)
     100        chisqConvergence = true;
     101    }
     102
     103    int gainFactorMode = psMetadataLookupS32 (&status, recipe, "LMM_FIT_GAIN_FACTOR_MODE"); // Fit tolerance
     104    if (!status) {
     105        // default to the old method (chisqConvergence)
     106        gainFactorMode = 0;
     107    }
     108
     109    // perform full extended source non-linear fits?
     110    bool isInteractive = psMetadataLookupBool (&status, recipe, "EXTENDED_SOURCE_FITS_INTERACTIVE");
     111    if (!status) isInteractive = false;
     112
     113    // Define source fitting parameters for extended source fits
     114    pmSourceFitOptions *fitOptions = pmSourceFitOptionsAlloc();
     115    fitOptions->mode           = PM_SOURCE_FIT_EXT_AND_SKY;
     116    fitOptions->saveCovariance = true;  // XXX make this a user option?
     117    fitOptions->covarFactor    = psImageCovarianceFactorForAperture(readout->covariance, 10.0); // Covariance matrix
     118    fitOptions->nIter          = fitIter;
     119    fitOptions->minTol         = fitMinTol;
     120    fitOptions->maxTol         = fitMaxTol;
     121
     122    fitOptions->gainFactorMode   = gainFactorMode;
     123    fitOptions->chisqConvergence = chisqConvergence;
     124    fitOptions->isInteractive    = isInteractive;
    85125
    86126    // source analysis is done in S/N order (brightest first)
     
    105145
    106146            // allocate a job -- if threads are not defined, this just runs the job
    107             psThreadJob *job = psThreadJobAlloc ("PSPHOT_KRON_ITERATE");
     147            psThreadJob *job = psThreadJobAlloc ("PSPHOT_GALAXY_SHAPES");
    108148
    109149            psArrayAdd(job->args, 1, readout);
    110150            psArrayAdd(job->args, 1, cells->data[j]); // sources
    111             PS_ARRAY_ADD_SCALAR(job->args, markVal,            PS_TYPE_IMAGE_MASK);
    112             PS_ARRAY_ADD_SCALAR(job->args, maskVal,            PS_TYPE_IMAGE_MASK);
    113             PS_ARRAY_ADD_SCALAR(job->args, pass,               PS_TYPE_S32);
     151            psArrayAdd(job->args, 1, fitOptions);
     152            PS_ARRAY_ADD_SCALAR(job->args, markVal, PS_TYPE_IMAGE_MASK);
     153            PS_ARRAY_ADD_SCALAR(job->args, maskVal, PS_TYPE_IMAGE_MASK);
     154            PS_ARRAY_ADD_SCALAR(job->args, psfSize, PS_TYPE_S32);
    114155
    115156// set this to 0 to run without threading
     
    146187    psFree (cellGroups);
    147188
    148     psLogMsg ("psphot.kron", PS_LOG_WARN, "measure kron fluxes : %f sec for %ld objects\n", psTimerMark ("psphot.kron"), sources->n);
     189    psLogMsg ("psphot.galaxy", PS_LOG_WARN, "measure galaxy shapes : %f sec for %ld objects\n", psTimerMark ("psphot.galaxy"), sources->n);
    149190    return true;
    150191}
     
    152193bool psphotGalaxyShape_Threaded (psThreadJob *job) {
    153194
    154     pmReadout *readout              = job->args->data[0];
    155     psArray *sources                = job->args->data[2];
    156     psImageMaskType markVal         = PS_SCALAR_VALUE(job->args->data[4],PS_TYPE_IMAGE_MASK_DATA);
    157     psImageMaskType maskVal         = PS_SCALAR_VALUE(job->args->data[5],PS_TYPE_IMAGE_MASK_DATA);
     195    pmReadout *readout             = job->args->data[0];
     196    psArray *sources               = job->args->data[1];
     197    pmSourceFitOptions *fitOptions = job->args->data[2];
     198    psImageMaskType markVal        = PS_SCALAR_VALUE(job->args->data[3],PS_TYPE_IMAGE_MASK_DATA);
     199    psImageMaskType maskVal        = PS_SCALAR_VALUE(job->args->data[4],PS_TYPE_IMAGE_MASK_DATA);
     200    int psfSize                    = PS_SCALAR_VALUE(job->args->data[5],S32);
    158201
    159202    for (int i = 0; i < sources->n; i++) {
    160 
    161         // XXX what do i need for the kron flux (given a supplied kron radius?)
    162203
    163204        pmSource *source = sources->data[i];
     
    184225
    185226        // re-allocate image, weight, mask arrays for each peak with box big enough to fit BIG_RADIUS
    186         bool extend = pmSourceRedefinePixels (source, readout, source->peak->x, source->peak->y, windowRadius + 2);
     227        pmSourceRedefinePixels (source, readout, source->peak->x, source->peak->y, windowRadius + 2);
    187228        psAssert (source->pixels, "WTF?");
    188229
    189230        // this function populates moments->Mrf,GalaxyShape,GalaxyShapeErr
    190231        // do the following for a set of shapes (Ex,Ey)
    191         psphotGalaxyShapeSource (source, maskVal);
     232        psphotGalaxyShapeGrid (source, fitOptions, maskVal, psfSize);
    192233        psImageMaskPixels (source->maskObj, "AND", PS_NOT_IMAGE_MASK(markVal));
    193234
     
    198239}
    199240
    200 bool psphotGalaxyShapeGrid (pmSource *source, psImageMaskType maskVal, float psfSize) {
    201 
    202     pmPCMdata *pcm = pmPCMinit (source, &options, model, maskVal, psfSize);
    203 
    204   // I have some source guess (e0, e1, e2)
    205   psEllipsePol guessPol;
    206 
    207   // I am going to retain the value of e0, and grid search around e1,e2
    208   // I need to study a valid range (and distribution?) for e1,e2)
    209 
    210   for (float de1 = -0.2; de1 <= +0.2; de1 += 0.1) {
    211     for (float de2 = -0.2; de2 <= +0.2; de2 += 0.1) {
     241bool psphotGalaxyShapeGrid (pmSource *source, pmSourceFitOptions *fitOptions, psImageMaskType maskVal, int psfSize) {
     242
     243    // allocate the model
     244    // XXX need to figure out how to get the model into the program
     245    pmModelType modelType = pmModelClassGetType ("PS_MODEL_EXP");
     246
     247    pmModel *model = pmModelAlloc(modelType);
     248    if (!model) {
     249        return NULL;
     250    }
     251
     252    pmPCMdata *pcm = pmPCMinit (source, fitOptions, model, maskVal, psfSize);
     253
     254    psF32 *PAR = pcm->modelConv->params->data.F32;
     255
     256    // I have some source guess (e0, e1, e2)
     257    psEllipsePol guessPol;
     258
     259    // I am going to retain the value of e0, and grid search around e1,e2
     260    // I need to study a valid range (and distribution?) for e1,e2)
     261
     262    for (float de1 = -0.2; de1 <= +0.2; de1 += 0.1) {
     263        for (float de2 = -0.2; de2 <= +0.2; de2 += 0.1) {
    212264 
    213       psEllipsePol testPol = guessPol;
    214       testPol.e1 += de1;
    215       testPol.e2 += de2;
    216 
    217       psEllipseShape shape;
    218       psEllipsePolToShape (&shape, testPol);
    219      
    220       PAR[PM_PAR_SXX] = shape.sxx;
    221       PAR[PM_PAR_SXY] = shape.sxy;
    222       PAR[PM_PAR_SYY] = shape.syy;
    223 
    224       // where do the results go??
    225       psphotGalaxyShapeSource (pcm, source, maskVal);
    226     }
    227   }
    228 
    229   return true;
     265            psEllipsePol testPol = guessPol;
     266            testPol.e1 += de1;
     267            testPol.e2 += de2;
     268
     269            psEllipseAxes axes = psEllipsePolToAxes (testPol, 0.1);
     270
     271            pmPSF_AxesToModel (PAR, axes, modelType);
     272
     273            // where do the results go??
     274            psphotGalaxyShapeSource (pcm, source, maskVal, psfSize);
     275        }
     276    }
     277
     278    return true;
    230279}
    231280
    232281// fit the given model to the source and find chisq & normalization
    233282// XXX is this a single-component model? sersic with a supplied index, Reff, axis ratio, and theta?
    234 bool psphotGalaxyShapeSource (pmPCMdata *pcm, pmSource *source, psImageMaskType maskVal) {
     283bool psphotGalaxyShapeSource (pmPCMdata *pcm, pmSource *source, psImageMaskType maskVal, int psfSize) {
    235284
    236285    PS_ASSERT_PTR_NON_NULL(source, false);
    237286    PS_ASSERT_PTR_NON_NULL(source->peak, false);
    238287    PS_ASSERT_PTR_NON_NULL(source->pixels, false);
    239 
    240     psF32 *PAR = pcm->modelConv->params->data.F32;
    241288
    242289    // generate the modelFlux
     
    249296
    250297    for (int iy = 0; iy < source->pixels->numRows; iy++) {
    251       for (int ix = 0; ix < source->pixels->numCols; ix++) {
    252         // skip masked points
    253         if (source->maskObj->data.PS_TYPE_IMAGE_MASK_DATA[iy][ix]) {
    254           continue;
     298        for (int ix = 0; ix < source->pixels->numCols; ix++) {
     299            // skip masked points
     300            if (source->maskObj->data.PS_TYPE_IMAGE_MASK_DATA[iy][ix]) {
     301                continue;
     302            }
     303            // skip zero-variance points
     304            if (source->variance->data.F32[iy][ix] == 0) {
     305                continue;
     306            }
     307            // skip nan value points
     308            if (!isfinite(source->pixels->data.F32[iy][ix])) {
     309                continue;
     310            }
     311
     312            float fy = source->pixels->data.F32[iy][ix];
     313            float fm = source->modelFlux->data.F32[iy][ix];
     314            float wt = (usePoisson) ? 1.0 / source->variance->data.F32[iy][ix] : 1.0;
     315
     316            YY += PS_SQR(fy) * wt;
     317            YM += fm * fy * wt;
     318            MM += PS_SQR(fm) * wt;
    255319        }
    256         // skip zero-variance points
    257         if (source->variance->data.F32[iy][ix] == 0) {
    258           continue;
    259         }
    260         // skip nan value points
    261         if (!isfinite(source->pixels->data.F32[iy][ix])) {
    262           continue;
    263         }
    264 
    265         float fy = source->pixels->data.F32[iy][ix];
    266         float fm = source->modelFlux->data.F32[iy][ix];
    267         float wt = (usePoisson) ? 1.0 / source->variance->data.F32[iy][ix] : 1.0;
    268 
    269         YY += PS_SQR(fy) * wt;
    270         YM += fm * fy * wt;
    271         MM += PS_SQR(fm) * wt;
    272       }
    273320    }
    274321
     
    276323    float Chisq = YY - 2 * Io * YM + Io * Io * MM;
    277324
    278     return true;
    279 }
     325    fprintf (stderr, "Io: %f, Chisq: %f\n", Io, Chisq);
     326
     327    return true;
     328}
Note: See TracChangeset for help on using the changeset viewer.