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

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)]
#
#$Log: not supported by cvs2svn $

APSStandardSetup

set lattice default
set outDir ""
set sector B1C0
set plane H
set amplitude ""
#0, 0-12; 1: 12-86; 2: 86-160; 3: 160-220; 4: 220-230
set region 0
set startRegion 0
set endRegion 0
set load 0
set install 0
set refRamp /home/helios/oagData/booster/ramps/correctors/lattices/$lattice/HVCorr.ramp
set args $argv
APSParseArguments {sector  amplitude lattice load plane outDir install refRamp region startRegion endRegion}

set bumpDir /home/helios/oagData/booster/ramps/correctors/lattices/$lattice
set env(EPICS_CA_MAX_ARRAY_BYTES) 140000000
if [regexp {H} $sector] {
    set pl H
    set sector1 $sector
} elseif [regexp {V} $sector] {
    set sector1 $sector
    set pl V
} else {
    set pl [string toupper $plane]
    set sector1 ${sector}$pl
}

if ![string length $outDir] {
    set outFile /tmp/${sector1}-[clock format [clock seconds] -format %Y%m%d:%H%M%S].ramp
} else {
    set outFile $outDir/${sector1}.ramp
}

if ![string length $amplitude] {
    #read it from a place holder, B:BconLockoutAO (not in use now, use it for BPM amplitude value)
    if [catch {exec cavget -list=B:BconLockoutAO -pend=10 -printErrors} amplitude] {
	puts stderr $amplitude
	exit 1
    }
} else {
    if [catch {exec cavput -list=B:BconLockoutAO=$amplitude -pend=20} result] {
        puts stderr "Error setting amplitude: $result"
        exit 1
    }
}

set tmpRoot /tmp/[APSTmpString]

if [catch {exec sddsprocess $bumpDir/${pl}coeff.sdds -match=par,Sector=$sector1 $tmpRoot.1} result] {
    puts stderr "Error1: $result"
    exit 1
}
set corrList [exec sdds2stream -col=CorrectorName $tmpRoot.1]
set kList [exec sdds2stream -col=K $tmpRoot.1]

if {$startRegion!=$endRegion} {
    set regionOpt -region=start=$startRegion,end=$endRegion
} else {
    set regionOpt -region=$region
}

if $load {
    if [catch {exec makeboosterbump  -correctorList=[join $corrList ,] -coef=[join $kList ,] -amplitude=$amplitude \
                 -reference=$refRamp -outRampFile=$outFile $regionOpt } result] {
        puts stderr "Error generating and loading new ramp: $result"
        exit 1
    }
    } else {
    if [catch {exec makeboosterbump  -dryRun -correctorList=[join $corrList ,] -coef=[join $kList ,] -amplitude=$amplitude \
                 -reference=$refRamp -outRampFile=$outFile $regionOpt } result] {
        puts stderr "Error generating  new ramp: $result"
        exit 1
       }
       
}

if $install {
    set newFile [APSNextGenerationedName -directory $bumpDir -name HVCorr.ramp-0000 -separator - -newFile 1]
    cd $bumpDir 
    set corrList [exec sdds2stream -par=CorrName $outFile]
    set option "-match=par,CorrName=[lindex $corrList 0],!"
    for {set i 1} {$i<[llength $corrList]} {incr i} {
	append option ",CorrName=[lindex $corrList $i],!,&"
    }
    if [catch {exec sddsprocess HVCorr.ramp $tmpRoot.other $option} result] {
        puts stderr "Error in generating new ramp1: $result"
        exit 1
    }
    if [catch {exec sddscombine $tmpRoot.other $outFile -pipe=out \
                 | sddsprocess -pipe "-reprint=par,CorrectorBump,$corrList" \
                 | sddssort -pipe=in -par=CorrName $newFile } result] {
	return -code error "Error in generating new file: $result"
    }
    exec rm HVCorr.ramp
    exec ln -s $newFile HVCorr.ramp
}

exit 0
