###CHECKPOINT ARGS:  -nostartlog -nolog-flow -nolog-blocks -maxcmds=100
program "%Z% MODULE: %M% RELEASE: %R%.%L% CREATED: %G% %U%"
source "/mdisw/cfl/sim/src/pztff_fd_cal.src"
descr "pztff_fd_cal - execute a flat-field using  PZT's"
# program pztff_fd_cal.src, created 96.01.23 Igor Zayer 
#
# Moves the ISS mirror in open loop using the PZT offsets. Must be
# in open loop, checked by 'checkiss'. Except an operational ISS,
# it doesn't assume anything: You just get darks if You haven't 
# opened the door... The offsets are optimized for CALmode, but 
# the mode is NOT being checked here - You get what You asked for.
#
# Calls: open_log, checkiss, log_iss_par, close_log
# ------
#
# Variable usage:
#----------------
# !GI0: exposure time in ticks
# !GS6: 'OK to proceed with ISS' indicator (= "enable ISS" if OK)
# !GS8: name of 160k datafile
# !GS9: name of user logfile 
# 
# !LI0: loop count variable
# !LI1: original PZT A offset
# !LI2: original PZT B offset
# !LI3: original PZT C offset
# !LI4: answer variable for exp.time
# !LI5: temporarily stores !GI0
#----------------

display "Executing procedure pztff_fd_cal"
perform open_log_v05
write  !GS9 "Executing procedure pztff_fd_cal :"
write  !GS9 " %s" !DATE
writen !GS9 " %s" !TIME

## ENABLE THE QUEUE
tcb mbipldin 0x0136

## SEE WHETHER ISS IS USEABLE
perform checkiss_v05	       ## this also logs initial ISS params.
if !GS6 <> "enable ISS" then
  writen !GS9 " ISS not ready or closed loop - giving up!"
  goto done
end if

## SET EXPOSURE TIME TO FIXED 600 TICKS
set !GI0 = 600
 
## FLASH CCD FIRST (using PROG TAP_CONT_8X8BIN = 33)
tcb mbcmhdr1 0x4040 0x4040 0 1 33 
tcb mbcmtp3r 0x8031 0 4000
tcb mbdummy

tcb mbcmtp3r 0x8031 0 4000
tcb mbdummy

## OPEN DATAFILE 
open d !GS8
writen !GS9 " 160k data stored in: %s" !GS8

## TAKE A DARK (NON-TIME-CRITICAL) WITH TAP_CONT_1X1 (= 38)
tcb mbcmhdr1 0x4040 0x4040 0 1 38
tcb mbcmdrk 0x8031 21 !GI0
wait time 50

## SETUP HEADER TO USE IP PROG TAPB_14FRM (= 52)
tcb mbcmhdr1 0x4040 0x4040 0 1 52 

## SEE WHERE WE START FROM
# set !LI1 = getint(mkltaos)
# set !LI2 = getint(mkltbos)
# set !LI3 = getint(mkltcos)

tcblock mbltaofs 56
tcblock mbltbofs 48
tcblock mbltcofs 85
tcb mbsqreg 0  1               ## tcblock mbipldin 0x140 3072  1
tcb mbcmtp1r 0x8031 1 !GI0

tcblock mbltaofs 70
tcblock mbltbofs 41
tcblock mbltcofs 78
tcb mbsqreg 0  2
tcb mbcmtp1r 0x8031 2 !GI0

tcblock mbltaofs 70
tcblock mbltbofs 50
tcblock mbltcofs 69
tcb mbsqreg 0  3
tcb mbcmtp1r 0x8031 3 !GI0

tcblock mbltaofs 56
tcblock mbltbofs 67
tcblock mbltcofs 67
tcb mbsqreg 0  4
tcb mbcmtp1r 0x8031 4 !GI0

tcblock mbltaofs 26
tcblock mbltbofs 72
tcblock mbltcofs 91
tcb mbsqreg 0  5
tcb mbcmtp1r 0x8031 5 !GI0

tcblock mbltaofs 11
tcblock mbltbofs 61
tcblock mbltcofs 117
tcb mbsqreg 0  6
tcb mbcmtp1r 0x8031 6 !GI0

tcblock mbltaofs 41
tcblock mbltbofs 27
tcblock mbltcofs 121
tcb mbsqreg 0  7
tcb mbcmtp1r 0x8031 7 !GI0

tcblock mbltaofs 70
tcblock mbltbofs 12
tcblock mbltcofs 106
tcb mbsqreg 0  8
tcb mbcmtp1r 0x8031 8 !GI0

tcblock mbltaofs 115
tcblock mbltbofs 0
tcblock mbltcofs 75
tcb mbsqreg 0  9
tcb mbcmtp1r 0x8031 9 !GI0

tcblock mbltaofs 127
tcblock mbltbofs 27
tcblock mbltcofs 27
tcb mbsqreg 0  10 
tcb mbcmtp1r 0x8031 10 !GI0

tcblock mbltaofs 100
tcblock mbltbofs 73
tcblock mbltcofs 16
tcb mbsqreg 0  11
tcb mbcmtp1r 0x8031 11 !GI0

tcblock mbltaofs 56
tcblock mbltbofs 114
tcblock mbltcofs 20
tcb mbsqreg 0  12
tcb mbcmtp1r 0x8031 12 !GI0

tcblock mbltaofs 11
tcblock mbltbofs 117
tcblock mbltcofs 61
tcb mbsqreg 0  13
tcb mbcmtp1r 0x8031 13 !GI0

tcblock mbltaofs 0
tcblock mbltbofs 100
tcblock mbltcofs 100
tcb mbsqreg 0  14
tcb mbcmtp1r 0x8031 14 !GI0


## PUT OFFSETS BACK WHERE THEY WERE...
tcblock mbltaofs 64
tcblock mbltbofs 64
tcblock mbltcofs 64

## NOW DOWNLINK...
tcb mbsqreg 0  15
tcb mbcmtp1r 0x8031 0 !GI0
wait time 50
tcb mbsqreg 0  16
tcb mbcmtp1r 0x8031 0 !GI0
wait time 50
tcb mbsqreg 0  17
tcb mbcmtp1r 0x8031 0 !GI0
wait time 50
tcb mbsqreg 0  18
tcb mbcmtp1r 0x8031 0 !GI0
wait time 50
tcb mbsqreg 0  19
tcb mbcmtp1r 0x8031 0 !GI0
wait time 50
tcb mbsqreg 0  20
tcb mbcmtp1r 0x8031 0 !GI0
wait time 50
tcb mbsqreg 0  21
tcb mbcmtp1r 0x8031 0 !GI0
wait time 50
tcb mbsqreg 0  22
tcb mbcmtp1r 0x8031 0 !GI0
wait time 50
tcb mbsqreg 0  23
tcb mbcmtp1r 0x8031 0 !GI0
wait time 50
tcb mbsqreg 0  24
tcb mbcmtp1r 0x8031 0 !GI0
wait time 50
tcb mbsqreg 0  25
tcb mbcmtp1r 0x8031 0 !GI0
wait time 50
tcb mbsqreg 0  26
tcb mbcmtp1r 0x8031 0 !GI0
wait time 50
tcb mbsqreg 0  27
tcb mbcmtp1r 0x8031 0 !GI0
wait time 50
tcb mbsqreg 0  28
tcb mbcmtp1r 0x8031 0 !GI0
wait time 50

## TAKE ANOTHER DARK (NON-TIME-CRITICAL) WITH TAP_CONT_1X1 (= 38)
tcb mbcmhdr1 0x4040 0x4040 0 1 38
tcb mbcmdrk 0x8031 22 !GI0
wait time 50

## SET HEADER TO A SAFE QPROG_TAP_CONT_4X4BIN (=30)
tcb mbcmhdr1 0x4040 0x4040 0 1 30

## LOG FINAL SATE OF ISS
###sync hk###   ## this is to allow for light travel time, not HK..
perform log_iss_par_v03

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