  	// Class for temperature curve
	// Date: May 22 99


	// Give applet access to all the AWT toolkit libraries
	import java.awt.*;
	import java.applet.*;
	//import java.awt.event.*;

        public class tempcurve extends java.applet.Applet
        {
        // declare design variables for class

        int appw,apph;    // size of applet window
        int khour,k, Total, Elapse, number, incrementx;
        float Amplitude;
        String Guideline;

        chart barChart;
        // establish data to be plotted
        float Tmp[]= new float [24];

        //float Tmp[] = {5,-2};

        public void init() {

       //find size of applet
       appw=size().width;
       apph = size().height;

       setBackground (Color.white);
       incrementx=appw/24;

       // create chart object
      temperature (30, 10);
       barChart = new chart (Tmp,Tmp.length);
       }

        public void paint(Graphics g) {

        // draw graph
        barChart.paint(g,5,apph-5,appw-10,apph-10);
        //draw main fixed elements of graph


       //the bottom two lines and white rectangle
       g.setColor (Color.lightGray);
       g.fillRect(10,apph*9/10,appw-20,apph*99/100);
       g.setColor (Color.black);
       g.drawString ("Horas",10,apph*97/100);
       g.setColor (Color.lightGray);
       g.fillRect(10,apph*9/10,appw/4,apph*99/100);
       g.fillRect(10,apph*9/10,appw-20,apph*99/100);
       g.drawLine (5,apph*9/10,appw-5,apph*9/10);
       g.drawLine (5,apph*99/100,appw-5,apph*99/100);

       g.setColor (Color.black);

       //now the titles for the x values are the hours
       number=0;
       for (int n=1; n<=24; n++)
       g.drawString (""+n,(((appw*3)/48)+2)+(n-2)*incrementx,apph*97/100);

       g.setColor (Color.lightGray);
       g.fillRect(10,apph*9/10,appw/10,apph*99/100);
       g.setColor (Color.black);
       g.drawString ("Hours:",12,apph*97/100);

        g.setColor (Color.black);
        // the top line
        g.drawLine (5,apph-apph*99/100,appw-5,apph-apph*99/100);
         //the higher quarter line
        g.drawLine (5,apph/4,appw-5,apph/4);
        //the middle line
        g.drawLine (5,apph/2,appw-5,apph/2);
        //the lower quarter line
        g.drawLine (5,(apph/4)*3,appw-5,(apph/4)*3);
        //the bottom line
        g.drawLine (5,apph-2,appw-5,apph-2);


        }
        private int toRasters(float number, float scale)
        {
        return Math.round(number*scale);
        }

    //This function generates a curve from two temperature values
	public void temperature (float Tmax, float Tmin)

{
	//Total is the difference between the times of maximum and minimum temperatures
	// In this version it is fixed because the maximum is set at 15hrs and
	// the minimum at 6 hours. But in numerical values it also changes depending on the time of day
	// because of sine curve calculations

	Total=9;

	//Amplitude is the difference between maximum and minimum temperature
    //khour is the time of the day for which we are computing a value
	//Elapse is the relationship between the time and the time of maximum temperature


	Amplitude=Tmax-Tmin;


    //This loop computes the hourly temperature
	for (int k=0;k<24;k++)
    {

    khour=k;
    Elapse=k-15;

	if (khour>=0 && khour<=5)
	{
	khour=(khour+24);
	Elapse = khour-15;
	Total=15;
    Tmp[k]=Tmax-.5f*(Amplitude)*(1.0f-(float)(Math.cos(Math.PI*Elapse/Total)));
    }

    else if (khour>5 && khour <=15)
    {

	Total=9;
	Tmp[k]=Tmax-.5f*(Amplitude)*(1.0f-(float)(Math.cos(Math.PI*Elapse/Total)));
    }

	else if (khour>15 && khour <=24)
	{
	Total=15;
	Tmp[k]=Tmax-.5f*(Amplitude)*(1.0f-(float)(Math.cos(Math.PI*Elapse/Total)));
	//System.out.println ("Hour="+k+"temp="+Tmp[k]);
	}

}

    //    System.out.println ("done with temp");
}
        public void changePositionT(float Tmax,float Tmin, int season)
        {
        String Guideline;
        if (season == 1) Guideline = "SummerGuide";
        else Guideline = "WinterGuide";

       System.out.println("in changePosition " + Guideline);
	    Tmax= Tmax;
	    Tmin= Tmin;
	    temperature (Tmax, Tmin);
        barChart.setTmp(Tmp);
        repaint();

        // set up link to other applet and send data
        AppletContext context = getAppletContext();
        Applet partner = context.getApplet (Guideline);
        ((tmpanalysis)partner).setTmp (Tmp);

 repaint();
        }
        }
