3 use iso_c_binding,
only : c_short, c_int, c_float, c_double, c_bool
28 integer (c_int),
allocatable ::
row2d(:,:)
29 integer (c_int),
allocatable ::
col2d(:,:)
30 integer (c_int),
allocatable ::
row1d(:)
31 integer (c_int),
allocatable ::
col1d(:)
42 integer (c_int),
parameter ::
d8_west = 16
81 integer (c_int),
intent(in) :: iteration_index
82 integer (c_int) :: cell_index
85 cell_index = iteration_index
96 integer (c_int),
intent(in) :: iteration_index
97 integer (c_int) :: target_index
115 integer (c_int),
intent(in) :: cell_index
116 integer (c_int) :: iteration_index
119 integer (c_int) :: current_cell_index
120 logical (c_bool) :: found_match
129 if ( current_cell_index == cell_index )
then
140 if ( .not. found_match ) iteration_index = cell_index
148 logical (c_bool),
intent(in) :: lactive(:,:)
149 integer (c_int),
intent(inout) :: sort_order(:)
152 integer (c_int) :: number_of_cols
153 integer (c_int) :: number_of_rows
154 integer (c_int) :: istat
155 integer (c_int) :: column_num
156 integer (c_int) :: row_num
157 integer (c_int) :: iteration_index
158 integer (c_int) :: icount
159 character (len=256) :: sbuf
161 integer (c_int) :: col_lbound, col_ubound
162 integer (c_int) :: row_lbound, row_ubound
163 integer (c_int) :: cell_index, target_index
164 integer (c_int) :: iunitnum
165 integer (c_int) :: target_row_num, target_col_num
169 col_lbound = lbound(lactive, 1)
170 col_ubound = ubound(lactive, 1)
172 row_lbound = lbound(lactive, 2)
173 row_ubound = ubound(lactive, 2)
177 number_of_cols = ubound(lactive, 1)
178 number_of_rows = ubound(lactive, 2)
180 allocate(
target_row( number_of_cols, number_of_rows ), stat=istat )
181 call assert( istat==0,
"Problem allocating memory", __file__, __line__ )
183 allocate(
target_col( number_of_cols, number_of_rows ), stat=istat )
184 call assert( istat==0,
"Problem allocating memory", __file__, __line__ )
188 call assert( istat==0,
"Problem allocating memory", __file__, __line__ )
190 allocate(
col2d( number_of_cols, number_of_rows ), stat=istat )
191 call assert( istat==0,
"Problem allocating memory", __file__, __line__ )
193 allocate(
row2d( number_of_cols, number_of_rows ), stat=istat )
194 call assert( istat==0,
"Problem allocating memory", __file__, __line__ )
196 allocate(
col1d( count( lactive) ), stat=istat )
197 call assert( istat==0,
"Problem allocating memory", __file__, __line__ )
199 allocate(
row1d( count( lactive) ), stat=istat )
200 call assert( istat==0,
"Problem allocating memory", __file__, __line__ )
203 call assert( istat==0,
"Problem allocating memory", __file__, __line__ )
206 call assert( istat==0,
"Problem allocating memory", __file__, __line__ )
208 allocate(
row_index( count( lactive) ), stat=istat )
209 call assert( istat==0,
"Problem allocating memory", __file__, __line__ )
212 call assert( istat==0,
"Problem allocating memory", __file__, __line__ )
215 call assert( istat==0,
"Problem allocating memory", __file__, __line__ )
218 call assert( istat==0,
"Problem allocating memory", __file__, __line__ )
223 call die(
"A FLOW_DIRECTION grid must be supplied in order to make use of this option.", __file__, __line__)
236 do row_num=row_lbound, row_ubound
237 do column_num=col_lbound, col_ubound
239 col2d( column_num, row_num ) = column_num
240 row2d( column_num, row_num ) = row_num
258 iostat=istat, status=
"REPLACE")
260 write(iunitnum,*)
"Sort_Order"//
tab//
"CELL_INDEX"//
tab//
"TARGET_INDEX"//
tab//
"From_COL" &
261 //
tab//
"From_ROW"//
tab//
"To_COL"//
tab//
"To_ROW"//
tab//
"D8_flowdir"//
tab &
262 //
"Num_Adjacent_Upslope_Connections"//
tab//
"Sum_of_Upslope_Contributing_Cells"
266 do iteration_index = 1, ubound(
col1d,1)
273 sort_order(iteration_index) = cell_index
276 rownum =>
row1d( cell_index ), &
277 colnum =>
col1d( cell_index ), &
281 row1d( cell_index ) ), &
283 row1d( cell_index ) ), &
285 row1d( cell_index ) ) )
287 write(sbuf,*) iteration_index,
tab, cell_index,
tab, target_index
291 if ( target_index > 0 .and. target_index <= ubound(
col1d,1) )
then
292 target_row_num =
row1d( target_index )
293 target_col_num =
col1d( target_index )
295 target_row_num = -9999
296 target_col_num = -9999
303 write(iunitnum,*) trim(sbuf)
317 integer (c_int),
intent(in) :: icol
318 integer (c_int),
intent(in) :: irow
319 integer (c_int) :: cell_index
322 integer (c_int) :: iindex
323 logical (c_bool) :: lfound
332 if(
col1d( iindex ) == icol .and.
row1d( iindex ) == irow )
then
349 logical (c_bool),
intent(in) :: lActive(:,:)
352 integer (c_int) :: row_num
353 integer (c_int) :: column_num
354 integer (c_int) :: col_lbound, col_ubound
355 integer (c_int) :: row_lbound, row_ubound
357 col_lbound = lbound(lactive, 1)
358 col_ubound = ubound(lactive, 1)
360 row_lbound = lbound(lactive, 2)
361 row_ubound = ubound(lactive, 2)
365 do row_num=row_lbound, row_ubound
366 do column_num=col_lbound, col_ubound
368 select case ( dir( column_num, row_num ) )
372 target_col(column_num, row_num) = column_num + 1
377 target_col(column_num, row_num) = column_num + 1
387 target_col(column_num, row_num) = column_num - 1
392 target_col(column_num, row_num) = column_num - 1
397 target_col(column_num, row_num) = column_num - 1
407 target_col(column_num, row_num) = column_num + 1
432 logical (c_bool),
intent(in) :: lActive(:,:)
435 integer (c_int) :: row_num
436 integer (c_int) :: column_num
437 integer (c_int) :: iColsrch
438 integer (c_int) :: iRowsrch
439 integer (c_int) :: iNumberOfChangedCells
440 integer (c_int) :: col_lbound, col_ubound
441 integer (c_int) :: row_lbound, row_ubound
442 integer (c_int) :: iUpslopeSum, iUpslopeConnections
443 logical (c_bool) :: are_there_unmarked_upslope_cells
444 logical (c_bool) :: lCircular
445 integer (c_int) :: iNumberRemaining
446 integer (c_int) :: indx, k, iCount
447 integer (c_int) :: num_cells_marked_this_iteration
448 integer (c_int) :: iPasses
449 integer (c_int) :: iPassesWithoutChange
450 type (GENERAL_GRID_T),
pointer :: pTempGrid
452 col_lbound = lbound(lactive, 1)
453 col_ubound = ubound(lactive, 1)
455 row_lbound = lbound(lactive, 2)
456 row_ubound = ubound(lactive, 2)
463 ipasseswithoutchange = 0
465 ptempgrid=>
grid_create( bnds%iNumCols, bnds%iNumRows, bnds%fX_ll, bnds%fY_ll, &
466 bnds%fX_ur, bnds%fY_ur, datatype_int )
471 inumberofchangedcells = 0_c_int
472 num_cells_marked_this_iteration = 0_c_int
473 ipasses = ipasses + 1
476 do row_num=row_lbound, row_ubound
477 do column_num=col_lbound, col_ubound
479 if ( .not. lactive(column_num, row_num) ) cycle
484 inumberofchangedcells = inumberofchangedcells + 1
485 iupslopesum = 0_c_int
486 iupslopeconnections = 0_c_int
487 are_there_unmarked_upslope_cells = false
491 local_search:
do irowsrch=max( row_num-1, row_lbound),min( row_num+1, row_ubound)
492 do icolsrch=max( column_num-1, col_lbound),min( column_num+1, col_ubound)
495 if ( (
target_col(icolsrch, irowsrch) == column_num ) &
496 .and. (
target_row(icolsrch, irowsrch) == row_num ) )
then
500 if ( .not. lactive( icolsrch, irowsrch ) ) cycle
504 if ( (
target_col( column_num, row_num ) == icolsrch ) &
505 .and. (
target_row( column_num, row_num ) == irowsrch ) ) lcircular = true
514 iupslopeconnections = iupslopeconnections + 1
522 are_there_unmarked_upslope_cells = true
535 if ( .not. are_there_unmarked_upslope_cells )
then
538 num_cells_marked_this_iteration = num_cells_marked_this_iteration + 1
562 elseif ( ipasseswithoutchange > 10 )
then
564 num_cells_marked_this_iteration = num_cells_marked_this_iteration + 1
585 if ( inumberremaining==0 )
exit main_loop
588 //
"out of ", count( lactive ),
" active cells."
591 if ( num_cells_marked_this_iteration==0 )
then
593 ipasseswithoutchange = ipasseswithoutchange + 1
601 write(*,
"(/,1x,'Summary of remaining unmarked cells')")
606 .and.
pd8_flowdir%pGrdBase%iData==k .and. lactive )
607 if( icount > 0 )
then
608 write(*,fmt=
"(3x,i8,' unmarked grid cells have flowdir value: ',i8)") icount, k
612 write(*,fmt=
"(3x,a)") repeat(
"-",60)
613 write(*,fmt=
"(3x,i8,' Total cells with nonzero flow " &
614 //
"direction values')") count(
pd8_flowdir%pGrdBase%iData > 0 )
618 "problem_gridcells.asc", ptempgrid)
626 call grid_writearcgrid(
"D8_Flow_Routing__Upslope_Contributing_Area.asc", ptempgrid)
629 call grid_writearcgrid(
"D8_Flow_Routing__Number_of_Adjacent_Upslope_Connections.asc", ptempgrid)
This module contains physical constants and convenience functions aimed at performing unit conversion...
logical(c_bool), parameter, public true
type(bounds_t), public bnds
logical(c_bool), parameter, public false
character(len=:), allocatable, public output_directory_name
integer(c_int), parameter datatype_int
Defines the DATA_CATALOG_T data type, which contains type-bound procedures to add,...
type(data_catalog_t), public dat
DAT is a global to hold data catalog entries.
subroutine, public die(smessage, smodule, iline, shints, scalledby, icalledbyline)
character(len=1), parameter, public tab
Provides support for input and output of gridded ASCII data, as well as for creation and destruction ...
subroutine, public grid_destroy(pgrd)
subroutine, public grid_writearcgrid(sfilename, pgrd)
integer(c_int), dimension(:,:), allocatable, public number_of_upslope_connections
integer(c_int), dimension(:,:), allocatable col2d
integer(c_int), dimension(:), allocatable row_index
type(data_catalog_entry_t), pointer pd8_flowdir
integer(c_int), dimension(:,:), allocatable target_col
integer(c_int) function routing_d8_get_index(icol, irow)
integer(c_int), dimension(:), allocatable column_index
integer(c_int), parameter d8_southwest
integer(c_int), dimension(:,:), allocatable, public sum_of_upslope_cells
integer(c_int), parameter, public d8_undetermined
subroutine routing_d8_determine_solution_order(lactive)
integer(c_int), parameter d8_north
integer(c_int), dimension(:), allocatable row1d
integer(c_int), dimension(:), allocatable target_index_l
elemental integer(c_int) function, public get_target_index(iteration_index)
elemental integer(c_int) function, public get_sort_order(cell_index)
integer(c_int), parameter d8_northeast
integer(c_int), dimension(:), allocatable col1d
subroutine, public routing_d8_initialize(lactive, sort_order)
integer(c_int), dimension(:), allocatable sort_order_l
elemental integer(c_int) function, public get_cell_index(iteration_index)
integer(c_int), parameter d8_south
subroutine routing_d8_assign_downstream_row_col(lactive)
integer(c_int), parameter d8_west
integer(c_int), dimension(:,:), allocatable row2d
@TODO remove redundant data elements; row1d, col1d, etc. are now also stored in the model data struct...
integer(c_int), parameter d8_northwest
integer(c_int), parameter d8_east
integer(c_int), parameter d8_southeast
logical(c_bool), dimension(:,:), allocatable is_downslope_target_marked
integer(c_int), dimension(:,:), allocatable target_row