#include "model_tools.h"
#include <math.h>
#include <stdlib.h> // random numbers

double model_tools::CalcEfficiency(double *obs, double *sim, int size)
{
	double sum_obsminsim_2	= 0.0;
	double sum_obsminmean_2 = 0.0;
	double mean_obs			= 0.0;
	double sum_obs			= 0.0; // sum of observed discharge
	double eff				= 0.0; // Nash-Sutcliffe Efficiency

	for (int i = 0; i < size; i++)
	{
		sum_obsminsim_2 += pow((obs[i] - sim[i]),2);
		sum_obs += obs[i];
	}
	mean_obs = sum_obs / size;
	for (int i = 0; i < size; i++)
	{
		sum_obsminmean_2 += pow((obs[i] - mean_obs),2);
	}
	eff = 1 - (sum_obsminsim_2 / sum_obsminmean_2);

	return (eff);
}


//-------------------------------------------------------------------
double model_tools::Random_double(double lb, double ub)
{
	// lb = lower bound, ub = upper bound
	double random;

	random = rand()/(1.+RAND_MAX);
	random = lb + (ub - lb) * random;

	return(random);
}
//-------------------------------------------------------------------

bool model_tools::LeapYear(int year)
{
	if ( (year % 4   == 0) &&
		 (year % 100 != 0) ||
		 (year % 400 == 0) )
		 return(true);
	else 
		return(false);
}
//-------------------------------------------------------------------

