/**********************************************************
 * Version $Id: AvWiMa1.cpp 911 2011-02-14 16:38:15Z reklov_w $
 *********************************************************/

///////////////////////////////////////////////////////////
//                                                       //
//                        Tool:                        //
//                        AvWiMa                         //
//                                                       //
//                       for SAGA                        //
//      System for Automated Geoscientific Analyses      //
//                                                       //
//-------------------------------------------------------//
//                                                       //
//                      AvWiMa1.cpp                      //
//                                                       //
//                                                       //
//-------------------------------------------------------//
//                                                       //
//                                                       //
//    by Alessandro Perego (Italy)                       //
//                                                       //
//    e-mail:     alper78@alice.it                       //
//                                                       //
//                                                       //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
#include "AvWiMa1.h"

//---------------------------------------------------------
CAvWiMa1::CAvWiMa1(void)
{
	// 1. Info...
	Set_Name(_TL("Average With Mask 1"));
	Set_Author(_TL("Alessandro Perego"));
	Set_Description(_TL("Average With Mask 1 calculates average for cells specified by a mask grid. Cell excluded by the mask grid are NOT used in the average calculation."));

	// 2. Parameters...
	Parameters.Add_Grid(NULL, "INPUT", _TL("Input"), _TL("This must be your input data of type grid."), PARAMETER_INPUT);
	Parameters.Add_Grid(NULL, "MASK", _TL("Mask Grid"), _TL("This grid indicates the cells you want calculate the average."), PARAMETER_INPUT);
	Parameters.Add_Grid(NULL, "RESULT", _TL("AWM1 Grid"), _TL("New grid filtered with the AvWiMa1 tool"), PARAMETER_OUTPUT);
	Parameters.Add_Value(NULL, "V", _TL("Mask value"), _TL("Value of right cells in the Mask Grid"), PARAMETER_TYPE_Double, 1.0);
	Parameters.Add_Value(NULL, "RX", _TL("Radius X"), _TL(""), PARAMETER_TYPE_Int, 1, 1, true);
	Parameters.Add_Value(NULL, "RY", _TL("Radius Y"), _TL(""), PARAMETER_TYPE_Int, 1, 1, true);
}

//---------------------------------------------------------
CAvWiMa1::~CAvWiMa1(void)
{}

//---------------------------------------------------------
bool CAvWiMa1::On_Execute(void)
{
	int  x, y, Rx, Ry, ax, ay, bx, by, ix, iy, n;
	double  Sum, m, im, v;
	CSG_Grid  *pInput, *pMask, *pResult;

	pInput = Parameters("INPUT")->asGrid();
	pMask = Parameters("MASK")->asGrid();
	pResult	= Parameters("RESULT")->asGrid();
	v = Parameters("V")->asDouble();
	Rx = Parameters("RX")->asInt();
	Ry = Parameters("RY")->asInt();

	//-----------------------------------------------------
	for(y=0; y<Get_NY() && Set_Progress(y); y++)
	{
		for(x=0; x<Get_NX(); x++)
		{
			m = pMask->asDouble(x, y);
			if( m == v)
			{
				Sum = 0.0;
				n = 0;
				
				ax = x - Rx;
				bx = x + Ry;

				ay = y - Ry;
				by = y + Ry;
				
				if( ax <  0 )		{	ax	= 0;			}	
				if( bx >= Get_NX() ){	bx	= Get_NX() - 1;	}
				if( ay <  0 )		{	ay	= 0;			}
				if( by >= Get_NY() ){	by	= Get_NY() - 1;	}

				for(iy=ay; iy<=by; iy++)
				{
					for(ix=ax; ix<=bx; ix++)
					{
						im = pMask->asDouble(ix, iy);
						if( im == v )
						{
							Sum += pInput->asDouble(ix, iy);
							n++;
						}
					}
				}

				if( n > 0 )
				{
					pResult->Set_Value(x, y, Sum/n);
				}
			}
			else
			{
				pResult->Set_Value(x, y, pInput->asDouble(x, y));
			}
		}
	}
	return( true );
}

	