#!/bin/sh
set -e
# For the 5 angles in the PARASOL reflectances generated by the PARASOL simulator.
# Compute the mean for each reflectance PL0* by dividing each by ML0* and set to missing where ML0*=0.
# Combine into a single file with ibuf(4) corresponding to an angle, i.e., PL01 -> PL, ibuf(4)=1, etc.

if [ $# -ne 12 ] ; then
  echo "Error in $0: invalid number of arguments $#"
  exit 1
fi
pfx=$1
pl01=$2
pl02=$3
pl03=$4
pl04=$5
pl05=$6
ml01=$7
ml02=$8
ml03=$9
ml04=${10}
ml05=${11}
out=${12}

files=""
n=1
while [ $n -le 5 ] ; do
  n=`echo $n | awk '{printf "%02d", $1}'`

  eval pl=\$pl$n
  eval ml=\$ml$n

  access $pl ${pfx}_$pl
  access $ml ${pfx}_$ml

  # division and masking
  div $pl $ml temp
  echo "C* FMSKPLT        -1 NEXT   GT      0.00    1     1.E38    1" | ccc fmskplt temp mtemp $ml

  # relabel the name and ibuf(4)
  echo "C*RELABL   GRID
C*         GRID             PL$n" | ccc relabl mtemp f$n

  release $pl $ml temp mtemp

  files="$files f$n"
  n=`expr $n + 1`
done

# merge all files into one
rmerge ${pfx}_${out} $files

rm $files .ccc_cards
