proc APSMeasureOneVariableResponse {args} {
    global expDone
    set readback ""
    set control ""
    set average 1
    set changePause 5
    set interval 1
    set steps 1
    set initialValue 0
    set finalValue 0
    set output ""
    set relativeToOrig 1
    set interactive 0
    set destroyWindow 0
    set readingType ave
    set usage "Usage: APSMeasureOneVariableResponse -reaback <readback PV> -control \
       <control PV> -average <number> -changePause <pause after change> \
       -interval <interval between readings> -steps <points of setpoint> \
       -intialValue <initial value of setpoint> -finalValue <final value of setpoint> \
       -output <filename for output> -relativeToOrig <1 or 0> -interactive <1 or 0>\
       -destroyWindow <0 or 1> -readingType {min|max|ave|midpt}"

    APSStrictParseArguments {readback control average changePause interval steps \
        initialValue finalValue output relativeToOrig interactive destroyWindow}

    if {$readback=="" || $control==""} {
        return -code error "no control or readback is given!\n$usage"
    }
    switch $readingType {
        min {
            set readbackColumn Min$readback
        }
        max {
            set readbackColumn Max$readback
        }
        midpt {
            set readbackColumn Midpt$readback
        }
        default {
            set readbackColumn $readback
        }
    }
    
    set template /home/helios/oagData/maintainReadbackFiles/OneVariableTemplate.exp
    set expDone 0
    set out /tmp/[APSTmpString]
    APSAddToTmpFileList -ID 1 -fileList $out
    if {$output==""} {
        set output $out.fit
        APSAddToTmpFileList -ID 1 -fileList $output
    }
    
    set macro -macro=readback=$readback,control=$control,numToAve=$average,steps=$steps,
    set macro ${macro}initialValue=$initialValue,finalValue=$finalValue,post_pause=$changePause
    set macro ${macro},interval=$interval,relativeToOrig=$relativeToOrig
    if $interactive {
        set win [APSUniqueName .]
        APSExecLog $win -width 60 -unixCommand \
          "sddsexperiment $template $out $macro -verbose" \
          -callback "set expDone done" -abortCallback "set expDone abort" \
          -cancelCallback "set expDone cancel"
        tkwait variable expDone
        switch $expDone {
            cancel -
            abort {
                if $destroyWindow {
                    destroy $win
                }
                return -code error "APSMeasureOneVariableResponse: experiment was cancelled or aborted"
            }
            done {
                #process the output
                if [catch {processGain -filename $out -readback $readback \
                             -control $control -output $output} result] {
                    if $destroyWindow {
                        destroy $win
                    }
                    return -code error "APSMeasureOneVariableResponse: $result"
                }
                if $destroyWindow {
                    destroy $win
                }
                return $result
            }
        }
    } else {
        if [catch {eval exec sddsexperiment $template $out $macro} result] {
            return -code error "APSMeasureOneVariableResponse: $result"
        }
        #process the output
        if [catch {processGain -filename $out -readback $readbackColumn \
                     -control $control -output $output} result] {
            return -code error "APSMeasureOneVariableResponse: $result"
        }
        return $result
    } 
}

proc processGain {args} {
    set filename ""
    set readback ""
    set control ""
    set output ""
    APSStrictParseArguments {filename readback control output}
    if {$filename=="" || $readback=="" || $control==""} {
        return -code error "no filename or readback or control is given for processing"
    }
    if {$output==""} {
        set output $filename.fit
        APSAddToTmpFileList -ID 1 -fileList $output
    }
    if [catch {exec sddspfit $filename $output -column=$control,$readback} result] {
        return -code error "$result"
    }
    if [catch {exec sdds2stream -par=Slope $output} result] {
        return -code error "$result"
    }
    #plot the result
    #exec sddsplot $filename.fit -col=$control,$readback -graphic=symbol,scale=2 \
       # -col=$control,${readback}Fit -graphic=line -end &
    return $result
}