IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 27177


Ignore:
Timestamp:
Mar 3, 2010, 5:11:47 PM (16 years ago)
Author:
Paul Price
Message:

Add error checking.

Location:
trunk/psModules/src/objects
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/psModules/src/objects/pmPSF_IO.c

    r25979 r27177  
    122122    if (view->chip == -1) {
    123123        if (!pmPSFmodelWriteFPA(fpa, view, file, config)) {
    124             psError(PS_ERR_IO, false, "Failed to write PSF for fpa");
     124            psError(psErrorCodeLast(), false, "Failed to write PSF for fpa");
    125125            return false;
    126126        }
     
    135135    if (view->cell == -1) {
    136136        if (!pmPSFmodelWriteChip (chip, view, file, config)) {
    137             psError(PS_ERR_IO, false, "Failed to write PSF for chip");
     137            psError(psErrorCodeLast(), false, "Failed to write PSF for chip");
    138138            return false;
    139139        }
     
    141141    }
    142142
    143     psError(PS_ERR_IO, false, "PSF must be written at the chip level");
     143    psError(PM_ERR_CONFIG, true, "PSF must be written at the chip level");
    144144    return false;
    145145}
     
    158158        thisView->chip = i;
    159159        if (!pmPSFmodelWriteChip (chip, thisView, file, config)) {
    160             psError(PS_ERR_IO, false, "Failed to write PSF for %dth chip", i);
     160            psError(psErrorCodeLast(), false, "Failed to write PSF for %dth chip", i);
    161161            psFree(thisView);
    162162            return false;
     
    173173    PS_ASSERT_PTR_NON_NULL(chip, false);
    174174
    175     if (!pmPSFmodelWrite (chip->analysis, view, file, config)) {
    176         psError(PS_ERR_IO, false, "Failed to write PSF for chip");
     175    if (!pmPSFmodelWrite(chip->analysis, view, file, config)) {
     176        psError(psErrorCodeLast(), false, "Failed to write PSF for chip");
    177177        return false;
    178178    }
     
    197197    char *headName, *tableName, *residName;
    198198
    199     if (!analysis) return false;
     199    if (!analysis) {
     200        psError(PM_ERR_PROG, true, "No analysis metadata for chip.");
     201        return false;
     202    }
    200203
    201204    // select the current recipe
    202205    psMetadata *recipe = psMetadataLookupPtr (NULL, config->recipes, "PSPHOT");
    203206    if (!recipe) {
    204         psError(PS_ERR_UNKNOWN, false, "missing recipe %s", "PSPHOT");
     207        psError(PM_ERR_CONFIG, false, "missing recipe %s", "PSPHOT");
    205208        return false;
    206209    }
     
    213216    // get the current header
    214217    pmFPA *fpa = pmFPAfileSuitableFPA(file, view, config, false); // Suitable FPA for writing
     218    if (!fpa) {
     219        psError(psErrorCodeLast(), false, "Unable to get FPA for writing.");
     220        return false;
     221    }
    215222    pmHDU *hdu = psMemIncrRefCounter(pmFPAviewThisHDU(view, fpa));
    216223    psFree(fpa);
    217224    if (!hdu) {
    218         psError(PS_ERR_UNKNOWN, false, "Unable to find HDU");
     225        psError(PM_ERR_CONFIG, false, "Unable to find HDU");
    219226        return false;
    220227    }
     
    234241        psMetadata *menu = psMetadataLookupMetadata(&status, file->camera, "EXTNAME.RULES");
    235242        if (!menu) {
    236             psError(PS_ERR_UNKNOWN, true, "missing EXTNAME.RULES in camera.config");
     243            psError(PM_ERR_CONFIG, true, "missing EXTNAME.RULES in camera.config");
    237244            psFree(hdu);
    238245            return false;
     
    242249        rule = psMetadataLookupStr(&status, menu, "PSF.HEAD");
    243250        if (!rule) {
    244             psError(PS_ERR_UNKNOWN, false, "missing entry for PSF.HEAD in EXTNAME.RULES in camera.config");
     251            psError(PM_ERR_CONFIG, false, "missing entry for PSF.HEAD in EXTNAME.RULES in camera.config");
    245252            psFree(hdu);
    246253            return false;
     
    251258        rule = psMetadataLookupStr(&status, menu, "PSF.TABLE");
    252259        if (!rule) {
    253             psError(PS_ERR_UNKNOWN, false, "missing entry for PSF.TABLE in EXTNAME.RULES in camera.config");
     260            psError(PM_ERR_CONFIG, false, "missing entry for PSF.TABLE in EXTNAME.RULES in camera.config");
    254261            psFree (headName);
    255262            psFree(hdu);
     
    261268        rule = psMetadataLookupStr(&status, menu, "PSF.RESID");
    262269        if (!rule) {
    263             psError(PS_ERR_UNKNOWN, false, "missing entry for PSF.RESID in EXTNAME.RULES in camera.config");
     270            psError(PM_ERR_CONFIG, false, "missing entry for PSF.RESID in EXTNAME.RULES in camera.config");
    264271            psFree (headName);
    265272            psFree (tableName);
     
    294301        }
    295302
    296         psFitsWriteBlank (file->fits, hdu->header, headName);
     303        if (!psFitsWriteBlank(file->fits, hdu->header, headName)) {
     304            psError(psErrorCodeLast(), false, "Unable to write PSF PHU.");
     305            psFree(hdu);
     306            return false;
     307        }
    297308        psTrace ("pmFPAfile", 5, "wrote ext head %s (type: %d)\n", file->filename, file->type);
    298309        file->header = hdu->header;
     
    304315    pmPSF *psf = psMetadataLookupPtr (&status, analysis, "PSPHOT.PSF");
    305316    if (!psf) {
    306         psError(PS_ERR_UNKNOWN, true, "missing PSF for this analysis metadata");
     317        psError(PM_ERR_PROG, true, "missing PSF for this analysis metadata");
    307318        psFree (tableName);
    308319        psFree (residName);
     
    322333        psMetadataAddBool (header, PS_LIST_TAIL, "ERR_PAR",  0, "Use Poisson errors in fits?", psf->poissonErrorsParams);
    323334
    324         int nPar = pmModelClassParameterCount (psf->type)    ;
     335        int nPar = pmModelClassParameterCount (psf->type);
    325336        psMetadataAdd (header, PS_LIST_TAIL, "PSF_NPAR", PS_DATA_S32, "PSF model parameter count", nPar);
    326337
     
    436447        // write an empty FITS segment if we have no PSF information
    437448        if (psfTable->n == 0) {
    438             // XXX this is probably an error (if we have a PSF, how do we have no data?)
    439             psFitsWriteBlank (file->fits, header, tableName);
     449            psError(PM_ERR_PROG, true, "No PSF data to write.");
     450            psFree(tableName);
     451            psFree(residName);
     452            psFree(psfTable);
     453            psFree(header);
     454            return false;
    440455        } else {
    441456            psTrace ("pmFPAfile", 5, "writing psf data %s\n", tableName);
    442             if (!psFitsWriteTable (file->fits, header, psfTable, tableName)) {
    443                 psError(PS_ERR_IO, false, "writing psf table data %s\n", tableName);
     457            if (!psFitsWriteTable(file->fits, header, psfTable, tableName)) {
     458                psError(psErrorCodeLast(), false, "Error writing psf table data %s\n", tableName);
    444459                psFree (tableName);
    445460                psFree (residName);
     
    459474        if (psf->residuals == NULL) {
    460475            // set some header keywords to make it clear there are no residuals?
    461             psFitsWriteBlank (file->fits, header, residName);
     476            if (!psFitsWriteBlank(file->fits, header, residName)) {
     477                psError(psErrorCodeLast(), false, "Unable to write blank PSF residual image.");
     478                psFree(residName);
     479                psFree(header);
     480                return false;
     481            }
    462482            psFree (residName);
    463483            psFree (header);
     
    479499            psArrayAdd (images, 1, psf->residuals->Ry);
    480500
    481             psFitsWriteImageCube (file->fits, header, images, residName);
     501            if (!psFitsWriteImageCube (file->fits, header, images, residName)) {
     502                psError(psErrorCodeLast(), false, "Unable to write PSF residuals.");
     503                psFree(images);
     504                psFree(residName);
     505                psFree(header);
     506                return false;
     507            }
    482508            psFree (images);
    483509        } else {
    484510            // this call creates an extension with NAXIS3 = 1
    485             psFitsWriteImage(file->fits, header, psf->residuals->Ro, 0, residName);
     511            if (!psFitsWriteImage(file->fits, header, psf->residuals->Ro, 0, residName)) {
     512                psError(psErrorCodeLast(), false, "Unable to write PSF residuals.");
     513                psFree(images);
     514                psFree(residName);
     515                psFree(header);
     516                return false;
     517            }
    486518        }
    487519        psFree (residName);
     
    495527        if (0) {
    496528            // set some header keywords to make it clear there are no residuals?
    497             psFitsWriteBlank (file->fits, header, residName);
     529            if (!psFitsWriteBlank (file->fits, header, residName)) {
     530                psError(psErrorCodeLast(), false, "Unable to write blank PSF residuals.");
     531                psFree(residName);
     532                psFree(header);
     533                return false;
     534            }
    498535            psFree (residName);
    499536            psFree (header);
     
    501538        }
    502539
    503         int DX = 65;
    504         int DY = 65;
    505 
    506         psImage *psfMosaic = psImageAlloc (DX, DY, PS_TYPE_F32);
    507         psImageInit (psfMosaic, 0.0);
    508 
    509         pmModel *modelRef = pmModelAlloc(psf->type);
    510 
    511         // use the center of the center pixel of the image
    512         float xc = 0.5*psf->fieldNx;
    513         float yc = 0.5*psf->fieldNy;
    514 
    515         // assign the x and y coords to the image center
    516         // create an object with center intensity of 1000
    517         modelRef->params->data.F32[PM_PAR_SKY] = 0;
    518         modelRef->params->data.F32[PM_PAR_I0] = 1.000;
    519         modelRef->params->data.F32[PM_PAR_XPOS] = xc;
    520         modelRef->params->data.F32[PM_PAR_YPOS] = yc;
    521 
    522         // create modelPSF from this model
    523         pmModel *model = pmModelFromPSF (modelRef, psf);
    524         if (model) {
    525             // place the reference object in the image center
    526             pmModelAddWithOffset (psfMosaic, NULL, model, PM_MODEL_OP_FULL | PM_MODEL_OP_CENTER, 0, 0.0, 0.0);
    527             psFree (model);
    528        
    529             if (false) {
    530                 // this call creates an extension with NAXIS3 = 3
    531                 psArray *images = psArrayAllocEmpty (3);
    532                 psArrayAdd (images, 1, psfMosaic);
    533                 // psArrayAdd (images, 1, psfModel);
    534                 // psArrayAdd (images, 1, psfModel);
    535 
    536                 psFitsWriteImageCube (file->fits, header, images, "PSF_MODEL");
    537                 psFree (images);
    538             } else {
    539                 // this call creates an extension with NAXIS3 = 1
    540                 // XXX need to replace PSF_MODEL with rule-based name like residName
    541                 psFitsWriteImage(file->fits, header, psfMosaic, 0, "PSF_MODEL");
    542             }
    543         }
     540        int DX = 65;
     541        int DY = 65;
     542
     543        psImage *psfMosaic = psImageAlloc (DX, DY, PS_TYPE_F32);
     544        psImageInit (psfMosaic, 0.0);
     545
     546        pmModel *modelRef = pmModelAlloc(psf->type);
     547
     548        // use the center of the center pixel of the image
     549        float xc = 0.5*psf->fieldNx;
     550        float yc = 0.5*psf->fieldNy;
     551
     552        // assign the x and y coords to the image center
     553        // create an object with center intensity of 1000
     554        modelRef->params->data.F32[PM_PAR_SKY] = 0;
     555        modelRef->params->data.F32[PM_PAR_I0] = 1.000;
     556        modelRef->params->data.F32[PM_PAR_XPOS] = xc;
     557        modelRef->params->data.F32[PM_PAR_YPOS] = yc;
     558
     559        // create modelPSF from this model
     560        pmModel *model = pmModelFromPSF (modelRef, psf);
     561        if (model) {
     562            // place the reference object in the image center
     563            pmModelAddWithOffset (psfMosaic, NULL, model, PM_MODEL_OP_FULL | PM_MODEL_OP_CENTER, 0, 0.0, 0.0);
     564            psFree (model);
     565
     566            if (false) {
     567                // this call creates an extension with NAXIS3 = 3
     568                psArray *images = psArrayAllocEmpty (3);
     569                psArrayAdd (images, 1, psfMosaic);
     570                // psArrayAdd (images, 1, psfModel);
     571                // psArrayAdd (images, 1, psfModel);
     572
     573                if (!psFitsWriteImageCube (file->fits, header, images, "PSF_MODEL")) {
     574                    psError(psErrorCodeLast(), false, "Unable to write PSF representation.");
     575                    psFree(images);
     576                    psFree(psfMosaic);
     577                    psFree(modelRef);
     578                    psFree(header);
     579                    return false;
     580                }
     581                psFree (images);
     582            } else {
     583                // this call creates an extension with NAXIS3 = 1
     584                // XXX need to replace PSF_MODEL with rule-based name like residName
     585                if (!psFitsWriteImage(file->fits, header, psfMosaic, 0, "PSF_MODEL")) {
     586                    psError(psErrorCodeLast(), false, "Unable to write PSF representation.");
     587                    psFree(psfMosaic);
     588                    psFree(modelRef);
     589                    psFree(header);
     590                    return false;
     591                }
     592            }
     593        }
    544594
    545595        psFree (psfMosaic);
     
    594644    // find the FPA phu
    595645    pmFPA *fpa = pmFPAfileSuitableFPA(file, view, config, false); // Suitable FPA for writing
     646    if (!fpa) {
     647        psError(psErrorCodeLast(), false, "Unable to build FPA to write.");
     648        return false;
     649    }
    596650    pmHDU *phu = psMemIncrRefCounter(pmFPAviewThisPHU(view, fpa));
    597651    psFree(fpa);
     
    604658        psMetadataCopy (outhead, phu->header);
    605659    } else {
    606         pmConfigConformHeader (outhead, file->format);
     660        if (!pmConfigConformHeader (outhead, file->format)) {
     661            psError(psErrorCodeLast(), false, "Unable to conform header of PSF PHU.");
     662            psFree(phu);
     663            return false;
     664        }
    607665    }
    608666    psFree(phu);
    609667
    610668    psMetadataAddBool (outhead, PS_LIST_TAIL, "EXTEND", PS_META_REPLACE, "this file has extensions", true);
    611     psFitsWriteBlank (file->fits, outhead, "");
     669    if (!psFitsWriteBlank (file->fits, outhead, "")) {
     670        psError(psErrorCodeLast(), false, "Unable to write PHU for PSF.");
     671        psFree(outhead);
     672        return false;
     673    }
    612674    file->wrote_phu = true;
    613675
     
    639701    }
    640702
    641     psError(PS_ERR_IO, false, "PSF must be read at the chip level");
     703    psError(PM_ERR_CONFIG, true, "PSF must be read at the chip level");
    642704    return false;
    643705}
     
    666728
    667729    if (!pmPSFmodelRead (chip->analysis, view, file, config)) {
    668         psError(PS_ERR_IO, false, "Failed to write PSF for chip");
     730        psError(psErrorCodeLast(), false, "Failed to write PSF for chip");
    669731        return false;
    670732    }
     
    689751    psMetadata *recipe = psMetadataLookupPtr (NULL, config->recipes, "PSPHOT");
    690752    if (!recipe) {
    691         psError(PS_ERR_UNKNOWN, false, "missing recipe %s", "PSPHOT");
     753        psError(PM_ERR_CONFIG, false, "missing recipe %s", "PSPHOT");
    692754        return false;
    693755    }
     
    696758    psMetadata *menu = psMetadataLookupMetadata(&status, file->camera, "EXTNAME.RULES");
    697759    if (!menu) {
    698         psError(PS_ERR_UNKNOWN, true, "missing EXTNAME.RULES in camera.config");
     760        psError(PM_ERR_CONFIG, true, "missing EXTNAME.RULES in camera.config");
    699761        return false;
    700762    }
     
    702764    rule = psMetadataLookupStr(&status, menu, "PSF.TABLE");
    703765    if (!rule) {
    704         psError(PS_ERR_UNKNOWN, true, "missing entry for PSF.TABLE in EXTNAME.RULES in camera.config");
     766        psError(PM_ERR_CONFIG, true, "missing entry for PSF.TABLE in EXTNAME.RULES in camera.config");
    705767        return false;
    706768    }
     
    709771    rule = psMetadataLookupStr(&status, menu, "PSF.RESID");
    710772    if (!rule) {
    711         psError(PS_ERR_UNKNOWN, true, "missing entry for PSF.RESID in EXTNAME.RULES in camera.config");
     773        psError(PM_ERR_CONFIG, true, "missing entry for PSF.RESID in EXTNAME.RULES in camera.config");
    712774        return false;
    713775    }
     
    717779    // advance to the table data extension
    718780    // since we have read the IMAGE header, the TABLE header should exist
    719     if (!psFitsMoveExtName (file->fits, tableName)) {
    720         psAbort("cannot find data extension %s in %s", tableName, file->filename);
     781    if (!psFitsMoveExtName(file->fits, tableName)) {
     782        psError(psErrorCodeLast(), false, "cannot find data extension %s in %s", tableName, file->filename);
     783        return false;
    721784    }
    722785
    723786    // load the PSF model table header
    724787    header = psFitsReadHeader (NULL, file->fits);
    725     if (!header) psAbort("cannot read table header");
     788    if (!header) {
     789        psError(psErrorCodeLast(), false, "Cannot read PSF table header.");
     790        return false;
     791    }
    726792
    727793    pmPSFOptions *options = pmPSFOptionsAlloc();
     
    835901        char *modeName = psMetadataLookupStr (&status, header, name);
    836902        if (!status) {
    837             psError(PS_ERR_UNKNOWN, true, "inconsistent PSF header: NX & NY defined for PAR %d, but not MD", i);
     903            psError(PM_ERR_IO, true, "inconsistent PSF header: NX & NY defined for PAR %d, but not MD", i);
    838904            return false;
    839905        }
     
    869935    // read the raw table data
    870936    psArray *table = psFitsReadTable (file->fits);
     937    if (!table) {
     938        psError(psErrorCodeLast(), false, "Unable to read PSF table.");
     939        psFree(header);
     940        return false;
     941    }
    871942
    872943    // fill in the matching psf->params entries
     
    913984    // since we have read the IMAGE header, the TABLE header should exist
    914985    if (!psFitsMoveExtName (file->fits, imageName)) {
    915         psAbort("cannot find data extension %s in %s", imageName, file->filename);
     986        psError(psErrorCodeLast(), false, "Cannot find PSF data extension %s in %s",
     987                imageName, file->filename);
     988        return false;
    916989    }
    917990
    918991    header = psFitsReadHeader (NULL, file->fits);
     992    if (!header) {
     993        psError(psErrorCodeLast(), false, "Unable to read PSF header.");
     994        return false;
     995    }
    919996    int Naxis = psMetadataLookupS32 (&status, header, "NAXIS");
    920997    if (Naxis != 0) {
     
    9361013
    9371014        psRegion fullImage = {0, 0, 0, 0};
    938         psFitsReadImageBuffer(psf->residuals->Ro, file->fits, fullImage, 0); // Desired pixels
     1015        if (!psFitsReadImageBuffer(psf->residuals->Ro, file->fits, fullImage, 0)) {
     1016            psError(psErrorCodeLast(), false, "Unable to read PSF residual image.");
     1017            return false;
     1018        }
    9391019        if (Nz > 1) {
    9401020            assert (Nz == 3);
    941             psFitsReadImageBuffer(psf->residuals->Rx, file->fits, fullImage, 1); // Desired pixels
    942             psFitsReadImageBuffer(psf->residuals->Ry, file->fits, fullImage, 2); // Desired pixels
     1021            if (!psFitsReadImageBuffer(psf->residuals->Rx, file->fits, fullImage, 1)) {
     1022                psError(psErrorCodeLast(), false, "Unable to read PSF residual image.");
     1023                return false;
     1024            }
     1025            if (!psFitsReadImageBuffer(psf->residuals->Ry, file->fits, fullImage, 2)) {
     1026                psError(psErrorCodeLast(), false, "Unable to read PSF residual image.");
     1027                return false;
     1028            }
    9431029        }
    9441030        // XXX notice that we are not saving the resid->mask
  • trunk/psModules/src/objects/pmSourceIO_CMF_PS1_V1.c

    r26964 r27177  
    216216
    217217    if (table->n == 0) {
    218         psFitsWriteBlank(fits, header, extname);
     218        if (!psFitsWriteBlank(fits, header, extname)) {
     219            psError(psErrorCodeLast(), false, "Unable to write empty sources file.");
     220            psFree(table);
     221            psFree(header);
     222            return false;
     223        }
    219224        psFree(table);
    220225        psFree(header);
     
    224229    psTrace ("pmFPAfile", 5, "writing ext data %s\n", extname);
    225230    if (!psFitsWriteTable(fits, header, table, extname)) {
    226         psError(PS_ERR_IO, false, "writing ext data %s\n", extname);
     231        psError(psErrorCodeLast(), false, "writing ext data %s\n", extname);
    227232        psFree(table);
    228233        psFree(header);
     
    486491
    487492    if (table->n == 0) {
    488         psFitsWriteBlank (fits, outhead, extname);
     493        if (!psFitsWriteBlank (fits, outhead, extname)) {
     494            psError(psErrorCodeLast(), false, "Unable to write empty sources.");
     495            psFree(outhead);
     496            psFree(table);
     497            return false;
     498        }
    489499        psFree (outhead);
    490500        psFree (table);
     
    494504    psTrace ("pmFPAfile", 5, "writing ext data %s\n", extname);
    495505    if (!psFitsWriteTable (fits, outhead, table, extname)) {
    496         psError(PS_ERR_IO, false, "writing ext data %s\n", extname);
     506        psError(psErrorCodeLast(), false, "writing ext data %s\n", extname);
    497507        psFree (outhead);
    498508        psFree(table);
     
    612622
    613623    if (table->n == 0) {
    614         psFitsWriteBlank (fits, outhead, extname);
     624        if (!psFitsWriteBlank (fits, outhead, extname)) {
     625            psError(psErrorCodeLast(), false, "Unable to write empty sources.");
     626            psFree(outhead);
     627            psFree(table);
     628            return false;
     629        }
    615630        psFree (outhead);
    616631        psFree (table);
     
    620635    psTrace ("pmFPAfile", 5, "writing ext data %s\n", extname);
    621636    if (!psFitsWriteTable (fits, outhead, table, extname)) {
    622         psError(PS_ERR_IO, false, "writing ext data %s\n", extname);
     637        psError(psErrorCodeLast(), false, "writing ext data %s\n", extname);
    623638        psFree (outhead);
    624639        psFree(table);
  • trunk/psModules/src/objects/pmSourceIO_CMF_PS1_V2.c

    r26964 r27177  
    216216
    217217    if (table->n == 0) {
    218         psFitsWriteBlank(fits, header, extname);
     218        if (!psFitsWriteBlank(fits, header, extname)) {
     219            psError(psErrorCodeLast(), false, "Unable to write blank sources file.");
     220            psFree(table);
     221            psFree(header);
     222            return false;
     223        }
    219224        psFree(table);
    220225        psFree(header);
     
    224229    psTrace ("pmFPAfile", 5, "writing ext data %s\n", extname);
    225230    if (!psFitsWriteTable(fits, header, table, extname)) {
    226         psError(PS_ERR_IO, false, "writing ext data %s\n", extname);
     231        psError(psErrorCodeLast(), false, "writing ext data %s\n", extname);
    227232        psFree(table);
    228233        psFree(header);
     
    264269    for (int i = 0; i < numSources; i++) {
    265270        psMetadata *row = psFitsReadTableRow(fits, i); // Table row
     271        if (!row) {
     272            psError(psErrorCodeLast(), false, "Unable to read row %d of sources", i);
     273            psFree(sources);
     274            return NULL;
     275        }
    266276
    267277        pmSource *source = pmSourceAlloc ();
     
    487497
    488498    if (table->n == 0) {
    489         psFitsWriteBlank (fits, outhead, extname);
     499        if (!psFitsWriteBlank (fits, outhead, extname)) {
     500            psError(psErrorCodeLast(), false, "Unable to write empty sources file.");
     501            psFree(outhead);
     502            psFree(table);
     503            return false;
     504        }
    490505        psFree (outhead);
    491506        psFree (table);
     
    495510    psTrace ("pmFPAfile", 5, "writing ext data %s\n", extname);
    496511    if (!psFitsWriteTable (fits, outhead, table, extname)) {
    497         psError(PS_ERR_IO, false, "writing ext data %s\n", extname);
     512        psError(psErrorCodeLast(), false, "writing ext data %s\n", extname);
    498513        psFree (outhead);
    499514        psFree(table);
     
    613628
    614629    if (table->n == 0) {
    615         psFitsWriteBlank (fits, outhead, extname);
     630        if (!psFitsWriteBlank (fits, outhead, extname)) {
     631            psError(psErrorCodeLast(), false, "Unable to write empty sources file.");
     632            psFree(outhead);
     633            psFree(table);
     634            return false;
     635        }
    616636        psFree (outhead);
    617637        psFree (table);
     
    621641    psTrace ("pmFPAfile", 5, "writing ext data %s\n", extname);
    622642    if (!psFitsWriteTable (fits, outhead, table, extname)) {
    623         psError(PS_ERR_IO, false, "writing ext data %s\n", extname);
     643        psError(psErrorCodeLast(), false, "writing ext data %s\n", extname);
    624644        psFree (outhead);
    625645        psFree(table);
  • trunk/psModules/src/objects/pmSourceIO_MatchedRefs.c

    r26893 r27177  
    6767
    6868    if (!table) {
    69         table = psArrayAllocEmpty (0x1000);
    70         pmFPAview *view = pmFPAviewAlloc (0);
     69        table = psArrayAllocEmpty (0x1000);
     70        pmFPAview *view = pmFPAviewAlloc (0);
    7171
    72         // this loop selects the matched stars for all chips
    73         while ((chip = pmFPAviewNextChip (view, fpa, 1)) != NULL) {
    74             psTrace ("psastro", 4, "Chip %d: %x %x\n", view->chip, chip->file_exists, chip->process);
    75             if (!chip->process || !chip->file_exists) continue;
     72        // this loop selects the matched stars for all chips
     73        while ((chip = pmFPAviewNextChip (view, fpa, 1)) != NULL) {
     74            psTrace ("psastro", 4, "Chip %d: %x %x\n", view->chip, chip->file_exists, chip->process);
     75            if (!chip->process || !chip->file_exists) continue;
    7676
    77             char *chipName = psMetadataLookupStr(NULL, chip->concepts, "CHIP.NAME");
     77            char *chipName = psMetadataLookupStr(NULL, chip->concepts, "CHIP.NAME");
    7878
    79             while ((cell = pmFPAviewNextCell (view, fpa, 1)) != NULL) {
    80                 psTrace ("psastro", 4, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process);
    81                 if (!cell->process || !cell->file_exists) continue;
     79            while ((cell = pmFPAviewNextCell (view, fpa, 1)) != NULL) {
     80                psTrace ("psastro", 4, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process);
     81                if (!cell->process || !cell->file_exists) continue;
    8282
    83                 // process each of the readouts
    84                 // XXX there can only be one readout per chip, right?
    85                 while ((readout = pmFPAviewNextReadout (view, fpa, 1)) != NULL) {
    86                     if (! readout->data_exists) continue;
     83                // process each of the readouts
     84                // XXX there can only be one readout per chip, right?
     85                while ((readout = pmFPAviewNextReadout (view, fpa, 1)) != NULL) {
     86                    if (! readout->data_exists) continue;
    8787
    88                     // select the raw objects for this readout
    89                     psArray *rawstars = psMetadataLookupPtr (NULL, readout->analysis, "PSASTRO.RAWSTARS");
    90                     if (rawstars == NULL) continue;
     88                    // select the raw objects for this readout
     89                    psArray *rawstars = psMetadataLookupPtr (NULL, readout->analysis, "PSASTRO.RAWSTARS");
     90                    if (rawstars == NULL) continue;
    9191
    92                     // select the raw objects for this readout
    93                     psArray *refstars = psMetadataLookupPtr (NULL, readout->analysis, "PSASTRO.REFSTARS");
    94                     if (refstars == NULL) continue;
    95                     psTrace ("psastro", 4, "Trying %ld refstars\n", refstars->n);
     92                    // select the raw objects for this readout
     93                    psArray *refstars = psMetadataLookupPtr (NULL, readout->analysis, "PSASTRO.REFSTARS");
     94                    if (refstars == NULL) continue;
     95                    psTrace ("psastro", 4, "Trying %ld refstars\n", refstars->n);
    9696
    9797# if (0)
    98                     // XXX test
    99                     FILE *outfile = fopen ("refstars.dat", "w");
    100                     assert (outfile);
    101                     for (int nn = 0; nn < refstars->n; nn++) {
    102                         pmAstromObj *ref = refstars->data[nn];
    103                         fprintf (outfile, "%lf %lf\n", ref->sky->r*PS_DEG_RAD, ref->sky->d*PS_DEG_RAD);
    104                     }
    105                     fclose (outfile);
     98                    // XXX test
     99                    FILE *outfile = fopen ("refstars.dat", "w");
     100                    assert (outfile);
     101                    for (int nn = 0; nn < refstars->n; nn++) {
     102                        pmAstromObj *ref = refstars->data[nn];
     103                        fprintf (outfile, "%lf %lf\n", ref->sky->r*PS_DEG_RAD, ref->sky->d*PS_DEG_RAD);
     104                    }
     105                    fclose (outfile);
    106106# endif
    107107
    108                     psArray *matches = psMetadataLookupPtr (NULL, readout->analysis, "PSASTRO.MATCH");
    109                     if (matches == NULL) continue;
     108                    psArray *matches = psMetadataLookupPtr (NULL, readout->analysis, "PSASTRO.MATCH");
     109                    if (matches == NULL) continue;
    110110
    111                     for (int i = 0; i < matches->n; i++) {
    112                         pmAstromMatch *match = matches->data[i];
     111                    for (int i = 0; i < matches->n; i++) {
     112                        pmAstromMatch *match = matches->data[i];
    113113
    114                         pmAstromObj *raw = rawstars->data[match->raw];
    115                         pmAstromObj *ref = refstars->data[match->ref];
     114                        pmAstromObj *raw = rawstars->data[match->raw];
     115                        pmAstromObj *ref = refstars->data[match->ref];
    116116
    117                         psMetadata *row = psMetadataAlloc ();
    118                         psMetadataAdd (row, PS_LIST_TAIL, "RA",       PS_DATA_F64, "right ascension (deg, J2000)", PM_DEG_RAD*ref->sky->r);
    119                         psMetadataAdd (row, PS_LIST_TAIL, "DEC",      PS_DATA_F64, "declination (deg, J2000)",     PM_DEG_RAD*ref->sky->d);
    120                         psMetadataAdd (row, PS_LIST_TAIL, "X_CHIP",   PS_DATA_F32, "x coord on chip",              raw->chip->x);
    121                         psMetadataAdd (row, PS_LIST_TAIL, "Y_CHIP",   PS_DATA_F32, "y coord on chip",              raw->chip->y);
    122                         psMetadataAdd (row, PS_LIST_TAIL, "X_CHIP_FIT",PS_DATA_F32, "x fitted coord on chip",      ref->chip->x);
    123                         psMetadataAdd (row, PS_LIST_TAIL, "Y_CHIP_FIT",PS_DATA_F32, "y fitted coord on chip",      ref->chip->y);
    124                         psMetadataAdd (row, PS_LIST_TAIL, "X_FPA",    PS_DATA_F32, "x coord on focal plane",       raw->FP->x);
    125                         psMetadataAdd (row, PS_LIST_TAIL, "Y_FPA",    PS_DATA_F32, "y coord on focal plane",       raw->FP->y);
    126                         psMetadataAdd (row, PS_LIST_TAIL, "MAG_INST", PS_DATA_F32, "instrumental magnitude",       raw->Mag);
    127                         psMetadataAdd (row, PS_LIST_TAIL, "MAG_REF",  PS_DATA_F32, "reference star magnitude",     ref->Mag);
    128                         psMetadataAdd (row, PS_LIST_TAIL, "COLOR_REF",PS_DATA_F32, "reference star color",         ref->Color);
    129                         psMetadataAdd (row, PS_LIST_TAIL, "CHIP_ID",  PS_DATA_STRING, "chip identifier",           chipName);
    130                         // XXX need to add the reference color, but this needs getstar / dvo.photcodes for the reference to be refined.
     117                        psMetadata *row = psMetadataAlloc ();
     118                        psMetadataAdd (row, PS_LIST_TAIL, "RA",       PS_DATA_F64, "right ascension (deg, J2000)", PM_DEG_RAD*ref->sky->r);
     119                        psMetadataAdd (row, PS_LIST_TAIL, "DEC",      PS_DATA_F64, "declination (deg, J2000)",     PM_DEG_RAD*ref->sky->d);
     120                        psMetadataAdd (row, PS_LIST_TAIL, "X_CHIP",   PS_DATA_F32, "x coord on chip",              raw->chip->x);
     121                        psMetadataAdd (row, PS_LIST_TAIL, "Y_CHIP",   PS_DATA_F32, "y coord on chip",              raw->chip->y);
     122                        psMetadataAdd (row, PS_LIST_TAIL, "X_CHIP_FIT",PS_DATA_F32, "x fitted coord on chip",      ref->chip->x);
     123                        psMetadataAdd (row, PS_LIST_TAIL, "Y_CHIP_FIT",PS_DATA_F32, "y fitted coord on chip",      ref->chip->y);
     124                        psMetadataAdd (row, PS_LIST_TAIL, "X_FPA",    PS_DATA_F32, "x coord on focal plane",       raw->FP->x);
     125                        psMetadataAdd (row, PS_LIST_TAIL, "Y_FPA",    PS_DATA_F32, "y coord on focal plane",       raw->FP->y);
     126                        psMetadataAdd (row, PS_LIST_TAIL, "MAG_INST", PS_DATA_F32, "instrumental magnitude",       raw->Mag);
     127                        psMetadataAdd (row, PS_LIST_TAIL, "MAG_REF",  PS_DATA_F32, "reference star magnitude",     ref->Mag);
     128                        psMetadataAdd (row, PS_LIST_TAIL, "COLOR_REF",PS_DATA_F32, "reference star color",         ref->Color);
     129                        psMetadataAdd (row, PS_LIST_TAIL, "CHIP_ID",  PS_DATA_STRING, "chip identifier",           chipName);
     130                        // XXX need to add the reference color, but this needs getstar / dvo.photcodes for the reference to be refined.
    131131
    132                         psArrayAdd (table, 100, row);
    133                         psFree (row);
    134                     }
    135                 }
    136             }
    137         }
    138         psFree (view);
     132                        psArrayAdd (table, 100, row);
     133                        psFree (row);
     134                    }
     135                }
     136            }
     137        }
     138        psFree (view);
    139139
    140         if (table->n == 0) {
    141             psFree(table);
    142             return true;
    143         }
     140        if (table->n == 0) {
     141            psFree(table);
     142            return true;
     143        }
    144144    }
    145145
    146146    if (!psFitsWriteTable(fits, NULL, table, "MATCHED_REFS")) {
    147         psError(PS_ERR_IO, false, "writing MATCHED_REFS\n");
     147        psError(psErrorCodeLast(), false, "writing MATCHED_REFS\n");
    148148        psFree(table);
    149149        return false;
     
    165165    // It is not an error to lack this entry -- psFitsMoveExtNameClean does not raise an error
    166166    if (!psFitsMoveExtNameClean (fits, "MATCHED_REFS")) {
    167         psMetadataAddBool (fpa->analysis, PS_LIST_TAIL, "READ.REFMATCH", PS_META_REPLACE, "attempted to read MATCHED_REFS", true);
    168         return true;
     167        psMetadataAddBool (fpa->analysis, PS_LIST_TAIL, "READ.REFMATCH", PS_META_REPLACE, "attempted to read MATCHED_REFS", true);
     168        return true;
    169169    }
    170    
     170
    171171    // We get the size of the table, and allocate the array of sources first because the table
    172172    // is large and ephemeral --- when the table gets blown away, whatever is allocated after
     
    178178    for (int i = 0; i < numRows; i++) {
    179179        psMetadata *row = psFitsReadTableRow(fits, i); // Table row
    180         rows->data[i] = row;
     180        if (!row) {
     181            psError(psErrorCodeLast(), false, "Unable to read row %d of matched references.", i);
     182            psFree(rows);
     183            return false;
     184        }
     185        rows->data[i] = row;
    181186    }
    182187
Note: See TracChangeset for help on using the changeset viewer.