-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLSDChiNetwork.cpp
5863 lines (4935 loc) · 212 KB
/
LSDChiNetwork.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
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// LSDChiNetwork
// Land Surface Dynamics ChiNetwork
//
// An object within the University
// of Edinburgh Land Surface Dynamics group topographic toolbox
// for analysing channels using the integral method of channel
// analysis
//
// Developed by:
// Simon M. Mudd
// Martin D. Hurst
// David T. Milodowski
// Stuart W.D. Grieve
// Declan A. Valters
// Fiona Clubb
//
// Copyright (C) 2013 Simon M. Mudd 2013
//
// 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
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// source code for the LSDChiNetwork object
// this object organizes the chi analysis for several
// channel segments. It can be used in conjunction with the LSD topographic
// analysis package and also used as a standalone package
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//
// This object is written by
// Simon M. Mudd, University of Edinburgh
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//
// Version 0.0.1 5/4/2013
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//-----------------------------------------------------------------
//DOCUMENTATION URL: http://www.geos.ed.ac.uk/~s0675405/LSD_Docs/
//-----------------------------------------------------------------
#include <vector>
#include <list>
#include <algorithm>
#include <string>
#include <fstream>
#include "TNT/tnt.h"
#include "LSDChiNetwork.hpp"
#include "LSDMostLikelyPartitionsFinder.hpp"
#include "LSDStatsTools.hpp"
#include "LSDRaster.hpp"
#include "LSDFlowInfo.hpp"
using namespace std;
using namespace TNT;
#ifndef LSDChiNetwork_CPP
#define LSDChiNetwork_CPP
void LSDChiNetwork::create()
{
// Nothing, empty constructor
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// first create routine
//
// SMM 01/03/2013
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDChiNetwork::create(string channel_network_fname)
{
ifstream channel_data_in;
channel_data_in.open(channel_network_fname.c_str());
if( channel_data_in.fail() )
{
cout << "\nFATAL ERROR: the channel network file \"" << channel_network_fname
<< "\" doesn't exist" << endl;
exit(EXIT_FAILURE);
}
int channel_number;
int receiver_cnumber;
int recevier_cnode;
int node;
int row;
int col;
float flow_dist;
float elev;
float drain_area;
int last_cn = 0; // this is 1 if this is the first node in a channel
int last_receiver_node = -1;
int last_receiver_channel = -1;
vector<int> empty_int;
vector<float> empty_float;
vector<int> node_vec;
vector<int> row_vec;
vector<int> col_vec;
vector<float> flow_dist_vec;
vector<float> elev_vec;
vector<float> drain_area_vec;
channel_data_in >> NRows >> NCols >> XMinimum >> YMinimum >> DataResolution >> NoDataValue;
while( channel_data_in >> channel_number >> receiver_cnumber >> recevier_cnode
>> node >> row >> col >> flow_dist >> elev >> drain_area)
{
// get the receiver_channel and receiver node for the first channel (these will be recursive)
if (last_receiver_node == -1)
{
last_receiver_node = recevier_cnode;
last_receiver_channel = receiver_cnumber;
}
// if this is a new channel add the old channel data to the data members and reset the
// vectors for assimilating data
if (channel_number != last_cn)
{
cout << "new channel: " << channel_number << " last cn: " << last_cn << endl;
node_indices.push_back(node_vec);
row_indices.push_back(row_vec);
col_indices.push_back(col_vec);
elevations.push_back(elev_vec);
flow_distances.push_back(flow_dist_vec);
drainage_areas.push_back(drain_area_vec);
node_on_receiver_channel.push_back(last_receiver_node);
receiver_channel.push_back(last_receiver_channel);
node_vec = empty_int;
row_vec = empty_int;
col_vec = empty_int;
flow_dist_vec = empty_float;
elev_vec = empty_float;
drain_area_vec = empty_float;
// reset the receiver nodde and channel
last_receiver_node = recevier_cnode;
last_receiver_channel = receiver_cnumber;
last_cn = channel_number;
}
// now push back the data
node_vec.push_back(node);
row_vec.push_back(row);
col_vec.push_back(col);
flow_dist_vec.push_back(flow_dist);
elev_vec.push_back(elev);
drain_area_vec.push_back(drain_area);
}
// push back the data for the final channel
node_indices.push_back(node_vec);
row_indices.push_back(row_vec);
col_indices.push_back(col_vec);
elevations.push_back(elev_vec);
flow_distances.push_back(flow_dist_vec);
drainage_areas.push_back(drain_area_vec);
node_on_receiver_channel.push_back(last_receiver_node);
receiver_channel.push_back(last_receiver_channel);
// now initiate the chi values
int n_channels = int(elevations.size());
for (int i = 0; i< n_channels; i++)
{
int n_nodes_in_channel = (node_indices[i].size());
vector<float> empty_chi(n_nodes_in_channel,0.0);
chis.push_back(empty_chi);
}
// close the infile
channel_data_in.close();
I_should_calculate_chi = true;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//
// This version of the create function only creates a single channel
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDChiNetwork::create(LSDFlowInfo& FlowInfo, int SourceNode, int OutletNode, LSDRaster& Elevation,
LSDRaster& FlowDistance, LSDRaster& DrainageArea)
{
int current_node, reciever_node;
int row;
int col;
float flow_dist;
float elev;
float drain_area;
//int last_cn = 0; // this is 1 if this is the first node in a channel
//int last_receiver_node = -1;
//int last_receiver_channel = -1;
vector<int> empty_int;
vector<float> empty_float;
vector<int> node_vec;
vector<int> row_vec;
vector<int> col_vec;
vector<float> flow_dist_vec;
vector<float> elev_vec;
vector<float> drain_area_vec;
vector<float> chi_vec;
// we start from the top, accumulating data along the way
FlowInfo.retrieve_current_row_and_col(SourceNode,row,col);
flow_dist = FlowDistance.get_data_element(row,col);
elev = Elevation.get_data_element(row,col);
drain_area = DrainageArea.get_data_element(row,col);
// now push back the data
node_vec.push_back(SourceNode);
row_vec.push_back(row);
col_vec.push_back(col);
flow_dist_vec.push_back(flow_dist);
elev_vec.push_back(elev);
drain_area_vec.push_back(drain_area);
chi_vec.push_back(0.0);
current_node = SourceNode;
// now move downstream
while(current_node != OutletNode)
{
FlowInfo.retrieve_receiver_information(current_node,reciever_node, row, col);
// catch the loop if the OutletNode is not on the flow path
if(current_node == reciever_node)
{
cout <<"Making a chi network, but you reached a base level node before expected." << endl;
cout <<"You need to make sure you've got the right source and outlet nodes." << endl;
current_node = OutletNode;
}
else
{
// write the data to the vectors
flow_dist = FlowDistance.get_data_element(row,col);
elev = Elevation.get_data_element(row,col);
drain_area = DrainageArea.get_data_element(row,col);
node_vec.push_back(reciever_node);
row_vec.push_back(row);
col_vec.push_back(col);
flow_dist_vec.push_back(flow_dist);
elev_vec.push_back(elev);
drain_area_vec.push_back(drain_area);
chi_vec.push_back(0.0); // in this version the chi vec is calculated seperately.
}
// set the current node to the reciever
current_node = reciever_node;
}
// update the data elements
node_indices.push_back(node_vec);
row_indices.push_back(row_vec);
col_indices.push_back(col_vec);
elevations.push_back(elev_vec);
flow_distances.push_back(flow_dist_vec);
drainage_areas.push_back(drain_area_vec);
node_on_receiver_channel.push_back(OutletNode);
chis.push_back(chi_vec);
receiver_channel.push_back(0);
I_should_calculate_chi = true;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//
// This version of the create function only creates a single channel, but this time
// it uses existing chi data
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDChiNetwork::create(LSDFlowInfo& FlowInfo, int SourceNode, int OutletNode, LSDRaster& Elevation,
LSDRaster& FlowDistance, LSDRaster& DrainageArea,
LSDRaster& Chi)
{
int current_node, reciever_node;
int row;
int col;
float flow_dist;
float elev;
float drain_area;
float chi;
//int last_cn = 0; // this is 1 if this is the first node in a channel
//int last_receiver_node = -1;
//int last_receiver_channel = -1;
vector<int> empty_int;
vector<float> empty_float;
vector<int> node_vec;
vector<int> row_vec;
vector<int> col_vec;
vector<float> flow_dist_vec;
vector<float> elev_vec;
vector<float> drain_area_vec;
vector<float> chi_vec;
// we start from the top, accumulating data along the way
FlowInfo.retrieve_current_row_and_col(SourceNode,row,col);
flow_dist = FlowDistance.get_data_element(row,col);
elev = Elevation.get_data_element(row,col);
drain_area = DrainageArea.get_data_element(row,col);
chi = Chi.get_data_element(row,col);
// now push back the data
node_vec.push_back(SourceNode);
row_vec.push_back(row);
col_vec.push_back(col);
flow_dist_vec.push_back(flow_dist);
elev_vec.push_back(elev);
drain_area_vec.push_back(drain_area);
chi_vec.push_back(chi);
current_node = SourceNode;
// now move downstream
while(current_node != OutletNode)
{
FlowInfo.retrieve_receiver_information(current_node,reciever_node, row, col);
// catch the loop if the OutletNode is not on the flow path
if(current_node == reciever_node)
{
cout <<"Making a chi network, but you reached a base level node before expected." << endl;
cout <<"You need to make sure you've got the right source and outlet nodes." << endl;
current_node = OutletNode;
}
else
{
// write the data to the vectors
flow_dist = FlowDistance.get_data_element(row,col);
elev = Elevation.get_data_element(row,col);
drain_area = DrainageArea.get_data_element(row,col);
chi = Chi.get_data_element(row,col);
node_vec.push_back(reciever_node);
row_vec.push_back(row);
col_vec.push_back(col);
flow_dist_vec.push_back(flow_dist);
elev_vec.push_back(elev);
drain_area_vec.push_back(drain_area);
chi_vec.push_back(chi);
}
// set the current node to the reciever
current_node = reciever_node;
}
// update the data elements
node_indices.push_back(node_vec);
row_indices.push_back(row_vec);
col_indices.push_back(col_vec);
elevations.push_back(elev_vec);
flow_distances.push_back(flow_dist_vec);
drainage_areas.push_back(drain_area_vec);
chis.push_back(chi_vec);
node_on_receiver_channel.push_back(OutletNode);
receiver_channel.push_back(0);
//cout << "I got chi from a raster, DUDE!" << endl;
I_should_calculate_chi = false;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//
// this function extends the tributaries so all tributaries start from their source and then
// run all the way down to the outlet. That is, downstream nodes are reapeated in each
// tributary.
//
// The purpose of this is to pick up segments that may only have propagated
// a very short distance upstream from the tributary junction and are therefore
// shorter than the minimum segment length.
//
// The function is constucted so all the member functions should continue to work on the channel
// network once the tributaries have been recalucalted
//
// IMPORTANT: This only works if all the tributaries drain to the mainstem
//
// SMM 01/03/2013
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDChiNetwork::extend_tributaries_to_outlet()
{
int n_channels = elevations.size();
int n_nodes_on_mainstem = elevations[0].size();
if(n_channels>1)
{
for(int chan = 1; chan<n_channels; chan++)
{
int this_receiver_channel = receiver_channel[chan];
int this_node_on_receiver_channel = node_on_receiver_channel[chan];
for(int node = this_node_on_receiver_channel+1; node< n_nodes_on_mainstem; node++)
{
node_indices[chan].push_back( node_indices[this_receiver_channel][node] );
row_indices[chan].push_back ( row_indices[this_receiver_channel][node] );
col_indices[chan].push_back ( col_indices[this_receiver_channel][node] );
elevations[chan].push_back ( elevations[this_receiver_channel][node] );
flow_distances[chan].push_back ( flow_distances[this_receiver_channel][node] );
drainage_areas[chan].push_back ( drainage_areas[this_receiver_channel][node] );
chis[chan].push_back ( chis[this_receiver_channel][node] );
}
node_on_receiver_channel[chan] = int( node_indices[chan].size() )-1;
receiver_channel[chan] = chan;
}
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// this function prints the details of an individual channel to screen for bug checking
// SMM 01/03/2013
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDChiNetwork::print_channel_details_to_screen(int channel_number)
{
if (channel_number >= int(elevations.size()))
{
cout << "Channel number is in reference to a channel that doesn't exist" << endl;
cout << " using the last possible channel " << endl;
channel_number = int( elevations.size())-1;
}
vector<int> node = node_indices[channel_number];
vector<int> row = row_indices[channel_number];
vector<int> col = col_indices[channel_number];
vector<float> elevation = elevations[channel_number];
vector<float> flow_distance = flow_distances[channel_number];
vector<float> drainage_area = drainage_areas[channel_number];
vector<float> chi = chis[channel_number];
int n_nodes = node.size();
for (int i = 0; i< n_nodes; i++)
{
cout << channel_number << " " << receiver_channel[channel_number] << " "
<< node_on_receiver_channel[channel_number] << " "
<< node[i] << " " << row[i] << " " << col[i] << " " << flow_distance[i] << " "
<< chi[i] << " " << elevation[i] << " " << drainage_area[i] << endl;
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// this function prints the details of all channels to a file
// format is
// A_0 m_over_n
// channel_number node_on_receiver_channel node_index row col flow_distance chi elevation darainage_area
//
// SMM 01/03/2013
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDChiNetwork::print_channel_details_to_file(string fname, float A_0, float m_over_n)
{
ofstream channel_profile_out;
channel_profile_out.open(fname.c_str());
if (I_should_calculate_chi)
{
calculate_chi(A_0, m_over_n);
}
channel_profile_out << A_0 << " " << m_over_n << endl;
int n_channels = chis.size();
for (int channel_number = 0; channel_number< n_channels; channel_number++)
{
vector<int> node = node_indices[channel_number];
vector<int> row = row_indices[channel_number];
vector<int> col = col_indices[channel_number];
vector<float> elevation = elevations[channel_number];
vector<float> flow_distance = flow_distances[channel_number];
vector<float> drainage_area = drainage_areas[channel_number];
vector<float> chi = chis[channel_number];
int n_nodes = node.size();
for (int i = 0; i< n_nodes; i++)
{
channel_profile_out << channel_number << " " << receiver_channel[channel_number] << " "
<< node_on_receiver_channel[channel_number] << " "
<< node[i] << " " << row[i] << " " << col[i] << " " << flow_distance[i] << " "
<< chi[i] << " " << elevation[i] << " " << drainage_area[i] << endl;
}
}
channel_profile_out.close();
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// this function prints the details of all channels to a file
// it includes data from monte carlo fittin
// format is
// A_0 m_over_n
// channel_number node_on_receiver_channel node_index row col flow_distance chi elevation darainage_area...
//
// SMM 01/03/2013
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDChiNetwork::print_channel_details_to_file_full_fitted(string fname)
{
float m_over_n = m_over_n_for_fitted_data;
float A_0 = A_0_for_fitted_data;
ofstream channel_profile_out;
channel_profile_out.open(fname.c_str());
if (I_should_calculate_chi)
{
calculate_chi(A_0, m_over_n);
}
channel_profile_out << A_0 << " " << m_over_n << endl;
int n_channels = chis.size();
if (chi_b_means.size() == chis.size())
{
for (int channel_number = 0; channel_number< n_channels; channel_number++)
{
vector<int> node = node_indices[channel_number];
vector<int> row = row_indices[channel_number];
vector<int> col = col_indices[channel_number];
vector<float> elevation = elevations[channel_number];
vector<float> flow_distance = flow_distances[channel_number];
vector<float> drainage_area = drainage_areas[channel_number];
vector<float> chi = chis[channel_number];
vector<float> m_mean = chi_m_means[channel_number];
vector<float> m_standard_deviation = chi_m_standard_deviations[channel_number];
vector<float> m_standard_error = chi_m_standard_errors[channel_number];
vector<float> b_mean = chi_b_means[channel_number];
vector<float> b_standard_deviation = chi_b_standard_deviations[channel_number];
vector<float> b_standard_error = chi_b_standard_errors[channel_number];
vector<float> DW_mean = chi_DW_means[channel_number];
vector<float> DW_standard_deviation = chi_DW_standard_deviations[channel_number];
vector<float> DW_standard_error = chi_DW_standard_errors[channel_number];
vector<float> fitted_elev_mean = all_fitted_elev_means[channel_number];
vector<float> fitted_elev_standard_deviation = all_fitted_elev_standard_deviations[channel_number];
vector<float> fitted_elev_standard_error = all_fitted_elev_standard_errors[channel_number];
vector<int> n_data_points_uic = n_data_points_used_in_stats[channel_number];
int n_nodes = node.size();
for (int i = 0; i< n_nodes; i++)
{
channel_profile_out << channel_number << " " << receiver_channel[channel_number] << " "
<< node_on_receiver_channel[channel_number] << " "
<< node[i] << " " << row[i] << " " << col[i] << " " << flow_distance[i] << " "
<< chi[i] << " " << elevation[i] << " " << drainage_area[i] << " "
<< n_data_points_uic[i] << " "
<< m_mean[i] << " " << m_standard_deviation[i] << " " << m_standard_error[i] << " "
<< b_mean[i] << " " << b_standard_deviation[i] << " " << b_standard_error[i] << " "
<< DW_mean[i] << " " << DW_standard_deviation[i] << " " << DW_standard_error[i] << " "
<< fitted_elev_mean[i] << " " << fitted_elev_standard_deviation[i] << " "
<< fitted_elev_standard_error[i] << " " << endl;
}
}
}
else
{
cout << "LSDChiNetwork Line 276 you don't seem to have run the monte carlo fitting routine" << endl;
}
channel_profile_out.close();
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// this function prints the details of all channels to a file
// it includes data from monte carlo fittin
// format is
// A_0 m_over_n
// channel_number node_on_receiver_channel node_index row col flow_distance chi elevation darainage_area...
//
// SMM 01/03/2013
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDChiNetwork::print_channel_details_to_file_full_fitted(string fname, int target_nodes, int minimum_segment_length)
{
float m_over_n = m_over_n_for_fitted_data;
float A_0 = A_0_for_fitted_data;
ofstream channel_profile_out;
channel_profile_out.open(fname.c_str());
if (I_should_calculate_chi)
{
calculate_chi(A_0, m_over_n);
}
channel_profile_out << A_0 << " " << m_over_n << endl;
int n_channels = chis.size();
if (chi_b_means.size() == chis.size())
{
int N = calculate_skip(target_nodes);
is_channel_long_enough_test(minimum_segment_length,N);
for (int channel_number = 0; channel_number< n_channels; channel_number++)
{
if(is_tributary_long_enough[channel_number] == 1)
{
vector<int> node = node_indices[channel_number];
vector<int> row = row_indices[channel_number];
vector<int> col = col_indices[channel_number];
vector<float> elevation = elevations[channel_number];
vector<float> flow_distance = flow_distances[channel_number];
vector<float> drainage_area = drainage_areas[channel_number];
vector<float> chi = chis[channel_number];
vector<float> m_mean = chi_m_means[channel_number];
vector<float> m_standard_deviation = chi_m_standard_deviations[channel_number];
vector<float> m_standard_error = chi_m_standard_errors[channel_number];
vector<float> b_mean = chi_b_means[channel_number];
vector<float> b_standard_deviation = chi_b_standard_deviations[channel_number];
vector<float> b_standard_error = chi_b_standard_errors[channel_number];
vector<float> DW_mean = chi_DW_means[channel_number];
vector<float> DW_standard_deviation = chi_DW_standard_deviations[channel_number];
vector<float> DW_standard_error = chi_DW_standard_errors[channel_number];
vector<float> fitted_elev_mean = all_fitted_elev_means[channel_number];
vector<float> fitted_elev_standard_deviation = all_fitted_elev_standard_deviations[channel_number];
vector<float> fitted_elev_standard_error = all_fitted_elev_standard_errors[channel_number];
vector<int> n_data_points_uic = n_data_points_used_in_stats[channel_number];
int n_nodes = node.size();
for (int i = 0; i< n_nodes; i++)
{
channel_profile_out << channel_number << " " << receiver_channel[channel_number] << " "
<< node_on_receiver_channel[channel_number] << " "
<< node[i] << " " << row[i] << " " << col[i] << " " << flow_distance[i] << " "
<< chi[i] << " " << elevation[i] << " " << drainage_area[i] << " "
<< n_data_points_uic[i] << " "
<< m_mean[i] << " " << m_standard_deviation[i] << " " << m_standard_error[i] << " "
<< b_mean[i] << " " << b_standard_deviation[i] << " " << b_standard_error[i] << " "
<< DW_mean[i] << " " << DW_standard_deviation[i] << " " << DW_standard_error[i] << " "
<< fitted_elev_mean[i] << " " << fitted_elev_standard_deviation[i] << " "
<< fitted_elev_standard_error[i] << " " << endl;
}
}
}
}
else
{
cout << "LSDChiNetwork Line 276 you don't seem to have run the monte carlo fitting routine" << endl;
}
channel_profile_out.close();
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//
// This function prints channel detaisl to a file that can be read by ArcMap
// in csv format
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDChiNetwork::print_channel_details_to_file_full_fitted_for_ArcMap(string fname)
{
// fid the last '.' in the filename to use in the scv filename
unsigned dot = fname.find_last_of(".");
string prefix = fname.substr(0,dot);
//string suffix = str.substr(dot);
string insert = "_for_Arc.csv";
string outfname = prefix+insert;
cout << "the Arc tree filename is: " << outfname << endl;
// open the outfile
ofstream ArcChan_out;
ArcChan_out.open(outfname.c_str());
ArcChan_out.precision(10);
// get the paramaters and calculate chi
float m_over_n = m_over_n_for_fitted_data;
float A_0 = A_0_for_fitted_data;
if (I_should_calculate_chi)
{
calculate_chi(A_0, m_over_n);
}
// set up an id placeholder. This is for arcmap
int id = 0;
double x,y;
int n_channels = chis.size();
// print the header information
ArcChan_out << "id,x,y,channel_number,receiver_channel,node_on_reciever_channel,";
ArcChan_out << "node,row,column,flow_distance,chi,elevation,drainage_area,n_data_points,";
ArcChan_out << "m_mean,m_st_dev,m_std_err,b_mean,b_st_dev,b_std_err,";
ArcChan_out << "DW_mean,DW_st_dev,DW_std_err,fitted_elev_mean,fitted_elev_st_dev,fitted_elev_std_err" << endl;
if (chi_b_means.size() == chis.size())
{
for (int channel_number = 0; channel_number< n_channels; channel_number++)
{
vector<int> node = node_indices[channel_number];
vector<int> row = row_indices[channel_number];
vector<int> col = col_indices[channel_number];
vector<float> elevation = elevations[channel_number];
vector<float> flow_distance = flow_distances[channel_number];
vector<float> drainage_area = drainage_areas[channel_number];
vector<float> chi = chis[channel_number];
vector<float> m_mean = chi_m_means[channel_number];
vector<float> m_standard_deviation = chi_m_standard_deviations[channel_number];
vector<float> m_standard_error = chi_m_standard_errors[channel_number];
vector<float> b_mean = chi_b_means[channel_number];
vector<float> b_standard_deviation = chi_b_standard_deviations[channel_number];
vector<float> b_standard_error = chi_b_standard_errors[channel_number];
vector<float> DW_mean = chi_DW_means[channel_number];
vector<float> DW_standard_deviation = chi_DW_standard_deviations[channel_number];
vector<float> DW_standard_error = chi_DW_standard_errors[channel_number];
vector<float> fitted_elev_mean = all_fitted_elev_means[channel_number];
vector<float> fitted_elev_standard_deviation = all_fitted_elev_standard_deviations[channel_number];
vector<float> fitted_elev_standard_error = all_fitted_elev_standard_errors[channel_number];
vector<int> n_data_points_uic = n_data_points_used_in_stats[channel_number];
// loop through the nodes in the channel
int n_nodes = node.size();
for (int i = 0; i< n_nodes; i++)
{
// increment the id and calculate the x and y locations
// the last 0.0001*DataResolution is to make sure there are no integer data points
id++;
x = XMinimum + float(col[i])*DataResolution + 0.5*DataResolution + 0.0001*DataResolution;
// y location is a little different because the DEM starts from the top corner
y = YMinimum + float(NRows-row[i])*DataResolution - 0.5*DataResolution + 0.0001*DataResolution;
ArcChan_out << id << "," << x << "," << y << ","
<< channel_number << "," << receiver_channel[channel_number] << ","
<< node_on_receiver_channel[channel_number] << ","
<< node[i] << "," << row[i] << "," << col[i] << "," << flow_distance[i] << ","
<< chi[i] << "," << elevation[i] << "," << drainage_area[i] << ","
<< n_data_points_uic[i] << ","
<< m_mean[i] << "," << m_standard_deviation[i] << "," << m_standard_error[i] << ","
<< b_mean[i] << "," << b_standard_deviation[i] << "," << b_standard_error[i] << ","
<< DW_mean[i] << "," << DW_standard_deviation[i] << "," << DW_standard_error[i] << ","
<< fitted_elev_mean[i] << "," << fitted_elev_standard_deviation[i] << ","
<< fitted_elev_standard_error[i] << endl;
}
}
}
else
{
cout << "LSDChiNetwork Line 276 you don't seem to have run the monte carlo fitting routine" << endl;
}
ArcChan_out.close();
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// LSDChiNetwork::slope_area_extraction_vertical_intervals
//
// this one is the vertical intervals version: it measures slope over fixed vertical
// intervals as reccomended by Wobus et al 2006.
// This function gets slope data and area data for use in making slope area plots.
// It generates several data elements, which are written to the file with name fname (passed to
// function). The file format is for each row
// SA_file << chan << " " << start_row << " " << mp_row << " " << end_row << " "
// << start_col << " " << mp_col << " " << end_row << " "
// << start_interval_elevations << " "
// << mp_interval_elevations << " " << end_interval_elevations << " "
// << start_interval_flowdistance << " " << mp_interval_flowdistance << " "
// << end_interval_flowdistance << " "
// << start_area << " " << mp_area << " " << end_area << " " << slope
// << " " << log10(mp_area) << " " << log10(slope) << endl;
//
// where start, mp and end denote the start of the interval over which slope is measured, the midpoint
// and the end.
//
// The area thin fraction is used to thin the data so that segments with large changes in drainage
// area are not used in the regression (because these will affect the mean slope)
// the fraction is determined by (downslope_area-upslope_area)/midpoint_area.
// So if the fraction is 1 it means that the change is area is equal to the area at the midpoint
// a restictive value is 0.05, you will eliminate major tributaries with a 0.2, and
// 1 will catch almost all of the data.
//
// SMM 01/03/2013
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDChiNetwork::slope_area_extraction_vertical_intervals(float interval, float area_thin_fraction,
string fname)
{
int n_channels = elevations.size();
float half_interval = interval/2;
float target_end_interval_elevations;
float target_mp_interval_elevations;
float start_interval_elevations;
float end_interval_elevations;
float mp_interval_elevations = -9999;
float start_interval_flowdistance;
float end_interval_flowdistance;
float mp_interval_flowdistance = -9999;
int start_row, start_col;
int mp_row = -9999;
int mp_col = -9999;
int end_row, end_col;
float start_area;
float mp_area = -9999;
float end_area;
float area_thin_frac_for_test;
cout << "n channels is: " << n_channels << endl;
ofstream SA_file;
SA_file.open(fname.c_str());
// loop through the channels
for (int chan = 0; chan<n_channels; chan++)
{
int n_nodes_this_channel = (elevations[chan].size());
int final_node_flag = 0;
int end_flag;
int mp_flag;
int n = 0;
//cout << "n: " << n << " final_node_flag: " << final_node_flag << " nnodes: " << n_nodes_this_channel << endl;
// now, start at the top node (node 0) of each channel and
// work down. The algorithm looks downstream until it hits
// the midpoint, and then continues until it hits
// the end point
// if it encounters the end of the channel before it hits
// the end point the loop exits
while (final_node_flag == 0 && n < n_nodes_this_channel)
{
start_interval_elevations = elevations[chan][n];
start_interval_flowdistance = flow_distances[chan][n];
start_area = drainage_areas[chan][n];
start_row = row_indices[chan][n];
start_col = col_indices[chan][n];
target_end_interval_elevations = start_interval_elevations-interval;
target_mp_interval_elevations = start_interval_elevations-half_interval;
int search_node = n+1;
// reset midpoint and end flags
mp_flag = 0;
end_flag = 0;
//cout << "n is: " << n << " and end_flag is: " << end_flag << endl;
// now work downstream
while (search_node < n_nodes_this_channel && end_flag == 0)
{
//cout << "search_node: " << search_node << " elev: " << elevations[chan][search_node]
// << " and target mp, end: " << target_mp_interval_elevations << " " << target_end_interval_elevations << endl;
// see if search node is the midpoint node
if ( elevations[chan][search_node] <= target_mp_interval_elevations && mp_flag == 0)
{
mp_interval_elevations = elevations[chan][search_node];
mp_interval_flowdistance = flow_distances[chan][search_node];
mp_area = drainage_areas[chan][search_node];
mp_row = row_indices[chan][search_node];
mp_col = col_indices[chan][search_node];
// set midpoint flag so it doens't collect downstream nodes
mp_flag = 1;
}
// see if the search node is the end node
if (elevations[chan][search_node] <= target_end_interval_elevations)
{
end_interval_elevations = elevations[chan][search_node];
end_interval_flowdistance = flow_distances[chan][search_node];
end_area = drainage_areas[chan][search_node];
end_row = row_indices[chan][search_node];
end_col = col_indices[chan][search_node];
// set end flag so the search node is reset
end_flag = 1;
}
search_node++;
}
// if the end flag == 0 (that means it found the end interval) then print the information
// to file. If it didn't reach the end flag that means the previous node was the final
// flag
if (end_flag == 1)
{
float slope = (start_interval_elevations-end_interval_elevations)/
(start_interval_flowdistance-end_interval_flowdistance);
// the data take a log of the slope so it is necessary to have this statement
// the the case of a negative or zero slope
if(slope <=0)
{
slope = 0.0000000001;
}
area_thin_frac_for_test = (end_area-start_area)/mp_area;
if (area_thin_frac_for_test < area_thin_fraction)
{
SA_file << chan << " " << start_row << " " << mp_row << " " << end_row << " "
<< start_col << " " << mp_col << " " << end_col << " "
<< start_interval_elevations << " "
<< mp_interval_elevations << " " << end_interval_elevations << " "
<< start_interval_flowdistance << " " << mp_interval_flowdistance << " "
<< end_interval_flowdistance << " "
<< start_area << " " << mp_area << " " << end_area << " " << slope
<< " " << log10(mp_area) << " " << log10(slope) << endl;
}
}
else
{
final_node_flag = 1;
}
n++;
}
}
SA_file.close();