/////////////////////////////////////////////////////////////////////////////
// Name:        day_length.h
// Author:      Stefan Liersch
// Contact:		stefan.liersch@ufz.de stefan.liersch@gmail.com
// Created:     2007-03-06
//
// --------------------------------------------------------------
// Note:		This class estimates following parameters
//				- sunrise
//				- sunset
//				- day length
//				- declination of the sun
//				Input parameters:
//				- Julian day
//				- the latitude of the location
/////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////
// ToDo
// -------------------------------------------------------------------------
// - 
//
//
/////////////////////////////////////////////////////////////////////////////

#ifndef __day_length_h
#define __day_length_h

//-------------------------------------------------------------------
static double CONV = 3.14159265359 / 180.0;
static double PI	= 3.14159265359;
//-------------------------------------------------------------------

class Cday_length
{
public:

	Cday_length();
	Cday_length(int JulianDay, double Latitude);

	//-------------------------------------------------------------------
	// Use this constructor if you are interested in an array of day length
	// double DayLength[366].
	// After creating an instance of this class,
	// you have access to the array DayLength[] that stores for each day (1-366)
	// in the year the day length corresponding to the given latitude.
	// Use function "double Get_DayLength(int JulianDay)" to receive 
	// the day length of the corresponding Julian day.
	Cday_length(double Latitude);
	//-------------------------------------------------------------------
	
	~Cday_length();

	double			Calc_Declination(int JulianDay, double CONV);

	double			Calc_Sunrise(double Latitude, double Declination);

	double			Calc_Sunset(double Latitude, double Declination)
						{return(-	Calc_Sunrise(Latitude,Declination));};

	double			Calc_DayLength();

	//---------------------------------------------------------------
	// GET FUNCTIONS
	//---------------------------------------------------------------
	int				Get_Sunrise_HH(double sunrise)	{ return(int(_timeconv_sr(sunrise))); };
	int				Get_Sunrise_MM(double sunrise)	{ return(int((_timeconv_sr(sunrise) - int(_timeconv_sr(sunrise) ))*60)); };
	int				Get_Sunset_HH(double sunrise)	{ return(int(_timeconv_ss(-sunrise))); };
	int				Get_Sunset_MM(double sunrise)	{ return(int((_timeconv_ss(-sunrise) - int(_timeconv_ss(-sunrise) ))*60)); };
	double			Get_DayLength(int JulianDay)	{ return(DayLength[JulianDay - 1]); };
	double			Get_DayLength(double sunrise)	{ return(-sunrise / PI * 180 / 15 * 2); };
	double			Get_DayLength(int JulianDay, double Latitude);
	double			Get_Declination()				{ return(Declination); };
	double			Get_Sunrise()					{ return(Sunrise); };
	double			Get_Sunset()					{ return(Sunset); };
	//---------------------------------------------------------------

private:
	int				JulianDay;
	double			Latitude;		// in degrees
	double			Declination;
	double			Sunrise;
	double			Sunset;

	int				Sunrise_HH;
	int				Sunrise_MM;
	int				Sunset_HH;
	int				Sunset_MM;

	double			DayLength[366];

	double			_timeconv_sr(double sunrise)
						{return(12 - ((sunrise / PI * 180) / 15));};

	double			_timeconv_ss(double sunrise)
						{return(12 + ((sunrise / PI * 180) / 15));};

/*
     aaw = zeitkonv_auf(sr)
	 aaw1 = int(aaw)
	 aaw2 = (aaw - aaw1) * 60
	 aaw3 = int(aaw2)
*/
};

#endif /* __day_length_h */