1 
2 ///////////////////////////////////////////////////////////
3 //                                                       //
4 //                         SAGA                          //
5 //                                                       //
6 //      System for Automated Geoscientific Analyses      //
7 //                                                       //
8 //                     Tool Library                      //
9 //                     Grid_Calculus                     //
10 //                                                       //
11 //-------------------------------------------------------//
12 //                                                       //
13 //                grid_histogram_match.cpp               //
14 //                                                       //
15 //                 Copyright (C) 2019 by                 //
16 //                      Olaf Conrad                      //
17 //                                                       //
18 //-------------------------------------------------------//
19 //                                                       //
20 // This file is part of 'SAGA - System for Automated     //
21 // Geoscientific Analyses'. SAGA is free software; you   //
22 // can redistribute it and/or modify it under the terms  //
23 // of the GNU General Public License as published by the //
24 // Free Software Foundation, either version 2 of the     //
25 // License, or (at your option) any later version.       //
26 //                                                       //
27 // SAGA is distributed in the hope that it will be       //
28 // useful, but WITHOUT ANY WARRANTY; without even the    //
29 // implied warranty of MERCHANTABILITY or FITNESS FOR A  //
30 // PARTICULAR PURPOSE. See the GNU General Public        //
31 // License for more details.                             //
32 //                                                       //
33 // You should have received a copy of the GNU General    //
34 // Public License along with this program; if not, see   //
35 // <http://www.gnu.org/licenses/>.                       //
36 //                                                       //
37 //-------------------------------------------------------//
38 //                                                       //
39 //    e-mail:     oconrad@saga-gis.org                   //
40 //                                                       //
41 //    contact:    Olaf Conrad                            //
42 //                Institute of Geography                 //
43 //                University of Hamburg                  //
44 //                Germany                                //
45 //                                                       //
46 ///////////////////////////////////////////////////////////
47 
48 //---------------------------------------------------------
49 #include "grid_histogram_match.h"
50 
51 
52 ///////////////////////////////////////////////////////////
53 //														 //
54 //														 //
55 //														 //
56 ///////////////////////////////////////////////////////////
57 
58 //---------------------------------------------------------
CGrid_Histogram_Match(void)59 CGrid_Histogram_Match::CGrid_Histogram_Match(void)
60 {
61 	Set_Name		(_TL("Histogram Matching"));
62 
63 	Set_Author		("O.Conrad (c) 2019");
64 
65 	Set_Description	(_TW(
66 		"This tool alters the values of a grid so that its value "
67 		"distribution (its histogram), matches that of a reference grid. "
68 		"The first method simply uses arithmetic mean and standard "
69 		"deviation for adjustment, which usually is sufficient for normal "
70 		"distributed values. The second method performs a more precise "
71 		"adjustment based on the grids' histograms. "
72 	));
73 
74 	Parameters.Add_Grid(
75 		"", "GRID"		, _TL("Grid"),
76 		_TL(""),
77 		PARAMETER_INPUT
78 	);
79 
80 	Parameters.Add_Grid(
81 		"", "MATCHED"	, _TL("Adjusted Grid"),
82 		_TL(""),
83 		PARAMETER_OUTPUT_OPTIONAL
84 	);
85 
86 	Parameters.Add_Grid(
87 		"", "REFERENCE"	, _TL("Reference Grid"),
88 		_TL(""),
89 		PARAMETER_INPUT, false
90 	);
91 
92 	Parameters.Add_Choice(
93 		"", "METHOD"	, _TL("Method"),
94 		_TL(""),
95 		CSG_String::Format("%s|%s",
96 			_TL("standard deviation"),
97 			_TL("histogram")
98 		), 1
99 	);
100 
101 	Parameters.Add_Int(
102 		"", "NCLASSES"	, _TL("Histogramm Classes"),
103 		_TL("Number of histogram classes for internal use."),
104 		10, 100, true
105 	);
106 
107 	Parameters.Add_Int(
108 		"", "MAXSAMPLES", _TL("Maximum Sample Size"),
109 		_TL("If set to zero all data will be used to build the histograms."),
110 		1000000, 0, true
111 	);
112 }
113 
114 
115 ///////////////////////////////////////////////////////////
116 //														 //
117 ///////////////////////////////////////////////////////////
118 
119 //---------------------------------------------------------
On_Parameters_Enable(CSG_Parameters * pParameters,CSG_Parameter * pParameter)120 int CGrid_Histogram_Match::On_Parameters_Enable(CSG_Parameters *pParameters, CSG_Parameter *pParameter)
121 {
122 	if( pParameter->Cmp_Identifier("METHOD") )
123 	{
124 		pParameters->Set_Enabled("NCLASSES"  , pParameter->asInt() == 1);
125 		pParameters->Set_Enabled("MAXSAMPLES", pParameter->asInt() == 1);
126 	}
127 
128 	return( CSG_Tool_Grid::On_Parameters_Enable(pParameters, pParameter) );
129 }
130 
131 
132 ///////////////////////////////////////////////////////////
133 //														 //
134 ///////////////////////////////////////////////////////////
135 
136 //---------------------------------------------------------
On_Execute(void)137 bool CGrid_Histogram_Match::On_Execute(void)
138 {
139 	//-----------------------------------------------------
140 	CSG_Grid	*pGrid	= Parameters("GRID")->asGrid();
141 
142 	if( Parameters("MATCHED")->asGrid() && Parameters("MATCHED")->asGrid() != pGrid )
143 	{
144 		CSG_Grid	*pResult	= Parameters("MATCHED")->asGrid();
145 
146 		pResult->Create(*pGrid);
147 
148 		pResult->Fmt_Name("%s [%s]", pGrid->Get_Name(), _TL("histogram match"));
149 
150 		pGrid	= pResult;
151 	}
152 
153 	//-----------------------------------------------------
154 	CSG_Grid	*pReference	= Parameters("REFERENCE")->asGrid();
155 
156 	CSG_Simple_Statistics Statistics[2]; CSG_Histogram Histogram[2];
157 
158 	int	Method	= Parameters("METHOD")->asInt();
159 
160 	switch( Method )
161 	{
162 	case  0:
163 		Statistics[0]	= pReference->Get_Statistics();
164 
165 		if( Statistics[0].Get_StdDev() <= 0.0 )
166 		{
167 			Error_Fmt("%s [%s]", _TL("no variance in data set"), pReference->Get_Name());
168 
169 			return( false );
170 		}
171 
172 		Statistics[1]	= pGrid->Get_Statistics();
173 
174 		if( Statistics[1].Get_StdDev() <= 0.0 )
175 		{
176 			Error_Fmt("%s [%s]", _TL("no variance in data set"), pGrid->Get_Name());
177 
178 			return( false );
179 		}
180 		break;
181 
182 	default:
183 		if( !Histogram[0].Create(Parameters("NCLASSES")->asInt(),
184 			pReference->Get_Min(), pReference->Get_Max(), pReference, (size_t)Parameters("MAXSAMPLES")->asInt()) )
185 		{
186 			Error_Fmt("%s [%s]", _TL("failed to create histogram"), pReference->Get_Name());
187 
188 			return( false );
189 		}
190 
191 		if( !Histogram[1].Create(Parameters("NCLASSES")->asInt(),
192 			pGrid->Get_Min(), pGrid->Get_Max(), pGrid, (size_t)Parameters("MAXSAMPLES")->asInt()) )
193 		{
194 			Error_Fmt("%s [%s]", _TL("failed to create histogram"), pGrid->Get_Name());
195 
196 			return( false );
197 		}
198 		break;
199 	}
200 
201 	//-----------------------------------------------------
202 	for(int y=0; y<Get_NY() && Set_Progress(y); y++)
203 	{
204 		#ifndef _DEBUG
205 		#pragma omp parallel for
206 		#endif
207 		for(int x=0; x<Get_NX(); x++)
208 		{
209 			if( !pGrid->is_NoData(x, y) )
210 			{
211 				double	v	= pGrid->asDouble(x, y);
212 
213 				switch( Method )
214 				{
215 				case  0:
216 					v	= Statistics[0].Get_Mean() + (v - Statistics[1].Get_Mean()) * Statistics[0].Get_StdDev() / Statistics[1].Get_StdDev();
217 					break;
218 
219 				default:
220 					v	= Histogram[0].Get_Quantile(Histogram[1].Get_Quantile_Value(v));
221 					break;
222 				}
223 
224 				pGrid->Set_Value(x, y, v);
225 			}
226 		}
227 	}
228 
229 	//-----------------------------------------------------
230 	if( pGrid != Parameters("MATCHED")->asGrid() )
231 	{
232 		DataObject_Update(pGrid);	// only declared output data is updated automatically by the framework
233 	}
234 
235 	return( true );
236 }
237 
238 
239 ///////////////////////////////////////////////////////////
240 //														 //
241 //														 //
242 //														 //
243 ///////////////////////////////////////////////////////////
244 
245 //---------------------------------------------------------
246