/** @file  psFits.c
 *
 *  @brief Contains Fits I/O routines
 *
 *  @ingroup FileIO
 *
 *  @author Robert DeSonia, MHPCC
 *
 *  @version $Revision: 1.71.2.5 $ $Name: not supported by cvs2svn $
 *  @date $Date: 2007-10-03 20:57:13 $
 *
 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <unistd.h>

#include "psFits.h"
#include "psFitsHeader.h"
#include "string.h"
#include "psError.h"
#include "psAssert.h"
#include "psImageStructManip.h"
#include "psMemory.h"
#include "psString.h"
#include "psLogMsg.h"
#include "psTrace.h"
#include "psVector.h"
#include "psAbort.h"

#define MAX_STRING_LENGTH 256  // maximum length string for FITS routines
static char *defaultExtword = "EXTNAME";

static bool isHDUEmpty(const psFits* fits)
{
    /* check for keys - no keys means this is really an empty HDU */
    int keysexist = -1;
    int morekeys;
    int status = 0;

    fits_get_hdrspace(fits->fd, &keysexist, &morekeys, &status);

    // if no keys exist and not primary HDU, this really is an empty HDU
    if (keysexist == 0) {
        return true;
    }

    return false;

}

static bool fitsClose(psFits* fits)
{
    int status = 0;

    if (fits != NULL) {
        if (fits_close_file(fits->fd, &status)) {
            char fitsErr[MAX_STRING_LENGTH];
            fits_get_errstatus(status, fitsErr);
            psError(PS_ERR_BAD_FITS, true,
                    "Error while closing psFits object. CFITSIO error: %s",
                    fitsErr);
            return false;
        }
        fits->fd = NULL;
    }
    return true;
}

static void fitsFree(psFits* fits)
{
    if (!fits) return;
    if (fits->fd) {
        fitsClose(fits);
    }
    psFree (fits->extword);
}

bool psFitsClose(psFits* fits)
{
    PS_ASSERT_FITS_NON_NULL(fits, false);

    bool status = fitsClose(fits);
    psFree(fits);

    return status;
}

psFits* psFitsOpen(const char* name, const char* mode)
{
    PS_ASSERT_STRING_NON_EMPTY(name, NULL);

    int status = 0;
    fitsfile *fptr = NULL;      /* Pointer to the FITS file */

    /* check the mode to determine how to open/create file */
    int iomode;
    bool newFile;
    if (strcmp(mode,"r") == 0) {
        iomode = READONLY;
        newFile = false;
    } else if (strcmp(mode,"rw") == 0 || strcmp(mode,"r+") == 0) {
        iomode = READWRITE;
        newFile = false;
    } else if (strcmp(mode,"w") == 0 || strcmp(mode,"w+") == 0) {
        iomode = READWRITE;
        newFile = true;
    } else if (strcmp(mode,"a") == 0|| strcmp(mode,"a+") == 0) {
        iomode = READWRITE;
        newFile = (access(name, F_OK) != 0);
    } else {
        // mode is not valid
        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
                "Specified mode, '%s', is invalid.  Supported modes are r, r+, rw, w, w+, a, or a+.",
                mode);
        return NULL;
    }

    if (newFile) {
        /* Check if an existing file is in the way before creating file*/
        if (access(name, F_OK) == 0) {
            // file exists, delete old one first
            remove(name);
        }

        #if ( CFITSIO_DISKFILE == 1 )
        (void)fits_create_diskfile
        #else
        (void)fits_create_file
        #endif
        (&fptr, name, &status);
        if (fptr == NULL || status != 0) {
            char fitsErr[MAX_STRING_LENGTH];
            fits_get_errstatus(status, fitsErr);
            psError(PS_ERR_IO, true,
                    _("Could not create file,'%s'. CFITSIO Error: %s"),
                    name, fitsErr);
            return NULL;
        }
    } else {
        #if ( CFITSIO_DISKFILE == 1 )
        (void)fits_open_diskfile
        #else
        (void)fits_open_file
        #endif
        (&fptr, name, iomode, &status);
        if (fptr == NULL || status != 0) {
            char fitsErr[MAX_STRING_LENGTH];
            fits_get_errstatus(status, fitsErr);
            psError(PS_ERR_IO, true,
                    _("Could not open file,'%s'. CFITSIO Error: %s"),
                    name, fitsErr);
            return NULL;
        }
    }

    psFits* fits = psAlloc(sizeof(psFits));
    fits->fd = fptr;
    fits->writable = (iomode == READWRITE);
    fits->extword = NULL;
    fits->conventions.compression = true;
    psMemSetDeallocator(fits,(psFreeFunc)fitsFree);

    return fits;
}

psErrorCode p_psFitsError(const char* filename, unsigned int lineno, const char* func, int status,
                          bool new, const char *errorMsg, ...)
{
    if (status == 0) {
        return PS_ERR_NONE;
    }

    va_list ap;                         // Variable arguments
    va_start(ap, errorMsg);
    psString msg = NULL;                // Message to pass to psError
    psStringAppendV(&msg, errorMsg, ap);
    va_end(ap);

    char cfitsioMsg[MAX_STRING_LENGTH];   // Error message from cfitsio
    (void)fits_get_errstatus(status, cfitsioMsg);

    psStringAppend(&msg, "[CFITSIO error: %s]", cfitsioMsg);

    psErrorCode code = p_psError(filename, lineno, func, PS_ERR_IO, new, msg); // Error code
    psFree(msg);
    return code;
}

static void psFitsCompressionFree(psFitsCompression *comp)
{
    PS_ASSERT_PTR_NON_NULL(comp,);
    psFree(comp->tilesize);
}

psFitsCompression* psFitsCompressionAlloc(
    psFitsCompressionType type,         ///< type of compression
    psVector *tilesize,                 ///< vector defining compression tile size
    int noisebits,                      ///< noise bits
    int scale,                          ///< hcompress scale
    int smooth                          ///< hcompress smothing
)
{
    psFitsCompression *comp = psAlloc(sizeof(psFitsCompression));
    psMemSetDeallocator(comp, (psFreeFunc) psFitsCompressionFree);

    comp->type = type;
    comp->tilesize = psVectorCopy(NULL, tilesize, PS_DATA_S64);
    comp->noisebits = noisebits;
    comp->scale = scale;
    comp->smooth = smooth;

    return comp;
}

bool psMemCheckFits(psPtr ptr)
{
    PS_ASSERT_PTR(ptr, false);
    return ( psMemGetDeallocator(ptr) == (psFreeFunc)fitsFree );
}

bool psFitsSetExtnameWord (psFits *fits, const char *extword)
{
    PS_ASSERT_PTR_NON_NULL(fits,    false);
    PS_ASSERT_PTR_NON_NULL(extword, false);

    psFree (fits->extword);
    fits->extword = psStringCopy (extword);
    return true;
}

// Files compressed with cfitsio's "imcopy" program may have multiple EXTNAME keywords, with the first set to
// COMPRESSED_IMAGE.  However, fits_movnam_hdu won't find the second (proper) value of EXTNAME, and so can
// fail to find a perfectly legitimate extension, simply because imcopy does something silly.  However, we
// really want to be able to read these files (MegaCam data are shipped as imcopy-compressed images).
// Therefore, we implement our own version of moving to an extension specified by name.  The pure cfitsio
// version is used if "conventions.compression" handling is turned off in the psFits structure.
bool psFitsMoveExtName(const psFits* fits,
                       const char* extname)
{
    PS_ASSERT_FITS_NON_NULL(fits, false);
    PS_ASSERT_STRING_NON_EMPTY(extname, false);

    int status = 0;

    if (!fits->conventions.compression && !fits->extword) {
        // User wants to use cfitsio.  Good luck to them!
        if (fits_movnam_hdu(fits->fd, ANY_HDU, (char*)extname, 0, &status) != 0) {
            psFitsError(status, true, _("Could not find HDU '%s'"), extname);
            return false;
        }
        return true;
    }

    bool ignoreCI = (fits->conventions.compression &&
                     (strcmp(extname, "COMPRESSED_IMAGE") != 0)); // Ignore COMPRESSED_IMAGE extension name?
    char *extword = (fits->extword ? fits->extword : "EXTNAME"); // Word to use as extension name

#if 0
    // XXX Future optimisation: loop through from the current HDU to the end, then from the start to the
    // current position.  This will save seeking through the file multiple times.
    int currentExt = psFitsGetExtNum(fits); // Current extension number
    int numExt = psFitsGetSize(fits);   // Total number of extensions
#endif

    for (int i = 1; true; i++) {
        int hdutype = 0;
        if (fits_movabs_hdu(fits->fd, i, &hdutype, &status)) {
            // We've run off the end
            psFitsError(status, true, _("Could not find HDU with %s = '%s'"), extword, extname);
            return false;
        }
        // Is there a keyword called 'extword'? (read as string regardless of type)
        char name[MAX_STRING_LENGTH];  // Name of extension
        if (fits_read_keyword(fits->fd, extword, name, NULL, &status)) {
            // It doesn't exist in the header.
            // This isn't the extension you're looking for.  Move along.
            status = 0;
            continue;
        }
        char *fixed = p_psFitsHeaderParseString(name); // Parsed version (removing quotes and spaces)

        if (ignoreCI && strcmp(fixed, "COMPRESSED_IMAGE") == 0) {
            // Read it again, Sam
            if (fits_read_keyword(fits->fd, extword, name, NULL, &status)) {
                status = 0;
                continue;
            }
            fixed = p_psFitsHeaderParseString(name);
        }

        if (strcmp(fixed, extname) == 0) {
            // We've arrived
            return true;
        }
    }
    psAbort("Should never reach here.");
}

bool psFitsMoveExtNum(const psFits* fits,
                      int extnum,
                      bool relative)
{
    PS_ASSERT_FITS_NON_NULL(fits, false);

    int status = 0;
    int hdutype = 0;

    if (relative) {
        fits_movrel_hdu(fits->fd, extnum, &hdutype, &status);
        if (status != 0) {
            char fitsErr[MAX_STRING_LENGTH];
            fits_get_errstatus(status, fitsErr);
            psError(PS_ERR_LOCATION_INVALID, true,
                    _("Could not move %d HDUs from current position. CFITSIO Error: %s"),
                    extnum, fitsErr);
            return false;
        }
    } else {
        fits_movabs_hdu(fits->fd, extnum+1, &hdutype, &status);
        if (status != 0) {
            char fitsErr[MAX_STRING_LENGTH];
            fits_get_errstatus(status, fitsErr);
            psError(PS_ERR_LOCATION_INVALID, true,
                    _("Could not move to specified HDU #%d. CFITSIO Error: %s"),
                    extnum, fitsErr);
            return false;
        }
    }

    return true;
}

bool psFitsMoveLast(psFits* fits)
{
    PS_ASSERT_FITS_NON_NULL(fits, false);

    int size = psFitsGetSize(fits);
    if (size == 0) { // empty file -- no action needed
        return true;
    } else {
        return psFitsMoveExtNum(fits,size-1,false);
    }
}

int psFitsGetExtNum(const psFits* fits)
{
    PS_ASSERT_FITS_NON_NULL(fits, false);
    int hdunum;
    return fits_get_hdu_num(fits->fd,&hdunum) - 1;
}

psString psFitsGetExtName(const psFits* fits)
{
    PS_ASSERT_FITS_NON_NULL(fits, NULL);

    int status = 0;
    char name[MAX_STRING_LENGTH];

    char *extword = (fits->extword == NULL) ? defaultExtword : fits->extword;

    if (fits_read_key_str(fits->fd, extword, name, NULL, &status) != 0) {
        psError(PS_ERR_BAD_PARAMETER_NULL, true,
                _("Header keyword %s is not found"), extword);
        return NULL;
    }
    return psStringCopy(name);
}

bool psFitsSetExtName(psFits* fits, const char* name)
{
    PS_ASSERT_FITS_NON_NULL(fits, false);
    PS_ASSERT_STRING_NON_EMPTY(name, false);

    int status = 0;

    char *extword = (fits->extword == NULL) ? defaultExtword : fits->extword;

    if (fits_update_key_str(fits->fd, extword, (char*)name, NULL, &status) != 0) {
        char fitsErr[MAX_STRING_LENGTH];
        (void)fits_get_errstatus(status, fitsErr);
        psError(PS_ERR_IO, true,
                _("Could not write data to file. CFITSIO Error: %s"),
                fitsErr);
        return false;
    }

    return true;
}

bool psFitsDeleteExtNum(psFits* fits,
                        int extnum,
                        bool relative)
{
    PS_ASSERT_FITS_NON_NULL(fits, false);

    if (! fits->writable) {
        psError(PS_ERR_IO, true,
                _("The psFits object is not writable."));
        return false;
    }

    // move to the specified HDU
    if (! psFitsMoveExtNum(fits,extnum,relative) ) {
        psError(PS_ERR_BAD_PARAMETER_VALUE, false,
                "Failed to delete HDU #%d",
                extnum);
        return false;
    }

    int status = 0;

    // OK, now let's delete the HDU
    if (fits_delete_hdu(fits->fd, NULL, &status) != 0) {
        char fitsErr[MAX_STRING_LENGTH];
        (void)fits_get_errstatus(status, fitsErr);
        psError(PS_ERR_IO, true,
                _("Could not write data to file. CFITSIO Error: %s"),
                fitsErr);
        return false;
    }

    return true;
}

bool psFitsDeleteExtName(psFits* fits,
                         const char* extname)
{
    PS_ASSERT_FITS_NON_NULL(fits, false);
    PS_ASSERT_STRING_NON_EMPTY(extname, false);

    if (! fits->writable) {
        psError(PS_ERR_IO, true,
                _("The psFits object is not writable."));
        return false;
    }

    // move to the specified HDU
    if (! psFitsMoveExtName(fits,extname) ) {
        psError(PS_ERR_BAD_PARAMETER_VALUE, false,
                "Failed to delete HDU with the name '%s'",
                extname);
        return false;
    }


    int status = 0;

    // OK, now let's delete the HDU
    if (fits_delete_hdu(fits->fd, NULL, &status) != 0) {
        char fitsErr[MAX_STRING_LENGTH];
        (void)fits_get_errstatus(status, fitsErr);
        psError(PS_ERR_IO, true,
                _("Could not write data to file. CFITSIO Error: %s"),
                fitsErr);
        return false;
    }

    return true;
}

int psFitsGetSize(const psFits* fits)
{
    PS_ASSERT_FITS_NON_NULL(fits, 0);

    int num = 0;
    int status = 0;

    if (fits_get_num_hdus(fits->fd, &num, &status) != 0) {
        char fitsErr[MAX_STRING_LENGTH];
        fits_get_errstatus(status, fitsErr);
        psError(PS_ERR_LOCATION_INVALID, true,
                _("Failed to determine the number of HDUs. CFITSIO Error: %s"),
                fitsErr);
        return 0;
    }

    return num;
}

psFitsType psFitsGetExtType(const psFits* fits)
{
    PS_ASSERT_FITS_NON_NULL(fits, PS_FITS_TYPE_NONE);

    int status = 0;
    int hdutype = PS_FITS_TYPE_NONE;

    if (fits_get_hdu_type(fits->fd, &hdutype, &status) != 0) {
        char fitsErr[MAX_STRING_LENGTH];
        fits_get_errstatus(status, fitsErr);
        psError(PS_ERR_LOCATION_INVALID, true,
                _("Failed to determine an HDU type. CFITSIO Error: %s"),
                fitsErr);
        return PS_FITS_TYPE_NONE;
    }

    if (hdutype == PS_FITS_TYPE_IMAGE &&
            psFitsGetExtNum(fits) > 0 &&
            isHDUEmpty(fits)) {
        return PS_FITS_TYPE_ANY;
    }

    return hdutype;
}

bool psFitsTruncate(psFits* fits)
{
    PS_ASSERT_FITS_NON_NULL(fits, NULL);

    if (! fits->writable) {
        psError(PS_ERR_IO, true,
                _("The psFits object is not writable."));
        return PS_FITS_TYPE_NONE;
    }

    int newEnd = psFitsGetExtNum(fits);

    psFitsMoveLast(fits);
    int end = psFitsGetExtNum(fits);

    // delete HDUs from end to beginning position + 1;
    for (int lcv=end;lcv > newEnd; lcv--) {
        if (! psFitsDeleteExtNum(fits,lcv,false)) {
            // failed to delete an HDU!?
            psError(PS_ERR_UNKNOWN, false,
                    "Failed to truncate file.  HDU #%d out of %d could not be deleted.",
                    lcv,end);
            return false;
        }
    }

    return true;
}


bool psFitsSetCompression(
    psFits* fits,                       ///< psFits object to close
    psFitsCompressionType type,         ///< type of compression
    psVector *tilesize,
    int noisebits,                      ///< noise bits
    int scale,                          ///< hcompress scale
    int smooth                          ///< hcompress smothing
)
{
    PS_ASSERT_FITS_NON_NULL(fits, false);

    // convert psFitsCompressionType to cfitsio compression types
    int comptype;
    switch (type) {
        case PS_FITS_COMPRESS_NONE:
            comptype = 0x0;
            break;
        case PS_FITS_COMPRESS_GZIP:
            comptype = GZIP_1;
            break;
        case PS_FITS_COMPRESS_RICE:
            comptype = RICE_1;
            break;
        case PS_FITS_COMPRESS_HCOMPRESS:
            comptype = HCOMPRESS_1;
            break;
        case PS_FITS_COMPRESS_PLIO:
            comptype = PLIO_1;
            break;
        default:
            psError(PS_ERR_UNKNOWN, true, "invalid psFitsCompressionType");
            return false;
    }

    int status = 0;
    if (fits_set_compression_type(fits->fd, comptype, &status)) {
        char fitsErr[MAX_STRING_LENGTH];
        fits_get_errstatus(status, fitsErr);
        psError(PS_ERR_BAD_FITS, true,
            "Error while configuring compression. CFITSIO error: %s", fitsErr);
        return false;
    }

    // convert a psVector into the (long *) array that cfitsio requires
    psVector *dim = NULL;
    if (sizeof(long) == sizeof(psS64)) {
        dim = psVectorCopy(NULL, tilesize, PS_DATA_S64);
        fits_set_tile_dim(fits->fd, psVectorLength(dim), (long *)dim->data.S64, &status);
    } else if (sizeof(long) == sizeof(psS32)) {
        dim = psVectorCopy(NULL, tilesize, PS_DATA_S32);
        fits_set_tile_dim(fits->fd, psVectorLength(dim), (long *)dim->data.S32, &status);
    } else {
        psAbort("can't map (long) type to a psLib type");
    }
    psFree(dim);
    // status check belongs to fits_set_tile_dim() call
    if (status) {
        fits_set_compression_type(fits->fd, 0x0, &status);
        char fitsErr[MAX_STRING_LENGTH];
        fits_get_errstatus(status, fitsErr);
        psError(PS_ERR_BAD_FITS, true,
            "Error while configuring compression. CFITSIO error: %s", fitsErr);
        return false;
    }

    if (fits_set_noise_bits(fits->fd, noisebits, &status)) {
        fits_set_compression_type(fits->fd, 0x0, &status);
        char fitsErr[MAX_STRING_LENGTH];
        fits_get_errstatus(status, fitsErr);
        psError(PS_ERR_BAD_FITS, true,
            "Error while configuring compression. CFITSIO error: %s", fitsErr);
        return false;
    }

#if FITS_HCOMP
    if (fits_set_hcomp_scale(fits->fd, scale, &status)) {
        fits_set_compression_type(fits->fd, 0x0, &status);
        char fitsErr[MAX_STRING_LENGTH];
        fits_get_errstatus(status, fitsErr);
        psError(PS_ERR_BAD_FITS, true,
            "Error while configuring compression. CFITSIO error: %s", fitsErr);
        return false;
    }
    if (fits_set_hcomp_smooth(fits->fd, smooth, &status)) {
        fits_set_compression_type(fits->fd, 0x0, &status);
        char fitsErr[MAX_STRING_LENGTH];
        fits_get_errstatus(status, fitsErr);
        psError(PS_ERR_BAD_FITS, true,
            "Error while configuring compression. CFITSIO error: %s", fitsErr);
        return false;
    }
#endif // FITS_HCOMP

    return true;
}

psFitsCompression *psFitsCompressionGet(psFits* fits)
{
    PS_ASSERT_FITS_NON_NULL(fits, NULL);

    int status = 0;                     // cfitsio status

    psFitsCompressionType type = psFitsCompressionGetType(fits);
    if (type < 0) {
        psError(PS_ERR_UNKNOWN, false, "Unable to get compression type.");
        return NULL;
    }

    psElemType tileType;                // Type corresponding to "long"
    if (sizeof(long) == sizeof(psS64)) {
        tileType = PS_TYPE_S64;
    } else if (sizeof(long) == sizeof(psS32)) {
        tileType = PS_TYPE_S32;
    } else {
        psAbort("can't map (long) type to a psLib type");
    }

    psVector *tiles = psVectorAlloc(3, tileType); // Tile sizes
    if (fits_get_tile_dim(fits->fd, 3, (long*)tiles->data.U8, &status)) {
        psFitsError(status, true, "Unable to get compression tile sizes.");
        psFree(tiles);
        return NULL;
    }

    int noisebits;                      // Noise bits for compression
    if (fits_get_noise_bits(fits->fd, &noisebits, &status)) {
        psFitsError(status, true, "Unable to get compression noise bits.");
        psFree(tiles);
        return NULL;
    }

    int hscale = 0, hsmooth = 0;        // Scaling and smoothing for HCOMPRESS

#if FITS_HCOMP
    if (fits_get_hcomp_scale(fits->fd, &hscale, &status)) {
        psFitsError(status, true, "Unable to get HCOMPRESS scaling.");
        psFree(tiles);
        return NULL;
    }
    if (fits_get_hcomp_smooth(fits->fd, &hsmooth, &status)) {
        psFitsError(status, true, "Unable to get HCOMPRESS smoothing.");
        psFree(tiles);
        return NULL;
    }
#endif // FITS_HCOMP

    psFitsCompression *compress = psFitsCompressionAlloc(type, tiles, noisebits, hscale, hsmooth);
    psFree(tiles);                      // Drop reference

    return compress;
}

psFitsCompressionType psFitsCompressionGetType(psFits* fits)
{
    PS_ASSERT_FITS_NON_NULL(fits, -1);

    int status = 0;                     // cfitsio status
    int comptype = 0;                   // cfitsio compression type
    if (fits_get_compression_type(fits->fd, &comptype, &status)) {
        psFitsError(status, true, "Unable to get compression type.");
        return -1;
    }

    psFitsCompressionType type;
    switch (comptype) {
      case 0:
        type = PS_FITS_COMPRESS_NONE;
        break;
      case GZIP_1:
        type = PS_FITS_COMPRESS_GZIP;
        break;
      case RICE_1:
        type = PS_FITS_COMPRESS_RICE;
        break;
      case HCOMPRESS_1:
        type = PS_FITS_COMPRESS_HCOMPRESS;
        break;
      case PLIO_1:
        type = PS_FITS_COMPRESS_PLIO;
        break;
      default:
        psError(PS_ERR_UNKNOWN, true, "cfitsio reports unknown compression type.");
        return -1;
    }

    return type;
}


bool psFitsCompressionApply(
    psFits* fits,                       ///< psFits object to close
    psFitsCompression *comp             ///< options object
)
{
    return psFitsSetCompression(fits, comp->type, comp->tilesize, comp->noisebits, comp->scale, comp->smooth);
}


psDataType p_psFitsTypeFromCfitsio(int datatype)
{
    switch (datatype) {
    case TBYTE:
        return PS_TYPE_U8;
    case TSBYTE:
        return PS_TYPE_S8;
    case TSHORT:
        return PS_TYPE_S16;
    case TUSHORT:
        return PS_TYPE_U16;
    case TLONG:
        if (sizeof(long) == 8) {
            return PS_TYPE_S64;
        }
        // no break
    case TINT:
        return PS_TYPE_S32;
    case TULONG:
        if (sizeof(unsigned long) == 8) {
            return PS_TYPE_U64;
        }
        // no break
    case TUINT:
        return PS_TYPE_U32;
    case TLONGLONG:
        return PS_TYPE_S64;
    case TFLOAT:
        return PS_TYPE_F32;
    case TDOUBLE:
        return PS_TYPE_F64;
    case TLOGICAL:
        return PS_TYPE_BOOL;
    default:
        psError(PS_ERR_IO, true,
                "Unknown FITS datatype, %d.",
                datatype);
        return 0;
    }
}

bool p_psFitsTypeToCfitsio(psDataType type, int* bitPix, double* bZero, int* dataType)
{
    int bitpix;
    int datatype;
    double bzero = 0.0;

    switch (type) {

    case PS_TYPE_U8:
        bitpix = BYTE_IMG;
        datatype = TBYTE;
        break;

    case PS_TYPE_S8:
        bitpix = BYTE_IMG;
        bzero = INT8_MIN;
        datatype = TSBYTE;
        break;

    case PS_TYPE_U16:
        bitpix = SHORT_IMG;
        bzero = -1.0 * INT16_MIN;
        datatype = TUSHORT;
        break;

    case PS_TYPE_S16:
        bitpix = SHORT_IMG;
        datatype = TSHORT;
        break;

    case PS_TYPE_U32:
        bitpix = LONG_IMG;
        bzero = -1.0 * INT32_MIN;
        datatype = TUINT;
        break;

    case PS_TYPE_S32:
        bitpix = LONG_IMG;
        datatype = TINT;
        break;

    case PS_TYPE_F32:
        bitpix = FLOAT_IMG;
        datatype = TFLOAT;
        break;

    case PS_TYPE_F64:
        bitpix = DOUBLE_IMG;
        datatype = TDOUBLE;
        break;

    case PS_DATA_STRING:
        bitpix = BYTE_IMG;
        datatype = TSTRING;
        break;

    case PS_DATA_BOOL:
        bitpix = BYTE_IMG;
        datatype = TLOGICAL;
        break;

    default: {
            char* typeStr;
            PS_TYPE_NAME(typeStr,type);
            psError(PS_ERR_BAD_PARAMETER_TYPE, true,
                    _("Specified type, %s, is not supported."),
                    typeStr);
            return false;
        }
    }

    // pass back the requested parameters  (NULL parameters are not set, of course).
    if (bitPix != NULL) {
        *bitPix = bitpix;
    }

    if (dataType != NULL) {
        *dataType = datatype;
    }

    if (bZero != NULL) {
        *bZero = bzero;
    }

    return true;
}


psFitsCompressionType psFitsCompressionTypeFromString(const char *string)
{
    if (!string || strlen(string) == 0) {
        psWarning("Unable to identify compression type --- none set.");
        return PS_FITS_COMPRESS_NONE;
    }

    if (strcmp(string, "GZIP") == 0) return PS_FITS_COMPRESS_GZIP;
    if (strcmp(string, "RICE") == 0) return PS_FITS_COMPRESS_RICE;
    if (strcmp(string, "HCOMPRESS") == 0) return PS_FITS_COMPRESS_HCOMPRESS;
    if (strcmp(string, "PLIO") == 0) return PS_FITS_COMPRESS_PLIO;

    psWarning("Unable to identify compression type (%s) --- none set.", string);
    return PS_FITS_COMPRESS_NONE;
}
