IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 30717


Ignore:
Timestamp:
Feb 21, 2011, 9:01:25 AM (15 years ago)
Author:
eugene
Message:

allow -n option for fhead

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20110213/Ohana/src/tools/src/fhead.c

    r27491 r30717  
    11# include <ohana.h>
    22# include <gfitsio.h>
     3# include <regex.h>
    34
    45int main (int argc, char **argv) {
    56
    6   int N, Extend, Nextend, status;
     7  int N, Extnum, Nextend, status;
    78  int i, j;
    89  off_t nbytes;
    910  Header head;
    10   char *p;
     11  char *p, *CCDKeyword, *Extname, extname[80];
     12  FILE *f;
     13  off_t Nbytes;
     14  regex_t preg;
    1115
    12   Extend = FALSE;
     16  CCDKeyword = NULL;
     17  if ((N = get_argument (argc, argv, "-keyword"))) {
     18    remove_argument (N, &argc, argv);
     19    CCDKeyword = strcreate (argv[N]);
     20    remove_argument (N, &argc, argv);
     21  }
     22  if (CCDKeyword == NULL) {
     23    CCDKeyword = strcreate ("EXTNAME");
     24  }
     25
     26  Extnum = FALSE;
    1327  Nextend = 0;
    1428  if ((N = get_argument (argc, argv, "-x"))) {
    15     Extend = TRUE;
     29    Extnum = TRUE;
    1630    remove_argument (N, &argc, argv);
    1731    Nextend = atoi (argv[N]);
    1832    remove_argument (N, &argc, argv);
     33  }
     34
     35  Extname = NULL;
     36  if ((N = get_argument (argc, argv, "-n"))) {
     37    remove_argument (N, &argc, argv);
     38    Extname = strcreate (argv[N]);
     39    remove_argument (N, &argc, argv);
     40    regcomp (&preg, Extname, REG_EXTENDED);
    1941  }
    2042
     
    2345      fprintf (stdout, "------> %s <------\n", argv[i]);
    2446   
    25     if (Extend) {
    26       status = gfits_read_Xheader (argv[i], &head, Nextend);
    27     } else {
    28       status = gfits_read_header (argv[i], &head);
    29     }     
    30 
    31     if (!status) continue;
    32 
    33     for (j = 79; j < head.datasize; j+= 80) {
    34       head.buffer[j] = 10;
     47    status = FALSE;
     48    if (!Extnum && !Extname) {
     49      if (!gfits_read_header (argv[i], &head)) {
     50        continue;
     51      }
     52    }
     53    if (Extnum) {
     54      if (!gfits_read_Xheader (argv[i], &head, Nextend)) {
     55        continue;
     56      }
     57    }
     58    if (Extname) {
     59      /* keep reading headers until we reach the one we want */
     60      Nextend = 0;
     61      f = fopen (argv[i], "r");
     62      if (f == NULL) continue;
     63      while (gfits_fread_header (f, &head)) {
     64        /* extract the EXTNAME (or other CCDKeyword) for this component (set to PHU for 0th component) */
     65        if (!gfits_scan (&head, CCDKeyword, "%s", 1, extname)) {
     66          if (Nextend == 0) {
     67            strcpy (extname, "PHU");
     68          } else {
     69            strcpy (extname, "UNKNOWN");
     70          }
     71        }
     72        if (!regexec (&preg, extname, 0, NULL, 0)) {
     73          goto done;
     74        }
     75   
     76        Nbytes = gfits_data_size (&head);
     77        fseeko (f, Nbytes, SEEK_CUR);
     78        Nextend ++;
     79      }
     80      // failed to find the desired header
     81      continue;
    3582    }
    3683
    37     p = gfits_header_field (&head, "END", 1);
    38     nbytes = p - head.buffer;
    39     fwrite (head.buffer, nbytes, 1, stdout);
    40     gfits_free_header (&head);
     84  done:
     85      for (j = 79; j < head.datasize; j+= 80) {
     86        head.buffer[j] = 10;
     87      }
    4188
     89      p = gfits_header_field (&head, "END", 1);
     90      nbytes = p - head.buffer;
     91      fwrite (head.buffer, nbytes, 1, stdout);
     92      gfits_free_header (&head);
     93
     94    }
     95    exit (0);
    4296  }
    43   exit (0);
    44 }
Note: See TracChangeset for help on using the changeset viewer.