1 
2 ///////////////////////////////////////////////////////////
3 //                                                       //
4 //                         SAGA                          //
5 //                                                       //
6 //      System for Automated Geoscientific Analyses      //
7 //                                                       //
8 //                     Tool Library                      //
9 //                     climate_tools                     //
10 //                                                       //
11 //-------------------------------------------------------//
12 //                                                       //
13 //                   air_pressure.cpp                    //
14 //                                                       //
15 //                 Copyright (C) 2020 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.de                    //
40 //                                                       //
41 //    contact:    Olaf Conrad                            //
42 //                Institute of Geography                 //
43 //                University of Hamburg                  //
44 //                Germany                                //
45 //                                                       //
46 ///////////////////////////////////////////////////////////
47 
48 //---------------------------------------------------------
49 #include "air_pressure.h"
50 
51 
52 ///////////////////////////////////////////////////////////
53 //														 //
54 //														 //
55 //														 //
56 ///////////////////////////////////////////////////////////
57 
58 //---------------------------------------------------------
CAirPressure_Scaling(void)59 CAirPressure_Scaling::CAirPressure_Scaling(void)
60 {
61 	Set_Name		(_TL("Air Pressure Adjustment"));
62 
63 	Set_Author		("O.Conrad (c) 2020");
64 
65 	Set_Description	(_TW(
66 		"This tool adjusts air pressure values to the elevation "
67 		"using the barometric formula. Default values refer to "
68 		"the international standard atmosphere. "
69 	));
70 
71 	//-----------------------------------------------------
72 	Parameters.Add_Grid_or_Const("", "P", _TL("Air Pressure"          ), _TL("[hPa]"    ), 1013.25  ,    0.  , false, 0., false, false);
73 	Parameters.Add_Grid_or_Const("", "Z", _TL("Air Pressure Elevation"), _TL("[m]"      ),    0.    ,    0.  , false, 0., false, false);
74 	Parameters.Add_Grid_or_Const("", "T", _TL("Temperature"           ), _TL("[Celsius]"),    0.    , -273.15, false, 0., false, false);
75 	Parameters.Add_Grid_or_Const("", "L", _TL("Temperature Lapse Rate"), _TL("[K/m]"    ),    0.0065,    0.  , false, 0., false, false);
76 
77 	Parameters.Add_Grid("",
78 		"DEM"		, _TL("Elevation"),
79 		_TL("[m]"),
80 		PARAMETER_INPUT
81 	);
82 
83 	Parameters.Add_Grid("",
84 		"P_ADJ"		, _TL("Adjusted Air Pressure"),
85 		_TL(""),
86 		PARAMETER_OUTPUT
87 	);
88 }
89 
90 
91 ///////////////////////////////////////////////////////////
92 //														 //
93 ///////////////////////////////////////////////////////////
94 
95 //---------------------------------------------------------
On_Parameters_Enable(CSG_Parameters * pParameters,CSG_Parameter * pParameter)96 int CAirPressure_Scaling::On_Parameters_Enable(CSG_Parameters *pParameters, CSG_Parameter *pParameter)
97 {
98 	return( CSG_Tool_Grid::On_Parameters_Enable(pParameters, pParameter) );
99 }
100 
101 
102 ///////////////////////////////////////////////////////////
103 //														 //
104 ///////////////////////////////////////////////////////////
105 
106 //---------------------------------------------------------
On_Execute(void)107 bool CAirPressure_Scaling::On_Execute(void)
108 {
109 	CSG_Grid *pP = Parameters("P")->asGrid(); double P = Parameters("P")->asDouble();
110 	CSG_Grid *pZ = Parameters("Z")->asGrid(); double Z = Parameters("Z")->asDouble();
111 	CSG_Grid *pT = Parameters("T")->asGrid(); double T = Parameters("T")->asDouble();
112 	CSG_Grid *pL = Parameters("L")->asGrid(); double L = Parameters("L")->asDouble();
113 
114 	CSG_Grid *pDEM  = Parameters("DEM"  )->asGrid();
115 	CSG_Grid *pPadj	= Parameters("P_ADJ")->asGrid();
116 
117 	//-----------------------------------------------------
118 	for(int y=0; y<Get_NY() && Set_Progress(y); y++)
119 	{
120 		double	py	= Get_YMin() + y * Get_Cellsize();
121 
122 		#pragma omp parallel for
123 		for(int x=0; x<Get_NX(); x++)
124 		{
125 			double	Padj = -1.;
126 
127 			if( !pDEM->is_NoData(x, y) )
128 			{
129 				double	px	= Get_XMin() + x * Get_Cellsize(),
130 					_P = P, _Z = Z, _T = T, _L = L;
131 
132 				if( (!pP || pP->Get_Value(px, py, _P))
133 				&&  (!pZ || pZ->Get_Value(px, py, _Z))
134 				&&  (!pT || pT->Get_Value(px, py, _T))
135 				&&  (!pL || pL->Get_Value(px, py, _L)) )
136 				{
137 					Padj	= _P * pow(1. - (_L * (pDEM->asDouble(x, y) - _Z) / (273.15 + _T)), 5.255);
138 				}
139 			}
140 
141 			if( Padj < 0. )
142 			{
143 				pPadj->Set_NoData(x, y);
144 			}
145 			else
146 			{
147 				pPadj->Set_Value(x, y, Padj);
148 			}
149 		}
150 	}
151 
152 	//-----------------------------------------------------
153 	return( true );
154 }
155 
156 
157 ///////////////////////////////////////////////////////////
158 //														 //
159 //														 //
160 //														 //
161 ///////////////////////////////////////////////////////////
162 
163 //---------------------------------------------------------
164