#!/bin/bash
#
# Copying and distribution of this file, with or without modification, are
# permitted in any medium without royalty provided the copyright notice and
# this notice are preserved. This file is offered as-is, without any warranty.

set -e

# Check command usage
if [ -z $1 ] || [ $# -ge 2 ]
then
  echo "Usage: $0 SCHIFF-OUTPUT"
  exit 1
fi

# Ensure that the submitted file exists
schiff_output=$1
if [ ! -f $schiff_output ]
then
  echo "Can't find the \"$schiff_output\" file"
  exit 1
fi

# Create a temporary directory
tmpdir=`mktemp -d`

# Split the schiff-output at each empty line. Save the resulting "scratch"
# files in the temporary directory.
csplit $schiff_output -q -z /^$/ {*} -f $tmpdir/scratch -b %d

nwlens=`cat $tmpdir/scratch0 | wc -l` # Number of wavelength
wlens=() # List of wavelengths

# Check that all scratch files exist
for ((i=0; i < $(($nwlens*3 + 2)); ++i ))
do
  if [ ! -f $tmpdir/scratch$i ]
  then
    echo "The file \"$schiff_output\" is not a valid schiff-output."
    exit 1
  fi
  sed -i '/^$/d' $tmpdir/scratch$i # Remove empty line
done

################################################################################
# Cross sections
################################################################################
echo "Write xsections.txt"
echo -e \
"# Cross sections and average projected area.\n"\
"# \n"\
"# line format: \"W E e A a S s P p\"\n"\
"# - \"W\" the wavelength in vacuum (expressed in micron);\n"\
"# - \"E\", \"A\" and \"S\" the expected value of the extinction, absorption and\n"\
"#   scattering cross sections, respectively, in square microns per particle;\n"\
"# - \"P\" the estimated average projected area in square microns per particle;\n"\
"# - \"e\", \"a\", \"s\" and \"p\" the standard error of the aforementioned\n"\
"#   estimations." > xsections.txt
cat $tmpdir/scratch0 >> xsections.txt

################################################################################
# Phase function descriptors
################################################################################
echo "Write descs.txt"
for ((i=0; i < $nwlens; ++i))
do
  desc=(`sed -n "$(($i+1))p" $tmpdir/scratch1`)
  str="wavelength = ${desc[0]} micron\n"\
"theta-limit = ${desc[1]} radians\n"\
"Ws(theta-limit) = ${desc[2]} +/- ${desc[3]}\n"\
"Wc(theta-limit) = ${desc[4]} +/- ${desc[5]}\n"\
"n = ${desc[6]}\n"\
"scattering angles count = ${desc[7]}\n"\
"invcum phase function entries = ${desc[8]}\n"\

  if [ $i -eq 0 ]
  then
    echo -e $str > descs.txt
  else
    echo -e $str >> descs.txt
  fi

  wlens+=(${desc[0]}) # Append the wavelength in the wlens array
done

################################################################################
# Phase functions
################################################################################
for ((i=0; i < $nwlens; ++i))
do
  filename=func_${wlens[$i]}.txt
  echo "Write $filename"
  echo -e \
"# Phase functions for the wavelength ${wlens[$i]} microns\n"\
"# line format: \"angle(radians) expected-value standard-error\"" > ${filename}
  cat $tmpdir/scratch$((2+$i)) >> ${filename}
done

################################################################################
# Cumulative phase functions
################################################################################
for ((i=0; i < $nwlens; ++i))
do
  filename=cumul_${wlens[$i]}.txt
  echo "Write $filename"
  echo -e \
"# Cumulative phase functions for the wavelength ${wlens[$i]} microns\n"\
"# line format: \"angle(radians) expected-value standard-error\"" > ${filename}
  cat $tmpdir/scratch$((2+$nwlens+$i)) >> ${filename}
done

################################################################################
# Inverse cumulative phase functions
################################################################################
for ((i=0; i < $nwlens; ++i))
do
  filename=invcumul_${wlens[$i]}.txt
  echo "Write $filename"
  echo -e \
"# Inverse cumulative phase functions for the wavelength ${wlens[$i]} microns\n"\
"# line format: \"probability angle(radians)\"" > ${filename}
  cat $tmpdir/scratch$((2+2*$nwlens+$i)) >> ${filename}
done

exit 0
