IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Dec 30, 2008, 10:01:42 AM (18 years ago)
Author:
eugene
Message:

add ability to read HIERARCH keywords if basic keyword is not found

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/libfits/header/F_scan.c

    r10073 r21059  
    11# include <ohana.h>
    22# include <gfitsio.h>
    3 
    4 char *gfits_keyword_start (char *line);
    5 char *gfits_keyword_end (char *line);
    63
    74int gfits_scan (Header *header, char *field, char *mode, int N,...) {
     
    1916
    2017  char *p, *q, *s, tmp[81];
    21   int Nchar;
     18  int Nchar, status;
    2219  double value;
    2320 
    2421  /* find the correct line with field */
    2522  p = gfits_header_field (header, field, N);
    26   if (p == NULL) return (FALSE);
     23  if (p == NULL) {
     24    status = gfits_vscan_hierarch (header, field, mode, N, argp);
     25    return (status);
     26  }
    2727
    2828  /* non-data entry (COMMENT, HISTORY) */
     
    9292}
    9393
     94# define HIERARCH_LENGTH 71
     95int gfits_vscan_hierarch (Header *header, char *field, char *mode, int N, va_list argp) {
     96
     97  char *p, *q, *s, tmp[81];
     98  int Nchar, Nfield;
     99  double value;
     100 
     101  /* find the correct line with field */
     102  p = gfits_header_hierarch_field (header, field, N);
     103  if (p == NULL) return (FALSE);
     104
     105  /* non-data entries (COMMENT, HISTORY) are not allowed */
     106  if (!strcmp (mode, "%S")) {
     107    return (FALSE);
     108  }
     109
     110  /* all others require '=' in column p + 1 + strlen(field) */
     111  Nfield = strlen (field);
     112  if (p[Nfield + 1] != '=') return (FALSE);
     113
     114  /* comment from data line */
     115  if (!strcmp (mode, "%C")) {
     116    q = gfits_hierarch_keyword_end (p, field);
     117    if (!q) return (FALSE);
     118    q += 3;
     119    q = MIN (p + HIERARCH_LENGTH, q);
     120    bzero (tmp, 81);
     121    Nchar = MIN (HIERARCH_LENGTH, p + HIERARCH_LENGTH - q);
     122    memcpy (tmp, q, Nchar);
     123    stripwhite (tmp);
     124    strcpy (va_arg (argp, char *), tmp);
     125    return (TRUE);
     126  }
     127
     128  /* extract data into char array (exclude containing ' chars) */
     129  if (!strcmp (mode, "%s")) {
     130    s = gfits_hierarch_keyword_start (p, field); /* points at first char (not ') */
     131    q = gfits_hierarch_keyword_end (p, field); /* points at following space or ' */
     132
     133    Nchar = MIN (HIERARCH_LENGTH, MAX (0, (q - s)));
     134    bzero (tmp, 81);
     135    memcpy (tmp, s, Nchar);
     136    stripwhite (tmp);
     137    strcpy (va_arg (argp, char *), tmp);
     138    return (TRUE);
     139  }
     140 
     141  /* boolean data, requires int target */
     142  if (!strcmp (mode, "%t")) {
     143    s = gfits_hierarch_keyword_start (p, field);
     144    if (*s == 'T') {
     145      *va_arg (argp, int *) = TRUE;
     146      return (TRUE);
     147    }
     148    if (*s == 'F') {
     149      *va_arg (argp, int *) = FALSE;   
     150      return (TRUE);
     151    }
     152  }
     153
     154  /* remaining options are numerical data */
     155  /* need to interpret 1.0d5 as 1.0e5 */
     156  s = gfits_hierarch_keyword_start (p, field); /* points at first char (not ') */
     157  q = gfits_hierarch_keyword_end (p, field); /* points at following space or ' */
     158  value = strtod (s, &q);
     159  if ((*q == 'd') || (*q == 'D'))
     160    value *= pow (10.0, atof (q + 1));
     161
     162  if (!strcmp (mode, "%d"))  { *va_arg (argp, int *)       = value; return (TRUE); }
     163  if (!strcmp (mode, "%u"))  { *va_arg (argp, unsigned *)  = value; return (TRUE); }
     164  if (!strcmp (mode, "%ld")) { *va_arg (argp, long *)      = value; return (TRUE); }
     165  if (!strcmp (mode, "%hd")) { *va_arg (argp, short *)     = value; return (TRUE); }
     166  if (!strcmp (mode, "%f"))  { *va_arg (argp, float *)     = value; return (TRUE); }
     167  if (!strcmp (mode, "%lf")) { *va_arg (argp, double *)    = value; return (TRUE); }
     168
     169  /* no valid mode found */
     170  return (FALSE);
     171}
     172
    94173/* if the variable argument stuff breaks on another system, look
    95174at F_modify.c as it has the old code */
Note: See TracChangeset for help on using the changeset viewer.