#!/bin/sh
# \
exec oagwish "$0" "$@"


#
# Input directory /home/helios/oagData/PSSCPDB/req/sr
# Output directory /home/helios/oagData/PSGROUP/data

set auto_path [linsert $auto_path 0 /usr/local/oag/apps/lib/$env(HOST_ARCH)]
set auto_path [linsert $auto_path 0 /usr/local/oag/lib_patch/$env(HOST_ARCH)]

set debug 0

package require BLT

set CVSRevisionAuthor "\$Revision: 1.5 $ \$Author: soliday $"
set destination statusWindow
set status "The SR Gespac addressing test which you are about to perform requires that all SR raw supplies be on. It is recommended that the user of this test monitor ps status during the run."

proc makeDir {pathname} {
    if {[file isdirectory $pathname]} {
	return $pathname
    } elseif {[file exists $pathname]} {
	return -code error "Non-directory $pathname already exists"
    } else {
	if [catch {makeDir [file dirname $pathname]} result] {
	    return -code error $result
	}
	if [catch {exec mkdir $pathname} result] {
	    return -code error $result
	}
#	catch {exec chmod a+w $pathname}
	return $pathname
    }
}

proc devSend {device val} {
    if {[catch {exec devSend $device $val} results]} {
	APSSetVarAndUpdate status $results
    }
}

proc devSetAll {device val} {
    if {[catch {exec devSend $device set $val $val $val $val $val $val $val $val $val $val $val $val $val $val $val $val $val $val $val $val $val $val $val $val $val $val $val $val $val $val $val $val $val $val $val $val $val $val} results]} {
	APSSetVarAndUpdate status $results
    }
}

proc PSsend {args} {
    set loc ""
    set type ""
    set val off
    APSStrictParseArguments {loc type val}
    set sect_num 1

    if {($val != "on") && ($val != "off")} {
	set comm "devSetAll"
    } else {
	set comm "devSend"
    }

    if {$loc == "R"} {
	while {${sect_num} < 41} {
	    ${comm} S${sect_num}:${type} ${val} 
	    incr sect_num 2
	}
	set sect_num 2

	while {${sect_num} < 41} {
	    ${comm} S${sect_num}:${type} ${val}
	    incr sect_num 2
	}
    } elseif {$loc == "F"} {
	${comm} S36:${type} ${val}
	${comm} S38:${type} ${val}
	${comm} S40:${type} ${val}
	${comm} S37:${type} ${val}
	${comm} S39:${type} ${val}
	${comm} S1:${type} ${val}
    } elseif {$loc == "E"} {
	${comm} S30:${type} ${val}
	${comm} S32:${type} ${val}
	${comm} S34:${type} ${val}
	${comm} S31:${type} ${val}
	${comm} S33:${type} ${val}
	${comm} S35:${type} ${val}
    } elseif {$loc == "D"} {
	${comm} S24:${type} ${val}
	${comm} S26:${type} ${val}
	${comm} S28:${type} ${val}
	${comm} S25:${type} ${val}
	${comm} S27:${type} ${val}
	${comm} S29:${type} ${val}
    } elseif {$loc == "C"} {
	${comm} S18:${type} ${val}
	${comm} S20:${type} ${val}
	${comm} S22:${type} ${val}
	${comm} S19:${type} ${val}
	${comm} S21:${type} ${val}
	${comm} S23:${type} ${val}
    } elseif {$loc == "B"} {
	${comm} S10:${type} ${val}
	${comm} S12:${type} ${val}
	${comm} S14:${type} ${val}
	${comm} S16:${type} ${val}
	${comm} S11:${type} ${val}
	${comm} S13:${type} ${val}
	${comm} S15:${type} ${val}
	${comm} S17:${type} ${val}
    } elseif {$loc == "A"} {
	${comm} S2:${type} ${val}
	${comm} S4:${type} ${val}
	${comm} S6:${type} ${val}
	${comm} S8:${type} ${val}
	${comm} S3:${type} ${val}
	${comm} S5:${type} ${val}
	${comm} S7:${type} ${val}
	${comm} S9:${type} ${val}
    }
}

proc PSqtsend {args} {
    set loc ""
    set qt ""
    set val ""
    APSStrictParseArguments {loc qt val}
    set sect_num 1

    if {($val != "on") && ($val != "off")} {
	set comm "devSetAll"
    } else {
	set comm "devSend"
    }

    if {$qt <= 1} {
	set cab1 "A:H2"
	set cab2 "A:V3"
	set cab3 "B:H4"
	set cab4 "B:V3"
	set cab5 "B:H1"
    } elseif {$qt == 2} {
	set cab1 "A:S1"
	set cab2 "A:S3"
	set cab3 "A:S4"
	set cab4 "B:S2"
	set cab5 "B:Q2"
    } elseif {$qt == 3} {
	set cab1 "A:H1"
	set cab2 "A:H3"
	set cab3 "A:H4"
	set cab4 "B:H3"
	set cab5 "B:H2"
    } elseif {$qt == 4} {
	set cab1 "A:Q2"
	set cab2 "A:S2"
	set cab3 "A:Q4"
	set cab4 "B:S3"
	set cab5 "B:S1"
    }

    if {$loc == "R"} {
	while {${sect_num} < 41} {
	    ${comm} S${sect_num}${cab1} ${val}
	    ${comm} S${sect_num}${cab2} ${val}
	    ${comm} S${sect_num}${cab3} ${val}
	    ${comm} S${sect_num}${cab4} ${val}
	    ${comm} S${sect_num}${cab5} ${val}
	    incr sect_num 2
	}
	set sect_num 2

	while {${sect_num} < 41} {
	    ${comm} S${sect_num}${cab1} ${val} 
	    ${comm} S${sect_num}${cab2} ${val}
	    ${comm} S${sect_num}${cab3} ${val}
	    ${comm} S${sect_num}${cab4} ${val}
	    ${comm} S${sect_num}${cab5} ${val}
	    incr sect_num 2
	}
    } elseif {$loc == "F"} {
	${comm} S36${cab1} ${val}
	${comm} S36${cab2} ${val}
	${comm} S36${cab3} ${val}
	${comm} S36${cab4} ${val}
	${comm} S36${cab5} ${val}
	${comm} S38${cab1} ${val}
	${comm} S38${cab2} ${val}
	${comm} S38${cab3} ${val}
	${comm} S38${cab4} ${val}
	${comm} S38${cab5} ${val}
	${comm} S40${cab1} ${val}
	${comm} S40${cab2} ${val}
	${comm} S40${cab3} ${val}
	${comm} S40${cab4} ${val}
	${comm} S40${cab5} ${val}
	${comm} S37${cab1} ${val}
	${comm} S37${cab2} ${val}
	${comm} S37${cab3} ${val}
	${comm} S37${cab4} ${val}
	${comm} S37${cab5} ${val}
	${comm} S39${cab1} ${val}
	${comm} S39${cab2} ${val}
	${comm} S39${cab3} ${val}
	${comm} S39${cab4} ${val}
	${comm} S39${cab5} ${val}
	${comm} S1${cab1} ${val}
	${comm} S1${cab2} ${val}
	${comm} S1${cab3} ${val}
	${comm} S1${cab4} ${val}
	${comm} S1${cab5} ${val}
    } elseif {$loc == "E"} {
	${comm} S30${cab1} ${val}
	${comm} S30${cab2} ${val}
	${comm} S30${cab3} ${val}
	${comm} S30${cab4} ${val}
	${comm} S30${cab5} ${val}
	${comm} S32${cab1} ${val}
	${comm} S32${cab2} ${val}
	${comm} S32${cab3} ${val}
	${comm} S32${cab4} ${val}
	${comm} S32${cab5} ${val}
	${comm} S34${cab1} ${val}
	${comm} S34${cab2} ${val}
	${comm} S34${cab3} ${val}
	${comm} S34${cab4} ${val}
	${comm} S34${cab5} ${val}
	${comm} S31${cab1} ${val}
	${comm} S31${cab2} ${val}
	${comm} S31${cab3} ${val}
	${comm} S31${cab4} ${val}
	${comm} S31${cab5} ${val}
	${comm} S33${cab1} ${val}
	${comm} S33${cab2} ${val}
	${comm} S33${cab3} ${val}
	${comm} S33${cab4} ${val}
	${comm} S33${cab5} ${val}
	${comm} S35${cab1} ${val}
	${comm} S35${cab2} ${val}
	${comm} S35${cab3} ${val}
	${comm} S35${cab4} ${val}
	${comm} S35${cab5} ${val}
    } elseif {$loc == "D"} {
	${comm} S24${cab1} ${val}
	${comm} S24${cab2} ${val}
	${comm} S24${cab3} ${val}
	${comm} S24${cab4} ${val}
	${comm} S24${cab5} ${val}
	${comm} S26${cab1} ${val}
	${comm} S26${cab2} ${val}
	${comm} S26${cab3} ${val}
	${comm} S26${cab4} ${val}
	${comm} S26${cab5} ${val}
	${comm} S28${cab1} ${val}
	${comm} S28${cab2} ${val}
	${comm} S28${cab3} ${val}
	${comm} S28${cab4} ${val}
	${comm} S28${cab5} ${val}
	${comm} S25${cab1} ${val}
	${comm} S25${cab2} ${val}
	${comm} S25${cab3} ${val}
	${comm} S25${cab4} ${val}
	${comm} S25${cab5} ${val}
	${comm} S27${cab1} ${val}
	${comm} S27${cab2} ${val}
	${comm} S27${cab3} ${val}
	${comm} S27${cab4} ${val}
	${comm} S27${cab5} ${val}
	${comm} S29${cab1} ${val}
	${comm} S29${cab2} ${val}
	${comm} S29${cab3} ${val}
	${comm} S29${cab4} ${val}
	${comm} S29${cab5} ${val}
    } elseif {$loc == "C"} {
	${comm} S18${cab1} ${val}
	${comm} S18${cab2} ${val}
	${comm} S18${cab3} ${val}
	${comm} S18${cab4} ${val}
	${comm} S18${cab5} ${val}
	${comm} S20${cab1} ${val}
	${comm} S20${cab2} ${val}
	${comm} S20${cab3} ${val}
	${comm} S20${cab4} ${val}
	${comm} S20${cab5} ${val}
	${comm} S22${cab1} ${val}
	${comm} S22${cab2} ${val}
	${comm} S22${cab3} ${val}
	${comm} S22${cab4} ${val}
	${comm} S22${cab5} ${val}
	${comm} S19${cab1} ${val}
	${comm} S19${cab2} ${val}
	${comm} S19${cab3} ${val}
	${comm} S19${cab4} ${val}
	${comm} S19${cab5} ${val}
	${comm} S21${cab1} ${val}
	${comm} S21${cab2} ${val}
	${comm} S21${cab3} ${val}
	${comm} S21${cab4} ${val}
	${comm} S21${cab5} ${val}
	${comm} S23${cab1} ${val}
	${comm} S23${cab2} ${val}
	${comm} S23${cab3} ${val}
	${comm} S23${cab4} ${val}
	${comm} S23${cab5} ${val}
    } elseif {$loc == "B"} {
	${comm} S10${cab1} ${val}
	${comm} S10${cab2} ${val}
	${comm} S10${cab3} ${val}
	${comm} S10${cab4} ${val}
	${comm} S10${cab5} ${val}
	${comm} S12${cab1} ${val}
	${comm} S12${cab2} ${val}
	${comm} S12${cab3} ${val}
	${comm} S12${cab4} ${val}
	${comm} S12${cab5} ${val}
	${comm} S14${cab1} ${val}
	${comm} S14${cab2} ${val}
	${comm} S14${cab3} ${val}
	${comm} S14${cab4} ${val}
	${comm} S14${cab5} ${val}
	${comm} S16${cab1} ${val}
	${comm} S16${cab2} ${val}
	${comm} S16${cab3} ${val}
	${comm} S16${cab4} ${val}
	${comm} S16${cab5} ${val}
	${comm} S11${cab1} ${val}
	${comm} S11${cab2} ${val}
	${comm} S11${cab3} ${val}
	${comm} S11${cab4} ${val}
	${comm} S11${cab5} ${val}
	${comm} S13${cab1} ${val}
	${comm} S13${cab2} ${val}
	${comm} S13${cab3} ${val}
	${comm} S13${cab4} ${val}
	${comm} S13${cab5} ${val}
	${comm} S15${cab1} ${val}
	${comm} S15${cab2} ${val}
	${comm} S15${cab3} ${val}
	${comm} S15${cab4} ${val}
	${comm} S15${cab5} ${val}
	${comm} S17${cab1} ${val}
	${comm} S17${cab2} ${val}
	${comm} S17${cab3} ${val}
	${comm} S17${cab4} ${val}
	${comm} S17${cab5} ${val}
    } elseif {$loc == "A"} {
	${comm} S2${cab1} ${val}
	${comm} S2${cab2} ${val}
	${comm} S2${cab3} ${val}
	${comm} S2${cab4} ${val}
	${comm} S2${cab5} ${val}
	${comm} S4${cab1} ${val}
	${comm} S4${cab2} ${val}
	${comm} S4${cab3} ${val}
	${comm} S4${cab4} ${val}
	${comm} S4${cab5} ${val}
	${comm} S6${cab1} ${val}
	${comm} S6${cab2} ${val}
	${comm} S6${cab3} ${val}
	${comm} S6${cab4} ${val}
	${comm} S6${cab5} ${val}
	${comm} S8${cab1} ${val}
	${comm} S8${cab2} ${val}
	${comm} S8${cab3} ${val}
	${comm} S8${cab4} ${val}
	${comm} S8${cab5} ${val}
	${comm} S3${cab1} ${val}
	${comm} S3${cab2} ${val}
	${comm} S3${cab3} ${val}
	${comm} S3${cab4} ${val}
	${comm} S3${cab5} ${val}
	${comm} S5${cab1} ${val}
	${comm} S5${cab2} ${val}
	${comm} S5${cab3} ${val}
	${comm} S5${cab4} ${val}
	${comm} S5${cab5} ${val}
	${comm} S7${cab1} ${val}
	${comm} S7${cab2} ${val}
	${comm} S7${cab3} ${val}
	${comm} S7${cab4} ${val}
	${comm} S7${cab5} ${val}
	${comm} S9${cab1} ${val}
	${comm} S9${cab2} ${val}
	${comm} S9${cab3} ${val}
	${comm} S9${cab4} ${val}
	${comm} S9${cab5} ${val}
    }
}

proc gestest {args} {
    set txtFile ""
    set quarter 1
    set statusFile ""
    set currentFile ""
    set voltsFile ""
    APSStrictParseArguments {txtFile quarter statusFile currentFile voltsFile}
    if {$quarter == 1} {
	set header "SLOT 1"
    } elseif {$quarter == 2} {
	set header "SLOT 3"
    } elseif {$quarter == 3} {
	set header "SLOT 5"
    } elseif {$quarter == 4} {
	set header "SLOT 7"
    }
    sdds load $statusFile statusData
    sdds load $currentFile currentData
    if {[llength $voltsFile]} {
	sdds load $voltsFile voltsData
    }
    set fp [open $txtFile "a"]
    puts $fp "\n-------------------------------------\n"
    puts $fp " Possible GesPac problems for $header\n"
    puts $fp "-------------------------------------\n\n"

    set tag 0

    set count [llength [lindex $statusData(Column.Index) 0]]
    for {set x 0} {$x < $count} {incr x} {
	set p 0
	foreach name $statusData(ColumnNames) {
	    if {($name == "Index") || ($name == "Rootname")} {
		continue
	    }
	    if {[lindex [lindex $statusData(Column.$name) 0] $p] > 1.01} {
		set sector [string range [lindex [lindex $statusData(Column.Rootname) 0] $p] 1 end]
		if {(($sector == 40) && ($p == 1)) || 
		    (($sector == 39) && ($p == 4)) ||
		    (($sector == 1) && ($p == 1))} {
		} else {
		    set tag 1
		    puts $fp "Failed StatusCALC check in sector $sector, cabinet [expr ($p + 1)]\n"
		    puts $fp "-> Check GES_INP & GES_OUT cards.\n\n"
		}
	    }
	    incr p
	}
	set p 0
	foreach name $currentData(ColumnNames) {
	    if {($name == "Index") || ($name == "Rootname")} {
		continue
	    }
	    set value [lindex [lindex $currentData(Column.$name) 0] $p]
	    if {($value < 45) || ($value > 55)} {
		set sector [string range [lindex [lindex $currentData(Column.Rootname) 0] $p] 1 end]
		if {(($sector == 40) && ($p == 1)) || 
		    (($sector == 39) && ($p == 4)) ||
		    (($sector == 1) && ($p == 1))} {
		} else {
		    set tag 1
		    puts $fp "Failed CurrentAI check in sector $sector, cabinet [expr ($p + 1)]\n"
		    puts $fp "-> Check GES_TIMER card.\n\n"
		}
	    }
	    incr p
	}
	if {[llength $voltsFile]} {
	    set p 0
	    foreach name $voltsData(ColumnNames) {
		if {($name == "Index") || ($name == "Rootname")} {
		    continue
		}
		set value [lindex [lindex $voltsData(Column.$name) 0] $p]
		if {$value < .5} {
		    set sector [string range [lindex [lindex $voltsData(Column.Rootname) 0] $p] 1 end]
		    set tag 1
		    puts $fp "Failed OutVoltageAI check in sector $sector, cabinet [expr ($p + 1)]\n"
		    puts $fp "-> Check GES_ADC card.\n\n"
		}
		incr p
	    }
	}
    }
    if {$tag == 0} {
	puts $fp "None found.\n\n"
    }
    close $fp
}

proc test {args} {
    set location ""
    APSStrictParseArguments {location}
    global debug abortNow abortVar
    set abortNow 0
    set abortVar 0

    if {![llength $location]} {
	APSSetVarAndUpdate status "Please select a zone"
	return
    }
    APSSetVarAndUpdate status "Runnig GESPAC Test on ${location}"
    APSSetVarAndUpdate status "Test will take approximatly 40 minutes" 

    set cnt 0
    set loc $location
    set sec [clock seconds]
    set now [clock format $sec]
    set suf [clock format $sec -format "%H%M"]

    set month /home/helios/oagData/PSGROUP/data/[clock format $sec -format "%y%m"]
#    set month /home/helios/PSGROUP/data/[clock format $sec -format "%y%m"]
#    set month /home/helios/SOLIDAY/PSGROUP/data/[clock format $sec -format "%y%m"]
    set today ${month}/[clock format $sec -format "%d"]

    if [catch {makeDir $today} result] {
	APSSetVarAndUpdate status "$result"
	return
    }
    set originalDir [pwd]
    cd ${today}

    .userFrame.f1.abort.button configure -state normal

    APSSetVarAndUpdate status "Turning off supplies...\n"
    PSqtsend -loc $loc -qt 1 -val 0
    PSqtsend -loc $loc -qt 1 -val 0
    PSqtsend -loc $loc -qt 2 -val 0
    PSqtsend -loc $loc -qt 2 -val 0
    PSqtsend -loc $loc -qt 3 -val 0
    PSqtsend -loc $loc -qt 3 -val 0
    PSqtsend -loc $loc -qt 4 -val 0
    PSqtsend -loc $loc -qt 4 -val 0


    PSsend -loc $loc -type Q -val off
    PSsend -loc $loc -type S -val off
    PSsend -loc $loc -type H -val off
    PSsend -loc $loc -type V -val off

    if {$abortNow == 1} {
	APSSetVarAndUpdate status "Aborted by user"
	cd $originalDir
	return
    }

    APSSetVarAndUpdate status "Turning on & running left side / top supplies..."

    PSqtsend -loc $loc -qt 1 -val on

    APSSetVarAndUpdate status "Waiting 120 seconds"
    if {$debug} {
	APSWaitWithUpdate -waitSeconds 5 -abortVariable abortNow
    } else {
	APSWaitWithUpdate -waitSeconds 120 -abortVariable abortNow
    }

    if {$abortNow == 1} {
	APSSetVarAndUpdate status "Aborted by user"
	cd $originalDir
	return
    }

    PSqtsend -loc $loc -qt 1 -val 50
    PSqtsend -loc $loc -qt 1 -val 50

    APSSetVarAndUpdate status "Waiting 120 seconds"
    if {$debug} {
	APSWaitWithUpdate -waitSeconds 5 -abortVariable abortNow
    } else {
	APSWaitWithUpdate -waitSeconds 120 -abortVariable abortNow
    }

    if {$abortNow == 1} {
	APSSetVarAndUpdate status "Aborted by user"
	cd $originalDir
	return
    }

    APSSetVarAndUpdate status "Collecting data..."

    if [catch {
	blt::bgexec abortVar sddsvmonitor \
	    /home/helios/oagData/PSSCPDB/req/sr/l/sr_${loc}_l_t_status.req \
	    ${suf}_sr_${loc}_l_t_status.sdds \
	    -step=1
    } result] {
	APSSetVarAndUpdate status "problem running sddsvmonitor : $result"
	file delete ${suf}_sr_${loc}_l_t_status.sdds
	cd $originalDir
	return
    }
    if {$abortNow == 1} {
	APSSetVarAndUpdate status "Aborted by user"
	cd $originalDir
	return
    }

    if [catch {
	blt::bgexec abortVar sddsvmonitor \
	    /home/helios/oagData/PSSCPDB/req/sr/l/sr_${loc}_l_t_current.req \
	    ${suf}_sr_${loc}_l_t_current.sdds \
	    -step=1
    } result] {
	APSSetVarAndUpdate status "problem running sddsvmonitor : $result"
	file delete ${suf}_sr_${loc}_l_t_current.sdds
	cd $originalDir
	return
    }
    if {$abortNow == 1} {
	APSSetVarAndUpdate status "Aborted by user"
	cd $originalDir
	return
    }

    if {![file exists ${suf}_sr_${loc}_l_t_status.sdds]} {
	APSSetVarAndUpdate status "error: file <${suf}_sr_${loc}_l_t_status.sdds> was not created."
	cd $originalDir
	return
    }
    if {![file exists ${suf}_sr_${loc}_l_t_current.sdds]} {
	APSSetVarAndUpdate status "error: file <${suf}_sr_${loc}_l_t_current.sdds> was not created."
	cd $originalDir
	return
    }
    
    APSSetVarAndUpdate status "Turning down left side / top supplies...\n"
    PSqtsend -loc $loc -qt 1 -val 0
    PSqtsend -loc $loc -qt 1 -val 0
    APSWaitWithUpdate -waitSeconds 10 -abortVariable abortNow
    if {$abortNow == 1} {
	APSSetVarAndUpdate status "Aborted by user"
	cd $originalDir
	return
    }

    PSqtsend -loc $loc -qt 1 -val off
    PSqtsend -loc $loc -qt 1 -val off
    APSWaitWithUpdate -waitSeconds 10 -abortVariable abortNow
    if {$abortNow == 1} {
	APSSetVarAndUpdate status "Aborted by user"
	cd $originalDir
	return
    }

    APSSetVarAndUpdate status "Turning on & running left side / bottom supplies..."

    PSqtsend -loc $loc -qt 2 -val on
    APSSetVarAndUpdate status "Waiting 120 seconds"
    if {$debug} {
	APSWaitWithUpdate -waitSeconds 5 -abortVariable abortNow
    } else {
	APSWaitWithUpdate -waitSeconds 120 -abortVariable abortNow
    }
    if {$abortNow == 1} {
	APSSetVarAndUpdate status "Aborted by user"
	cd $originalDir
	return
    }

    PSqtsend -loc $loc -qt 2 -val 50
    PSqtsend -loc $loc -qt 2 -val 50
    APSSetVarAndUpdate status "Waiting 120 seconds"
    if {$debug} {
	APSWaitWithUpdate -waitSeconds 5 -abortVariable abortNow
    } else {
	APSWaitWithUpdate -waitSeconds 120 -abortVariable abortNow
    }
    if {$abortNow == 1} {
	APSSetVarAndUpdate status "Aborted by user"
	cd $originalDir
	return
    }

    APSSetVarAndUpdate status "Collecting data..."

    if [catch {
	blt::bgexec abortVar sddsvmonitor \
	    /home/helios/oagData/PSSCPDB/req/sr/l/sr_${loc}_l_b_status.req \
	    ${suf}_sr_${loc}_l_b_status.sdds \
	    -step=1
    } result] {
	APSSetVarAndUpdate status "problem running sddsvmonitor : $result"
	file delete ${suf}_sr_${loc}_l_b_status.sdds
	cd $originalDir
	return
    }
    if {$abortNow == 1} {
	APSSetVarAndUpdate status "Aborted by user"
	cd $originalDir
	return
    }

    if [catch {
	blt::bgexec abortVar sddsvmonitor \
	    /home/helios/oagData/PSSCPDB/req/sr/l/sr_${loc}_l_b_current.req \
	    ${suf}_sr_${loc}_l_b_current.sdds \
	    -step=1
    } result] {
	APSSetVarAndUpdate status "problem running sddsvmonitor : $result"
	file delete ${suf}_sr_${loc}_l_b_current.sdds
	cd $originalDir
	return
    }
    if {$abortNow == 1} {
	APSSetVarAndUpdate status "Aborted by user"
	cd $originalDir
	return
    }

    if [catch {
	blt::bgexec abortVar sddsvmonitor \
	    /home/helios/oagData/PSSCPDB/req/sr/l/sr_${loc}_l_b_volts.req \
	    ${suf}_sr_${loc}_l_b_volts.sdds \
	    -step=1
    } result] {
	APSSetVarAndUpdate status "problem running sddsvmonitor : $result"
	file delete ${suf}_sr_${loc}_l_b_volts.sdds
	cd $originalDir
	return
    }
    if {$abortNow == 1} {
	APSSetVarAndUpdate status "Aborted by user"
	cd $originalDir
	return
    }

    if {![file exists ${suf}_sr_${loc}_l_b_status.sdds]} {
	APSSetVarAndUpdate status "error: file <${suf}_sr_${loc}_l_b_status.sdds> was not created."
	cd $originalDir
	return
    }
    if {![file exists ${suf}_sr_${loc}_l_b_current.sdds]} {
	APSSetVarAndUpdate status "error: file <${suf}_sr_${loc}_l_b_current.sdds> was not created."
	cd $originalDir
	return
    }
    if {![file exists ${suf}_sr_${loc}_l_b_volts.sdds]} {
	APSSetVarAndUpdate status "error: file <${suf}_sr_${loc}_l_b_volts.sdds> was not created."
	cd $originalDir
	return
    }

    APSSetVarAndUpdate status "Turning down left side / bottom supplies...\n"

    PSqtsend -loc $loc -qt 2 -val 0
    PSqtsend -loc $loc -qt 2 -val 0
    APSWaitWithUpdate -waitSeconds 10 -abortVariable abortNow
    if {$abortNow == 1} {
	APSSetVarAndUpdate status "Aborted by user"
	cd $originalDir
	return
    }

    PSqtsend -loc $loc -qt 2 -val off
    PSqtsend -loc $loc -qt 2 -val off
    APSWaitWithUpdate -waitSeconds 10 -abortVariable abortNow
    if {$abortNow == 1} {
	APSSetVarAndUpdate status "Aborted by user"
	cd $originalDir
	return
    }

    APSSetVarAndUpdate status "Turning on & running right side / top supplies..."

    PSqtsend -loc $loc -qt 3 -val on
    APSSetVarAndUpdate status "Waiting 120 seconds"
    if {$debug} {
	APSWaitWithUpdate -waitSeconds 5 -abortVariable abortNow
    } else {
	APSWaitWithUpdate -waitSeconds 120 -abortVariable abortNow
    }
    if {$abortNow == 1} {
	APSSetVarAndUpdate status "Aborted by user"
	cd $originalDir
	return
    }

    PSqtsend -loc $loc -qt 3 -val 50
    PSqtsend -loc $loc -qt 3 -val 50
    APSSetVarAndUpdate status "Waiting 120 seconds"
    if {$debug} {
	APSWaitWithUpdate -waitSeconds 5 -abortVariable abortNow
    } else {
	APSWaitWithUpdate -waitSeconds 120 -abortVariable abortNow
    }
    if {$abortNow == 1} {
	APSSetVarAndUpdate status "Aborted by user"
	cd $originalDir
	return
    }

    APSSetVarAndUpdate status "Collecting data..."

    if [catch {
	blt::bgexec abortVar sddsvmonitor \
	    /home/helios/oagData/PSSCPDB/req/sr/r/sr_${loc}_r_t_status.req \
	    ${suf}_sr_${loc}_r_t_status.sdds \
	    -step=1
    } result] {
	APSSetVarAndUpdate status "problem running sddsvmonitor : $result"
	file delete ${suf}_sr_${loc}_r_t_status.sdds
	cd $originalDir
	return
    }
    if {$abortNow == 1} {
	APSSetVarAndUpdate status "Aborted by user"
	cd $originalDir
	return
    }

    if [catch {
	blt::bgexec abortVar sddsvmonitor \
	    /home/helios/oagData/PSSCPDB/req/sr/r/sr_${loc}_r_t_current.req \
	    ${suf}_sr_${loc}_r_t_current.sdds \
	    -step=1
    } result] {
	APSSetVarAndUpdate status "problem running sddsvmonitor : $result"
	file delete ${suf}_sr_${loc}_r_t_current.sdds
	cd $originalDir
	return
    }
    if {$abortNow == 1} {
	APSSetVarAndUpdate status "Aborted by user"
	cd $originalDir
	return
    }

    if {![file exists ${suf}_sr_${loc}_r_t_status.sdds]} {
	APSSetVarAndUpdate status "error: file <${suf}_sr_${loc}_r_t_status.sdds> was not created."
	cd $originalDir
	return
    }
    if {![file exists ${suf}_sr_${loc}_r_t_current.sdds]} {
	APSSetVarAndUpdate status "error: file <${suf}_sr_${loc}_r_t_current.sdds> was not created."
	cd $originalDir
	return
    }

    APSSetVarAndUpdate status "Turning down right side / top supplies...\n"

    PSqtsend -loc $loc -qt 3 -val 0
    PSqtsend -loc $loc -qt 3 -val 0
    APSWaitWithUpdate -waitSeconds 10 -abortVariable abortNow
    if {$abortNow == 1} {
	APSSetVarAndUpdate status "Aborted by user"
	cd $originalDir
	return
    }

    PSqtsend -loc $loc -qt 3 -val off
    PSqtsend -loc $loc -qt 3 -val off
    APSWaitWithUpdate -waitSeconds 10 -abortVariable abortNow
    if {$abortNow == 1} {
	APSSetVarAndUpdate status "Aborted by user"
	cd $originalDir
	return
    }

    APSSetVarAndUpdate status "Turning on & running right side / bottom supplies..."

    PSqtsend -loc $loc -qt 4 -val on
    APSSetVarAndUpdate status "Waiting 120 seconds"
    if {$debug} {
	APSWaitWithUpdate -waitSeconds 5 -abortVariable abortNow
    } else {
	APSWaitWithUpdate -waitSeconds 120 -abortVariable abortNow
    }
    if {$abortNow == 1} {
	APSSetVarAndUpdate status "Aborted by user"
	cd $originalDir
	return
    }

    PSqtsend -loc $loc -qt 4 -val 50
    PSqtsend -loc $loc -qt 4 -val 50
    APSSetVarAndUpdate status "Waiting 120 seconds"
    if {$debug} {
	APSWaitWithUpdate -waitSeconds 5 -abortVariable abortNow
    } else {
	APSWaitWithUpdate -waitSeconds 120 -abortVariable abortNow
    }
    if {$abortNow == 1} {
	APSSetVarAndUpdate status "Aborted by user"
	cd $originalDir
	return
    }

    APSSetVarAndUpdate status "Collecting data..."

    if [catch {
	blt::bgexec abortVar sddsvmonitor \
	    /home/helios/oagData/PSSCPDB/req/sr/r/sr_${loc}_r_b_status.req \
	    ${suf}_sr_${loc}_r_b_status.sdds \
	    -step=1
    } result] {
	APSSetVarAndUpdate status "problem running sddsvmonitor : $result"
	file delete ${suf}_sr_${loc}_r_b_status.sdds
	cd $originalDir
	return
    }
    if {$abortNow == 1} {
	APSSetVarAndUpdate status "Aborted by user"
	cd $originalDir
	return
    }

    if [catch {
	blt::bgexec abortVar sddsvmonitor \
	    /home/helios/oagData/PSSCPDB/req/sr/r/sr_${loc}_r_b_current.req \
	    ${suf}_sr_${loc}_r_b_current.sdds \
	    -step=1
    } result] {
	APSSetVarAndUpdate status "problem running sddsvmonitor : $result"
	file delete ${suf}_sr_${loc}_r_b_current.sdds
	cd $originalDir
	return
    }
    if {$abortNow == 1} {
	APSSetVarAndUpdate status "Aborted by user"
	cd $originalDir
	return
    }

    if [catch {
	blt::bgexec abortVar sddsvmonitor \
	    /home/helios/oagData/PSSCPDB/req/sr/r/sr_${loc}_r_b_volts.req \
	    ${suf}_sr_${loc}_r_b_volts.sdds \
	    -step=1
    } result] {
	APSSetVarAndUpdate status "problem running sddsvmonitor : $result"
	file delete ${suf}_sr_${loc}_r_b_volts.sdds
	cd $originalDir
	return
    }
    if {$abortNow == 1} {
	APSSetVarAndUpdate status "Aborted by user"
	cd $originalDir
	return
    }

    if {![file exists ${suf}_sr_${loc}_r_b_status.sdds]} {
	APSSetVarAndUpdate status "error: file <${suf}_sr_${loc}_r_b_status.sdds> was not created."
	cd $originalDir
	return
    }
    if {![file exists ${suf}_sr_${loc}_r_b_current.sdds]} {
	APSSetVarAndUpdate status "error: file <${suf}_sr_${loc}_r_b_current.sdds> was not created."
	cd $originalDir
	return
    }
    if {![file exists ${suf}_sr_${loc}_r_b_volts.sdds]} {
	APSSetVarAndUpdate status "error: file <${suf}_sr_${loc}_r_b_volts.sdds> was not created."
	cd $originalDir
	return
    }

    APSSetVarAndUpdate status "Turning down right side / bottom supplies...\n"

    PSqtsend -loc $loc -qt 4 -val 0
    PSqtsend -loc $loc -qt 4 -val 0
    APSWaitWithUpdate -waitSeconds 10 -abortVariable abortNow
    if {$abortNow == 1} {
	APSSetVarAndUpdate status "Aborted by user"
	cd $originalDir
	return
    }

    PSqtsend -loc $loc -qt 4 -val off
    PSqtsend -loc $loc -qt 4 -val off

    APSSetVarAndUpdate status "Creating report...\n"

    if {$loc != "R"} {
	append output "Gespac Report for Zone ${loc}\n\n"
    } else {
	append output "Gespac Report for SR\n\n"
    }
    append output "${now}\n"

    set fID [open ${suf}_ges_${loc}_report.txt "w"]
    puts $fID $output
    close $fID

    if {[catch {
	gestest -txtFile ${suf}_ges_${loc}_report.txt \
	    -quarter 1 \
	    -statusFile ${suf}_sr_${loc}_l_t_status.sdds \
	    -currentFile ${suf}_sr_${loc}_l_t_current.sdds
	gestest -txtFile ${suf}_ges_${loc}_report.txt \
	    -quarter 2 \
	    -statusFile ${suf}_sr_${loc}_l_b_status.sdds \
	    -currentFile ${suf}_sr_${loc}_l_b_current.sdds \
	    -voltsFile ${suf}_sr_${loc}_l_b_volts.sdds
	gestest -txtFile ${suf}_ges_${loc}_report.txt \
	    -quarter 3 \
	    -statusFile ${suf}_sr_${loc}_r_t_status.sdds \
	    -currentFile ${suf}_sr_${loc}_r_t_current.sdds
	gestest -txtFile ${suf}_ges_${loc}_report.txt \
	    -quarter 4 \
	    -statusFile ${suf}_sr_${loc}_r_b_status.sdds \
	    -currentFile ${suf}_sr_${loc}_r_b_current.sdds \
	    -voltsFile ${suf}_sr_${loc}_r_b_volts.sdds
    } results]} {
	APSSetVarAndUpdate status "problem generating reports : $results"
	cd $originalDir
	return
    }
    set output ""
    append output "Cabinet Layout\n"
    append output "\n"
    append output "\n"
    append output "Cabinet 5   Cabinet 4   Cabinet 3   Cabinet 2   Cabinet 1\n"
    append output "---------   ---------   ---------   ---------   ---------\n"
    append output "|BH1|BH2|   |BV3|BH3|   |BH4|AH4|   |AV3|AH3|   |AH2|AH1|\n"
    append output "---------   ---------   ---------   ---------   ---------\n"
    append output "|BV1|BV2|   |BQS|BMT|   |BV4|AV4|   |AQS|AMT|   |AV2|AV1|\n"
    append output "---------   ---------   ---------   ---------   ---------\n"
    append output "|BQ2|BS1|   |BS2|BS3|   |AS4|AQ4|   |AS3|AS2|   |AS1|AQ2|\n"
    append output "---------   ---------   ---------   ---------   ---------\n"
    append output "|BQ1|BQ3|   |   |BQ4|   |BQ5|AQ5|   |   |   |   |AQ3|AQ1|\n"
    append output "---------   ---------   ---------   ---------   ---------\n"
    
    set fID [open ${suf}_ges_${loc}_report.txt "a"]
    puts $fID $output
    close $fID

    APSSetVarAndUpdate status "Finished gespac test.\n"

    cd $originalDir
}

######## Main #######

APSApplication . \
    -name "Storage Ring Gespac Testing" \
    -version $CVSRevisionAuthor \
    -overview "Template Application" \
    -contextHelp "Help for Template Application"

APSScrolledStatus .status \
    -parent .userFrame \
    -packOption "-side top" \
    -textVariable status \
    -width 70 \
    -height 8

APSRadioButtonFrame .testSelection \
    -parent .userFrame \
    -orientation vertical \
    -label "Select Zone" \
    -variable location \
    -packOption "-side top" \
    -valueList {A B C D E F R} \
    -buttonList {"Zone A" "Zone B" "Zone C" "Zone D" "Zone E" "Zone F" "Entire Ring" } \
    -contextHelp "Select location for the test to run."

pack [frame .userFrame.f1] -side top
APSButton .testbutton \
    -parent .userFrame.f1 \
    -text "Start Test" \
    -command { test -location $location} \
    -contextHelp "Starts Test in Specified area."

APSButton .abort \
    -parent .userFrame.f1 \
    -text "Abort" \
    -command "set abortNow 1; set abortVar 1"

