///////////////////////////////////////////////////////////
// File:			rr_dlg_preproc.cpp
//
// Author:			Stefan Liersch
// Contact:			stefan.liersch@googlemail.com,
//					stefan.liersch@ufz.de
//
// Date:			2007-08-10
// last modified:	2007-09-02
//
//---------------------------------------------------------
// DESCRIPTION
//---------------------------------------------------------
// DIALOG for
// Preprocessing time series data for rainfall-runoff models
//
//---------------------------------------------------------
// ToDo
//---------------------------------------------------------
// 
//
///////////////////////////////////////////////////////////

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

#include "hydrology/rr_dlg_preproc.h"
#include "hydrology/rr_preprocessing.h"
#include "pgsql/pg_date_dlg.h"

// SAGA Header
#include "saga_gui/saga_frame.h"		// location of class Cpgdatabase
										// using global *g_pSAGA_Frame pointer
#include "saga_gui/wksp_data_manager.h" // g_pData
#include "saga_gui/wksp_simplefeature_manager.h"
#include "saga_gui/wksp_layer.h"
#include "saga_gui/res_images.h"
#include "res_dialogs.h"
#include "saga_gui/helper.h"			// using function MSG_General_Add()
#define MSG_ADD(s)	MSG_General_Add(s, true, true);

#include <fstream> // for test output only
//---------------------------------------------------------------------
// IDs defined in 'res_commands.h'
BEGIN_EVENT_TABLE(Crr_dlg_preproc, wxDialog)
	EVT_BUTTON(ID_BUTTON_Select_Discharge,	_On_Button_Select_Discharge)
	EVT_BUTTON(ID_BUTTON_Select_PCP,		_On_Button_Select_precipitation)
	EVT_BUTTON(ID_BUTTON_Select_TMP,		_On_Button_Select_temperature)
	EVT_BUTTON(ID_BUTTON_GetTimeSeries,		_On_Button_GetTimeSeries)
	EVT_BUTTON(ID_BUTTON_PG_WEIGHT_TS1,		_On_Button_Weight_TS1)
	EVT_BUTTON(ID_BUTTON_PG_WEIGHT_TS2,		_On_Button_Weight_TS2)
	EVT_BUTTON(wxID_OK,						_On_Button_OK)
END_EVENT_TABLE()

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


//---------------------------------------------------------------------
// CONSTRUCTORS
Crr_dlg_preproc::Crr_dlg_preproc(wxWindow *parent, wxWindowID id, const wxString &title)
								: wxDialog(parent, id, title,
								  wxDefaultPosition, wxDefaultSize,
								  wxDEFAULT_DIALOG_STYLE)
{
	_CreateDlg();
	_ZeroAll();
}
//---------------------------------------------------------------------

Crr_dlg_preproc::Crr_dlg_preproc()
{
	_ZeroAll();
}
//---------------------------------------------------------------------

// DESTRUCTOR
Crr_dlg_preproc::~Crr_dlg_preproc()
{
	delete[] m_pDischarge;
	delete[] m_pPCP;
	delete[] m_pTMP;
	delete m_LUT_discharge;
	delete m_LUT_pcp;
	delete m_LUT_tmp;
	
	this->Destroy();
}
//---------------------------------------------------------------------

///////////////////////////////////////////////////////////
//
//	DIALOG design
//
///////////////////////////////////////////////////////////

void Crr_dlg_preproc::_CreateDlg()
{
	wxBoxSizer *mainBoxSizer = new wxBoxSizer( wxVERTICAL );

	// BOX DISCHRARGE --------------------------------------
	wxStaticBox *statbox_ds = new wxStaticBox( this, -1, wxT("Discharge Time Series") );
	statbox_ds->SetFont( wxFont( 8, wxSWISS, wxNORMAL, wxBOLD ) );
    wxStaticBoxSizer *StatBoxDischarge = new wxStaticBoxSizer( statbox_ds, wxHORIZONTAL );

	m_pTextCtrl_Discharge = new wxTextCtrl(this, ID_TEXT_DischargeTable, wxT(""), wxDefaultPosition, wxSize(150,-1), 0);
	m_pTextCtrl_Discharge->SetEditable(false);
	m_pButSelectDischarge = new wxButton(this, ID_BUTTON_Select_Discharge, wxT("Get Discharge Gauge"), wxDefaultPosition, wxSize(200,-1), 0);
	StatBoxDischarge->Add(m_pTextCtrl_Discharge, 0, wxALIGN_LEFT|wxALL, 5);
	StatBoxDischarge->Add(m_pButSelectDischarge, 0, wxALIGN_LEFT|wxALL, 5);
	// BOX DISCHRARGE --------------------------------------

	// BOX PRECIPITATION------------------------------------
	wxStaticBox *statbox_pcp = new wxStaticBox( this, -1, wxT("Precipitation Time Series") );
	statbox_pcp->SetFont( wxFont( 8, wxSWISS, wxNORMAL, wxBOLD ) );
    wxStaticBoxSizer *StatBoxPCP = new wxStaticBoxSizer( statbox_pcp, wxHORIZONTAL );

	m_pChoicePCP = new wxChoice(this, ID_CHOICE_PCP, wxDefaultPosition, wxSize(150,-1), NULL, 0);
	m_pButSelectPCP = new wxButton(this, ID_BUTTON_Select_PCP, wxT("Get Precipitation Gauge(s)"), wxDefaultPosition, wxSize(200,-1), 0);

	m_pButWeight_TS1 = new wxBitmapButton( this, ID_BUTTON_PG_WEIGHT_TS1, IMG_Get_Bitmap(ID_IMG_PG_WEIGHT), wxDefaultPosition, wxSize(28,28), wxBU_AUTODRAW );
	m_pButWeight_TS1->Disable();
	m_pButWeight_TS1->SetToolTip("Define weighting factors");

	StatBoxPCP->Add(m_pChoicePCP, 0, wxALIGN_LEFT|wxALL, 5);
	StatBoxPCP->Add(m_pButSelectPCP, 0, wxALIGN_LEFT|wxALL, 5);
	StatBoxPCP->Add(m_pButWeight_TS1, 0, wxALIGN_CENTER|wxALL, 5);
	// BOX PRECIPITATION------------------------------------

	// BOX TEMPERATURE -------------------------------------
	wxStaticBox *statbox_tmp = new wxStaticBox( this, -1, wxT("Temperature Time Series") );
	statbox_tmp->SetFont( wxFont( 8, wxSWISS, wxNORMAL, wxBOLD ) );
    wxStaticBoxSizer *StatBoxTMP = new wxStaticBoxSizer( statbox_tmp, wxHORIZONTAL );

	m_pChoiceTMP = new wxChoice(this, ID_CHOICE_TMP, wxDefaultPosition, wxSize(150,-1), NULL, 0);
	m_pButSelectTMP = new wxButton(this, ID_BUTTON_Select_TMP, wxT("Get Temperature Gauge(s)"), wxDefaultPosition, wxSize(200,-1), 0);

	m_pButWeight_TS2 = new wxBitmapButton( this, ID_BUTTON_PG_WEIGHT_TS2, IMG_Get_Bitmap(ID_IMG_PG_WEIGHT), wxDefaultPosition, wxSize(28,28), wxBU_AUTODRAW );
	m_pButWeight_TS2->Disable();
	m_pButWeight_TS2->SetToolTip("Define weighting factors");

	StatBoxTMP->Add(m_pChoiceTMP, 0, wxALIGN_LEFT|wxALL, 5);
	StatBoxTMP->Add(m_pButSelectTMP, 0, wxALIGN_LEFT|wxALL, 5);
	StatBoxTMP->Add(m_pButWeight_TS2, 0, wxALIGN_CENTER|wxALL, 5);

	// BOX TEMPERATURE -------------------------------------

	// BUTTON BOX ------------------------------------------
    wxBoxSizer *ButBoxSizer = new wxBoxSizer( wxHORIZONTAL );
    m_pButOK = new wxButton( this, wxID_OK, wxT("Create Model Input"), wxDefaultPosition, wxDefaultSize, 0 );
	m_pButOK->Disable();
    ButBoxSizer->Add( m_pButOK, 0, wxALIGN_CENTER|wxALL, 5 );
    wxButton *ButCancel = new wxButton( this, wxID_CANCEL, wxT("Cancel"), wxDefaultPosition, wxDefaultSize, 0 );
    ButBoxSizer->Add( ButCancel, 0, wxALIGN_CENTER|wxALL, 5 );
	m_pButGetTimeSeries = new wxButton( this, ID_BUTTON_GetTimeSeries, wxT("Analyze Time Series"), wxDefaultPosition, wxDefaultSize, 0 );
	ButBoxSizer->Add( m_pButGetTimeSeries, 0, wxALIGN_CENTER|wxALL, 5 );
	// BUTTON BOX ------------------------------------------

	// Add items to mainBoxSizer
	mainBoxSizer->Add(StatBoxDischarge,0,wxALIGN_LEFT|wxALL,5);
	mainBoxSizer->Add(StatBoxPCP,0,wxALIGN_LEFT|wxALL,5);
	mainBoxSizer->Add(StatBoxTMP,0,wxALIGN_LEFT|wxALL,5);
	mainBoxSizer->Add(ButBoxSizer, 0, wxALIGN_CENTER|wxALL, 5 );

    this->SetSizer( mainBoxSizer );
	mainBoxSizer->Fit(this);
    mainBoxSizer->SetSizeHints( this );

}

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

void Crr_dlg_preproc::_ZeroAll()
{
	m_LUT_discharge = NULL;
	m_LUT_pcp		= NULL;
	m_LUT_tmp		= NULL;
	m_pDischarge	= NULL;
	m_pPCP			= NULL;
	m_pTMP			= NULL;
}
//---------------------------------------------------------------------
///////////////////////////////////////////////////////////
//
//	EVENT TABLE FUNCTIONS
//
///////////////////////////////////////////////////////////

void Crr_dlg_preproc::_On_Button_Select_Discharge(wxCommandEvent &event)
{
	class CWKSP_SimpleFeature	*pWKSP_SimpleFeature = NULL;
	CSG_SimpleFeature			*pSimpleFeature = NULL;
	wxString					PK_ColumnName = "";
	int							PK_ColumnNumber = 0;
	wxString					LUT_Name = "";
	int							*ID;
	int							nIDs = 0;

	pSimpleFeature = Get_ActiveFeature();

	if ( pSimpleFeature != NULL )
		pWKSP_SimpleFeature = Get_ActiveWKSP_Shapes(pSimpleFeature);

	if ( pWKSP_SimpleFeature != NULL )
		LUT_Name = Get_LUT_Name(pWKSP_SimpleFeature, PK_ColumnNumber, PK_ColumnName);

	if ( LUT_Name != "" )
	{
		if (pSimpleFeature->Get_Selection_Count() > 0)
		{
			ID = new int[pSimpleFeature->Get_Selection_Count()];
			// assign parameters ID and nIDs
			ID = Get_StationIDs(pSimpleFeature, ID, nIDs, PK_ColumnNumber);
			// load look up table
			m_LUT_discharge = new CLU_Table4Col(LUT_Name,ID,nIDs);
			// assign precipitation time series
			m_pDischarge = new CTimeSeries[nIDs];
			Assign_TimeSeries(m_LUT_discharge, nIDs, m_pDischarge);

			m_pTextCtrl_Discharge->Clear();
			m_pTextCtrl_Discharge->SetValue(m_pDischarge[0].Get_Name());

			delete m_LUT_discharge;
			delete[] ID;
		} else {
			SG_UI_Dlg_Message("Please select a discharge gauge","Error: No Gauge Selected");
		}
	}
}
//---------------------------------------------------------------------

void Crr_dlg_preproc::_On_Button_Select_precipitation(wxCommandEvent &event)
{
	class CWKSP_SimpleFeature	*pWKSP_SimpleFeature = NULL;
	CSG_SimpleFeature			*pSimpleFeature = NULL;
	wxString					PK_ColumnName = "";
	int							PK_ColumnNumber = 0;
	wxString					LUT_Name = "";
	int							*ID;
	int							nIDs = 0;
	
	pSimpleFeature = Get_ActiveFeature();

	if ( pSimpleFeature != NULL )
		pWKSP_SimpleFeature = Get_ActiveWKSP_Shapes(pSimpleFeature);

	if ( pWKSP_SimpleFeature != NULL )
		LUT_Name = Get_LUT_Name(pWKSP_SimpleFeature, PK_ColumnNumber, PK_ColumnName);

	if ( LUT_Name != "" )
	{
		if (pSimpleFeature->Get_Selection_Count() > 0)
		{
			ID = new int[pSimpleFeature->Get_Selection_Count()];
			// assign parameters ID and nIDs
			ID = Get_StationIDs(pSimpleFeature, ID, nIDs, PK_ColumnNumber);
			// load look up table
			m_LUT_pcp = new CLU_Table4Col(LUT_Name,ID,nIDs);
			// assign precipitation time series
			m_pPCP = new CTimeSeries[nIDs];
			Assign_TimeSeries(m_LUT_pcp, nIDs, m_pPCP);

			m_pChoicePCP->Clear();
			wxString tmpName;
			for (int i = 0; i < nIDs; i++)
			{
				tmpName.append(wxString::Format("%d",ID[i]));
				tmpName.append(" | ");
				tmpName.append(m_pPCP[i].Get_Name());
				m_pChoicePCP->Append(tmpName);
				tmpName.Clear();
				//m_pChoicePCP->Append(m_pPCP[i].Get_Name());
			}
			m_pChoicePCP->SetSelection(0);

			m_pButWeight_TS1->Enable();

			delete m_LUT_pcp;
			delete[] ID;
		} else {
			SG_UI_Dlg_Message("Please select one ore more precipitation gauge(s)","Error: No Gauge Selected");
		}
	}
}
//---------------------------------------------------------------------

void Crr_dlg_preproc::_On_Button_Weight_TS1(wxCommandEvent &event)
{
	Create_DLG_Weight(m_pPCP, m_pChoicePCP);
}
//---------------------------------------------------------------------

void Crr_dlg_preproc::Create_DLG_Weight(CTimeSeries *pTS, wxChoice *choice)
{
	CSG_Parameters			P;
	wxArrayString			gauge;

	P.Set_Name("Set the weighting factors of the gauges");

	for (int i = 0; i < choice->GetCount(); i++)
	{
		gauge.Add(choice->GetString(i));
		P.Add_Value(
			NULL	, gauge[i].c_str()	, gauge[i].c_str(),
			"The weighting factor of a gauge can be a value between 1 and 100",
			PARAMETER_TYPE_Int, 1, 1, true, 100, true
		);
	}
	//-----------------------------------------------------
	if( DLG_Parameters(&P) )
	{
		for (int i = 0; i < choice->GetCount(); i++)
		{
			pTS[i].Set_Weight(P(gauge[i])->asInt());
			MSG_ADD(wxString::Format("%s: %d",pTS[i].Get_Name().c_str(),pTS[i].Get_weight()));
		}
	}
}
//---------------------------------------------------------------------

void Crr_dlg_preproc::_On_Button_Select_temperature(wxCommandEvent &event)
{
	class CWKSP_SimpleFeature	*pWKSP_SimpleFeature = NULL;
	CSG_SimpleFeature			*pSimpleFeature = NULL;
	wxString					PK_ColumnName = "";
	int							PK_ColumnNumber = 0;
	wxString					LUT_Name = "";
	int							*ID;
	int							nIDs = 0;

	pSimpleFeature = Get_ActiveFeature();

	if ( pSimpleFeature != NULL )
		pWKSP_SimpleFeature = Get_ActiveWKSP_Shapes(pSimpleFeature);

	if ( pWKSP_SimpleFeature != NULL )
		LUT_Name = Get_LUT_Name(pWKSP_SimpleFeature, PK_ColumnNumber, PK_ColumnName);

	if ( LUT_Name != "" )
	{
		if (pSimpleFeature->Get_Selection_Count() > 0)
		{
			ID = new int[pSimpleFeature->Get_Selection_Count()];
			// assign parameters ID and nIDs
			ID = Get_StationIDs(pSimpleFeature, ID, nIDs, PK_ColumnNumber);
			// load look up table
			m_LUT_tmp = new CLU_Table4Col(LUT_Name,ID,nIDs);
			// assign precipitation time series
			m_pTMP = new CTimeSeries[nIDs];
			Assign_TimeSeries(m_LUT_tmp, nIDs, m_pTMP);
			
			m_pChoiceTMP->Clear();
			wxString tmpName;
			for (int i = 0; i < nIDs; i++)
			{
				tmpName.append(wxString::Format("%d",ID[i]));
				tmpName.append(" | ");
				tmpName.append(m_pTMP[i].Get_Name());
				m_pChoiceTMP->Append(tmpName);
				tmpName.Clear();
				//m_pChoiceTMP->Append(m_pTMP[i].Get_Name());
			}
			m_pChoiceTMP->SetSelection(0);

			m_pButWeight_TS2->Enable();

			delete m_LUT_tmp;
			delete[] ID;
		} else {
			SG_UI_Dlg_Message("Please select one ore more temperature station(s)","Error: No Station Selected");
		}
	}
}
//---------------------------------------------------------------------

void Crr_dlg_preproc::_On_Button_Weight_TS2(wxCommandEvent &event)
{
	Create_DLG_Weight(m_pTMP, m_pChoiceTMP);
}
//---------------------------------------------------------------------

void Crr_dlg_preproc::_On_Button_GetTimeSeries(wxCommandEvent &event)
{
	_Create_TimeSeries();
}
//---------------------------------------------------------------------

void Crr_dlg_preproc::_On_Button_OK(wxCommandEvent &event)
{
	CRR_preprocessing	*rr = new CRR_preprocessing(m_pDischarge,1,
												    m_pPCP, m_pChoicePCP->GetCount(),
													m_pTMP, m_pChoiceTMP->GetCount(),
													m_pDischarge->Get_Date_Begin(), m_pDischarge->Get_Date_End());

	//this->Destroy();
	m_pButOK->Disable();
	delete rr;
}
//---------------------------------------------------------------------

/*
bool Crr_dlg_preproc::_Select_Init(CSG_SimpleFeature *pSimpleFeature, CWKSP_Shapes *pWKSP_Shapes, wxString &LUT_Name,
								   int &PK_ColumnNumber, wxString &PK_ColumnName)
{
	pSimpleFeature = Get_ActiveFeature();

	if ( pSimpleFeature != NULL )
	{
		pWKSP_Shapes = Get_ActiveWKSP_Shapes(pSimpleFeature);
	} else {
		return(false);
	}
	if ( pWKSP_Shapes != NULL )
	{
		LUT_Name = Get_LUT_Name(pWKSP_Shapes, PK_ColumnNumber, PK_ColumnName);
		return(true);
	} else {
		return(false);
	}
}
*/
//---------------------------------------------------------------------

CSG_SimpleFeature *Crr_dlg_preproc::Get_ActiveFeature()
{
	if (Get_Active_Layer())	// CWKSP_Layer
	{
		CSG_Data_Object* m_pObject = Get_Active_Layer()->Get_Object();
		// Check, if a SimpleFeature is active
		if (m_pObject->Get_ObjectType() == DATAOBJECT_TYPE_SimpleFeature)
		{
			return(m_pObject->asSimpleFeature());
		} else {
			SG_UI_Dlg_Message("Please select a SimpleFeature Layer","Error: Wrong Layer Type Selected");
			return(NULL);
		}
	} else {
		SG_UI_Dlg_Message("Please select a SimpleFeature Layer!","Error: No Layer Selected");
		return(NULL);
	}
}
//---------------------------------------------------------------------

CWKSP_SimpleFeature *Crr_dlg_preproc::Get_ActiveWKSP_Shapes(CSG_SimpleFeature *pSimpleFeature)
{
	class CWKSP_SimpleFeature_Manager	*pSimpleFeature_Manager;

	if (Get_Active_Layer())	// CWKSP_Layer
	{
		pSimpleFeature_Manager = g_pData->Get_SimpleFeatures();
		return( pSimpleFeature_Manager->Get_Shapes(pSimpleFeature) );
	} else {
		return(NULL);
	}
}
//---------------------------------------------------------------------

wxString Crr_dlg_preproc::Get_LUT_Name(CWKSP_SimpleFeature *pWKSP_SimpleFeature, int &PK_ColumnNumber, wxString &PK_ColumnName)
{
	return( pWKSP_SimpleFeature->_Select_LU_Table(PK_ColumnNumber, PK_ColumnName) );
}
//---------------------------------------------------------------------

int *Crr_dlg_preproc::Get_StationIDs(CSG_SimpleFeature *pSimpleFeature, int *ID, int &nIDs, int PK_ColumnNumber)
{
	if ( pSimpleFeature->Get_Selection_Count() > 0 )
	{
		nIDs = pSimpleFeature->Get_Selection_Count();
		for (int i = 0; i < pSimpleFeature->Get_Selection_Count(); i++)
		{
			ID[i] = pSimpleFeature->Get_Selection(i)->Get_Record()->asInt(PK_ColumnNumber);
		}
	}
	return(ID);
}
//---------------------------------------------------------------------

void Crr_dlg_preproc::Assign_TimeSeries(CLU_Table4Col *lut, int nTS, CTimeSeries *ts)
{
	for (int i = 0; i < nTS; i++)
	{
		ts[i].Set_Name(lut->Get_TSStationName(i));
		ts[i].Set_DateCol(lut->Get_TSDateName(i));
		ts[i].Set_ValueCol(lut->Get_TSValueName(i));
	}
}
//---------------------------------------------------------------------

wxArrayString Crr_dlg_preproc::_Get_TimeSeriesNames(CWKSP_SimpleFeature *pWKSP_SimpleFeature, CSG_SimpleFeature *pSimpleFeature,
													wxString LUT_Name, int PK_ColumnNumber, wxString PK_ColumnName)
{
	wxArrayString	aPK_Value_str;
	wxArrayString	aTableNames;		// return value
	wxArrayString	atemp;

	if (pSimpleFeature->Get_Selection_Count() > 0)
	{
		for (int i = 0; i < pSimpleFeature->Get_Selection_Count(); i++)
		{
			aPK_Value_str.Add(pSimpleFeature->Get_Selection(i)->Get_Record()->asString(PK_ColumnNumber));
			atemp = pWKSP_SimpleFeature->_Get_Station_TableName(aPK_Value_str[i], LUT_Name,PK_ColumnName);
			for (unsigned int y = 0; y < atemp.size(); y++)
				aTableNames.Add(atemp[y]);
		}
	}
	return(aTableNames);
}
//---------------------------------------------------------------------

///////////////////////////////////////////////////////////
//
//	PUBLIC GET FUNCTIONS
//
///////////////////////////////////////////////////////////

const char* Crr_dlg_preproc::Get_Discharge_Name()
{
	m_pTextCtrl_Discharge->SelectAll();
	return( m_pTextCtrl_Discharge->GetStringSelection().c_str() );
}
//---------------------------------------------------------------------

wxArrayString Crr_dlg_preproc::Get_PCP_Names()
{
	wxArrayString	pcpStations;
	for (int i = 0; i < m_pChoicePCP->GetCount(); i++)
		pcpStations.Add(m_pChoicePCP->GetString(i));
	return(pcpStations);
}
//---------------------------------------------------------------------

wxArrayString Crr_dlg_preproc::Get_TMP_Names()
{
	wxArrayString	tmpStations;
	for (int i = 0; i < m_pChoiceTMP->GetCount(); i++)
		tmpStations.Add(m_pChoiceTMP->GetString(i));
	return(tmpStations);
}

///////////////////////////////////////////////////////////
//
//	TIME SERIES FUNCTIONS
//
///////////////////////////////////////////////////////////

void Crr_dlg_preproc::_Create_TimeSeries()
{
	//wxDateTime begin_discharge = wxDateTime::UNow();
	wxDateTime begin_discharge;
	begin_discharge.Set(g_pSAGA_Frame->db->Init_wxDateTime().GetJDN());
	//wxDateTime end_discharge = wxDateTime::UNow();
	wxDateTime end_discharge;
	end_discharge.Set(g_pSAGA_Frame->db->Init_wxDateTime().GetJDN());

	if ( m_pDischarge	!= NULL &&
		 m_pPCP			!= NULL &&
		 m_pTMP			!= NULL )
	{
		CDateDLG *date_dlg = new CDateDLG(g_pSAGA_Frame, wxID_ANY, wxT("Select a Period"));
		begin_discharge	= Get_Begin(m_pDischarge->Get_Name().c_str(),m_pDischarge->Get_date_col().c_str());
		end_discharge	= Get_End(m_pDischarge->Get_Name().c_str(),m_pDischarge->Get_date_col().c_str());
		date_dlg->Set_Date1(begin_discharge);
		date_dlg->Set_Date2(end_discharge);
		if (date_dlg->ShowModal() == wxID_OK)
		{
			m_pDischarge->Set_Date_Begin(date_dlg->Get_Date1());
			m_pDischarge->Set_Date_End(date_dlg->Get_Date2());
			m_pButOK->Enable();
		}
	} else {
		if ( m_pDischarge == NULL )
			SG_UI_Dlg_Message("Please select a discharge gauge!","Error: No Gauge Selected");
		if ( m_pPCP == NULL )
			SG_UI_Dlg_Message("Please select a precipitation gauge!","Error: No Gauge Selected");
		if ( m_pTMP == NULL )
			SG_UI_Dlg_Message("Please select a temperature gauge!","Error: No Gauge Selected");
	}
}
//---------------------------------------------------------------------

wxDateTime Crr_dlg_preproc::Get_Begin(const char *tablename, const char* date_col)
{
	//wxDateTime begin = wxDateTime::UNow();
	wxDateTime begin;
	begin.Set(g_pSAGA_Frame->db->Init_wxDateTime().GetJDN());
	begin.SetYear(g_pSAGA_Frame->db->GetFirstYear(tablename,date_col));
	begin.SetDay(g_pSAGA_Frame->db->GetFirstDay(tablename,date_col));
	g_pSAGA_Frame->db->AssignMonthByInt(begin, g_pSAGA_Frame->db->GetFirstMonth(tablename,date_col)-1);
		
	return(begin);
}
//---------------------------------------------------------------------

wxDateTime Crr_dlg_preproc::Get_End(const char *tablename, const char* date_col)
{
	//wxDateTime end = wxDateTime::UNow();
	wxDateTime end;
	end.Set(g_pSAGA_Frame->db->Init_wxDateTime().GetJDN());
	end.SetYear(g_pSAGA_Frame->db->GetLastYear(tablename,date_col));
	end.SetDay(g_pSAGA_Frame->db->GetLastDay(tablename,date_col));
	g_pSAGA_Frame->db->AssignMonthByInt(end, g_pSAGA_Frame->db->GetLastMonth(tablename,date_col)-1);
		
	return(end);
}
//---------------------------------------------------------------------
