Index: /branches/eam_branches/ipp-20110213/Ohana/src/tools/src/fhead.c
===================================================================
--- /branches/eam_branches/ipp-20110213/Ohana/src/tools/src/fhead.c	(revision 30716)
+++ /branches/eam_branches/ipp-20110213/Ohana/src/tools/src/fhead.c	(revision 30717)
@@ -1,20 +1,42 @@
 # include <ohana.h>
 # include <gfitsio.h>
+# include <regex.h>
 
 int main (int argc, char **argv) {
 
-  int N, Extend, Nextend, status;
+  int N, Extnum, Nextend, status;
   int i, j;
   off_t nbytes;
   Header head;
-  char *p;
+  char *p, *CCDKeyword, *Extname, extname[80];
+  FILE *f;
+  off_t Nbytes;
+  regex_t preg;
 
-  Extend = FALSE;
+  CCDKeyword = NULL;
+  if ((N = get_argument (argc, argv, "-keyword"))) {
+    remove_argument (N, &argc, argv);
+    CCDKeyword = strcreate (argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+  if (CCDKeyword == NULL) {
+    CCDKeyword = strcreate ("EXTNAME");
+  }
+
+  Extnum = FALSE;
   Nextend = 0;
   if ((N = get_argument (argc, argv, "-x"))) {
-    Extend = TRUE;
+    Extnum = TRUE;
     remove_argument (N, &argc, argv);
     Nextend = atoi (argv[N]);
     remove_argument (N, &argc, argv);
+  }
+
+  Extname = NULL;
+  if ((N = get_argument (argc, argv, "-n"))) {
+    remove_argument (N, &argc, argv);
+    Extname = strcreate (argv[N]);
+    remove_argument (N, &argc, argv);
+    regcomp (&preg, Extname, REG_EXTENDED);
   }
 
@@ -23,22 +45,52 @@
       fprintf (stdout, "------> %s <------\n", argv[i]);
     
-    if (Extend) {
-      status = gfits_read_Xheader (argv[i], &head, Nextend);
-    } else {
-      status = gfits_read_header (argv[i], &head);
-    }      
-
-    if (!status) continue;
-
-    for (j = 79; j < head.datasize; j+= 80) {
-      head.buffer[j] = 10;
+    status = FALSE;
+    if (!Extnum && !Extname) {
+      if (!gfits_read_header (argv[i], &head)) {
+	continue;
+      }
+    }
+    if (Extnum) {
+      if (!gfits_read_Xheader (argv[i], &head, Nextend)) {
+	continue;
+      }
+    } 
+    if (Extname) {
+      /* keep reading headers until we reach the one we want */
+      Nextend = 0;
+      f = fopen (argv[i], "r");
+      if (f == NULL) continue;
+      while (gfits_fread_header (f, &head)) {
+	/* extract the EXTNAME (or other CCDKeyword) for this component (set to PHU for 0th component) */
+	if (!gfits_scan (&head, CCDKeyword, "%s", 1, extname)) {
+	  if (Nextend == 0) {
+	    strcpy (extname, "PHU");
+	  } else {
+	    strcpy (extname, "UNKNOWN");
+	  }
+	}
+	if (!regexec (&preg, extname, 0, NULL, 0)) {
+	  goto done;
+	}
+    
+	Nbytes = gfits_data_size (&head);
+	fseeko (f, Nbytes, SEEK_CUR);
+	Nextend ++;
+      }
+      // failed to find the desired header
+      continue;
     }
 
-    p = gfits_header_field (&head, "END", 1);
-    nbytes = p - head.buffer;
-    fwrite (head.buffer, nbytes, 1, stdout);
-    gfits_free_header (&head);
+  done:
+      for (j = 79; j < head.datasize; j+= 80) {
+	head.buffer[j] = 10;
+      }
 
+      p = gfits_header_field (&head, "END", 1);
+      nbytes = p - head.buffer;
+      fwrite (head.buffer, nbytes, 1, stdout);
+      gfits_free_header (&head);
+
+    }
+    exit (0);
   }
-  exit (0);
-}
