Name: malign-acq
Directory: /kroot/kss/nirspec/csh
Purpose: Imalign acquisition script.


#!/bin/csh -f
#
# malign-acq
#
#    Takes SCAM images in pmfm for ingestion into Malign, in order
#    to piston and tilt the secondary mirror.  Files are repeatedly written
#    as scam-350a.fits, scam-350b.fits, scam+350a.fits, scam+350b.fits,
#    to directory /sdata600/nirspec/malign.
#
# Version 1: 1mar99 JAL
# Version 2: 2jun99 TAB
#
# options: { stack }:  To take stacked image at completion.
#
###########################################################################

# For remote ops 2.
setenv DISPLAY hanauma:0.1
source /kroot/kss/nirspec/ui/idllib/idl51_setup

set sim = `show -s dcs2 SIMULATE`
if ($sim[3] == "true") then
	malign_dcs.tk
endif

malign.tk
modify -s nirspec itime2 = 2
modify -s nirspec coadds2 = 10

# Offset telescope.
echo ""
echo " Offsetting telescope (10,-10) arcsec from PO REF..."
modify -s dcs2 instxoff = 10.0 instyoff = -10.0 rel2curr=t
waitfor -s dcs2 axestat = tracking

#  Handle Ctrl-C.
onintr reset

# Take a sky frame, using the nod parameters.
echo ""
echo " Taking sky frame with nods..."
set nodn = 40
set node = 0
/home/waimea/nirspec/dcsexamples/en $node $nodn
modify -s nirspec comment="malign sky"
modify -s nirspec go2=1
wfg2
cp /tmp/TEMPFITS2 /tmp/buf2.fits
/home/waimea/nirspec/dcsexamples/en `echo 0 - $node | bc -l` `echo 0 - $nodn | bc -l`

# Put ACS in +pmfm.
echo ""
echo " Setting ACS in +pmfm..."
modify -s nirspec comment="malign +350 pmfm"
modify -s acs2 pmfm = 350
sleep 40

reimage:

# Take first +pmfm frame.
modify -s nirspec go2=1
wfg2
cp /tmp/TEMPFITS2 /tmp/buf1.fits

# Display the result and check it for flux.
echo ""
echo " The first image is displayed (with no sky subtraction); "
echo ""
echo "  To avoid saturation: ( sky counts < 200,000 DN )"
echo "                       ( segment counts < 250,000 DN )"
echo ""
echo "  Enter  to try a fainter or brighter star,"
echo ""
echo "   OR, adjust pointing, then enter  to re-image,"
echo ""
echo "   OR, enter  to continue:"

set cont = $<
if ($cont == 'n') then
 goto abort
else 
 if ($cont == 'r') then
 goto reimage
else
 goto continue
endif
 
continue:

# Invoke IDL to do the subtraction: perform sdiff = buf1 - buf2
modify -s nirspec display2=0
echo ""
echo " Invoking IDL for subtraction ..."
setenv IDL_ARG1 "/tmp/buf1.fits" 
setenv IDL_ARG2 "/tmp/buf2.fits"
idl /kroot/kss/nirspec/ui/idllib/sdiff.pro
cp /tmp/sdiff.fits /sdata600/nirspec/malign/scam+350a.fits

# Display the result sky subtracted result; same for rest of images.
modify -s nirspec dispname2="/tmp/sdiff.fits"
modify -s nirspec display2=1

echo ""
echo " Tell the OA that the first frame is done."
echo "   NIRSPEC will beep after each frame."
echo ""

# Take 2nd +pmfm frame.
modify -s nirspec go2=1
wfg2
cp /tmp/TEMPFITS2 /tmp/buf1.fits

# Invoke IDL to do the subtraction: perform sdiff = buf1 - buf2
echo " Invoking IDL for subtraction ..."
modify -s nirspec display2=0
setenv IDL_ARG1 "/tmp/buf1.fits" 
setenv IDL_ARG2 "/tmp/buf2.fits"
idl /kroot/kss/nirspec/ui/idllib/sdiff.pro
modify -s nirspec dispname2="/tmp/sdiff.fits"
modify -s nirspec display2=1
cp /tmp/sdiff.fits /sdata600/nirspec/malign/scam+350b.fits
echo "  The 2nd frame is done."
echo ""

# --
# Put ACS in -pmfm.
echo ""
echo " Setting ACS in -pmfm..."
modify -s nirspec comment="malign -350 pmfm"
modify -s acs2 pmfm = -350
sleep 40

# Take 1st -pmfm frame.
modify -s nirspec go2=1
wfg2
cp /tmp/TEMPFITS2 /tmp/buf1.fits

# Invoke IDL to do the subtraction: perform sdiff = buf1 - buf2
echo ""
echo " Invoking IDL for subtraction ..."
modify -s nirspec display2=0
setenv IDL_ARG1 "/tmp/buf1.fits" 
setenv IDL_ARG2 "/tmp/buf2.fits"
idl /kroot/kss/nirspec/ui/idllib/sdiff
modify -s nirspec dispname2="/tmp/sdiff.fits"
modify -s nirspec display2=1
cp /tmp/sdiff.fits /sdata600/nirspec/malign/scam-350a.fits
echo "  The 3rd frame is done."
echo ""

# Take 2nd -pmfm frame.
modify -s nirspec go2=1
wfg2
cp /tmp/TEMPFITS2 /tmp/buf1.fits

# Invoke IDL to do the subtraction: perform sdiff = buf1 - buf2
echo " Invoking IDL for subtraction ..."
modify -s nirspec display2=0
setenv IDL_ARG1 "/tmp/buf1.fits" 
setenv IDL_ARG2 "/tmp/buf2.fits"
idl /kroot/kss/nirspec/ui/idllib/sdiff
modify -s nirspec dispname2="/tmp/sdiff.fits"
modify -s nirspec display2=1
cp /tmp/sdiff.fits /sdata600/nirspec/malign/scam-350b.fits
echo "  The last frame is done."
echo ""

# Set SCAM exposure to itime2=1, coadds2=1
echo ""
echo " Setting SCAM exposure parameters to 1 s, 1 coadd..."
modify -s nirspec itime2 = 1
modify -s nirspec coadds2 = 1

# Return ACS to pmfm = 0
echo ""
echo " Resetting ACS pmfm to 0..."
modify -s acs2 pmfm = 0

# Take stacked image if requested.
if ("$1" == "stack") then
   sleep 40
    echo ""
    echo " Taking stacked image..."
    modify -s nirspec itime2=0.100
    modify -s nirspec coadds2=50
    set node = `show -terse -s nirspec node`
    set nodn = `show -terse -s nirspec nodn`
    modify -s nirspec node=7
    modify -s nirspec nodn=7
    snapi
   # Restore node and nodn
    modify -s nirspec node=$node
    modify -s nirspec nodn=$nodn
endif

# Offset telescope back.
echo ""
echo " Offsetting telescope (-10,10) arcsec back to PO REF..."
modify -s dcs2 instxoff = -10.0 instyoff = 10.0 rel2curr=t
waitfor -s dcs2 axestat = tracking

malign_done.tk
sleep 10
exit 0

abort:

# Set SCAM exposure to itime2=1, coadds2=1
echo ""
echo " Setting SCAM exposure parameters to 1 s, 1 coadd..."
modify -s nirspec itime2 = 1
modify -s nirspec coadds2 = 1

# Check for pmfm=0.
set pmfm = `show -s acs2 pmfm`
set pmfm = `echo $pmfm[3] | sed "s/\..*//"`
if ($pmfm != 0) then
 echo ""
 echo " Resetting ACS pmfm to 0..."
 modify -s acs2 pmfm = 0
endif

# Offset telescope back.
echo ""
echo " Offsetting telescope (-10,10) arcsec back to PO REF..."
modify -s dcs2 instxoff = -10.0 instyoff = 10.0 rel2curr=t
waitfor -s dcs2 axestat = tracking

malign_done.tk
sleep 10
exit 0