#!/bin/sh
# summer overheating worst zone determination script: version 1.1 of Novemeber 1994
rm -f $comf_dir/tmp/comfort_pt
comfort_results=$comf_dir/tmp/comfort_results

echo "Locating the worst zone....."
echo
				# Use res to get max & min zone temperatures.

echo "***** comfort_worstz: get zone comfort temperatures from res" >>$comf_dir/tmp/comfort_trace
echo "***** comfort index is ${comfort_index}" >>$comf_dir/tmp/comfort_trace

if test ! -f $comfort_results ; then
comfort_results=`${IFE_HOME}/sys/bin/dialog -p "There was a problem finding the results... please supply name: " -file "/tmp     " -d "/tmp/def_prb/sum_resul.res" -h1 "The default file can be used if there was" -h2 "an error in the simulation."`
fi
res -mode script -file ${comfort_results} >>$comf_dir/tmp/comfort_trace  2>$comf_dir/tmp/comfort_pt  <<.
${comfort_results}
y
a
n
a
d
n
-
-
y
.


echo finished assessing zones in the problem...

# Use awk to get overheating zones, ranked in PZ.

PZ=`cat $comf_dir/tmp/comfort_pt | awk -f ${IFE_HOME}/sys/bin/summer_comfort/comfort_awk_pt`

echo $PZ

if [ "X$PZ" = "X" ]; then
     echo
     echo "Congratulations!"
     echo "All zones are comfortable according to the selected criteria."
     echo
     echo "Zone summary table follows ...."
     echo

     cat $comf_dir/tmp/comfort_pt > $comf_dir/tmp/comfort_lock
     echo
     cat $comf_dir/tmp/comfort_pt
     echo
     echo
     echo "Type return to terminate."
     read xa			# Hang around until told to go away.
     exit 0			# Normal exit (0) to indicate no overheating.
fi

echo
echo "The following zones (rank ordered) suffer discomfort: $PZ"
echo
        
# Locate worst occupied (or worst) zone.

WZ=
for i in $PZ
do
    echo "Now checking zone $i for occupants..."
    if [ "X$WZ" = "X" ]; then
	WZ=$i  			# Worst Zone
    fi

res -mode script -file ${comfort_results} >>$comf_dir/tmp/comfort_trace  2>$comf_dir/tmp/comfort_cgs  <<.
${comfort_results}
y
a
n
a
q
<
1
$i
-
-
y
.

    ocup=`awk '$1 == "Number" {print $5}' $comf_dir/tmp/comfort_cgs`
    echo Number of occupied hours is $ocup

    if [ $ocup -gt 0 ]; then
	echo "Occupied."
	echo "Worst discomfort therefore occurs in occupied zone $i."

	OZF=1			# Occupied Zone Found
	WZ=$i
	break
    else
	echo "Not occupied."
    fi
done

if [ "X$OZF" = "X" ]; then
    echo "Worst discomfort therefore occurs in unoccupied zone $WZ."
fi
echo
echo "Zone summary table follows ...."
echo
cat $comf_dir/tmp/comfort_pt
sleep 8

# Put WZ in lock file for comfort_cause.
echo Remembering that the worst zone is $WZ 
echo $WZ >$comf_dir/tmp/comfort_lock
echo
echo "Graphs of zone performance will appear in separate windows."

echo " ATTENTION......."
echo " Type return to continue to the next part of the summer_overheating analysis."
read xb						# hang around.
exit 0
