/*
**++
**  MODULE DESCRIPTION:
**
**      This module is the temperature humidity index calculaion.
**
**  AUTHORS:
**
**      Phil Baughn (BASIC version)
**	Rod Falanga (C version)
**
**  CREATION DATE:  24 October 1990
**
**  MODIFICATION HISTORY:
**
**      {@tbs@}...
**--
*/


/*
**
**  INCLUDE FILES
**
*/

#ifdef VMS
#include stdio
#else
#include <stdio.h>
#endif


/*
**
**  MACRO DEFINITIONS
**
*/

#define DUMMY_LEN	50

/*
**++
**  FUNCTIONAL DESCRIPTION:
**
**      This is the main function for the temperature humidity
**  index calculation.
**
**--
*/
#ifdef VMS
extern void humidity_index (void)
#else
void humidity_index ()
#endif
{
#ifdef VMS
    extern void position (int , int), 
                print (char *), 
                get_date_time (char *, char *), 
                get_string (char *),
		explain(char *);
#else
    extern void position (), 
                print (), 
                get_date_time (), 
                get_string (),
		explain();
#endif
    char date [DUMMY_LEN], 
         time [DUMMY_LEN], 
         again [DUMMY_LEN], 
	 please_explain[DUMMY_LEN],
         temp_humid_index_str [DUMMY_LEN];
#ifdef VMS
    extern double get_double (void);
#else
    extern double get_double ();
#endif
    double temperature, 
	   relative_humidity, 
	   a, 
	   th;

    /*	 
    **  Enter a do() loop until the User indicates that they want to quit.
    */	 
    do
    {
        Erase();

        /*	 
        **  Show the User the current system's date and time as well as this
	**  module's name.
        */	 
	get_date_time(date, time);
	position (2, 26);
	print ("TEMPERATURE HUMIDITY INDEX");
	position (4, 34);
	print (date);
	position (5, 35);
	print (time);

        /*	 
        **  Display some information on what is temperature humidity index is.
        */	 
	position (7, 24);
	print ("THE TEMPERATURE HUMIDITY INDEX");
	position (8, 21);
	print ("DETERMINES THE EFFECTIVE TEMPERATURE");

        /*	 
        **  Now get some information from the User.
        */	 
	position (11, 12);
	print ("ENTER THE TEMPERATURE IN FAHRENHEIT                 ");
	temperature = get_double();
	position (12, 12);
	print ("ENTER THE RELATIVE HUMIDITY  (`50'= 50% )           ");
	relative_humidity = get_double();

        /*	 
        **  I'm not sure that this positioning and print of blanks is necessary
	**  but I'm going to leave this here until I get a chance to run
	**  the finished program.
        */	 
	position (18, 25);
	print ("                                   ");

        /*	 
        **  Now perform the calculations.
        */	 
	if (relative_humidity > 94.)
	    a = ((.195 * temperature) - 15.);
	else if (relative_humidity > 89. && relative_humidity < 95.)
	    a = ((.18 * temperature) - 15.);

	if (relative_humidity > 79. && relative_humidity < 90.)
	    a = ((.1667 * temperature) - 15.);
	else if (relative_humidity > 69. && relative_humidity < 80. )
            a = ((.145 * temperature) - 15.);

	if (relative_humidity > 59. && relative_humidity < 70. )
	    a = ((.1233 * temperature) - 15.);
	else if (relative_humidity < 60. )
            a = ((.085 * temperature) - 15.);

	th = (((.8 * temperature) + 15.) + a);

        /*	 
        **  Now notify the User of the temperature humidity index.
        */	 
	position(20, 10);
	print("THE TEMPERATURE HUMIDITY INDEX =  ");
        sprintf (temp_humid_index_str, "%.1f", th);
	print(temp_humid_index_str);
	print(" DEGREES FAHRENHEIT");

        /*	 
        **  Ask the User if they want to do it again.
        */	 
	position(23, 17);
	print("ANOTHER TEMPERATURE HUMIDITY INDEX (Y/N)");
	get_string(again);
    } while (again[0]=='Y' || again[0]=='y');

    /*	 
    **  Ask The User if they want to get some explanation of what
    **	the humidity index is.
    */	 
    position(24, 16);
    print("View THI Explanation & Comfort Table? (Y/N)");
    get_string(please_explain);
    if (please_explain[0]=='Y' || please_explain[0]=='y')
        explain(temp_humid_index_str);
}   /* end of humidity_index() */

/*
**++
**  FUNCTIONAL DESCRIPTION:
**
**      This function gives an explanation of what the temperature humidity
**  index is.
**
**  FORMAL PARAMETERS:
**
**      temp_humid:
**          a point of type char which is the last calculated temperature
**		humidity index
**
**--
*/
#ifdef VMS
static void explain (char *temp_humid)
#else
static void explain (temp_humid)
char *temp_humid;
#endif
{
#ifdef VMS
    extern void position (int, int), print (char *);
#else
    extern void position (), print ();
#endif

    Erase();
    print ("\n");
    print("    Your Temperature-Humidity Index reading was ");
    print(temp_humid);
    print(".\n");
    print ("  \n");
    print ("    Readings in excess of  70  represent the point  where a few people\n");
    print ("    begin  to  feel  uncomfortable.   Over 75, about 1/2 of all people\n");
    print ("    will feel uncomfortable. Nearly all people will feel uncomfortable\n");
    print ("    with  readings  over  79  with rapidly  decreasing work efficiency\n");
    print ("    begining  with  levels  in excess of  84;  and EXTREME DANGER with\n");
    print ("    possibility of heat  exhaustion  and heat stroke begin with levels\n");
    print ("    of 92 and higher.\n");
    print ("  \n");
    print ("    The THI number, used to express the  combined temperature-humidity\n");
    print ("    effect provides a fairly good index of equivalent heat stress.  In\n");
    print ("    engineering, this combined index is refered to as `effective temp-\n");
    print ("    erature'. The weather bureau has also been known to refer to it as\n");
    print ("    the Discomfort Index.  It is NOT the same as the `Heat Index' even\n");
    print ("    though they both help to compute `Appearant' Temperatures.\n");
    print ("  \n");
    print ("  \n");
}   /* end of explain() */
