#!/bin/csh
#---------------------------------------------------------------------------
# prep_helio: Process MAS output as input of ENLIL computation.
#
# Usage: prep_helio <Carrington rotation> <resolution>
#---------------------------------------------------------------------------

# Set parameters
set cr = $1
set resolution = $2

# Set environment variables
setenv CORHEL_DIR "$CORHEL_ROOT"
setenv CORHEL_BIN "$CORHEL_ROOT/bin"
setenv CORHEL_RUN "$CORHEL_ROOT/run/cr$cr-$resolution"

pushd $CORHEL_DIR/tools/corona > /dev/null
source ./set.sh run
popd > /dev/null


# Set up the boundary conditions for the heliospheric run
cd $CORHEL_RUN/helio_bc

echo "Magnetic field file format [text|hdf]:"                                 >> mapfl.dat
echo "'hdf'"                                                                  >> mapfl.dat
echo "File name for Br:"                                                      >> mapfl.dat
echo "'../corona/br002.hdf'"                                                  >> mapfl.dat
echo "File name for Bt:"                                                      >> mapfl.dat
echo "'../corona/bt002.hdf'"                                                  >> mapfl.dat
echo "File name for Bp:"                                                      >> mapfl.dat
echo "'../corona/bp002.hdf'"                                                  >> mapfl.dat
echo "Format of final field line mapping output files [text|hdf]:"            >> mapfl.dat
echo "'hdf'"                                                                  >> mapfl.dat
echo "Flag to trace forward from R0 to R1 [.true.|.false.]:"                  >> mapfl.dat
echo ".true."                                                                 >> mapfl.dat
echo "File name for forward mapping final field line radius file:"            >> mapfl.dat
echo "'r1.hdf'"                                                               >> mapfl.dat
echo "File name for forward mapping final field line theta file:"             >> mapfl.dat
echo "'t1.hdf'"                                                               >> mapfl.dat
echo "File name for forward mapping final field line phi file:"               >> mapfl.dat
echo "'p1.hdf'"                                                               >> mapfl.dat
echo "File name for forward mapping final field line expansion factor file:"  >> mapfl.dat
echo "'e1.hdf'"                                                               >> mapfl.dat
echo "Flag to trace backward from R1 to R0 [.true.|.false.]:"                 >> mapfl.dat
echo ".true."                                                                 >> mapfl.dat
echo "File name for backward mapping final field line radius file:"           >> mapfl.dat
echo "'r0.hdf'"                                                               >> mapfl.dat
echo "File name for backward mapping final field line theta file:"            >> mapfl.dat
echo "'t0.hdf'"                                                               >> mapfl.dat
echo "File name for backward mapping final field line phi file:"              >> mapfl.dat
echo "'p0.hdf'"                                                               >> mapfl.dat
echo "File name for backward mapping final field line expansion factor file:" >> mapfl.dat
echo "'e0.hdf'"                                                               >> mapfl.dat
echo "Field line tracing segment multiplier DSMULT:"                          >> mapfl.dat
echo "5."                                                                     >> mapfl.dat
echo "New (theta,phi) mesh flag:"                                             >> mapfl.dat
echo ".true."                                                                 >> mapfl.dat
echo "New (theta,phi) mesh dimensions (nt,np):"                               >> mapfl.dat
echo "91,181"                                                                 >> mapfl.dat

# Calculate mapping, speed, scale input B
# mapfl2 maps along field lines
$CORONA_BIN/mapfl2 15.

# Generate coronal hole distance file to make getv go quicker
$CORONA_BIN/ch_dist << EOF
r1.hdf
ch_dist.hdf
EOF

# Extract a slice of Br from a mid/high altitude as input for the helio model
$CORONA_BIN/extract -mp -x0 55 -x1 55 $CORHEL_RUN/corona/br002.hdf br_helio.hdf

# Make it periodic in phi (i.e., longitude)
$CORONA_BIN/mkper -y br_helio.hdf br_helio.hdf

# Scale the data by an appropriate amount to represent a field at 30 Rs
$CORONA_BIN/scale -n 0.37129 br_helio.hdf br_helio_scaled.hdf

# Convert the data to text
$CORONA_BIN/hdf2txt br_helio_scaled

# wait first for any background jobs to complete before running this
wait

# getv2 maps out the speeds from the photosphere to 30 Rs
$CORONA_BIN/getv2 0.05 0.025
$CORONA_BIN/hdf2txt v_smooth_0.05_0.025

exit 0
