IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Apr 21, 2014, 5:42:34 AM (12 years ago)
Author:
eugene
Message:

merge changes from trunk

Location:
branches/eam_branches/ps2-tc3-20130727
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ps2-tc3-20130727

  • branches/eam_branches/ps2-tc3-20130727/psModules

    • Property svn:mergeinfo deleted
  • branches/eam_branches/ps2-tc3-20130727/psModules/src/objects

    • Property svn:ignore
      •  

        old new  
        1212pmSourceIO_CMF_PS1_V1.v1.c
        1313pmSourceIO_CMF_PS1_V4.c
         14pmSourceIO_CMF_PS1_V5.c
        1415pmSourceIO_CMF_PS1_SV1.c
        1516pmSourceIO_CMF_PS1_SV2.c
  • branches/eam_branches/ps2-tc3-20130727/psModules/src/objects/pmSourceIO_CMF.c.in

    r35768 r36680  
    4444#include "pmSourceDiffStats.h"
    4545#include "pmSourceSatstar.h"
     46#include "pmSourceLensing.h"
    4647#include "pmSource.h"
    4748#include "pmSourceFitModel.h"
     
    178179        @>PS1_V2,PS1_SV?@         psMetadataAdd (row, PS_LIST_TAIL, "MOMENTS_M4C",      PS_DATA_F32, "fourth momemt cos theta",                    moments.M_c4);
    179180        @>PS1_V2,PS1_SV?@         psMetadataAdd (row, PS_LIST_TAIL, "MOMENTS_M4S",      PS_DATA_F32, "fourth momemt sin theta",                    moments.M_s4);
     181
     182        // Lensing parameters:
     183        if (source->lensingOBJ && source->lensingOBJ->smear) {
     184          @>PS1_V4@               psMetadataAdd (row, PS_LIST_TAIL, "LENS_X11_SM_OBJ",  PS_DATA_F32, "smear polarizability element (objects)",     source->lensingOBJ->smear->X11);
     185          @>PS1_V4@               psMetadataAdd (row, PS_LIST_TAIL, "LENS_X12_SM_OBJ",  PS_DATA_F32, "smear polarizability element (objects)",     source->lensingOBJ->smear->X12);
     186          @>PS1_V4@               psMetadataAdd (row, PS_LIST_TAIL, "LENS_X22_SM_OBJ",  PS_DATA_F32, "smear polarizability element (objects)",     source->lensingOBJ->smear->X22);
     187          @>PS1_V4@               psMetadataAdd (row, PS_LIST_TAIL, "LENS_E1_SM_OBJ",   PS_DATA_F32, "smear polarizability element (objects)",     source->lensingOBJ->smear->e1);
     188          @>PS1_V4@               psMetadataAdd (row, PS_LIST_TAIL, "LENS_E2_SM_OBJ",   PS_DATA_F32, "smear polarizability element (objects)",     source->lensingOBJ->smear->e2);
     189        }
     190
     191        if (source->lensingOBJ && source->lensingOBJ->shear) {
     192          @>PS1_V4@               psMetadataAdd (row, PS_LIST_TAIL, "LENS_X11_SH_OBJ",  PS_DATA_F32, "shear polarizability element (objects)",     source->lensingOBJ->shear->X11);
     193          @>PS1_V4@               psMetadataAdd (row, PS_LIST_TAIL, "LENS_X12_SH_OBJ",  PS_DATA_F32, "shear polarizability element (objects)",     source->lensingOBJ->shear->X12);
     194          @>PS1_V4@               psMetadataAdd (row, PS_LIST_TAIL, "LENS_X22_SH_OBJ",  PS_DATA_F32, "shear polarizability element (objects)",     source->lensingOBJ->shear->X22);
     195          @>PS1_V4@               psMetadataAdd (row, PS_LIST_TAIL, "LENS_E1_SH_OBJ",   PS_DATA_F32, "shear polarizability element (objects)",     source->lensingOBJ->shear->e1);
     196          @>PS1_V4@               psMetadataAdd (row, PS_LIST_TAIL, "LENS_E2_SH_OBJ",   PS_DATA_F32, "shear polarizability element (objects)",     source->lensingOBJ->shear->e2);
     197        }
     198
     199        if (source->lensingOBJ && source->lensingPSF->smear) {
     200          @>PS1_V4@               psMetadataAdd (row, PS_LIST_TAIL, "LENS_X11_SM_PSF",  PS_DATA_F32, "smear polarizability element (objects)",     source->lensingPSF->smear->X11);
     201          @>PS1_V4@               psMetadataAdd (row, PS_LIST_TAIL, "LENS_X12_SM_PSF",  PS_DATA_F32, "smear polarizability element (objects)",     source->lensingPSF->smear->X12);
     202          @>PS1_V4@               psMetadataAdd (row, PS_LIST_TAIL, "LENS_X22_SM_PSF",  PS_DATA_F32, "smear polarizability element (objects)",     source->lensingPSF->smear->X22);
     203          @>PS1_V4@               psMetadataAdd (row, PS_LIST_TAIL, "LENS_E1_SM_PSF",   PS_DATA_F32, "smear polarizability element (objects)",     source->lensingPSF->smear->e1);
     204          @>PS1_V4@               psMetadataAdd (row, PS_LIST_TAIL, "LENS_E2_SM_PSF",   PS_DATA_F32, "smear polarizability element (objects)",     source->lensingPSF->smear->e2);
     205        }
     206
     207        if (source->lensingOBJ && source->lensingPSF->shear) {
     208          @>PS1_V4@               psMetadataAdd (row, PS_LIST_TAIL, "LENS_X11_SH_PSF",  PS_DATA_F32, "shear polarizability element (objects)",     source->lensingPSF->shear->X11);
     209          @>PS1_V4@               psMetadataAdd (row, PS_LIST_TAIL, "LENS_X12_SH_PSF",  PS_DATA_F32, "shear polarizability element (objects)",     source->lensingPSF->shear->X12);
     210          @>PS1_V4@               psMetadataAdd (row, PS_LIST_TAIL, "LENS_X22_SH_PSF",  PS_DATA_F32, "shear polarizability element (objects)",     source->lensingPSF->shear->X22);
     211          @>PS1_V4@               psMetadataAdd (row, PS_LIST_TAIL, "LENS_E1_SH_PSF",   PS_DATA_F32, "shear polarizability element (objects)",     source->lensingPSF->shear->e1);
     212          @>PS1_V4@               psMetadataAdd (row, PS_LIST_TAIL, "LENS_E2_SH_PSF",   PS_DATA_F32, "shear polarizability element (objects)",     source->lensingPSF->shear->e2);
     213        }
    180214
    181215        @>PS1_V2,PS1_SV?,>PS1_DV1@ psMetadataAdd (row, PS_LIST_TAIL, "MOMENTS_R1",       PS_DATA_F32, "first radial moment",                        moments.Mrf);
     
    708742            return false;
    709743        }
    710         // Find the source with this sequence number.
    711         // XXX: I am assuming that sources is sorted in order of seq
     744        // Find the source with this sequence number using the sourceIndex.
    712745        long seq = psMetadataLookupU32 (&status, row, "IPP_IDET");
    713         pmSource *source = NULL;
    714 #ifndef ASSUME_SORTED
    715         long j = seq < sources->n ? seq : sources->n - 1;
    716         for (; j >= 0; j--) {
    717             source = sources->data[j];
    718             if (source->seq == seq) {
    719                 break;
    720             }
    721         }
    722 #else
    723746        long j = sourceIndex[seq];
    724747        psAssert(j >= 0 && j < sources->n, "invalid sourceIndex");
    725         source = sources->data[j];
    726 #endif
     748        pmSource *source = sources->data[j];
    727749        if (!source) {
    728750            psError(PS_ERR_UNKNOWN, false, "Failed to find source for row %ld sequence number %ld\n", i, seq);
     
    789811    char name[64];
    790812
     813    pmModelType modelTypeTrail = pmModelClassGetType("PS_MODEL_TRAIL");
     814
    791815    // create a header to hold the output data
    792816    psMetadata *outhead = psMetadataAlloc ();
    793817
     818    pmModelClassWriteHeader(outhead);
     819
    794820    // write the links to the image header
    795821    psMetadataAddStr (outhead, PS_LIST_TAIL, "EXTNAME", PS_META_REPLACE, "xsrc table extension", extname);
     
    798824    sources = psArraySort (sources, pmSourceSortByFlux);
    799825
    800     @>PS1_DV2@ float magOffset;
    801     @>PS1_DV2@ float zeroptErr;
    802     @>PS1_DV2@ float fwhmMajor;
    803     @>PS1_DV2@ float fwhmMinor;
    804     @>PS1_DV2@ pmSourceOutputsCommonValues (&magOffset, &zeroptErr, &fwhmMajor, &fwhmMinor, readout, imageHeader);
     826    float magOffset;
     827    float zeroptErr;
     828    float fwhmMajor;
     829    float fwhmMinor;
     830    pmSourceOutputsCommonValues (&magOffset, &zeroptErr, &fwhmMajor, &fwhmMinor, readout, imageHeader);
    805831
    806832    // we are writing one row per model; we need to write out same number of columns for each row: find the max Nparams
     
    823849    @>PS1_DV2@ pmChip *chip = readout->parent->parent;
    824850
     851    pmModelStatus badModel = PM_MODEL_STATUS_NONE;
     852    badModel |= PM_MODEL_STATUS_BADARGS;
     853    badModel |= PM_MODEL_STATUS_OFFIMAGE;
     854    badModel |= PM_MODEL_STATUS_NAN_CHISQ;
     855    badModel |= PM_MODEL_SERSIC_PCM_FAIL_GUESS;
     856    badModel |= PM_MODEL_SERSIC_PCM_FAIL_GRID;
     857    badModel |= PM_MODEL_PCM_FAIL_GUESS;
     858
    825859    table = psArrayAllocEmpty (sources->n);
    826860
     
    847881
    848882            // skip models which were not actually fitted
    849             if (model->flags & PM_MODEL_STATUS_BADARGS) continue;
     883            // XXX
     884            if (model->flags & badModel) continue;
    850885
    851886            PAR = model->params->data.F32;
     
    853888            xPos = PAR[PM_PAR_XPOS];
    854889            yPos = PAR[PM_PAR_YPOS];
    855             xErr = dPAR[PM_PAR_XPOS];
    856             yErr = dPAR[PM_PAR_YPOS];
     890
     891            // for the extended source models, we do not always fit the centroid in the non-linear fitting process
     892            // current situation (hard-wired into psphotSourceFits.c:psphotFitPCM,
     893            // SERSIC, DEV, EXP : X,Y not fitted (PCM and not PCM)
     894            // TRAIL : X,Y are fitted
     895            //
     896           
     897            // XXX this should be based on what happened, not on the model type
     898            if (model->type == modelTypeTrail) {
     899                xErr = dPAR[PM_PAR_XPOS];
     900                yErr = dPAR[PM_PAR_YPOS];
     901            } else {
     902                // this is definitely an underestimate since it does not
     903                // account for the extent of the source
     904                xErr = fwhmMajor * model->magErr / 2.35;
     905                yErr = fwhmMinor * model->magErr / 2.35;
     906            }
    857907
    858908            @>PS1_DV2@ psSphere ptSky = {0.0, 0.0, 0.0, 0.0};
     
    870920            row = psMetadataAlloc ();
    871921
    872             // XXX we are not writing out the mode (flags) or the type (psf, ext, etc)
    873922            // the psMetadataAdd entry and the double quotes are used by grep to select the output fields for automatic documentation
    874923            // This set of psMetadataAdd Entries marks the "----" "Start of the XFIT segment"
     
    888937            @>PS1_DV2@ float calMag = isfinite(magOffset) ? model->mag + magOffset : NAN;
    889938            @>PS1_DV2@ psMetadataAdd (row, PS_LIST_TAIL, "EXT_CAL_MAG", PS_DATA_F32, "EXT Magnitude using supplied calibration",   calMag);
    890             @>PS1_DV2@ psMetadataAdd (row, PS_LIST_TAIL, "EXT_CHISQ",   PS_DATA_F32, "EXT Magnitude using supplied calibration",   model->chisq);
    891             @>PS1_DV2@ psMetadataAdd (row, PS_LIST_TAIL, "EXT_NDOF",    PS_DATA_S32, "EXT Magnitude using supplied calibration",   model->nDOF);
     939            @>PS1_DV2,PS1_SV?@ psMetadataAdd (row, PS_LIST_TAIL, "EXT_CHISQ",   PS_DATA_F32, "EXT Model Chisq",   model->chisq);
     940            @>PS1_DV2,PS1_SV?@ psMetadataAdd (row, PS_LIST_TAIL, "EXT_NDOF",    PS_DATA_S32, "EXT Model num degrees of freedom",   model->nDOF);
     941            @>PS1_SV1,PS1_SV?@ psMetadataAdd (row, PS_LIST_TAIL, "EXT_MODEL_TYPE",    PS_DATA_S32, "type for chosen EXT_MODEL",   source->modelEXT ? source->modelEXT->type : -1);
     942
     943            // EAM : adding for PV2 outputs:
     944            @>PS1_SV1@ psMetadataAdd (row, PS_LIST_TAIL, "EXT_FLAGS", PS_DATA_S16, "model fit flags (pmModelStatus)", source->modelEXT ? source->modelEXT->flags : 0);
    892945
    893946            @>PS1_DV2@ psMetadataAddF32 (row, PS_LIST_TAIL, "POSANGLE",   0, "position angle at source (degrees)",         posAngle);
     
    946999
    9471000                snprintf (name, 64, "EXT_PAR_%02d", k);
    948 
     1001               
    9491002                if (k < model->params->n) {
    9501003                    psMetadataAddF32 (row, PS_LIST_TAIL, name, 0, "", model->params->data.F32[k]);
     
    9561009            // optionally, write out the covariance matrix values
    9571010            // XXX do I need to pad this to match the biggest covar matrix?
    958             if (model->covar) {
     1011            if (false && model->covar) {
    9591012                for (int iy = 0; iy < model->covar->numCols; iy++) {
    9601013                    for (int ix = iy; ix < model->covar->numCols; ix++) {
     
    10051058        return false;
    10061059    }
     1060    // set up the lookup table to translate between input model types and output model types
     1061    // if not defined it is assumed that the tables are the same
     1062    pmModelClassReadHeader(tableHeader);
    10071063
    10081064    for (long i = 0; i < numSources; i++) {
     
    10131069            return false;
    10141070        }
    1015         // Find the source with this sequence number.
    1016         // XXX: I am assuming that sources is sorted in order of seq.
    10171071        long seq = psMetadataLookupU32 (&status, row, "IPP_IDET");
    1018         long j = seq < sources->n ? seq : sources->n - 1;
    1019         pmSource *source = NULL;
    1020         for (; j >= 0; j--) {
    1021             source = sources->data[j];
    1022             if (source->seq == seq) {
    1023                 break;
    1024             }
    1025         }
     1072        long j = sourceIndex[seq];
     1073        psAssert(j >= 0 && j < sources->n, "invalid sourceIndex");
     1074        pmSource *source = sources->data[j];
    10261075        if (!source) {
    10271076            psError(PS_ERR_UNKNOWN, false, "Failed to find source for row %ld sequence number %ld\n", i, seq);
     
    10571106        model->mag = psMetadataLookupF32(&status, row, "EXT_INST_MAG");
    10581107        model->magErr = psMetadataLookupF32(&status, row, "EXT_INST_MAG_SIG");
     1108
     1109        model->chisq = psMetadataLookupF32(&status, row, "EXT_CHISQ");
     1110        model->nDOF = psMetadataLookupF32(&status, row, "EXT_NDOF");
     1111
     1112        // EXT_MODEL_TYPE gives the model chosen by psphot as the best.
     1113        // Putting this into the XFIT table makes 3 copies of it (one for each model)
     1114        // but since we have many fewer XFIT rows than psf rows that is cheaper than putting it
     1115        // in the psf table.
     1116        psS32 extModelType = psMetadataLookupS32(&status, row, "EXT_MODEL_TYPE");
     1117        if (status) {
     1118            // translate between the type value in xfit and values used by this program
     1119            extModelType = pmModelClassGetLocalType(extModelType);
     1120        } else {
     1121            // older cmfs don't have this column
     1122            extModelType = -1;
     1123        }
    10591124
    10601125        psEllipseAxes axes;
     
    10721137        if (model->params->n > 7) {
    10731138            PAR[7] = psMetadataLookupF32(&status, row, "EXT_PAR_07");
    1074         }
    1075         // read the covariance matrix
    1076         int nparams = model->params->n;
    1077         psImage *covar = psImageAlloc(nparams, nparams, PS_TYPE_F32);
    1078         for (int y = 0; y < nparams; y++) {
    1079             for (int x = 0; x < nparams; x++) {
    1080                 char name[64];
    1081                 snprintf(name, 64, "EXT_COVAR_%02d_%02d", y, x);
    1082                 covar->data.F32[y][x] = psMetadataLookupF32(&status, row, name);
     1139            // XXX add an error:
     1140            // dPAR[7] = psMetadataLookupF32(&status, row, "EXT_PAR_07_");
     1141        }
     1142
     1143        // XXX : make this depend on what is in the cmf
     1144        if (0) {
     1145            // read the covariance matrix
     1146            int nparams = model->params->n;
     1147            psImage *covar = psImageAlloc(nparams, nparams, PS_TYPE_F32);
     1148            for (int y = 0; y < nparams; y++) {
     1149                for (int x = 0; x < nparams; x++) {
     1150                    char name[64];
     1151                    snprintf(name, 64, "EXT_COVAR_%02d_%02d", y, x);
     1152                    covar->data.F32[y][x] = psMetadataLookupF32(&status, row, name);
     1153                }
     1154            }
     1155            model->covar = covar;
     1156        }
     1157
     1158        if (modelType == extModelType) {
     1159            // The software that created this source picked this model as the best of the fits.
     1160            // Set the extModel to point to it.
     1161            // This is important for programs like psastro (skycal) so that its output cmfs
     1162            // will have valid EXT_MODEL_TYPE
     1163            psFree(source->modelEXT);
     1164            source->modelEXT = psMemIncrRefCounter(model);
     1165            source->type = PM_SOURCE_TYPE_EXTENDED;
     1166            if (0) {
     1167                // since FLAGS were read we don't need to do this
     1168                source->mode |= PM_SOURCE_MODE_EXTMODEL;
     1169                source->mode |= PM_SOURCE_MODE_NONLINEAR_FIT;
    10831170            }
    10841171        }
    1085         model->covar = covar;
    10861172
    10871173        psArrayAdd(source->modelFits, 1, model);
    10881174        psFree(model);
    1089 
    10901175        psFree(row);
    10911176    }
     
    12091294
    12101295        write_annuli:
    1211             psMetadataAdd (row, PS_LIST_TAIL, "APER_FLUX",       PS_DATA_VECTOR, "flux within annuli",       radFlux);
    1212             psMetadataAdd (row, PS_LIST_TAIL, "APER_FLUX_ERR",   PS_DATA_VECTOR, "flux error in annuli",     radFluxErr);
    1213             psMetadataAdd (row, PS_LIST_TAIL, "APER_FLUX_STDEV", PS_DATA_VECTOR, "flux standard deviation",  radFluxStdev);
    1214             psMetadataAdd (row, PS_LIST_TAIL, "APER_FILL",       PS_DATA_VECTOR, "fill factor of annuli",    radFill);
     1296            psMetadataAddVector (row, PS_LIST_TAIL, "APER_FLUX",       PS_META_REPLACE, "flux within annuli",       radFlux);
     1297            psMetadataAddVector (row, PS_LIST_TAIL, "APER_FLUX_ERR",   PS_META_REPLACE, "flux error in annuli",     radFluxErr);
     1298            psMetadataAddVector (row, PS_LIST_TAIL, "APER_FLUX_STDEV", PS_META_REPLACE, "flux standard deviation",  radFluxStdev);
     1299            psMetadataAddVector (row, PS_LIST_TAIL, "APER_FILL",       PS_META_REPLACE, "fill factor of annuli",    radFill);
    12151300            psFree (radFlux);
    12161301            psFree (radFluxErr);
     
    12771362            return false;
    12781363        }
    1279         // Find the source with this sequence number.
    1280         // XXX: I am assuming that sources is sorted in order of seq.
    12811364        long seq = psMetadataLookupU32 (&status, row, "IPP_IDET");
    1282         long j = seq < sources->n ? seq : sources->n - 1;
    1283         pmSource *source = NULL;
    1284         for (; j >= 0; j--) {
    1285             source = sources->data[j];
    1286             if (source->seq == seq) {
    1287                 break;
    1288             }
    1289         }
     1365        long j = sourceIndex[seq];
     1366        psAssert(j >= 0 && j < sources->n, "invalid sourceIndex");
     1367        pmSource *source = sources->data[j];
    12901368        if (!source) {
    12911369            psError(PS_ERR_UNKNOWN, false, "Failed to find source for row %ld sequence number %ld\n", i, seq);
     
    13431421    return true;
    13441422}
     1423
     1424// XXX where should I record the number of columns??
     1425bool pmSourcesWrite_CMF_@CMFMODE@_XGAL (psFits *fits, pmReadout *readout, psArray *sources, char *extname, psMetadata *recipe)
     1426{
     1427    bool status = false;
     1428
     1429    // perform full non-linear fits / extended source analysis?
     1430    if (!psMetadataLookupBool (&status, recipe, "GALAXY_SHAPES")) {
     1431        psLogMsg ("psphot", PS_LOG_INFO, "galaxy shapes were not measured, skipping\n");
     1432        return true;
     1433    }
     1434
     1435    // create a header to hold the output data
     1436    psMetadata *outhead = psMetadataAlloc ();
     1437
     1438    // write the links to the image header
     1439    psMetadataAddStr (outhead, PS_LIST_TAIL, "EXTNAME", PS_META_REPLACE, "galaxy table extension", extname);
     1440
     1441    psMetadataAddStr (outhead, PS_LIST_TAIL, "HI", PS_META_REPLACE, "does this get through?", "THERE");
     1442
     1443    // let's write these out in S/N order
     1444    sources = psArraySort (sources, pmSourceSortByFlux);
     1445
     1446    psArray *table = psArrayAllocEmpty (sources->n);
     1447
     1448    for (int i = 0; i < sources->n; i++) {
     1449
     1450        pmSource *thisSource = sources->data[i];
     1451
     1452        // this is the "real" version of this source
     1453        pmSource *source = thisSource->parent ? thisSource->parent : thisSource;
     1454
     1455        // if we did not fit the galaxy model, modelFits will be NULL
     1456        if (source->modelFits == NULL) continue;
     1457
     1458        // if we did not fit the galaxy model, galaxyFits will also be NULL
     1459        if (source->galaxyFits == NULL) continue;
     1460
     1461        pmModel *model = source->modelFits->data[0];
     1462        if (!model) return false;
     1463
     1464        // X,Y coordinates are stored with the model parameters
     1465        psF32 *PAR = model->params->data.F32;
     1466
     1467        psMetadata *row = psMetadataAlloc ();
     1468
     1469        // we write out the x,y positions so people can link to the psf either way (position or ID)
     1470        psMetadataAddU32 (row, PS_LIST_TAIL, "IPP_IDET",         0, "IPP detection identifier index", source->seq);
     1471        psMetadataAddF32 (row, PS_LIST_TAIL, "X_FIT",            0, "model x coordinate",             PAR[PM_PAR_XPOS]);
     1472        psMetadataAddF32 (row, PS_LIST_TAIL, "Y_FIT",            0, "model y coordinate",             PAR[PM_PAR_YPOS]);
     1473        psMetadataAddF32 (row, PS_LIST_TAIL, "NPIX",             0, "number of pixels for fits",      source->galaxyFits->nPix);
     1474
     1475        psVector *Flux = source->galaxyFits->Flux;
     1476        psVector *dFlux = source->galaxyFits->dFlux;
     1477        psVector *chisq = source->galaxyFits->chisq;
     1478
     1479        psMetadataAddVector (row, PS_LIST_TAIL, "GAL_FLUX",     PS_META_REPLACE, "normalization for galaxy flux", Flux);
     1480        psMetadataAddVector (row, PS_LIST_TAIL, "GAL_FLUX_ERR", PS_META_REPLACE, "error on normalization", dFlux);
     1481        psMetadataAddVector (row, PS_LIST_TAIL, "GAL_CHISQ",    PS_META_REPLACE, "galaxy fit chisq", chisq);
     1482
     1483        psArrayAdd (table, 100, row);
     1484        psFree (row);
     1485    }
     1486
     1487    if (table->n == 0) {
     1488        if (!psFitsWriteBlank (fits, outhead, extname)) {
     1489            psError(psErrorCodeLast(), false, "Unable to write empty sources file.");
     1490            psFree(outhead);
     1491            psFree(table);
     1492            return false;
     1493        }
     1494        psFree (outhead);
     1495        psFree (table);
     1496        return true;
     1497    }
     1498
     1499    psTrace ("pmFPAfile", 5, "writing galaxy data %s\n", extname);
     1500    if (!psFitsWriteTable (fits, outhead, table, extname)) {
     1501        psError(psErrorCodeLast(), false, "writing galaxy data %s\n", extname);
     1502        psFree (outhead);
     1503        psFree(table);
     1504        return false;
     1505    }
     1506    psFree (outhead);
     1507    psFree (table);
     1508    return true;
     1509}
     1510
     1511bool pmSourcesRead_CMF_@CMFMODE@_XGAL(psFits *fits, pmReadout *readout, psMetadata *hduHeader, psMetadata *tableHeader, psArray *sources, long *sourceIndex)
     1512{
     1513    PS_ASSERT_PTR_NON_NULL(fits, false);
     1514    PS_ASSERT_PTR_NON_NULL(sources, false);
     1515
     1516    bool status;
     1517    long numSources = psFitsTableSize(fits); // Number of sources in table
     1518    if (numSources == 0) {
     1519        psError(psErrorCodeLast(), false, "XGAL Table contains no entries\n");
     1520        return false;
     1521    }
     1522
     1523    for (long i = 0; i < numSources; i++) {
     1524        psMetadata *row = psFitsReadTableRow(fits, i); // Table row
     1525        if (!row) {
     1526            psError(psErrorCodeLast(), false, "Unable to read row %ld of sources", i);
     1527            psFree(row);
     1528            return false;
     1529        }
     1530        // Find the source with this sequence number.
     1531        // XXX: I am assuming that sources is sorted in order of seq
     1532        long seq = psMetadataLookupU32 (&status, row, "IPP_IDET");
     1533        long j = sourceIndex[seq];
     1534        psAssert(j >= 0 && j < sources->n, "invalid sourceIndex");
     1535
     1536        pmSource *source = sources->data[j];
     1537        if (!source) {
     1538            psError(PS_ERR_UNKNOWN, false, "Failed to find source for row %ld sequence number %ld\n", i, seq);
     1539            psFree(row);
     1540            return false;
     1541        }
     1542
     1543        psVector *Flux  = psMetadataLookupVector(&status, row, "GAL_FLUX");
     1544        psVector *dFlux = psMetadataLookupVector(&status, row, "GAL_FLUX_ERR");
     1545        psVector *chisq = psMetadataLookupVector(&status, row, "GAL_CHISQ");
     1546
     1547        if (Flux && Flux->n > 0) {
     1548            psFree(source->galaxyFits);
     1549            source->galaxyFits = pmSourceGalaxyFitsAlloc();
     1550            source->galaxyFits->nPix = psMetadataLookupF32(&status, row, "NPIX");
     1551
     1552            psFree(source->galaxyFits->Flux);
     1553            source->galaxyFits->Flux  = psMemIncrRefCounter(Flux);
     1554            psFree(source->galaxyFits->dFlux);
     1555            source->galaxyFits->dFlux = psMemIncrRefCounter(dFlux);
     1556            psFree(source->galaxyFits->chisq);
     1557            source->galaxyFits->chisq = psMemIncrRefCounter(chisq);
     1558        }
     1559
     1560        psFree(row);
     1561    }
     1562
     1563    return true;
     1564}
Note: See TracChangeset for help on using the changeset viewer.