IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 12585


Ignore:
Timestamp:
Mar 24, 2007, 7:53:55 AM (19 years ago)
Author:
magnier
Message:

modified pmFPAfileIO to call PHU read/write operations from data read/write operations as needed; pmFPAfileBlank becomes pmFPAfileWritePHU

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

Legend:

Unmodified
Added
Removed
  • trunk/psModules/src/camera/pmFPAfileIO.c

    r12558 r12585  
    1717#include "pmFPARead.h"
    1818#include "pmFPAWrite.h"
     19#include "pmFPAfileIO.h"
    1920#include "pmFPAfileFitsIO.h"
    2021#include "pmPeaks.h"
     
    3031#include "pmDetrendDB.h"
    3132
    32 // Open a file that contains an image
    33 static bool openImage(pmFPAfile *file,  // File to open
    34                       const pmFPAview *view, // View pointing to open level
    35                       const char *mode,  // Open mode (for psFitsOpen)
    36                       pmConfig *config  // Configuration
    37                      )
    38 {
    39     bool status;
    40 
    41     file->fits = psFitsOpen (file->filename, mode);
    42     if (file->fits == NULL) {
    43         psError(PS_ERR_IO, false, "error opening file %s\n", file->filename);
    44         return false;
    45     }
    46     file->state = PM_FPA_STATE_OPEN;
    47    
    48     // if needed, set the optional EXTWORD field based on the camera value
    49     if (config && config->format) {
    50         psMetadata *fileMenu = psMetadataLookupMetadata (NULL, config->format, "FILE");
    51         if (!fileMenu) {
    52             psError (PS_ERR_IO, true, "FILE METADATA missing from camera format %s\n", config->formatName);
    53             return false;
    54         }
    55         char *extword = psMetadataLookupStr (&status, fileMenu, "EXTWORD");
    56         if (status) {
    57             psFitsSetExtnameWord (file->fits, extword);
    58         }
    59     }
    60 
    61     // In some cases, I need to call pmFPAAddSourceFromHeader after I've opened the file,
    62     // specifically if I have not called this function on startup.  This happens for the
    63     // images supplied by the detrend database, which are only identified here (above).
    64     // this is never true for the output images, which are constructed later
    65 
    66     if (file->mode == PM_FPA_MODE_READ) {
    67         bool addSource = false;
    68         switch (file->fileLevel) {
    69         case PM_FPA_LEVEL_FPA:
    70             addSource = !file->fpa->hdu;
    71             break;
    72         case PM_FPA_LEVEL_CHIP: {
    73                 pmChip *chip = pmFPAviewThisChip(view, file->fpa);
    74                 if (!chip) {
    75                     psError (PS_ERR_IO, true, "inconsistent file/fpa: fileLevel is CHIP, view is FPA");
    76                     return false;
    77                 }
    78                 addSource = !chip->hdu;
    79                 break;
    80             }
    81         case PM_FPA_LEVEL_CELL: {
    82                 pmCell *cell = pmFPAviewThisCell(view, file->fpa);
    83                 if (!cell) {
    84                     psError (PS_ERR_IO, true, "inconsistent file/fpa: fileLevel is CELL, view is FPA");
    85                     return false;
    86                 }
    87                 addSource = !cell->hdu;
    88                 break;
    89             }
    90         default:
    91             psAbort("fileLevel not correctly set");
    92             break;
    93         }
    94 
    95         if (addSource) {
    96             psMetadata *phu = psFitsReadHeader (NULL, file->fits);
    97             if (!file->format) {
    98                 file->format = pmConfigCameraFormatFromHeader (config, phu);
    99                 if (!file->format) {
    100                     psError(PS_ERR_IO, false, "Failed to read CCD format from %s\n", file->filename);
    101                     psFree(phu);
    102                     return NULL;
    103                 }
    104             } else {
    105                 bool valid;
    106                 if (!pmConfigValidateCameraFormat (&valid, file->format, phu)) {
    107                     psError (PS_ERR_UNKNOWN, false, "Error in config scripts\n");
    108                     psFree (phu);
    109                     return NULL;
    110                 }
    111                 if (!valid) {
    112                     psError(PS_ERR_IO, false, "file %s is not from the required camera", file->filename);
    113                     psFree (phu);
    114                     return NULL;
    115                 }
    116             }
    117             pmFPAview *thisView = pmFPAAddSourceFromHeader (file->fpa, phu, file->format);
    118             psFree (thisView);
    119             psFree (phu);
    120             // XXX we can check the output view to be sure it corresponds to our current view
    121         }
    122     }
    123 #if 0
    124     // XXXX can we now drop this?  (EAM 2007.03.15)
    125     // This should be done by pmFPAfileBlank
    126     if (file->mode == PM_FPA_MODE_WRITE) {
    127         // Want to write out any potential blank
    128         pmFPA *fpa = pmFPAfileSuitableFPA(file, view, config);
    129 
    130         switch (file->fileLevel) {
    131         case PM_FPA_LEVEL_FPA:
    132             pmFPAWrite(fpa, file->fits, NULL, true, false);
    133             break;
    134         case PM_FPA_LEVEL_CHIP: {
    135                 pmChip *chip = pmFPAviewThisChip(view, fpa);
    136                 pmChipWrite(chip, file->fits, NULL, true, false);
    137                 break;
    138             }
    139         case PM_FPA_LEVEL_CELL: {
    140                 pmCell *cell = pmFPAviewThisCell(view, fpa);
    141                 pmCellWrite(cell, file->fits, NULL, true);
    142                 break;
    143             }
    144         default:
    145             psAbort("fileLevel not correctly set");
    146             break;
    147         }
    148 
    149         psFree(fpa);
    150     }
    151 #endif
    152     return true;
    153 
    154 }
    155 
    156 
     33// XXXX should we require this function to be called only within pmFPAfileRead or pmFPAfileWrite?
    15734// open file (if not already opened)
    15835bool pmFPAfileOpen (pmFPAfile *file, const pmFPAview *view, pmConfig *config)
     
    17552    }
    17653
    177     // get the current level
    178     pmFPALevel level = pmFPAviewLevel (view);
    179 
    180     // do we need to open this file?
    181     if (level != file->fileLevel) {
    182         psTrace("psModules.camera", 6, "skip open of %s at this level %s: fileLevel is %s",
    183                 file->name, pmFPALevelToName(level), pmFPALevelToName(file->fileLevel));
    184         return true;
    185     }
    186 
     54    // XXX are these programming errors?
    18755    if (file->mode == PM_FPA_MODE_NONE) {
    18856        psError(PS_ERR_IO, true, "File is mode PM_FPA_MODE_NONE");
     
    19967        mode = writeMode;
    20068    }
    201 
    20269    if ((file->mode == PM_FPA_MODE_WRITE) && !file->save) {
    20370        psTrace("psModules.camera", 6, "skip open for %s, output file not requested", file->name);
     
    281148
    282149    switch (file->type) {
    283       // open the FITS types:
     150        // open the FITS types:
    284151      case PM_FPA_FILE_IMAGE:
    285152      case PM_FPA_FILE_MASK:
     
    287154      case PM_FPA_FILE_HEADER:
    288155      case PM_FPA_FILE_FRINGE:
    289         psTrace ("psModules.camera", 5, "opening %s (type: %d)\n", file->filename, file->type);
    290         if (!openImage(file, view, mode, config)) {
    291             psError(PS_ERR_UNKNOWN, false, "Unable to open image %s (type %d)\n", file->filename, file->type);
    292             return false;
    293         }
    294         break;
    295         // open the FITS object files
    296156      case PM_FPA_FILE_CMF:
    297157        psTrace ("psModules.camera", 5, "opening %s (type: %d)\n", file->filename, file->type);
     
    315175            }
    316176        }
    317         break;
    318 
    319       // defer opening TEXT types:
     177
     178        // In some cases, we need to read the PHU after we've opened the file.  This happens for
     179        // the images supplied by the detrend database, which are only identified here (above).
     180        if (!pmFPAfileReadPHU (file, view, config)) {
     181            psError (PS_ERR_IO, true, "error reading PHU for %s (%s)\n", file->filename, file->name);
     182            return false;
     183        }
     184        break;
     185
     186        // defer opening TEXT types:
    320187      case PM_FPA_FILE_SX:
    321188      case PM_FPA_FILE_OBJ:
     
    340207    PS_ASSERT_PTR_NON_NULL(view, false);
    341208
     209    // an internal file should not be sent here
     210    // XXX is this a programming error?
     211    if (file->mode == PM_FPA_MODE_INTERNAL) {
     212        psError(PS_ERR_IO, true, "File is mode PM_FPA_MODE_INTERNAL");
     213        return false;
     214    }
     215
    342216    if (file->state & PM_FPA_STATE_INACTIVE) {
    343217        psTrace("psModules.camera", 6, "skip read for %s, files is inactive", file->name);
     
    350224    }
    351225
    352     // an internal file should not be returned to here
    353     if (file->mode == PM_FPA_MODE_INTERNAL) {
    354         psError(PS_ERR_IO, true, "File is mode PM_FPA_MODE_INTERNAL");
    355         return false;
    356     }
    357 
    358226    // get the current level
    359227    pmFPALevel level = pmFPAviewLevel (view);
    360228
    361     // do we need to open this file?
    362     if (level == file->fileLevel) {
    363         if (!pmFPAfileOpen (file, view, config)) {
    364             psError(PS_ERR_IO, false, "failed to open file %s", file->name);
    365             return false;
    366         }
    367     }
    368 
    369     // do we need to read this file?
     229    // do we need to read this file? defer until we read the correct level
    370230    if (level != file->dataLevel) {
    371231        psTrace("psModules.camera", 6, "skip reading of %s at this level %s: dataLevel is %s",
     
    374234    }
    375235
     236    // do we need to open this file?
     237    if (level >= file->fileLevel) {
     238        // we are allowed to open a file at a level which is not the fileLevel, but
     239        // we need to supply view at the fileLevel for the file lookup functions below
     240        pmFPAview *fileView = pmFPAviewForLevel (file->fileLevel, view);
     241        if (!pmFPAfileOpen (file, fileView, config)) {
     242            psError(PS_ERR_IO, false, "failed to open file %s (%s)", file->filename, file->name);
     243            psFree (fileView);
     244            return false;
     245        }
     246        psFree (fileView);
     247    }
     248
    376249    // We need to read it --- double-check it's open!
    377250    if (file->state == PM_FPA_STATE_CLOSED) {
    378         file->fileLevel = level;
    379         if (!pmFPAfileOpen (file, view, config)) {
    380             psError(PS_ERR_IO, false, "failed to open file %s when attempting to read", file->name);
    381             return false;
    382         }
    383     }
    384 
     251        psError(PS_ERR_IO, false, "failed to open file %s when attempting to read", file->name);
     252        return false;
     253    }
     254
     255    // select a reading method
     256    bool status = false;
    385257    switch (file->type) {
    386     case PM_FPA_FILE_IMAGE:
    387         if (!pmFPAviewReadFitsImage(view, file)) {
    388             psError(PS_ERR_UNKNOWN, false, "skipping %s (type: %d)\n", file->filename, file->type);
    389             return false;
    390         }
    391         psTrace ("psModules.camera", 5, "reading %s (type: %d)\n", file->filename, file->type);
    392         break;
    393     case PM_FPA_FILE_MASK:
    394         if (!pmFPAviewReadFitsMask(view, file)) {
    395             psError(PS_ERR_UNKNOWN, false, "skipping %s (type: %d)\n", file->filename, file->type);
    396             return false;
    397         }
    398         psTrace ("psModules.camera", 5, "reading %s (type: %d)\n", file->filename, file->type);
    399         break;
    400     case PM_FPA_FILE_WEIGHT:
    401         if (!pmFPAviewReadFitsWeight(view, file)) {
    402             psError(PS_ERR_UNKNOWN, false, "skipping %s (type: %d)\n", file->filename, file->type);
    403             return false;
    404         }
    405         psTrace ("psModules.camera", 5, "reading %s (type: %d)\n", file->filename, file->type);
    406         break;
    407     case PM_FPA_FILE_HEADER:
    408         if (!pmFPAviewReadFitsHeaderSet(view, file)) {
    409             psError(PS_ERR_UNKNOWN, false, "skipping %s (type: %d)\n", file->filename, file->type);
    410             return false;
    411         }
    412         psTrace ("psModules.camera", 5, "reading %s (type: %d)\n", file->filename, file->type);
    413         break;
    414     case PM_FPA_FILE_FRINGE:
    415         if (!pmFPAviewReadFitsImage (view, file)) {
    416             psError(PS_ERR_UNKNOWN, false, "skipping %s (type: %d)\n", file->filename, file->type);
    417             return false;
    418         }
    419         psTrace ("psModules.camera", 5, "reading %s (type: %d)\n", file->filename, file->type);
    420 
    421         if (!pmFPAviewReadFitsTable(view, file, "FRINGE")) {
    422             psError(PS_ERR_UNKNOWN, false, "Unable to read fringe data from %s.\n", file->filename);
    423             return false;
    424         }
    425 
    426         break;
    427 
    428     case PM_FPA_FILE_SX:
    429     case PM_FPA_FILE_RAW:
    430     case PM_FPA_FILE_OBJ:
    431     case PM_FPA_FILE_CMP:
    432     case PM_FPA_FILE_CMF:
    433         pmFPAviewReadObjects (view, file, config);
    434         psTrace ("psModules.camera", 5, "reading %s (type: %d)\n", file->filename, file->type);
    435         break;
    436 
    437     case PM_FPA_FILE_PSF:
    438         pmFPAviewReadPSFmodel (view, file, config);
    439         psTrace ("psModules.camera", 5, "reading %s (type: %d)\n", file->filename, file->type);
    440         break;
    441 
    442     case PM_FPA_FILE_JPEG:
    443     case PM_FPA_FILE_KAPA:
    444         break;
    445 
    446     default:
    447         psError(PS_ERR_IO, true, "warning: type mismatch; saw type %d", file->type);
    448         return false;
    449     }
     258      case PM_FPA_FILE_IMAGE:
     259        status = pmFPAviewReadFitsImage(view, file);
     260        break;
     261      case PM_FPA_FILE_MASK:
     262        status = pmFPAviewReadFitsMask(view, file);
     263        break;
     264      case PM_FPA_FILE_WEIGHT:
     265        status = pmFPAviewReadFitsWeight(view, file);
     266        break;
     267      case PM_FPA_FILE_HEADER:
     268        status = pmFPAviewReadFitsHeaderSet(view, file);
     269        break;
     270      case PM_FPA_FILE_FRINGE:
     271        status = pmFPAviewReadFitsImage (view, file);
     272        if (status) {
     273            if (!pmFPAviewReadFitsTable(view, file, "FRINGE")) {
     274                psError(PS_ERR_UNKNOWN, false, "Unable to read fringe data from %s.\n", file->filename);
     275                return false;
     276            }
     277        }
     278        break;
     279      case PM_FPA_FILE_SX:
     280      case PM_FPA_FILE_RAW:
     281      case PM_FPA_FILE_OBJ:
     282      case PM_FPA_FILE_CMP:
     283      case PM_FPA_FILE_CMF:
     284        status = pmFPAviewReadObjects (view, file, config);
     285        break;
     286      case PM_FPA_FILE_PSF:
     287        status = pmFPAviewReadPSFmodel (view, file, config);
     288        break;
     289      case PM_FPA_FILE_JPEG:
     290      case PM_FPA_FILE_KAPA:
     291        break;
     292      default:
     293        psError(PS_ERR_IO, true, "warning: type mismatch; saw type %d (%s)", file->type, file->name);
     294        return false;
     295    }
     296
     297    if (!status) {
     298        psError(PS_ERR_UNKNOWN, false, "skipping %s (%s)\n", file->filename, file->name);
     299        return false;
     300    }
     301    psTrace ("psModules.camera", 5, "read %s (%s)\n", file->filename, file->name);
     302
    450303    return true;
    451304}
     
    461314    }
    462315
    463     // an internal file should not be returned to here
     316    // an internal file should not be sent here
    464317    if (file->mode == PM_FPA_MODE_INTERNAL) {
    465318        psError(PS_ERR_IO, true, "File is mode PM_FPA_MODE_INTERNAL");
     
    470323    pmFPALevel level = pmFPAviewLevel (view);
    471324
    472     // do we need to read this file?
     325    // do we need to free this file?
    473326    if (level != file->freeLevel) {
    474327        psTrace("psModules.camera", 6, "skip free of %s at this level %s: freeLevel is %s",
     
    484337    case PM_FPA_FILE_FRINGE:
    485338        if (pmFPAviewFreeData(view, file)) {
    486             psTrace ("psModules.camera", 5, "freed %s for %s (type: %d)\n", file->filename, file->name, file->type);
     339            psTrace ("psModules.camera", 5, "freed %s (%s)\n", file->filename, file->name);
    487340            if (file->filename == NULL) {
    488341                psTrace ("psModules.camera", 5, "filename is not defined for %s\n", file->name);
    489342            }
    490343        } else {
    491             psError(PS_ERR_UNKNOWN, false, "skipping %s (type: %d)\n", file->filename, file->type);
     344            psError(PS_ERR_UNKNOWN, false, "skipping %s (%s)\n", file->filename, file->name);
    492345            return false;
    493346        }
     
    499352    case PM_FPA_FILE_CMF:
    500353        // pmFPAviewFreeObjects (view, file);
    501         psTrace ("psModules.camera", 5, "NOT freeing %s (type: %d)\n", file->filename, file->type);
     354        psTrace ("psModules.camera", 5, "NOT freeing %s (%s)\n", file->filename, file->name);
    502355        break;
    503356
     
    515368}
    516369
    517 bool pmFPAfileBlank(pmFPAfile *file, const pmFPAview *view, pmConfig *config)
     370bool pmFPAfileWrite(pmFPAfile *file, const pmFPAview *view, pmConfig *config)
    518371{
    519372    PS_ASSERT_PTR_NON_NULL(file, false);
     
    521374
    522375    if (file->state & PM_FPA_STATE_INACTIVE) {
    523         psTrace("psModules.camera", 6, "skip blank for %s, files is inactive", file->name);
     376        psTrace("psModules.camera", 6, "skip write for %s, files is inactive", file->name);
    524377        return true;
    525378    }
    526379
    527380    if (file->mode != PM_FPA_MODE_WRITE) {
    528         psTrace("psModules.camera", 6, "skip blank for %s, mode is not WRITE", file->name);
     381        psTrace("psModules.camera", 6, "skip write for %s, mode is not WRITE", file->name);
    529382        return true;
    530383    }
     
    537390
    538391    if (!file->save) {
    539         psTrace("psModules.camera", 6, "skip blank for %s, save is FALSE", file->name);
     392        psTrace("psModules.camera", 6, "skip write for %s, save is FALSE", file->name);
    540393        return true;
    541394    }
     
    544397    pmFPALevel level = pmFPAviewLevel (view);
    545398
    546     if (level != file->fileLevel) {
    547         psTrace("psModules.camera", 6, "skip blank for %s, level is %s", file->name,
    548                 pmFPALevelToName(file->freeLevel));
    549         return true;
    550     }
    551 
    552     // do we need to open this file?
    553     if (!pmFPAfileOpen (file, view, config)) {
    554         psError(PS_ERR_IO, false, "failed to open %s", file->filename);
    555         return false;
    556     }
    557 
    558     switch (file->type) {
    559     case PM_FPA_FILE_IMAGE:
    560     case PM_FPA_FILE_MASK:
    561     case PM_FPA_FILE_WEIGHT:
    562     case PM_FPA_FILE_FRINGE:
    563     if (file->mode == PM_FPA_MODE_WRITE) {
    564         // Want to write out any potential blank
    565         pmFPA *fpa = pmFPAfileSuitableFPA(file, view, config); // FPA, ensuring it's ready to write a blank
    566 
    567         switch (file->fileLevel) {
    568         case PM_FPA_LEVEL_FPA:
    569             pmFPAWrite(fpa, file->fits, NULL, true, false);
    570             break;
    571         case PM_FPA_LEVEL_CHIP: {
    572                 pmChip *chip = pmFPAviewThisChip(view, fpa);
    573                 pmChipWrite(chip, file->fits, NULL, true, false);
    574                 break;
    575             }
    576         case PM_FPA_LEVEL_CELL: {
    577                 pmCell *cell = pmFPAviewThisCell(view, fpa);
    578                 pmCellWrite(cell, file->fits, NULL, true);
    579                 break;
    580             }
    581         default:
    582             psAbort("fileLevel not correctly set");
    583             break;
    584         }
    585 
    586         psFree(fpa);
    587     }
    588     case PM_FPA_FILE_SX:
    589     case PM_FPA_FILE_RAW:
    590     case PM_FPA_FILE_OBJ:
    591     case PM_FPA_FILE_CMP:
    592     case PM_FPA_FILE_CMF:
    593     case PM_FPA_FILE_PSF:
    594     case PM_FPA_FILE_JPEG:
    595     case PM_FPA_FILE_KAPA:
    596       break;
    597     default:
    598         fprintf (stderr, "warning: type mismatch\n");
    599         return false;
    600     }
    601     return true;
    602 }
    603 
    604 
    605 bool pmFPAfileWrite(pmFPAfile *file, const pmFPAview *view, pmConfig *config)
    606 {
    607     PS_ASSERT_PTR_NON_NULL(file, false);
    608     PS_ASSERT_PTR_NON_NULL(view, false);
    609 
    610     if (file->state & PM_FPA_STATE_INACTIVE) {
    611         psTrace("psModules.camera", 6, "skip write for %s, files is inactive", file->name);
    612         return true;
    613     }
    614 
    615     if (file->mode != PM_FPA_MODE_WRITE) {
    616         psTrace("psModules.camera", 6, "skip write for %s, mode is not WRITE", file->name);
    617         return true;
    618     }
    619 
    620     // an internal file should not be returned to here
    621     if (file->mode == PM_FPA_MODE_INTERNAL) {
    622         psError(PS_ERR_IO, true, "File is mode PM_FPA_MODE_INTERNAL");
    623         return false;
    624     }
    625 
    626     if (!file->save) {
    627         psTrace("psModules.camera", 6, "skip write for %s, save is FALSE", file->name);
    628         return true;
    629     }
    630 
    631     // get the current level
    632     pmFPALevel level = pmFPAviewLevel (view);
    633 
    634399    // do we need to write this file?
    635400    if ((file->mosaicLevel == PM_FPA_LEVEL_NONE && level != file->dataLevel) || // No mosaicking happening
    636             (file->mosaicLevel != PM_FPA_LEVEL_NONE && level != file->mosaicLevel)) { // Mosaicking happening
     401        (file->mosaicLevel != PM_FPA_LEVEL_NONE && level != file->mosaicLevel)) { // Mosaicking happening
    637402        psTrace("psModules.camera", 6, "skip writing of %s at this level %s: dataLevel is %s",
    638403                file->name, pmFPALevelToName(level), pmFPALevelToName(file->dataLevel));
     
    641406
    642407    // do we need to open this file?
     408    // XXX do we need both mosaicLevel and dataLevel?
     409# if (0)   
    643410    if (level >= file->fileLevel &&
    644             (file->mosaicLevel == PM_FPA_LEVEL_NONE ||
    645             (file->mosaicLevel != PM_FPA_LEVEL_NONE && level >= file->mosaicLevel))) {
     411        (file->mosaicLevel == PM_FPA_LEVEL_NONE ||
     412        (file->mosaicLevel != PM_FPA_LEVEL_NONE && level >= file->mosaicLevel))) {
    646413        if (!pmFPAfileOpen (file, view, config)) {
    647414            psError(PS_ERR_IO, false, "failed to open %s", file->filename);
     
    649416        }
    650417    }
    651 
     418# endif
     419
     420    // open the file if not yet opened
     421    if (level >= file->fileLevel) {
     422        // we are allowed to open a file at a level which is not the fileLevel, but
     423        // we need to supply view at the fileLevel for the file lookup functions below
     424        pmFPAview *fileView = pmFPAviewForLevel (file->fileLevel, view);
     425        if (!pmFPAfileOpen (file, fileView, config)) {
     426            psError(PS_ERR_IO, false, "failed to open %s (%s)", file->filename, file->name);
     427            psFree (fileView);
     428            return false;
     429        }
     430        psFree (fileView);
     431    }
     432
     433    // do we need to write out a PHU?
     434    if (level >= file->fileLevel) {
     435        if (!pmFPAfileWritePHU (file, view, config)) {
     436            psError(PS_ERR_IO, false, "failed to write phu for %s (%s)", file->filename, file->name);
     437            return false;
     438        }
     439    }
     440
     441    // XXX catch error status
    652442    switch (file->type) {
    653443    case PM_FPA_FILE_IMAGE:
     
    908698        switch (place) {
    909699        case PM_FPA_BEFORE:
     700# if (0)
    910701            if (!pmFPAfileOpen(file, view, config)) {
    911702                psError(PS_ERR_IO, false, "failed OPEN in FPA_BEFORE block for %s", file->name);
    912703                goto failure;
    913704            }
     705# endif
    914706            if (!pmFPAfileRead (file, view, config)) {
    915707                psError(PS_ERR_IO, false, "failed READ in FPA_BEFORE block for %s", file->name);
     
    920712                goto failure;
    921713            }
     714# if (0)
    922715            if (!pmFPAfileBlank(file, view, config)) {
    923716                psError(PS_ERR_IO, false, "failed BLANK in FPA_BEFORE block for %s", file->name);
    924717                goto failure;
    925718            }
     719# endif
    926720            break;
    927721        case PM_FPA_AFTER:
     
    968762}
    969763
     764// for this file and view, if we need to read a PHU, read it.  return true for any non-error
     765// condition. this function should be called by pmFPAfileOpen.
     766bool pmFPAfileReadPHU (pmFPAfile *file, const pmFPAview *view, pmConfig *config)
     767{
     768    // required conditions
     769    if (file->mode != PM_FPA_MODE_READ) return true;
     770    if (file->state != PM_FPA_STATE_OPEN) psAbort ("pmFPAfileReadPHU called on unopened file");
     771    if (file->fpa == NULL) psAbort ("pmFPAfileReadPHU called on file without an FPA");
     772
     773    // check if we need to read a PHU (if not, return true)
     774    switch (file->fileLevel) {
     775      case PM_FPA_LEVEL_FPA:
     776        if (file->fpa->hdu) return true;
     777        break;
     778      case PM_FPA_LEVEL_CHIP: {
     779          pmChip *chip = pmFPAviewThisChip(view, file->fpa);
     780          if (!chip) psAbort ("inconsistent file/fpa: fileLevel is CHIP, view is FPA");
     781          if (chip->hdu) return true;
     782          break;
     783      }
     784      case PM_FPA_LEVEL_CELL: {
     785          pmCell *cell = pmFPAviewThisCell(view, file->fpa);
     786          if (!cell) psAbort ("inconsistent file/fpa: fileLevel is CELL, view is FPA");
     787          if (cell->hdu) return true;
     788          break;
     789      }
     790      default:
     791        psAbort("fileLevel not correctly set");
     792        break;
     793    }
     794
     795    // XXX do we need to advance to the first HDU?
     796    psMetadata *phu = psFitsReadHeader (NULL, file->fits);
     797    if (!file->format) {
     798        file->format = pmConfigCameraFormatFromHeader (config, phu);
     799        if (!file->format) {
     800            psError(PS_ERR_IO, false, "Failed to read CCD format from %s\n", file->filename);
     801            psFree(phu);
     802            return false;
     803        }
     804    } else {
     805        bool valid;
     806        if (!pmConfigValidateCameraFormat (&valid, file->format, phu)) {
     807            psError (PS_ERR_UNKNOWN, false, "Error in camera configuration\n");
     808            psFree (phu);
     809            return false;
     810        }
     811        if (!valid) {
     812            psError(PS_ERR_IO, false, "file %s is not from the required camera", file->filename);
     813            psFree (phu);
     814            return false;
     815        }
     816    }
     817    pmFPAview *thisView = pmFPAAddSourceFromHeader (file->fpa, phu, file->format);
     818    psFree (thisView);
     819    psFree (phu);
     820    // XXX we can check the output view to be sure it corresponds to our current view
     821    return true;
     822}
     823
     824// XXX this function is only called from pmFPAfileWrite
     825bool pmFPAfileWritePHU(pmFPAfile *file, const pmFPAview *view, pmConfig *config)
     826{
     827    PS_ASSERT_PTR_NON_NULL(file, false);
     828    PS_ASSERT_PTR_NON_NULL(view, false);
     829
     830    // XXX we need to test IF we need to write out a PHU blank
     831    // is this function performed by the pmFPAWrite,etc functions?
     832
     833    switch (file->type) {
     834    case PM_FPA_FILE_IMAGE:
     835    case PM_FPA_FILE_MASK:
     836    case PM_FPA_FILE_WEIGHT:
     837    case PM_FPA_FILE_FRINGE:
     838    if (file->mode == PM_FPA_MODE_WRITE) {
     839        // Want to write out any potential blank
     840        pmFPA *fpa = pmFPAfileSuitableFPA(file, view, config); // FPA, ensuring it's ready to write a blank
     841
     842        switch (file->fileLevel) {
     843        case PM_FPA_LEVEL_FPA:
     844            pmFPAWrite(fpa, file->fits, NULL, true, false);
     845            break;
     846        case PM_FPA_LEVEL_CHIP: {
     847                pmChip *chip = pmFPAviewThisChip(view, fpa);
     848                pmChipWrite(chip, file->fits, NULL, true, false);
     849                break;
     850            }
     851        case PM_FPA_LEVEL_CELL: {
     852                pmCell *cell = pmFPAviewThisCell(view, fpa);
     853                pmCellWrite(cell, file->fits, NULL, true);
     854                break;
     855            }
     856        default:
     857            psAbort("fileLevel not correctly set");
     858            break;
     859        }
     860
     861        psFree(fpa);
     862    }
     863    case PM_FPA_FILE_SX:
     864    case PM_FPA_FILE_RAW:
     865    case PM_FPA_FILE_OBJ:
     866    case PM_FPA_FILE_CMP:
     867    case PM_FPA_FILE_CMF:
     868    case PM_FPA_FILE_PSF:
     869    case PM_FPA_FILE_JPEG:
     870    case PM_FPA_FILE_KAPA:
     871      break;
     872    default:
     873        fprintf (stderr, "warning: type mismatch\n");
     874        return false;
     875    }
     876    return true;
     877}
     878
     879# if (0)
     880
     881//XXX from pmFPAfileOpen (Image section)
     882        psTrace ("psModules.camera", 5, "opening %s (type: %d)\n", file->filename, file->type);
     883        if (!openImage(file, view, mode, config)) {
     884            psError(PS_ERR_UNKNOWN, false, "Unable to open image %s (type %d)\n", file->filename, file->type);
     885            return false;
     886        }
     887        break;
     888        // open the FITS object files
     889/////
     890
     891
     892// Open a file that contains an image
     893static bool openImage(pmFPAfile *file,  // File to open
     894                      const pmFPAview *view, // View pointing to open level
     895                      const char *mode,  // Open mode (for psFitsOpen)
     896                      pmConfig *config  // Configuration
     897                     )
     898{
     899    bool status;
     900
     901    file->fits = psFitsOpen (file->filename, mode);
     902    if (file->fits == NULL) {
     903        psError(PS_ERR_IO, false, "error opening file %s\n", file->filename);
     904        return false;
     905    }
     906    file->state = PM_FPA_STATE_OPEN;
     907   
     908    // if needed, set the optional EXTWORD field based on the camera value
     909    if (config && config->format) {
     910        psMetadata *fileMenu = psMetadataLookupMetadata (NULL, config->format, "FILE");
     911        if (!fileMenu) {
     912            psError (PS_ERR_IO, true, "FILE METADATA missing from camera format %s\n", config->formatName);
     913            return false;
     914        }
     915        char *extword = psMetadataLookupStr (&status, fileMenu, "EXTWORD");
     916        if (status) {
     917            psFitsSetExtnameWord (file->fits, extword);
     918        }
     919    }
     920
     921    // In some cases, we need to read the PHU after we've opened the file.  This happens for
     922    // the images supplied by the detrend database, which are only identified here (above).
     923    if (!pmFPAfileReadPHU (file, view, config)) {
     924        psError (PS_ERR_IO, true, "error reading PHU for %s (%s)\n", file->filename, file->name);
     925        return false;
     926    }
     927    return true;
     928}
     929# endif
  • trunk/psModules/src/camera/pmFPAfileIO.h

    r11801 r12585  
    44 * @author EAM, IfA
    55 *
    6  * @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
    7  * @date $Date: 2007-02-15 00:51:20 $
     6 * @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
     7 * @date $Date: 2007-03-24 17:53:55 $
    88 * Copyright 2004-2005 Institute for Astronomy, University of Hawaii
    99 */
     
    3939bool pmFPAfileIOChecks (pmConfig *config, const pmFPAview *view, pmFPAfilePlace place);
    4040
     41bool pmFPAfileWritePHU(pmFPAfile *file, const pmFPAview *view, pmConfig *config);
     42bool pmFPAfileReadPHU (pmFPAfile *file, const pmFPAview *view, pmConfig *config);
     43
    4144/// @}
    4245# endif
  • trunk/psModules/src/camera/pmFPAview.c

    r11687 r12585  
    33 *  @author EAM, IfA
    44 *
    5  *  @version $Revision: 1.9 $ $Name: not supported by cvs2svn $
    6  *  @date $Date: 2007-02-07 23:58:17 $
     5 *  @version $Revision: 1.10 $ $Name: not supported by cvs2svn $
     6 *  @date $Date: 2007-03-24 17:53:55 $
    77 *
    88 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    5151}
    5252
     53// return a view restricted to the level (must be >= the input level)
     54pmFPAview *pmFPAviewForLevel(pmFPALevel level, const pmFPAview *input)
     55{
     56    PS_ASSERT_PTR_NON_NULL(input, NULL);
     57
     58    pmFPAview *output = pmFPAviewAlloc (input->nRows);
     59    *output = *input;
     60
     61    switch (level) {
     62      case PM_FPA_LEVEL_FPA:
     63        output->chip = -1;
     64      case PM_FPA_LEVEL_CHIP:
     65        output->cell = -1;
     66      case PM_FPA_LEVEL_CELL:
     67        output->readout = -1;
     68        break;
     69      default:
     70        break;
     71    }
     72    return output;
     73}
    5374
    5475pmFPALevel pmFPAviewLevel(const pmFPAview *view)
  • trunk/psModules/src/camera/pmFPAview.h

    r11253 r12585  
    44 * @author Eugene Magnier, IfA
    55 *
    6  * @version $Revision: 1.6 $ $Name: not supported by cvs2svn $
    7  * @date $Date: 2007-01-24 02:54:14 $
     6 * @version $Revision: 1.7 $ $Name: not supported by cvs2svn $
     7 * @date $Date: 2007-03-24 17:53:55 $
    88 * Copyright 2005-2006 Institute for Astronomy, University of Hawaii
    99 */
     
    4141bool pmFPAviewReset(pmFPAview *view     ///< View to reset
    4242                   );
     43
     44// return a view restricted to the level (must be >= the input level)
     45pmFPAview *pmFPAviewForLevel(pmFPALevel level, const pmFPAview *input);
    4346
    4447/// Determine the current view level
Note: See TracChangeset for help on using the changeset viewer.