IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Apr 8, 2008, 8:36:06 AM (18 years ago)
Author:
eugene
Message:

merging from eam_branch_20080324 : psphot work on extended source fitting and related I/O functions

File:
1 edited

Legend:

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

    r14655 r17396  
    1 # include "psphot.h"
     1# include "psphotInternal.h"
     2# define USE_DELTA_PSF 0
    23
    34// save as static values so they may be set externally
    45static psF32 PM_SOURCE_FIT_MODEL_NUM_ITERATIONS = 15;
    56static psF32 PM_SOURCE_FIT_MODEL_TOLERANCE = 0.1;
    6 // static psF32 PM_SOURCE_FIT_MODEL_WEIGHT = 1.0;
    7 // static bool  PM_SOURCE_FIT_MODEL_PIX_WEIGHTS = true;
    87
    98// input source has both modelPSF and modelEXT.  on successful exit, we set the
    109// modelConv to contain the fitted parameters, and the modelFlux to contain the
    1110// convolved model image.
    12 bool psphotPSFConvModel (pmSource *source, psMetadata *recipe, psMaskType maskVal) {
     11pmModel *psphotPSFConvModel (pmReadout *readout, pmSource *source, pmModelType modelType, psMaskType maskVal, int psfSize) {
    1312   
    14     bool status;
    15 
    16     int psfSize = psMetadataLookupS32 (&status, recipe, "PCM_BOX_SIZE");
    17     if (!status) {
    18         psfSize = 2;
    19     }
    20 
    2113    // make sure we save a cached copy of the psf flux
    2214    pmSourceCachePSF (source, maskVal);
    2315
    2416    // convert the cached cached psf model for this source to a psKernel
    25     // XXX for the moment, hard-wire the kernel to be 5x5 (2 pix radius)
    26     // XXX for the moment, hard-wire the kernel to be 9x9 (4 pix radius)
    2717    psKernel *psf = psphotKernelFromPSF (source, psfSize);
     18    if (!psf) return NULL;
    2819
    29     // psf must be normalized (integral = 1.0)
    30     double sum = 0.0;
    31     for (int i = 0; i < psf->image->numRows; i++) {
    32         for (int j = 0; j < psf->image->numCols; j++) {
    33             sum += psf->image->data.F32[i][j];
    34         }
    35     }
    36     assert (sum > 0.0);
    37     for (int i = 0; i < psf->image->numRows; i++) {
    38         for (int j = 0; j < psf->image->numCols; j++) {
    39             psf->image->data.F32[i][j] /= sum;
    40         }
    41     }
    42 
    43 # if (0)
    44     // XXX sanity check: convolve with delta function should behave like unconvolved version
    45     for (int i = 0; i < psf->image->numRows; i++) {
    46         for (int j = 0; j < psf->image->numCols; j++) {
    47             psf->image->data.F32[i][j] = 0.0;
    48         }
    49     }
    50     psf->image->data.F32[2][2] = 1.0;
     20# if (USE_DELTA_PSF)
     21    psImageInit (psf->image, 0.0);
     22    psf->image->data.F32[(int)(0.5*psf->image->numRows)][(int)(0.5*psf->image->numCols)] = 1.0;
    5123# endif
    5224
     
    5426    // XXX we could modify the parameter values or even the model
    5527    // here based on the observed seeing (some lookup table...)
    56     pmModel *modelConv = pmModelCopy (source->modelEXT);
     28
     29    // use the source moments, etc to guess basic model parameters
     30    pmModel *modelConv = pmSourceModelGuess (source, modelType);
     31    if (!modelConv) {
     32        psFree (psf);
     33        return NULL;
     34    }
     35
     36    // XXX test : modify the Io, SXX, SYY terms based on the psf SXX, SYY terms:
     37    psEllipseShape psfShape;
     38    psfShape.sx  = source->modelPSF->params->data.F32[PM_PAR_SXX] / M_SQRT2;
     39    psfShape.sxy = source->modelPSF->params->data.F32[PM_PAR_SXY];
     40    psfShape.sy  = source->modelPSF->params->data.F32[PM_PAR_SYY] / M_SQRT2;
     41    psEllipseAxes psfAxes = psEllipseShapeToAxes (psfShape, 20.0);
     42
     43    // XXX test : modify the Io, SXX, SYY terms based on the psf SXX, SYY terms:
     44    psEllipseShape extShape;
     45    extShape.sx  = modelConv->params->data.F32[PM_PAR_SXX] / M_SQRT2;
     46    extShape.sxy = modelConv->params->data.F32[PM_PAR_SXY];
     47    extShape.sy  = modelConv->params->data.F32[PM_PAR_SYY] / M_SQRT2;
     48    psEllipseAxes extAxes = psEllipseShapeToAxes (extShape, 20.0);
     49
     50    // decrease the initial guess ellipse by psf_minor axis:
     51    psEllipseAxes extAxesMod;
     52    extAxesMod.major = sqrt (PS_MAX (1.0, PS_SQR(extAxes.major) - PS_SQR(psfAxes.minor)));
     53    extAxesMod.minor = sqrt (PS_MAX (1.0, PS_SQR(extAxes.minor) - PS_SQR(psfAxes.minor)));
     54    extAxesMod.theta = extAxes.theta;
     55
     56    psEllipseShape extShapeMod = psEllipseAxesToShape (extAxesMod);
     57    modelConv->params->data.F32[PM_PAR_SXX] = extShapeMod.sx * M_SQRT2;
     58    modelConv->params->data.F32[PM_PAR_SXY] = extShapeMod.sxy;
     59    modelConv->params->data.F32[PM_PAR_SYY] = extShapeMod.sy * M_SQRT2;
     60
     61    // increase the initial guess central intensity by 2pi r^2:
     62    modelConv->params->data.F32[PM_PAR_I0] *= (1.0 + PS_SQR(psfAxes.minor) / PS_SQR(extAxesMod.minor));
     63
    5764    psVector *params  = modelConv->params;
    5865    psVector *dparams = modelConv->dparams;
     66
     67    psphotCheckRadiusEXT (readout, source, modelConv);
    5968
    6069    // create the minimization constraints
     
    91100    psTrace ("psphot", 4, "niter: %d, chisq: %f", myMin->iter, myMin->value);
    92101
    93     // renormalize output model image
     102    // renormalize output model image (generated by fitting process)
    94103    float Io = params->data.F32[PM_PAR_I0];
    95104    for (int iy = 0; iy < source->modelFlux->numRows; iy++) {
     
    115124    onPic &= (params->data.F32[PM_PAR_YPOS] >= source->pixels->row0);
    116125    onPic &= (params->data.F32[PM_PAR_YPOS] <  source->pixels->row0 + source->pixels->numRows);
    117     if (!onPic) {
    118         modelConv->flags |= PM_MODEL_STATUS_OFFIMAGE;
    119     }
     126    if (!onPic) modelConv->flags |= PM_MODEL_STATUS_OFFIMAGE;
    120127
    121     source->mode |= PM_SOURCE_MODE_FITTED;
    122     source->modelConv = modelConv;
     128    source->mode |= PM_SOURCE_MODE_FITTED; // XXX is this needed?
    123129
     130    psFree(psf);
    124131    psFree(myMin);
    125132    psFree(covar);
    126133    psFree(constraint);
    127134
    128     bool retval = (onPic && fitStatus);
    129     psTrace("psphot", 5, "---- %s(%d) end ----\n", __func__, retval);
    130     return(retval);
     135    return modelConv;
    131136}
Note: See TracChangeset for help on using the changeset viewer.