View on GitHub

IRAF Community Distribution

IRAF maintained by the community

Home | Installation | Packages | X11IRAF | PyRAF | Forum

Making a list of images based on the existence of a keyword

Frank Valdes wrote on Nov 18, 1999

Q: Is it possible to test for the existence of a keyword in the header of a list
of images?  I am trying to collect a list of filenames based on whether the
header contains a particular keyword or not.  This seems like a standard job
for HSELECT, however, I can't find any expression to use as a test for keyword
existence.

For example, let's say I want to list all images that have the keyword "OBSMAG"
in the header; the syntax would be something like:
   hselect *.imh "$I" "OBSMAG != ''"
However, as soon as the task comes across the first image in the list that
doesn't contain the keyword OBSMAG, it crashes with:

ERROR: Image header parameter not found

and terminates the task.  It seems that HSELECT will crash in any situation
where a non-existing keyword is used in the EXPR parameter of the task.

I know you can use IMGETS to check the existence, e.g., 

   imgets (img, "OBSMAG", >& "dev$null")
   if (imgets.value != "0") {..blah...blah...blah...

but IMGETS will work on only one file at a time, so this would involve some
additional hassle in the script (going through a while loop, etc.) doing
something that HSELECT seems well-designed to do in a single command {and could
be asily done on the command line}.

If you have any suggestions, I'd appreciate it.



A: HSELECT really should allow checking on the existence of a keyword but,
as you found, it does not.  There are a number of ways to accomplish this
but not in one line.  Using IMGETS in a loop is one way.  Note that
HSELECT can be used like IMGETS with a construct like

	hselect (img, "$I,OBSMAG", yes) | scan (imname, value)
	if (nscan() == 2)
	    printf ("%s\n", >> "exists")
	   
	   
But you need either a loop on the images or write the hselect output
in one call to a file and then a loop to scan the file for lines with
two items.

One clever way I just figured out to get a one line command is to use MATCH
and capitalize on the tab put in by HSELECT when the keyword is found:

	hselect *.imh $I,OBSMAG yes | match "	" STDIN | fields STDIN 1 > list
	
(Note the argument in match is a TAB and not spaces).  With MATCH you
can also select lines without the tab to get the images without
the keyword and then FIELDS would not be needed.

Mike Fitzpatrick wrote on Nov 18, 1999

        I'm sure there will be other suggestions as well, but note that 
since the V2.11 image format stores actual strings in the header one really
sleazy way to to what you want is to simply use grep, for instance

        !grep -l ^OBSMAG *.imh

will print the filename for only those files with an OBSMAG keyword.  The
'^' is needed to match at the beginning of a line and care should be taken
to avoid strings like "RA" which might unintentionally match "RADIUS" but
it's a one-liner.  This will *not* work for V2.10-format OIF images but
will work with FITS if you skip the '^' part of the match.

Cheers,
-Mike

Last post on Nov 18, 1999