KEYRENAME (rename FITS keywords including HIERARCH keywords)
Frank Valdes wrote on May 08, 2000
# KEYRENAME - Rename FITS keywords.
#
# A keyword translation file of the form
#
# xxxxxxxxx=yyyyyyy
#
# is used to translate FITS keywords in the input text file to create an
# output text file. All lines with no translation are left unchanged. The
# file to be translated is often generated by the HFIX task from an image
# header. Note that if the input and output filenames are the same then the
# modified version will replace the input version when the task completes.
#
# The string
# to the left of the equal sign is matched exactly against the string to
# the left of the equal sign in the input file being edited. Note that blanks
# are significant. A common example is translating the ESO HIERARCH
# keywords using a translation file such as
#
# HIERARCH ESO GEN EXPO TYPE = IMAGETYP
# HIERARCH ESO INS OPTI-1 NAME= FILTER1
#
# If a match is found the string to the right of the equal sign is
# substituted. The substitution will be just the first word and it will be
# padded to the 8 character FITS keyword length. In the above example
# the input and output lines will look like
#
# in: HIERARCH ESO GEN EXPO TYPE = 'SCI' / Type of exposure
# in: HIERARCH ESO INS OPTI-1 NAME= 'ESO U' / Filter name
#
# out: IMAGETYP= 'SCI' / Type of exposure
# out: FILTER1 = 'ESO U' / Filter name
#
# This task is commonly used with the task HFIX in a command such as:
#
# cl> hfix eso.fits command="keyrename $fname $fname trans.dat"
#
# To define this task, copy it to your home directory. Then in your login.cl
# or loginuser.cl files or interactive enter:
#
# task keyrename = home$keyrename.cl
#
procedure keyrename (input, output, translation)
file input {prompt="Input filename"}
file output {prompt="Output filename"}
file translation {prompt="Translation filename"}
struct *fd1, *fd2
begin
file in, out, trans, tmp
string key1, value
struct line, key
int idx
# Read query parameters.
in = input
out = output
trans = translation
# Set temporary file if the input and output filenames are the same.
if (in == out)
tmp = mktemp ("tmp$iraf")
else
tmp = out
# Scan each line in the input and translate.
fd1 = in
while (fscan (fd1, line) != EOF) {
# Find the equal sign and pass lines without one.
idx = stridx ("=", line)
if (idx == 0) {
printf ("%s\n", line, >> tmp)
next
}
# Extract the keyword part and the value part.
key = substr (line, 1, idx-1)
value = substr (line, idx, 80)
# Search for match in the translation file.
fd2 = trans
while (fscan (fd2, line) != EOF) {
idx = stridx ("=", line)
if (idx == 0)
next
key1 = substr (line, 1, idx-1)
if (key1 == key) {
key1 = substr (line, idx+1, 80)
# Strip whitespace and format to 8 characters.
printf ("%s\n", key1) | scan (key1)
printf ("%-8.8s\n", key1) | scan (key)
break
}
}
fd2 = ""
# Output line with translation if one was found.
printf ("%s%s\n", key, value, >> tmp)
}
fd1 = ""
# Replace input with output if the names are the same.
if (in == out) {
delete (in, verify-)
rename (tmp, out, field="all")
}
end
Last post on May 08, 2000