#include "day_length.h"
#include "math.h"

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

Cday_length::Cday_length()
{
}

Cday_length::Cday_length(int JulianDay, double Latitude)
{
	this->JulianDay = JulianDay;
	this->Latitude = Latitude;
	Declination	= Calc_Declination(JulianDay, CONV);
	Sunrise		= Calc_Sunrise(Latitude, Declination);
	Sunset		= Calc_Sunset(Latitude, Declination);
	for (int i = 0; i < 366; i++)
		DayLength[i] = Get_DayLength(i+1,Latitude);
}

Cday_length::Cday_length(double Latitude)
{
	this->Latitude = Latitude;
	for (int i = 0; i < 366; i++)
		DayLength[i] = -Get_DayLength(Calc_Sunrise(Latitude,Calc_Declination(i+1, CONV)));
}
Cday_length::~Cday_length()
{}

/////////////////////////////////////////////////////////////////////
//
//				PUBLIC FUNCTIONS
//
/////////////////////////////////////////////////////////////////////

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

double Cday_length::Calc_Declination(int JulianDay, double CONV)
{
	double declination = 0.0;

	declination = -.3948 - 23.2559 * cos(CONV * double(JulianDay) + 0.1588);
	declination = declination - 0.3915 * cos(2 * CONV * double(JulianDay) + 0.0942);
	declination = (declination - 0.1764 * cos(3 * CONV * double(JulianDay)+.4538)) * CONV;

	return(declination);
}
//-------------------------------------------------------------------

double Cday_length::Calc_Sunrise(double Latitude, double declination)
{
	double sunset	= 0.0;
	double f		= CONV * Latitude;
	double t		= -1.0 * CONV * 0.8333;

	sunset = (sin(t)-(sin(f) * sin(declination))) / (cos(f) * cos(declination));
	sunset = (PI / 2 - atan(sunset / sqrt(1 - pow(sunset,2))));

	return(sunset);
}
//-------------------------------------------------------------------

double Cday_length::Get_DayLength(int JulianDay, double Latitude)
{
	double day_length = 0.0;
	day_length = Calc_Sunrise(Latitude, Calc_Declination(JulianDay, CONV));

	return(Get_DayLength(-day_length));
}
//-------------------------------------------------------------------
/*
int Cday_length::Get_Sunrise_HH(double sunrise)
{
	int hh = 0;

	hh = int(_timeconv_sr(sunrise));

	return(hh);
}
*/
/////////////////////////////////////////////////////////////////////
//
//				PRIVATE FUNCTIONS
//
/////////////////////////////////////////////////////////////////////

