IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 3, 2010, 8:50:52 AM (16 years ago)
Author:
eugene
Message:

updates from trunk

Location:
branches/simtest_nebulous_branches
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/simtest_nebulous_branches

  • branches/simtest_nebulous_branches/psModules

  • branches/simtest_nebulous_branches/psModules/src/objects/pmPSF_IO.c

    r21183 r27840  
    2828#include "pmConfig.h"
    2929#include "pmDetrendDB.h"
     30#include "pmErrorCodes.h"
    3031
    3132#include "pmHDU.h"
     
    4950#include "pmSource.h"
    5051#include "pmModelClass.h"
     52#include "pmModelUtils.h"
    5153#include "pmSourceIO.h"
    5254
     
    121123    if (view->chip == -1) {
    122124        if (!pmPSFmodelWriteFPA(fpa, view, file, config)) {
    123             psError(PS_ERR_IO, false, "Failed to write PSF for fpa");
     125            psError(psErrorCodeLast(), false, "Failed to write PSF for fpa");
    124126            return false;
    125127        }
     
    134136    if (view->cell == -1) {
    135137        if (!pmPSFmodelWriteChip (chip, view, file, config)) {
    136             psError(PS_ERR_IO, false, "Failed to write PSF for chip");
     138            psError(psErrorCodeLast(), false, "Failed to write PSF for chip");
    137139            return false;
    138140        }
     
    140142    }
    141143
    142     psError(PS_ERR_IO, false, "PSF must be written at the chip level");
     144    psError(PM_ERR_CONFIG, true, "PSF must be written at the chip level");
    143145    return false;
    144146}
     
    157159        thisView->chip = i;
    158160        if (!pmPSFmodelWriteChip (chip, thisView, file, config)) {
    159             psError(PS_ERR_IO, false, "Failed to write PSF for %dth chip", i);
     161            psError(psErrorCodeLast(), false, "Failed to write PSF for %dth chip", i);
    160162            psFree(thisView);
    161163            return false;
     
    172174    PS_ASSERT_PTR_NON_NULL(chip, false);
    173175
    174     if (!pmPSFmodelWrite (chip->analysis, view, file, config)) {
    175         psError(PS_ERR_IO, false, "Failed to write PSF for chip");
     176    if (!pmPSFmodelWrite(chip->analysis, view, file, config)) {
     177        psError(psErrorCodeLast(), false, "Failed to write PSF for chip");
    176178        return false;
    177179    }
     
    196198    char *headName, *tableName, *residName;
    197199
    198     if (!analysis) return false;
     200    if (!analysis) {
     201        psError(PM_ERR_PROG, true, "No analysis metadata for chip.");
     202        return false;
     203    }
    199204
    200205    // select the current recipe
    201206    psMetadata *recipe = psMetadataLookupPtr (NULL, config->recipes, "PSPHOT");
    202207    if (!recipe) {
    203         psError(PS_ERR_UNKNOWN, false, "missing recipe %s", "PSPHOT");
     208        psError(PM_ERR_CONFIG, false, "missing recipe %s", "PSPHOT");
    204209        return false;
    205210    }
     
    212217    // get the current header
    213218    pmFPA *fpa = pmFPAfileSuitableFPA(file, view, config, false); // Suitable FPA for writing
     219    if (!fpa) {
     220        psError(psErrorCodeLast(), false, "Unable to get FPA for writing.");
     221        return false;
     222    }
    214223    pmHDU *hdu = psMemIncrRefCounter(pmFPAviewThisHDU(view, fpa));
    215224    psFree(fpa);
    216225    if (!hdu) {
    217         psError(PS_ERR_UNKNOWN, false, "Unable to find HDU");
     226        psError(PM_ERR_CONFIG, false, "Unable to find HDU");
    218227        return false;
    219228    }
     
    233242        psMetadata *menu = psMetadataLookupMetadata(&status, file->camera, "EXTNAME.RULES");
    234243        if (!menu) {
    235             psError(PS_ERR_UNKNOWN, true, "missing EXTNAME.RULES in camera.config");
     244            psError(PM_ERR_CONFIG, true, "missing EXTNAME.RULES in camera.config");
    236245            psFree(hdu);
    237246            return false;
     
    241250        rule = psMetadataLookupStr(&status, menu, "PSF.HEAD");
    242251        if (!rule) {
    243             psError(PS_ERR_UNKNOWN, false, "missing entry for PSF.HEAD in EXTNAME.RULES in camera.config");
     252            psError(PM_ERR_CONFIG, false, "missing entry for PSF.HEAD in EXTNAME.RULES in camera.config");
    244253            psFree(hdu);
    245254            return false;
     
    250259        rule = psMetadataLookupStr(&status, menu, "PSF.TABLE");
    251260        if (!rule) {
    252             psError(PS_ERR_UNKNOWN, false, "missing entry for PSF.TABLE in EXTNAME.RULES in camera.config");
     261            psError(PM_ERR_CONFIG, false, "missing entry for PSF.TABLE in EXTNAME.RULES in camera.config");
    253262            psFree (headName);
    254263            psFree(hdu);
     
    260269        rule = psMetadataLookupStr(&status, menu, "PSF.RESID");
    261270        if (!rule) {
    262             psError(PS_ERR_UNKNOWN, false, "missing entry for PSF.RESID in EXTNAME.RULES in camera.config");
     271            psError(PM_ERR_CONFIG, false, "missing entry for PSF.RESID in EXTNAME.RULES in camera.config");
    263272            psFree (headName);
    264273            psFree (tableName);
     
    267276        }
    268277        residName = pmFPAfileNameFromRule (rule, file, view);
     278
     279        // EXTNAME for psf image
     280        // rule = psMetadataLookupStr(&status, menu, "PSF.RESID");
     281        // if (!rule) {
     282        //     psError(PS_ERR_UNKNOWN, false, "missing entry for PSF.RESID in EXTNAME.RULES in camera.config");
     283        //     psFree (headName);
     284        //     psFree (tableName);
     285        //     psFree(hdu);
     286        //     return false;
     287        // }
     288        // residName = pmFPAfileNameFromRule (rule, file, view);
    269289    }
    270290
     
    282302        }
    283303
    284         psFitsWriteBlank (file->fits, hdu->header, headName);
     304        if (!psFitsWriteBlank(file->fits, hdu->header, headName)) {
     305            psError(psErrorCodeLast(), false, "Unable to write PSF PHU.");
     306            psFree(hdu);
     307            return false;
     308        }
    285309        psTrace ("pmFPAfile", 5, "wrote ext head %s (type: %d)\n", file->filename, file->type);
    286310        file->header = hdu->header;
     
    292316    pmPSF *psf = psMetadataLookupPtr (&status, analysis, "PSPHOT.PSF");
    293317    if (!psf) {
    294         psError(PS_ERR_UNKNOWN, true, "missing PSF for this analysis metadata");
     318        psError(PM_ERR_PROG, true, "missing PSF for this analysis metadata");
    295319        psFree (tableName);
    296320        psFree (residName);
     
    310334        psMetadataAddBool (header, PS_LIST_TAIL, "ERR_PAR",  0, "Use Poisson errors in fits?", psf->poissonErrorsParams);
    311335
    312         int nPar = pmModelClassParameterCount (psf->type)    ;
     336        int nPar = pmModelClassParameterCount (psf->type);
    313337        psMetadataAdd (header, PS_LIST_TAIL, "PSF_NPAR", PS_DATA_S32, "PSF model parameter count", nPar);
    314338
     
    321345        pmPSFClump psfClump;
    322346
    323         // we now save clump parameters for each region : need to save all of those
    324         int nRegions = psMetadataLookupS32 (&status, recipe, "PSF.CLUMP.NREGIONS");
    325         psMetadataAddS32 (header, PS_LIST_TAIL, "PSF_CLN", PS_META_REPLACE, "number of psf clump regions", nRegions);
    326         for (int i = 0; i < nRegions; i++) {
    327             char regionName[64];
    328             snprintf (regionName, 64, "PSF.CLUMP.REGION.%03d", i);
    329             psMetadata *regionMD = psMetadataLookupPtr (&status, recipe, regionName);
    330 
    331             psfClump.X  = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.X");   assert (status);
    332             psfClump.Y  = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.Y");   assert (status);
    333             psfClump.dX = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.DX");  assert (status);
    334             psfClump.dY = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.DY");  assert (status);
    335 
    336             char key[16];
    337             snprintf (key, 9, "CLX_%03d", i);
    338             psMetadataAddF32 (header, PS_LIST_TAIL, key, PS_META_REPLACE, "psf clump center", psfClump.X);
    339             snprintf (key, 9, "CLY_%03d", i);
    340             psMetadataAddF32 (header, PS_LIST_TAIL, key, PS_META_REPLACE, "psf clump center", psfClump.Y);
    341             snprintf (key, 9, "CLDX_%03d", i);
    342             psMetadataAddF32 (header, PS_LIST_TAIL, key, PS_META_REPLACE, "psf clump size", psfClump.dX);
    343             snprintf (key, 9, "CLDY_%03d", i);
    344             psMetadataAddF32 (header, PS_LIST_TAIL, key, PS_META_REPLACE, "psf clump size", psfClump.dY);
    345         }
     347        // we now save clump parameters for each region : need to save all of those
     348        int nRegions = psMetadataLookupS32 (&status, analysis, "PSF.CLUMP.NREGIONS");
     349        psMetadataAddS32 (header, PS_LIST_TAIL, "PSF_CLN", PS_META_REPLACE, "number of psf clump regions", nRegions);
     350        for (int i = 0; i < nRegions; i++) {
     351            char regionName[64];
     352            snprintf (regionName, 64, "PSF.CLUMP.REGION.%03d", i);
     353            psMetadata *regionMD = psMetadataLookupPtr (&status, analysis, regionName);
     354
     355            psfClump.X  = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.X");   assert (status);
     356            psfClump.Y  = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.Y");   assert (status);
     357            psfClump.dX = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.DX");  assert (status);
     358            psfClump.dY = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.DY");  assert (status);
     359
     360            char key[16];
     361            snprintf (key, 9, "CLX_%03d", i);
     362            psMetadataAddF32 (header, PS_LIST_TAIL, key, PS_META_REPLACE, "psf clump center", psfClump.X);
     363            snprintf (key, 9, "CLY_%03d", i);
     364            psMetadataAddF32 (header, PS_LIST_TAIL, key, PS_META_REPLACE, "psf clump center", psfClump.Y);
     365            snprintf (key, 9, "CLDX_%03d", i);
     366            psMetadataAddF32 (header, PS_LIST_TAIL, key, PS_META_REPLACE, "psf clump size", psfClump.dX);
     367            snprintf (key, 9, "CLDY_%03d", i);
     368            psMetadataAddF32 (header, PS_LIST_TAIL, key, PS_META_REPLACE, "psf clump size", psfClump.dY);
     369        }
    346370
    347371        // save the dimensions of each parameter
     
    424448        // write an empty FITS segment if we have no PSF information
    425449        if (psfTable->n == 0) {
    426             // XXX this is probably an error (if we have a PSF, how do we have no data?)
    427             psFitsWriteBlank (file->fits, header, tableName);
     450            psError(PM_ERR_PROG, true, "No PSF data to write.");
     451            psFree(tableName);
     452            psFree(residName);
     453            psFree(psfTable);
     454            psFree(header);
     455            return false;
    428456        } else {
    429457            psTrace ("pmFPAfile", 5, "writing psf data %s\n", tableName);
    430             if (!psFitsWriteTable (file->fits, header, psfTable, tableName)) {
    431                 psError(PS_ERR_IO, false, "writing psf table data %s\n", tableName);
     458            if (!psFitsWriteTable(file->fits, header, psfTable, tableName)) {
     459                psError(psErrorCodeLast(), false, "Error writing psf table data %s\n", tableName);
    432460                psFree (tableName);
    433461                psFree (residName);
     
    447475        if (psf->residuals == NULL) {
    448476            // set some header keywords to make it clear there are no residuals?
    449             psFitsWriteBlank (file->fits, header, residName);
     477            if (!psFitsWriteBlank(file->fits, header, residName)) {
     478                psError(psErrorCodeLast(), false, "Unable to write blank PSF residual image.");
     479                psFree(residName);
     480                psFree(header);
     481                return false;
     482            }
    450483            psFree (residName);
    451484            psFree (header);
     
    458491        psMetadataAddS32 (header, PS_LIST_TAIL, "YCENTER", 0, "", psf->residuals->yCenter);
    459492
    460         // write the residuals as three planes of the image
    461         // this call creates an extension with NAXIS3 = 3
     493        // write the residuals as planes of the image
     494        psArray *images = psArrayAllocEmpty (1);
     495        psArrayAdd (images, 1, psf->residuals->Ro);  // z = 0 is Ro
     496
    462497        if (psf->residuals->Rx) {
    463             // this call creates an extension with NAXIS3 = 3
    464             psArray *images = psArrayAllocEmpty (3);
    465             psArrayAdd (images, 1, psf->residuals->Ro);
    466498            psArrayAdd (images, 1, psf->residuals->Rx);
    467499            psArrayAdd (images, 1, psf->residuals->Ry);
    468 
    469             psFitsWriteImageCube (file->fits, header, images, residName);
    470             psFree (images);
    471         } else {
    472             // this call creates an extension with NAXIS3 = 1
    473             psFitsWriteImage(file->fits, header, psf->residuals->Ro, 0, residName);
    474         }
     500        }
     501
     502        // note that all N plane are implicitly of the same type, so we convert the mask
     503        if (psf->residuals->mask) {
     504            psImage *mask = psImageCopy (NULL, psf->residuals->mask, psf->residuals->Ro->type.type);
     505            psArrayAdd (images, 1, mask);
     506            psFree (mask);
     507        }
     508
     509        // psFitsWriteImageCube (file->fits, header, images, residName);
     510        // psFree (images);
     511
     512        if (!psFitsWriteImageCube (file->fits, header, images, residName)) {
     513            psError(psErrorCodeLast(), false, "Unable to write PSF residuals.");
     514            psFree(images);
     515            psFree(residName);
     516            psFree(header);
     517            return false;
     518        }
     519        psFree (images);
    475520        psFree (residName);
     521        psFree (header);
     522    }
     523
     524    // write a representation of the psf model
     525    {
     526        psMetadata *header = psMetadataAlloc ();
     527
     528        if (0) {
     529            // set some header keywords to make it clear there are no residuals?
     530            if (!psFitsWriteBlank (file->fits, header, residName)) {
     531                psError(psErrorCodeLast(), false, "Unable to write blank PSF residuals.");
     532                psFree(residName);
     533                psFree(header);
     534                return false;
     535            }
     536            psFree (residName);
     537            psFree (header);
     538            return true;
     539        }
     540
     541        int DX = 65;
     542        int DY = 65;
     543
     544        psImage *psfMosaic = psImageAlloc (DX, DY, PS_TYPE_F32);
     545        psImageInit (psfMosaic, 0.0);
     546
     547        pmModel *modelRef = pmModelAlloc(psf->type);
     548
     549        // use the center of the center pixel of the image
     550        float xc = 0.5*psf->fieldNx;
     551        float yc = 0.5*psf->fieldNy;
     552
     553        // assign the x and y coords to the image center
     554        // create an object with center intensity of 1000
     555        modelRef->params->data.F32[PM_PAR_SKY] = 0;
     556        modelRef->params->data.F32[PM_PAR_I0] = 1.000;
     557        modelRef->params->data.F32[PM_PAR_XPOS] = xc;
     558        modelRef->params->data.F32[PM_PAR_YPOS] = yc;
     559
     560        // create modelPSF from this model
     561        pmModel *model = pmModelFromPSF (modelRef, psf);
     562        if (model) {
     563            // place the reference object in the image center
     564            pmModelAddWithOffset (psfMosaic, NULL, model, PM_MODEL_OP_FULL | PM_MODEL_OP_CENTER, 0, 0.0, 0.0);
     565            psFree (model);
     566
     567            if (false) {
     568                // this call creates an extension with NAXIS3 = 3
     569                psArray *images = psArrayAllocEmpty (3);
     570                psArrayAdd (images, 1, psfMosaic);
     571                // psArrayAdd (images, 1, psfModel);
     572                // psArrayAdd (images, 1, psfModel);
     573
     574                if (!psFitsWriteImageCube (file->fits, header, images, "PSF_MODEL")) {
     575                    psError(psErrorCodeLast(), false, "Unable to write PSF representation.");
     576                    psFree(images);
     577                    psFree(psfMosaic);
     578                    psFree(modelRef);
     579                    psFree(header);
     580                    return false;
     581                }
     582                psFree (images);
     583            } else {
     584                // this call creates an extension with NAXIS3 = 1
     585                // XXX need to replace PSF_MODEL with rule-based name like residName
     586                if (!psFitsWriteImage(file->fits, header, psfMosaic, 0, "PSF_MODEL")) {
     587                    psError(psErrorCodeLast(), false, "Unable to write PSF representation.");
     588                    psFree(psfMosaic);
     589                    psFree(modelRef);
     590                    psFree(header);
     591                    return false;
     592                }
     593            }
     594        }
     595
     596        psFree (psfMosaic);
     597        psFree (modelRef);
    476598        psFree (header);
    477599    }
     
    523645    // find the FPA phu
    524646    pmFPA *fpa = pmFPAfileSuitableFPA(file, view, config, false); // Suitable FPA for writing
     647    if (!fpa) {
     648        psError(psErrorCodeLast(), false, "Unable to build FPA to write.");
     649        return false;
     650    }
    525651    pmHDU *phu = psMemIncrRefCounter(pmFPAviewThisPHU(view, fpa));
    526652    psFree(fpa);
     
    533659        psMetadataCopy (outhead, phu->header);
    534660    } else {
    535         pmConfigConformHeader (outhead, file->format);
     661        if (!pmConfigConformHeader (outhead, file->format)) {
     662            psError(psErrorCodeLast(), false, "Unable to conform header of PSF PHU.");
     663            psFree(phu);
     664            return false;
     665        }
    536666    }
    537667    psFree(phu);
    538668
    539669    psMetadataAddBool (outhead, PS_LIST_TAIL, "EXTEND", PS_META_REPLACE, "this file has extensions", true);
    540     psFitsWriteBlank (file->fits, outhead, "");
     670    if (!psFitsWriteBlank (file->fits, outhead, "")) {
     671        psError(psErrorCodeLast(), false, "Unable to write PHU for PSF.");
     672        psFree(outhead);
     673        return false;
     674    }
    541675    file->wrote_phu = true;
    542676
     
    568702    }
    569703
    570     psError(PS_ERR_IO, false, "PSF must be read at the chip level");
     704    psError(PM_ERR_CONFIG, true, "PSF must be read at the chip level");
    571705    return false;
    572706}
     
    595729
    596730    if (!pmPSFmodelRead (chip->analysis, view, file, config)) {
    597         psError(PS_ERR_IO, false, "Failed to write PSF for chip");
     731        psError(psErrorCodeLast(), false, "Failed to write PSF for chip");
    598732        return false;
    599733    }
     
    618752    psMetadata *recipe = psMetadataLookupPtr (NULL, config->recipes, "PSPHOT");
    619753    if (!recipe) {
    620         psError(PS_ERR_UNKNOWN, false, "missing recipe %s", "PSPHOT");
     754        psError(PM_ERR_CONFIG, false, "missing recipe %s", "PSPHOT");
    621755        return false;
    622756    }
     
    625759    psMetadata *menu = psMetadataLookupMetadata(&status, file->camera, "EXTNAME.RULES");
    626760    if (!menu) {
    627         psError(PS_ERR_UNKNOWN, true, "missing EXTNAME.RULES in camera.config");
     761        psError(PM_ERR_CONFIG, true, "missing EXTNAME.RULES in camera.config");
    628762        return false;
    629763    }
     
    631765    rule = psMetadataLookupStr(&status, menu, "PSF.TABLE");
    632766    if (!rule) {
    633         psError(PS_ERR_UNKNOWN, true, "missing entry for PSF.TABLE in EXTNAME.RULES in camera.config");
     767        psError(PM_ERR_CONFIG, true, "missing entry for PSF.TABLE in EXTNAME.RULES in camera.config");
    634768        return false;
    635769    }
     
    638772    rule = psMetadataLookupStr(&status, menu, "PSF.RESID");
    639773    if (!rule) {
    640         psError(PS_ERR_UNKNOWN, true, "missing entry for PSF.RESID in EXTNAME.RULES in camera.config");
     774        psError(PM_ERR_CONFIG, true, "missing entry for PSF.RESID in EXTNAME.RULES in camera.config");
    641775        return false;
    642776    }
     
    646780    // advance to the table data extension
    647781    // since we have read the IMAGE header, the TABLE header should exist
    648     if (!psFitsMoveExtName (file->fits, tableName)) {
    649         psAbort("cannot find data extension %s in %s", tableName, file->filename);
     782    if (!psFitsMoveExtName(file->fits, tableName)) {
     783        psError(psErrorCodeLast(), false, "cannot find data extension %s in %s", tableName, file->filename);
     784        return false;
    650785    }
    651786
    652787    // load the PSF model table header
    653788    header = psFitsReadHeader (NULL, file->fits);
    654     if (!header) psAbort("cannot read table header");
     789    if (!header) {
     790        psError(psErrorCodeLast(), false, "Cannot read PSF table header.");
     791        return false;
     792    }
    655793
    656794    pmPSFOptions *options = pmPSFOptionsAlloc();
     
    667805    int nRegions = psMetadataLookupS32 (&status, header, "PSF_CLN");
    668806    if (!status) {
    669         // read old-style psf clump data
    670 
    671         char regionName[64];
    672         snprintf (regionName, 64, "PSF.CLUMP.REGION.000");
    673         psMetadataAddS32 (recipe, PS_LIST_TAIL, "PSF.CLUMP.NREGIONS",  PS_META_REPLACE, "psf clump regions", 1);
    674 
    675         psMetadata *regionMD = psMetadataLookupPtr (&status, recipe, regionName);
    676         if (!regionMD) {
    677             regionMD = psMetadataAlloc();
    678             psMetadataAddMetadata (recipe, PS_LIST_TAIL, regionName, PS_META_REPLACE, "psf clump region", regionMD);
    679             psFree (regionMD);
    680         }
    681 
    682         // psf clump data
    683         pmPSFClump psfClump;
    684 
    685         psfClump.X  = psMetadataLookupF32 (&status, header, "PSF_CLX" );  assert(status);
    686         psfClump.Y  = psMetadataLookupF32 (&status, header, "PSF_CLY" );  assert(status);
    687         psfClump.dX = psMetadataLookupF32 (&status, header, "PSF_CLDX");  assert(status);
    688         psfClump.dY = psMetadataLookupF32 (&status, header, "PSF_CLDY");  assert(status);
    689 
    690         psMetadataAddF32 (regionMD, PS_LIST_TAIL, "PSF.CLUMP.X",  PS_META_REPLACE, "psf clump center", psfClump.X);
    691         psMetadataAddF32 (regionMD, PS_LIST_TAIL, "PSF.CLUMP.Y",  PS_META_REPLACE, "psf clump center", psfClump.Y);
    692         psMetadataAddF32 (regionMD, PS_LIST_TAIL, "PSF.CLUMP.DX", PS_META_REPLACE, "psf clump center", psfClump.dX);
    693         psMetadataAddF32 (regionMD, PS_LIST_TAIL, "PSF.CLUMP.DY", PS_META_REPLACE, "psf clump center", psfClump.dY);
     807        // read old-style psf clump data
     808
     809        char regionName[64];
     810        snprintf (regionName, 64, "PSF.CLUMP.REGION.000");
     811        psMetadataAddS32 (analysis, PS_LIST_TAIL, "PSF.CLUMP.NREGIONS",  PS_META_REPLACE, "psf clump regions", 1);
     812
     813        psMetadata *regionMD = psMetadataLookupPtr (&status, analysis, regionName);
     814        if (!regionMD) {
     815            regionMD = psMetadataAlloc();
     816            psMetadataAddMetadata (analysis, PS_LIST_TAIL, regionName, PS_META_REPLACE, "psf clump region", regionMD);
     817            psFree (regionMD);
     818        }
     819
     820        // psf clump data
     821        pmPSFClump psfClump;
     822
     823        psfClump.X  = psMetadataLookupF32 (&status, header, "PSF_CLX" );  assert(status);
     824        psfClump.Y  = psMetadataLookupF32 (&status, header, "PSF_CLY" );  assert(status);
     825        psfClump.dX = psMetadataLookupF32 (&status, header, "PSF_CLDX");  assert(status);
     826        psfClump.dY = psMetadataLookupF32 (&status, header, "PSF_CLDY");  assert(status);
     827
     828        psMetadataAddF32 (regionMD, PS_LIST_TAIL, "PSF.CLUMP.X",  PS_META_REPLACE, "psf clump center", psfClump.X);
     829        psMetadataAddF32 (regionMD, PS_LIST_TAIL, "PSF.CLUMP.Y",  PS_META_REPLACE, "psf clump center", psfClump.Y);
     830        psMetadataAddF32 (regionMD, PS_LIST_TAIL, "PSF.CLUMP.DX", PS_META_REPLACE, "psf clump center", psfClump.dX);
     831        psMetadataAddF32 (regionMD, PS_LIST_TAIL, "PSF.CLUMP.DY", PS_META_REPLACE, "psf clump center", psfClump.dY);
    694832    } else {
    695         psMetadataAddS32 (recipe, PS_LIST_TAIL, "PSF.CLUMP.NREGIONS",  PS_META_REPLACE, "psf clump regions", nRegions);
    696 
    697         for (int i = 0; i < nRegions; i++) {
    698             char key[10];
    699             char regionName[64];
    700             snprintf (regionName, 64, "PSF.CLUMP.REGION.%03d", i);
    701 
    702             psMetadata *regionMD = psMetadataLookupPtr (&status, recipe, regionName);
    703             if (!regionMD) {
    704                 regionMD = psMetadataAlloc();
    705                 psMetadataAddMetadata (recipe, PS_LIST_TAIL, regionName, PS_META_REPLACE, "psf clump region", regionMD);
    706                 psFree (regionMD);
    707             }
    708 
    709             // psf clump data
    710             pmPSFClump psfClump;
    711 
    712             snprintf (key, 9, "CLX_%03d", i);
    713             psfClump.X  = psMetadataLookupF32 (&status, header, key);  assert(status);
    714             snprintf (key, 9, "CLY_%03d", i);
    715             psfClump.Y  = psMetadataLookupF32 (&status, header, key);  assert(status);
    716             snprintf (key, 9, "CLDX_%03d", i);
    717             psfClump.dX = psMetadataLookupF32 (&status, header, key);  assert(status);
    718             snprintf (key, 9, "CLDY_%03d", i);
    719             psfClump.dY = psMetadataLookupF32 (&status, header, key);  assert(status);
    720 
    721             psMetadataAddF32 (regionMD, PS_LIST_TAIL, "PSF.CLUMP.X",  PS_META_REPLACE, "psf clump center", psfClump.X);
    722             psMetadataAddF32 (regionMD, PS_LIST_TAIL, "PSF.CLUMP.Y",  PS_META_REPLACE, "psf clump center", psfClump.Y);
    723             psMetadataAddF32 (regionMD, PS_LIST_TAIL, "PSF.CLUMP.DX", PS_META_REPLACE, "psf clump center", psfClump.dX);
    724             psMetadataAddF32 (regionMD, PS_LIST_TAIL, "PSF.CLUMP.DY", PS_META_REPLACE, "psf clump center", psfClump.dY);
    725         }
     833        psMetadataAddS32 (analysis, PS_LIST_TAIL, "PSF.CLUMP.NREGIONS",  PS_META_REPLACE, "psf clump regions", nRegions);
     834
     835        for (int i = 0; i < nRegions; i++) {
     836            char key[10];
     837            char regionName[64];
     838            snprintf (regionName, 64, "PSF.CLUMP.REGION.%03d", i);
     839
     840            psMetadata *regionMD = psMetadataLookupPtr (&status, analysis, regionName);
     841            if (!regionMD) {
     842                regionMD = psMetadataAlloc();
     843                psMetadataAddMetadata (analysis, PS_LIST_TAIL, regionName, PS_META_REPLACE, "psf clump region", regionMD);
     844                psFree (regionMD);
     845            }
     846
     847            // psf clump data
     848            pmPSFClump psfClump;
     849
     850            snprintf (key, 9, "CLX_%03d", i);
     851            psfClump.X  = psMetadataLookupF32 (&status, header, key);  assert(status);
     852            snprintf (key, 9, "CLY_%03d", i);
     853            psfClump.Y  = psMetadataLookupF32 (&status, header, key);  assert(status);
     854            snprintf (key, 9, "CLDX_%03d", i);
     855            psfClump.dX = psMetadataLookupF32 (&status, header, key);  assert(status);
     856            snprintf (key, 9, "CLDY_%03d", i);
     857            psfClump.dY = psMetadataLookupF32 (&status, header, key);  assert(status);
     858
     859            psMetadataAddF32 (regionMD, PS_LIST_TAIL, "PSF.CLUMP.X",  PS_META_REPLACE, "psf clump center", psfClump.X);
     860            psMetadataAddF32 (regionMD, PS_LIST_TAIL, "PSF.CLUMP.Y",  PS_META_REPLACE, "psf clump center", psfClump.Y);
     861            psMetadataAddF32 (regionMD, PS_LIST_TAIL, "PSF.CLUMP.DX", PS_META_REPLACE, "psf clump center", psfClump.dX);
     862            psMetadataAddF32 (regionMD, PS_LIST_TAIL, "PSF.CLUMP.DY", PS_META_REPLACE, "psf clump center", psfClump.dY);
     863        }
    726864    }
    727865
     
    764902        char *modeName = psMetadataLookupStr (&status, header, name);
    765903        if (!status) {
    766             psError(PS_ERR_UNKNOWN, true, "inconsistent PSF header: NX & NY defined for PAR %d, but not MD", i);
     904            psError(PM_ERR_PROG, true, "inconsistent PSF header: NX & NY defined for PAR %d, but not MD", i);
    767905            return false;
    768906        }
     
    798936    // read the raw table data
    799937    psArray *table = psFitsReadTable (file->fits);
     938    if (!table) {
     939        psError(psErrorCodeLast(), false, "Unable to read PSF table.");
     940        psFree(header);
     941        return false;
     942    }
    800943
    801944    // fill in the matching psf->params entries
     
    842985    // since we have read the IMAGE header, the TABLE header should exist
    843986    if (!psFitsMoveExtName (file->fits, imageName)) {
    844         psAbort("cannot find data extension %s in %s", imageName, file->filename);
     987        psError(psErrorCodeLast(), false, "Cannot find PSF data extension %s in %s",
     988                imageName, file->filename);
     989        return false;
    845990    }
    846991
    847992    header = psFitsReadHeader (NULL, file->fits);
     993    if (!header) {
     994        psError(psErrorCodeLast(), false, "Unable to read PSF header.");
     995        return false;
     996    }
    848997    int Naxis = psMetadataLookupS32 (&status, header, "NAXIS");
    849998    if (Naxis != 0) {
     
    8651014
    8661015        psRegion fullImage = {0, 0, 0, 0};
    867         psFitsReadImageBuffer(psf->residuals->Ro, file->fits, fullImage, 0); // Desired pixels
    868         if (Nz > 1) {
    869             assert (Nz == 3);
    870             psFitsReadImageBuffer(psf->residuals->Rx, file->fits, fullImage, 1); // Desired pixels
    871             psFitsReadImageBuffer(psf->residuals->Ry, file->fits, fullImage, 2); // Desired pixels
    872         }
    873         // XXX notice that we are not saving the resid->mask
     1016        if (!psFitsReadImageBuffer(psf->residuals->Ro, file->fits, fullImage, 0)) {
     1017            psError(psErrorCodeLast(), false, "Unable to read PSF residual image.");
     1018            return false;
     1019        }
     1020
     1021        // note that all N plane are implicitly of the same type, so we convert the mask
     1022        psImage *mask = psImageCopy(NULL, psf->residuals->mask, psf->residuals->Ro->type.type);
     1023        psImageInit (psf->residuals->mask, 0);
     1024        psImageInit (psf->residuals->Rx, 0.0);
     1025        psImageInit (psf->residuals->Ry, 0.0);
     1026        switch (Nz) {
     1027          case 1: // Ro only
     1028            break;
     1029          case 2: // Ro and mask
     1030            if (!psFitsReadImageBuffer(mask, file->fits, fullImage, 1)) {
     1031                psError(psErrorCodeLast(), false, "Unable to read PSF residual image.");
     1032                return false;
     1033            }
     1034            psImageCopy (psf->residuals->mask, mask, PM_TYPE_RESID_MASK);
     1035            break;
     1036          case 3: // Ro, Rx and Ry, no mask
     1037            if (!psFitsReadImageBuffer(psf->residuals->Rx, file->fits, fullImage, 1)) {
     1038                psError(psErrorCodeLast(), false, "Unable to read PSF residual image.");
     1039                return false;
     1040            }
     1041            if (!psFitsReadImageBuffer(psf->residuals->Ry, file->fits, fullImage, 2)) {
     1042                psError(psErrorCodeLast(), false, "Unable to read PSF residual image.");
     1043                return false;
     1044            }
     1045            break;
     1046          case 4: // Ro, Rx, Ry, and mask:
     1047            if (!psFitsReadImageBuffer(psf->residuals->Rx, file->fits, fullImage, 1)) {
     1048                psError(psErrorCodeLast(), false, "Unable to read PSF residual image.");
     1049                return false;
     1050            }
     1051            if (!psFitsReadImageBuffer(psf->residuals->Ry, file->fits, fullImage, 2)) {
     1052                psError(psErrorCodeLast(), false, "Unable to read PSF residual image.");
     1053                return false;
     1054            }
     1055            if (!psFitsReadImageBuffer(mask, file->fits, fullImage, 3)) {
     1056                psError(psErrorCodeLast(), false, "Unable to read PSF residual image.");
     1057                return false;
     1058            }
     1059            psImageCopy (psf->residuals->mask, mask, PM_TYPE_RESID_MASK);
     1060            break;
     1061        }
     1062        psFree (mask);
    8741063    }
    8751064
Note: See TracChangeset for help on using the changeset viewer.