      SUBROUTINE FLOW_DIR1(SURF,FLINDI,FLINDJ,ROWS,COLS,CMARK,SDEP,SDEN,
     &DEM,RUNOFF,SX,ICE)

      ! THIS SUBROUTINE LOCATES THE DESTINATION CELL FOR EVERY DEM CELL
      ! AND IS BASED ON LOWEST LYING NEIHBOURHOOD ALGORITHM

      ! DESCRIPTION OF VARIABLES:

      ! FLINDI: STORES COLUMN COORDINATE OF THE DESTINATION CELL.
      ! GETS MODIFIED IN THIS SUBROUTINE

      ! FLINDJ: STORES ROW COORDINATE OF THE DESTINATION CELL.
      ! GETS MODIFIED IN THIS SUBROUTINE

      ! ROWS,COLS: STORES ROWS AND COLUMNS OF THE STUDY AREA. REMAINS
      ! UNMODIFIED IN THIS SUBROUTINE

      ! RUNOFF: CONTAINS RUNOFF MELTWATER (IN METERS) FOR EVERY GRID
      ! CELL. REMAINS UNMODIFIED IN THIS SUBROUTINE

      ! SX: CONSTANT

      ! CMARK: SHOWS 1 FOR CREVASSED CELL AND 0 FOR NON CREVASSED CELL.
      ! UPDATED EVERY TIMESTEP IN THE CREV_CELLS1.F SUBROUTINE. GETS
      ! UNMODIFIED IN THIS SUBROUTINE

      ! SURF: CONTAINS SURFACE HEIGHT (IN METERS) OF EVERY GRID CELL.
      ! REMAINS UNMODIFIED IN THIS SUBROUTINE

      ! DEM: CONTAINS DEM SURFACE ELEVATION (IN METERS) FOR EVERY GRID
      ! CELL. REMAINS UNMODIFIED IN THIS SUBROUTINE

      ! SDEN: CONTAINS SNOW DENSITY (IN kg/m^3) FOR EVERY GRID CELL.
      ! REMAINS UNMODIFIED IN THIS SUBROUTINE

      ! SDEP: CONTAINS SNOW DEPTH (IN m) FOR EVERY GRID CELL.
      ! REMAINS UNMODIFIED IN THIS SUBROUTINE

      ! ICE: CONTAINS ICE THICKNESS (IN METERS) FOR EVERY GRID CELL.
      ! REMAINS UNMODIFIED IN THIS SUBROUTINE

      IMPLICIT NONE

      
      INTEGER :: SX,ROWS,COLS,FLAG_SINK(COLS,ROWS),FLINDI(COLS,ROWS),
     &FLINDJ(COLS,ROWS),I,J,NN,S,K,COUNTY,COUNTX,COUNTZ,XIND(SX),
     &YIND(SX),CMARK(COLS,ROWS),II(9),JJ(9),NUM1,TIMESTEP,XX,YY,XX1,
     &YY1,I1,N,A,B,ICLOW,NTHREADS,OMP_GET_MAX_THREADS,
     &OMP_GET_THREAD_NUM,CHUNK,ISTART,IEND,TID

      DOUBLE PRECISION :: SURF(COLS,ROWS),LAKE_DEPTH(COLS,ROWS),
     &DEM(COLS,ROWS),MU,PATHY(SX),SN_DEP(SX),SN_DEN(SX),
     &VALS(9),RUNOFF(COLS,ROWS),SLOPE,KK,V,DEL_T(SX),SUM1,
     &SDEN(COLS,ROWS),SDEP(COLS,ROWS),LOW,MELT(COLS,ROWS),
     &RAIN(COLS,ROWS),T_CB,Q,ICE(COLS,ROWS)


      LAKE_DEPTH=SURF-DEM
      MU=1.763e-3 
      TIMESTEP=86400

      ISTART=2
      IEND=COLS-1

!$OMP PARALLEL
!$OMP& DEFAULT(PRIVATE)
!$OMP& SHARED(FLINDI,FLINDJ,SDEN,SDEP,SURF,LAKE_DEPTH,DEM,
!$OMP& RUNOFF,MU,COLS,ROWS,NTHREADS,CMARK,ICE)
!$    NTHREADS=OMP_GET_MAX_THREADS()
!$    TID=OMP_GET_THREAD_NUM()
!$    CHUNK=(COLS+NTHREADS-1)/NTHREADS
!$    ISTART=TID*CHUNK+1
!$    IEND=MIN((TID+1)*CHUNK,COLS-1)      

      DO 100 I=ISTART,IEND
      DO 110 J=2,ROWS-1

      NN=1
      S=I
      K=J
      COUNTY=1
      COUNTX=1
      COUNTZ=1
      PATHY=PATHY-PATHY
      SN_DEN=SN_DEN-SN_DEN
      SN_DEP=SN_DEP-SN_DEP
      XIND=XIND-XIND
      YIND=YIND-YIND

      ! FINDING THE DESTINATION FOR ALL NON-LAKE, NON-CREVASSED CELLS.
      ! ALSO FINDING THE LOCATION OF ALL SINK CELLS. A SINK CELL IS
      ! DEFINED AS A SURFACE DEPRESSION OR A LAKE CELL. A CREVASSED CELL
      ! IS NOT CLASSIFIED AS A SINK CELL.

      ! FINDING THE LOCATION OF THE PATH CELL

      IF ((CMARK(S,K).EQ.0).OR.(LAKE_DEPTH(S,K).EQ.0)) THEN

      DO WHILE ((NN.EQ.1).AND.(S.GE.2).AND.(S.LE.COLS-1).AND.
     &(K.LE.ROWS-1).AND.(CMARK(S,K).EQ.0).AND.(SURF(I,J).GT.0))
      IF (COUNTZ.EQ.1) THEN
              XIND(COUNTX)=S
              YIND(COUNTX)=K
              SN_DEP(COUNTX)=SDEP(S,K)
              SN_DEN(COUNTX)=SDEN(S,K)
              COUNTX=COUNTX+1
              COUNTZ=COUNTZ+1
      END IF
      II=(/S-1,S-1,S-1,S,S,S,S+1,S+1,S+1/)
      JJ=(/K-1,K,K+1,K-1,K,K+1,K-1,K,K+1/)

      DO 120 N=1,9
      A=II(N)
      B=JJ(N)
      VALS(N)=SURF(A,B)
120   CONTINUE
      ICLOW=0
      LOW=999999

      DO 130 N=1,9
      IF (VALS(N).LT.LOW) THEN
              ICLOW=N
              LOW=VALS(N)
      END IF
130   CONTINUE
      IF (VALS(5).EQ.LOW) THEN
              NN=0
              PATHY(COUNTY)=0
              COUNTY=COUNTY+1            
      ELSE
              IF ((ABS(II(ICLOW)-S).EQ.1).AND.
     &(ABS(JJ(ICLOW)-K).EQ.1)) THEN
                      PATHY(COUNTY)=100*1.414
              ELSE
                      PATHY(COUNTY)=100
              END IF
              S=II(ICLOW)
              K=JJ(ICLOW)
              XIND(COUNTX)=S
              YIND(COUNTX)=K
              SN_DEP(COUNTX)=SDEP(S,K)
              SN_DEN(COUNTX)=SDEN(S,K)
              COUNTX=COUNTX+1
              COUNTY=COUNTY+1
              NN=1
      END IF
      END DO

      ELSE
      PATHY(1)=0
      XIND(1)=S
      YIND(1)=K
      END IF

      ! FINDING THE LOCATION OF DESTINATION BASED ON TOTAL DELAY TIME

      NUM1=COUNT(XIND.NE.0)

      IF (NUM1.GT.0) THEN
              NUM1=COUNT(XIND.NE.0) ! TOTAL NUMBER OF PATH CELLS
              DO 140 I1=1,NUM1-1
              XX=XIND(I1)
              YY=YIND(I1)
              XX1=XIND(I1+1)
              YY1=YIND(I1+1)
              SLOPE=(SURF(XX,YY)-SURF(XX1,YY1))/PATHY(I1)
              KK=0.077*(0.001*0.001)*EXP(-7.8*(SDEN(XX,YY)/1000))
              IF (SLOPE.LT.0) SLOPE=0

              ! COLBECK FLOW

              IF (SDEP(XX,YY).GE.0.25) THEN
                      Q=(RUNOFF(XX,YY))*100*100
                      IF (I1.EQ.1) THEN
                              T_CB=(0.63*SDEP(XX,YY))/
     &(((3*1000*9.8/MU)**(1/3))*(KK**(1/3))*(Q**(2/3)))
                      ELSE
                              T_CB=0.0
                      END IF
                      V=((1000*9.8/MU)*KK*SLOPE)/0.68
              ELSE

              ! MANNING FLOW

                      T_CB=0.0
                      IF (RUNOFF(XX,YY).LE.0.034) THEN
                              V=(1/0.05)*(0.034**(2/3))*SQRT(SLOPE)
                      ELSE
                              V=(1/0.05)*(RUNOFF(XX,YY)**(2/3))
     &*SQRT(SLOPE)
                      END IF
              END IF
              DEL_T(I1)=(PATHY(I1)/V)+T_CB
              IF (V.LE.0) DEL_T(I1)=9999999
140           CONTINUE
              XX=XIND(1)
              YY=YIND(1)
              XX1=XIND(NUM1)
              YY1=YIND(NUM1)
              COUNTX=0
              SUM1=0
              DEL_T=DEL_T/86400.
              IF (SUM(DEL_T).LT.1) THEN
!$OMP CRITICAL                      
                      FLINDI(XX,YY)=XX1 ! COL NO. OF DESTINATION
                      FLINDJ(XX,YY)=YY1 ! ROW NO. OF DESTINATION
!$OMP END CRITICAL                      
              ELSE
                      DO WHILE ((SUM1.LE.1).AND.(COUNTX.LT.NUM1))
                      COUNTX=COUNTX+1
                      SUM1=SUM1+DEL_T(COUNTX)
                      END DO
!$OMP CRITICAL                      
                      FLINDI(XX,YY)=XIND(COUNTX) ! COL NO. OF DESTINATION
                      FLINDJ(XX,YY)=YIND(COUNTX) ! ROW NO. OF DESTINATION
!$OMP END CRITICAL                      
              END IF
      END IF      
      
110   CONTINUE
100   CONTINUE
!$OMP END PARALLEL

!      PRINT *,SUM(FLAG_SINK)

      RETURN

      END SUBROUTINE FLOW_DIR1
