#!/bin/csh  -f
set nonomatch

#
# elegant2astra
# converts elegant phase-space output file to ASTRA format
# M. Borland, ANL/APS
#

if ($#argv != 2) then
        echo "Usage: elegant2astra <input> <output>"
        exit 1
endif

set input = $1
set output = $2

if ( ! -e $input ) then
    echo "Not found: $input"
    exit 1
endif
if ( -e $output ) then
    echo "In use: $output"
    exit 1
endif

set tmpRoot = `tmpname`

# Define momentum columns and z column.  Change units and sense of t.
sddsconvert $input -pipe=out -retain=column,t,p,x,xp,y,yp \
    | sddsprocess -pipe=in $tmpRoot.1 \
    "-define=column,pz,p xp sqr yp sqr + 1 + sqrt / mev * 1e6 *,units=eV" \
    "-define=column,px,pz xp *,units=eV" \
    "-define=column,py,pz yp *,units=eV" \
    "-define=column,z,0,units=m" \
    "-redefine=column,t,t -1e9 *,units=ns" 

# Process to find the average coordinates, then subtract from all coordinates.
sddsprocess $tmpRoot.1 $tmpRoot.3 -delete=parameter,* \
    -process=*,ave,%s0 "-redefine=column,%s,%s %s0 -,select=*" 

# Turn the average coordinate values into a column
sddsprocess $tmpRoot.1 -pipe=out \
    -process=*,ave,%s \
    | sddscollapse -pipe \
    | sddsprocess -pipe=in $tmpRoot.2 \
    -process=Charge,first,Charge 

# Combine the average (reference) data and the particle-by-particle offset data
sddscombine $tmpRoot.2 $tmpRoot.3 -merge -pipe=out \
    -retain=column,x,y,z,t,px,py,pz \
  | sddsprocess -pipe \
    "-redefine=column,q,Charge n_rows / 1e9 *,units=nC" \
    "-redefine=column,particleIndex,1,type=long" \
    "-redefine=column,statusFlag,-1,type=long" \
  | tee $output.sdds \
  | sdds2plaindata -pipe=in $output \
    -outputMode=ascii -noRowCount \
    -column=x,format=%12.4e \
    -column=y,format=%12.4e \
    -column=z,format=%12.4e \
    -column=px,format=%12.4e \
    -column=py,format=%12.4e \
    -column=pz,format=%12.4e \
    -column=t,format=%12.4e \
    -column=q,format=%12.4e \
    -column=particleIndex,format=%4ld \
    -column=statusFlag,format=%4ld 

\rm $tmpRoot.1 $tmpRoot.2 $tmpRoot.3
