-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPlot.cpp
More file actions
546 lines (474 loc) · 15.5 KB
/
Plot.cpp
File metadata and controls
546 lines (474 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
//---------------------------------------------------------------------------
#include <iostream>
#include <sstream>
#include "Plot.h"
#include "SimManager.h"
#include "ParsingFunctions.h"
//---------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////////////////
// Destructor
////////////////////////////////////////////////////////////////////////////
clPlot::~clPlot()
{
delete[] mp_fAziTans;
delete[] mp_iAllowedFileTypes;
mp_iAllowedFileTypes = NULL;
}
/////////////////////////////////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////////////////////////////////
clPlot::clPlot(clSimManager * p_oSimManager) : clWorkerBase(p_oSimManager)
{
try
{
m_sNameString = "plot";
m_iCellSize = 8;
m_sPlotTitle = "";
m_iNumXGrids = 0;
m_iNumYGrids = 0;
m_fPlotArea = 0;
m_fPlotLenX = 0;
m_fPlotLenY = 0;
m_fLatitude = 0;
mp_fAziTans = NULL;
m_fMeanAnnualPrecipMm = 0;
m_fMeanTempC = 0;
m_fNDep = 0;
m_fMaxX = 0;
m_fMaxY = 0;
m_fWaterDeficit = 0;
m_fSeasonalPrecipitation = 0;
m_fLTMPrecipMm = 0;
m_fLTMTempC = 0;
m_fLTMWaterDeficit = 0;
m_fLTMSeasonalPrecipitation = 0;
//Allowed file types
m_iNumAllowedTypes = 2;
mp_iAllowedFileTypes = new int[m_iNumAllowedTypes];
mp_iAllowedFileTypes[0] = parfile;
mp_iAllowedFileTypes[1] = detailed_output;
}
catch (modelErr& err)
{
throw(err);
}
catch (modelMsg & msg)
{
throw(msg);
} //non-fatal error
catch (...)
{
modelErr stcErr;
stcErr.iErrorCode = UNKNOWN;
stcErr.sFunction = "clPlot::clPlot";
throw(stcErr);
}
}
///////////////////////////////////////////////////////////////////////////
// GetData()
//////////////////////////////////////////////////////////////////////////
void clPlot::GetData(DOMDocument * p_oDoc)
{
try
{
DOMNodeList * p_oNodeList;
DOMNode * p_oDocNode;
DOMElement * p_oElement;
XMLCh *sTag;
char * cData;
//Get the parent element
sTag = XMLString::transcode("plot");
p_oNodeList = p_oDoc->getElementsByTagName(sTag);
XMLString::release(&sTag);
if (0 == p_oNodeList->getLength())
{
modelErr stcErr;
stcErr.iErrorCode = DATA_MISSING;
stcErr.sFunction = "clPlot::GetData";
stcErr.sMoreInfo = "plot";
throw(stcErr);
}
p_oDocNode = p_oNodeList->item(0);
p_oElement = (DOMElement *) p_oDocNode;
//----- Latitude --------------------------------------------------------//
FillSingleValue(p_oElement, "plot_latitude", & m_fLatitude, true);
//----- Plot length in the X direction ----------------------------------//
FillSingleValue(p_oElement, "plot_lenX", & m_fPlotLenX, true);
//----- Plot length in the Y direction ----------------------------------//
FillSingleValue(p_oElement, "plot_lenY", & m_fPlotLenY, true);
//----- Plot mean annual precip, mm; not required -----------------------//
FillSingleValue(p_oElement, "plot_precip_mm_yr", & m_fMeanAnnualPrecipMm, false);
//----- Long-term mean annual precip; not required ----------------------//
FillSingleValue(p_oElement, "plot_ltm_precip", & m_fLTMPrecipMm, false);
//----- Plot mean annual temp, C; not required --------------------------//
FillSingleValue(p_oElement, "plot_temp_C", & m_fMeanTempC, false);
//----- Plot long-term temperature; not required ------------------------//
FillSingleValue(p_oElement, "plot_ltm_temp", & m_fLTMTempC, false);
//----- Plot annual N deposition; not required --------------------------//
FillSingleValue(p_oElement, "plot_n_dep", & m_fNDep, false);
//----- Seasonal precipitation; not required ----------------------------//
FillSingleValue(p_oElement, "plot_seasonal_precipitation", & m_fSeasonalPrecipitation, false);
//----- Long-term mean seasonal precip; not required --------------------//
FillSingleValue(p_oElement, "plot_ltm_seasonal_precipitation", & m_fLTMSeasonalPrecipitation, false);
//----- Water deficit; not required -------------------------------------//
FillSingleValue(p_oElement, "plot_water_deficit", & m_fWaterDeficit, false);
//----- Long-term water deficit; not required ---------------------------//
FillSingleValue(p_oElement, "plot_ltm_water_deficit", & m_fLTMWaterDeficit, false);
//----- Plot title; not required ----------------------------------------//
sTag = XMLString::transcode("plot_title");
p_oNodeList = p_oElement->getElementsByTagName(sTag);
XMLString::release(&sTag);
if (0 != p_oNodeList->getLength())
{
cData = XMLString::transcode(p_oNodeList->item(0)->getFirstChild()->getNodeValue());
m_sPlotTitle = cData;
delete[] cData;
}
//Finish-up calculations
m_iNumXGrids = (int)ceil(m_fPlotLenX / m_iCellSize);
m_iNumYGrids = (int)ceil(m_fPlotLenY / m_iCellSize);
m_fPlotArea = (m_fPlotLenX * m_fPlotLenY) / 10000; //in hectares
m_fMaxX = m_fPlotLenX - 0.01;
m_fMaxY = m_fPlotLenY - 0.01;
PopulateAziTans();
}
catch (modelErr& err)
{
throw(err);
}
catch (modelMsg & msg)
{
throw(msg);
} //non-fatal error
catch (...)
{
modelErr stcErr;
stcErr.iErrorCode = UNKNOWN;
stcErr.sFunction = "clPlot::GetData";
throw(stcErr);
}
}
///////////////////////////////////////////////////////////////////////////
// GetDistance()
//////////////////////////////////////////////////////////////////////////
float clPlot::GetDistance(float fFromX, float fFromY, float fToX, float fToY)
{
float fXDistance, fYDistance, fDistance1, fDistance2;
//For each axis, find the shortest distance: with or without torus
//wrapping. To torus-wrap, find the smallest coordinate and add the
//plot length to it.
if (fToX > fFromX)
{
fDistance1 = fToX - fFromX;
fDistance2 = (fFromX + m_fPlotLenX) - fToX;
if (fDistance1 < fDistance2) fXDistance = fDistance1;
else
fXDistance = fDistance2;
}
else
{
fDistance1 = fFromX - fToX;
fDistance2 = (fToX + m_fPlotLenX) - fFromX;
if (fDistance1 < fDistance2) fXDistance = fDistance1;
else
fXDistance = fDistance2;
}
if (fToY > fFromY)
{
fDistance1 = fToY - fFromY;
fDistance2 = (fFromY + m_fPlotLenY) - fToY;
if (fDistance1 < fDistance2) fYDistance = fDistance1;
else
fYDistance = fDistance2;
}
else
{
fDistance1 = fFromY - fToY;
fDistance2 = (fToY + m_fPlotLenY) - fFromY;
if (fDistance1 < fDistance2) fYDistance = fDistance1;
else
fYDistance = fDistance2;
}
return sqrt((fXDistance * fXDistance) + (fYDistance * fYDistance));
}
///////////////////////////////////////////////////////////////////////////
// GetUncorrectedX()
///////////////////////////////////////////////////////////////////////////
float clPlot::GetUncorrectedX(float fFromX, float fAzimuth, float fDistance)
{
float fAzimuthPrime;
//Make sure distance is greater than 0, and azimuth is between 0 and 2PI
if (fDistance < 0) {
modelErr stcErr;
stcErr.iErrorCode = BAD_DATA;
stcErr.sFunction = "clPlot::GetUncorrectedX";
std::stringstream s;
s << "Distance must be greater than zero. Is: " << fDistance;
stcErr.sMoreInfo = s.str();
throw(stcErr);
}
if (fAzimuth < 0 || fAzimuth >= 6.283185072) {
modelErr stcErr;
stcErr.iErrorCode = BAD_DATA;
stcErr.sFunction = "clPlot::GetUncorrectedX";
std::stringstream s;
s << "Azimuth must be between 0 and 2PI. Is: " << fAzimuth;
stcErr.sMoreInfo = s.str();
throw(stcErr);
}
//Correct the azimuth depending on the quadrant
if (fAzimuth <= 1.570796326) { //PI/2
return (sin(fAzimuth) * fDistance) + fFromX;
}
else if (fAzimuth > 1.570796326 && fAzimuth <= 3.141592653) {
fAzimuthPrime = 3.141592653 - fAzimuth; //PI - azimuth
return (sin(fAzimuthPrime) * fDistance) + fFromX;
}
else if (fAzimuth > 3.141592653 && fAzimuth <= 4.71238898) {
fAzimuthPrime = fAzimuth - 3.141592653;
return fFromX - (sin(fAzimuthPrime) * fDistance);
}
else {
fAzimuthPrime = 6.283185072 - fAzimuth; //2PI - azimuth
return fFromX - (sin(fAzimuthPrime) * fDistance);
}
}
///////////////////////////////////////////////////////////////////////////
// GetUncorrectedY()
///////////////////////////////////////////////////////////////////////////
float clPlot::GetUncorrectedY(float fFromY, float fAzimuth, float fDistance)
{
float fAzimuthPrime;
//Make sure distance is greater than 0, and azimuth is between 0 and 2PI
if (fDistance < 0) {
modelErr stcErr;
stcErr.iErrorCode = BAD_DATA;
stcErr.sFunction = "clPlot::GetUncorrectedY";
std::stringstream s;
s << "Distance must be greater than zero. Is: " << fDistance;
stcErr.sMoreInfo = s.str();
throw(stcErr);
}
if (fAzimuth < 0 || fAzimuth >= 6.283185072) {
modelErr stcErr;
stcErr.iErrorCode = BAD_DATA;
stcErr.sFunction = "clPlot::GetUncorrectedY";
std::stringstream s;
s << "Azimuth must be between 0 and 2PI. Is: " << fAzimuth;
stcErr.sMoreInfo = s.str();
throw(stcErr);
}
//Correct the azimuth depending on the quadrant
if (fAzimuth < 1.570796326) { //PI/2
return (cos(fAzimuth) * fDistance) + fFromY;
}
else if (fAzimuth >= 1.570796326 && fAzimuth < 3.141592653) {
fAzimuthPrime = 3.141592653 - fAzimuth; //PI - azimuth
return fFromY - (cos(fAzimuthPrime) * fDistance);
}
else if (fAzimuth >= 3.141592653 && fAzimuth < 4.71238898) {
fAzimuthPrime = fAzimuth - 3.141592653;
return fFromY - (cos(fAzimuthPrime) * fDistance);
}
else {
fAzimuthPrime = 6.283185072 - fAzimuth; //2PI - azimuth
return fFromY + (cos(fAzimuthPrime) * fDistance);
}
}
///////////////////////////////////////////////////////////////////////////
// GetAzimuthAngle()
///////////////////////////////////////////////////////////////////////////
float clPlot::GetAzimuthAngle(float fFromX, float fFromY, float fToX, float fToY)
{
float fDistX = GetXDistance(fFromX, fToX), fDistY = GetYDistance(fFromY, fToY),
fAzimuth = 0; //azimuth angle to return
//Calculate the azimuth - correct for quadrant of "to" relative to "from"
//(counting clockwise from upper right)
if ((fDistY > 0) && (fDistX >= 0)) //first quadrant
fAzimuth = atan((fDistX) / (fDistY));
if ((fDistX > 0) && (fDistY <= 0)) //second quadrant
fAzimuth = (M_PI / 2.0) + atan((-1.0 * fDistY) / (fDistX));
if ((fDistX <= 0) && (fDistY < 0)) //third quadrant
fAzimuth = M_PI + atan((-1.0 * fDistX) / (-1.0 * fDistY));
if ((fDistX < 0) && (fDistY >= 0)) //fourth quadrant
fAzimuth = (1.5 * M_PI) + atan((fDistY) / (-1.0 * fDistX));
return fAzimuth;
}
////////////////////////////////////////////////////////////////////////////
// GetFastAzimuthAngle()
//////////////////////////////////////////////////////////////////////////
int clPlot::GetFastAzimuthAngle(float fFromX, float fFromY, float fToX, float fToY)
{
int i, //loop counter
iAzimuth = -1; //azimuth value to return
float fDistX = GetXDistance(fFromX, fToX), fDistY = GetYDistance(fFromY, fToY), fTanAzimuth; //tangent of the azimuth angle
//If the Y distance is 0 - shortcut. If the X distance is positive, the
//value is 90; if negative, 270.
if (fDistY == 0)
{
if (fDistX > 0)
return 90;
else if (fDistX < 0)
return 270;
else
return 0;
}
else if (fDistX == 0)
{
if (fDistY > 0)
return 0;
else
return 180;
}
//Calculate the tangent of azimuth - correct for quadrant of "to" relative
//to "from" (counting clockwise from upper right). Then start looking at
//tangent values in that quadrant until we find the array bucket where the
//value fits between its value and the next one
fTanAzimuth = fDistX / fDistY;
if (fTanAzimuth > 9999)
fTanAzimuth = 9999;
if ((fDistY > 0) && (fDistX >= 0))
{ //first quadrant
for (i = 0; i < 91; i++)
{
if (mp_fAziTans[i] <= fTanAzimuth && mp_fAziTans[i + 1] > fTanAzimuth)
{
iAzimuth = i; break;
}
}
}
else if ((fDistX > 0) && (fDistY <= 0))
{ //second quadrant
//If very close to 90, the for loop may not catch it
if (fTanAzimuth < mp_fAziTans[91])
return 90;
for (i = 91; i < 180; i++)
{
if (mp_fAziTans[i] <= fTanAzimuth && mp_fAziTans[i + 1] > fTanAzimuth)
{
iAzimuth = i; break;
}
}
}
else if ((fDistX <= 0) && (fDistY < 0))
{ //third quadrant
//If very close to 180, the for loop may not catch it
if (fTanAzimuth < mp_fAziTans[180])
return 180;
for (i = 180; i < 271; i++)
{
if (mp_fAziTans[i] <= fTanAzimuth && mp_fAziTans[i + 1] > fTanAzimuth)
{
iAzimuth = i; break;
}
}
}
else if ((fDistX < 0) && (fDistY >= 0))
{ //fourth quadrant
//If very close to 270, the for loop may not catch it
if (fTanAzimuth < mp_fAziTans[271])
return 270;
for (i = 271; i < 359; i++)
{
if (mp_fAziTans[i] <= fTanAzimuth && mp_fAziTans[i + 1] > fTanAzimuth)
{
iAzimuth = i; break;
}
if (iAzimuth < 0) iAzimuth = 359; //avoid going over the end of the array
}
}
return iAzimuth;
}
///////////////////////////////////////////////////////////////////////////
// PopulateAziTans()
//////////////////////////////////////////////////////////////////////////
void clPlot::PopulateAziTans()
{
try
{
float fAngle;
short int i; //loop counter
//Declare the array - sized at 360
mp_fAziTans = new float[360];
//For each array bucket, calculate the tangent to the degree that forms its
//lower bound (array index 0 = 0 degrees, array index 30 = 30 degrees).
//Watch out for 90 and 270! Set them to 10000.
for (i = 0; i < 360; i++)
{
if (i == 90 || i == 270)
{
mp_fAziTans[i] = 10000;
}
else
{
//Convert i to radians
fAngle = i * CONVERT_TO_RADIANS;
mp_fAziTans[i] = tan(fAngle);
}
}
}
catch (modelErr& err)
{
throw(err);
}
catch (modelMsg & msg)
{
throw(msg);
} //non-fatal error
catch (...)
{
modelErr stcErr;
stcErr.iErrorCode = UNKNOWN;
stcErr.sFunction = "clPlot::PopulateAziTans";
throw(stcErr);
}
}
///////////////////////////////////////////////////////////////////////////
// CorrectX()
//////////////////////////////////////////////////////////////////////////
float clPlot::CorrectX(float fX)
{
//Check for overflow-type conditions so we don't loop too deep
if (fabs(fX) > 10.0 * m_fPlotLenX)
{
modelErr stcErr;
stcErr.iErrorCode = BAD_DATA;
stcErr.sFunction = "clPlot::CorrectX";
std::stringstream s;
s << "Possible uninitialized variable - " << fX;
stcErr.sMoreInfo = s.str();
throw(stcErr);
}
if (fX < 0)
fX += m_fPlotLenX;
else if (fX >= m_fMaxX)
fX -= m_fMaxX;
//If that wasn't enough to fix it - try again
if (fX < 0 || fX >= m_fMaxX)
fX = CorrectX(fX);
return fX;
}
///////////////////////////////////////////////////////////////////////////
// CorrectY()
//////////////////////////////////////////////////////////////////////////
float clPlot::CorrectY(float fY)
{
if (fabs(fY) > 10.0 * m_fPlotLenY)
{
modelErr stcErr;
stcErr.iErrorCode = BAD_DATA;
stcErr.sFunction = "clPlot::CorrectX";
std::stringstream s;
s << "Possible uninitialized variable - " << fY;
stcErr.sMoreInfo = s.str();
throw(stcErr);
}
if (fY < 0)
fY += m_fPlotLenY;
else if (fY >= m_fMaxY)
fY -= m_fMaxY;
//If that wasn't enough to fix it - try again
if (fY < 0 || fY >= m_fMaxY)
fY = CorrectY(fY);
return fY;
}