IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 21059


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

Location:
trunk/Ohana/src/libfits
Files:
4 edited

Legend:

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

    r16995 r21059  
    6060}
    6161
     62// find the given field among the HIERARCH keywords
     63// these have the form: HIERARCH KEYWORD = value
     64// returns a pointer to the start of the field
     65char *gfits_header_hierarch_field (Header *header, char *field, int N) {
     66
     67  char *buf, *ptr;
     68  int i, Nwant, Nfound, Nfield;
     69
     70  Nfield = strlen (field);
     71  if (Nfield >= 71) return NULL;
     72
     73  buf = header[0].buffer;
     74
     75  /* find the Nth entry */
     76  if (N > 0) {
     77    Nfound = 0;
     78    for (i = 0; i < header[0].size; i+= FT_LINE_LENGTH, buf += FT_LINE_LENGTH) {
     79      // have we found a HIERARCH entry?
     80      if (strncmp ("HIERARCH", buf, 8)) continue;
     81      ptr = buf + 9; // start of following keyword
     82      if (strncmp (field, ptr, Nfield)) continue;
     83      Nfound ++;
     84      if (Nfound == N) return (ptr);
     85    }
     86  }
     87
     88  /* find the Ntotal - Nth entry */
     89  if (N < 0) {
     90    /* count the entries */
     91    Nfound = 0;
     92    for (i = 0; i < header[0].size; i+= FT_LINE_LENGTH, buf += FT_LINE_LENGTH) {
     93      if (strncmp ("HIERARCH", buf, 8)) continue;
     94      ptr = buf + 9; // start of following keyword
     95      if (strncmp (field, ptr, Nfield)) continue;
     96      Nfound ++;
     97    }
     98
     99    Nwant = Nfound + N + 1;
     100    buf = header[0].buffer;
     101
     102    /* find the Nwant entry */
     103    Nfound = 0;
     104    for (i = 0; i < header[0].size; i+= FT_LINE_LENGTH, buf += FT_LINE_LENGTH) {
     105      if (strncmp ("HIERARCH", buf, 8)) continue;
     106      ptr = buf + 9; // start of following keyword
     107      if (strncmp (field, ptr, Nfield)) continue;
     108      Nfound ++;
     109      if (Nwant == Nfound) return (ptr);
     110    }
     111  }
     112
     113  return ((char *) NULL);
     114
     115/*
     116
     117   find the Nth entry of this keyword.
     118   if N < 0, find the last - N - 1 word (ie, -1 = last)
     119
     120*/
     121}
     122
    62123/*********************** fits header field ****************************/
    63124char *gfits_header_lineno (Header *header, int N) {
  • trunk/Ohana/src/libfits/header/F_modify.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);
    6 void pad_ending (char *line, char value, int Nbyte);
    73
    84int gfits_modify (Header *header, char *field, char *mode, int N,...) {
     
    5147    strncpy (comment, qe, p + 80 - qe);
    5248  }
    53   pad_ending (comment, ' ', 82);  /* comment must contain spaces to the end */
     49  gfits_pad_ending (comment, ' ', 82);  /* comment must contain spaces to the end */
    5450
    5551  /* write the numeric modes */
     
    9692    strncpy (data, qs, MAX (MIN (qe - qs, 71), 0));
    9793    strncpy (comment, va_arg (argp, char *), 80);
    98     pad_ending (comment, ' ', 82);  /* comment must contain spaces to the end */
     94    gfits_pad_ending (comment, ' ', 82);  /* comment must contain spaces to the end */
    9995    snprintf (string, 81, "%-8s= %s / %-s", field, data, comment);
    10096    /* this will keep the original line, but truncate the comment */
     
    156152  } else {
    157153    while ((*c1 != ' ') && (c1 < line + 80)) c1++;
     154  }
     155  return (c1);
     156}
     157
     158/* given a pointer to the FITS HIERARCH card field name, return pointer to the start of the data area */
     159char *gfits_hierarch_keyword_start (char *line, char *field) {
     160
     161  char *c;
     162
     163  /* find the end of the existing data region */
     164  c = line + strlen(field) + 1;
     165  if (*c != '=') return (c);  /* non-data fields are not allowed */
     166 
     167  c += 2;
     168  /* advance pointer over WHITESPACE */
     169  while ((*c == ' ') && (c < line + 71)) { c++; }
     170 
     171  /* skip one quote mark */
     172  if ((*c == 0x27) && (c < line + 71)) { c++; }
     173
     174  return (c);
     175}
     176
     177/* given a pointer to the FITS HIERARCH card field name, return pointer to the end of the data area */
     178char *gfits_hierarch_keyword_end (char *line, char *field) {
     179
     180  int done;
     181  char *c1, *c2;
     182
     183  /* find the end of the existing data region */
     184  c1 = line + strlen(field) + 1;
     185  if (*c1 != '=') return (NULL);  /* non-data fields are not allowed */
     186  c1 += 2;
     187
     188  /* advance pointer over WHITESPACE */
     189  while ((*c1 == ' ') && (c1 < line + 71)) { c1++; }
     190 
     191  if (c1[0] == 0x27) { /* entry a string, skip over 'fred' */
     192    for (done = FALSE, c2 = c1 + 1; !done && (c2 < line + 71); c2++) {
     193      if ((c2[0] == 0x27) && (c2[1] == 0x27)) {
     194        c2 += 2;
     195        continue;
     196      }
     197      if (c2[0] == 0x27) {
     198        c1 = c2;
     199        done = TRUE;
     200      }
     201    }
     202    if (!done) { /* error in line: mismatched ' chars, return fixed position */
     203      c1 = line + 30;
     204    }
     205  } else {
     206    while ((*c1 != ' ') && (c1 < line + 71)) c1++;
    158207  }
    159208  return (c1);
     
    171220
    172221/* fill 'line' with Nbyte space from first NULL to last byte with value */
    173 void pad_ending (char *line, char value, int Nbyte) {
     222void gfits_pad_ending (char *line, char value, int Nbyte) {
    174223 
    175224  char *p;
  • 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 */
  • trunk/Ohana/src/libfits/include/gfitsio.h

    r20652 r21059  
    133133int     gfits_primary_to_extended      PROTO((Header *header, char *exttype, char *comment));
    134134int     gfits_modify_extended          PROTO((Header *header, char *exttype, char *comment));
    135 
     135char   *gfits_keyword_start            PROTO((char *line));
     136char   *gfits_keyword_end              PROTO((char *line));
     137char   *gfits_hierarch_keyword_start   PROTO((char *line, char *field));
     138char   *gfits_hierarch_keyword_end     PROTO((char *line, char *field));
     139void    gfits_pad_ending               PROTO((char *line, char value, int Nbyte));
     140int     gfits_vscan_hierarch           PROTO((Header *header, char *field, char *mode, int N, va_list argp));
     141char   *gfits_header_hierarch_field    PROTO((Header *header, char *field, int N));
    136142
    137143/******************************* Matrix functions *************/
Note: See TracChangeset for help on using the changeset viewer.