# With a minimum of 4 parameters 
#   (limits of a rectangle, relative to pole height),
# this script makes a sorted overview of the best performing photometries
# to illuminate the rectangle 
#   (relative flux hitting it, average and minimum ill.)


# Further parameter may be -co, to compute a continuous row of lights for
#  at least one case (that one sorted on minimum illuminances).  
# Or -n as well (this is slow!), to compute even preliminary numbers 
#  more accurately (as averages and minima are concerned), per pixel.

# (by Jan Hollan, using his ies2tab programme, Oct 2007)

D='ldt'

P='GE SMBt LED'

N=${1}_${2}_${3}_${4}

ND=`echo $@ | sed -nr "s/.*-co.*/c/p"`
if [[ $ND = 'c' ]] 
then
 ND=c`echo $@ | sed -nr "s/.*-rh([0-9.]+).*/\1/p"`
fi
if [[ $ND = 'c' ]] 
then
 ND=c4
fi

if [ ! -d r$N$ND ]
then
 mkdir r$N$ND
 cd r$N$ND
 mkdir small
 cd ..
fi 

if [ ! -d r$N$ND/small ]
then
 cd r$N$ND
 mkdir small
 cd ..
fi 


shopt -s nullglob

for H in $D/*.ldt $D/*.LDT $D/*.ies $D/*.IES $D/*.eld $D/*.ELD
do
# G is the file name without the directory (ldt assumed as a default)
 G=${H/$D\/}
# E is the suffix
 E=${G/*\.}
# F is the file name without directory and suffix
 F=${G/\.$E}
echo $F 
 R="-u$1:$2:$3:$4 $5 $6 $7 $8"
 I="-l -mi $R"
 if [[ $E != 'IES' ]]
 then 
 if [[ $E != 'ies' ]]
 then 
  L="-e"
  I="$I -e"
 fi
 fi


 ies2tab $I   -n$F $H >012.txt
 convert $F.ppm r$N$ND/$F.png
 ies2tab $I -cm1 -n$F $H >012.txt
 convert $F.ppm r$N$ND/small/$F.png
 rm $F.ppm
 sed -rn "/^.+$/{ 
 s|$D/||
s/\.$E//p}" 012.txt >>r${N}_mi.txt
 
done

rm 012.txt

echo "
Luminaires are sorted according to decreasing minimum illuminance 
they provide within a rectangle of $1:$2:$3:$4 pole heights
" >r${N}_Smi.txt


echo "
Luminaires are sorted according to decreasing average illuminance 
they provide within a rectangle of $1:$2:$3:$4 pole heights
" >r${N}_Sav.txt

echo "
Luminaires are sorted according to decreasing useful fraction 
(of the outcoming luminaire flux)
they provide within a rectangle of $1:$2:$3:$4 pole heights
" >r${N}_Suf.txt

echo "
Luminaires are sorted according to <b>increasing</b> ratio average/minimum 
(<b>decreasing uniformity</b>, kind of)
they provide within a rectangle of $1:$2:$3:$4 pole heights
" >r${N}_Sav_mi.txt


ies2tab -c $I -n$F | sed "s/^/  /" >>r${N}_Smi.txt
ies2tab -c $I -n$F | sed "s/^/  /" >>r${N}_Sav.txt
ies2tab -c $I -n$F | sed "s/^/  /" >>r${N}_Suf.txt
ies2tab -c $I -n$F | sed "s/^/  /" >>r${N}_Sav_mi.txt


#sed -r 's/([0-9.]+ +[0-9.]+ +[0-9.]+ +[0-9.]+ +).+Full_CutOff +([^ ]+) +(.+)/\3 \1\2/
# s/   / /g ' fco.txt >ron.txt

sort -k 15 -r -n r${N}_mi.txt >>r${N}_Smi.txt
sort -k 14 -r -n r${N}_mi.txt >>r${N}_Sav.txt
sort -k 12 -r -n r${N}_mi.txt >>r${N}_Suf.txt
sort -k 16    -n r${N}_mi.txt >>r${N}_Sav_mi.txt

mv r${N}_Smi.txt r${N}_mi.txt
mv r${N}_Sav.txt r${N}_av.txt
mv r${N}_Suf.txt r${N}_uf.txt
mv r${N}_Sav_mi.txt r${N}_av_mi.txt


for FI in r${N}_mi.txt r${N}_uf.txt r${N}_av.txt r${N}_av_mi.txt 
do
 echo '<PRE>' >${FI/txt/htm}
 sed -r "
s/^([[:blank:]]*)#  //
s/^([[:blank:]]*)#//
s/^([[:blank:]]*FiOut)/        \1/
s/^([[:blank:]]*%  )/          \1/
   /^ 100/{
s|(.+) ([^ ]+)$|<A href=\"r$N$ND/\2.png\"><img src=\"r$N$ND/small/\2.png\" alt=\"img\"></A> \1<A href=\"htm/\2.htm\">\2</A>|
} " $FI >>${FI/txt/htm}
 echo '</PRE>' >>${FI/txt/htm}
done
