IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 10858


Ignore:
Timestamp:
Dec 29, 2006, 4:35:23 PM (19 years ago)
Author:
eugene
Message:

adding extension regex matching

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/misc/src/fields.c

    r7080 r10858  
    11# include <ohana.h>
    22# include <gfitsio.h>
     3# include <sys/types.h>
     4# include <regex.h>
     5
     6int print_fields (char *filename, char *extname, Header *header, int argc, char **argv);
    37
    48int main (int argc, char **argv) {
    59
    610  int i;
     11  FILE *f;
    712  Header header;
    8   char filename[1000], buffer[1000];
    9   int N, Extend, Nextend, status, GotFile, GotField;
     13  char filename[1000], *Extname, extname[80];
     14  int N, Nbytes, Extnum, Nextend, status, GotFile, GotField, GotExtension;
    1015
    11   Extend = FALSE;
     16  regex_t preg;
     17
     18  Extnum = FALSE;
    1219  if ((N = get_argument (argc, argv, "-x"))) {
    13     Extend = TRUE;
     20    Extnum = TRUE;
    1421    remove_argument (N, &argc, argv);
    1522    Nextend = atoi (argv[N]);
    1623    remove_argument (N, &argc, argv);
     24  }
     25
     26  Extname = NULL;
     27  if ((N = get_argument (argc, argv, "-n"))) {
     28    remove_argument (N, &argc, argv);
     29    Extname = strcreate (argv[N]);
     30    remove_argument (N, &argc, argv);
     31    regcomp (&preg, Extname, REG_EXTENDED);
    1732  }
    1833
     
    2136
    2237  while (fscanf (stdin, "%s", filename) != EOF) {
    23     if (Extend) {
    24       status = gfits_read_Xheader (filename, &header, Nextend);
    25     } else {
    26       status = gfits_read_header (filename, &header);
    27     }
    28     if (!status) {
    29       GotFile = FALSE;
     38    if (!Extnum && !Extname) {
     39      GotFile &= gfits_read_header (filename, &header);
     40      GotField &= print_fields (filename, NULL, &header, argc, argv);
    3041      continue;
    3142    }
    32      
    33     fprintf (stdout, "%s  ", filename);
    34     for (i = 1; i < argc; i++) {
    35       bzero (buffer, 1000);
    36       status = gfits_scan (&header, argv[i], "%s", 1, buffer);
    37       if (!status) {
    38         GotField = FALSE;
     43    if (Extnum) {
     44      GotFile  &= gfits_read_Xheader (filename, &header, Nextend);
     45      GotField &= print_fields (filename, NULL, &header, argc, argv);
     46      continue;
     47    }
     48
     49    if (Extname) {
     50      /* keep reading headers, only parse fields for matching headers */
     51      Nextend = 0;
     52      GotExtension = FALSE;
     53      f = fopen (filename, "r");
     54      if (f == NULL) {
     55        GotFile = FALSE;
     56        continue;
    3957      }
    40       stripwhite (buffer);
    41       fprintf (stdout, "%s  ", buffer);
     58      while (gfits_fread_header (f, &header)) {
     59        /* extract the EXTNAME for this component (set to PHU for 0th component) */
     60        status = gfits_scan (&header, "EXTNAME", "%s", 1, extname);
     61        if (!status) {
     62          if (Nextend == 0) {
     63            strcpy (extname, "PHU");
     64          } else {
     65            strcpy (extname, "UNKNOWN");
     66          }
     67        }
     68        if (!regexec (&preg, extname, 0, NULL, 0)) {
     69          GotField &= print_fields (filename, extname, &header, argc, argv);
     70          GotExtension = TRUE;
     71        }   
     72   
     73        Nbytes = gfits_matrix_size (&header);
     74        fseek (f, Nbytes, SEEK_CUR);
     75        Nextend ++;
     76
     77        GotFile = gfits_read_Xheader (filename, &header, Nextend);
     78        continue;
     79      }
     80      fclose (f);
     81      if (Nextend == 0) {
     82        GotFile = FALSE;
     83      }
    4284    }
    43     fprintf (stdout, "\n");
     85  }
    4486
    45     gfits_free_header (&header);
    46   }
     87  regfree (&preg);
    4788
    4889  if (!GotFile) exit (1);
    4990  if (!GotField) exit (2);
     91  if (!GotExtension) exit (3);
    5092  exit (0);
    5193}
     94
     95int print_fields (char *filename, char *extname, Header *header, int argc, char **argv) {
     96
     97  int i, status, GotField;
     98  char buffer[1000];
     99
     100  GotField = TRUE;
     101
     102  if (extname) {
     103    fprintf (stdout, "%s[%s]  ", filename, extname);
     104  } else {
     105    fprintf (stdout, "%s  ", filename);
     106  }
     107  for (i = 1; i < argc; i++) {
     108    bzero (buffer, 1000);
     109    GotField &= gfits_scan (header, argv[i], "%s", 1, buffer);
     110    stripwhite (buffer);
     111    fprintf (stdout, "%s  ", buffer);
     112  }
     113  fprintf (stdout, "\n");
     114  gfits_free_header (header);
     115  return (GotField);
     116}
Note: See TracChangeset for help on using the changeset viewer.