IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Nov 8, 2013, 10:41:29 AM (13 years ago)
Author:
bills
Message:

implement creation of CFF files from sources. some tweaks to reading of cffs

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20130904/psModules/src/objects/pmSourceIO_CFF.c

    r36244 r36270  
    6565    int modelType = pmModelClassGetType ("PS_MODEL_GAUSS");
    6666
    67     char *PSF_NAME = psMetadataLookupStr (&status, header, "PSF_NAME");
     67    char *PSF_NAME = psMetadataLookupStr (&status, header, "PSFMODEL");
    6868    if (PSF_NAME != NULL) {
    6969        modelType = pmModelClassGetType (PSF_NAME);
     
    107107        // XXX: we need to put a lookup table in the cff header to define the correspondence of the
    108108        // model type values in the cff with our models. (We want to use an interger for efficiency
    109         // but the values depend on the array in pmModelClass.c
     109        // but the value for each model type is set on the organization of the the array in pmModelClass.c
     110        // For now use the input values verbatim and trust the user that this is valid value
    110111        int   galaxyModelType = psMetadataLookupS32(&status, row, "MODEL_TYPE");
    111112        float Sindex     = psMetadataLookupF32 (&status, row, "INDEX"); // Should this be PAR_07 not sersic index
     
    173174        }
    174175
    175         if (fitGalaxy) {
     176        // XXX: should use < 0 as invalid galaxyModelType
     177
     178        if (fitGalaxy && galaxyModelType > 0) {
    176179            source->modelFits = psArrayAllocEmpty (1);
    177180            pmModel *model = pmModelAlloc(galaxyModelType);
     
    192195                xPAR[PM_PAR_7] = 0.5 / Sindex;
    193196            }
     197            // XXX: set source->modelEXT to this model and flag as extended so that we have a better
     198            // shot at subtracting extended sources?
    194199
    195200            psArrayAdd (source->modelFits, 1, model);
     
    204209}
    205210
     211bool pmSourcesWrite_CFF(psFits *fits, psArray *sources, psMetadata *header, psMetadata *recipe) {
     212   
     213    char *extname = "SkyChip.cff";
     214   
     215    psArray *table = psArrayAllocEmpty(sources->n);
     216
     217    pmModelType sersicModelType = pmModelClassGetType("PS_MODEL_SERSCIC");
     218
     219    for (int i = 0; i < sources->n; i++) {
     220        pmSource *thisSource = sources->data[i];
     221        pmSource *source = thisSource->parent ? thisSource->parent : thisSource;
     222
     223        psF32 xPos, yPos, flux, rMajor, rMinor, theta;
     224        psS32 modelType = 0;
     225        bool fitGalaxy = false;
     226        bool psfStar = false;
     227        psS32 sersicIndex = 0;
     228        if (source->modelFits == NULL) {
     229            pmModel *model = source->modelPSF;
     230            if (model == NULL) continue;
     231            psF32 *PAR = model->params->data.F32;
     232            if (!isfinite(PAR[PM_PAR_SXX]) || !isfinite(PAR[PM_PAR_SYY])  || !isfinite(PAR[PM_PAR_SXY]) ||
     233                !isfinite(source->psfFlux)) {
     234                continue;
     235            }
     236
     237            xPos = model->params->data.F32[PM_PAR_XPOS];
     238            yPos = model->params->data.F32[PM_PAR_YPOS];
     239            flux = source->psfFlux;
     240            psfStar = (source->mode & PM_SOURCE_MODE_PSFSTAR) ? true : false;
     241            rMajor = 0;
     242            rMinor = 0;
     243            theta = 0;
     244        } else {
     245            // Choose the extended model to use for this source.
     246            // For now we use the one set as the modelEXT for this source which had the best fit.
     247            // XXX: make this controllable by recipe
     248            //   use best (as implemented here)
     249            //   choose specific type
     250            int iModel = -1;
     251            if (source->modelEXT) {
     252                pmModelType ext_model_type = source->modelEXT->type;
     253                for (int j=0; j<source->modelFits->n; j++) {
     254                    pmModel *aModel = source->modelFits->data[j];
     255                    if (aModel->type == ext_model_type) {
     256                        iModel = j;
     257                        break;
     258                    }
     259                }
     260            }
     261            if (iModel == -1) {
     262                // Can this happen? perhaps if model type is qgauss (extended but below s/n for model fits?
     263                // XXX: Should this be an assert?
     264                continue;
     265            }
     266            pmModel *model = source->modelFits->data[iModel];
     267            psF32 *PAR = model->params->data.F32;
     268            xPos = PAR[PM_PAR_XPOS];
     269            yPos = PAR[PM_PAR_YPOS];
     270            if (model->type == pmModelClassGetType("PS_MODEL_TRAIL")) {
     271                // XXX: do we need to handle this type
     272                continue;
     273            } else {
     274                if (!isfinite(PAR[PM_PAR_SXX]) || !isfinite(PAR[PM_PAR_SYY])  || !isfinite(PAR[PM_PAR_SXY]) ||
     275                    !isfinite(model->mag)) {
     276                    // bad model
     277                    continue;
     278                }
     279                psEllipseAxes axes = pmPSF_ModelToAxes (PAR, model->type);
     280                rMajor = axes.major;
     281                rMinor = axes.minor;
     282                theta  = axes.theta*PS_DEG_RAD;
     283                flux = pow(10.0, -0.4*model->mag);
     284                fitGalaxy = true;
     285            }
     286            modelType = model->type;
     287            if (modelType == sersicModelType) {
     288                sersicIndex = 0.5 / PAR[PM_PAR_7];
     289            }
     290        }
     291        psMetadata *row = psMetadataAlloc();
     292        psMetadataAddU32 (row, PS_LIST_TAIL, "ID",         0,   "IPP detection identifier",  source->seq);
     293        psMetadataAddF32 (row, PS_LIST_TAIL, "X",                0, "x coordinate",          xPos);
     294        psMetadataAddF32 (row, PS_LIST_TAIL, "Y",                0, "y coordinate",          yPos);
     295        psMetadataAddF32 (row, PS_LIST_TAIL, "FLUX",             0, "flux",                  flux);
     296        psMetadataAddF32 (row, PS_LIST_TAIL, "AP_RADIUS",        0, "aperture radius",       source->apRadius);
     297        psMetadataAddF32 (row, PS_LIST_TAIL, "KRON_RADIUS",      0, "Kron radius",           source->moments->Mrf * 2.5);
     298        psMetadataAddF32 (row, PS_LIST_TAIL, "PETRO_RADIUS",     0, "Petrosian Radius",      source->extpars ? source->extpars->petrosianRadius : NAN);
     299        psMetadataAddBool (row, PS_LIST_TAIL, "FIT_GALAXY",      0, "source has xfit",       fitGalaxy);
     300        psMetadataAddBool (row, PS_LIST_TAIL, "PSF_STAR",        0, "source was psf star",   psfStar);
     301        psMetadataAddF32 (row, PS_LIST_TAIL, "R_MAJOR",          0, "radius of major axis",  rMajor);
     302        psMetadataAddF32 (row, PS_LIST_TAIL, "R_MINOR",          0, "radius of minor axis",  rMinor);
     303        psMetadataAddF32 (row, PS_LIST_TAIL, "THETA",            0, "theta",                 theta);
     304        psMetadataAddS32 (row, PS_LIST_TAIL, "MODEL_TYPE",       0, "model type",            modelType);
     305        psMetadataAddS32 (row, PS_LIST_TAIL, "INDEX",            0, "sersic index",          sersicIndex);
     306
     307        psArrayAdd(table, 100, row);
     308    }
     309
     310    if (!psFitsWriteTable(fits, NULL, table, extname)) {
     311        psError(psErrorCodeLast(), false, "writing ext data %s\n", extname);
     312        psFree(table);
     313        psFree(header);
     314        return false;
     315    }
     316    psFree(table);
     317    // psFree(header);
     318
     319    return true;
     320}
Note: See TracChangeset for help on using the changeset viewer.