/* @file  psFitsImage.h
 * @brief Contains Fits I/O routines
 *
 * @author Robert DeSonia, MHPCC
 *
 * @version $Revision: 1.8 $ $Name: not supported by cvs2svn $
 * @date $Date: 2007-11-16 01:04:56 $
 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
 */

#ifndef PS_FITSIMAGE_H
#define PS_FITSIMAGE_H

/// @addtogroup FileIO Input/Output
/// @{

#include "psFits.h"
#include "psType.h"
#include "psArray.h"
#include "psVector.h"
#include "psMetadata.h"
#include "psImage.h"

/// Return the dimensions and type of the FITS image
bool psFitsImageSize(int *numCols, int *numRows, ///< Size of image
                     psElemType *type,  ///< Type of image
                     const psFits *fits, ///< FITS file pointer
                     psRegion region    ///< Region in the FITS image to read
    );

/** Reads an image, given the desired region and z-plane.
 *
 *  @return psImage*     the read image or NULL if there was an error.
 */
psImage* psFitsReadImage(
    const psFits* fits,                ///< the psFits object
    psRegion region,                   ///< the region in the FITS image to read
    int z                              ///< the z-plane in the FITS image cube to read
);

// Read an image into an extant buffer
psImage* psFitsReadImageBuffer(psImage *output, // Output image buffer
                               const psFits *fits,    // the psFits object
                               psRegion region, // the region in the FITS image to read
                               int z           // the z-plane in the FITS image cube to read
                              );

/** Writes an image, given the desired region and z-plane.
 *
 *  @return bool        TRUE is the write was successful, otherwise FALSE.
 */
bool psFitsWriteImage(
    psFits* fits,                      ///< the psFits object
    psMetadata* header,                 ///< header items for the new HDU.  Can be NULL.
    const psImage* input,              ///< the image to output
    int depth,                         ///< the number of z-planes of the FITS image data cube
    const char* extname                ///< FITS extension name
);

/** Writes an image, given the desired region and z-plane.  A new IMAGE HDU is
 *  appended to the end of the FITS file.
 *
 *  @return bool        TRUE is the write was successful, otherwise FALSE.
 */
bool psFitsInsertImage(
    psFits* fits,                      ///< the psFits object
    psMetadata* header,                 ///< header items for the new HDU.  Can be NULL.
    const psImage* input,              ///< the image to output
    int depth,                         ///< the number of z-planes of the FITS image data cube
    const char* extname,               ///< FITS extension name
    bool after                         ///< if TRUE, inserts HDU after current HDU, otherwise before
);

/** Updates the FITS file image, given the desired region and z-plane. a new
 *  IMAGE HDU is inserted before or after, depending on the AFTER parameter,
 *  the current HDU.
 *
 *  @return bool        TRUE is the write was successful, otherwise FALSE.
 */
bool psFitsUpdateImage(
    psFits* fits,                      ///< the psFits object
    const psImage* input,              ///< the image to output
    int x0,                            ///< psImage's x-axis origin in FITS image coordinates
    int y0,                            ///< psImage's y-axis origin in FITS image coordinates
    int z                              ///< the z-planes of the FITS image data cube to write
);

psArray *psFitsReadImageCube(const psFits *fits, psRegion region);
bool psFitsWriteImageCube(psFits *fits, psMetadata *header, const psArray *input, const char *extname);
bool psFitsUpdateImageCube(psFits *fits, const psArray *input, int x0, int y0);

/// @}
#endif // #ifndef PS_FITS_H
