Mapper
rate_table.c
Go to the documentation of this file.
1 #include "rate_table.h"
2 #include <math.h>
3 
4 /*****************************STAGE TO FLOW*********************************/
5 /*-------------------------------------------------------------------------
6  This function finds where the given stage is in the given
7  rating table and then calls the appropriate function to do the actual
8  conversion.
9  Precondition: Given a rating table structure,
10  and a stage
11  Postcondition: Converts the stage to flow,
12  gives a warning flag if above or below
13 --------------------------------------------------------------------------*/
14 int GetFlow(RatingTable A, double Stage, int *Warning)
15 {
16  int Flow,
17  i;
18 
19  double
20  ShiftStage,
21  Level;
22 
23  ShiftStage = Stage; /* + A.RatShf[0]; took shift out! */
24 
25  if( ShiftStage < A.HMin)
26  {
27  Flow = BelowStage2Flow( A, ShiftStage);
28  return Flow;
29  *Warning = 1;
30  }
31 
32  if( ShiftStage > A.HMax)
33  {
34  Flow = AboveStage2Flow( A, ShiftStage);
35  return Flow;
36  *Warning = 1;
37  }
38 
39  for( i = 0; i <= A.AryMax; i++)
40  {
41  Level = A.RatStg[i] - ShiftStage;
42  if( Level == 0)
43  {
44  Flow = ExactStage2Flow( A, i);
45  return Flow;
46  *Warning = 0;
47  }
48 
49  if( Level > 0)
50  {
51  Flow = InterpolateStage2Flow( A, ShiftStage, i);
52  return Flow;
53  *Warning = 0;
54  }
55  }
56 }
57 
58 
59 /*-------------------------------------------------------------------------
60  This function converts stage to flow when the given stage is below the
61  given rating table.
62 -------------------------------------------------------------------------*/
63 int BelowStage2Flow( RatingTable A, double ShiftStage)
64 {
65  double Slope = A.QMin / A.HMin;
66  double flow = ShiftStage * Slope;
67  int Flow = flow;
68  return Flow;
69 }
70 
71 
72 /*-------------------------------------------------------------------------
73  This function converts stage to flow when the given stage is above the
74  given rating table.
75 ---------------------------------------------------------------------------*/
76 int AboveStage2Flow( RatingTable A, double ShiftStage)
77 {
78  double
79  flow,
80  Del,
81  Slope,
82  Var1;
83  int
84  Flow;
85 
86  Del = log10(A.HMax) - log10(A.RatStg[A.AryMax - 1]);
87  Slope = (log10(A.QMax) - log10(A.RatQ[A.AryMax - 1])) / Del;
88  Var1 = log10(A.QMax);
89  flow = Var1 + (Slope * (log10(ShiftStage) - log10(A.HMax)));
90  flow = pow(10.0,flow);
91  Flow = flow;
92  return Flow;
93 }
94 
95 
96 /*-----------------------------------------------------------------------
97  This function converts stage to flow when the given stage is exactly
98  one of the stages in the given rating table.
99 -------------------------------------------------------------------------*/
101 {
102  double flow = A.RatQ[i];
103  int Flow = flow;
104  return Flow;
105 }
106 
107 
108 /*------------------------------------------------------------------------
109  This function converts stage to flow when the given stage is in between
110  the stages in the given rating table.
111 --------------------------------------------------------------------------*/
112 int InterpolateStage2Flow( RatingTable A, double ShiftStage, int i)
113 {
114  double
115  flow,
116  Del,
117  Slope,
118  Var2;
119  int
120  Flow,
121  High = i,
122  Low = i - 1;
123 
124  Del = log10(A.RatStg[High]) - log10(A.RatStg[Low]);
125  Slope = (log10(A.RatQ[High]) - log10(A.RatQ[Low])) / Del;
126  Var2 = log10(A.RatQ[Low]);
127  flow = Var2 + (Slope * (log10(ShiftStage) - log10(A.RatStg[Low])));
128  flow = pow(10.0,flow);
129  Flow = flow;
130 
131  return Flow;
132 }
133 
134 /********************************FLOW TO STAGE*****************************/
135 
136 /*-------------------------------------------------------------------------
137  This function finds where the given flow is in the given
138  rating table and then calls the appropriate function to do the actual
139  conversion.
140  Precondition: Given a rating table structure,
141  and a flow
142  Postcondition: Converts the flow to stage,
143  gives a warning flag if above or below
144 --------------------------------------------------------------------------*/
145 double GetStage(RatingTable A, int Flow, int *Warning)
146 {
147  int i;
148 
149  double
150  Stage,
151  Level;
152 
153  if( Flow < A.QMin)
154  {
155  Stage = BelowFlow2Stage( A, Flow);
156  return Stage;
157  *Warning = 1;
158  }
159 
160  if( Flow > A.QMax)
161  {
162  Stage = AboveFlow2Stage( A, Flow);
163  return Stage;
164  *Warning = 1;
165  }
166 
167  for( i = 0; i <= A.AryMax; i++)
168  {
169  Level = A.RatQ[i] - Flow;
170  if( Level == 0)
171  {
172  Stage = ExactFlow2Stage( A, i);
173  return Stage;
174  *Warning = 0;
175  }
176 
177  if( Level > 0)
178  {
179  Stage = InterpolateFlow2Stage( A, Flow, i);
180  return Stage;
181  *Warning = 0;
182  }
183  }
184 }
185 
186 
187 
188 /*-------------------------------------------------------------------------
189  This function converts flow to stage when the given flow is below the
190  given rating table.
191 -------------------------------------------------------------------------*/
192 double BelowFlow2Stage( RatingTable A, int Flow)
193 {
194  double flow = Flow;
195  double Slope = A.HMin / A.QMin;
196  double Stage = flow * Slope;
197  double ShiftStage = Stage; /* - A.RatStg[0]; took shift out*/
198  return ShiftStage;
199 }
200 
201 
202 
203 /*-------------------------------------------------------------------------
204  This function converts flow to stage when the given flow is above the
205  given rating table.
206 ---------------------------------------------------------------------------*/
207 double AboveFlow2Stage( RatingTable A, int Flow)
208 {
209  double
210  flow,
211  Del,
212  Slope,
213  Var1,
214  Stage,
215  ShiftStage;
216  flow = Flow;
217 
218  Del = log10(A.QMax) - log10(A.RatQ[A.AryMax - 1]);
219  Slope = (log10(A.HMax) - log10(A.RatStg[A.AryMax - 1])) / Del;
220  Var1 = log10(A.HMax);
221  Stage = Var1 + (Slope * (log10(flow) - log10(A.QMax)));
222  ShiftStage = pow(10.0,Stage); /* - A.RatShf[0]; took shift out!*/
223  return ShiftStage;
224 }
225 
226 
227 
228 
229 
230 /*-----------------------------------------------------------------------
231  This function converts flow to stage when the given flow is exactly
232  one of the flows in the given rating table.
233 -------------------------------------------------------------------------*/
235 {
236  double Stage = A.RatStg[i];
237  double ShiftStage = Stage; /* - A.RatShf[0]; took out shift!*/
238  return ShiftStage;
239 }
240 
241 
242 
243 /*------------------------------------------------------------------------
244  This function converts flow to stage when the given flow is in between
245  the flows in the given rating table.
246 --------------------------------------------------------------------------*/
247 double InterpolateFlow2Stage( RatingTable A, int Flow, int i)
248 {
249  double
250  flow,
251  Del,
252  Slope,
253  Var2,
254  Stage,
255  ShiftStage;
256  int
257  High = i,
258  Low = i - 1;
259  flow = Flow;
260 
261  Del = log10(A.RatQ[High]) - log10(A.RatQ[Low]);
262 
263  Slope = (log10(A.RatStg[High]) - log10(A.RatStg[Low])) / Del;
264  Var2 = log10(A.RatStg[Low]);
265  Stage = Var2 + (Slope * (log10(flow) - log10(A.RatQ[Low])));
266  ShiftStage = pow(10.0,Stage); /* - A.RatShf[0];took shift out!*/
267  return ShiftStage;
268 }
269 
270 
271 
272 
static int i
static double Slope
Definition: rat.c:135
double ExactFlow2Stage(RatingTable A, int i)
Definition: rate_table.c:234
double InterpolateFlow2Stage(RatingTable A, int Flow, int i)
Definition: rate_table.c:247
int BelowStage2Flow(RatingTable A, double ShiftStage)
Definition: rate_table.c:63
double AboveFlow2Stage(RatingTable A, int Flow)
Definition: rate_table.c:207
int ExactStage2Flow(RatingTable A, int i)
Definition: rate_table.c:100
double BelowFlow2Stage(RatingTable A, int Flow)
Definition: rate_table.c:192
double GetStage(RatingTable A, int Flow, int *Warning)
Definition: rate_table.c:145
int AboveStage2Flow(RatingTable A, double ShiftStage)
Definition: rate_table.c:76
int InterpolateStage2Flow(RatingTable A, double ShiftStage, int i)
Definition: rate_table.c:112
int GetFlow(RatingTable A, double Stage, int *Warning)
Definition: rate_table.c:14
int RatQ[100]
Definition: rate_table.h:13
double HMax
Definition: rate_table.h:16
double HMin
Definition: rate_table.h:15
double RatStg[100]
Definition: rate_table.h:12