#include "pet_sl.h"
#include "math.h"

//-------------------------------------------------------------------

CpET::CpET()
{}

CpET::~CpET()
{}
//-------------------------------------------------------------------

// Calclate pET time series after Hamon (1961)
CpET::CpET(double *temperature, int size, double Latitude, int first_year)
{
	pET_Hamon = new double[size];
	daylength = new Cday_length(Latitude);

	_Calc_pET_Hamon(temperature, size, first_year, pET_Hamon, daylength);
}
//-------------------------------------------------------------------

void CpET::_Calc_pET_Hamon(double* temperature, int size, int first_year,
						   double* pET_Hamon, Cday_length* dl)
{
	int i = 0;
	int j = 0;
	int year = first_year;
	int days = 0;	// number of days in current years

	while (i < size) // size = number of temperature values
	{
		if (_LeapYear(year)) days = 366;
		else days = 365;
		while (j < days && i < size)
		{
			pET_Hamon[i] = pow(dl->Get_DayLength(j+1) / 12, 2)
							* exp(temperature[i] / 16);

			j++;
			i++;
		}
		j = 0;
		year += 1;
	}
}
//-------------------------------------------------------------------

bool CpET::_LeapYear(int year)
{
	bool cond1 = false;
	bool cond2 = false;
	bool cond3 = false;
	bool leapyear = true;

	if (year % 4   == 0)	cond1 = true;
	if (year % 100 == 0)	cond2 = true;
	if (year % 400 == 0)	cond3 = true;

	if (!cond1)
	{
		leapyear = false;
	} else {
		if (cond2 && !cond3)
			leapyear = true;
	}

	return(leapyear);
}



