-
Notifications
You must be signed in to change notification settings - Fork 0
/
LSDParameterParser.cpp
1056 lines (904 loc) · 35.7 KB
/
LSDParameterParser.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
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// LSDParameterParser.cpp
//
// Land Surface Dynamics Parameter Parser Object
//
// An object for keeping track of parameters
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// An object within the University
// of Edinburgh Land Surface Dynamics group topographic toolbox
// for calculating concntration of environmental tracers, CRNs, TCN, fallout
// nuclides
//
// Developed by:
// Simon M. Mudd
// Martin D. Hurst
// David T. Milodowski
// Stuart W.D. Grieve
// Declan A. Valters
// Fiona Clubb
//
// Copyright (C) 2016 Simon M. Mudd 2016
//
// Developer can be contacted by simon.m.mudd _at_ ed.ac.uk
//
// Simon Mudd
// University of Edinburgh
// School of GeoSciences
// Drummond Street
// Edinburgh, EH8 9XP
// Scotland
// United Kingdom
//
// This program is free software;
// you can redistribute it and/or modify it under the terms of the
// GNU General Public License as published by the Free Software Foundation;
// either version 2 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 General Public License for more details.
//
// You should have received a copy of the
// GNU General Public License along with this program;
// if not, write to:
// Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor,
// Boston, MA 02110-1301
// USA
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#include <fstream>
#include <math.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <map>
#include "TNT/tnt.h"
#include "LSDStatsTools.hpp"
#include "LSDParameterParser.hpp"
using namespace std;
using namespace TNT;
#ifndef LSDParameterParser_CPP
#define LSDParameterParser_CPP
// This basic function is not part of the parameter parser object
// but instead is the function called at the beginning of any
// LSDTT driver call
vector<string> DriverIngestor(int nNumberofArgs,char *argv[])
{
cout << "=========================================================" << endl;
cout << "|| You have called an LSDTopoTools program. ||" << endl;
cout << "|| Prepare to explore topographic data! ||" << endl;
cout << "|| You can read the documentation at: ||" << endl;
cout << "=========================================================" << endl;
string path_name = ".";
string file_name = "LSDTT_parameters.driver";
//Test for correct input arguments
if (nNumberofArgs == 1)
{
cout << "You have not given me any arguments. I am going to look" << endl;
cout << "in this directory for a file with the extension .driver" << endl;
cout << "I'll use the first one I find (in alphabetical ordering)." << endl;
cout << "If I don't find one I am going to exit." << endl;
}
if (nNumberofArgs == 2)
{
cout << "I have one argument. I don't know if this is a directory path" << endl;
cout << "or a driver filename. I am going to assume it is a directory path" << endl;
cout << "if it containes the character . or /" << endl;
string temp_arg = argv[1];
string s_dot = ".";
string sl_dot = "/";
bool this_is_a_path = false;
bool path_find = false;
if (temp_arg.find(s_dot) != std::string::npos)
{
path_find = true;
}
if (temp_arg.find(sl_dot) != std::string::npos)
{
path_find = true;
}
if (this_is_a_path)
{
path_name = temp_arg;
}
else
{
file_name = temp_arg;
}
}
//Test for correct input arguments
if (nNumberofArgs==3)
{
cout << "I am reading the two arguments you gave me as the path name and the file name." << endl;
path_name = argv[1];
file_name = argv[2];
}
if (nNumberofArgs>=3)
{
cout << "You have provided more than two arguments. " << endl;
cout << "I only expect 2. I am going to assume you meant" << endl;
cout << "to give me the first two." << endl;
path_name = argv[1];
file_name = argv[2];
}
vector<string> path_and_file;
path_and_file.push_back(path_name);
path_and_file.push_back(file_name);
return path_and_file;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Create functions
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDParameterParser::create()
{
cout << "I have created an empty parameter parser object. " << endl;
cout << "Surely you want to give it a filename?" << endl;
}
// This creates using a path and a filename
void LSDParameterParser::create(string PathName, string FileName)
{
// Make sure the path has an extension
PathName = FixPath(PathName);
string FullName = PathName+FileName;
param_file_path = PathName;
param_fname = FileName;
ifstream file_info_in;
file_info_in.open(FullName.c_str());
// check if the parameter file exists
if( file_info_in.fail() )
{
cout << "\nFATAL ERROR: The parameter file \"" << FullName
<< "\" doesn't exist" << endl;
exit(EXIT_FAILURE);
}
// now ingest the parameters
cout << "Parsing the file" << endl;
LSDPP_parse_file_into_parameter_map(FullName);
parse_file_IO();
// make sure the files are okay
check_boundary_conditions();
check_file_extensions_and_paths();
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Forces the raster extensions to be bil
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDParameterParser::force_bil_extension()
{
cout << "===============================" << endl;
cout << "WARNING!!! This program requires georeferencing so only bil format" << endl;
cout << "Topographic data will be allowed!!" << endl;
cout << "===============================" << endl;
dem_read_extension = "bil";
dem_write_extension = "bil";
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Gets a line of the parameter file. Has a long buffer so you can add long path
// names.
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDParameterParser::LSDPP_parse_line(ifstream &infile, string ¶meter, string &value)
{
char c;
char buff[1024];
int pos = 0;
int word = 0;
while ( infile.get(c) )
{
if (pos >= 1024)
{
cout << "Buffer overrun, word too long in parameter line: " << endl;
string line;
getline(infile, line);
cout << "\t" << buff << " ! \n" << line << endl;
exit(1);
}
// preceeding whitespace
if (c == '#')
{
if (word == 0)
{
parameter = "NULL";
value = "NULL";
}
if (word == 1)
value = "NULL";
word = 2;
}
if ((c == ' ' || c == '\t') && pos == 0)
continue;
else if ( (c == ':' && word == 0) || ( (c == ' ' || c == '\n' || c == '\t') && word == 1))
{
while (buff[pos-1] == ' ' || buff[pos-1] == '\t')
--pos; // Trailing whitespace
buff[pos] = '\0'; // Append Null char
if (word == 0)
parameter = buff; // Assign buffer contents
else if (word == 1)
value = buff;
++word;
pos = 0; // Rewind buffer
}
else if ( c == '\n' && word == 0 )
{
parameter = "NULL";
buff[pos] = '\0';
value = buff;
++word;
}
else if (word < 2)
{
buff[pos] = c;
++pos;
}
if (c == '\n')
break;
}
if (word == 0)
{
parameter = "NULL";
value = "NULL";
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// This reads the parameter file, placing all parameters into a map
// with string values and string key. As you give the parameter parser
// default maps, it will scan these sting and convert them into the correct data type
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDParameterParser::LSDPP_parse_file_into_parameter_map(string FullName)
{
ifstream infile;
infile.open(FullName.c_str());
string parameter, value, lower, lower_val;
string bc;
cout << "Hello, I am going to parse your LSDTopoTools parameter file for you. " << endl;
cout << "The parameter filename is: " << FullName << endl;
// this will hold all the parameter values.
map<string,string> temp_parameters;
// now ingest parameters
while (infile.good())
{
LSDPP_parse_line(infile, parameter, value);
lower = parameter;
//if (parameter == "NULL")
// continue;
//for (unsigned int i=0; i<parameter.length(); ++i)
//{
// lower[i] = tolower(parameter[i]);
//}
cout << "parameter is: " << lower << " and value is: " << value << endl;
// get rid of control characters
value = RemoveControlCharactersFromEndOfString(value);
temp_parameters[lower] = value;
}
parameter_map = temp_parameters;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This uses the parameter map to get file input and output
void LSDParameterParser::parse_file_IO()
{
cout << endl << endl << endl << "----------------------" << endl;
cout << "Parsing the file I/O" << endl;
bool found_cheads = false;
if(parameter_map.find("dem read extension") != parameter_map.end())
{
dem_read_extension = parameter_map["dem read extension"];
// get rid of any control characters from the end (if param file was made in DOS)
dem_read_extension = RemoveControlCharactersFromEndOfString(dem_read_extension);
}
if(parameter_map.find("dem write extension") != parameter_map.end())
{
dem_write_extension = parameter_map["dem write extension"];
// get rid of any control characters from the end (if param file was made in DOS)
dem_write_extension = RemoveControlCharactersFromEndOfString(dem_write_extension);
}
if(parameter_map.find("write path") != parameter_map.end())
{
write_path = parameter_map["write path"];
// get rid of any control characters from the end (if param file was made in DOS)
write_path = RemoveControlCharactersFromEndOfString(write_path);
}
if(parameter_map.find("write fname") != parameter_map.end())
{
write_fname = parameter_map["write fname"];
// get rid of any control characters from the end (if param file was made in DOS)
write_fname = RemoveControlCharactersFromEndOfString(write_fname);
//cout << "Got the write name, it is: " << write_fname << endl;
}
if(parameter_map.find("read path") != parameter_map.end())
{
read_path = parameter_map["read path"];
// get rid of any control characters from the end (if param file was made in DOS)
read_path = RemoveControlCharactersFromEndOfString(read_path);
//cout << "Got the write name, it is: " << write_fname << endl;
}
else
{
cout << "I did not find a read path so I am assuming the file is in this current directory." << endl;
read_path = "./";
}
if(parameter_map.find("read fname") != parameter_map.end())
{
read_fname = parameter_map["read fname"];
// get rid of any control characters from the end (if param file was made in DOS)
read_fname = RemoveControlCharactersFromEndOfString(read_fname);
//cout << "Got the write name, it is: " << write_fname << endl;
}
if(parameter_map.find("CHeads_file") != parameter_map.end())
{
cout << endl << endl << endl;
cout << "===========================" << endl;
cout << "I have a channel heads file using the parameter string CHeads_file." << endl;
cout << "If you have another parameter channel heads fname it will overwrite this parameter." << endl;
cout << "This option is used for backwards compatibility but the channel heads fname is preferred." << endl;
cout << "===========================" << endl;
cout << endl << endl << endl;
CHeads_file = parameter_map["CHeads_file"];
// get rid of any control characters from the end (if param file was made in DOS)
CHeads_file = RemoveControlCharactersFromEndOfString(CHeads_file);
cout << "Got the channel heads name, it is: " << CHeads_file << endl;
if(found_cheads)
{
cout << "Warning, channel head file is being overwritten--you have it twice in parameter file." << endl;
}
found_cheads = true;
}
else
{
cout << "I didn't find CHeads_file in the parameter map" << endl;
}
if(parameter_map.find("channel heads fname") != parameter_map.end())
{
cout << "I found a channel heads fname which is" << parameter_map["channel heads fname"] << endl;
CHeads_file = parameter_map["channel heads fname"];
// get rid of any control characters from the end (if param file was made in DOS)
CHeads_file = RemoveControlCharactersFromEndOfString(CHeads_file);
//cout << "Got the channel heads name, it is: " << CHeads_file << endl;
if(found_cheads)
{
cout << "Warning, channel head file is being overwritten--you have it twice in parameter file." << endl;
}
found_cheads = true;
}
else
{
if(not found_cheads)
{
CHeads_file = "NULL";
}
else
{
cout << "I didn't find a channel heads fname but I have a previous channel heads from the CHeads_file" << endl;
cout << "I will use that. It is: " << CHeads_file << endl;
}
}
if(parameter_map.find("baselevel junctions fname") != parameter_map.end())
{
BaselevelJunctions_file = parameter_map["baselevel junctions fname"];
// get rid of any control characters from the end (if param file was made in DOS)
BaselevelJunctions_file = RemoveControlCharactersFromEndOfString(BaselevelJunctions_file);
}
else
{
BaselevelJunctions_file = "NULL";
}
if(parameter_map.find("channel segments fname") != parameter_map.end())
{
ChannelSegments_file = parameter_map["channel segments fname"];
// get rid of any control characters from the end (if param file was made in DOS)
ChannelSegments_file = RemoveControlCharactersFromEndOfString(ChannelSegments_file);
}
else
{
ChannelSegments_file = "NULL";
}
if(parameter_map.find("floodplain fname") != parameter_map.end())
{
Floodplain_file = parameter_map["floodplain fname"];
// get rid of any control characters from the end (if param file was made in DOS)
Floodplain_file = RemoveControlCharactersFromEndOfString(Floodplain_file);
}
else
{
Floodplain_file = "NULL";
}
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This forces parsing of everything in the file
// Used for copying parameter files
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDParameterParser::force_parse()
{
cout << "Forcing parsing of the parameter file" << endl;
for( map<string, string >::iterator it = parameter_map.begin(); it != parameter_map.end(); ++it)
{
string key = it->first;
cout << "Key is: " <<it->first << "\n";
if(key != "CHeads file" && key != "read fname" && key != "write fname" &&
key != "read path" && key != "write path" && key != "read extension" &&
key != "write extension" && key != "NULL")
{
parameters_read_map[it->first] = it->second;
}
}
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This parses all the default parameter maps.
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDParameterParser::parse_all_parameters(map<string,float> default_map_f,
map<string,int> default_map_i, map<string,bool> default_map_b,
map<string,string> default_map_s)
{
parse_float_parameters(default_map_f);
parse_int_parameters(default_map_i);
parse_bool_parameters(default_map_b);
parse_string_parameters(default_map_s);
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This parses all the default parameter maps.
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDParameterParser::parse_all_parameters(map<string,float> default_map_f,
map<string,int> default_map_i, map<string,bool> default_map_b,
map<string,string> default_map_s,
map<string,double> default_map_d)
{
parse_float_parameters(default_map_f);
parse_int_parameters(default_map_i);
parse_bool_parameters(default_map_b);
parse_string_parameters(default_map_s);
parse_double_parameters(default_map_d);
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// These two functions takes a map of defualt parameters and returns the parameters for the
// current implementation
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDParameterParser::parse_float_parameters(map<string,float> default_map)
{
// the idea is to look through the default map, getting the keys, and then
// looking for the keys in the parameter maps
vector<string> these_keys = extract_keys(default_map);
// loop through the keys
int n_keys = int(these_keys.size());
for(int i = 0; i<n_keys; i++)
{
cout << "Key is: " << these_keys[i] << endl;
// If the key is contained in the parsed parameters, use the parsed parameter
if(parameter_map.find(these_keys[i]) != parameter_map.end())
{
cout << "Found key " << these_keys[i];
// convert the value to float
float_parameters[these_keys[i]] = atof(parameter_map[these_keys[i]].c_str());
parameters_read_map[these_keys[i]] = parameter_map[these_keys[i]];
cout << " it is: " << parameter_map[these_keys[i]] << " check: " << float_parameters[these_keys[i]] << endl;
}
else // the key is not in the parsed parameters. Use the default.
{
float_parameters[these_keys[i]] = default_map[these_keys[i]];
defaults_used_map[these_keys[i]] = dtoa(default_map[these_keys[i]]);
}
}
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Parse double parameters (for coords) FJC 20/11/17
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDParameterParser::parse_double_parameters(map<string,double> default_map)
{
// the idea is to look through the default map, getting the keys, and then
// looking for the keys in the parameter maps
vector<string> these_keys = extract_keys(default_map);
// loop through the keys
int n_keys = int(these_keys.size());
for(int i = 0; i<n_keys; i++)
{
cout << "Key is: " << these_keys[i] << endl;
// If the key is contained in the parsed parameters, use the parsed parameter
if(parameter_map.find(these_keys[i]) != parameter_map.end())
{
cout << "Found key " << these_keys[i];
// convert the value to float
double_parameters[these_keys[i]] = atof(parameter_map[these_keys[i]].c_str());
parameters_read_map[these_keys[i]] = parameter_map[these_keys[i]];
cout << " it is: " << parameter_map[these_keys[i]] << " check: " << double_parameters[these_keys[i]] << endl;
}
else // the key is not in the parsed parameters. Use the default.
{
double_parameters[these_keys[i]] = default_map[these_keys[i]];
defaults_used_map[these_keys[i]] = dtoa(default_map[these_keys[i]]);
}
}
}
void LSDParameterParser::parse_int_parameters(map<string,int> default_map)
{
// the idea is to look through the default map, getting the keys, and then
// looking for the keys in the parameter maps
vector<string> these_keys = extract_keys(default_map);
// loop through the keys
int n_keys = int(these_keys.size());
for(int i = 0; i<n_keys; i++)
{
cout << "Key is: " << these_keys[i] << endl;
// If the key is contained in the parsed parameters, use the parsed parameter
if(parameter_map.find(these_keys[i]) != parameter_map.end())
{
//cout << "Found the key" << endl;
// convert the value to float
int_parameters[these_keys[i]] = atoi(parameter_map[these_keys[i]].c_str());
//cout << " it is: " << parameter_map[these_keys[i]] << " check: " << int_parameters[these_keys[i]] << endl;
parameters_read_map[these_keys[i]] = parameter_map[these_keys[i]];
}
else // the key is not in the parsed parameters. Use the default.
{
int_parameters[these_keys[i]] = default_map[these_keys[i]];
defaults_used_map[these_keys[i]] = itoa(default_map[these_keys[i]]);
}
}
}
void LSDParameterParser::parse_bool_parameters(map<string,bool> default_map)
{
// the idea is to look through the default map, getting the keys, and then
// looking for the keys in the parameter maps
vector<string> these_keys = extract_keys(default_map);
// loop through the keys
int n_keys = int(these_keys.size());
for(int i = 0; i<n_keys; i++)
{
cout << "Key is: " << these_keys[i] << endl;
// If the key is contained in the parsed parameters, use the parsed parameter
if(parameter_map.find(these_keys[i]) != parameter_map.end())
{
// convert the value to bool
string value = parameter_map[these_keys[i]];
bool temp_bool = (value == "true" || value== "True" || value == "TRUE" || value== "T" || value== "t") ? true : false;
bool_parameters[these_keys[i]] = temp_bool;
parameters_read_map[these_keys[i]] = parameter_map[these_keys[i]];
}
else // the key is not in the parsed parameters. Use the default.
{
bool_parameters[these_keys[i]] = default_map[these_keys[i]];
if (default_map[these_keys[i]] == true)
{
defaults_used_map[these_keys[i]] = "true";
}
else
{
defaults_used_map[these_keys[i]] = "false" ;
}
}
}
}
void LSDParameterParser::parse_string_parameters(map<string,string> default_map)
{
// the idea is to look through the default map, getting the keys, and then
// looking for the keys in the parameter maps
vector<string> these_keys = extract_keys(default_map);
// loop through the keys
int n_keys = int(these_keys.size());
for(int i = 0; i<n_keys; i++)
{
cout << "Key is: " << these_keys[i] << endl;
// If the key is contained in the parsed parameters, use the parsed parameter
if(parameter_map.find(these_keys[i]) != parameter_map.end())
{
string_parameters[these_keys[i]] = parameter_map[these_keys[i]];
parameters_read_map[these_keys[i]] = parameter_map[these_keys[i]];
}
else // the key is not in the parsed parameters. Use the default.
{
string_parameters[these_keys[i]] = default_map[these_keys[i]];
defaults_used_map[these_keys[i]] = default_map[these_keys[i]];
}
}
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This parses a vector of strings
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
vector<string> LSDParameterParser::parse_string_vector(string key)
{
string this_string = string_parameters[key];
// reset the string vec
vector<string> this_string_vec;
// create a stringstream
stringstream ss(this_string);
// import the data, using a comma to separate
while( ss.good() )
{
string substr;
getline( ss, substr, ',' );
// remove the spaces
substr.erase(remove_if(substr.begin(), substr.end(), ::isspace), substr.end());
// remove control characters
substr.erase(remove_if(substr.begin(), substr.end(), ::iscntrl), substr.end());
// add the string to the string vec
this_string_vec.push_back( substr );
}
return this_string_vec;
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This parses a vector of ints
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
vector<int> LSDParameterParser::parse_int_vector(string key)
{
string this_string = string_parameters[key];
// reset the string vec
vector<int> this_int_vec;
// create a stringstream
stringstream ss(this_string);
// import the data, using a comma to separate
while( ss.good() )
{
string substr;
getline( ss, substr, ',' );
// remove the spaces
substr.erase(remove_if(substr.begin(), substr.end(), ::isspace), substr.end());
// remove control characters
substr.erase(remove_if(substr.begin(), substr.end(), ::iscntrl), substr.end());
// add the string to the string vec
this_int_vec.push_back( atoi(substr.c_str()) );
}
return this_int_vec;
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This parses a vector of ints
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
vector<float> LSDParameterParser::parse_float_vector(string key)
{
cout << "I am going to parse a float vector for you!" << endl;
string this_string = string_parameters[key];
// reset the string vec
vector<float> this_float_vec;
// create a stringstream
stringstream ss(this_string);
// import the data, using a comma to separate
while( ss.good() )
{
string substr;
getline( ss, substr, ',' );
// remove the spaces
substr.erase(remove_if(substr.begin(), substr.end(), ::isspace), substr.end());
// remove control characters
substr.erase(remove_if(substr.begin(), substr.end(), ::iscntrl), substr.end());
// add the string to the string vec
this_float_vec.push_back( atof(substr.c_str()) );
}
return this_float_vec;
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This checks boundary conditions for flow info
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDParameterParser::check_boundary_conditions()
{
if( int(boundary_conditions.size()) != 4)
{
cout << "Boundary conditions not assigned! Defaulting to no flux." << endl;
vector<string> temp_bc(4);
for (int i = 0; i< 4; i++)
{
temp_bc[i] = "n";
}
boundary_conditions = temp_bc;
}
for (int i =0; i< 4; i++)
{
cout << "Boundary["<<i<<"]: "<<boundary_conditions[i]<< endl;
}
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This function checks the file extensions for reading and writing DEMs
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDParameterParser::check_file_extensions_and_paths()
{
// first check the extensions
if (dem_read_extension != "asc" && dem_read_extension != "flt" && dem_read_extension != "bil" &&
dem_write_extension != "asc" && dem_write_extension != "flt" && dem_write_extension != "bil")
{
cout << "LSDParameterParser Raster file extension not assigned! Defaulting to bil format." << endl;
cout << "You entered: " << dem_read_extension << "!" <<endl;
dem_read_extension = "bil";
dem_write_extension = "bil";
}
else
{
if (dem_read_extension != "asc" && dem_read_extension != "flt" && dem_read_extension != "bil")
{
cout << "DEM read extension not assigned, defaulting to write extension." << endl;
dem_read_extension = dem_write_extension;
}
else
{
//cout << "DEM write extension not assigned, defaulting to read extension." << endl;
dem_write_extension = dem_read_extension;
}
}
// now check the paths
//cout << "Write path length is: " << write_path.length() << endl;
if (write_path.length() == 0)
{
write_path = param_file_path;
if (read_path.length() != 0)
{
write_path = read_path;
}
}
//cout << "CHECKING NAMES, Write fname is: " << write_fname << endl;
//cout << "The write fname length is " << write_fname.length() << endl;
if (write_fname.length() == 0)
{
if (read_fname.length() != 0)
{
write_fname = read_fname;
}
write_fname = get_string_before_dot(param_fname);
//cout << "Write fname not assigned, defaulting to name of parameter file." << endl;
//cout << "The write fname is: " << write_fname << endl;
}
// now check the path
//cout << "Read path length is: " << read_path.length() << endl;
if (read_path.length() == 0)
{
read_path = write_path;
}
if (read_fname.length() == 0)
{
read_fname = get_string_before_dot(param_fname);
//cout << "Read fname not assigned, defaulting to name of parameter file." << endl;
//cout << "The read fname is: " << read_fname << endl;
}
// make sure the read and write paths have the slash at the end
write_path = FixPath(write_path);
read_path = FixPath(read_path);
cout << "The full read fname is:\n " << read_path+read_fname << endl;
cout << "The full write fname is:\n " << write_path+write_fname << endl;
cout << "The read and write extensions are: " << dem_read_extension
<< " " << dem_write_extension << endl;
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This function strips the text after the final dot in a string
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
string LSDParameterParser::get_string_before_dot(string this_string)
{
string cut_string;
unsigned found = this_string.find_last_of(".");
cut_string = this_string.substr(0,found);
return cut_string;
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This prints parameters read to file, so you can make sure your parameters
// have ingested properly
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDParameterParser::print_parameters()
{
string fname = write_path+write_fname+"_ingestedParam.param";
ofstream params_out;
params_out.open(fname.c_str());
params_out << "# Here are the paramters ingested and set by the parameter parser" << endl;
params_out << "# The file names and paths are: " << endl;
params_out << "read path: " << read_path << endl;
params_out << "read fname: " << read_fname << endl;
params_out << "write path: " << write_path << endl;
params_out << "write fname: " << write_fname << endl;
params_out << "CHeads file: " << CHeads_file << endl;
params_out << "read extension: " << dem_read_extension << endl;
params_out << "write extension: " << dem_write_extension << endl << endl;
params_out << "# ====================================" << endl;
params_out << "# Now for parameters read from file." << endl;
params_out << "# If an expected parameter is not here check your spelling." << endl;
vector<string> empty_vec;
vector<string> these_keys = extract_keys(parameters_read_map);
for(int i = 0; i< int(these_keys.size()); i++)
{
params_out << these_keys[i] << ": " << parameters_read_map[these_keys[i]] << endl;
}
params_out << endl << "# ====================================" << endl;
params_out << "# Now for the default parameters." << endl;
these_keys = empty_vec;
these_keys = extract_keys(defaults_used_map);
for(int i = 0; i< int(these_keys.size()); i++)
{
params_out << these_keys[i] << ": " << defaults_used_map[these_keys[i]] << endl;
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This prints parameters read to file, so you can make sure your parameters
// have ingested properly
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDParameterParser::print_parameters(string fname_prefix)
{
string fname = write_path+fname_prefix;
ofstream params_out;
params_out.open(fname.c_str());
params_out << "# Here are the paramters ingested and set by the parameter parser" << endl;
params_out << "# The file names and paths are: " << endl;
params_out << "read path: " << read_path << endl;
params_out << "read fname: " << read_fname << endl;
params_out << "write path: " << write_path << endl;
params_out << "write fname: " << write_fname << endl;
params_out << "CHeads file: " << CHeads_file << endl;
params_out << "read extension: " << dem_read_extension << endl;
params_out << "write extension: " << dem_write_extension << endl << endl;
params_out << "# ====================================" << endl;
params_out << "# Now for parameters read from file." << endl;
params_out << "# If an expected parameter is not here check your spelling." << endl;
vector<string> empty_vec;
vector<string> these_keys = extract_keys(parameters_read_map);
for(int i = 0; i< int(these_keys.size()); i++)
{
params_out << these_keys[i] << ": " << parameters_read_map[these_keys[i]] << endl;
}
params_out << endl << "# ====================================" << endl;
params_out << "# Now for the default parameters." << endl;
these_keys = empty_vec;
these_keys = extract_keys(defaults_used_map);
for(int i = 0; i< int(these_keys.size()); i++)
{
params_out << these_keys[i] << ": " << defaults_used_map[these_keys[i]] << endl;
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This prints parameters read to file, so you can make sure your parameters
// have ingested properly
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDParameterParser::replace_and_print_parameter_file(string parameter_fname,
string new_read_path, string new_read_fname,
string new_write_path, string new_write_fname,
map<string,string> replace_parameters)
{
string fname = write_path+parameter_fname;
ofstream params_out;