1 /**********************************************************
2  * Version $Id$
3  *********************************************************/
4 
5 ///////////////////////////////////////////////////////////
6 //                                                       //
7 //                         SAGA                          //
8 //                                                       //
9 //      System for Automated Geoscientific Analyses      //
10 //                                                       //
11 //                     Tool Library                      //
12 //                     Grid_Calculus                     //
13 //                                                       //
14 //-------------------------------------------------------//
15 //                                                       //
16 //                  Grid_Difference.cpp                  //
17 //                                                       //
18 //                 Copyright (C) 2009 by                 //
19 //                      Olaf Conrad                      //
20 //                                                       //
21 //-------------------------------------------------------//
22 //                                                       //
23 // This file is part of 'SAGA - System for Automated     //
24 // Geoscientific Analyses'. SAGA is free software; you   //
25 // can redistribute it and/or modify it under the terms  //
26 // of the GNU General Public License as published by the //
27 // Free Software Foundation, either version 2 of the     //
28 // License, or (at your option) any later version.       //
29 //                                                       //
30 // SAGA is distributed in the hope that it will be       //
31 // useful, but WITHOUT ANY WARRANTY; without even the    //
32 // implied warranty of MERCHANTABILITY or FITNESS FOR A  //
33 // PARTICULAR PURPOSE. See the GNU General Public        //
34 // License for more details.                             //
35 //                                                       //
36 // You should have received a copy of the GNU General    //
37 // Public License along with this program; if not, see   //
38 // <http://www.gnu.org/licenses/>.                       //
39 //                                                       //
40 //-------------------------------------------------------//
41 //                                                       //
42 //    e-mail:     oconrad@saga-gis.org                   //
43 //                                                       //
44 //    contact:    Olaf Conrad                            //
45 //                Institute of Geography                 //
46 //                University of Hamburg                  //
47 //                Germany                                //
48 //                                                       //
49 ///////////////////////////////////////////////////////////
50 
51 //---------------------------------------------------------
52 
53 
54 ///////////////////////////////////////////////////////////
55 //														 //
56 //														 //
57 //														 //
58 ///////////////////////////////////////////////////////////
59 
60 //---------------------------------------------------------
61 #include "grid_difference.h"
62 
63 
64 ///////////////////////////////////////////////////////////
65 //														 //
66 //														 //
67 //														 //
68 ///////////////////////////////////////////////////////////
69 
70 //---------------------------------------------------------
CGrid_Difference(void)71 CGrid_Difference::CGrid_Difference(void)
72 {
73 	Set_Name		(_TL("Grid Difference"));
74 
75 	Set_Author		("O.Conrad (c) 2009");
76 
77 	Set_Description	(_TW(
78 		""
79 	));
80 
81 	Parameters.Add_Grid(
82 		NULL	, "A"	, _TL("A"),
83 		_TL(""),
84 		PARAMETER_INPUT
85 	);
86 
87 	Parameters.Add_Grid(
88 		NULL	, "B"	, _TL("B"),
89 		_TL(""),
90 		PARAMETER_INPUT
91 	);
92 
93 	Parameters.Add_Grid(
94 		NULL	, "C"	, _TL("Difference (A - B)"),
95 		_TL(""),
96 		PARAMETER_OUTPUT
97 	);
98 }
99 
100 
101 ///////////////////////////////////////////////////////////
102 //														 //
103 ///////////////////////////////////////////////////////////
104 
105 //---------------------------------------------------------
On_Execute(void)106 bool CGrid_Difference::On_Execute(void)
107 {
108 	//-----------------------------------------------------
109 	CSG_Grid	*pA	= Parameters("A")->asGrid();
110 	CSG_Grid	*pB	= Parameters("B")->asGrid();
111 	CSG_Grid	*pC	= Parameters("C")->asGrid();
112 
113 	DataObject_Set_Colors(pC, 11, SG_COLORS_RED_GREY_BLUE);
114 
115 	//-----------------------------------------------------
116 	for(int y=0; y<Get_NY() && Set_Progress(y); y++)
117 	{
118 		#pragma omp parallel for
119 		for(int x=0; x<Get_NX(); x++)
120 		{
121 			if( pA->is_NoData(x, y) || pB->is_NoData(x, y) )
122 			{
123 				pC->Set_NoData(x, y);
124 			}
125 			else
126 			{
127 				pC->Set_Value(x, y, pA->asDouble(x, y) - pB->asDouble(x, y));
128 			}
129 		}
130 	}
131 
132 	//-----------------------------------------------------
133 	return( true );
134 }
135 
136 
137 ///////////////////////////////////////////////////////////
138 //														 //
139 //														 //
140 //														 //
141 ///////////////////////////////////////////////////////////
142 
143 //---------------------------------------------------------
CGrid_Division(void)144 CGrid_Division::CGrid_Division(void)
145 {
146 	Set_Name		(_TL("Grid Division"));
147 
148 	Set_Author		("O.Conrad (c) 2011");
149 
150 	Set_Description	(_TW(
151 		""
152 	));
153 
154 	Parameters.Add_Grid(
155 		NULL	, "A"	, _TL("Dividend"),
156 		_TL(""),
157 		PARAMETER_INPUT
158 	);
159 
160 	Parameters.Add_Grid(
161 		NULL	, "B"	, _TL("Divisor"),
162 		_TL(""),
163 		PARAMETER_INPUT
164 	);
165 
166 	Parameters.Add_Grid(
167 		NULL	, "C"	, _TL("Quotient"),
168 		_TL(""),
169 		PARAMETER_OUTPUT
170 	);
171 }
172 
173 
174 ///////////////////////////////////////////////////////////
175 //														 //
176 ///////////////////////////////////////////////////////////
177 
178 //---------------------------------------------------------
On_Execute(void)179 bool CGrid_Division::On_Execute(void)
180 {
181 	//-----------------------------------------------------
182 	CSG_Grid	*pA	= Parameters("A")->asGrid();
183 	CSG_Grid	*pB	= Parameters("B")->asGrid();
184 	CSG_Grid	*pC	= Parameters("C")->asGrid();
185 
186 	DataObject_Set_Colors(pC, 11, SG_COLORS_RED_GREY_BLUE);
187 
188 	//-----------------------------------------------------
189 	for(int y=0; y<Get_NY() && Set_Progress(y); y++)
190 	{
191 		#pragma omp parallel for
192 		for(int x=0; x<Get_NX(); x++)
193 		{
194 			if( pA->is_NoData(x, y) || pB->is_NoData(x, y) || pB->asDouble(x, y) == 0.0 )
195 			{
196 				pC->Set_NoData(x, y);
197 			}
198 			else
199 			{
200 				pC->Set_Value(x, y, pA->asDouble(x, y) / pB->asDouble(x, y));
201 			}
202 		}
203 	}
204 
205 	//-----------------------------------------------------
206 	return( true );
207 }
208 
209 
210 ///////////////////////////////////////////////////////////
211 //														 //
212 //														 //
213 //														 //
214 ///////////////////////////////////////////////////////////
215 
216 //---------------------------------------------------------
CGrids_Sum(void)217 CGrids_Sum::CGrids_Sum(void)
218 {
219 	Set_Name		(_TL("Grids Sum"));
220 
221 	Set_Author		("O.Conrad (c) 2010");
222 
223 	Set_Description	(_TW(
224 		"Cellwise addition of grid values."
225 	));
226 
227 	Parameters.Add_Grid_List(
228 		NULL	, "GRIDS"	, _TL("Grids"),
229 		_TL(""),
230 		PARAMETER_INPUT
231 	);
232 
233 	Parameters.Add_Grid(
234 		NULL	, "RESULT"	, _TL("Sum"),
235 		_TL(""),
236 		PARAMETER_OUTPUT
237 	);
238 
239 	Parameters.Add_Value(
240 		NULL	, "NODATA"	, _TL("Count No Data as Zero"),
241 		_TL(""),
242 		PARAMETER_TYPE_Bool, false
243 	);
244 }
245 
246 
247 ///////////////////////////////////////////////////////////
248 //														 //
249 ///////////////////////////////////////////////////////////
250 
251 //---------------------------------------------------------
On_Execute(void)252 bool CGrids_Sum::On_Execute(void)
253 {
254 	//-----------------------------------------------------
255 	CSG_Parameter_Grid_List	*pGrids	= Parameters("GRIDS" )->asGridList();
256 
257 	if( pGrids->Get_Grid_Count() < 1 )
258 	{
259 		Error_Set(_TL("no grid in list"));
260 
261 		return( false );
262 	}
263 
264 	//-----------------------------------------------------
265 	CSG_Grid	*pResult	= Parameters("RESULT")->asGrid();
266 
267 	bool	bNoData	= Parameters("NODATA")->asBool();
268 
269 	//-----------------------------------------------------
270 	for(int y=0; y<Get_NY() && Set_Progress(y); y++)
271 	{
272 		#pragma omp parallel for
273 		for(int x=0; x<Get_NX(); x++)
274 		{
275 			int		n	= 0;
276 			double	d	= 0.0;
277 
278 			for(int i=0; i<pGrids->Get_Grid_Count(); i++)
279 			{
280 				if( pGrids->Get_Grid(i)->is_InGrid(x, y) )
281 				{
282 					n	++;
283 					d	+= pGrids->Get_Grid(i)->asDouble(x, y);
284 				}
285 			}
286 
287 			if( bNoData ? n > 0 : n == pGrids->Get_Grid_Count() )
288 			{
289 				pResult->Set_Value(x, y, d);
290 			}
291 			else
292 			{
293 				pResult->Set_NoData(x, y);
294 			}
295 		}
296 	}
297 
298 	//-----------------------------------------------------
299 	return( true );
300 }
301 
302 
303 ///////////////////////////////////////////////////////////
304 //														 //
305 //														 //
306 //														 //
307 ///////////////////////////////////////////////////////////
308 
309 //---------------------------------------------------------
CGrids_Product(void)310 CGrids_Product::CGrids_Product(void)
311 {
312 	Set_Name		(_TL("Grids Product"));
313 
314 	Set_Author		("O.Conrad (c) 2010");
315 
316 	Set_Description	(_TW(
317 		"Cellwise multiplication of grid values."
318 	));
319 
320 	Parameters.Add_Grid_List(
321 		NULL	, "GRIDS"	, _TL("Grids"),
322 		_TL(""),
323 		PARAMETER_INPUT
324 	);
325 
326 	Parameters.Add_Grid(
327 		NULL	, "RESULT"	, _TL("Product"),
328 		_TL(""),
329 		PARAMETER_OUTPUT
330 	);
331 
332 	Parameters.Add_Value(
333 		NULL	, "NODATA"	, _TL("Count No Data as Zero"),
334 		_TL(""),
335 		PARAMETER_TYPE_Bool, false
336 	);
337 }
338 
339 
340 ///////////////////////////////////////////////////////////
341 //														 //
342 ///////////////////////////////////////////////////////////
343 
344 //---------------------------------------------------------
On_Execute(void)345 bool CGrids_Product::On_Execute(void)
346 {
347 	//-----------------------------------------------------
348 	CSG_Parameter_Grid_List	*pGrids	= Parameters("GRIDS" )->asGridList();
349 
350 	if( pGrids->Get_Grid_Count() < 1 )
351 	{
352 		Error_Set(_TL("no grid in list"));
353 
354 		return( false );
355 	}
356 
357 	//-----------------------------------------------------
358 	CSG_Grid	*pResult	= Parameters("RESULT")->asGrid();
359 
360 	bool	bNoData	= Parameters("NODATA")->asBool();
361 
362 	//-----------------------------------------------------
363 	for(int y=0; y<Get_NY() && Set_Progress(y); y++)
364 	{
365 		#pragma omp parallel for
366 		for(int x=0; x<Get_NX(); x++)
367 		{
368 			int		n	= 0;
369 			double	d	= 0.0;
370 
371 			for(int i=0; i<pGrids->Get_Grid_Count(); i++)
372 			{
373 				if( pGrids->Get_Grid(i)->is_InGrid(x, y) )
374 				{
375 					if( n++ < 1 )
376 					{
377 						d 	 = pGrids->Get_Grid(i)->asDouble(x, y);
378 					}
379 					else
380 					{
381 						d	*= pGrids->Get_Grid(i)->asDouble(x, y);
382 					}
383 				}
384 			}
385 
386 			if( bNoData ? n > 0 : n == pGrids->Get_Grid_Count() )
387 			{
388 				pResult->Set_Value(x, y, d);
389 			}
390 			else
391 			{
392 				pResult->Set_NoData(x, y);
393 			}
394 		}
395 	}
396 
397 	//-----------------------------------------------------
398 	return( true );
399 }
400 
401 
402 ///////////////////////////////////////////////////////////
403 //														 //
404 //														 //
405 //														 //
406 ///////////////////////////////////////////////////////////
407 
408 //---------------------------------------------------------
409