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

#
# Tool to convert magnet setpoints to elegant parameters file.
# Should work for transport from gun through end of L5.
#

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)]
APSDebugPath
set CVSRevisionAuthor "\$Author: borland $"

APSApplication . -name GenerateLinacElegantParameters \
  -overview "Converts current setpoints for gun through L5 into elegant parameters. The dipoles are not included."

set status ""
APSScrolledStatus .status -parent .userFrame -textVariable status -height 10 -width 100

proc SetStatus {text} {
    APSSetVarAndUpdate status $text
}

proc AutoSetOutputFile {} {
    global LPLSCRChoice outputFile
    set inputFile $LPLSCRChoice(Filename)
    if [string length $inputFile] {
        set outputFile [file tail [file root $inputFile]].param
        SetStatus "Set output file to $outputFile"
    } else {
        SetStatus "No input file name, so didn't auto-set output file name."
    }
}

proc RunConversion {} {
    global LPLSCRChoice outputFile gun gunMomentum postL2Momentum postL4Momentum postL5Momentum
    set inputFile $LPLSCRChoice(Filename)
    if [file exists $outputFile] {
        if [APSYesNoPopUp "$outputFile already exists. Overwrite?"] {
            file delete -force $outputFile
        } else {
            SetStatus "$outputFile exists. Operation aborted."
            return
        }
    }

    SetStatus "Performing conversion."
    set columnList [APSGetSDDSNames -fileName $inputFile -class column]

    set preprocessOptionList ""
    if [lsearch -exact ElementName $columnList]==-1 {
        lappend preprocessOptionList -edit=column,DeviceName,ControlName,%/:CurrentAO//%/:SetDacCurrentC//
    }
    if [lsearch -exact Current $columnList]==-1 {
        lappend preprocessOptionList -scan=column,Current,ValueString,%le,type=double,units=A
    }

    set tmpList ""
    if [llength $preprocessOptionList] {
        set t1 /tmp/[APSTmpString]
        lappend tmpList $t1
        if [catch {eval exec sddsprocess  $inputFile $t1 \
            -match=col,ControlName=*:CurrentAO,ControlName=*:SetDacCurrentC,| \
            $preprocessOptionList} result] {
            SetStatus "$result"
            return
        }
        set inputFile $t1
    }

    if [catch {APSGenerateLinacElegantParameters \
        -input $inputFile -output $outputFile \
        -gun $gun -gunMomentum $gunMomentum -BCMomentum $postL2Momentum \
        -postL4Momentum $postL4Momentum -postL5Momentum $postL5Momentum} result] {
        SetStatus "$result"
    } else {
        SetStatus "Done."
    }
    catch {eval file delete $tmpList}
}


proc ViewOutputFile {} {
    global outputFile
    if ![string length $outputFile] {
        SetStatus "No output file named"
        return
    }
    if ![file exists $outputFile] {
        SetStatus "Output file $outputFile not found"
        return
    }
    APSExecLog  .print -name "Printout for $outputFile" \
      -unixCommand "sddsprintout -column=ElementName -column=ElementParameter -column=ParameterValue $outputFile"
}

APSAddSCRDialog .scr -parent .userFrame -system LPL -arrayName LPLSCRChoice

set outputFile ""
set gun RG2
set gunMomentum 3.0
set postL2Momentum 150.0
set postL4Momentum [expr 150 + (425-150)/2.0]
set postL5Momentum 425.0
APSRadioButtonFrame .rbgun -parent .userFrame -label "Gun: " -orientation horizontal -variable gun \
    -buttonList "RG1 RG2" -valueList "RG1 RG2"
APSLabeledEntry .gunMomentum -parent .userFrame -textVariable gunMomentum  -label "Post gun momentum (MeV/C): "
APSLabeledEntry .pL2Momentum -parent .userFrame -textVariable postL2Momentum  -label "Post L2 momentum (MeV/C): "
APSLabeledEntry .pL4Momentum -parent .userFrame -textVariable postL4Momentum  -label "Post L4 momentum (MeV/C): "
APSLabeledEntry .pL5Momentum -parent .userFrame -textVariable postL5Momentum  -label "Post L5 momentum (MeV/C): "

APSLabeledEntry .output -parent .userFrame -textVariable outputFile -width 90 -label "Output file: "

APSButton .autoname -parent .userFrame -text "Autoname" -command AutoSetOutputFile
APSButton .run -parent .userFrame -text "Run" -command RunConversion
APSButton .view -parent .userFrame -text "View" -command ViewOutputFile
