###CHECKPOINT ARGS:  -nostartlog -nolog-flow -nolog-blocks -queue=0
program "%Z% MODULE: %M% RELEASE: %R%.%L% CREATED: %G% %U%"
source "/mdisw/cfl/sim/src/iss_cal_pic.src"
descr "iss_cal_pic - Take exposures while offsetting PZT's"
# program iss_cal_pic.src, created 96.01.26 Igor Zayer
#
#	21-Mar-96  (IZ)	put wait statements in proper order so that
#			housekeeping has time to stabilize!!
#
#	Offsets one PZT at a time & takes exposures to calibrate
#	the error-signals vs. displacement in arcsec. Take pictures
#	in burst mode to minimize S/C drift effects. Stand-alone.
#
# Calls: checkiss, log_iss_par
# ------
#
# Variable usage:
#----------------
# !GI0: exposure time in ticks
# !GI8: 'error signal nulled' indicator (0 = not nulled, 1 = OK to lock ISS)
# !GR8: Voltage value of nulled error signal (=2.27), MUST be supplied HERE !!!
# !GS0: 'bypass limit switch check': (= "MDISIM" for testing on MDISIM)
# !GS6: 'OK to proceed with ISS' indicator (= "enable ISS" if OK)
# !GS8: name of 160k datafile
# !GS9: name of user logfile 
# 
# !LS0: answer string
#----------------

display "Executing procedure iss_cal_pic"

## OPEN A SPECIFIC LOGFILE (NO CHOICE OF NAMING)
if !GS9 <> "" then
  perf  close_log_v04
end if
setstr !LS0 = "cal_pic"
open user !LS0 dt !GS9 w
display "Logging to: %s" !GS9
write  !GS9 "Log File: %s" !GS9
writen !GS9 " "
write  !GS9 "Executing procedure iss_cal_pic :"
write  !GS9 " %s" !DATE
writen !GS9 " %s" !TIME

## GO TO NOMINAL OBSMODE
perform obsmode_1_v01

## ENABLE THE QUEUE & SET EXP. TIME
tcb mbipldin 0x0136
set !GI0 = 600

## SEE WHETHER ISS IS USEABLE
if !GS0 = "MDISIM" then	      ## skip this checking if on SIM system
  goto mode
end if
set !GR8 = 2.27
perform checkiss_v05	       ## this also logs initial ISS params.
if !GI8 <> 1 then
  if !GS6 <> "enable ISS" then
    goto done
  end if
  beep
  beep
  display " You need to null error signals first !"
  menu !LS0 "Want to null error signals?" "YES" "NO, giveup"
  if !LS0 = "NO, giveup" then 
    writen !GS9 " Error signals not nulled - giving up!"
    goto done
  end if
  perform null_err_v04
  if !GS6 <> "enable ISS" then
    writen !GS9 " Giving up!"
    goto done
  end if
end if

mode:
## MAKE SURE LOOP IS OPEN
tcb mbltopnl

## OPEN DATAFILE FOR PICTURES
open d !GS8
writen !GS9 " 160k (exposure-) data stored in: %s" !GS8

## SETUP HEADER TO USE IP PROG TAP_CONT_1X1 (= 38)
tcb mbcmhdr1 0x4040 0x4040 0 1 38

## PICTURE AT NOMINAL FIRST
writen !GS9 " FD Obs Exposure at HOME = 64"
tcb mbcmtp1r 0x8040 1 !GI0
wait time 15
perf log_iss_par_v03
#
## OFFSET PZT A & TAKE PICTURES IN FD 1X1
tcblock mbltaofs 34
writen !GS9 " FD Obs Exposure at A = 34"
tcb mbcmtp1r 0x8040 2 !GI0
wait time 15
perf log_iss_par_v03
#
tcblock mbltaofs 64
writen !GS9 " FD Obs Exposure at A = 64 = HOME"
tcb mbcmtp1r 0x8040 3 !GI0
wait time 15
perf log_iss_par_v03
#
tcblock mbltaofs 94
writen !GS9 " FD Obs Exposure at A = 94"
tcb mbcmtp1r 0x8040 4 !GI0
wait time 15
perf log_iss_par_v03
#
tcblock mbltaofs 64	## go back home
writen !GS9 " FD Obs Exposure at HOME"
tcb mbcmtp1r 0x8040 5 !GI0
wait time 15
perf log_iss_par_v03
#
## OFFSET PZT B & TAKE PICTURES IN FD 1X1
tcblock mbltbofs 34
writen !GS9 " FD Obs Exposure at B = 34"
tcb mbcmtp1r 0x8040 6 !GI0
wait time 15
perf log_iss_par_v03
#
tcblock mbltbofs 64
writen !GS9 " FD Obs Exposure at B = 64 = HOME"
tcb mbcmtp1r 0x8040 7 !GI0
wait time 15
perf log_iss_par_v03
#
tcblock mbltbofs 94
writen !GS9 " FD Obs Exposure at B = 94"
tcb mbcmtp1r 0x8040 8 !GI0
wait time 15
perf log_iss_par_v03
#
tcblock mbltbofs 64	## go back home
writen !GS9 " FD Obs Exposure at HOME"
tcb mbcmtp1r 0x8040 9 !GI0
wait time 15
perf log_iss_par_v03
#
## OFFSET PZT C & TAKE PICTURES IN FD 1X1
tcblock mbltcofs 34
writen !GS9 " FD Obs Exposure at C = 34"
tcb mbcmtp1r 0x8040 10 !GI0
wait time 15
perf log_iss_par_v03
#
tcblock mbltcofs 64
writen !GS9 " FD Obs Exposure at C = 64 = HOME"
tcb mbcmtp1r 0x8040 11 !GI0
wait time 15
perf log_iss_par_v03
#
tcblock mbltcofs 94
writen !GS9 " FD Obs Exposure at C = 94"
tcb mbcmtp1r 0x8040 12 !GI0
wait time 15
perf log_iss_par_v03
#
tcblock mbltcofs 64	## go back home
writen !GS9 " FD Obs Exposure at HOME"
tcb mbcmtp1r 0x8040 13 !GI0
wait time 30
perf log_iss_par_v03
#
## TAKE A DARK
writen !GS9 " DARK Exposure"
tcb mbcmdrk 0x8040 21 !GI0
wait time 20
#
## SET HEADER TO A SAFE QPROG_TAP_CONT_4X4BIN (=30)
tcb mbcmhdr1 0x4040 0x4040 0 1 30

done:
writen !GS9 "Finished procedure iss_cal_pic : %s" !TIME
writen !GS9 " "
display "Finished Procedure iss_cal_pic                                   "
perform close_log_v04
return
