#!/bin/bash

# Script to make a deflection map from By(x) data by extending it in y direction.
# Doesn't satisfy Maxwell's equations but might be useful nevertheless.
#
# M. Borland, ANL, 2020

if [ $# -ne 2 ] ; then
   echo "usage: makeDeflectionMap <input:ByVsXDataFile> <output:deflectionMap>"
   exit 1
fi

input=$1
output=$2

if [ ! -e $input ] ; then
    echo "error: file $input not found"
    exit 1
fi
if [ -e $output ] ; then
    echo "error: file $output already exists"
    exit 1
fi

sddscombine $1 $1 $1 -pipe=out \
    | sddssort -pipe -column=x \
    | sddsprocess -pipe \
		  -define=parameter,L,$L,units=m "-define=parameter,H,$H,units=T*m" \
		  "-define=column,y,i_page 2 - 1 *,units=m" \
		  -process=By,last,ByMiddle,functionOf=x,upper=0 \
		  "-define=column,xpFactor,By ByMiddle / " \
		  "-define=column,ypFactor,0" \
    | sddscombine -pipe -merge \
    | sddssort -pipe=in $output -column=y,incr -column=x,incr
	
