forked from beerelf/AWAS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy of SDFdata.cpp
executable file
·4722 lines (3934 loc) · 129 KB
/
Copy of SDFdata.cpp
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
#include "stdafx.h"
#include <math.h>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <cstdlib>
#include "TextImportDlg.h"
#include "SDFdata.h"
#include "utils.h"
#include "stl_utils.h"
char* SiteData::m_timeUnitsStr[] = {"Days", "Weeks", "Months", "Years"};
vector<int> SiteData::daysPerMonth;
WellPumpingTD::wellUnitsEnum WellPumpingTD::m_pumpingUnits = WellPumpingTD::Acft;
// *** WARNING: this is copied from the SDFdaily project. DO not edit this!!
//######################### SitesManager ########################//
int SitesManager::NoData = -999;
float SitesManager::m_currentVersion = 2.9f;
SitesManager::SitesManager()
: m_startYear(0), m_endYear(0), m_histEndYear(0), m_histStartYear(0),
m_simStartYear(0), m_simEndYear(0),
m_simStartMonth(0), m_runIgnoreMonth(11),
m_runIgnoreYear(0), m_useIgnoreYear(false),
m_displayMode(DISPLAY_ORIGINAL), m_yearMode(YEAR_CALENDAR),
m_projDataColor(RGB(200, 255, 0)),
m_forecastMode(ItemData::FORECAST_NONE), m_preForecastMode(ItemData::FORECAST_NONE),
m_useAverageDaysInMonth(false), m_useMonthlyURFForDaily(false),
m_copyIncludesHeader(false), m_useNewErrorFunc(false)
{
for (int m=0; m<12; m_pMult[m++] = 1);
}
string
SitesManager::ReadProject(string fname, bool ignoreOutput, bool checkForYearType, bool addInPlace)
{
// Check for text file import from SPCU.
{
int s = fname.length();
if (s > 3) {
if (fname.substr(s-3, 3) == "txt") {
return ImportProjectNew(fname, addInPlace);
// *** disabled old format
#if 0
// If this is an old format of the spreadsheet, then ask for year type.
ItemData::YearTypeEnum yearType = ItemData::CALENDAR;
ifstream istr(fname.c_str());
istr.getline(buf, sizeof(buf));
vector<string> tokens;
stringtok(tokens, buf, "\t");
if (tokens.size() > 5) {
// New format, year type is stored in the spreadsheet.
return ImportProjectNew(fname, addInPlace);
}
else {
if (checkForYearType) {
TextImportDlg dlg;
if (dlg.DoModal() == IDOK) {
yearType = (ItemData::YearTypeEnum)dlg.m_yearType;
}
else {
return "";
}
}
return ImportProject(fname, yearType, addInPlace);
}
// *** end disabled code
#endif
}
if (fname.substr(s-3, 3) == "sdf") {
return ImportSDFView(fname, addInPlace);
}
}
}
if (*(fname.end()-1) == 'o') {
ReadOutput(fname);
// Request for output file, but read input first.
//*(fname.end() -1) = 'i';
return "";
}
string retval;
int ival;
ifstream istr(fname.c_str());
if (!istr.is_open()) {
retval += "Unable to open project file " + fname + "\n";
return retval;
}
// Save basename so we know where to write results to.
int loc = fname.rfind('.');
m_baseName = fname.substr(0, loc);
// First line is version (0 == original, 1 == modified
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "version") == NULL) {
retval += "Parse error looking for 'version=' header in file " + fname + "\n";
return retval;
}
float version;
istr >> version;
if (version > m_currentVersion) {
return "Error: the input file has a version number that is newer than the program version. Please update the software by clicking \"Check for Updates\" in the help menu.";
}
if (!addInPlace)
{
m_yearMode = YEAR_CALENDAR;
if (version >= 1.1f) {
// Read period of record. Makes sense only for daily form.
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "start year") == NULL) {
retval += "Parse error looking for 'start year=' header in file " + fname + "\n";
return retval;
}
istr >> m_startYear;
// Read historical start year.
if (version >= 1.7f) {
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "historical start year") == NULL) {
retval += "Parse error looking for 'historical start year=' header in file " + fname + "\n";
return retval;
}
istr >> m_histStartYear;
}
if (version >= 1.6f) {
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "historical end year") == NULL) {
retval += "Parse error looking for 'historical end year=' header in file " + fname + "\n";
return retval;
}
istr >> m_histEndYear;
}
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "end year") == NULL) {
retval += "Parse error looking for 'end year=' header in file " + fname + "\n";
return retval;
}
istr >> m_endYear;
if (version >= 1.7f) {
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "synth label") == NULL) {
retval += "Parse error looking for 'synth label=' header in file " + fname + "\n";
return retval;
}
istr.getline(buf, sizeof(buf));
m_synthDataLabel = buf;
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "presynth label") == NULL) {
retval += "Parse error looking for 'presynth label=' header in file " + fname + "\n";
return retval;
}
istr.getline(buf, sizeof(buf));
m_preSynthDataLabel = buf;
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "synth mode") == NULL) {
retval += "Parse error looking for 'synth mode=' header in file " + fname + "\n";
return retval;
}
int ival;
istr >> ival;
m_forecastMode = (ItemData::ForecastEnum)ival;
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "presynth mode") == NULL) {
retval += "Parse error looking for 'presynth mode=' header in file " + fname + "\n";
return retval;
}
istr >> ival;
m_preForecastMode = (ItemData::ForecastEnum)ival;
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "number of synth years") == NULL) {
retval += "Parse error looking for 'number of synth years=' header in file " + fname + "\n";
return retval;
}
int nitems;
istr >> nitems;
m_yearList.resize(nitems);
unsigned int i;
for (i=0; i<nitems; i++) {
istr >> m_yearList[i];
}
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "number of presynth years") == NULL) {
retval += "Parse error looking for 'number of presynth years=' header in file " + fname + "\n";
return retval;
}
istr >> nitems;
m_preYearList.resize(nitems);
for (i=0; i<nitems; i++) {
istr >> m_preYearList[i];
}
}
if (version < 1.6f) m_histEndYear = m_endYear;
if (version < 1.7f) m_histStartYear = m_startYear;
if (version >= 1.3f) {
if (version < 1.5f) {
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "native year mode") == NULL) {
retval += "Parse error looking for 'native year mode=' header in file " + fname + "\n";
return retval;
}
istr >> ival;
//m_nativeYearMode = (SitesManager::YearModeEnum)ival;
}
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "view year mode") == NULL) {
retval += "Parse error looking for 'view year mode=' header in file " + fname + "\n";
return retval;
}
istr >> ival;
m_yearMode = (SitesManager::YearModeEnum)ival;
}
}
if (version >= 1.4f) {
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "display mode") == NULL) {
retval += "Parse error looking for 'display=' header in file " + fname + "\n";
return retval;
}
istr >> ival;
m_displayMode = (DisplayModeEnum)ival;
}
SiteData::TimeUnitsEnum timeUnits;
if (version >= 1.7f) {
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "time units") == NULL) {
retval += "Parse error looking for 'time units=' header in file " + fname + "\n";
return retval;
}
istr >> ival;
timeUnits = (SiteData::TimeUnitsEnum)ival;
}
if (version >= 1.8f) {
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "use average days per month") == NULL) {
retval += "Parse error looking for 'use average days per month=' header in file " + fname + "\n";
return retval;
}
istr >> ival;
m_useAverageDaysInMonth = (bool)ival;
}
if (version >= 2.9f) {
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "URF uses monthly values for daily calculation") == NULL) {
retval += "Parse error looking for 'URF uses monthly values for daily calculation=' header in file " + fname + "\n";
return retval;
}
istr >> ival;
m_useMonthlyURFForDaily = (bool)ival;
}
if (version >= 2.6f) {
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "use new error function") == NULL) {
retval += "Parse error looking for 'use new error function=' header in file " + fname + "\n";
return retval;
}
istr >> ival;
m_useNewErrorFunc = (bool)ival;
}
if (version >= 1.9f) {
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "run start") == NULL) {
retval += "Parse error looking for 'run start=' header in file " + fname + "\n";
return retval;
}
istr >> m_simStartYear;
if (version >= 2.0f) {
// Start month is also included.
istr >> m_simStartMonth;
}
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "run end") == NULL) {
retval += "Parse error looking for 'run end=' header in file " + fname + "\n";
return retval;
}
istr >> m_simEndYear;
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "run ignore year") == NULL) {
retval += "Parse error looking for 'run ignore year=' header in file " + fname + "\n";
return retval;
}
istr >> m_runIgnoreYear;
if (version >= 2.0f) {
// Start month is also included.
istr >> m_runIgnoreMonth;
}
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "use run ignore year") == NULL) {
retval += "Parse error looking for 'use run ignore year=' header in file " + fname + "\n";
return retval;
}
int ival;
istr >> ival;
m_useIgnoreYear = ival;
}
else {
m_simStartYear = m_startYear;
m_simEndYear = m_endYear;
m_runIgnoreYear = m_endYear;
}
if (version >= 2.1f) {
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "projected data multipliers") == NULL) {
retval += "Parse error looking for 'projected data multipliers=' header in file " + fname + "\n";
return retval;
}
for (int m=0; m<12; m++) {
istr >> m_pMult[m];
}
}
if (version >= 2.5f) {
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "copy includes header") == NULL) {
retval += "Parse error looking for 'copy includes header=' header in file " + fname + "\n";
return retval;
}
istr >> m_copyIncludesHeader;
}
}
else
{
// Skip to the number of sites line.
istr.getline(buf, sizeof(buf));
// This is the line before number of sites
while (strstr(buf, "copy includes header") == NULL) {
istr.getline(buf, sizeof(buf));
}
}
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "number of sites") == NULL) {
retval += "Parse error looking for 'number of sites=' header in file " + fname + "\n";
return retval;
}
int nsites;
istr >> nsites;
m_siteList.clear();
int nyears = m_endYear - m_startYear + 1;
for (int isite=0; isite<nsites; isite++) {
SiteData sd;
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "header1") == NULL) {
retval += "Parse error looking for 'header1=' header in file " + fname + "\n";
return retval;
}
istr.getline(buf, sizeof(buf));
sd.m_header1 = buf;
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "header2") == NULL) {
retval += "Parse error looking for 'header2=' header in file " + fname + "\n";
return retval;
}
istr.getline(buf, sizeof(buf));
sd.m_header2 = buf;
if (version >= 1.2f) {
// Read in pumping type, well or recharge
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "pumping type") == NULL) {
retval += "Parse error looking for 'pumping type=' header in file " + fname + "\n";
return retval;
}
istr.getline(buf, sizeof(buf));
string cbuf(buf);
sd.m_siteType = (cbuf == "irrigation" ? SiteData::WELL : SiteData::RECHARGE);
}
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "boundary condition") == NULL) {
retval += "Parse error looking for 'boundary condition=' header in file " + fname + "\n";
return retval;
}
int ival;
istr >> ival;
sd.m_bi = (SiteData::BoundaryConditionsEnum)ival;
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "compute depletion for segment") == NULL) {
retval += "Parse error looking for 'compute depletion for segment=' header in file " + fname + "\n";
return retval;
}
istr >> ival;
sd.m_computeDepletionForSegment = (ival == 1) ? TRUE : FALSE;
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "transmissivity (tr)") == NULL) {
retval += "Parse error looking for 'transmissivity (tr)=' header in file " + fname + "\n";
return retval;
}
istr >> sd.m_tr;
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "specific yield (s)") == NULL) {
retval += "Parse error looking for 'specific yield (s)=' header in file " + fname + "\n";
return retval;
}
istr >> sd.m_s;
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "distance from well to stream (dxx)") == NULL) {
retval += "Parse error looking for 'distance from well to stream (dxx)=' header in file " + fname + "\n";
return retval;
}
istr >> sd.m_dxx;
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "time units") == NULL) {
retval += "Parse error looking for 'time units=' header in file " + fname + "\n";
return retval;
}
istr >> ival;
if (m_displayMode == DISPLAY_MODIFIED) {
if (isite == 0) SetTimeUnits((SiteData::TimeUnitsEnum)ival);
sd.SetTimeUnits((SiteData::TimeUnitsEnum)ival);
sd.SetPeriod(m_startYear, m_endYear);
}
else {
sd.m_timeUnits = (SiteData::TimeUnitsEnum)ival;
}
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "number of time units between printouts (tbp)") == NULL) {
retval += "Parse error looking for 'number of time units between printouts (tbp)=' header in file " + fname + "\n";
return retval;
}
istr >> sd.m_tbp;
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "number of cycles (seasons) to be simulated (nc)") == NULL) {
retval += "Parse error looking for 'number of cycles (seasons) to be simulated (nc)=' header in file " + fname + "\n";
return retval;
}
istr >> sd.m_nc;
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "effective sdf") == NULL) {
retval += "Parse error looking for 'effective sdf=' header in file " + fname + "\n";
return retval;
}
istr >> sd.m_sdf;
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "distance from the stream to the parallel impermeable boundary (w)") == NULL) {
retval += "Parse error looking for 'distance from the stream to the parallel impermeable boundary (w)=' header in file " + fname + "\n";
return retval;
}
istr >> sd.m_w;
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "distance between well and no flow boundary (b)") == NULL) {
retval += "Parse error looking for 'distance between well and no flow boundary (b)=' header in file " + fname + "\n";
return retval;
}
istr >> sd.m_b;
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "calculate depletion for segment of stream (zzseg)") == NULL) {
retval += "Parse error looking for 'calculate depletion for segment of stream (zzseg)=' header in file " + fname + "\n";
return retval;
}
istr >> sd.m_zzseg;
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "length of stream (z1, z2)") == NULL) {
retval += "Parse error looking for 'length of stream (z1, z2)=' header in file " + fname + "\n";
return retval;
}
istr >> sd.m_z1 >> sd.m_z2;
bool showInOutput = true;
if (version >= 2.2f && version < 2.4f) {
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "show in output") == NULL) {
retval += "Parse error looking for 'show in output=' header in file " + fname + "\n";
return retval;
}
string sval;
istr >> sval;
showInOutput = (sval == "yes");
}
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "number of pumping records") == NULL) {
retval += "Parse error looking for 'number of pumping records=' header in file " + fname + "\n";
return retval;
}
int nrecs;
istr >> nrecs;
if (m_displayMode == DISPLAY_ORIGINAL) {
sd.m_pumpingRecTD.SetSpan(nrecs);
sd.m_metaDataTD.SetSpan(nrecs);
if (sd.m_siteType == SiteData::WELL) {
sd.m_wellPumpingTD.SetSpan(nrecs);
}
else {
sd.m_rechargeTD.SetSpan(nrecs);
}
}
for (int irec=0; irec<nrecs; irec++) {
istr.getline(buf, sizeof(buf), '=');
if (strstr(buf, "delt,q,meta") == NULL) {
retval += "Parse error looking for 'delt,q=' header in file " + fname + "\n";
return retval;
}
double q;
int delt;
istr >> delt >> q;
if (version < 2.6f && m_timeUnits == SiteData::MONTHS) {
// Convert from actual days per month to average days per month.
int m = irec % 12;
int y = irec / 12;
double ndays = getDaysInMonth(m, m_startYear + y);
q = q * ndays / 30.41667;
}
sd.m_pumpingRecTD.SetValue(PumpingRecTD::Delta, irec, delt);
sd.m_pumpingRecTD.SetValue(PumpingRecTD::Q, irec, q);
sd.m_tt += delt;
int meta;
istr >> meta;
sd.m_metaDataTD.SetValue(MetaDataTD::MetaData, irec, meta);
}
sd.m_tt *= sd.m_nc;
try {
if (sd.m_siteType == SiteData::WELL) {
istr >> sd.m_wellPumpingTD;
}
else {
istr >> sd.m_rechargeTD;
}
}
catch (InputError err) {
AfxMessageBox((err.ErrorMsg() + " in file " + fname).c_str());
return err.ErrorMsg() + " in file " + fname;
}
if (version >= 2.3f) {
// Read show in output values.
try {
nrecs = ParseInput<int>(istr, "Number of output values");
sd.m_outputCols.resize(nrecs);
for (int i=0; i<nrecs; i++) {
string active = ParseInputString(istr, "Output value");
sd.m_outputCols[i] = (active == "yes");
}
} catch (ParseInputException *pie) {
AfxMessageBox((pie->ErrorMsg() + " in file " + fname).c_str());
delete pie;
}
}
else {
// Older version use the first value.
sd.m_outputCols.resize(1);
sd.m_outputCols[0] = showInOutput;
}
// Change nodata to zero.
sd.m_pumpingRecTD.Fill(PumpingRecTD::Q, 0, 0, -1, true);
m_siteList.push_back(sd);
}
if (version >= 2.3f) {
if (!m_custOutput.Read(istr)) {
AfxMessageBox("Failed to read custom output.");
}
}
// Check for output.
istr.close();
// Fix customized output masks.
UpdateCustOutput();
ReadURF_Data(fname, version);
if (!ignoreOutput) ReadOutput(fname);
return "";
}
string
SitesManager::ImportProject(string fname, ItemData::YearTypeEnum yearType, bool addInPlace)
{
ifstream istr(fname.c_str());
if (!istr.is_open()) {
return "Unable to open project file " + fname + "\n";
}
// Save basename so we know where to write results to.
int loc = fname.rfind('.');
m_baseName = fname.substr(0, loc);
m_yearMode = (YearModeEnum)yearType;
istr.getline(buf, sizeof(buf));
istringstream istrstr(buf);
// Check for token count to make sure the input is legal.
vector<string> tokens;
stringtok(tokens, buf, "\t");
bool valid = false;
if (tokens.size() >= 4)
{
// make sure first three tokens are numbers.
for (int i=0; i<3; i++)
{
if (!isNumber(tokens[i]))
{
break;
}
}
valid = true;
}
if (!valid)
{
return "This is not a valid import file. It must start with the project header (start year, end year, historical end year)";
}
int histEndYear;
string timeType;
int imp_startYear=-1, imp_endYear=-1, imp_histEndYear=-1;
istrstr >> imp_startYear >> imp_endYear >> imp_histEndYear >> timeType;
bool import = false;
if (m_endYear < 1)
{
// new project, building from scratch
m_startYear = imp_startYear;
m_endYear = imp_endYear;
histEndYear = imp_histEndYear;
SetTimeUnits((timeType == "monthly") ? SiteData::MONTHS : SiteData::DAYS);
// Allow for projected data from CUModel.
TRACE0("Warning: projected input not handled.\n");
// If the year is not calendar, then add an extra year at the beginning.
int offsetYear(0);
if (yearType != ItemData::CALENDAR) {
offsetYear = 1;
}
m_histStartYear = m_startYear - offsetYear;
m_histEndYear = m_endYear;
SetPeriod(m_startYear - offsetYear, m_endYear);
}
else
{
import = true;
}
int nyears = m_endYear - m_startYear + 1;
int imp_nyears = imp_endYear - imp_startYear + 1;
m_displayMode = DISPLAY_MODIFIED;
// Keep a list of sites updated.
vector<int> sitesUpdated;
vector<string> sitesNotFound;
int icnt = -1;
while (istr.good()) {
// Second record is well info
istr.getline(buf, sizeof(buf));
if (trim(buf).empty()) continue;
//if (!istr.good()) break;
istringstream istrstr(buf);
char buf1[256];
istrstr.getline(buf1, sizeof(buf1), '\t');
string newSiteHeader = trim(buf1);
icnt++;
// Search for a site with the name and update its values; otherwise add
// a new site.
int idx = -1;
if (!addInPlace)
{
idx = FindSite(newSiteHeader);
}
else
{
if (icnt < m_siteList.size())
{
idx = icnt;
}
}
bool found = idx >= 0;
if (!found) {
// Not found; add new.
idx = AddSite(newSiteHeader, "imported from IDSCU");
sitesNotFound.push_back(newSiteHeader);
}
else
{
// check that sites weren't updated multiple times.
if (find(sitesUpdated, idx) != sitesUpdated.end())
{
AfxMessageBox(("Warning: site " + m_siteList[idx].m_header1 + " updated more than once by " + newSiteHeader).c_str());
}
sitesUpdated.push_back(idx);
}
SiteData &sd = m_siteList[idx];
sd.SetTimeUnits(m_timeUnits);
istrstr >> sd.m_sdf;
enum SiteData::SiteTypeEnum st = SiteData::WELL;
if (istrstr.good()) {
string cbuf;
istrstr >> cbuf;
if (cbuf.empty() || cbuf[0] == '0') {
st = SiteData::WELL;
}
else {
st = SiteData::RECHARGE;
}
}
sd.SetSiteType(st);
// Read in other params. Only update if they are > 0
float params[5];
for (int i=0; i<5; params[i++] = 0);
if (istrstr.good()) istrstr >> params[0];
if (istrstr.good()) istrstr >> params[1];
if (istrstr.good()) istrstr >> params[2];
if (istrstr.good()) istrstr >> params[3];
if (istrstr.good()) istrstr >> params[4];
if (params[0] > 0) sd.m_w = params[0];
if (params[1] > 0) sd.m_b = params[1];
if (params[2] > 0) sd.m_tr = params[2];
if (params[3] > 0) sd.m_s = params[3];
if (params[4] > 0) sd.m_dxx = params[4];
//if (istrstr.good()) istrstr >> sd.m_w; // distance from the stream to the parallel impermeable boundary
//if (istrstr.good()) istrstr >> sd.m_b; // distance between well and no flow boundary
//if (istrstr.good()) istrstr >> sd.m_tr; // transmissivity
//if (istrstr.good()) istrstr >> sd.m_s; // specific yield
//if (istrstr.good()) istrstr >> sd.m_dxx; // distance from well to stream
if (!found) {
// Try to guess mode.
if (sd.m_w > 0) sd.m_bi = SiteData::ALLUVIAL_AQUIFER;
else if (sd.m_dxx > 0) sd.m_bi = SiteData::INFINITE_AQUIFER;
else if (sd.m_b > 0) sd.m_bi = SiteData::NO_FLOW;
else sd.m_bi = SiteData::EFFECTIVE_SDF;
}
sd.m_pumpingRecTD.SetNYears(nyears, m_startYear);
sd.m_metaDataTD.SetNYears(nyears, m_startYear);
// Read pumping
for (int iyear=0; iyear<imp_nyears; iyear++) {
if (!istr.good()) break;
istr.getline(buf, sizeof(buf));
istringstream istrstr(buf);
int yearToken;
istrstr >> yearToken;
if (yearToken != imp_startYear + iyear) {
AfxMessageBox(("Read pump year " + to_string<int>(yearToken)
+ " for well " + sd.m_header1
+ "; should be year "
+ to_string<int>(imp_startYear + iyear)).c_str());
return FALSE;
}
int cnt(0);
while (istrstr.good()) {
// Check for junk at the end.
string sval;
istrstr >> sval;
if (sval.empty()) break;
double q;
q = atof(sval.c_str());
//SetSiteData(q, -1, idx, 0,
double gpmval = q * 325851.0/1440;
//double gpmval = q * 325900.0/1440;
if (m_timeUnits == SiteData::MONTHS) {
// convert daily -> monthly
//int days = getDaysInMonth(cnt%12, m_startYear + iyear, yearType);
// New try: use average days in month. This will cause the modified form to conform to the model.
double days = 30.41667;
gpmval /= days; // convert monthly -> daily
}
int jm, jd;
if (m_timeUnits == SiteData::DAYS) {
jday2date(cnt, yearToken, &jd, &jm);
// Change date to start from 0.
jm--;
jd--;
}
else {
jm = cnt;
jd = -1;
}
sd.m_pumpingRecTD.SetValueDate(yearType, PumpingRecTD::Delta, 1, yearToken, jm, jd);
sd.m_pumpingRecTD.SetValueDate(yearType, PumpingRecTD::Q, gpmval, yearToken, jm, jd);
sd.m_metaDataTD.SetValueDate(yearType, MetaDataTD::MetaData, gpmval, yearToken, jm, jd);
cnt++;
}
}
}
if (sitesNotFound.size() > 0 && import)
{
string out_str;
for (int i=0; i<sitesNotFound.size(); i++)
{
out_str += sitesNotFound[i] + "\r\n";
}
AfxMessageBox(("Sites in import list that were not matched in original project: \r\n" + out_str).c_str());
}
return "";
}
string
SitesManager::ImportProjectNew(string fname, bool addInPlace)
{
ifstream istr(fname.c_str());
if (!istr.is_open()) {
return "Unable to open project file " + fname + "\n";
}
// Check for compact format.
YearModeEnum oriYearMode = m_yearMode;
bool compactFormat = false;
istr.getline(buf, sizeof(buf));
// Test for three numbers in a row. If not found, then the format must be compact.
vector<string> toks;
stringtok(toks, buf, "\t");
if (!(toks.size() >= 3 && toks[0].size() == 4 && toks[1].size() == 4 && toks[2].size() == 4 && atoi(toks[0].c_str()) > 1900 && atoi(toks[1].c_str()) > 1900 && atoi(toks[2].c_str()) > 1900)) {
if (GetStartYear() == 0) {
// Flag error that compact format can only be used with an existing dataset.
return "Error: this dataset can only be imported into an existing project.";
}
compactFormat = true;
// Rewind back to beginning.
istr.seekg(0);
// Ask the user for year type.
TextImportDlg dlg;
if (dlg.DoModal() == IDOK) {
m_yearMode = (YearModeEnum)dlg.m_yearType;
}
}
// Save basename so we know where to write results to.
int loc = fname.rfind('.');
m_baseName = fname.substr(0, loc);
// This beginning is the reverse of CSDFDailyDoc::WriteSpreadsheetHeader
istringstream istrstr(buf);
int imp_nyears = 1; // compact format reads one year at a time.
if (!compactFormat) {
int histEndYear;
string timeUnits;
string sYearMode;
istrstr >> m_startYear >> m_endYear >> m_histStartYear >> m_histEndYear >> timeUnits >> sYearMode;
sYearMode = sToLower(sYearMode);
if (sYearMode == "calendar") {
m_yearMode = YEAR_CALENDAR;
}
else if (sYearMode == "irrigation") {
m_yearMode = YEAR_IRRIGATION;
// reduce the start years by one because the native format is calendar, which forces an extra year at the beginning to represent all the data.
m_startYear--;
m_histStartYear--;
}
else if (sYearMode == "usgs") {
m_yearMode = YEAR_USGS;
// reduce the start years by one because the native format is calendar, which forces an extra year at the beginning to represent all the data.
m_startYear--;
m_histStartYear--;
}
imp_nyears = m_endYear - m_startYear + 1;
if (m_yearMode != YEAR_CALENDAR) { // Subtract a year because non-calendar year types start a year early.
imp_nyears--;
}
m_displayMode = DISPLAY_MODIFIED;
SetTimeUnits((sToLower(timeUnits) == "monthly") ? SiteData::MONTHS : SiteData::DAYS);
// Read customized output options
m_custOutput.SetSize(0);
char fbuf[20];
istrstr >> ws;
istrstr.getline(fbuf, sizeof(fbuf), '\t');
while (string(fbuf).length() > 0) // "Format is..."
{
if (!trim(fbuf).empty() && trim(fbuf).find("Format") == string::npos)
{
m_custOutput.AddColumn();
m_custOutput.SetActive(m_custOutput.GetSize()-1, true);
m_custOutput.SetLabel(m_custOutput.GetSize()-1, fbuf);
}
fbuf[0] = '\0';
istrstr.getline(fbuf, sizeof(fbuf), '\t');