# This program loads the booster corrector ramps
# for both horizontal and vertical planes.

proc APSMpBoosterLoadCorrectorRampsInfo {} {
    return "This program loads the booster corrector ramps\
for both horizontal and vertical planes."
    return
}

proc APSMpBoosterLoadCorrectorRampsDialog {frame} {
    APSSetVarAndUpdate status "Load Booster Corrector Ramps"
    label $frame.la1 -text "Click OK to Load Booster Corrector Ramps"
    pack $frame.la1 -side top
    return
}

proc APSMpBoosterLoadCorrectorRamps {args} {
    set loadAdl 1
    set lattice ""
    APSStrictParseArguments {loadAdl lattice}
    APSMpStep "Load Booster Corrector Ramps" \
	-promptDialog APSMpBoosterLoadCorrectorRampsDialog
    
    set BCorrRampDir /home/helios/oagData/booster/ramps/correctors
    set BCorrRampLoadCommand "ramploadnew $BCorrRampDir/HVCorr.ramp"
    
    if ![string length $lattice] {
	APSMpReturn error "Booster lattice is not given!"
    }
    file delete -force /home/helios/oagData/booster/ramps/correctors/HVCorr.ramp
    file link -symbolic /home/helios/oagData/booster/ramps/correctors/HVCorr.ramp /home/helios/oagData/booster/ramps/correctors/lattices/${lattice}/HVCorr.ramp
    

    APSMpStep "Load the booster corrector ramps" -proc APSMpBoosterLoadCorrectorRampsNormal
    if [catch {APSMpBoosterLoadCorrectorRampsNormal -filename $BCorrRampDir/HVCorr.ramp -loadAdl $loadAdl} done] {
	APSMpReturn error "APSMpBoosterLoadCorrectorRamps: $done"
    }
    APSSetVarAndUpdate status "Done"
    after 2000

    APSMpReturn ok
}

proc APSMpBoosterLoadCorrectorRampsNormal {args} {
    set filename ""
    set horizontal 1
    set vertical 1
    set selection b
    set loadAdl 1
    APSStrictParseArguments {selection filename loadAdl}
    if {$loadAdl} {
	catch {exec medm -x -attach /net/helios/iocapps/adlsys/booster/psApp/BoosterRampControl.adl &}
    }
    if {$selection=="v"} {
	set horizontal 0
    } elseif {$selection=="h"} {
	set vertical 0
    }
    
    if {$filename==""} {
	APSSetVarAndUpdate status "No filename specified"
	APSMpReturn error "APSMpBoosterLoadCorrectorRampsNormal: No filename specified"
    }

    set opt ""
    if {$horizontal && $vertical} {
	set opt "-b"
    } elseif $horizontal {
	set opt "-h"
    } elseif $vertical {
	set opt "-v"
    }
    APSMpStep "Loading ramp" -desc "Load ramp"
    if [catch {exec ramploadnew $filename} results] {
	APSMpReturn error "APSMpBoosterLoadCorrectorRampsNormal: $results"
    }
    APSMpReturn ok
    #setting foreground and set currentAOs are not needed for new AFGs.
   # APSWaitWithUpdate -waitSeconds 5
   # APSMpStep "Setting AFG foreground" -desc "Set AFG foreground"
   # if [catch {exec setbcorr -fg -q 2> /dev/null} results] {
   #	APSMpReturn error "APSMpBoosterLoadCorrectorRampsNormal: $results"
   #   }
   # APSMpStep "Setting CurrentAO values" -desc "Set CurrentAO values"
   # if [catch {exex setbramp2ao $opt} results] {
   #	APSMpReturn error "APSMpBoosterLoadCorrectorRampsNormal: $results"
   #   }
   # APSMpReturn ok
}



proc APSMpBoosterCheckCorrectorRampTable {args} {
    set BoosterLattice ""
    APSStrictParseArguments {BoosterLattice}

    set BCorrRampDir /home/helios/oagData/booster/ramps/correctors/lattices/$BoosterLattice
    
    if {[catch {exec sddsprocess  $BCorrRampDir/HVCorr.ramp -pipe=out \
                  "-reprint=par,WaveformPV,B:%s:source,CorrName" \
                  | sdds2stream -par=WaveformPV -pipe } waveformPVs]} {
        APSMpReturn error "APSMpBoosterCheckCorrectorRampTable1: $waveformPVs"
    }
    
    if {[catch {exec sdds2stream -rows=bar $BCorrRampDir/HVCorr.ramp} rows]} {
        APSMpReturn error "APSMpBoosterCheckCorrectorRampTable2: $rows"
    }
    set rows [lindex $rows 0]

    set tmpRoot /tmp/[APSTmpString]
    if {[catch {exec sddswget $tmpRoot.1 -pv=[join $waveformPVs ,]} result]} {
        APSMpReturn error "APSMpBoosterCheckCorrectorRampTable3: $result"
    }
    APSAddToTempFileList $tmpRoot.1

    if {[catch {exec sddsprocess $tmpRoot.1 -pipe=out \
                  -clip=$rows,0,invert \
                  | sddsxref -pipe=in $BCorrRampDir/HVCorr.ramp $tmpRoot.2 \
                  -transfer=par,CorrName -nowarnings \
                  -take=RampTime,RampSetpoint} result]} {
       APSMpReturn error "APSMpBoosterCheckCorrectorRampTable4: $result"
    }
    APSAddToTempFileList $tmpRoot.2

    catch {exec sddsplot -groupby=page \
             -split=page \
             -sep=page \
             -graph=sym,vary=type,connect=type,subtype=type \
             -col=RampTime,Waveform \
             -title=@TimeStamp \
             -samescale=y   \
             -leg=specified=CurrentTable \
             $tmpRoot.2 \
             -col=RampTime,RampSetpoint \
             -topline=@CorrName \
             -leg=specified=ReferenceTable \
             $tmpRoot.2 & }
}
