IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 1970


Ignore:
Timestamp:
Oct 5, 2004, 3:06:27 PM (22 years ago)
Author:
harman
Message:

Changes for metadata IO

Location:
trunk/psLib/src
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/astronomy/psMetadata.c

    r1844 r1970  
    33*
    44*
    5 *  @brief Contains metadata struuctures, enumerations and functions prototypes
     5*  @brief Contains metadata structures, enumerations and functions prototypes.
    66*
    77*  This file defines metadata item, metadata type, metadata flags, metadata containers, and function
     
    1212*  @author Ross Harman, MHPCC
    1313*
    14 *  @version $Revision: 1.28 $ $Name: not supported by cvs2svn $
    15 *  @date $Date: 2004-09-21 23:27:55 $
     14*  @version $Revision: 1.29 $ $Name: not supported by cvs2svn $
     15*  @date $Date: 2004-10-06 01:06:20 $
    1616*
    1717*  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    3232#include "psList.h"
    3333#include "psHash.h"
     34#include "psVector.h"
    3435#include "psMetadata.h"
    3536#include "psString.h"
     
    3940/******************************************************************************/
    4041
    41 #define FITS_ERROR(STRING,PS_ERROR)                                                                          \
    42 fits_get_errstatus(status, fitsErr);                                                                     \
    43 psError(__func__, STRING, PS_ERROR, fitsErr);                                                            \
    44 status = 0;                                                                                              \
    45 fits_close_file(fd, &status);                                                                            \
    46 if(status){                                                                                              \
    47     fits_get_errstatus(status, fitsErr);                                                                  \
    48     psError(__func__, "Couldn't close FITS file. FITS error: %s", fitsErr);                               \
    49 }                                                                                                        \
    50 status = 0;                                                                                              \
    51 return output;
    52 
    53 /** Maximum size of a FITS line */
    54 #define FITS_LINE_SIZE 80
    55 
    5642/** Maximum size of a string */
    5743#define MAX_STRING_LENGTH 1024
     
    9379    psFree(metadataItem->items);
    9480
    95     if (type == PS_META_STR ||
    96             type == PS_META_IMG ||
    97             type == PS_META_JPEG || type == PS_META_PNG || type == PS_META_ASTROM || type == PS_META_UNKNOWN) {
     81    if (type == PS_META_STR || type == PS_META_VEC || type == PS_META_IMG || type == PS_META_JPEG ||
     82            type == PS_META_PNG || type == PS_META_ASTROM || type == PS_META_UNKNOWN) {
    9883        psFree(metadataItem->data.V);
    9984    }
     
    133118                                     va_list argPtr)
    134119{
     120    psVector *vecIn = NULL;
     121    psVector *vecOut = NULL;
    135122    psMetadataItem* metadataItem = NULL;
     123
    136124
    137125    if (name == NULL) {
     
    182170        metadataItem->data.V = psStringNCopy(va_arg(argPtr, char *), MAX_STRING_LENGTH);
    183171        break;
     172    case PS_META_VEC:
     173        vecIn = (psVector*)va_arg(argPtr, psVector*);
     174        vecOut = psVectorAlloc(vecIn->nalloc, vecIn->type.type);
     175        memcpy(vecOut->data.V, vecIn->data.V, vecIn->nalloc*PSELEMTYPE_SIZEOF(vecIn->type.type));
     176        metadataItem->data.V = vecOut;
     177        break;
    184178    case PS_META_IMG:
    185179    case PS_META_JPEG:
     
    373367    return true;
    374368}
    375 
    376 
    377369
    378370bool psMetadataRemove(psMetadata* restrict md, int where, const char *restrict key)
     
    603595    return entry;
    604596}
    605 
    606 void psMetadataItemPrint(FILE * fd, const char *format, const psMetadataItem* restrict metadataItem)
    607 {
    608     psMetadataType type;
    609 
    610     if (fd == NULL) {
    611         psError(__func__, "Null file descriptor not allowed");
    612         return;
    613     }
    614 
    615     if (format == NULL) {
    616         psError(__func__, "Null format not allowed");
    617         return;
    618     }
    619 
    620     if (metadataItem == NULL) {
    621         psError(__func__, "Null metadata not allowed");
    622         return;
    623     }
    624 
    625     type = metadataItem->type;
    626 
    627     switch (type) {
    628     case PS_META_BOOL:
    629         fprintf(fd, format, metadataItem->data.B);
    630         break;
    631     case PS_META_S32:
    632         fprintf(fd,format, metadataItem->data.S32);
    633         break;
    634     case PS_META_F32:
    635         fprintf(fd, format, metadataItem->data.F32);
    636         break;
    637     case PS_META_F64:
    638         fprintf(fd, format, metadataItem->data.F64);
    639         break;
    640     case PS_META_STR:
    641         fprintf(fd, format, metadataItem->data.V);
    642         break;
    643     case PS_META_ITEM_SET:
    644     case PS_META_IMG:
    645     case PS_META_JPEG:
    646     case PS_META_PNG:
    647     case PS_META_ASTROM:
    648     case PS_META_UNKNOWN:
    649     default:
    650         psError(__func__, " Invalid psMetadataType to print: %d", type);
    651     }
    652 }
    653 
    654 psMetadata* psMetadataReadHeader(psMetadata* output, char *extName, int extNum, char *fileName)
    655 {
    656     bool tempBool;
    657     bool success;
    658     char keyType;
    659     char keyName[FITS_LINE_SIZE];
    660     char keyValue[FITS_LINE_SIZE];
    661     char keyComment[FITS_LINE_SIZE];
    662     char fitsErr[MAX_STRING_LENGTH];
    663     int i;
    664     int hduType = 0;
    665     int status = 0;
    666     int numKeys = 0;
    667     int keyNum = 0;
    668     psMetadataType metadataItemType;
    669     fitsfile *fd = NULL;
    670 
    671     if(fileName == NULL) {
    672         psError(__func__, "Null fileName not allowed");
    673         return NULL;
    674     }
    675 
    676     fits_open_file(&fd, fileName, READONLY, &status);
    677     if(fd == NULL || status != 0) {
    678         FITS_ERROR("FITS error while opening file: %s %s", fileName);
    679         return NULL;
    680     }
    681 
    682     if (extName == NULL && extNum == 0) {
    683         psError(__func__, "Null extName and extNum = 0 not allowed");
    684         return NULL;
    685     } else if (extName && extNum) {
    686         psError(__func__, "Both extName and extNum arguments should not have non zero values.");
    687         return NULL;
    688     }
    689 
    690     // Allocate metadata if user didn't
    691     if (output == NULL) {
    692         output = psMetadataAlloc();
    693     }
    694 
    695     // Move to user designated HDU number or HDU name in FITS file. HDU numbers starts at one.
    696     if (extName != NULL) {
    697         if (fits_movnam_hdu(fd, ANY_HDU, extName, 0, &status) != 0) {
    698             FITS_ERROR("FITS error while locating header %s: %s", extName);
    699         }
    700     } else {
    701         if (fits_movabs_hdu(fd, extNum, &hduType, &status) != 0) {
    702             FITS_ERROR("FITS error while locating header %d: %s", extNum);
    703         }
    704     }
    705 
    706     // Get number of key names
    707     if (fits_get_hdrpos(fd, &numKeys, &keyNum, &status) != 0) {
    708         FITS_ERROR("FITS error while reading key %d: %s", keyNum);
    709     }
    710 
    711     // Get each key name. Keywords start at one.
    712     for (i = 1; i <= numKeys; i++) {
    713         if (fits_read_keyn(fd, i, keyName, keyValue, keyComment, &status) != 0) {
    714             FITS_ERROR("FITS error while reading key %d: %s", keyNum);
    715         }
    716         if (fits_get_keytype(keyValue, &keyType, &status) != 0) {
    717             fits_get_errstatus(status, fitsErr);
    718             if (status != VALUE_UNDEFINED) {
    719                 FITS_ERROR("FITS error while determining key %d type: %s", keyNum);
    720             } else {
    721                 // Some keywords are still valid if they don't have a type (like COMMENTS and HISTORY)
    722                 keyType = 'C';
    723                 status = 0;
    724             }
    725         }
    726 
    727         switch (keyType) {
    728         case 'I':
    729             metadataItemType = PS_META_S32;
    730             success =
    731                 psMetadataAdd(output, PS_LIST_TAIL, keyName, metadataItemType, keyComment,
    732                               atoi(keyValue));
    733             break;
    734         case 'F':
    735             metadataItemType = PS_META_F64;
    736             success =
    737                 psMetadataAdd(output, PS_LIST_TAIL, keyName, metadataItemType, keyComment,
    738                               atof(keyValue));
    739             break;
    740         case 'C':
    741             metadataItemType = PS_META_STR;
    742             success =
    743                 psMetadataAdd(output, PS_LIST_TAIL, keyName, metadataItemType, keyComment, keyValue);
    744             break;
    745         case 'L':
    746             metadataItemType = PS_META_BOOL;
    747             tempBool = (keyValue[0] == 'T') ? 1 : 0;
    748             success =
    749                 psMetadataAdd(output, PS_LIST_TAIL, keyName, metadataItemType, keyComment, tempBool);
    750             break;
    751         case 'U':
    752         case 'X':
    753         default:
    754             psError(__func__, "Invalid psMetadataType: %c", keyType);
    755             return output;
    756         }
    757 
    758         if (!success) {
    759             psError(__func__, "Failed to add metadata item. Name: %s", keyName);
    760             return output;
    761         }
    762     }
    763 
    764     return output;
    765 }
  • trunk/psLib/src/astronomy/psMetadata.h

    r1844 r1970  
    1111*  @author Ross Harman, MHPCC
    1212*
    13 *  @version $Revision: 1.22 $ $Name: not supported by cvs2svn $
    14 *  @date $Date: 2004-09-21 23:27:55 $
     13*  @version $Revision: 1.23 $ $Name: not supported by cvs2svn $
     14*  @date $Date: 2004-10-06 01:06:27 $
    1515*
    1616*  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    2121#include <stdarg.h>
    2222#include <stdio.h>
    23 #include <fitsio.h>
    2423
    2524#include "psHash.h"
     
    4039    PS_META_F64,                       ///< Double-precision float data.
    4140    PS_META_STR,                       ///< String data (Stored in as void *).
     41    PS_META_VEC,                       ///< Vector data (Stored in as void *).
    4242    PS_META_IMG,                       ///< Image data (Stored in as void *).
    4343    PS_META_JPEG,                      ///< JPEG data (Stored in as void *).
     
    8484psMetadata;
    8585
    86 /*****************************************************************************/
    87 
    88 /* FUNCTION PROTOTYPES                                                       */
    89 
    90 /*****************************************************************************/
    91 
    9286/** Create a metadata item.
    9387 *
     
    248242);
    249243
    250 /** Print metadata item to file.
    251  *
    252  *  Metadata items may be printed to an open file descriptor based on a
    253  *  provided format. The format is a sprintf format statement with exactly
    254  *  one % formatting command. If the metadata item type is a numeric type,
    255  *  this formatting command must also be numeric, and the type conversion
    256  *  performed to the value to match the format type. If the metadata type is
    257  *  a string, the fromatting command must also be for a string. If the
    258  *  metadata type is any other data type, printing is not allowed.
    259  *
    260  * @return psMetadataItem* : Pointer metadata item.
    261  */
    262 void psMetadataItemPrint(
    263     FILE * fd,                         ///< Pointer to file to write metadata item.
    264     const char *format,                ///< Format to print metadata item.
    265     const psMetadataItem* restrict metadataItem ///< Metadata item to print.
    266 );
    267 
    268 /** Read metadata header.
    269  *
    270  *  Read a metadata header from file. If the file is not found, an error is
    271  *  reported. This file is currently unimplemented.
    272  *
    273  *  @return psMetadata* : Pointer metadata.
    274  */
    275 psMetadata* psMetadataReadHeader(
    276     psMetadata* output,                ///< Resulting metadata from read.
    277     char *extName,                     ///< File name extension string.
    278     int extNum,                        ///< File name extension number. Starts at 1.
    279     char *fileName                     ///< Name of file to read.
    280 );
    281244
    282245/// @}
  • trunk/psLib/src/collections/psMetadata.c

    r1844 r1970  
    33*
    44*
    5 *  @brief Contains metadata struuctures, enumerations and functions prototypes
     5*  @brief Contains metadata structures, enumerations and functions prototypes.
    66*
    77*  This file defines metadata item, metadata type, metadata flags, metadata containers, and function
     
    1212*  @author Ross Harman, MHPCC
    1313*
    14 *  @version $Revision: 1.28 $ $Name: not supported by cvs2svn $
    15 *  @date $Date: 2004-09-21 23:27:55 $
     14*  @version $Revision: 1.29 $ $Name: not supported by cvs2svn $
     15*  @date $Date: 2004-10-06 01:06:20 $
    1616*
    1717*  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    3232#include "psList.h"
    3333#include "psHash.h"
     34#include "psVector.h"
    3435#include "psMetadata.h"
    3536#include "psString.h"
     
    3940/******************************************************************************/
    4041
    41 #define FITS_ERROR(STRING,PS_ERROR)                                                                          \
    42 fits_get_errstatus(status, fitsErr);                                                                     \
    43 psError(__func__, STRING, PS_ERROR, fitsErr);                                                            \
    44 status = 0;                                                                                              \
    45 fits_close_file(fd, &status);                                                                            \
    46 if(status){                                                                                              \
    47     fits_get_errstatus(status, fitsErr);                                                                  \
    48     psError(__func__, "Couldn't close FITS file. FITS error: %s", fitsErr);                               \
    49 }                                                                                                        \
    50 status = 0;                                                                                              \
    51 return output;
    52 
    53 /** Maximum size of a FITS line */
    54 #define FITS_LINE_SIZE 80
    55 
    5642/** Maximum size of a string */
    5743#define MAX_STRING_LENGTH 1024
     
    9379    psFree(metadataItem->items);
    9480
    95     if (type == PS_META_STR ||
    96             type == PS_META_IMG ||
    97             type == PS_META_JPEG || type == PS_META_PNG || type == PS_META_ASTROM || type == PS_META_UNKNOWN) {
     81    if (type == PS_META_STR || type == PS_META_VEC || type == PS_META_IMG || type == PS_META_JPEG ||
     82            type == PS_META_PNG || type == PS_META_ASTROM || type == PS_META_UNKNOWN) {
    9883        psFree(metadataItem->data.V);
    9984    }
     
    133118                                     va_list argPtr)
    134119{
     120    psVector *vecIn = NULL;
     121    psVector *vecOut = NULL;
    135122    psMetadataItem* metadataItem = NULL;
     123
    136124
    137125    if (name == NULL) {
     
    182170        metadataItem->data.V = psStringNCopy(va_arg(argPtr, char *), MAX_STRING_LENGTH);
    183171        break;
     172    case PS_META_VEC:
     173        vecIn = (psVector*)va_arg(argPtr, psVector*);
     174        vecOut = psVectorAlloc(vecIn->nalloc, vecIn->type.type);
     175        memcpy(vecOut->data.V, vecIn->data.V, vecIn->nalloc*PSELEMTYPE_SIZEOF(vecIn->type.type));
     176        metadataItem->data.V = vecOut;
     177        break;
    184178    case PS_META_IMG:
    185179    case PS_META_JPEG:
     
    373367    return true;
    374368}
    375 
    376 
    377369
    378370bool psMetadataRemove(psMetadata* restrict md, int where, const char *restrict key)
     
    603595    return entry;
    604596}
    605 
    606 void psMetadataItemPrint(FILE * fd, const char *format, const psMetadataItem* restrict metadataItem)
    607 {
    608     psMetadataType type;
    609 
    610     if (fd == NULL) {
    611         psError(__func__, "Null file descriptor not allowed");
    612         return;
    613     }
    614 
    615     if (format == NULL) {
    616         psError(__func__, "Null format not allowed");
    617         return;
    618     }
    619 
    620     if (metadataItem == NULL) {
    621         psError(__func__, "Null metadata not allowed");
    622         return;
    623     }
    624 
    625     type = metadataItem->type;
    626 
    627     switch (type) {
    628     case PS_META_BOOL:
    629         fprintf(fd, format, metadataItem->data.B);
    630         break;
    631     case PS_META_S32:
    632         fprintf(fd,format, metadataItem->data.S32);
    633         break;
    634     case PS_META_F32:
    635         fprintf(fd, format, metadataItem->data.F32);
    636         break;
    637     case PS_META_F64:
    638         fprintf(fd, format, metadataItem->data.F64);
    639         break;
    640     case PS_META_STR:
    641         fprintf(fd, format, metadataItem->data.V);
    642         break;
    643     case PS_META_ITEM_SET:
    644     case PS_META_IMG:
    645     case PS_META_JPEG:
    646     case PS_META_PNG:
    647     case PS_META_ASTROM:
    648     case PS_META_UNKNOWN:
    649     default:
    650         psError(__func__, " Invalid psMetadataType to print: %d", type);
    651     }
    652 }
    653 
    654 psMetadata* psMetadataReadHeader(psMetadata* output, char *extName, int extNum, char *fileName)
    655 {
    656     bool tempBool;
    657     bool success;
    658     char keyType;
    659     char keyName[FITS_LINE_SIZE];
    660     char keyValue[FITS_LINE_SIZE];
    661     char keyComment[FITS_LINE_SIZE];
    662     char fitsErr[MAX_STRING_LENGTH];
    663     int i;
    664     int hduType = 0;
    665     int status = 0;
    666     int numKeys = 0;
    667     int keyNum = 0;
    668     psMetadataType metadataItemType;
    669     fitsfile *fd = NULL;
    670 
    671     if(fileName == NULL) {
    672         psError(__func__, "Null fileName not allowed");
    673         return NULL;
    674     }
    675 
    676     fits_open_file(&fd, fileName, READONLY, &status);
    677     if(fd == NULL || status != 0) {
    678         FITS_ERROR("FITS error while opening file: %s %s", fileName);
    679         return NULL;
    680     }
    681 
    682     if (extName == NULL && extNum == 0) {
    683         psError(__func__, "Null extName and extNum = 0 not allowed");
    684         return NULL;
    685     } else if (extName && extNum) {
    686         psError(__func__, "Both extName and extNum arguments should not have non zero values.");
    687         return NULL;
    688     }
    689 
    690     // Allocate metadata if user didn't
    691     if (output == NULL) {
    692         output = psMetadataAlloc();
    693     }
    694 
    695     // Move to user designated HDU number or HDU name in FITS file. HDU numbers starts at one.
    696     if (extName != NULL) {
    697         if (fits_movnam_hdu(fd, ANY_HDU, extName, 0, &status) != 0) {
    698             FITS_ERROR("FITS error while locating header %s: %s", extName);
    699         }
    700     } else {
    701         if (fits_movabs_hdu(fd, extNum, &hduType, &status) != 0) {
    702             FITS_ERROR("FITS error while locating header %d: %s", extNum);
    703         }
    704     }
    705 
    706     // Get number of key names
    707     if (fits_get_hdrpos(fd, &numKeys, &keyNum, &status) != 0) {
    708         FITS_ERROR("FITS error while reading key %d: %s", keyNum);
    709     }
    710 
    711     // Get each key name. Keywords start at one.
    712     for (i = 1; i <= numKeys; i++) {
    713         if (fits_read_keyn(fd, i, keyName, keyValue, keyComment, &status) != 0) {
    714             FITS_ERROR("FITS error while reading key %d: %s", keyNum);
    715         }
    716         if (fits_get_keytype(keyValue, &keyType, &status) != 0) {
    717             fits_get_errstatus(status, fitsErr);
    718             if (status != VALUE_UNDEFINED) {
    719                 FITS_ERROR("FITS error while determining key %d type: %s", keyNum);
    720             } else {
    721                 // Some keywords are still valid if they don't have a type (like COMMENTS and HISTORY)
    722                 keyType = 'C';
    723                 status = 0;
    724             }
    725         }
    726 
    727         switch (keyType) {
    728         case 'I':
    729             metadataItemType = PS_META_S32;
    730             success =
    731                 psMetadataAdd(output, PS_LIST_TAIL, keyName, metadataItemType, keyComment,
    732                               atoi(keyValue));
    733             break;
    734         case 'F':
    735             metadataItemType = PS_META_F64;
    736             success =
    737                 psMetadataAdd(output, PS_LIST_TAIL, keyName, metadataItemType, keyComment,
    738                               atof(keyValue));
    739             break;
    740         case 'C':
    741             metadataItemType = PS_META_STR;
    742             success =
    743                 psMetadataAdd(output, PS_LIST_TAIL, keyName, metadataItemType, keyComment, keyValue);
    744             break;
    745         case 'L':
    746             metadataItemType = PS_META_BOOL;
    747             tempBool = (keyValue[0] == 'T') ? 1 : 0;
    748             success =
    749                 psMetadataAdd(output, PS_LIST_TAIL, keyName, metadataItemType, keyComment, tempBool);
    750             break;
    751         case 'U':
    752         case 'X':
    753         default:
    754             psError(__func__, "Invalid psMetadataType: %c", keyType);
    755             return output;
    756         }
    757 
    758         if (!success) {
    759             psError(__func__, "Failed to add metadata item. Name: %s", keyName);
    760             return output;
    761         }
    762     }
    763 
    764     return output;
    765 }
  • trunk/psLib/src/collections/psMetadata.h

    r1844 r1970  
    1111*  @author Ross Harman, MHPCC
    1212*
    13 *  @version $Revision: 1.22 $ $Name: not supported by cvs2svn $
    14 *  @date $Date: 2004-09-21 23:27:55 $
     13*  @version $Revision: 1.23 $ $Name: not supported by cvs2svn $
     14*  @date $Date: 2004-10-06 01:06:27 $
    1515*
    1616*  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    2121#include <stdarg.h>
    2222#include <stdio.h>
    23 #include <fitsio.h>
    2423
    2524#include "psHash.h"
     
    4039    PS_META_F64,                       ///< Double-precision float data.
    4140    PS_META_STR,                       ///< String data (Stored in as void *).
     41    PS_META_VEC,                       ///< Vector data (Stored in as void *).
    4242    PS_META_IMG,                       ///< Image data (Stored in as void *).
    4343    PS_META_JPEG,                      ///< JPEG data (Stored in as void *).
     
    8484psMetadata;
    8585
    86 /*****************************************************************************/
    87 
    88 /* FUNCTION PROTOTYPES                                                       */
    89 
    90 /*****************************************************************************/
    91 
    9286/** Create a metadata item.
    9387 *
     
    248242);
    249243
    250 /** Print metadata item to file.
    251  *
    252  *  Metadata items may be printed to an open file descriptor based on a
    253  *  provided format. The format is a sprintf format statement with exactly
    254  *  one % formatting command. If the metadata item type is a numeric type,
    255  *  this formatting command must also be numeric, and the type conversion
    256  *  performed to the value to match the format type. If the metadata type is
    257  *  a string, the fromatting command must also be for a string. If the
    258  *  metadata type is any other data type, printing is not allowed.
    259  *
    260  * @return psMetadataItem* : Pointer metadata item.
    261  */
    262 void psMetadataItemPrint(
    263     FILE * fd,                         ///< Pointer to file to write metadata item.
    264     const char *format,                ///< Format to print metadata item.
    265     const psMetadataItem* restrict metadataItem ///< Metadata item to print.
    266 );
    267 
    268 /** Read metadata header.
    269  *
    270  *  Read a metadata header from file. If the file is not found, an error is
    271  *  reported. This file is currently unimplemented.
    272  *
    273  *  @return psMetadata* : Pointer metadata.
    274  */
    275 psMetadata* psMetadataReadHeader(
    276     psMetadata* output,                ///< Resulting metadata from read.
    277     char *extName,                     ///< File name extension string.
    278     int extNum,                        ///< File name extension number. Starts at 1.
    279     char *fileName                     ///< Name of file to read.
    280 );
    281244
    282245/// @}
  • trunk/psLib/src/types/psMetadata.c

    r1844 r1970  
    33*
    44*
    5 *  @brief Contains metadata struuctures, enumerations and functions prototypes
     5*  @brief Contains metadata structures, enumerations and functions prototypes.
    66*
    77*  This file defines metadata item, metadata type, metadata flags, metadata containers, and function
     
    1212*  @author Ross Harman, MHPCC
    1313*
    14 *  @version $Revision: 1.28 $ $Name: not supported by cvs2svn $
    15 *  @date $Date: 2004-09-21 23:27:55 $
     14*  @version $Revision: 1.29 $ $Name: not supported by cvs2svn $
     15*  @date $Date: 2004-10-06 01:06:20 $
    1616*
    1717*  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    3232#include "psList.h"
    3333#include "psHash.h"
     34#include "psVector.h"
    3435#include "psMetadata.h"
    3536#include "psString.h"
     
    3940/******************************************************************************/
    4041
    41 #define FITS_ERROR(STRING,PS_ERROR)                                                                          \
    42 fits_get_errstatus(status, fitsErr);                                                                     \
    43 psError(__func__, STRING, PS_ERROR, fitsErr);                                                            \
    44 status = 0;                                                                                              \
    45 fits_close_file(fd, &status);                                                                            \
    46 if(status){                                                                                              \
    47     fits_get_errstatus(status, fitsErr);                                                                  \
    48     psError(__func__, "Couldn't close FITS file. FITS error: %s", fitsErr);                               \
    49 }                                                                                                        \
    50 status = 0;                                                                                              \
    51 return output;
    52 
    53 /** Maximum size of a FITS line */
    54 #define FITS_LINE_SIZE 80
    55 
    5642/** Maximum size of a string */
    5743#define MAX_STRING_LENGTH 1024
     
    9379    psFree(metadataItem->items);
    9480
    95     if (type == PS_META_STR ||
    96             type == PS_META_IMG ||
    97             type == PS_META_JPEG || type == PS_META_PNG || type == PS_META_ASTROM || type == PS_META_UNKNOWN) {
     81    if (type == PS_META_STR || type == PS_META_VEC || type == PS_META_IMG || type == PS_META_JPEG ||
     82            type == PS_META_PNG || type == PS_META_ASTROM || type == PS_META_UNKNOWN) {
    9883        psFree(metadataItem->data.V);
    9984    }
     
    133118                                     va_list argPtr)
    134119{
     120    psVector *vecIn = NULL;
     121    psVector *vecOut = NULL;
    135122    psMetadataItem* metadataItem = NULL;
     123
    136124
    137125    if (name == NULL) {
     
    182170        metadataItem->data.V = psStringNCopy(va_arg(argPtr, char *), MAX_STRING_LENGTH);
    183171        break;
     172    case PS_META_VEC:
     173        vecIn = (psVector*)va_arg(argPtr, psVector*);
     174        vecOut = psVectorAlloc(vecIn->nalloc, vecIn->type.type);
     175        memcpy(vecOut->data.V, vecIn->data.V, vecIn->nalloc*PSELEMTYPE_SIZEOF(vecIn->type.type));
     176        metadataItem->data.V = vecOut;
     177        break;
    184178    case PS_META_IMG:
    185179    case PS_META_JPEG:
     
    373367    return true;
    374368}
    375 
    376 
    377369
    378370bool psMetadataRemove(psMetadata* restrict md, int where, const char *restrict key)
     
    603595    return entry;
    604596}
    605 
    606 void psMetadataItemPrint(FILE * fd, const char *format, const psMetadataItem* restrict metadataItem)
    607 {
    608     psMetadataType type;
    609 
    610     if (fd == NULL) {
    611         psError(__func__, "Null file descriptor not allowed");
    612         return;
    613     }
    614 
    615     if (format == NULL) {
    616         psError(__func__, "Null format not allowed");
    617         return;
    618     }
    619 
    620     if (metadataItem == NULL) {
    621         psError(__func__, "Null metadata not allowed");
    622         return;
    623     }
    624 
    625     type = metadataItem->type;
    626 
    627     switch (type) {
    628     case PS_META_BOOL:
    629         fprintf(fd, format, metadataItem->data.B);
    630         break;
    631     case PS_META_S32:
    632         fprintf(fd,format, metadataItem->data.S32);
    633         break;
    634     case PS_META_F32:
    635         fprintf(fd, format, metadataItem->data.F32);
    636         break;
    637     case PS_META_F64:
    638         fprintf(fd, format, metadataItem->data.F64);
    639         break;
    640     case PS_META_STR:
    641         fprintf(fd, format, metadataItem->data.V);
    642         break;
    643     case PS_META_ITEM_SET:
    644     case PS_META_IMG:
    645     case PS_META_JPEG:
    646     case PS_META_PNG:
    647     case PS_META_ASTROM:
    648     case PS_META_UNKNOWN:
    649     default:
    650         psError(__func__, " Invalid psMetadataType to print: %d", type);
    651     }
    652 }
    653 
    654 psMetadata* psMetadataReadHeader(psMetadata* output, char *extName, int extNum, char *fileName)
    655 {
    656     bool tempBool;
    657     bool success;
    658     char keyType;
    659     char keyName[FITS_LINE_SIZE];
    660     char keyValue[FITS_LINE_SIZE];
    661     char keyComment[FITS_LINE_SIZE];
    662     char fitsErr[MAX_STRING_LENGTH];
    663     int i;
    664     int hduType = 0;
    665     int status = 0;
    666     int numKeys = 0;
    667     int keyNum = 0;
    668     psMetadataType metadataItemType;
    669     fitsfile *fd = NULL;
    670 
    671     if(fileName == NULL) {
    672         psError(__func__, "Null fileName not allowed");
    673         return NULL;
    674     }
    675 
    676     fits_open_file(&fd, fileName, READONLY, &status);
    677     if(fd == NULL || status != 0) {
    678         FITS_ERROR("FITS error while opening file: %s %s", fileName);
    679         return NULL;
    680     }
    681 
    682     if (extName == NULL && extNum == 0) {
    683         psError(__func__, "Null extName and extNum = 0 not allowed");
    684         return NULL;
    685     } else if (extName && extNum) {
    686         psError(__func__, "Both extName and extNum arguments should not have non zero values.");
    687         return NULL;
    688     }
    689 
    690     // Allocate metadata if user didn't
    691     if (output == NULL) {
    692         output = psMetadataAlloc();
    693     }
    694 
    695     // Move to user designated HDU number or HDU name in FITS file. HDU numbers starts at one.
    696     if (extName != NULL) {
    697         if (fits_movnam_hdu(fd, ANY_HDU, extName, 0, &status) != 0) {
    698             FITS_ERROR("FITS error while locating header %s: %s", extName);
    699         }
    700     } else {
    701         if (fits_movabs_hdu(fd, extNum, &hduType, &status) != 0) {
    702             FITS_ERROR("FITS error while locating header %d: %s", extNum);
    703         }
    704     }
    705 
    706     // Get number of key names
    707     if (fits_get_hdrpos(fd, &numKeys, &keyNum, &status) != 0) {
    708         FITS_ERROR("FITS error while reading key %d: %s", keyNum);
    709     }
    710 
    711     // Get each key name. Keywords start at one.
    712     for (i = 1; i <= numKeys; i++) {
    713         if (fits_read_keyn(fd, i, keyName, keyValue, keyComment, &status) != 0) {
    714             FITS_ERROR("FITS error while reading key %d: %s", keyNum);
    715         }
    716         if (fits_get_keytype(keyValue, &keyType, &status) != 0) {
    717             fits_get_errstatus(status, fitsErr);
    718             if (status != VALUE_UNDEFINED) {
    719                 FITS_ERROR("FITS error while determining key %d type: %s", keyNum);
    720             } else {
    721                 // Some keywords are still valid if they don't have a type (like COMMENTS and HISTORY)
    722                 keyType = 'C';
    723                 status = 0;
    724             }
    725         }
    726 
    727         switch (keyType) {
    728         case 'I':
    729             metadataItemType = PS_META_S32;
    730             success =
    731                 psMetadataAdd(output, PS_LIST_TAIL, keyName, metadataItemType, keyComment,
    732                               atoi(keyValue));
    733             break;
    734         case 'F':
    735             metadataItemType = PS_META_F64;
    736             success =
    737                 psMetadataAdd(output, PS_LIST_TAIL, keyName, metadataItemType, keyComment,
    738                               atof(keyValue));
    739             break;
    740         case 'C':
    741             metadataItemType = PS_META_STR;
    742             success =
    743                 psMetadataAdd(output, PS_LIST_TAIL, keyName, metadataItemType, keyComment, keyValue);
    744             break;
    745         case 'L':
    746             metadataItemType = PS_META_BOOL;
    747             tempBool = (keyValue[0] == 'T') ? 1 : 0;
    748             success =
    749                 psMetadataAdd(output, PS_LIST_TAIL, keyName, metadataItemType, keyComment, tempBool);
    750             break;
    751         case 'U':
    752         case 'X':
    753         default:
    754             psError(__func__, "Invalid psMetadataType: %c", keyType);
    755             return output;
    756         }
    757 
    758         if (!success) {
    759             psError(__func__, "Failed to add metadata item. Name: %s", keyName);
    760             return output;
    761         }
    762     }
    763 
    764     return output;
    765 }
  • trunk/psLib/src/types/psMetadata.h

    r1844 r1970  
    1111*  @author Ross Harman, MHPCC
    1212*
    13 *  @version $Revision: 1.22 $ $Name: not supported by cvs2svn $
    14 *  @date $Date: 2004-09-21 23:27:55 $
     13*  @version $Revision: 1.23 $ $Name: not supported by cvs2svn $
     14*  @date $Date: 2004-10-06 01:06:27 $
    1515*
    1616*  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    2121#include <stdarg.h>
    2222#include <stdio.h>
    23 #include <fitsio.h>
    2423
    2524#include "psHash.h"
     
    4039    PS_META_F64,                       ///< Double-precision float data.
    4140    PS_META_STR,                       ///< String data (Stored in as void *).
     41    PS_META_VEC,                       ///< Vector data (Stored in as void *).
    4242    PS_META_IMG,                       ///< Image data (Stored in as void *).
    4343    PS_META_JPEG,                      ///< JPEG data (Stored in as void *).
     
    8484psMetadata;
    8585
    86 /*****************************************************************************/
    87 
    88 /* FUNCTION PROTOTYPES                                                       */
    89 
    90 /*****************************************************************************/
    91 
    9286/** Create a metadata item.
    9387 *
     
    248242);
    249243
    250 /** Print metadata item to file.
    251  *
    252  *  Metadata items may be printed to an open file descriptor based on a
    253  *  provided format. The format is a sprintf format statement with exactly
    254  *  one % formatting command. If the metadata item type is a numeric type,
    255  *  this formatting command must also be numeric, and the type conversion
    256  *  performed to the value to match the format type. If the metadata type is
    257  *  a string, the fromatting command must also be for a string. If the
    258  *  metadata type is any other data type, printing is not allowed.
    259  *
    260  * @return psMetadataItem* : Pointer metadata item.
    261  */
    262 void psMetadataItemPrint(
    263     FILE * fd,                         ///< Pointer to file to write metadata item.
    264     const char *format,                ///< Format to print metadata item.
    265     const psMetadataItem* restrict metadataItem ///< Metadata item to print.
    266 );
    267 
    268 /** Read metadata header.
    269  *
    270  *  Read a metadata header from file. If the file is not found, an error is
    271  *  reported. This file is currently unimplemented.
    272  *
    273  *  @return psMetadata* : Pointer metadata.
    274  */
    275 psMetadata* psMetadataReadHeader(
    276     psMetadata* output,                ///< Resulting metadata from read.
    277     char *extName,                     ///< File name extension string.
    278     int extNum,                        ///< File name extension number. Starts at 1.
    279     char *fileName                     ///< Name of file to read.
    280 );
    281244
    282245/// @}
Note: See TracChangeset for help on using the changeset viewer.