IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jul 17, 2008, 12:38:15 PM (18 years ago)
Author:
Paul Price
Message:

The FPA format in psastroDataSave was bad: the GPC1 inputs (which is used for the output) had HDUs in the chips, but the output format is supposed to have an HDU in the FPA. It comes down to a pmFPAfileSuitableFPA not being called at the right place. To fix this, I've sprinkled calls to pmFPAfileSuitableFPA in all the FITS writing functions. This provides the HDU in the right place to these functions. pmFPAfileSuitableFPA should now automatically take care of header conformity, updating concepts, etc. Now, if adding a function to write FITS data, you should make sure that you call pmFPAfileSuitableFPA to get the right format, HDU, header keywords, etc.

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

Legend:

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

    r18554 r18601  
    2222#include "pmFPAConstruct.h"
    2323#include "pmDark.h"
    24 
    25 pmFPA *pmFPAfileSuitableFPA(const pmFPAfile *file, const pmFPAview *view, const pmConfig *config)
    26 {
    27     PS_ASSERT_PTR_NON_NULL(file, NULL);
    28     PS_ASSERT_PTR_NON_NULL(view, NULL);
     24#include "pmConcepts.h"
     25
     26// Get a suitable FPA for the file; generate it if necessary
     27static pmFPA *suitableFPA(const pmFPAfile *file, // File for which to get FPA
     28                          const pmFPAview *view, // View at which to produce the FPA
     29                          pmConfig *config, // Configuration (for concepts update)
     30                          bool pixels   // Worry about copying pixels?
     31    )
     32{
     33    psAssert(file, "It's supposed to be here");
     34    psAssert(view, "It's supposed to be here");
     35    psAssert(config, "It's supposed to be here");
    2936
    3037    if (!file->format) {                // Working with the same output format as input format
     
    8693    switch (level) {
    8794      case PM_FPA_LEVEL_FPA:
    88         if (!pmFPACopy(copy, file->fpa)) {
     95        if ((pixels && !pmFPACopy(copy, file->fpa)) ||
     96            (!pixels && !pmFPACopyStructure(copy, file->fpa, 1, 1))) {
    8997            psError(PS_ERR_UNKNOWN, false, "Unable to copy FPA for format conversion.\n");
    9098            return NULL;
     
    94102          pmChip *chip = pmFPAviewThisChip(view, copy); // Chip of interest
    95103          pmChip *srcChip = pmFPAviewThisChip(view, file->fpa); // Source chip
    96           if (!pmChipCopy(chip, srcChip)) {
     104          if ((pixels && !pmChipCopy(chip, srcChip)) ||
     105              (!pixels && !pmChipCopyStructure(chip, srcChip, 1, 1))) {
    97106              psError(PS_ERR_UNKNOWN, false, "Unable to copy chip for format conversion.\n");
    98107              return false;
     
    103112          pmCell *cell = pmFPAviewThisCell(view, copy); // Cell of interest
    104113          pmCell *srcCell = pmFPAviewThisCell(view, file->fpa); // Source cell
    105           if (!pmCellCopy(cell, srcCell)) {
     114          if ((pixels && !pmCellCopy(cell, srcCell)) ||
     115              (!pixels && !pmCellCopyStructure(cell, srcCell, 1, 1))) {
    106116              psError(PS_ERR_UNKNOWN, false, "Unable to copy cell for format conversion.\n");
    107117              return false;
     
    115125    }
    116126
     127    // Unreachable
    117128    return NULL;
    118 
     129}
     130
     131
     132pmFPA *pmFPAfileSuitableFPA(const pmFPAfile *file, const pmFPAview *view, pmConfig *config, bool pixels)
     133{
     134    PS_ASSERT_PTR_NON_NULL(file, NULL);
     135    PS_ASSERT_PTR_NON_NULL(view, NULL);
     136    PS_ASSERT_PTR_NON_NULL(config, NULL);
     137
     138    pmFPA *fpa = suitableFPA(file, view, config, pixels); // A suitable FPA for writing
     139    if (!fpa) {
     140        psError(PS_ERR_UNKNOWN, false, "Unable to produce suitable FPA.");
     141        return NULL;
     142    }
     143
     144    // Ensure headers and all are updated
     145    // This is here so that the individual write functions (e.g., images, PSFs, sources, etc) don't have to
     146    // take care of all this themselves (because they generally don't).
     147    switch (file->type) {
     148      case PM_FPA_FILE_IMAGE:
     149      case PM_FPA_FILE_MASK:
     150      case PM_FPA_FILE_WEIGHT:
     151      case PM_FPA_FILE_HEADER:
     152      case PM_FPA_FILE_FRINGE:
     153      case PM_FPA_FILE_DARK:
     154      case PM_FPA_FILE_CMP:
     155      case PM_FPA_FILE_CMF:
     156      case PM_FPA_FILE_PSF:
     157      case PM_FPA_FILE_ASTROM_MODEL:
     158      case PM_FPA_FILE_ASTROM_REFSTARS: {
     159          pmHDU *hdu = pmFPAviewThisHDU(view, fpa);
     160          if (hdu) {
     161              if (!hdu->header) {
     162                  hdu->header = psMetadataAlloc();
     163              }
     164              pmConfigConformHeader(hdu->header, file->format);
     165
     166              // whenever we write out a mask image, we should define the bits which represent mask concepts
     167              if (file->type == PM_FPA_FILE_MASK) {
     168                  assert (hdu->header);
     169                  if (!pmConfigMaskWriteHeader(config, hdu->header)) {
     170                      psError(PS_ERR_UNKNOWN, false,
     171                              "failed to set the bitmask names in the PHU header for Image %s (%s)\n",
     172                              file->filename, file->name);
     173                      return false;
     174                  }
     175              }
     176          }
     177
     178          pmChip *chip = pmFPAviewThisChip(view, fpa); // Chip of interest, or NULL
     179          pmCell *cell = pmFPAviewThisCell(view, fpa); // Cell of interest, or NULL
     180          if (!pmFPAUpdateNames(fpa, chip, cell)) {
     181              psError(PS_ERR_UNKNOWN, false, "Unable to update names in header.");
     182              return false;
     183          }
     184
     185          pmConceptSource sources = PM_CONCEPT_SOURCE_HEADER | PM_CONCEPT_SOURCE_CELLS |
     186              PM_CONCEPT_SOURCE_DEFAULTS | PM_CONCEPT_SOURCE_DATABASE; // Concept sources to write
     187          if (cell) {
     188              if (!pmConceptsWriteCell(cell, sources, true, config)) {
     189                  psError(PS_ERR_IO, false, "Unable to write concepts for cell.\n");
     190                  return false;
     191              }
     192          } else if (chip) {
     193              if (!pmConceptsWriteChip(chip, sources, true, true, config)) {
     194                  psError(PS_ERR_IO, false, "Unable to write concepts for chip.\n");
     195                  return false;
     196              }
     197          } else if (!pmConceptsWriteFPA(fpa, sources, true, config)) {
     198              psError(PS_ERR_IO, false, "Unable to write concepts for FPA.\n");
     199              return false;
     200          }
     201          break;
     202      }
     203      default:
     204        // No action
     205        break;
     206    }
     207
     208    return fpa;
    119209}
    120210
     
    142232
    143233// given an already-opened fits file, write the table corresponding to the specified view
    144 bool pmFPAviewWriteFitsTable(const pmFPAview *view, pmFPAfile *file, const char *name)
    145 {
    146     PS_ASSERT_PTR_NON_NULL(view, false);
    147     PS_ASSERT_PTR_NON_NULL(file, false);
    148 
    149     pmFPA *fpa = file->fpa;            // FPA of interest
     234bool pmFPAviewWriteFitsTable(const pmFPAview *view, pmFPAfile *file, const char *name, pmConfig *config)
     235{
     236    PS_ASSERT_PTR_NON_NULL(view, false);
     237    PS_ASSERT_PTR_NON_NULL(file, false);
     238
     239    pmFPA *fpa = pmFPAfileSuitableFPA(file, view, config, false); // FPA of interest
    150240    psFits *fits = file->fits;          // FITS file
    151241
     
    280370    PS_ASSERT_PTR_NON_NULL(fits, false);
    281371
    282     pmFPA *fpa = pmFPAfileSuitableFPA(file, view, config); // FPA to write
     372    pmFPA *fpa = pmFPAfileSuitableFPA(file, view, config, true); // FPA to write
    283373
    284374    switch (pmFPAviewLevel(view)) {
     
    489579
    490580    // select or generate the desired fpa in the correct output format
    491     pmFPA *fpa = pmFPAfileSuitableFPA(file, view, config);
     581    pmFPA *fpa = pmFPAfileSuitableFPA(file, view, config, false);
    492582    pmHDU *phu = pmFPAviewThisHDU(view, fpa);
    493583    if (!phu || !phu->blankPHU) {
     
    499589    // whenever we write out a mask image, we should define the bits which represent mask concepts
    500590    if (file->type == PM_FPA_FILE_MASK) {
    501         assert (phu->header);
    502         if (!pmConfigMaskWriteHeader (config, phu->header)) {
    503             psError(PS_ERR_UNKNOWN, false, "failed to set the bitmask names in the PHU header for Image %s (%s)\n", file->filename, file->name);
    504             return false;
    505         }
     591        assert (phu->header);
     592        if (!pmConfigMaskWriteHeader (config, phu->header)) {
     593            psError(PS_ERR_UNKNOWN, false, "failed to set the bitmask names in the PHU header for Image %s (%s)\n", file->filename, file->name);
     594            return false;
     595        }
    506596    }
    507597
  • trunk/psModules/src/camera/pmFPAfileFitsIO.h

    r18364 r18601  
    55 * @author PAP, IfA
    66 *
    7  * @version $Revision: 1.14 $ $Name: not supported by cvs2svn $
    8  * @date $Date: 2008-06-30 00:53:45 $
     7 * @version $Revision: 1.15 $ $Name: not supported by cvs2svn $
     8 * @date $Date: 2008-07-17 22:38:15 $
    99 * Copyright 2004-2005 Institute for Astronomy, University of Hawaii
    1010 */
     
    8686bool pmFPAviewWriteFitsTable(const pmFPAview *view, ///<  View specifying level of interest
    8787                             pmFPAfile *file, ///< FPA file to write
    88                              const char *name ///< Name of table
     88                             const char *name, ///< Name of table
     89                             pmConfig *config ///< Configuration
    8990                            );
    9091
     
    9798pmFPA *pmFPAfileSuitableFPA(const pmFPAfile *file,///< File containing the fpa
    9899                            const pmFPAview *view, ///< View at which to produce the fpa
    99                             const pmConfig *config ///< Configuration
     100                            pmConfig *config, ///< Configuration
     101                            bool pixels ///< Worry about copying the pixels?
    100102                           );
    101103
  • trunk/psModules/src/camera/pmFPAfileIO.c

    r18554 r18601  
    405405    }
    406406
    407     // check if the file is a FITS file (or uses the fits header)
    408     bool fitsType = false;
    409     fitsType |= (file->type == PM_FPA_FILE_IMAGE);
    410     fitsType |= (file->type == PM_FPA_FILE_MASK);
    411     fitsType |= (file->type == PM_FPA_FILE_WEIGHT);
    412     fitsType |= (file->type == PM_FPA_FILE_HEADER);
    413     fitsType |= (file->type == PM_FPA_FILE_FRINGE);
    414     fitsType |= (file->type == PM_FPA_FILE_DARK);
    415     fitsType |= (file->type == PM_FPA_FILE_CMP);
    416     fitsType |= (file->type == PM_FPA_FILE_CMF);
    417     fitsType |= (file->type == PM_FPA_FILE_PSF);
    418     fitsType |= (file->type == PM_FPA_FILE_ASTROM_MODEL);
    419     fitsType |= (file->type == PM_FPA_FILE_ASTROM_REFSTARS);
    420 
    421     // Ensure headers and all are updated
    422     // This is here so that the individual write functions (e.g., images, PSFs, sources, etc) don't have to
    423     // take care of all this themselves (because they generally don't).
    424     if (fitsType) {
    425         pmHDU *hdu = pmFPAviewThisHDU(view, file->fpa);
    426         if (hdu) {
    427             if (!hdu->header) {
    428                 hdu->header = psMetadataAlloc();
    429             }
    430             pmConfigConformHeader(hdu->header, file->format);
    431 
    432             // whenever we write out a mask image, we should define the bits which represent mask concepts
    433             if (file->type == PM_FPA_FILE_MASK) {
    434                 assert (hdu->header);
    435                 if (!pmConfigMaskWriteHeader (config, hdu->header)) {
    436                     psError(PS_ERR_UNKNOWN, false, "failed to set the bitmask names in the PHU header for Image %s (%s)\n", file->filename, file->name);
    437                     return false;
    438                 }
    439             }
    440         }
    441 
    442         pmFPA *fpa = file->fpa;         // FPA of interest
    443         pmChip *chip = pmFPAviewThisChip(view, file->fpa); // Chip of interest, or NULL
    444         pmCell *cell = pmFPAviewThisCell(view, file->fpa); // Cell of interest, or NULL
    445         if (!pmFPAUpdateNames(fpa, chip, cell)) {
    446             psError(PS_ERR_UNKNOWN, false, "Unable to update names in header.");
    447             return false;
    448         }
    449 
    450         pmConceptSource sources = PM_CONCEPT_SOURCE_HEADER | PM_CONCEPT_SOURCE_CELLS |
    451             PM_CONCEPT_SOURCE_DEFAULTS | PM_CONCEPT_SOURCE_DATABASE; // Concept sources to write
    452         if (cell) {
    453             if (!pmConceptsWriteCell(cell, sources, true, config)) {
    454                 psError(PS_ERR_IO, false, "Unable to write concepts for cell.\n");
    455                 return false;
    456             }
    457         } else if (chip) {
    458             if (!pmConceptsWriteChip(chip, sources, true, true, config)) {
    459                 psError(PS_ERR_IO, false, "Unable to write concepts for chip.\n");
    460                 return false;
    461             }
    462         } else if (!pmConceptsWriteFPA(fpa, sources, true, config)) {
    463             psError(PS_ERR_IO, false, "Unable to write concepts for FPA.\n");
    464             return false;
    465         }
    466     }
     407    // IMPORTANT: If adding a FITS-based file, make sure your write function uses an FPA produced by
     408    // pmFPAfileSuitableFPA.  This ensures the HDUs are at the correct level for your output format, and sets
     409    // the headers correctly.
    467410
    468411    // select a writing method
     
    487430        status = pmFPAviewWriteFitsImage (view, file, config);
    488431        if (status) {
    489             if (!pmFPAviewWriteFitsTable(view, file, "FRINGE")) {
     432            if (!pmFPAviewWriteFitsTable(view, file, "FRINGE", config)) {
    490433                psError(PS_ERR_UNKNOWN, false, "Unable to write fringe data from %s.\n", file->filename);
    491434                return false;
     
    802745          }
    803746
    804           // XXX if we have a mask file, then we need to read the mask bit names
    805           // defined for this file
    806           if (file->type == PM_FPA_FILE_MASK) {
    807             if (!pmConfigMaskReadHeader (config, phu)) {
    808                 psError(PS_ERR_IO, false, "error in mask bits");
    809                 return false;
    810             }
    811           }
     747          // XXX if we have a mask file, then we need to read the mask bit names
     748          // defined for this file
     749          if (file->type == PM_FPA_FILE_MASK) {
     750            if (!pmConfigMaskReadHeader (config, phu)) {
     751                psError(PS_ERR_IO, false, "error in mask bits");
     752                return false;
     753            }
     754          }
    812755
    813756          // determine the current format from the header
Note: See TracChangeset for help on using the changeset viewer.