#? / bin / bash

# With a minimum of 11 parameters 
# this script makes a sorted overview of the best performing photometries
# to illuminate the rectangle, if arranged in a continuous row

# $12 and further 3 parameters are appended to the ies2tab command line; 
#   -h# to get the true illuminance (with that height and file-included 
#    bulb flux) might be a good option.

# To achieve this, the script tries four rotations (0, 90, 180, 360 degrees)
#  and then uses a such one, which performs the best.

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


D='ldt'

P='GE SSMBt LED 3000 K'

if [[ ${11} = '' ]] 
then
 echo 'At least 11 parameters are needed:
 1: xmin (of a rectangle whose illuminance will be computed) 
 2: xmax 
 3: ymin 
 4: ymax 
 5: relative spacing (neighbouring poles distance / pole height), 
     usually should be set equal to xmax-xmin (if larger than that, 
     the computed useful fraction may be over 100 %)
 6: useful fraction (proportion of the flux of the row of luminaires hitting
                     the rectangle), at least
 7: range of allowed maximum (/ 1 lx) (specific) illuminance 
     within that rectangle, as #:#
 8: range of average illuminance as #:#
 9: range of minimum illuminance (/ 1 lx) there, as #:#
 10: maximal allowed ratio average/minimum
 11: specific luminous intensity (/ cd/klm) at >=80_degrees, at most
 (ev. further, non-obligatory parameters 
  are simply included in ies2tab command line before the file name).'
 exit
fi

UFmi=$6
MaB=${7/:*}; MaT=${7/*:}
AvB=${8/:*}; AvT=${8/*:}
MiB=${9/:*}; MiT=${9/*:}
RaMa=${10}
SIma=${11}

if (( `calc -q -d "($1 < $2)"`   )) && (( `calc -q -d "($3 < $4)"` )) \
&& (( `calc -q -d "($1 > -6.1)"` )) && (( `calc -q -d "($1 < 6.1)"` )) \
&& (( `calc -q -d "($2 > -6.1)"` )) && (( `calc -q -d "($2 < 6.1)"` )) \
&& (( `calc -q -d "($3 > -6.1)"` )) && (( `calc -q -d "($3 < 6.1)"` )) \
&& (( `calc -q -d "($4 > -6.1)"` )) && (( `calc -q -d "($4 < 6.1)"` )) \
&& (( `calc -q -d "(15   > $5)"` )) && (( `calc -q -d "(0.5  < $5)"` )) \
&& (( `calc -q -d "($MaB < $MaT)"` )) \
&& (( `calc -q -d "($AvB < $AvT)"` )) \
&& (( `calc -q -d "($MiB < $MiT)"` )) \
&& (( `calc -q -d "(100   > $UFmi)"` )) && (( `calc -q -d "(0.1  < $UFmi)"` )) \
&& (( `calc -q -d "(100   > $RaMa)"` )) && (( `calc -q -d "(1  < $RaMa)"` )) \
&& (( `calc -q -d "(100000  > $SIma)"` )) && (( `calc -q -d "(0.1  < $SIma)"` ))
then 
 echo 'rect_ilc.sh parameters are OK'
else
 echo 'Some parameter is outside acceptable limits, try once more...'
 exit
fi

TRUH=`echo $@ | sed -nr "s/.* -h([0-9.]+\.*[0-9]*).*/\1/p"`
if [[ $TRUH ]]
then
 N=${1}_${2}_${3}_${4}c$5
 TRUH="(for $TRUH m lamp height)"
else 
 N=${1}_${2}_${3}_${4}c$5h$TRUH
 TRUH='(specific -- for 1 m height and 1 klm bulb flux)'
fi


if [ ! -d r$N ]
then
 mkdir r$N
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="-co -u$1:$2:$3:$4 -rh$5 ${12} ${13} ${14} ${15}"
# $5 is relative spacing of the poles
 I="-l -mi $R"
 if [[ $E != 'IES' ]]
 then 
 if [[ $E != 'ies' ]]
 then 
  L="-e"
  I="$I -e"
 fi
 fi
 ies2tab $I        $H >012.txt
 NONS=`grep NONSENSE 012.txt`
# if [[ $NONS = '' ]] 
# then
if [[ ! $NONS = '' ]] 
then 
# cat 012.txt 
 tail -2 012.txt > 012t.txt
 mv 012t.txt 012.txt
# echo "just the bottom line:"
# cat 012.txt
fi 

 MSI=`sed -rn 's/(([^ ]+ +){2})([0-9]+\.[0-9]+).*/\3/p' 012.txt`

#echo $MSI

# skipping those with too large lum. int. at >=80 degrees
# if (( `calc -q -d "($MSI < $SIma)"` )) 
# then
  ies2tab $I -rv90  $H >>012.txt
  ies2tab $I -rv180 $H >>012.txt
  ies2tab $I -rv270  $H >>012.txt
  sed -rn "/^.+$/{ 
  s|$D/||
  s/\.$E//p}" 012.txt | grep -n . | sort -k 13 -r >012sM.txt
# sorting is on UF, as the minimum illuminance might be 0.#, too insensitive

  head -n1 012sM.txt >012.txt
  RON=`head -n1 012sM.txt | sed -nr "s/^(.).*/\1/p"`
  rm 012sM.txt
  ROT=`calc -q -d "(90*($RON-1))" | sed -r 's/[[:blank:]]*//g'`
# for the found optimal rotation, a plot is created: 
  ies2tab $I -rv$ROT -n${F}sm -cm1 $H >012.txt
#convert $F.ppm r$N/$F.png
 
 
  UF=`sed -rn 's/(([^ ]+ +){11})([0-9]+\.[0-9]+).*/\3/p' 012.txt`
  MAX=`sed -rn 's/(([^ ]+ +){12})([0-9]+\.[0-9]+).*/\3/p' 012.txt`
  AVE=`sed -rn 's/(([^ ]+ +){13})([0-9]+\.[0-9]+).*/\3/p' 012.txt`
  MIN=`sed -rn 's/(([^ ]+ +){14})([0-9]+\.[0-9]+).*/\3/p' 012.txt`
  RAT=`sed -rn 's/(([^ ]+ +){15})([0-9]+\.[0-9]+).*/\3/p' 012.txt`

#  echo $UFmi $MaB:$MaT $AvB:$AvT $MiB:$MiT $RaMa $SIma
#  echo $UF $MAX $AVE $MIN $RAT $MSI 
#  cat 012.txt

  if  \
     (( `calc -q -d "($MIN > $MiB)"` )) && (( `calc -q -d "($MIN < $MiT)"` )) \
  && (( `calc -q -d "($MAX > $MaB)"` )) && (( `calc -q -d "($MAX < $MaT)"` )) \
  && (( `calc -q -d "($AVE > $AvB)"` )) && (( `calc -q -d "($AVE < $AvT)"` )) \
  && (( `calc -q -d "($UF > $UFmi)"` )) \
  && (( `calc -q -d "($RAT < $RaMa)"` )) 
  then 
   ies2tab $I -rv$ROT -n$F $H >012.txt
   sed -rn "/^.+$/{ 
    s/^/$RON: /
    s|$D/||
    s/\.$E//p}" 012.txt >>r${N}_min.txt
  echo $UF $MAX $AVE $MIN $RAT $MSI 
  cat 012.txt
#   if [ ! -f r$N/$F.png ]
#   then
    convert $F.ppm r$N/$F.png
#   fi    
   rm $F.ppm
  else
   rm ${F}sm.ppm
  fi
# fi
# else
#   cat 012.txt >>over100p.txt    
# fi
done

#rm 012.txt

 
if [[ ${12} ]] 
then 
 OPARM="Non-obligatory parameters to ies2tab were: ${12} ${13} ${14} ${15}"
fi

echo "
Luminaires are sorted according to an increasing ratio of average/minimum illuminance 
they provide within a rectangle of $1:$2:$3:$4 pole heights. 
The row of luminaires obeys the following limits:
 $5 as relative spacing (neighbouring poles distance / pole height)
 $6 as useful fraction / 1 % (proportion of the flux of the row of luminaires 
                           hitting the rectangle), at least
 $7 as maximum $TRUH illuminance (/ 1 lx) allowed range within that rectangle 
 $8 as average $TRUH illuminance (/ 1 lx) range
 $9 as minimum $TRUH illuminance (/ 1 lx) range
 ${10} as maximal allowed ratio average/minimum
 ${11} specific luminous intensity (/ cd/klm) at >=80_degrees, at most.

Command line (-rv# had # in 0, 90, 180, 270 to maximize minimum illuminance)
to produce the inside of each line within this overview: 
 ies2tab $I -rv# -n <ldt/file_name>
$OPARM

Ev. rotation of the luminaire needed to maximize this value is indicated by
the 1st column larger than 1: -- 2: 90, 3: 180 and 4: 270 degrees
" >r${N}_Smin.txt

ies2tab -c $I -n | sed "s/^/  /" >>r${N}_Smin.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 17 r${N}_min.txt >>r${N}_Smin.txt

cp r${N}_Smin.txt r${N}_min.txt

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