###CHECKPOINT ARGS:  -nolog-flow -nolog-blocks -maxcmds=150
program "%Z% MODULE: %M% RELEASE: %R%.%L% CREATED: %G% %U%"
source "/mdisw/cfl/flight/src/pztff_fd_obs_v07.src"
descr "pztff_fd_obs - execute a flat-field using  PZT's"
# program pztff_fd_obs.src, created 95.12.14 Igor Zayer 
#	951221  (IZ) modified to run properly in queuing mode, using
#		     dummy commands to set short waits.
#	960123  (IZ) reduced wait times (compressed downlink),
#		     removed dummy cmds, since cadence = 3sec/cmd,
#		     removed intermediate PZT zero offsets,
#		     set PZT's back to their fixed-value home (=64) 
#		     modified PZT offsets according to Luiz Sa,
#		     use mbcmtp1r parameters to label images,
#		     included necessary 'checkpoint' options.
#	960303  (IZ) re-inserted intermediate PZT zero offsets,
#		     removed openlog to allow checkpoint to do it.
#
# 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 OBSmode, but 
# the mode is NOT being checked here - You get what You asked for.
#
# Calls: checkiss, log_iss_par
# ------
#
# 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_obs"
write  !GS9 "Executing procedure pztff_fd_obs :"
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 0x8030 0 4000
tcb mbdummy

tcb mbcmtp3r 0x8030 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 0x8030 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)

## MAKE SURE WE HAVE DEDICATED NRT FOR NEXT 5 MINUTES
beep
beep
beep
menu !LS0 "MAKE SURE WE HAVE DEDICATED NRT FOR NEXT 5MIN!!!" "OK" "ABORT"
if !LS0 = "ABORT" then 
  goto done
end if

tcblock mbltaofs 0
tcblock mbltbofs 0
tcblock mbltcofs 0
tcblock mbltaofs 63
tcblock mbltbofs 68
tcblock mbltcofs 68
tcb mbsqreg 0  1               ## tcblock mbipldin 0x140 3072  1
tcb mbcmtp1r 0x8030 1 !GI0

tcblock mbltaofs 0
tcblock mbltbofs 0
tcblock mbltcofs 0
tcblock mbltaofs 70
tcblock mbltbofs 55
tcblock mbltcofs 64
tcb mbsqreg 0  2
tcb mbcmtp1r 0x8030 2 !GI0

tcblock mbltaofs 0
tcblock mbltbofs 0
tcblock mbltcofs 0
tcblock mbltaofs 70
tcblock mbltbofs 59
tcblock mbltcofs 59
tcb mbsqreg 0  3
tcb mbcmtp1r 0x8030 3 !GI0

tcblock mbltaofs 0
tcblock mbltbofs 0
tcblock mbltcofs 0
tcblock mbltaofs 63
tcblock mbltbofs 68
tcblock mbltcofs 58
tcb mbsqreg 0  4
tcb mbcmtp1r 0x8030 4 !GI0

tcblock mbltaofs 0
tcblock mbltbofs 0
tcblock mbltcofs 0
tcblock mbltaofs 41
tcblock mbltbofs 65
tcblock mbltcofs 84
tcb mbsqreg 0  5
tcb mbcmtp1r 0x8030 5 !GI0

tcblock mbltaofs 0
tcblock mbltbofs 0
tcblock mbltcofs 0
tcblock mbltaofs 56
tcblock mbltbofs 48
tcblock mbltcofs 85
tcb mbsqreg 0  6
tcb mbcmtp1r 0x8030 6 !GI0

tcblock mbltaofs 0
tcblock mbltbofs 0
tcblock mbltcofs 0
tcblock mbltaofs 93
tcblock mbltbofs 34
tcblock mbltcofs 62
tcb mbsqreg 0  7
tcb mbcmtp1r 0x8030 7 !GI0

tcblock mbltaofs 0
tcblock mbltbofs 0
tcblock mbltcofs 0
tcblock mbltaofs 100
tcblock mbltbofs 49
tcblock mbltcofs 40
tcb mbsqreg 0  8
tcb mbcmtp1r 0x8030 8 !GI0

tcblock mbltaofs 0
tcblock mbltbofs 0
tcblock mbltcofs 0
tcblock mbltaofs 48
tcblock mbltbofs 94
tcblock mbltcofs 47
tcb mbsqreg 0  9
tcb mbcmtp1r 0x8030 9 !GI0

tcblock mbltaofs 0
tcblock mbltbofs 0
tcblock mbltcofs 0
tcblock mbltaofs 4
tcblock mbltbofs 93
tcblock mbltcofs 93
tcb mbsqreg 0  10 
tcb mbcmtp1r 0x8030 10 !GI0

tcblock mbltaofs 0
tcblock mbltbofs 0
tcblock mbltcofs 0
tcblock mbltaofs 48
tcblock mbltbofs 28
tcblock mbltcofs 113
tcb mbsqreg 0  11
tcb mbcmtp1r 0x8030 11 !GI0

tcblock mbltaofs 0
tcblock mbltbofs 0
tcblock mbltcofs 0
tcblock mbltaofs 85
tcblock mbltbofs 10
tcblock mbltcofs 94
tcb mbsqreg 0  12
tcb mbcmtp1r 0x8030 12 !GI0

tcblock mbltaofs 0
tcblock mbltbofs 0
tcblock mbltcofs 0
tcblock mbltaofs 127
tcblock mbltbofs 27
tcblock mbltcofs 27
tcb mbsqreg 0  13
tcb mbcmtp1r 0x8030 13 !GI0

tcblock mbltaofs 0
tcblock mbltbofs 0
tcblock mbltcofs 0
tcblock mbltaofs 70
tcblock mbltbofs 102
tcblock mbltcofs 17
tcb mbsqreg 0  14
tcb mbcmtp1r 0x8030 14 !GI0


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

beep
beep
beep
display "You can SHARE NRT again now..."

## NOW DOWNLINK...
tcb mbsqreg 0  15
tcb mbcmtp1r 0x8030 0 !GI0
wait time 50
tcb mbsqreg 0  16
tcb mbcmtp1r 0x8030 0 !GI0
wait time 50
tcb mbsqreg 0  17
tcb mbcmtp1r 0x8030 0 !GI0
wait time 50
tcb mbsqreg 0  18
tcb mbcmtp1r 0x8030 0 !GI0
wait time 50
tcb mbsqreg 0  19
tcb mbcmtp1r 0x8030 0 !GI0
wait time 50
tcb mbsqreg 0  20
tcb mbcmtp1r 0x8030 0 !GI0
wait time 50
tcb mbsqreg 0  21
tcb mbcmtp1r 0x8030 0 !GI0
wait time 50
tcb mbsqreg 0  22
tcb mbcmtp1r 0x8030 0 !GI0
wait time 50
tcb mbsqreg 0  23
tcb mbcmtp1r 0x8030 0 !GI0
wait time 50
tcb mbsqreg 0  24
tcb mbcmtp1r 0x8030 0 !GI0
wait time 50
tcb mbsqreg 0  25
tcb mbcmtp1r 0x8030 0 !GI0
wait time 50
tcb mbsqreg 0  26
tcb mbcmtp1r 0x8030 0 !GI0
wait time 50
tcb mbsqreg 0  27
tcb mbcmtp1r 0x8030 0 !GI0
wait time 50
tcb mbsqreg 0  28
tcb mbcmtp1r 0x8030 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 0x8030 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_obs : %s" !TIME
writen !GS9 " "
display "Finished Procedure pztff_fd_obs                                   "
return
