[fitsbits] stupid shell tricks

Steve Allen sla at ucolick.org
Mon Oct 23 17:47:35 EDT 2006


Many years ago, before all the perl and Tcl and other scripting
languages, I produced a little unix-oriented script for extracting
FITS keyword values from the PHDU of a file.  While hunting for
limited bandwidth issues on one of our CCD controllers it just became
relevant to be able to extract keyword values from the XHDUs.

With apologies to Dave Letterman's "Stupid Pet Tricks" for the subject
line, I submit the attached script for the amusement of FITSbits.

--
Steve Allen                 <sla at ucolick.org>                WGS-84 (GPS)
UCO/Lick Observatory        Natural Sciences II, Room 165    Lat  +36.99858
University of California    Voice: +1 831 459 3046           Lng -122.06014
Santa Cruz, CA 95064        http://www.ucolick.org/~sla/     Hgt +250 m
-------------- next part --------------
#! /bin/sh
#       A little bourne shell script which acts like grep for FITS keywords.
#       Author: Steve Allen <sla at ucolick.org> 1993/2006
#       It takes as input a FITS file, and prints out the FITS key
#       values associated with the given FITS keyword.
#       Could be enhanced to identify the XTENSIONs, and to optionally print
#       the keyword itself and/or the comment field, but by that time
#       you should seriously be thinking of using perl, Tcl, python,
#       or some other more robust scripting language.

usage() {
    echo "Usage: $0 FITSkeyword [FITSfile ...]" >&2
    exit 1
}

if [ $# -lt 1 ]; then
    echo did not find any FITSkeyword
    usage
fi

# save the first (non-switch) argument (it should be the keyword)
fitskey=$1
#echo fitskey is '"'$fitskey'"'
# throw away the FITS keyword in the first (non-switch) argument
shift

# a legal FITS key must be 8 characters long with explicit trailing spaces
# the awk script ensures that arrangement
fitskey8=`echo $fitskey | awk '{printf("%-8.8s",$1)}'`
#echo fitskey8 is '"'$fitskey8'"'

awkscript="
BEGIN                   { h = 1; ht = \"PHDU\"; tp = 0; xn = \"\" }
/^XTENSION=/            { h = 1; ht = \"XHDU\"; tp = 0; xn = \"\"; next }
/^END */                { h = 0 }
			{ if ( ! h ) next }
			{
			    # string-valued
			    i = match( \$0, \"^${fitskey8}= *('.*[^']')\", ma )
			    if ( ! i ) {
				# non-string-valued
				i = match( \$0, \"^${fitskey8}= *([^/]*)\", ma )
			    }
			    if ( i ) {
				if ( ! tp ) {
				    printf( \"%s %s\\n\", ht, xn )
				    tp = 1
				}
				print ma[1]
				next
			    }
			}
/^EXTNAME =/            {
			  # find the EXTNAME value
			  i = match( \$0, \"[^']*'(.*[^'])'.*\", ma );
			  if ( i ) xn = ma[1]
			}
"
#echo awkscript is "$awkscript"

if [ -z "$*" ] ; then
    # act like a pipeline element
    (   dd conv=unblock cbs=80  | \
	awk "$awkscript" \
    ) 2>/dev/null
else
    for file in $* ; do
	keyval=`dd cbs=80 conv=unblock if=$file 2>/dev/null | \
	awk "$awkscript"`
	if [ ! -z "$keyval" ]; then
	    if [ $# -gt 1 ]; then
		echo "${file}: ${keyval}"
	    else
		echo "$keyval"
	    fi
	fi
    done
fi


More information about the fitsbits mailing list