-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeatherStream.cpp
More file actions
1792 lines (1559 loc) · 60.3 KB
/
WeatherStream.cpp
File metadata and controls
1792 lines (1559 loc) · 60.3 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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* WISE_Weather_Module: WeatherStream.cpp
* Copyright (C) 2023 WISE
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "types.h"
#include "weatherStream.pb.h"
#include "WTime.h"
#include "WeatherStream.h"
#include "WeatherCom_ext.h"
#include "results.h"
#include "DayCondition.h"
#include <fstream>
#include "propsysreplacement.h"
#include "GridCom_ext.h"
#include "misc.h"
#include "comcodes.h"
#include "REDappWrapper.h"
#include "doubleBuilder.h"
#include <vector>
#include <cstdint>
#include <algorithm>
#include "filesystem.hpp"
#include <boost/algorithm/string.hpp>
#include <boost/format.hpp>
#include "str_printf.h"
#ifdef DEBUG
#include <assert.h>
#endif
WeatherCondition::WeatherCondition() : m_timeManager(m_worldLocation), m_time((std::uint64_t)0, &m_timeManager) {
m_temp_alpha = -0.77;
m_temp_beta = 2.80; // temp alpha, beta defaults changed to Cdn averages 060619
m_temp_gamma = -2.20;
m_wind_alpha = 1.00;
m_wind_beta = 1.24;
m_wind_gamma = -3.59; // Denver defaults, "Beck & Trevitt, Forecasting diurnal variations in meteorological parameters for
// predicting fire behavior", 1989
m_spec_day.dFFMC = -1.0;
m_initialHFFMC = 0.0;
m_initialHFFMCTime = WTimeSpan(-1); // this means that we haven't specified an initial HFFMC value or time for that value
m_spec_day.dDMC = -1.0;
m_spec_day.dDC = -1.0;
m_spec_day.dBUI = -1.0;
m_spec_day.dISI = -1.0;
m_spec_day.dFWI = -1.0;
m_initialRain = 0.0;
m_fwi = new CCWFGM_FWI;
m_isCalculatedValuesValid = false;
m_options = FFMC_LAWSON;
m_firstHour = 0;
m_lastHour = 23;
}
WeatherCondition::WeatherCondition(const WeatherCondition &toCopy) : m_timeManager(m_worldLocation), m_time((std::uint64_t)0, &m_timeManager) {
*this = toCopy;
}
WeatherCondition &WeatherCondition::operator=(const WeatherCondition &toCopy) {
if (this == &toCopy)
return *this;
m_worldLocation = toCopy.m_worldLocation;
m_time.SetTime(toCopy.m_time);
m_temp_alpha = toCopy.m_temp_alpha;
m_temp_beta = toCopy.m_temp_beta;
m_temp_gamma = toCopy.m_temp_gamma;
m_wind_alpha = toCopy.m_wind_alpha;
m_wind_beta = toCopy.m_wind_beta;
m_wind_gamma = toCopy.m_wind_gamma;
m_spec_day = toCopy.m_spec_day;
m_initialHFFMC = toCopy.m_initialHFFMC;
m_initialHFFMCTime = toCopy.m_initialHFFMCTime;
m_initialRain = toCopy.m_initialRain;
m_options = toCopy.m_options;
m_firstHour = toCopy.m_firstHour;
m_lastHour = toCopy.m_lastHour;
m_fwi = new CCWFGM_FWI;
DailyCondition *dc = toCopy.m_readings.LH_Head();
while (dc->LN_Succ()) {
DailyCondition *ndc = new DailyCondition(*dc, this);
m_readings.AddTail(ndc);
dc = dc->LN_Succ();
}
m_isCalculatedValuesValid = toCopy.m_isCalculatedValuesValid;
return *this;
}
WeatherCondition::~WeatherCondition() {
ClearWeatherData();
}
DailyCondition * WeatherCondition::getDCReading(const WTime &time, bool add) {
DailyCondition *dc;
WTimeSpan index = time - m_time;
if (index.GetTotalSeconds() < 0) {
//if not allowed to add
if (!add)
return nullptr;
//if adding the day before the current first specified day and that day has data to hour 0
if (index.GetDays() == -1 && m_firstHour == 0) {
dc = new DailyCondition(this);
m_readings.AddHead(dc);
ClearConditions();
m_time -= WTimeSpan(1, 0, 0, 0);
return dc;
}
return nullptr;
}
dc = m_readings.IndexNode((std::uint32_t)index.GetDays());
if ((!dc) && (add)) {
if (m_readings.GetCount() > 0)
{
//no appending if the last day doesn't end on hour 23 and only append the day after the last day of the stream
if (m_lastHour != 23 || (index.GetDays() != m_readings.GetCount()))
return nullptr;
}
dc = new DailyCondition(this);
m_readings.AddTail(dc);
ClearConditions();
}
return dc;
}
void WeatherCondition::calculateValues() {
if (m_isCalculatedValuesValid)
return;
m_isCalculatedValuesValid = true;
if (m_weatherStation) {
PolymorphicAttribute v;
double temp;
m_weatherStation->GetAttribute(CWFGM_GRID_ATTRIBUTE_LATITUDE, &v);
VariantToDouble_(v, &temp);
m_worldLocation.m_latitude(temp);
m_weatherStation->GetAttribute(CWFGM_GRID_ATTRIBUTE_LONGITUDE, &v);
VariantToDouble_(v, &temp);
m_worldLocation.m_longitude(temp);
}
if (m_readings.IsEmpty()) // the stream has no data associated with it so abort now
return;
DailyCondition *fakeLast, *dc = m_readings.LH_Tail(); // this will take the diurnal curves and finish them off for the last day
if (!(dc->m_flags & DAY_HOURLY_SPECIFIED)) {
fakeLast = new DailyCondition(this);
m_readings.AddTail(fakeLast);
fakeLast->setDailyWeather(dc->dailyMinTemp(), dc->dailyMaxTemp(), dc->dailyMinWS(), dc->dailyMaxWS(), dc->dailyMinGust(), dc->dailyMaxGust(), dc->dailyMeanRH(), dc->dailyPrecip(), dc->dailyWD());
}
std::uint16_t i = 0;
dc = m_readings.LH_Head();
while (dc->LN_Succ()) {
dc->calculateTimes(i++);
dc = dc->LN_Succ();
}
dc = m_readings.LH_Head();
while (dc->LN_Succ()) {
dc->calculateHourlyConditions();
dc->calculateDailyConditions();
dc = dc->LN_Succ();
}
dc = m_readings.LH_Head();
while (dc->LN_Succ()) {
dc->calculateRemainingHourlyConditions();
dc = dc->LN_Succ();
}
dc = m_readings.LH_Tail(); // this will take the diurnal curves and finish them off for the last day
if (!(dc->m_flags & DAY_HOURLY_SPECIFIED)) {
m_readings.Remove(fakeLast); // then clean up
HSS_PRAGMA_WARNING_PUSH
HSS_PRAGMA_GCC(GCC diagnostic ignored "-Wdelete-non-virtual-dtor")
delete fakeLast;
HSS_PRAGMA_WARNING_POP
}
dc = m_readings.LH_Head();
while (dc->LN_Succ()) {
dc->calculateFWI();
dc->m_flags |= DAY_HOURLY_SPECIFIED; // #359 - Neal wants to just use the Beck/Trevitt work as a calculator now, and not store both types of wx conditions
dc = dc->LN_Succ();
}
}
bool WeatherCondition::GetDailyWeatherValues(const WTime &time, double *min_temp, double *max_temp, double *min_ws, double *max_ws, double* min_gust, double* max_gust, double *min_rh, double *precip, double *wd) {
DailyCondition *dc = getDCReading(time, false);
if (dc) {
calculateValues();
dc->getDailyWeather(min_temp, max_temp, min_ws, max_ws, min_gust, max_gust, min_rh, precip, wd);
}
return (dc != NULL);
}
bool WeatherCondition::SetDailyWeatherValues(const WTime &time, double min_temp, double max_temp, double min_ws, double max_ws, double min_gust, double max_gust, double min_rh, double precip, double wd) {
DailyCondition *dc = getDCReading(time, true);
if (dc) {
if (dc->m_flags & DAY_HOURLY_SPECIFIED)
return false;
double min_temp2, max_temp2, min_ws2, max_ws2, min_gust2, max_gust2, min_rh2, precip2, wd2;
double min_temp3, max_temp3, min_ws3, max_ws3, min_gust3, max_gust3, min_rh3, precip3, wd3;
dc->getDailyWeather(&min_temp2, &max_temp2, &min_ws2, &max_ws2, &min_gust2, &max_gust2, &min_rh2, &precip2, &wd2);
dc->setDailyWeather(min_temp, max_temp, min_ws, max_ws, min_gust, max_gust, min_rh, precip, wd);
dc->getDailyWeather(&min_temp3, &max_temp3, &min_ws3, &max_ws3, &min_gust3, &max_gust3, &min_rh3, &precip3, &wd3); // we are converting between data types so I'm trying to deal with rounding errors from those conversions
if ((fabs(min_temp2 - min_temp3) > 1e-5) ||
(fabs(max_temp2 - max_temp3) > 1e-5) ||
(fabs(min_ws2 - min_ws3) > 1e-5) ||
(fabs(max_ws2 - max_ws3) > 1e-5) ||
(fabs(min_gust2 - min_gust3) > 1e-5) ||
(fabs(max_gust2 - max_gust3) > 1e-5) ||
(fabs(min_rh2 - min_rh3) > 1e-5) ||
(fabs(precip2 - precip3) > 1e-5) ||
(fabs(wd2 - wd3) > 1e-5))
m_options &= (~(USER_SPECIFIED));
ClearConditions();
}
return (dc != NULL);
}
double WeatherCondition::GetHourlyRain(const WTime &time) {
DailyCondition *_dc = getDCReading(time, false);
if (_dc)
return _dc->hourlyPrecip(time);
return 0.0;
}
bool WeatherCondition::SetHourlyWeatherValues(const WTime &time, double temp, double rh, double precip, double ws, double gust, double wd, double dew) {
return SetHourlyWeatherValues(time, temp, rh, precip, ws, gust, wd, dew, false);
}
bool WeatherCondition::SetHourlyWeatherValues(const WTime& time, double temp, double rh, double precip, double ws, double gust, double wd, double dew, bool interp) {
return SetHourlyWeatherValues(time, temp, rh, precip, ws, gust, wd, dew, interp, false);
}
bool WeatherCondition::SetHourlyWeatherValues(const WTime &time, double temp, double rh, double precip, double ws, double gust, double wd, double dew, bool interp, bool ensemble) {
DailyCondition *dc = getDCReading(time, true);
if (dc) {
if (!(dc->m_flags & DAY_HOURLY_SPECIFIED))
return false;
WTime tm(m_time);
tm += WTimeSpan(m_readings.GetCount(), -(24 - m_lastHour), 0, 0);
WTimeSpan diff = time - tm;
if (diff.GetTotalHours() > 1)
return false;
else if (diff.GetTotalHours() == 1) {
m_lastHour += diff.GetTotalHours();
m_lastHour = m_lastHour % 24;
}
else if (time.GetHour(WTIME_FORMAT_AS_LOCAL | WTIME_FORMAT_WITHDST) < m_lastHour && (m_readings.GetCount() < 2 || diff.GetTotalHours() == -23))
m_lastHour = time.GetHour(WTIME_FORMAT_AS_LOCAL | WTIME_FORMAT_WITHDST);
double temp2, rh2, precip2, ws2, gust2, wd2, dew2;
double temp3, rh3, precip3, ws3, gust3, wd3, dew3;
dc->hourlyWeather(time, &temp2, &rh2, &precip2, &ws2, &gust2, &wd2, &dew2);
dc->setHourlyWeather(time, temp, rh, precip, ws, gust, wd, dew);
dc->hourlyWeather(time, &temp3, &rh3, &precip3, &ws3, &gust3, &wd3, &dew3);
if ((fabs(temp2 - temp3) > 1e-5) ||
(fabs(rh2 - rh3) > 1e-5) ||
(fabs(precip2 - precip3) > 1e-5) ||
(fabs(ws2 - ws3) > 1e-5) ||
(fabs(gust2 - gust3) > 1e-5) ||
(fabs(wd2 - wd3) > 1e-5) ||
(fabs(dew2 - dew3) > 1e-5)) {
m_options &= (~(USER_SPECIFIED));
if (ensemble) {
dc->m_flags |= DAY_ORIGIN_ENSEMBLE;
m_options |= 0x00000040;
}
else
dc->m_flags |= DAY_ORIGIN_MODIFIED;
}
if (interp) {
dc->setHourInterpolated(time.GetHour(WTIME_FORMAT_AS_LOCAL | WTIME_FORMAT_WITHDST));
}
else {
dc->clearHourInterpolated(time.GetHour(WTIME_FORMAT_AS_LOCAL | WTIME_FORMAT_WITHDST));
}
ClearConditions();
}
return (dc != NULL);
}
bool WeatherCondition::MakeHourlyObservations(const WTime &time) {
DailyCondition *dc = getDCReading(time, true);
if (dc) {
if (!(dc->m_flags & DAY_HOURLY_SPECIFIED)) {
dc->m_flags |= DAY_HOURLY_SPECIFIED;
ClearConditions();
}
}
return (dc != NULL);
}
bool WeatherCondition::MakeDailyObservations(const WTime &time) {
DailyCondition *dc = getDCReading(time, true);
if (dc) {
if (dc->m_flags & DAY_HOURLY_SPECIFIED) {
dc->m_flags &= (~(DAY_HOURLY_SPECIFIED));
ClearConditions();
}
}
return (dc != NULL);
}
std::int16_t WeatherCondition::WarnOnSunRiseSet() {
std::int16_t retval = 0;
DailyCondition *dc = m_readings.LH_Head();
while (dc->LN_Succ()) {
if (!(dc->m_flags & DAY_HOURLY_SPECIFIED)) {
if (dc->m_DayStart == dc->m_SunRise)
retval |= NO_SUNRISE;
if ((dc->m_DayStart + WTimeSpan(0, 23, 59, 59)) == dc->m_SunSet)
retval |= NO_SUNSET;
}
dc = dc->LN_Succ();
}
return retval;
}
std::uint16_t WeatherCondition::IsHourlyObservations(const WTime &time) {
DailyCondition *dc = getDCReading(time, false);
if (dc) {
if (dc->m_flags & DAY_HOURLY_SPECIFIED)
{
int32_t hour = time.GetHour(WTIME_FORMAT_AS_LOCAL | WTIME_FORMAT_WITHDST);
if (hour < firstHourOfDay(time) || hour > lastHourOfDay(time))
return 2;
return 1;
}
return 0;
}
return 2;
}
HRESULT WeatherCondition::IsAnyDailyObservations() const {
DailyCondition* dc = m_readings.LH_Head();
while (dc->LN_Succ()) {
if (!(dc->m_flags & DAY_HOURLY_SPECIFIED))
return S_OK;
dc = dc->LN_Succ();
}
return ERROR_SEVERITY_WARNING;
}
std::uint16_t WeatherCondition::IsModified(const WTime& time) {
DailyCondition* dc = getDCReading(time, false);
if (dc) {
if (dc->m_flags & DAY_ORIGIN_MODIFIED) {
int32_t hour = time.GetHour(WTIME_FORMAT_AS_LOCAL | WTIME_FORMAT_WITHDST);
if (hour < firstHourOfDay(time) || hour > lastHourOfDay(time))
return 2;
return 1;
}
return 0;
}
return 2;
}
HRESULT WeatherCondition::IsAnyModified() const {
DailyCondition* dc = m_readings.LH_Head();
while (dc->LN_Succ()) {
if (dc->m_flags & DAY_ORIGIN_MODIFIED)
return S_OK;
dc = dc->LN_Succ();
}
return ERROR_SEVERITY_WARNING;
}
std::uint16_t WeatherCondition::IsOriginFile(const WTime &time) {
DailyCondition *dc = getDCReading(time, false);
if (dc) {
if (dc->m_flags & DAY_ORIGIN_FILE)
return 0; // it's from a file (originally, could have been modified since then)
return 1; // it's not CREATED from a file (created by the user)
}
return 2; // time/date doesn't exist
}
std::uint16_t WeatherCondition::IsOriginEnsemble(const WTime& time) {
DailyCondition* dc = getDCReading(time, false);
if (dc) {
if (dc->m_flags & DAY_ORIGIN_ENSEMBLE)
return 0; // it's from a file (originally, could have been modified since then)
return 1; // it's not CREATED from a file (created by the user)
}
return 2; // time/date doesn't exist
}
uint8_t WeatherCondition::firstHourOfDay(const WTime& time)
{
WTime temp(time);
temp.PurgeToDay(WTIME_FORMAT_AS_LOCAL | WTIME_FORMAT_WITHDST);
WTime start(m_time);
start.PurgeToDay(WTIME_FORMAT_AS_LOCAL | WTIME_FORMAT_WITHDST);
WTime end(start);
end += WTimeSpan(m_readings.GetCount(), 0, 0, 0);
if ((temp < start) || (temp > end))
return (uint8_t)-1;
if (start == temp)
return (uint8_t)m_firstHour;
return (uint8_t)0;
}
uint8_t WeatherCondition::lastHourOfDay(const WTime& time)
{
WTime temp(time);
temp.PurgeToDay(WTIME_FORMAT_AS_LOCAL | WTIME_FORMAT_WITHDST);
WTime start(m_time);
start.PurgeToDay(WTIME_FORMAT_AS_LOCAL | WTIME_FORMAT_WITHDST);
WTime end(start);
end += WTimeSpan(m_readings.GetCount() - 1, m_lastHour, 0, 0);
end.PurgeToDay(WTIME_FORMAT_AS_LOCAL | WTIME_FORMAT_WITHDST);
if ((temp < start) || (temp > end))
return (uint8_t)-1;
if (end == temp)
return (uint8_t)m_lastHour;
return (uint8_t)23;
}
bool WeatherCondition::GetInstantaneousValues(const WTime &time, std::uint32_t method, IWXData *wx, IFWIData *ifwi, DFWIData *dfwi) {
WTime nt1(time);
nt1.PurgeToHour(WTIME_FORMAT_AS_LOCAL | WTIME_FORMAT_WITHDST);
calculateValues();
WTime nt2(nt1);
nt2 += WTimeSpan(0, 1, 0, 0);
DailyCondition *dc1 = getDCReading(nt1, false),
*dc2 = getDCReading(nt2, false);
double perc1, perc2;
double rh1, rh2;
if ((!dc1) || (!dc2) || (nt1 == time) || (!(method & CWFGM_GETWEATHER_INTERPOLATE_TEMPORAL))) {
if (dc1) {
if (wx) {
dc1->hourlyWeather(time, &wx->Temperature, &wx->RH, &wx->Precipitation, &wx->WindSpeed, &wx->WindGust, &wx->WindDirection, &wx->DewPointTemperature);
if (method & CWFGM_GETWEATHER_INTERPOLATE_TEMPORAL) // if temporal interp is turned on...
if ((!dc2) && (nt1 != time)) // and we don't have a reading for the next day AND we were asking for some time other than 11pm
wx->Precipitation = 0.0;
wx->SpecifiedBits = IWXDATA_SPECIFIED_TEMPERATURE | IWXDATA_SPECIFIED_RH | IWXDATA_SPECIFIED_PRECIPITATION | IWXDATA_SPECIFIED_WINDSPEED | IWXDATA_SPECIFIED_WINDDIRECTION | IWXDATA_SPECIFIED_DEWPOINTTEMPERATURE;
if (wx->WindGust >= 0.0)
wx->SpecifiedBits |= IWXDATA_SPECIFIED_WINDGUST;
if (dc1->isTimeInterpolated(time))
wx->SpecifiedBits |= IWXDATA_SPECIFIED_INTERPOLATED;
}
}
if ((!dc1) || (nt1 == time) || (!(method & CWFGM_GETWEATHER_INTERPOLATE_TEMPORAL))) {
if (dc1) {
if (ifwi) {
ifwi->FFMC = dc1->hourlyFFMC(time);
ifwi->ISI = dc1->ISI(time);
ifwi->FWI = dc1->FWI(time);
if (dc1->isHourlyFFMCSpecified(time)) ifwi->SpecifiedBits = IFWIDATA_SPECIFIED_FWI;
else ifwi->SpecifiedBits = 0;
ifwi->SpecifiedBits |= (m_options & FFMC_MASK) << 16;
}
}
if (dfwi) {
dfwi->SpecifiedBits = 0;
bool spec;
DailyFFMC(time, &dfwi->dFFMC, &spec); if (spec) dfwi->SpecifiedBits |= DFWIDATA_SPECIFIED_FFMC;
DC(time, &dfwi->dDC, &spec); if (spec) dfwi->SpecifiedBits |= DFWIDATA_SPECIFIED_DC;
DMC(time, &dfwi->dDMC, &spec); if (spec) dfwi->SpecifiedBits |= DFWIDATA_SPECIFIED_DMC;
BUI(time, &dfwi->dBUI, &spec); if (spec) dfwi->SpecifiedBits |= DFWIDATA_SPECIFIED_BUI;
DailyISI(time, &dfwi->dISI);
DailyFWI(time, &dfwi->dFWI);
if ((dfwi->dFFMC >= 0.0) && (dfwi->dISI == -1.0) && (dc1)) {
weak_assert(!dc1->LN_Pred()->LN_Pred());
const WTime dayNeutral(dc1->m_DayStart, WTIME_FORMAT_AS_LOCAL | WTIME_FORMAT_WITHDST, 1); // convert to a timezone-neutral time
const WTime dayLST(dayNeutral, WTIME_FORMAT_AS_LOCAL, -1); // gets us the start of the true LST day
WTime dayNoon(dayLST);
dayNoon += WTimeSpan(0, 12, 0, 0);
double ws = dc1->hourlyWS(dayNoon);
m_fwi->ISI_FBP(dfwi->dFFMC, ws, 24 * 60 * 60, &dfwi->dISI);
m_fwi->FWI(dfwi->dISI, dfwi->dBUI, &dfwi->dFWI);
}
}
if (dc1) {
if (!dc1->LN_Pred()->LN_Pred()) { // if the first day...
std::int32_t hour = time.GetHour(WTIME_FORMAT_AS_LOCAL | WTIME_FORMAT_WITHDST);
if (hour < m_firstHour)
return false;
}
}
if ((dc2) && (dc2 == dc1)) {
if (!dc2->LN_Succ()->LN_Succ()) { // if the last day....
std::int32_t hour = time.GetHour(WTIME_FORMAT_AS_LOCAL | WTIME_FORMAT_WITHDST);
if (hour > m_lastHour)
return false;
}
}
return (dc1 != nullptr);
}
rh1 = rh2 = wx->RH;
} else {
weak_assert(dc1);
double t1, p1, ws1, wd1, gust1, dew1;
double t2, p2, ws2, wd2, gust2, dew2;
dc1->hourlyWeather(nt1, &t1, &rh1, &p1, &ws1, &gust1, &wd1, &dew1);
dc2->hourlyWeather(nt2, &t2, &rh2, &p2, &ws2, &gust2, &wd2, &dew2);
perc2 = ((double)(time.GetTime(0) - nt1.GetTime(0))) / 3600.0;
perc1 = 1.0 - perc2;
IWXData dwx;
if (!wx)
wx = &dwx;
wx->Temperature = t1 * perc1 + t2 * perc2;
wx->DewPointTemperature = dew1 * perc1 + dew2 * perc2;
wx->RH = rh1 * perc1 + rh2 * perc2;
wx->Precipitation = p2 * perc2;
bool bb1 = ((ws1 < 0.0001) && (wd1 < 0.0001));
bool bb2 = ((ws2 < 0.0001) && (wd2 < 0.0001));
double wd_diff = NORMALIZE_ANGLE_RADIAN(wd2 - wd1);
if (bb1) wx->WindDirection = wd2; // ws/wd at start of hour is dead calm so no interp on wd
else if (bb2) wx->WindDirection = wd1; // ws/wd at start of hour is dead calm so no interp on wd
else { // interp wd linearly
if ((ws1 >= 0.0001) && (ws2 >= 0.0001) && ((wd_diff < DEGREE_TO_RADIAN(181.0)) && (wd_diff > DEGREE_TO_RADIAN(179.0)))) {
WTimeSpan ts = nt2 - nt1;
ts /= 2;
if (time <= (nt1 + ts))
wx->WindDirection = wd1;
else wx->WindDirection = wd2;
} else {
if (wd_diff > CONSTANTS_NAMESPACE::Pi<double>())
wd_diff -= CONSTANTS_NAMESPACE::TwoPi<double>();
wx->WindDirection = NORMALIZE_ANGLE_RADIAN(wd2 - perc1 * wd_diff);
}
}
if ((ws1 >= 0.0001) && (ws2 >= 0.0001) && ((wd_diff < DEGREE_TO_RADIAN(181.0)) && (wd_diff > DEGREE_TO_RADIAN(179.0)))) {
WTimeSpan ts = nt2 - nt1;
ts /= 2;
if (time <= (nt1 + ts))
wx->WindSpeed = ws1;
else wx->WindSpeed = ws2;
} else wx->WindSpeed = ws1 * perc1 + ws2 * perc2; // interp ws linearly
wx->SpecifiedBits = 0;
if (dc1->isTimeInterpolated(time))
wx->SpecifiedBits |= IWXDATA_SPECIFIED_INTERPOLATED;
}
DFWIData ddfwi;
if (!dfwi)
dfwi = &ddfwi;
dfwi->SpecifiedBits = 0;
bool spec;
DailyFFMC(time, &dfwi->dFFMC, &spec); if (spec) dfwi->SpecifiedBits |= DFWIDATA_SPECIFIED_FFMC;
DC(time, &dfwi->dDC, &spec); if (spec) dfwi->SpecifiedBits |= DFWIDATA_SPECIFIED_DC;
DMC(time, &dfwi->dDMC, &spec); if (spec) dfwi->SpecifiedBits |= DFWIDATA_SPECIFIED_DMC;
BUI(time, &dfwi->dBUI, &spec); if (spec) dfwi->SpecifiedBits |= DFWIDATA_SPECIFIED_BUI;
DailyISI(time, &dfwi->dISI);
DailyFWI(time, &dfwi->dFWI);
if ((dfwi->dFFMC >= 0.0) && (dfwi->dISI == -1.0)) {
weak_assert(!dc1->LN_Pred()->LN_Pred());
const WTime dayNeutral(dc1->m_DayStart, WTIME_FORMAT_AS_LOCAL | WTIME_FORMAT_WITHDST, 1); // convert to a timezone-neutral time
const WTime dayLST(dayNeutral, WTIME_FORMAT_AS_LOCAL, -1); // gets us the start of the true LST day
WTime dayNoon(dayLST);
dayNoon += WTimeSpan(0, 12, 0, 0);
double ws = dc1->hourlyWS(dayNoon);
m_fwi->ISI_FBP(dfwi->dFFMC, ws, 24 * 60 * 60, &dfwi->dISI);
m_fwi->FWI(dfwi->dISI, dfwi->dBUI, &dfwi->dFWI);
}
if (ifwi) {
ifwi->SpecifiedBits = 0;
double ffmc1 = dc1->hourlyFFMC(nt1),
ffmc2 = (dc2) ? dc2->hourlyFFMC(nt2) : ffmc1;
bool fs2 = (dc2) ? dc2->isHourlyFFMCSpecified(nt2) : false;
if (fs2) {
ifwi->SpecifiedBits |= IFWIDATA_SPECIFIED_FWI;
ifwi->FFMC = ffmc1 * perc1 + ffmc2 * perc2; // RWB: 080203: if the target FFMC value was specified by the user, then we should linearly interpolate to it to make sure we are consistent
} else { // but because using ffmc1 as a starting code (below) we aren't concerned about checking fs1
switch (m_options & FFMC_MASK) {
case FFMC_LAWSON: { double prev_ffmc, today_ffmc;
WTime dayStart(time);
dayStart.PurgeToDay(WTIME_FORMAT_AS_LOCAL | WTIME_FORMAT_WITHDST);
const WTime dayNeutral(dayStart, WTIME_FORMAT_AS_LOCAL | WTIME_FORMAT_WITHDST, 1); // convert to a timezone-neutral time
const WTime dayStartLst(dayNeutral, WTIME_FORMAT_AS_LOCAL, -1); // gets us the start of the true LST day
DailyFFMC(dayStart , &prev_ffmc, &spec);
DailyFFMC(dayStart + WTimeSpan(0, 18, 0, 0), &today_ffmc, &spec);
m_fwi->HourlyFFMC_Lawson_Contiguous(prev_ffmc,
today_ffmc, wx->Precipitation, wx->Temperature, rh1, wx->RH, rh2, wx->WindSpeed,
(std::uint32_t)(time - dayStartLst).GetTotalSeconds(), &ifwi->FFMC);
break;
}
default: { double in_ffmc = ffmc1;
m_fwi->HourlyFFMC_VanWagner(in_ffmc, wx->Precipitation, wx->Temperature, wx->RH, wx->WindSpeed,
WTimeSpan(time - nt1).GetTotalSeconds(), &ifwi->FFMC);
break;
}
}
}
m_fwi->ISI_FBP(ifwi->FFMC, wx->WindSpeed, time.GetTotalSeconds(), &ifwi->ISI);
m_fwi->FWI(ifwi->ISI, dfwi->dBUI, &ifwi->FWI);
}
return true;
}
bool WeatherCondition::HourlyFFMC(const WTime &time, double *ffmc) {
DailyCondition *_dc = getDCReading(time, false);
if (_dc) {
calculateValues();
*ffmc = _dc->hourlyFFMC(time);
} else if ((time < m_time) && (m_initialHFFMCTime == WTimeSpan(-1 * 60 * 60))) {
*ffmc = m_initialHFFMC;
return true;
} else {
weak_assert(false);
}
return (_dc != NULL);
}
bool WeatherCondition::DailyFFMC(const WTime &time, double *ffmc, bool *specified) {
const WTime dayNeutral(time, WTIME_FORMAT_AS_LOCAL, 1); // convert to a timezone-neutral time
const WTime dayLST(dayNeutral, WTIME_FORMAT_AS_LOCAL | WTIME_FORMAT_WITHDST, -1); // gets us the start of the true LST day
WTime dayNoon(dayLST);
dayNoon -= WTimeSpan(0, 12, 0, 0);
dayNoon.PurgeToDay(WTIME_FORMAT_AS_LOCAL | WTIME_FORMAT_WITHDST);
DailyCondition *_dc = getDCReading(dayNoon, false);
if (_dc) {
calculateValues();
*ffmc = _dc->dailyFFMC();
*specified = _dc->dailyFFMCSpecified();
} else if (dayNoon < m_time) {
*ffmc = m_spec_day.dFFMC;
*specified = true;
return true;
}
return (_dc != NULL);
}
bool WeatherCondition::DailyISI(const WTime &time, double *isi) {
const WTime dayNeutral(time, WTIME_FORMAT_AS_LOCAL, 1); // convert to a timezone-neutral time
const WTime dayLST(dayNeutral, WTIME_FORMAT_AS_LOCAL | WTIME_FORMAT_WITHDST, -1); // gets us the start of the true LST day
WTime dayNoon(dayLST);
dayNoon -= WTimeSpan(0, 12, 0, 0);
dayNoon.PurgeToDay(WTIME_FORMAT_AS_LOCAL | WTIME_FORMAT_WITHDST);
DailyCondition *_dc = getDCReading(dayNoon, false);
if (_dc) {
calculateValues();
*isi = _dc->dailyISI();
}
else if (dayNoon < m_time) {
*isi = -1.0;
return true;
}
return (_dc != nullptr);
}
bool WeatherCondition::DailyFWI(const WTime &time, double *fwi) {
const WTime dayNeutral(time, WTIME_FORMAT_AS_LOCAL, 1); // convert to a timezone-neutral time
const WTime dayLST(dayNeutral, WTIME_FORMAT_AS_LOCAL | WTIME_FORMAT_WITHDST, -1); // gets us the start of the true LST day
WTime dayNoon(dayLST);
dayNoon -= WTimeSpan(0, 12, 0, 0);
dayNoon.PurgeToDay(WTIME_FORMAT_AS_LOCAL | WTIME_FORMAT_WITHDST);
DailyCondition *_dc = getDCReading(dayNoon, false);
if (_dc) {
calculateValues();
*fwi = _dc->dailyFWI();
}
else if (dayNoon < m_time) {
*fwi = -1.0;
return true;
}
return (_dc != nullptr);
}
bool WeatherCondition::DC(const WTime &time, double *dc, bool *specified) {
const WTime dayNeutral(time, WTIME_FORMAT_AS_LOCAL, 1); // convert to a timezone-neutral time
const WTime dayLST(dayNeutral, WTIME_FORMAT_AS_LOCAL | WTIME_FORMAT_WITHDST, -1); // gets us the start of the true LST day
WTime dayNoon(dayLST);
dayNoon -= WTimeSpan(0, 12, 0, 0);
dayNoon.PurgeToDay(WTIME_FORMAT_AS_LOCAL | WTIME_FORMAT_WITHDST);
DailyCondition *_dc = getDCReading(dayNoon, false);
if (_dc) {
calculateValues();
*dc = _dc->DC();
*specified = _dc->DCSpecified();
} else if (dayNoon < m_time) {
*dc = m_spec_day.dDC;
*specified = true;
return true;
}
return (_dc != nullptr);
}
bool WeatherCondition::DMC(const WTime &time, double *dmc, bool *specified) {
const WTime dayNeutral(time, WTIME_FORMAT_AS_LOCAL, 1); // convert to a timezone-neutral time
const WTime dayLST(dayNeutral, WTIME_FORMAT_AS_LOCAL | WTIME_FORMAT_WITHDST, -1); // gets us the start of the true LST day
WTime dayNoon(dayLST);
dayNoon -= WTimeSpan(0, 12, 0, 0);
dayNoon.PurgeToDay(WTIME_FORMAT_AS_LOCAL | WTIME_FORMAT_WITHDST);
DailyCondition *_dc = getDCReading(dayNoon, false);
if (_dc) {
calculateValues();
*dmc = _dc->DMC();
*specified = _dc->DMCSpecified();
} else if (dayNoon < m_time) {
*dmc = m_spec_day.dDMC;
*specified = true;
return true;
}
return (_dc != nullptr);
}
bool WeatherCondition::BUI(const WTime &time, double *bui, bool *specified, bool recalculate) {
const WTime dayNeutral(time, WTIME_FORMAT_AS_LOCAL, 1); // convert to a timezone-neutral time
const WTime dayLST(dayNeutral, WTIME_FORMAT_AS_LOCAL | WTIME_FORMAT_WITHDST, -1); // gets us the start of the true LST day
WTime dayNoon(dayLST);
dayNoon -= WTimeSpan(0, 12, 0, 0);
dayNoon.PurgeToDay(WTIME_FORMAT_AS_LOCAL | WTIME_FORMAT_WITHDST);
DailyCondition *_dc = getDCReading(dayNoon, false);
if (_dc) {
if (recalculate)
calculateValues();
*bui = _dc->BUI();
*specified = _dc->BUISpecified();
} else if (dayNoon < m_time) {
if (m_spec_day.dBUI < 0.0) {
m_fwi->BUI(m_spec_day.dDC, m_spec_day.dDMC, bui);
*specified = false;
}
else {
*bui = m_spec_day.dBUI;
*specified = true;
}
return true;
}
return (_dc != nullptr);
}
bool WeatherCondition::CumulativePrecip(const WTime &time, const WTimeSpan &duration, double *rain) {
// this function needs more work, since it assumes both 'time' and 'duration' are on even hour increments
WTime t(time, &m_timeManager);
WTime end_t = m_time + WTimeSpan((std::int32_t)0, (std::int32_t)m_firstHour, (std::int32_t)0, (std::int32_t)0);
t.PurgeToHour(WTIME_FORMAT_AS_LOCAL | WTIME_FORMAT_WITHDST);
std::uint32_t hrs = duration.GetTotalHours();
*rain = 0.0;
std::uint16_t i;
for (i = 0; i < hrs; i++) {
if (t <= end_t)
break;
*rain += GetHourlyRain(t);
t -= WTimeSpan(0, 1, 0, 0);
}
if (i < hrs)
*rain += m_initialRain;
return true;
}
bool WeatherCondition::HourlyISI(const WTime &time, double *isi) {
DailyCondition *_dc = getDCReading(time, false);
if (_dc) {
calculateValues();
*isi = _dc->ISI(time);
return true;
}
return false;
}
bool WeatherCondition::HourlyFWI(const WTime &time, double *fwi) {
DailyCondition *_dc = getDCReading(time, false);
if (_dc) {
calculateValues();
*fwi = _dc->FWI(time);
return true;
}
return false;
}
void WeatherCondition::ClearConditions() {
m_isCalculatedValuesValid = false;
}
bool WeatherCondition::AnyFWICodesSpecified() {
DailyCondition *dc = m_readings.LH_Head();
while (dc->LN_Succ()) {
if (dc->AnyFWICodesSpecified())
return true;
dc = dc->LN_Succ();
}
return false;
}
void WeatherCondition::GetEndTime(WTime &EndTime)
{
int count=m_readings.GetCount() - 1;
WTimeSpan timeSpan(count, 0, 0, 0);
EndTime=WTime(m_time);
EndTime+=timeSpan;
EndTime += WTimeSpan(0, m_lastHour, 59, 59);
}
void WeatherCondition::SetEndTime(WTime &endTime) {
WTime currentEndTime(m_time);
WTimeSpan ts;
bool IncFlag = false;
std::uint32_t oldDays = NumDays();
if (oldDays==0) {
return;
}
currentEndTime = m_time + WTimeSpan(oldDays - 1, 0, 0, 0);
if (endTime > currentEndTime) {
ts = endTime - currentEndTime;
IncFlag = true;
} else {
ts = currentEndTime - endTime;
IncFlag = false;
}
std::uint32_t days = (std::uint32_t)ts.GetDays();
if (days >= oldDays && IncFlag == false)
days = oldDays;
if (IncFlag)
IncreaseConditions(currentEndTime, days);
else
DecreaseConditions(currentEndTime, days);
}
void WeatherCondition::IncreaseConditions(WTime ¤tEndTime, std::uint32_t days) {
WTime tempTime(currentEndTime);
tempTime += WTimeSpan(1, 0, 0, 0);
WTime start(tempTime.GetYear(0), tempTime.GetMonth(0), tempTime.GetDay(0), 0, 0, 0, currentEndTime.GetTimeManager());
for (std::uint32_t i = 0; i < days; i++) {
CopyDailyCondition(currentEndTime, start);
start += WTimeSpan(1, 0, 0, 0);
}
}
void WeatherCondition::DecreaseConditions(WTime ¤tEndTime, std::uint32_t days) {
DailyCondition *dc;
for(std::uint32_t i=0;i<days;i++) {
dc = m_readings.RemTail();
HSS_PRAGMA_WARNING_PUSH
HSS_PRAGMA_GCC(GCC diagnostic ignored "-Wdelete-non-virtual-dtor")
delete dc;
HSS_PRAGMA_WARNING_POP
}
}
void WeatherCondition::CopyDailyCondition(WTime &source, WTime &dest)
{
double min_temp;
double max_temp;
double min_ws;
double max_ws;
double min_gust;
double max_gust;
double rh;
double precip;
double wd;
GetDailyWeatherValues(source, &min_temp, &max_temp, &min_ws, &max_ws, &min_gust, &max_gust, &rh, &precip, &wd);
SetDailyWeatherValues(dest,min_temp,max_temp,min_ws,max_ws,min_gust,max_gust,rh,precip,wd);
}
void WeatherCondition::ClearWeatherData() {
DailyCondition *dc;
HSS_PRAGMA_WARNING_PUSH
HSS_PRAGMA_GCC(GCC diagnostic ignored "-Wdelete-non-virtual-dtor")
while ((dc = m_readings.RemHead()))
delete dc;
HSS_PRAGMA_WARNING_POP
}
bool isWeatherCollectionValid(const WeatherCollection &collection)
{
if ((collection.wd < 0.0) || (collection.wd > 360.0) ||
(collection.ws < 0.0) ||
(collection.rh < 0.0) || (collection.rh > 100.0) ||
(collection.precip < 0.0) ||
(collection.temp < -50.0) || (collection.temp > 60.0) ||
((collection.DMC < 0.0) && (collection.DMC != -1.0)) || ((collection.DC < 0.0) && (collection.DC != -1.0)) ||
(collection.DMC > 500.0) || (collection.DC > 1500.0))
return false;
return true;
}
HRESULT WeatherCondition::Import(const TCHAR *fileName, std::uint16_t options, std::shared_ptr<validation::validation_object> valid) {
std::vector<std::string> header;
DailyCondition *dc;
HRESULT hr = S_OK;
bool can_append = (options & CWFGM_WEATHERSTREAM_IMPORT_SUPPORT_APPEND) ? true : false;
if (m_readings.IsEmpty())
can_append = true;
if (options & CWFGM_WEATHERSTREAM_IMPORT_PURGE) {
ClearWeatherData();
can_append = true;
}
WeatherCollection *wc = nullptr;
int hour = 0, noonhour = -1;
std::uint16_t mode = 0;
std::uint32_t lines = 0;
FILE *f = NULL;
HSS_PRAGMA_WARNING_PUSH
HSS_PRAGMA_GCC(GCC diagnostic ignored "-Wunused-value")
_tfopen_s(&f, fileName, _T("r"));
HSS_PRAGMA_WARNING_POP
if (f) {
TCHAR file_type[40], line[256];
if (!_fgetts(line, 255, f)) { fclose(f); return ERROR_READ_FAULT | ERROR_SEVERITY_WARNING; }
ProcessHeader(line, header);
std::string flagStr;
flagStr = header[0];
if (boost::iequals(flagStr, _T("daily")))
mode = 1;
else if (boost::iequals(flagStr, _T("hourly")))
mode = 2;
else if (boost::iequals(flagStr, _T("date"))) {
mode = 1;
for (size_t ii = 1; ii < header.size(); ii++) {
std::string str = header[ii];