//---------------------------------------------------------
void CGrid_Merge::Get_Match(CSG_Grid *pGrid)
{
	if( pGrid && Parameters("MATCH")->asInt() )
	{
		Process_Set_Text(CSG_String::Format("%s: %s", _TL("matching histogram"), pGrid->Get_Name()));

		int	ax	= (int)((pGrid->Get_XMin() - m_pMosaic->Get_XMin()) / m_pMosaic->Get_Cellsize());	if( ax < 0 )	ax	= 0;
		int	ay	= (int)((pGrid->Get_YMin() - m_pMosaic->Get_YMin()) / m_pMosaic->Get_Cellsize());	if( ay < 0 )	ay	= 0;

		int	nx	= 1 + m_pMosaic->Get_System().Get_xWorld_to_Grid(pGrid->Get_XMax()); if( nx > m_pMosaic->Get_NX() )	nx	= m_pMosaic->Get_NX();
		int	ny	= 1 + m_pMosaic->Get_System().Get_yWorld_to_Grid(pGrid->Get_YMax()); if( ny > m_pMosaic->Get_NY() )	ny	= m_pMosaic->Get_NY();

		CSG_Vector	Z[2];

		for(int y=ay; y<ny && Set_Progress(y-ay, ny-ay); y++)
		{
			double	py	= m_pMosaic->Get_YMin() + y * m_pMosaic->Get_Cellsize();

			for(int x=ax; x<nx; x++)
			{
				if( !m_pMosaic->is_NoData(x, y) )
				{
					double	z, px	= m_pMosaic->Get_XMin() + x * m_pMosaic->Get_Cellsize();

					if( pGrid->Get_Value(px, py, z, GRID_INTERPOLATION_NearestNeighbour) )
					{
						Z[0].Add_Row(z);
						Z[1].Add_Row(m_pMosaic->asDouble(x, y));
					}
				}
			}
		}

		CSG_Regression	r;

		if( r.Calculate((int)Z[0].Get_Size(), Z[0].Get_Data(), Z[1].Get_Data()) )
		{
			m_Match.Create(2);

			m_Match[0]	= r.Get_Constant();
			m_Match[1]	= r.Get_Coefficient();

			Message_Add("histogram stretch:\n", false);
			Message_Add(r.asString(), false);

			return;
		}
	}

	m_Match.Destroy();
}
//---------------------------------------------------------
bool CTopographic_Correction::Get_Model(void)
{
	//-----------------------------------------------------
	m_pOriginal		= Parameters("ORIGINAL")	->asGrid();
	m_pCorrected	= Parameters("CORRECTED")	->asGrid();

	m_pCorrected	->Set_Name(CSG_String::Format(SG_T("%s [%s]"), m_pOriginal->Get_Name(), _TL("Topographic Correction")));

	m_Method		= Parameters("METHOD")		->asInt();

	m_Minnaert		= Parameters("MINNAERT")	->asDouble();

	switch( Parameters("MAXVALUE")->asInt() )
	{
	default:	m_maxValue	=   255;	break;
	case  1:	m_maxValue	= 65535;	break;
	}

	switch( m_Method )
	{
	//-----------------------------------------------------
	case 5:	// C Correction
		{
			Process_Set_Text(_TL("Regression Analysis"));

			CSG_Regression	R;

			sLong n		= Parameters("MAXCELLS")->asInt();
			int	nStep	= Get_NCells() < n ? 1 : (int)(Get_NCells() / n);

			for(n=0; n<Get_NCells() && Set_Progress_NCells(n); n+=nStep)
			{
				R.Add_Values(m_pOriginal->asDouble(n), m_Illumination.asDouble(n));
			}

			if( !R.Calculate() || !R.Get_Constant() )
			{
				return( false );
			}

			m_C	= R.Get_Coefficient() / R.Get_Constant();

			Message_Add(R.asString());
		}
		break;

	//-----------------------------------------------------
	case 6:	// Normalization (after Civco, modified by Law & Nichol)
		{
			m_C	= 1.0;
		}
		break;
	}

	//-----------------------------------------------------
	return( true );
}