IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

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

Add error checking.

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.