-
Notifications
You must be signed in to change notification settings - Fork 0
/
LSDFlowInfo.cpp
3481 lines (3078 loc) · 114 KB
/
LSDFlowInfo.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
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// LSDFlowInfo
// Land Surface Dynamics FlowInfo
//
// An object within the University
// of Edinburgh Land Surface Dynamics group topographic toolbox
// for organizing flow routing under the Fastscape algorithm
// (see Braun and Willett, Geomorphology 2013, v180, p 170-179)
//
//
// 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
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// LSDFlowInfo.cpp
// cpp file for the LSDFlowInfo object
// LSD stands for Land Surface Dynamics
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// This object is written by
// Simon M. Mudd, University of Edinburgh
// David Milodowski, University of Edinburgh
// Martin D. Hurst, British Geological Survey
// Fiona Clubb, University of Edinburgh
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// Version 0.1.0 21/10/2013
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//-----------------------------------------------------------------
//DOCUMENTATION URL: http://www.geos.ed.ac.uk/~s0675405/LSD_Docs/
//-----------------------------------------------------------------
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm>
#include <math.h>
#include "TNT/tnt.h"
#include "LSDFlowInfo.hpp"
#include "LSDIndexRaster.hpp"
#include "LSDStatsTools.hpp"
//#include "LSDRaster.hpp"
using namespace std;
using namespace TNT;
#ifndef LSDFlowInfo_CPP
#define LSDFlowInfo_CPP
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Create function, this is empty, you need to include a filename
//
// SMM 01/06/2012
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDFlowInfo::create()
{
cout << "You need to initialize with a LSDRaster!" << endl;
exit(EXIT_FAILURE);
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Create function, this creates from a pickled file
// fname is the name of the pickled flow info file
//
// SMM 01/06/2012
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDFlowInfo::create(string fname)
{
unpickle(fname);
}
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// algorithms for searching the vectors
// This gets the reciever of current_node (its node, row, and column)
//
// SMM 01/06/2012
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDFlowInfo::retrieve_receiver_information(int current_node,
int& receiver_node, int& receiver_row,
int& receiver_col)
{
int rn, rr, rc;
rn = ReceiverVector[current_node];
rr = RowIndex[rn];
rc = ColIndex[rn];
receiver_node = rn;
receiver_row = rr;
receiver_col = rc;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// algorithms for searching the vectors
// This gets the row and column of the current node
//
// SMM 01/06/2012
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDFlowInfo::retrieve_current_row_and_col(int current_node,int& curr_row,
int& curr_col)
{
int cr, cc;
cr = RowIndex[current_node];
cc = ColIndex[current_node];
curr_row = cr;
curr_col = cc;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// this function returns the base level node with the greatest drainage area
//
// SMM 01/06/2012
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
int LSDFlowInfo::retrieve_largest_base_level()
{
int n_bl = BaseLevelNodeList.size(); // get the number of baselevel nodes
int max_bl = 0;
for (int i = 0; i<n_bl; i++)
{
if(NContributingNodes[ BaseLevelNodeList[i] ] > max_bl)
{
max_bl = NContributingNodes[ BaseLevelNodeList[i] ];
}
}
return max_bl;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Get the node for a cell at a given row and column
//@author DTM
//@date 08/11/2013
int LSDFlowInfo::retrieve_node_from_row_and_column(int row, int column)
{
int Node = NodeIndex[row][column];
return Node;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// this function calcualtes the receiver nodes
// it returns the receiver vector r_i
// it also returns a flow direction array in this ordering:
//
// 7 0 1
// 6 -1 2
// 5 4 3
//
// note this is different from ArcMap flowdirection
// int Arc_flowdir; // flow direction in arcmap format
// // 32 64 128
// // 16 -- 1
// // 8 4 2
// one can convert nthese indices using the LSDIndexRaster object
// note in arc the row index increases down (to the south)
//
// SMM 01/06/2012
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDFlowInfo::create(vector<string> temp_BoundaryConditions,
LSDRaster& TopoRaster)
{
// initialize several data members
BoundaryConditions = temp_BoundaryConditions;
NRows = TopoRaster.NRows;
NCols = TopoRaster.NCols;
XMinimum = TopoRaster.XMinimum;
YMinimum = TopoRaster.YMinimum;
NoDataValue = int(TopoRaster.NoDataValue);
DataResolution = TopoRaster.DataResolution;
// Declare matrices for calculating flow routing
float one_ov_root2 = 0.707106781;
float target_elev; // a placeholder for the elevation of the potential receiver
float slope;
float max_slope; // the maximum slope away from a node
int max_slope_index; // index into the maximum slope
int row, col; // index for the rows and column
int receive_row,receive_col;
string::iterator string_iterator; // used to get characters from string
// we need logic for all of the boundaries.
// there are 3 kinds of edge boundaries:
// no flux
// base level
// periodic
// These are denoted in a vector of strings.
// the vector has four elements
// North boundary, East boundary, South bondary and West boundary
// the strings can be any length, as long as the first letter corresponds to the
// first letter of the boundary condition. It is not case sensitive.
// go through the boundaries
// A NOTE ON CARDINAL DIRECTIONS
// If one looks at the raster data, the top row in the data corresponds to the NORTH boundary
// This is the row that is first read into the code
// so row 0 is the NORTH boundary
// row NRows-1 is the SOUTH boundary
// column 0 is the WEST boundary
// column NCols-1 is the EAST boundary
vector<float> slopes(8,NoDataValue);
vector<int> row_kernal(8);
vector<int> col_kernal(8);
int ndv = NoDataValue;
NDataNodes = 0; // the number of nodes in the raster that have data
int one_if_a_baselevel_node; // this is a switch used to tag baseleve nodes
// the first thing you need to do is construct a topoglogy matrix
// the donor, receiver, etc lists are as long as the number of nodes.
// these are made of vectors that are exactly dimension n, which is the number of nodes with
// data. Each of these nodes has several index vectors, that point the program to where the node is
// we construct these index vectors first
// we need to loop through all the data before we calcualte slopes because the
// receiver node indices must be known before the slope calculations are run
vector<int> empty_vec;
RowIndex = empty_vec;
ColIndex = empty_vec;
BaseLevelNodeList = empty_vec;
ReceiverVector = empty_vec;
Array2D<int> ndv_raster(NRows,NCols,ndv);
NodeIndex = ndv_raster.copy();
FlowDirection = ndv_raster.copy();
FlowLengthCode = ndv_raster.copy();
// loop through the topo data finding places where there is actually data
for (row = 0; row<NRows; row++)
{
for (col = 0; col<NCols; col++)
{
// only do calcualtions if there is data
if(TopoRaster.RasterData[row][col] != NoDataValue)
{
RowIndex.push_back(row);
ColIndex.push_back(col);
NodeIndex[row][col] = NDataNodes;
NDataNodes++;
}
}
}
// now the row and col index are populated by the row and col of the node in row i
// and the node index has the indeces into the row and col vectors
// next up, make d, delta, and D vectors
vector<int> ndn_vec(NDataNodes,0);
vector<int> ndn_nodata_vec(NDataNodes,ndv);
vector<int> ndn_plusone_vec(NDataNodes+1,0);
vector<int> w_vector(NDataNodes,0);
NDonorsVector = ndn_vec;
DonorStackVector = ndn_vec;
DeltaVector = ndn_plusone_vec;
SVector = ndn_nodata_vec;
BLBasinVector = ndn_nodata_vec;
// this vector starts out empty and then base level nodes are added to it
for (row = 0; row<NRows; row++)
{
for (col = 0; col<NCols; col++)
{
// only do calcualtions if there is data
if(TopoRaster.RasterData[row][col] != NoDataValue)
{
// calcualte 8 slopes
// no slopes mean get NoDataValue entries
// the algorithm loops through the neighbors to the cells, collecting
// receiver indices. The order is
// 7 0 1
// 6 - 2
// 5 4 3
// where the above directions are cardinal directions
// do slope 0
row_kernal[0] = row-1;
row_kernal[1] = row-1;
row_kernal[2] = row;
row_kernal[3] = row+1;
row_kernal[4] = row+1;
row_kernal[5] = row+1;
row_kernal[6] = row;
row_kernal[7] = row-1;
col_kernal[0] = col;
col_kernal[1] = col+1;
col_kernal[2] = col+1;
col_kernal[3] = col+1;
col_kernal[4] = col;
col_kernal[5] = col-1;
col_kernal[6] = col-1;
col_kernal[7] = col-1;
// check for periodic boundary conditions
if( BoundaryConditions[0].find("P") == 0 || BoundaryConditions[0].find("p") == 0 )
{
if( BoundaryConditions[2].find("P") != 0 && BoundaryConditions[2].find("p") != 0 )
{
cout << "WARNING!!! North boundary is periodic! Changing South boundary to periodic" << endl;
BoundaryConditions[2] = "P";
}
}
if( BoundaryConditions[1].find("P") == 0 || BoundaryConditions[1].find("p") == 0 )
{
if( BoundaryConditions[3].find("P") != 0 && BoundaryConditions[3].find("p") != 0 )
{
cout << "WARNING!!! East boundary is periodic! Changing West boundary to periodic" << endl;
BoundaryConditions[3] = "P";
}
}
if( BoundaryConditions[2].find("P") == 0 || BoundaryConditions[2].find("p") == 0 )
{
if( BoundaryConditions[0].find("P") != 0 && BoundaryConditions[0].find("p") != 0 )
{
cout << "WARNING!!! South boundary is periodic! Changing North boundary to periodic" << endl;
BoundaryConditions[0] = "P";
}
}
if( BoundaryConditions[3].find("P") == 0 || BoundaryConditions[3].find("p") == 0 )
{
if( BoundaryConditions[1].find("P") != 0 && BoundaryConditions[1].find("p") != 0 )
{
cout << "WARNING!!! West boundary is periodic! Changing East boundary to periodic" << endl;
BoundaryConditions[1] = "P";
}
}
// reset baselevel switch for boundaries
one_if_a_baselevel_node = 0;
// NORTH BOUNDARY
if (row == 0)
{
if( BoundaryConditions[0].find("B") == 0 || BoundaryConditions[0].find("b") == 0 )
{
one_if_a_baselevel_node = 1;
}
else
{
// if periodic, reflect across to south boundary
if( BoundaryConditions[0].find("P") == 0 || BoundaryConditions[0].find("p") == 0 )
{
row_kernal[0] = NRows-1;
row_kernal[1] = NRows-1;
row_kernal[7] = NRows-1;
}
else
{
row_kernal[0] = ndv;
row_kernal[1] = ndv;
row_kernal[7] = ndv;
}
}
}
// EAST BOUNDAY
if (col == NCols-1)
{
if( BoundaryConditions[0].find("B") == 0 || BoundaryConditions[0].find("b") == 0 )
{
one_if_a_baselevel_node = 1;
}
else
{
if( BoundaryConditions[1].find("P") == 0 || BoundaryConditions[1].find("p") == 0)
{
col_kernal[1] = 0;
col_kernal[2] = 0;
col_kernal[3] = 0;
}
else
{
col_kernal[1] = ndv;
col_kernal[2] = ndv;
col_kernal[3] = ndv;
}
}
}
// SOUTH BOUNDARY
if (row == NRows-1)
{
if( BoundaryConditions[0].find("B") == 0 || BoundaryConditions[0].find("b") == 0 )
{
one_if_a_baselevel_node = 1;
}
else
{
if( BoundaryConditions[2].find("P") == 0 || BoundaryConditions[2].find("p") == 0)
{
row_kernal[3] = 0;
row_kernal[4] = 0;
row_kernal[5] = 0;
}
else
{
row_kernal[3] = ndv;
row_kernal[4] = ndv;
row_kernal[5] = ndv;
}
}
}
// WEST BOUNDARY
if (col == 0)
{
if( BoundaryConditions[0].find("B") == 0 || BoundaryConditions[0].find("b") == 0 )
{
one_if_a_baselevel_node = 1;
}
else
{
if( BoundaryConditions[3].find("P") == 0 || BoundaryConditions[3].find("p") == 0)
{
col_kernal[5] = NCols-1;
col_kernal[6] = NCols-1;
col_kernal[7] = NCols-1;
}
else
{
col_kernal[5] = ndv;
col_kernal[6] = ndv;
col_kernal[7] = ndv;
}
}
}
// now loop through the surrounding nodes, calcualting the slopes
// slopes with NoData get NoData slopes
// reminder of ordering:
// 7 0 1
// 6 - 2
// 5 4 3
// first logic for baselevel node
if (one_if_a_baselevel_node == 1)
{
// get reciever index
FlowDirection[row][col] = -1;
ReceiverVector.push_back(NodeIndex[row][col]);
FlowLengthCode[row][col] = 0;
}
// now the rest of the nodes
else
{
FlowLengthCode[row][col] = 0; // set flow length code to 0, this gets reset
// if there is a maximum slope
max_slope = 0;
max_slope_index = -1;
receive_row = row;
receive_col = col;
for (int slope_iter = 0; slope_iter<8; slope_iter++)
{
if (row_kernal[slope_iter] == ndv || col_kernal[slope_iter] == ndv)
{
slopes[slope_iter] = NoDataValue;
}
else
{
target_elev = TopoRaster.RasterData[ row_kernal[slope_iter] ][ col_kernal[slope_iter] ];
if(target_elev == NoDataValue)
{
slopes[slope_iter] = NoDataValue;
}
else
{
if(slope_iter%2 == 0)
{
//cout << "LINE 988, cardinal direction, slope iter = " << slope_iter << endl;
slope = TopoRaster.RasterData[row][col]-target_elev;
}
else
{
slope = one_ov_root2*(TopoRaster.RasterData[row][col]-target_elev);
}
if (slope > max_slope)
{
max_slope_index = slope_iter;
receive_row = row_kernal[slope_iter];
receive_col = col_kernal[slope_iter];
max_slope = slope;
if(slope_iter%2 == 0)
{
FlowLengthCode[row][col] = 1;
}
else
{
FlowLengthCode[row][col] = 2;
}
}
}
}
}
// get reciever index
FlowDirection[row][col] = max_slope_index;
ReceiverVector.push_back(NodeIndex[receive_row][receive_col]);
} // end if baselevel boundary conditional
// if the node is a base level node, add it to the base level node list
if (FlowLengthCode[row][col] == 0)
{
BaseLevelNodeList.push_back(NodeIndex[row][col]);
}
} // end if there is data conditional
} // end col loop
} // end row loop
//cout << "LINE 1015, NDataNodes: " << NDataNodes
// << " and size reciever: " << ReceiverVector.size() << endl;
//ofstream receiver_out;
//receiver_out.open("receiver_out.txt");
//cout << "LINE 414 LSDFlow info, writing receiver nodes to file" << endl;
//for (int i =0; i<int(ReceiverVector.size()); i++)
//{
// receiver_out << ReceiverVector[i] << endl;
//}
//receiver_out.close();
// first create the number of donors vector
// from braun and willett eq. 5
for(int i = 0; i<NDataNodes; i++)
{
NDonorsVector[ ReceiverVector[i] ]++;
}
// now create the delta vector
// this starts on the last element and works its way backwards
// from Braun and Willett eq 7 and 8
DeltaVector[NDataNodes] = NDataNodes;
for(int i = NDataNodes; i>0; i--)
{
DeltaVector[i-1] = DeltaVector[i] - NDonorsVector[i-1];
}
// now the DonorStack and the r vectors. These come from Braun and Willett
// equation 9.
// Note that in the manscript I have there is a typo in eqaution 9
// (Jean Braun's code is correct)
// it should be w_{r_i} = w_{r_i}+1
int r_index;
int w_index;
int delta_index;
for (int i = 0; i<NDataNodes; i++)
{
r_index = ReceiverVector[i];
delta_index = DeltaVector[ r_index ];
w_index = w_vector[ r_index ];
DonorStackVector[ delta_index+w_index ] = i;
w_vector[r_index] += 1;
//cout << "i: " << i << " r_i: " << r_index << " delta_i: " << delta_index << " w_index: " << w_index << endl;
}
// now go through the base level node list, building the drainage tree for each of these nodes as one goes along
int n_base_level_nodes;
n_base_level_nodes = BaseLevelNodeList.size();
int k;
int j_index;
int begin_delta_index, end_delta_index;
int l_index;
j_index = 0;
for (int i = 0; i<n_base_level_nodes; i++)
{
k = BaseLevelNodeList[i]; // set k to the base level node
// This doesn't seem to be in Braun and Willet but to get the ordering correct you
// need to make sure that the base level node appears first in the donorstack
// of nodes contributing to the baselevel node.
// For example, if base level node is 4, with 4 donors
// and the donor stack has 3 4 8 9
// the code has to put the 4 first.
if (DonorStackVector[ DeltaVector[k] ] != k)
{
int this_index = DonorStackVector[ DeltaVector[k] ];
int bs_node = k;
for(int ds_node = 1; ds_node < NDonorsVector[k]; ds_node++)
{
if( DonorStackVector[ DeltaVector[k] + ds_node ] == bs_node )
{
DonorStackVector[ DeltaVector[k] ] = k;
DonorStackVector[ DeltaVector[k] + ds_node ] = this_index;
}
}
}
// now run recursive algorithm
begin_delta_index = DeltaVector[k];
end_delta_index = DeltaVector[k+1];
//cout << "base_level_node is: " << k << " begin_index: " << begin_delta_index << " end: " << end_delta_index << endl;
for (int delta_index = begin_delta_index; delta_index<end_delta_index; delta_index++)
{
l_index = DonorStackVector[delta_index];
add_to_stack(l_index, j_index, k);
}
}
// now calcualte the indices
calculate_upslope_reference_indices();
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// recursive add_to_stack routine, from Braun and Willett eq. 12 and 13
//
// SMM 01/06/2012
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDFlowInfo::add_to_stack(int lm_index, int& j_index, int bl_node)
{
//cout << "j_index: " << j_index << " and s_vec: " << lm_index << endl;
SVector[j_index] = lm_index;
BLBasinVector[j_index] = bl_node;
j_index++;
int begin_m,end_m;
int l_index;
// if donating to itself, need escape hatch
if ( lm_index == bl_node)
{
begin_m = 0;
end_m = 0;
}
else
{
begin_m = DeltaVector[lm_index];
end_m = DeltaVector[ lm_index+1];
}
//cout << "lm_index: " << lm_index << " begin_m: " << begin_m << " end m: " << end_m << endl;
for( int m_index = begin_m; m_index<end_m; m_index++)
{
//cout << "recursion, begin_m: " << begin_m << " and end_m: " << end_m << endl;
l_index = DonorStackVector[m_index];
add_to_stack(l_index, j_index, bl_node);
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// this function pickles the data from the flowInfo object into a binary format
// which can be read by the unpickle function later
// the filename DOES NOT include and extension: this is added by the
// function
//
// WARNING: These files are HUGE and testing indicates they don't save much time
//
// SMM 01/06/2012
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDFlowInfo::pickle(string filename)
{
string ext = ".FIpickle";
string hdr_ext = ".FIpickle.hdr";
string hdr_fname = filename+hdr_ext;
string data_fname = filename+ext;
ofstream header_out;
header_out.open(hdr_fname.c_str());
int contributing_nodes = int(NContributingNodes.size());
int BLNodes = int(BaseLevelNodeList.size());
// print the header file
header_out << "ncols " << NCols
<< "\nnrows " << NRows
<< "\nxllcorner " << setprecision(14) << XMinimum
<< "\nyllcorner " << setprecision(14) << YMinimum
<< "\ncellsize " << DataResolution
<< "\nNODATA_value " << NoDataValue
<< "\nNDataNodes " << NDataNodes
<< "\nNBaseLevelNodes " << BLNodes
<< "\nNContributingNodes " << contributing_nodes
<< "\nBoundaryConditions ";
for(int i = 0; i<4; i++)
{
header_out << " " << BoundaryConditions[i];
}
header_out << endl;
header_out.close();
cout << "sizes RC indices: " << RowIndex.size() << " " << ColIndex.size() << endl;
cout << "BLNL size: " << BaseLevelNodeList.size() << endl;
cout << "donors: " << NDonorsVector.size() << " Reciev: " << ReceiverVector.size() << endl;
cout << "delta: " << DeltaVector.size() << " S: " << SVector.size() << endl;
cout << "donorstack: " << DonorStackVector.size() << " BBasin: " << BLBasinVector.size() << endl;
cout << "SVectorIndex " << SVectorIndex.size() << " NContrib: " << NContributingNodes.size() << endl;
// now do the main data
ofstream data_ofs(data_fname.c_str(), ios::out | ios::binary);
int temp;
for (int i=0; i<NRows; ++i)
{
for (int j=0; j<NCols; ++j)
{
temp = NodeIndex[i][j];
data_ofs.write(reinterpret_cast<char *>(&temp),sizeof(temp));
}
}
for (int i=0; i<NRows; ++i)
{
for (int j=0; j<NCols; ++j)
{
temp = FlowDirection[i][j];
data_ofs.write(reinterpret_cast<char *>(&temp),sizeof(temp));
}
}
for (int i=0; i<NRows; ++i)
{
for (int j=0; j<NCols; ++j)
{
temp = FlowLengthCode[i][j];
data_ofs.write(reinterpret_cast<char *>(&temp),sizeof(temp));
}
}
for (int i = 0; i<NDataNodes; i++)
{
temp = RowIndex[i];
data_ofs.write(reinterpret_cast<char *>(&temp),sizeof(temp));
}
for (int i = 0; i<NDataNodes; i++)
{
temp = ColIndex[i];
data_ofs.write(reinterpret_cast<char *>(&temp),sizeof(temp));
}
for (int i = 0; i<BLNodes; i++)
{
temp = BaseLevelNodeList[i];
data_ofs.write(reinterpret_cast<char *>(&temp),sizeof(temp));
}
for (int i = 0; i<NDataNodes; i++)
{
temp = NDonorsVector[i];
data_ofs.write(reinterpret_cast<char *>(&temp),sizeof(temp));
}
for (int i = 0; i<NDataNodes; i++)
{
temp = ReceiverVector[i];
data_ofs.write(reinterpret_cast<char *>(&temp),sizeof(temp));
}
for (int i = 0; i<NDataNodes+1; i++)
{
temp = DeltaVector[i];
data_ofs.write(reinterpret_cast<char *>(&temp),sizeof(temp));
}
for (int i = 0; i<NDataNodes; i++)
{
temp = DonorStackVector[i];
data_ofs.write(reinterpret_cast<char *>(&temp),sizeof(temp));
}
for (int i = 0; i<NDataNodes; i++)
{
temp = SVector[i];
data_ofs.write(reinterpret_cast<char *>(&temp),sizeof(temp));
}
for (int i = 0; i<NDataNodes; i++)
{
temp = BLBasinVector[i];
data_ofs.write(reinterpret_cast<char *>(&temp),sizeof(temp));
}
for (int i = 0; i<NDataNodes; i++)
{
temp = SVectorIndex[i];
data_ofs.write(reinterpret_cast<char *>(&temp),sizeof(temp));
}
for (int i = 0; i<contributing_nodes; i++)
{
temp = NContributingNodes[i];
data_ofs.write(reinterpret_cast<char *>(&temp),sizeof(temp));
}
data_ofs.close();
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// this unpickles a pickled flow info object. It is folded into a create function
//
// SMM 01/06/2012
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDFlowInfo::unpickle(string filename)
{
string ext = ".FIpickle";
string hdr_ext = ".FIpickle.hdr";
string hdr_fname = filename+hdr_ext;
string data_fname = filename+ext;
ifstream header_in;
header_in.open(hdr_fname.c_str());
string temp_str;
int contributing_nodes;
vector<string> bc(4);
int BLNodes;
header_in >> temp_str >> NCols >> temp_str >> NRows >> temp_str >> XMinimum
>> temp_str >> YMinimum >> temp_str >> DataResolution
>> temp_str >> NoDataValue >> temp_str >> NDataNodes
>> temp_str >> BLNodes
>> temp_str >> contributing_nodes
>> temp_str >> bc[0] >> bc[1] >> bc[2] >> bc[3];
header_in.close();
BoundaryConditions = bc;
// now read the data, using the binary stream option
ifstream ifs_data(data_fname.c_str(), ios::in | ios::binary);
if( ifs_data.fail() )
{
cout << "\nFATAL ERROR: the data file \"" << data_fname
<< "\" doesn't exist" << endl;
exit(EXIT_FAILURE);
}
else
{
// initialze the arrays
Array2D<int> data_array(NRows,NCols,NoDataValue);
NodeIndex = data_array.copy();
FlowDirection = data_array.copy();
FlowLengthCode = data_array.copy();
vector<int> data_vector(NDataNodes,NoDataValue);
vector<int> BLvector(BLNodes,NoDataValue);
vector<int> deltaV(NDataNodes+1,NoDataValue);
vector<int> CNvec(contributing_nodes,NoDataValue);
int temp;
for (int i=0; i<NRows; ++i)
{
for (int j=0; j<NCols; ++j)
{
ifs_data.read(reinterpret_cast<char*>(&temp), sizeof(temp));
NodeIndex[i][j] =temp;
}
}
for (int i=0; i<NRows; ++i)
{
for (int j=0; j<NCols; ++j)
{
ifs_data.read(reinterpret_cast<char*>(&temp), sizeof(temp));
FlowDirection[i][j] =temp;
}
}
for (int i=0; i<NRows; ++i)
{
for (int j=0; j<NCols; ++j)
{
ifs_data.read(reinterpret_cast<char*>(&temp), sizeof(temp));
FlowLengthCode[i][j] =temp;
}
}
RowIndex = data_vector;
for (int i=0; i<NDataNodes; ++i)
{
ifs_data.read(reinterpret_cast<char*>(&temp), sizeof(temp));
RowIndex[i] =temp;
}
ColIndex = data_vector;
for (int i=0; i<NDataNodes; ++i)
{
ifs_data.read(reinterpret_cast<char*>(&temp), sizeof(temp));
ColIndex[i] =temp;
}
BaseLevelNodeList = BLvector;
for (int i=0; i<BLNodes; ++i)
{
ifs_data.read(reinterpret_cast<char*>(&temp), sizeof(temp));
BaseLevelNodeList[i] =temp;
}
NDonorsVector = data_vector;
for (int i=0; i<NDataNodes; ++i)
{
ifs_data.read(reinterpret_cast<char*>(&temp), sizeof(temp));
NDonorsVector[i] =temp;
}
ReceiverVector = data_vector;
for (int i=0; i<NDataNodes; ++i)
{
ifs_data.read(reinterpret_cast<char*>(&temp), sizeof(temp));
ReceiverVector[i] =temp;
}
DeltaVector = deltaV;
for (int i=0; i<NDataNodes+1; ++i)
{
ifs_data.read(reinterpret_cast<char*>(&temp), sizeof(temp));
DeltaVector[i] =temp;
}
DonorStackVector = data_vector;
for (int i=0; i<NDataNodes; ++i)
{
ifs_data.read(reinterpret_cast<char*>(&temp), sizeof(temp));
DonorStackVector[i] =temp;
}
SVector = data_vector;
for (int i=0; i<NDataNodes; ++i)
{
ifs_data.read(reinterpret_cast<char*>(&temp), sizeof(temp));
SVector[i] =temp;
}
BLBasinVector = data_vector;
for (int i=0; i<NDataNodes; ++i)
{
ifs_data.read(reinterpret_cast<char*>(&temp), sizeof(temp));
BLBasinVector[i] =temp;
}
SVectorIndex = data_vector;
for (int i=0; i<NDataNodes; ++i)
{
ifs_data.read(reinterpret_cast<char*>(&temp), sizeof(temp));
SVectorIndex[i] =temp;
}
NContributingNodes = CNvec;
for (int i=0; i<contributing_nodes; ++i)
{
ifs_data.read(reinterpret_cast<char*>(&temp), sizeof(temp));
NContributingNodes[i] =temp;