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

set auto_path [linsert $auto_path 0  $env(OAG_TOP_DIR)/oag/apps/lib/$env(HOST_ARCH)]


APSStandardSetup

#set usage {usage: makeBunchTrainWF -rootname <string> -momentum <GeV>(6.0) -nBunches \#(324) -nGaps \#(2) -gapLength <buckets>(2) -nGuard \#(1) -guardMult \#(2) -nPerBunch \#(1000) -bunchWF <filename>}
set usage {usage: makeBunchTrainWF -rootname <string> -momentum <GeV>(6.0) -nBunches \#(324) -nPerBunch \#(1000) -bunchWF <filename>}
set rootname ""
set momentum 6.0
set nBunches 324
set nGaps 0
set gapLength 2
set nGuard 0
set guardMult 1
set nPerBunch 1000
set profile ""
set args $argv
#if {[APSParseArguments {profile rootname momentum nBunches nPerBunch gapLength nGaps nGuard guardMult}] || ![string length $rootname]} {
#    return -code error "$usage"
#}
if {[APSStrictParseArguments {rootname momentum nBunches nPerBunch gapLength nGaps nGuard guardMult bunchWF}] || ![string length $rootname]} {
	return -code error "$usage"
}

if [file exists $rootname.bun] {
   return -code error "in use: $rootname.bun"
}
#if ![file exists $profile] {
#    return -code error "not found: $profile"
#}

if {$nGaps>0 && [expr $nBunches%$nGaps!=0]} {
    puts stderr "nBunches must be a multiple of nGaps"
    exit 1
}

exec elegant Basic.ele
puts "Ran Basic.ele"

#if $nGaps>0 {
#    set seqLen [expr int($nBunches/$nGaps)]
#    set nSeq $nGaps
#} else {
#    set seqLen $nBunches
#    set nSeq 1
#    set gapLength 0
#}
#set offset 0
#for {set seq 0} {$seq<$nSeq} {incr seq} {
#    set offset [expr $seq*$seqLen]
#    for {set j 0} {$j<[expr $seqLen-$gapLength-2*$nGuard]} {incr j} {
#        set mult([expr $j+$offset]) 1
#    }
#    for {} {$j<[expr $seqLen-$nGuard-$gapLength]} {incr j} {
#        set mult([expr $j+$offset]) $guardMult
#    }
#    for {} {$j<[expr $seqLen-$nGuard]} {incr j} {
#        set mult([expr $j+$offset]) 0
#    }
#    for {} {$j<$seqLen} {incr j} {
#        set mult([expr $j+$offset]) $guardMult
#    }
#}


for {set j 0} {$j<$nBunches} {incr j} {
    set mult($j) ([exec sddsprocess $bunchWF -pipe=out -filter=col,Index,$j,$j | sdds2stream -pipe=in -col=WaveformNorm])
}

set jList ""
set mList ""
for {set j 0} {$j<$nBunches} {incr j} {
    lappend jList $j
    lappend mList $mult($j)
}
exec sddsmakedataset $rootname.pattern \
    -column=Slot,type=short -data=[join $jList ,] \
    -column=Multiplier,type=short -data=[join $mList ,]

puts "made data set"


set bunchFrequency [exec sddsprocess Basic.twi -pipe=out -process=s,max,sMax \
                        "-define=param,T0,sMax c_mks / pCentral beta.p /,units=s" \
                        "-define=param,fb,T0 rec $nBunches *,units=Hz" \
                        | sdds2stream -pipe -parameter=fb]

set bunList ""
set bunchNum 0
#set bunRefPart 100000

set bunRefPart [exec sdds2stream $rootname.bunRef -parameter=Particles]
puts $bunRefPart
set pSigma 0.0
set randomize 0
exec sddsprocess $rootname.bunRef "-redefine=param,IDSlotsPerBunch,$nPerBunch $guardMult *" -noWarning

for {set slot 0} {$slot<$nBunches} {incr slot} {
    puts "$slot"
    set m $mult($slot)
    #puts $m
    set tOffset [expr $slot/$bunchFrequency]
    set IDOffset [expr $guardMult*$bunchNum*$nPerBunch]    
    set curPart [expr $m*$nPerBunch]
    puts $curPart
    if $m!=0 {
	    if $randomize==1 {
		    exec sddsprocess $rootname.bunRef $rootname.bunRef.tmp -redefine=col,sortNum,rnd "-redefine=param,Particles,$curPart $pSigma grnd * 1 + *" -noWarning
		    exec sddsprocess $rootname.bunRef.tmp "-redefine=param,fracKept,Particles $bunRefPart /" -noWarning
	    #exec sddssort  $rootname.bunRef.tmp -col=sortNum -noWarning
	   # exec sddsprocess $rootname.bunRef.tmp $rootname.bun$bunchNum -filter=col,particleID,0,$nPerBunch -noWarning
		    exec sddsprocess $rootname.bunRef.tmp $rootname.bun$bunchNum -filter=col,sortNum,0,fracKept -noWarning
	    #exec sddsprocess $rootname.bunRef.tmp -redefine=col,newInd
	    #exec sddsprocess $rootname.bunRef.tmp $rootname.bun$bunchNum -filter=col,particleID,0,$curPart -noWarning
	    
		    exec sddsprocess $rootname.bun$bunchNum -noWarning \
			"-redefine=column,t,t $tOffset +,units=s" \
			"-redefine=column,particleID,i_row $IDOffset + 1 +,type=long" 

		    after 2000

		} else {
		    exec sddsprocess $rootname.bunRef $rootname.bunRef.tmp "-redefine=param,Particles,$curPart" -noWarning
		    #exec sddsprocess $rootname.bunRef.tmp "-redefine=param,fracKept,Particles $bunRefPart /" -noWarning
		    
		    exec sddsprocess $rootname.bunRef.tmp $rootname.bun$bunchNum -filter=col,particleID,0,Particles -noWarning

		    exec sddsprocess $rootname.bun$bunchNum -noWarning \
			"-redefine=column,t,t $tOffset +,units=s" \
			"-redefine=column,particleID,i_row $IDOffset + 1 +,type=long" 


		}
            lappend bunList $rootname.bun$bunchNum 
	    set bunchNum [expr $bunchNum+1]
    }
}

puts "made bunches"

eval exec sddscombine $bunList $rootname.bun
eval file delete $bunList $rootname.done $rootname.tsamples
#exec "rm *~ -f"

puts "end"

exit

rm -f *~
