-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLSDChiTools.cpp
More file actions
11859 lines (9760 loc) · 430 KB
/
LSDChiTools.cpp
File metadata and controls
11859 lines (9760 loc) · 430 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
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// LSDChiTools
// Land Surface Dynamics ChiTools object
//
// An object within the University
// of Edinburgh Land Surface Dynamics group topographic toolbox
// for performing various analyses in chi space
//
//
// Developed by:
// Simon M. Mudd
// Martin D. Hurst
// David T. Milodowski
// Stuart W.D. Grieve
// Declan A. Valters
// Fiona Clubb
// Boris Gailleton
//
// 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
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//
// LSDChiTools.cpp
// LSDChiTools object
// LSD stands for Land Surface Dynamics
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//
// This object is written by
// Simon M. Mudd, University of Edinburgh
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//-----------------------------------------------------------------
//DOCUMENTATION URL: http://www.geos.ed.ac.uk/~s0675405/LSD_Docs/
//-----------------------------------------------------------------
#ifndef LSDChiTools_CPP
#define LSDChiTools_CPP
#include <vector>
#include <string>
#include <fstream>
#include <algorithm>
#include <queue>
#include "TNT/tnt.h"
#include "LSDFlowInfo.hpp"
#include "LSDRaster.hpp"
#include "LSDIndexRaster.hpp"
#include "LSDChannel.hpp"
#include "LSDJunctionNetwork.hpp"
#include "LSDIndexChannel.hpp"
#include "LSDStatsTools.hpp"
#include "LSDShapeTools.hpp"
#include "LSDChiTools.hpp"
#include "LSDBasin.hpp"
#include "LSDChiNetwork.hpp"
#include "LSDMostLikelyPartitionsFinder.hpp"
#ifdef _OPENMP
#include <thread>
#endif
using namespace std;
using namespace TNT;
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Creates an LSDChiTools from an LSDRaster
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDChiTools::create(LSDRaster& ThisRaster)
{
NRows = ThisRaster.get_NRows();
NCols = ThisRaster.get_NCols();
XMinimum = ThisRaster.get_XMinimum();
YMinimum = ThisRaster.get_YMinimum();
DataResolution = ThisRaster.get_DataResolution();
NoDataValue = ThisRaster.get_NoDataValue();
GeoReferencingStrings = ThisRaster.get_GeoReferencingStrings();
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Creates an LSDChiTools from an LSDRaster
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDChiTools::create(LSDIndexRaster& ThisRaster)
{
NRows = ThisRaster.get_NRows();
NCols = ThisRaster.get_NCols();
XMinimum = ThisRaster.get_XMinimum();
YMinimum = ThisRaster.get_YMinimum();
DataResolution = ThisRaster.get_DataResolution();
NoDataValue = ThisRaster.get_NoDataValue();
GeoReferencingStrings = ThisRaster.get_GeoReferencingStrings();
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Creates an LSDChiTools from an LSDFlowInfo
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDChiTools::create(LSDFlowInfo& ThisFI)
{
NRows = ThisFI.get_NRows();
NCols = ThisFI.get_NCols();
XMinimum = ThisFI.get_XMinimum();
YMinimum = ThisFI.get_YMinimum();
DataResolution = ThisFI.get_DataResolution();
NoDataValue = ThisFI.get_NoDataValue();
GeoReferencingStrings = ThisFI.get_GeoReferencingStrings();
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Creates an LSDChiTools from an LSDFlowInfo
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDChiTools::create(LSDJunctionNetwork& ThisJN)
{
NRows = ThisJN.get_NRows();
NCols = ThisJN.get_NCols();
XMinimum = ThisJN.get_XMinimum();
YMinimum = ThisJN.get_YMinimum();
DataResolution = ThisJN.get_DataResolution();
NoDataValue = ThisJN.get_NoDataValue();
GeoReferencingStrings = ThisJN.get_GeoReferencingStrings();
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// the create function. This is default and throws an error
// SMM 2012
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDChiTools::create()
{
// cout << "LSDChiTools line 64 Warning you have an empty LSDChiTools!" << endl;
// exit(EXIT_FAILURE);
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//
// This resets all the data maps
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDChiTools::reset_data_maps()
{
map<int,float> empty_map;
vector<int> empty_vec;
M_chi_data_map = empty_map;
b_chi_data_map = empty_map;
elev_data_map = empty_map;
chi_data_map = empty_map;
flow_distance_data_map = empty_map;
drainage_area_data_map = empty_map;
node_sequence = empty_vec;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//
// This function returns the x and y location of a row and column
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDChiTools::get_x_and_y_locations(int row, int col, double& x_loc, double& y_loc)
{
x_loc = XMinimum + float(col)*DataResolution + 0.5*DataResolution;
// Slightly different logic for y because the DEM starts from the top corner
y_loc = YMinimum + float(NRows-row)*DataResolution - 0.5*DataResolution;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//
// This function returns the x and y location of a row and column
// Same as above but with floats
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDChiTools::get_x_and_y_locations(int row, int col, float& x_loc, float& y_loc)
{
x_loc = XMinimum + float(col)*DataResolution + 0.5*DataResolution;
// Slightly different logic for y because the DEM starts from the top corner
y_loc = YMinimum + float(NRows-row)*DataResolution - 0.5*DataResolution;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//
// Function to convert a node position with a row and column to a lat
// and long coordinate
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDChiTools::get_lat_and_long_locations(int row, int col, double& lat,
double& longitude, LSDCoordinateConverterLLandUTM Converter)
{
// get the x and y locations of the node
double x_loc,y_loc;
get_x_and_y_locations(row, col, x_loc, y_loc);
// get the UTM zone of the node
int UTM_zone;
bool is_North;
get_UTM_information(UTM_zone, is_North);
//cout << endl << endl << "Line 1034, UTM zone is: " << UTM_zone << endl;
if(UTM_zone == NoDataValue)
{
lat = NoDataValue;
longitude = NoDataValue;
}
else
{
// set the default ellipsoid to WGS84
int eId = 22;
double xld = double(x_loc);
double yld = double(y_loc);
// use the converter to convert to lat and long
double Lat,Long;
Converter.UTMtoLL(eId, yld, xld, UTM_zone, is_North, Lat, Long);
lat = Lat;
longitude = Long;
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//
// Function to convert a x/y position that is not necessarly a node into lat/long
// and long coordinate
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDChiTools::get_lat_and_long_locations_from_coordinate(float X, float Y, double& lat,
double& longitude, LSDCoordinateConverterLLandUTM Converter)
{
// get the UTM zone of the node
int UTM_zone;
bool is_North;
get_UTM_information(UTM_zone, is_North);
//cout << endl << endl << "Line 1034, UTM zone is: " << UTM_zone << endl;
if(UTM_zone == NoDataValue)
{
lat = NoDataValue;
longitude = NoDataValue;
}
else
{
// set the default ellipsoid to WGS84
int eId = 22;
double xld = double(X);
double yld = double(Y);
// use the converter to convert to lat and long
double Lat,Long;
Converter.UTMtoLL(eId, yld, xld, UTM_zone, is_North, Lat, Long);
lat = Lat;
longitude = Long;
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//
// This function gets the UTM zone
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDChiTools::get_UTM_information(int& UTM_zone, bool& is_North)
{
// set up strings and iterators
map<string,string>::iterator iter;
//check to see if there is already a map info string
string mi_key = "ENVI_map_info";
iter = GeoReferencingStrings.find(mi_key);
if (iter != GeoReferencingStrings.end() )
{
string info_str = GeoReferencingStrings[mi_key] ;
// now parse the string
vector<string> mapinfo_strings;
istringstream iss(info_str);
while( iss.good() )
{
string substr;
getline( iss, substr, ',' );
mapinfo_strings.push_back( substr );
}
UTM_zone = atoi(mapinfo_strings[7].c_str());
//cout << "Line 1041, UTM zone: " << UTM_zone << endl;
//cout << "LINE 1042 LSDRaster, N or S: " << mapinfo_strings[7] << endl;
// find if the zone is in the north
string n_str = "n";
string N_str = "N";
is_North = false;
size_t found = mapinfo_strings[8].find(N_str);
if (found!=string::npos)
{
is_North = true;
}
found = mapinfo_strings[8].find(n_str);
if (found!=string::npos)
{
is_North = true;
}
//cout << "is_North is: " << is_North << endl;
}
else
{
UTM_zone = NoDataValue;
is_North = false;
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// This updates the chi values using a new chi raster
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDChiTools::update_chi_data_map(LSDFlowInfo& FlowInfo, LSDRaster& Chi_coord)
{
if (chi_data_map.size() == 0)
{
cout << "Trying to update chi but you have not run the automator yet to" << endl;
cout << "organise the sources and channels. LSDChiTools::update_chi_data_map" << endl;
}
else
{
int n_nodes = int(node_sequence.size());
int this_node,row,col;
float updated_chi;
for(int node = 0; node<n_nodes; node++)
{
this_node = node_sequence[node];
FlowInfo.retrieve_current_row_and_col(this_node,row,col);
updated_chi = Chi_coord.get_data_element(row,col);
chi_data_map[this_node] = updated_chi;
}
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// This updates the chi values by calculating them directly from the FlowInfo object
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDChiTools::update_chi_data_map(LSDFlowInfo& FlowInfo, float A_0, float movern)
{
if (chi_data_map.size() == 0)
{
cout << "Trying to update chi but you have not run the automator yet to" << endl;
cout << "organise the sources and channels. LSDChiTools::update_chi_data_map" << endl;
}
else
{
cout << "WARNING: update_chi_data_map" << endl;
cout<< "Chi is being calculated from outlet nodes, so interior basins may have different starting chi values." << endl;
float thresh_area_for_chi = 0; // this gets chi in all nodes. Not much slower and avoids errors
LSDRaster this_chi_coordinate = FlowInfo.get_upslope_chi_from_all_baselevel_nodes(movern,A_0,thresh_area_for_chi);
int n_nodes = int(node_sequence.size());
int this_node,row,col;
float updated_chi;
for(int node = 0; node<n_nodes; node++)
{
this_node = node_sequence[node];
FlowInfo.retrieve_current_row_and_col(this_node,row,col);
updated_chi = this_chi_coordinate.get_data_element(row,col);
chi_data_map[this_node] = updated_chi;
}
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// This updates the chi values by calculating them directly from the FlowInfo object
// The outlet_node_from_basin_key_map map is generated by
// get_outlet_node_from_basin_key_map()
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDChiTools::update_chi_data_map_for_single_basin(LSDFlowInfo& FlowInfo, float A_0, float movern,
int minimum_contributing_pixels, int basin_key,
map<int,int> outlet_node_from_basin_key_map)
{
if (chi_data_map.size() == 0)
{
cout << "Trying to update chi but you have not run the automator yet to" << endl;
cout << "organise the sources and channels. LSDChiTools::update_chi_data_map_for_single_basin" << endl;
}
else
{
// get the source node of the basin in question
int outlet_node_index;
if ( outlet_node_from_basin_key_map.find(basin_key) == outlet_node_from_basin_key_map.end() )
{
cout << "Fatal error: you have selected a basin key (" << basin_key << " that is not amongst the basins." << endl;
exit(EXIT_FAILURE);
}
else
{
outlet_node_index = outlet_node_from_basin_key_map[basin_key];
}
// now get the chi coordinate updated for this basin only
//cout << "Outlet node index is: " << outlet_node_index << endl;
map<int,float> new_chi_map = FlowInfo.get_upslope_chi_from_single_starting_node(outlet_node_index , movern, A_0, minimum_contributing_pixels);
map<int,float>::iterator iter = new_chi_map.begin();
while(iter != new_chi_map.end())
{
//cout << "node is: " << iter->first << endl;
chi_data_map[iter->first] = iter->second;
iter++;
}
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// This updates the chi values by calculating them directly from the FlowInfo object
// The outlet_node_from_basin_key_map map is generated by
// get_outlet_node_from_basin_key_map()
// same as above but includes discharge raster
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDChiTools::update_chi_data_map_for_single_basin(LSDFlowInfo& FlowInfo, float A_0, float movern,
int minimum_contributing_pixels, int basin_key,
map<int,int> outlet_node_from_basin_key_map, LSDRaster& Discharge)
{
if (chi_data_map.size() == 0)
{
cout << "Trying to update chi but you have not run the automator yet to" << endl;
cout << "organise the sources and channels. LSDChiTools::update_chi_data_map_for_single_basin" << endl;
}
else
{
// get the source node of the basin in question
int outlet_node_index;
if ( outlet_node_from_basin_key_map.find(basin_key) == outlet_node_from_basin_key_map.end() )
{
cout << "Fatal error: you have selected a basin key (" << basin_key << " that is not amongst the basins." << endl;
exit(EXIT_FAILURE);
}
else
{
outlet_node_index = outlet_node_from_basin_key_map[basin_key];
}
// now get the chi coordinate updated for this basin only
map<int,float> new_chi_map = FlowInfo.get_upslope_chi_from_single_starting_node(outlet_node_index , movern, A_0, minimum_contributing_pixels, Discharge);
map<int,float>::iterator iter = new_chi_map.begin();
while(iter != new_chi_map.end())
{
chi_data_map[iter->first] = iter->second;
iter++;
}
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// This prints a chi map to csv with an area threshold in m^2
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDChiTools::chi_map_to_csv(LSDFlowInfo& FlowInfo, string chi_map_fname,
float A_0, float m_over_n, float area_threshold)
{
ofstream chi_map_csv_out;
chi_map_csv_out.open(chi_map_fname.c_str());
chi_map_csv_out.precision(9);
float chi_coord;
double latitude,longitude;
LSDCoordinateConverterLLandUTM Converter;
chi_map_csv_out << "latitude,longitude,chi" << endl;
LSDRaster Chi = FlowInfo.get_upslope_chi_from_all_baselevel_nodes(m_over_n, A_0, area_threshold);
float NDV = Chi.get_NoDataValue();
for(int row = 0; row<NRows; row++)
{
for(int col = 0; col<NCols; col++)
{
chi_coord = Chi.get_data_element(row,col);
if (chi_coord != NDV)
{
get_lat_and_long_locations(row, col, latitude, longitude, Converter);
chi_map_csv_out << latitude << "," << longitude << "," << chi_coord << endl;
}
}
}
chi_map_csv_out.close();
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// This prints a chi map to csv with an area threshold in m^2. You feed it the chi map
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDChiTools::chi_map_to_csv(LSDFlowInfo& FlowInfo, string chi_map_fname,
LSDRaster& chi_coord)
{
ofstream chi_map_csv_out;
chi_map_csv_out.open(chi_map_fname.c_str());
float this_chi_coord;
double latitude,longitude;
LSDCoordinateConverterLLandUTM Converter;
chi_map_csv_out << "latitude,longitude,chi" << endl;
float NDV = chi_coord.get_NoDataValue();
for(int row = 0; row<NRows; row++)
{
for(int col = 0; col<NCols; col++)
{
this_chi_coord = chi_coord.get_data_element(row,col);
if (this_chi_coord != NDV)
{
get_lat_and_long_locations(row, col, latitude, longitude, Converter);
chi_map_csv_out.precision(9);
chi_map_csv_out << latitude << "," << longitude << ",";
chi_map_csv_out.precision(5);
chi_map_csv_out << this_chi_coord << endl;
}
}
}
chi_map_csv_out.close();
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// This prints a chi map to csv with an area threshold in m^2. You feed it the chi map
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDChiTools::chi_map_to_csv(LSDFlowInfo& FlowInfo, string chi_map_fname,
LSDRaster& chi_coord, LSDIndexRaster& basin_raster)
{
ofstream chi_map_csv_out;
chi_map_csv_out.open(chi_map_fname.c_str());
float this_chi_coord;
int this_basin_number;
double latitude,longitude;
LSDCoordinateConverterLLandUTM Converter;
chi_map_csv_out << "latitude,longitude,chi,basin_junction" << endl;
float NDV = chi_coord.get_NoDataValue();
for(int row = 0; row<NRows; row++)
{
for(int col = 0; col<NCols; col++)
{
this_chi_coord = chi_coord.get_data_element(row,col);
this_basin_number = basin_raster.get_data_element(row,col);
if (this_chi_coord != NDV)
{
get_lat_and_long_locations(row, col, latitude, longitude, Converter);
chi_map_csv_out.precision(9);
chi_map_csv_out << latitude << "," << longitude << ",";
chi_map_csv_out.precision(5);
chi_map_csv_out << this_chi_coord << ",";
chi_map_csv_out << this_basin_number << endl;
}
}
}
chi_map_csv_out.close();
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// This prints a csv with basin_ID, all the litho count and the total
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDChiTools::simple_litho_basin_to_csv(LSDFlowInfo& FlowInfo, string csv_slbc_fname,
map<int,map<int,int> > map_slbc)
{
cout << "I am writing a simple basin/litho csv file: " << csv_slbc_fname << endl;
// opening the stream to write the csv file
ofstream csv_out;
csv_out.open(csv_slbc_fname.c_str());
// writing the header
csv_out << "basin_id,";
for(map<int,int>::iterator it = map_slbc[0].begin(); it !=map_slbc[0].end();++it)
{
csv_out << it->first << ",";
}
csv_out << "total"<< endl;
// preparing the writing
int total_temp = 0;
map<int,int> tempmapcsv; // temporary map to avoid mapception confusions
// writing, first loop through basins
for(map<int,map<int,int> >::iterator it1 = map_slbc.begin(); it1 !=map_slbc.end();++it1)
{
csv_out << it1->first<<","; // writing the basin ID
tempmapcsv = it1->second; // Getting the map of values
// Second loop through the map of litho
for(map<int,int>::iterator it = tempmapcsv.begin(); it !=tempmapcsv.end();++it)
{
csv_out << it->second << ","; // writing the value
total_temp += it->second; // temporary temp
}
// writing and reinitializing the total
csv_out << total_temp << endl;
total_temp = 0;
}
// Done, closing the file stream
csv_out.close();
cout << "I am done writing the simple litho file." << endl;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// This prints a csv with basin_ID, all the litho count and the total
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDChiTools::extended_litho_basin_to_csv(LSDFlowInfo& FlowInfo, string csv_slbc_fname,
map<int,map<int,int> > map_slbc)
{
cout << "I am writing a simple basin/litho csv file: " << csv_slbc_fname << endl;
// opening the stream to write the csv file
ofstream csv_out;
csv_out.open(csv_slbc_fname.c_str());
// writing the header
csv_out << "method," << "basin_id,";
for(map<int,int>::iterator it = map_slbc[0].begin(); it !=map_slbc[0].end();++it)
{
csv_out << it->first << ",";
}
csv_out << "total"<< endl;
// Some stats on it
map<int,map<int,float> > percentmap;
map<int,float> tempmappercent;
map<int,int> tempmapcsv1;
int total_temp;
float tvalue;
for(map<int,map<int,int> >::iterator it1 = map_slbc.begin(); it1 !=map_slbc.end();++it1)
{
total_temp = 0;
// Second loop through the map of litho
tempmapcsv1 = it1->second;
for(map<int,int>::iterator it = tempmapcsv1.begin(); it !=tempmapcsv1.end();++it)
{
total_temp += it->second;
}
for(map<int,int>::iterator it = tempmapcsv1.begin(); it !=tempmapcsv1.end();++it)
{
tvalue = it->second;
tempmappercent[it->first] = tvalue*100/float(total_temp);
}
percentmap[it1->first] = tempmappercent;
tempmappercent.clear();
total_temp = 0;
}
// preparing the writing
total_temp = 0;
map<int,int> tempmapcsv; // temporary map to avoid mapception confusions
// writing, first loop through basins
for(map<int,map<int,int> >::iterator it1 = map_slbc.begin(); it1 !=map_slbc.end();++it1)
{
// writing the raw count
csv_out << "count,";
csv_out << it1->first<<","; // writing the basin ID
tempmapcsv = it1->second; // Getting the map of values
// Second loop through the map of litho
for(map<int,int>::iterator it = tempmapcsv.begin(); it !=tempmapcsv.end();++it)
{
csv_out << it->second << ","; // writing the value
total_temp += it->second; // temporary temp
}
// writing and reinitializing the total
csv_out << total_temp << endl;
total_temp = 0;
// writing the percentage
csv_out << "percentage,";
csv_out << it1->first<<","; // writing the basin ID
tempmappercent = percentmap[it1->first]; // Getting the map of values
// Second loop through the map of litho
float percentemptot =0;
for(map<int,float>::iterator it = tempmappercent.begin(); it !=tempmappercent.end();++it)
{
csv_out << it->second << ","; // writing the value
percentemptot += it->second; // temporary temp
}
// writing and reinitializing the total
csv_out << percentemptot << endl; // should be 100 if everything is ok
percentemptot = 0;
}
// Done, closing the file stream
csv_out.close();
cout << "I am done writing the simple litho file." << endl;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// This function creates the data structures for keeping track of
// the channel network but only maps the chi coordinate.
// Mainly used for calculating m/n ratio.
// DOES NOT segment the chi-elevation profiles
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDChiTools::chi_map_automator_chi_only(LSDFlowInfo& FlowInfo,
vector<int> source_nodes,
vector<int> outlet_nodes,
vector<int> baselevel_node_of_each_basin,
LSDRaster& Elevation, LSDRaster& FlowDistance,
LSDRaster& DrainageArea, LSDRaster& chi_coordinate)
{
// These elements access the chi data
vector< vector<float> > chi_coordinates;
vector< vector<int> > chi_node_indices;
// these are for the individual channels
vector<float> these_chi_coordinates;
vector<int> these_chi_node_indices;
// these are maps that will store the data
map<int,float> chi_coord_map;
map<int,float> elev_map;
map<int,float> area_map;
map<int,float> flow_distance_map;
vector<int> node_sequence_vec;
// these are vectors that will store information about the individual nodes
// that allow us to map the nodes to specific channels during data visualisation
// These two maps have each node in the channel (the index)
// linked to a key (either the baselevel key or source key)
map<int,int> these_source_keys;
map<int,int> these_baselevel_keys;
// These two maps link keys, which are incrmented by one, to the
// junction or node of the baselevel or source
map<int,int> this_key_to_source_map;
map<int,int> this_key_to_baselevel_map;
// these are for working with the FlowInfo object
int this_node,row,col;
int this_base_level, this_source_node;
vector<int> empty_vec;
ordered_baselevel_nodes = empty_vec;
ordered_source_nodes = empty_vec;
// get the number of channels
int source_node_tracker = -1;
int baselevel_tracker = -1;
int ranked_source_node_tracker = -1;
int n_channels = int(source_nodes.size());
for(int chan = 0; chan<n_channels; chan++)
{
//cout << "Sampling channel " << chan+1 << " of " << n_channels << endl;
// get the base level
this_base_level = baselevel_node_of_each_basin[chan];
//cout << "Got the base level" << endl;
// If a key to this base level does not exist, add one.
if ( this_key_to_baselevel_map.find(this_base_level) == this_key_to_baselevel_map.end() )
{
baselevel_tracker++;
// this resets the ranked source node tracker
ranked_source_node_tracker = -1;
//cout << "Found a new baselevel. The node is: " << this_base_level << " and key is: " << baselevel_tracker << endl;
this_key_to_baselevel_map[this_base_level] = baselevel_tracker;
ordered_baselevel_nodes.push_back(this_base_level);
// Get the node of the source to the mainstem. This works because the
// mainstem is always the first channel listed in a basin.
source_node_of_mainstem_map[baselevel_tracker] = source_nodes[chan];
}
// now add the source tracker
source_node_tracker++;
ranked_source_node_tracker++;
// get the source node
this_source_node = source_nodes[chan];
// add the node to the trackers so that we can trace individual basin nodes
// for m over n calculations
ordered_source_nodes.push_back(this_source_node);
source_nodes_ranked_by_basin.push_back(ranked_source_node_tracker);
// now add the source node to the data map
this_key_to_source_map[this_source_node] = source_node_tracker;
//cout << "The source key is: " << source_node_tracker << " and basin key is: " << baselevel_tracker << endl;
// get this particular channel (it is a chi network with only one channel)
LSDChiNetwork ThisChiChannel(FlowInfo, source_nodes[chan], outlet_nodes[chan],
Elevation, FlowDistance, DrainageArea,chi_coordinate);
// okay the ChiNetwork has all the data about the m vales at this stage.
// Get these vales and print them to a raster
chi_coordinates = ThisChiChannel.get_chis();
chi_node_indices = ThisChiChannel.get_node_indices();
// now get the number of channels. This should be 1!
int n_channels = int(chi_coordinates.size());
if (n_channels != 1)
{
cout << "Whoa there, I am trying to make a chi map but something seems to have gone wrong with the channel extraction." << endl;
cout << "I should only have one channel per look but I have " << n_channels << " channels." << endl;
}
// now get chi coordantes
these_chi_coordinates = chi_coordinates[0];
these_chi_node_indices = chi_node_indices[0];
//cout << "I have " << these_chi_m_means.size() << " nodes." << endl;
int n_nodes_in_channel = int(these_chi_coordinates.size());
for (int node = 0; node< n_nodes_in_channel; node++)
{
this_node = these_chi_node_indices[node];
//cout << "This node is " << this_node << endl;
// only take the nodes that have not been found
if (chi_coord_map.find(this_node) == chi_coord_map.end() )
{
FlowInfo.retrieve_current_row_and_col(this_node,row,col);
//cout << "This is a new node; " << this_node << endl;
chi_coord_map[this_node] = these_chi_coordinates[node];
elev_map[this_node] = Elevation.get_data_element(row,col);
area_map[this_node] = DrainageArea.get_data_element(row,col);
flow_distance_map[this_node] = FlowDistance.get_data_element(row,col);
node_sequence_vec.push_back(this_node);
these_source_keys[this_node] = source_node_tracker;
these_baselevel_keys[this_node] = baselevel_tracker;
}
else
{
//cout << "I already have node: " << this_node << endl;
}
}
}
//cout << "I am all finished segmenting the channels!" << endl;
// set the object data members
elev_data_map = elev_map;
chi_data_map = chi_coord_map;
flow_distance_data_map = flow_distance_map;
drainage_area_data_map = area_map;
node_sequence = node_sequence_vec;
source_keys_map = these_source_keys;
baselevel_keys_map = these_baselevel_keys;
key_to_source_map = this_key_to_source_map;
key_to_baselevel_map = this_key_to_baselevel_map;
//cout << "BUG TRACKER" << endl; exit(EXIT_FAILURE);
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// This function is for calculating segments from all sources in a DEM
// The sources and their outlets are supplied by the source and outlet nodes
// vectors. These are generated from the LSDJunctionNetwork function
// get_overlapping_channels
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDChiTools::chi_map_automator(LSDFlowInfo& FlowInfo,
vector<int> source_nodes,
vector<int> outlet_nodes,
vector<int> baselevel_node_of_each_basin,
LSDRaster& Elevation, LSDRaster& FlowDistance,
LSDRaster& DrainageArea, LSDRaster& chi_coordinate,
int target_nodes,
int n_iterations, int skip,
int minimum_segment_length, float sigma)
{
// IMPORTANT THESE PARAMETERS ARE NOT USED BECAUSE CHI IS CALCULATED SEPARATELY
// However we need to give something to pass to the Monte carlo functions
// even through they are not used (they are inherited)
float A_0 = 1;
float m_over_n = 0.5;
// These elements access the chi data
vector< vector<float> > chi_m_means;
vector< vector<float> > chi_b_means;
vector< vector<float> > chi_coordinates;
vector< vector<int> > chi_node_indices;
// these are for the individual channels
vector<float> these_chi_m_means;
vector<float> these_chi_b_means;
vector<float> these_chi_coordinates;
vector<int> these_chi_node_indices;
// these are maps that will store the data
map<int,float> m_means_map;
map<int,float> b_means_map;
map<int,float> chi_coord_map;
map<int,float> elev_map;
map<int,float> area_map;
map<int,float> flow_distance_map;
vector<int> node_sequence_vec;
// these are vectors that will store information about the individual nodes
// that allow us to map the nodes to specific channels during data visualisation
// These two maps have each node in the channel (the index)
// linked to a key (either the baselevel key or source key)
map<int,int> these_source_keys;
map<int,int> these_baselevel_keys;
// These two maps link keys, which are incrmented by one, to the
// junction or node of the baselevel or source
map<int,int> this_key_to_source_map;
map<int,int> this_key_to_baselevel_map;
// these are for working with the FlowInfo object
int this_node,row,col;
int this_base_level, this_source_node;
// get the number of channels
int source_node_tracker = -1;
int baselevel_tracker = -1;
int ranked_source_node_tracker = -1;
int n_channels = int(source_nodes.size());
for(int chan = 0; chan<n_channels; chan++)
{