FITS cards and Tcl regexp

Steve Allen sla at hires.ucolick.org
Thu Jan 30 16:11:06 EST 1997


Whilst massaging more FITS information into and out of databases we
have come upon the need to dissect FITS cards into components using
regular expressions in Tcl.  The tricky parts are the recognition of
strings which may contain quotes and slashes, the the recognition of
integers and floating point values which may contain embedded spaces.

Here's a little tcl code snippet demonstrating the regular expressions.
It attempts to recognize anything legal even if not in FITS fixed format.
I invite FITS cards which might be so pathological as to cause
this code to stumble.

################snip here
#! /path/to/tcl

# matches a FITS card with a recognizable string value
set cre {^(........)= ( *)'(([^']|(''))*)'([^/]*)(/ *(.*))?$}
# matches a FITS card with a recognizable integer value
set ire {^(........)=  *([-+]?[0-9 ]*[0-9]|[-+]) *(/ *(.*))?$}
# matches a FITS card with a recognizable null value
set nre {^(........)= *(/ *(.*))?$}
# matches a FITS card with a recognizable logical value
set lre {^(........)=  *([TF]) *(/ *(.*))?$}
# matches a FITS card with a recognizable floating point value
set fre {^(........)=  *([-+]?[0-9 ]*(\.[0-9 ]*E?|\.*[0-9 ]*E)( *[-+])?([0-9 ]*[0-9])?) *(/ *(.*))?$}

set file [open FITS.dat r]
while { [gets $file aline] >= 0 } {
  if { [regexp $cre $aline line keyword preblnk value ch qu extrach \
  slashcmnt trimcmnt] } {
    # FITS string
    # If preblnk is not null then the string does not start where
    # the standard requires.
    # If extrach is not null then the card contains extra characters
    # following the string before the comment; this may indicate an error.
    puts sK:$keyword:\tV:$value:\tC:$trimcmnt:\tkv:$preblnk:\tvc:$extrach:
  } elseif { [regexp $ire $aline line keyword value slashcmnt trimcmnt] } {
    # FITS integer
    puts iK:$keyword:\tV:$value:\tC:$trimcmnt:
  } elseif { [regexp $lre $aline line keyword value slashcmnt trimcmnt] } {
    # FITS logical
    puts lK:$keyword:\tV:$value:\tC:$trimcmnt:
  } elseif { [regexp $fre $aline line keyword value edot esg exp \
  slashcmnt trimcmnt] } {
    # FITS floating point
    puts fK:$keyword:\tV:$value:\tC:$trimcmnt:
  } elseif { [regexp $nre $aline line keyword slashcmnt trimcmnt] } {
    # FITS null value
    puts nM:$keyword:\tC:$trimcmnt:
  } else {
    puts NOMATCH
  }
}
close $file
################snip here
--
Steve Allen          UCO/Lick Observatory       Santa Cruz, CA 95064
sla at ucolick.org      Voice: +1 408 459 3046     FAX: +1 408 454 9863
WWW: http://www.ucolick.org/~sla                PGP public keys:  see WWW




More information about the fitsbits mailing list