Skip to content

Commit 42c2dbe

Browse files
Fix RESORDER initialization
Fix initialization of RESORDER to avoid invalid indices and memory corruption when using naturalized flow files from previous runs.
1 parent c16f5a3 commit 42c2dbe

1 file changed

Lines changed: 16 additions & 9 deletions

File tree

routing/model/rout.f

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ PROGRAM rout
6969
INTEGER CATCHIJ(PMAX,2,NRESER_MAX,NSTATIONS_MAX)
7070
INTEGER H(NCOL,NROW)
7171
INTEGER PI(NSTATIONS_MAX),PJ(NSTATIONS_MAX),NR(NSTATIONS_MAX),DS_RESID
72-
INTEGER PII,PJJ
72+
INTEGER PII,PJJ,II
7373
INTEGER IROW,ICOL
7474
INTEGER LP,M,Y,J,I,K,pp,uu,rr
7575
INTEGER DPREC,FINAL
@@ -350,16 +350,8 @@ PROGRAM rout
350350
c STEP 5: Making Grid UH only for Reservoir Catchments (cells contributing to reservoirs and then to station under consideration)
351351
c------------------------------------------------------------------------------------------------------------------------------------------------------------------
352352
IF ((STEPBYSTEP .AND. ((NDAY_SIM.GT.1) .OR. (COUPLER_ITERATION.GT.1))) .OR. (NF_EXIST)) THEN
353-
print*, 'Running STEPBYSTEP Version...'
354353
print*, 'No need to remake grid UH | Naturalized Flow is saved from previous run ...' ! only define name of reservoir-station (NAMERS5)
355354
D=1 ! iterate only when the cell is a reservoir
356-
DO N = 1,NO_OF_BOX(NORESERVOIRS(I),I) ! loop over cells contributing to station
357-
IF ((RESER(CATCHIJ(N,1,NORESERVOIRS(I),I),CATCHIJ(N,2,NORESERVOIRS(I),I)).GT.0) .AND. (RESER(CATCHIJ(N,1,NORESERVOIRS(I),I),CATCHIJ(N,2,NORESERVOIRS(I),I)).NE.9999)) THEN
358-
WRITE(NAMERS(I,D),*) RESER(CATCHIJ(N,1,NORESERVOIRS(I),I),CATCHIJ(N,2,NORESERVOIRS(I),I))
359-
NAMERS5(I,RESORDER(I,D)) = 'RES'//trim(adjustl(NAMERS(I,D)))//"_"//trim(adjustl(NAME5(I)))
360-
D=D+1
361-
ENDIF
362-
END DO
363355
!READ RES_DIR
364356
10 FORMAT(I4)
365357
WRITE(ITEMP, 10) I
@@ -370,6 +362,21 @@ PROGRAM rout
370362
READ(42, *) (RES_DIRECT(N,K,I), K = 1,3)
371363
ENDDO
372364
close(42)
365+
DO N = 1,NO_OF_BOX(NORESERVOIRS(I),I) ! loop over cells contributing to station
366+
IF ((RESER(CATCHIJ(N,1,NORESERVOIRS(I),I),CATCHIJ(N,2,NORESERVOIRS(I),I)).GT.0) .AND. (RESER(CATCHIJ(N,1,NORESERVOIRS(I),I),CATCHIJ(N,2,NORESERVOIRS(I),I)).NE.9999)) THEN
367+
! find the reservoir order
368+
RESORDER(I,D) = NORESERVOIRS(I)
369+
DO II = 1, (NORESERVOIRS(I)-1) ! loop over reservoirs only [NORESERVOIRS is the last element which corresponds to the station under consideration]
370+
IF (CATCHIJ(N,2,NORESERVOIRS(I),I) .EQ. RES_DIRECT(II,1,I)) THEN
371+
RESORDER(I,D) = I
372+
END IF
373+
END DO
374+
WRITE(NAMERS(I,D),*) RESER(CATCHIJ(N,1,NORESERVOIRS(I),I),CATCHIJ(N,2,NORESERVOIRS(I),I))
375+
NAMERS5(I,RESORDER(I,D)) = 'RES'//trim(adjustl(NAMERS(I,D)))//"_"//trim(adjustl(NAME5(I)))
376+
D=D+1
377+
ENDIF
378+
END DO
379+
373380
ELSE
374381
print*, 'Making Grid UH for Reservoir Catchments...'
375382
D=1 ! iterate only when the cell is a reservoir

0 commit comments

Comments
 (0)