#!/bin/bash
set +x
#
# This script converts Suzaku V1.x HXD WEL files
# to Suzaku V2.x HXD WEL files. 
#
# convert_hxd_v1_v2.bash input_file output_file
#
# The input file should have had a name originally of the form
# ae{observation#}hxd_N_wel_uf.evt but this is not checked.
# This will be converted to the output file name provided.

export HEADASNOQUERY=1

# Verify the tools exist
if [ -z "$HEADAS" -o -z "$PFILES" ]; then
    echo "HEADAS/PFILES not defined! Abort!"
    exit 1
fi

# Verify the input
input_file=$1
output_file=$2

if [ -z "$input_file" ]; then
    echo "Usage: convert_hxd_v1_v2.bash input_file output_file"
    echo "input_file = HXD V1 WEL unfiltered file"
    echo "output_file = HXD V2 WEL unfiltered file"
    echo "The output_file should not exist; this is to protect against accidental overwrites."
    exit 1
fi

if [ ! -r "$input_file" ]; then
    echo "INPUT FILE $input_file IS NOT READABLE! ABORT!"
    exit 1
fi

if [ -z "$output_file" ]; then
    echo "NO OUTPUT FILE SPECIFIED! ABORT!"
    exit 1
fi

if [ -e "$output_file" ]; then
    echo "OUTPUT FILE $output_file ALREADY EXISTS! RM AND TRY AGAIN. ABORT!"
    exit 1
fi

# Check that the input is NOT already in V2 format
#
fkeypar $input_file HXD_FVER
exist=$(pget fkeypar.par exist)
if [ "$exist" == "yes" ]; then
    echo "$input_file is already in V2 format! Abort!"
    exit 1
fi

#
# Create the keyword changes file
touch fmodhead.tmp
if [ $? -ne 0 ]; then
    echo "UNABLE TO WRITE TMP FILES IN CURRENT DIR! ABORT!"
    exit 1
fi

#
# 1st stage changes
#
echo "TLMIN13 = -100.0" > fmodhead.tmp
echo "TLMAX13 =  100.0" >> fmodhead.tmp
echo "TTYPE20 = 'UPI_FAST'" >> fmodhead.tmp
echo "TTYPE21 = 'UPI_SLOW'" >> fmodhead.tmp
echo "TTYPE32 = 'UPI_PIN '" >> fmodhead.tmp
echo "TTYPE24 = 'UPI_PIN0'" >> fmodhead.tmp
echo "TTYPE26 = 'UPI_PIN1'" >> fmodhead.tmp
echo "TTYPE28 = 'UPI_PIN2'" >> fmodhead.tmp
echo "TTYPE30 = 'UPI_PIN3'" >> fmodhead.tmp
echo "HXD_FVER= 2" >> fmodhead.tmp

#
# 1st stage file copy
#
if [ "${input_file}" != "${input_file%.gz}" ]; then
    # Input file is compressed
    echo "WARNING: ${input_file} is compressed!"
    gunzip -c ${input_file} > ${input_file%.gz}.1
    input_file=${input_file%.gz}
    if [ $? -ne 0 ]; then
        echo "UNABLE TO GUNZIP ${input_file}! ABORT!"
        exit 1
    fi
else
    cp -p $input_file $input_file.1
fi

fmodhead "$input_file.1[1]" fmodhead.tmp
if [ $? -ne 0 ]; then
    echo "UNABLE TO MODIFY KEYWORDS IN 1ST STAGE COPY! ABORT!"
    exit 1
fi
current_input=$input_file.1

#
# 2nd stage copy
#
rm -f $input_file.2
ftcalc "${current_input}[1]" "${input_file}.2" "PI_FAST" "min(511,UPI_FAST+0.5)" tform=1I
if [ $? -ne 0 ]; then
    echo "UNABLE TO fcopy create $input_file.2! ABORT!"
    exit 1
fi
rm -f $current_input
current_input=$input_file.2

#
# 3rd stage copy
#
rm -f $input_file.3
ftcalc "${current_input}+1" "${input_file}.3" "PI_SLOW" "min(511,UPI_SLOW+0.5)" tform=1I
if [ $? -ne 0 ]; then
    echo "UNABLE TO fcopy create $input_file.3! ABORT!"
    exit 1
fi
rm -f $current_input
current_input=$input_file.3

#
# 4th stage copy
#
rm -f $input_file.4
ftcalc "${current_input}+1" "${input_file}.4" "PI_PIN" "min(255,UPI_PIN+0.5)" tform=1B
if [ $? -ne 0 ]; then
    echo "UNABLE TO fcopy create $input_file.4! ABORT!"
    exit 1
fi
rm -f $current_input
current_input=$input_file.4

#
# 5th stage copy
#
rm -f $input_file.5
ftcalc "${current_input}+1" "${input_file}.5" "PI_PIN0" "min(255,UPI_PIN0+0.5)" tform=1B
if [ $? -ne 0 ]; then
    echo "UNABLE TO fcopy create $input_file.5! ABORT!"
    exit 1
fi
rm -f $current_input
current_input=$input_file.5

#
# 6th stage copy
#
rm -f $input_file.6
ftcalc "${current_input}+1" "${input_file}.6" "PI_PIN1" "min(255,UPI_PIN1+0.5)" tform=1B
if [ $? -ne 0 ]; then
    echo "UNABLE TO fcopy create $input_file.6! ABORT!"
    exit 1
fi
rm -f $current_input
current_input=$input_file.6

#
# 7th stage copy
#
rm -f $input_file.7
ftcalc "${current_input}+1" "${input_file}.7" "PI_PIN2" "min(255,UPI_PIN2+0.5)" tform=1B
if [ $? -ne 0 ]; then
    echo "UNABLE TO fcopy create $input_file.7! ABORT!"
    exit 1
fi
rm -f $current_input
current_input=$input_file.7

#
# 8th stage copy
#
rm -f $input_file.8
ftcalc "${current_input}+1" "${input_file}.8" "PI_PIN3" "min(255,UPI_PIN3+0.5)" tform=1B
if [ $? -ne 0 ]; then
    echo "UNABLE TO fcopy create $input_file.8! ABORT!"
    exit 1
fi
rm -f $current_input
current_input=$input_file.8

# 
# Final name
#
mv $current_input $output_file

#
# Final keywords
#
echo "TLMIN33 = 0" > fmodhead.tmp
echo "TLMAX33 = 511" >> fmodhead.tmp
echo "TUNIT33 = 'chan'" >> fmodhead.tmp
echo "TLMIN34 = 0"   >> fmodhead.tmp
echo "TLMAX34 = 511" >> fmodhead.tmp
echo "TUNIT34 = 'chan'" >> fmodhead.tmp
echo "TLMIN35 = 0"   >> fmodhead.tmp
echo "TLMAX35 = 255" >> fmodhead.tmp
echo "TUNIT35 = 'chan'" >> fmodhead.tmp
echo "TLMIN36 = 0"   >> fmodhead.tmp
echo "TLMAX36 = 255" >> fmodhead.tmp
echo "TUNIT36 = 'chan'" >> fmodhead.tmp
echo "TLMIN37 = 0"   >> fmodhead.tmp
echo "TLMAX37 = 255" >> fmodhead.tmp
echo "TUNIT37 = 'chan'" >> fmodhead.tmp
echo "TLMIN38 = 0"   >> fmodhead.tmp
echo "TLMAX38 = 255" >> fmodhead.tmp
echo "TUNIT38 = 'chan'" >> fmodhead.tmp
echo "TLMIN39 = 0"   >> fmodhead.tmp
echo "TLMAX39 = 255" >> fmodhead.tmp
echo "TUNIT39 = 'chan'" >> fmodhead.tmp
fmodhead "$output_file[1]" fmodhead.tmp
if [ $? -ne 0 ]; then
    echo "UNABLE TO MODIFY KEYWORDS IN LAST STAGE FMODHEAD! ABORT!"
    exit 1
fi

#
# End
#
rm -f fmodhead.tmp 
exit 0
