      SUBROUTINE FILL_HALF_FULL_DEPRESSION1(ROWS,COLS,SURF,
     &DEM,RUNOFF,CMARK,NUC_I,NUC_J,SX)

      ! THIS SUBROUTINE FILLS THE PARTIALLY FILLED LAKES

      ! DESCRIPTION OF VARIABLES:

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

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

      ! SINK_I,SINK_J: 1-D ARRAY. STORES THE NUCLEI OF THE LAKES.
      ! REMAINS UNMODIFIED IN THIS SUBROUTINE

      ! SX: CONSTANT

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

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

      ! 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

      IMPLICIT NONE

      INTEGER :: SX,XXX,KKK,COLS,ROWS,I,J,NUC_I(SX),NUC_J(SX),
     &FLAG_SINK(COLS,ROWS),MARK1(COLS,ROWS),MARK2(COLS,ROWS),COUNTY,N,
     &PP,QQ,K1,K2,K3,K4,MARK(COLS,ROWS),II(9),JJ(9),INDVALX,A,B,P,
     &COUNTP,I1,J1,INDI(2),S,K,NN,COUNTZ,XIND(SX),YIND(SX),
     &XF(SX),YF(SX),CMARK(COLS,ROWS),ICLOW,KK,NUM1,
     &CATCH_FLAG(COLS,ROWS),VAL1(9),TEMP_XF,TEMP_YF,OUT_I,OUT_J,
     &MAX_LAK_EXT(COLS,ROWS),OV_LAKE(COLS,ROWS),TEMP_II,III(9),MM,I2,J2,
     &NTHREADS,CHUNK,TID,ISTART,IEND,OMP_GET_MAX_THREADS,
     &OMP_GET_THREAD_NUM,COND,TT,COUNTX,X,Y
      DOUBLE PRECISION :: LAKE_DEPTH(COLS,ROWS),SURF(COLS,ROWS),
     &DEM(COLS,ROWS),VALS(9),SURF_LAK,LAKE_VOL,DIFF_H,LOW,
     &SURF_CATCH(SX),TEMP_SURF,SURF_OUT,DEM_OUT,DUM_LAK(COLS,ROWS),
     &RUNOFF(COLS,ROWS),RUN_O_LAK(COLS,ROWS),SUMR,MAX_LAKE_VOL,
     &RUN_ON_LAKE(COLS,ROWS),RUNOFF_LAK_VOL,AV_LAKE_WATER_VOL,
     &LAKE_DEP_ISOL(COLS,ROWS),PRESENT_LAK_VOL,AV_RUNOFF_VOL,
     &MAX_LAK_AR,R1,LAK_AR,HH,X_L,FILL_VOL,ACT_LEV,N_L(SX),NN1

      XXX=COUNT(NUC_I.NE.0) ! TOTAL LAKE AND NON-LAKE CELLS
      DIFF_H=0 
      MARK1=0.
      DUM_LAK=0.
      MAX_LAK_EXT=0.
      MAX_LAKE_VOL=0

      ISTART=1
      IEND=XXX

!$OMP PARALLEL
!$OMP& DEFAULT(PRIVATE)
!$OMP& SHARED(SURF,DEM,RUNOFF,CMARK,SX,
!$OMP& XXX,NTHREADS,CHUNK,NUC_I,NUC_J,COLS,ROWS)
!$    NTHREADS=OMP_GET_MAX_THREADS()
!$    TID=OMP_GET_THREAD_NUM()
!$    CHUNK=(XXX+NTHREADS-1)/NTHREADS
!$    ISTART=TID*CHUNK+1
!$    IEND=MIN((TID+1)*CHUNK,XXX)

      DO 100 KKK=ISTART,IEND

      I=NUC_I(KKK)
      J=NUC_J(KKK)


      ! CONSIDER ALL PIXELS WHERE FLAG_SINK MATRIX IS ON AND WHERE THE
      ! LAKE DEPTH IS POSITIVE AND HAS RUNOFF

      IF ((SURF(I,J).GT.0)) THEN

!------------------------LAKE DELINEATION STARTS----------------------!

      COUNTY=0
      N=0
      PP=I
      QQ=J
      K1=0
      K2=0
      K3=0
      K4=0
      MARK=0. ! CLEAR THE LAKE EXTENT MATRIX
      LAKE_DEPTH=SURF-DEM

      COUNTY=0
      SURF_LAK=SURF(PP,QQ) ! LAKE SURFACE ELEVATION

      ! FIXING THE CORNERS OF THE SEARCH BOX FOR LAKE DELINEATION. SIZE
      ! OF THE SEARCH BOX CAN BE 120 BY 120 PIXELS CENTERED AROUND THE
      ! LAKE NUCLEUS

      P=25
      COUNTP=1
      DO WHILE ((P.GE.0).AND.(COUNTP.EQ.1))
      IF (PP-P.GE.2) THEN
              K1=P
              COUNTP=0
      ELSE
              P=P-1
              COUNTP=1
      END IF
      END DO

      P=25
      COUNTP=1
      DO WHILE ((P.GE.0).AND.(COUNTP.EQ.1))
      IF (PP+P.LE.COLS-1) THEN
              K2=P
              COUNTP=0
      ELSE
              P=P-1
              COUNTP=1
      END IF
      END DO

      P=25
      COUNTP=1
      DO WHILE ((P.GE.0).AND.(COUNTP.EQ.1))
      IF (QQ-P.GE.2) THEN
              K3=P
              COUNTP=0
      ELSE
              P=P-1
              COUNTP=1
      END IF
      END DO

      P=25
      COUNTP=1
      DO WHILE ((P.GE.0).AND.(COUNTP.EQ.1))
      IF (QQ+P.LE.ROWS-1) THEN
              K4=P
              COUNTP=0
      ELSE
              P=P-1
              COUNTP=1
      END IF
      END DO

      ! ESTIMATING THE ACTUAL LAKE EXTENT INSIDE THE SEARCH BOX AROUND
      ! THE NUCLEUS THAT WE HAD LOCATED EARLIER

      DO 120 I1=PP-K1,PP+K2
      DO 130 J1=QQ-K3,QQ+K4
      IF ((SURF(I1,J1).EQ.SURF_LAK)) THEN
              MARK(I1,J1)=1
      ELSE
              MARK(I1,J1)=0
      END IF
130   CONTINUE
120   CONTINUE
      
      INDI=MAXLOC(LAKE_DEPTH,MASK=MARK.EQ.1)
      PP=INDI(1) ! COLUMN NO. OF THE ACTUAL LAKE NUCLEUS
      QQ=INDI(2) ! ROW NO. OF THE ACTUAL LAKE NUCLEUS
      LAKE_VOL=SUM(LAKE_DEPTH,MASK=MARK.EQ.1) ! LAKE VOLUME

!--------------------LAKE DELINEATION ENDS--------------------------!

!------------------LAKE CATCHMENT DELINEATION STARTS----------------!

      ! FIXING THE CORNERS OF THE SEARCH BOX FOR CATCHMENT DELINEATION.
      ! SIZE OF THE SEARCH BOX CAN BE 200 BY 200 PIXELS CENTERED AROUND
      ! THE LAKE NUCLEUS

      P=100
      COUNTP=1
      DO WHILE ((P.GE.0).AND.(COUNTP.EQ.1))
      IF (PP-P.GE.2) THEN
              K1=P
              COUNTP=0
      ELSE
              P=P-1
              COUNTP=1
      END IF
      END DO

      P=100
      COUNTP=1
      DO WHILE ((P.GE.0).AND.(COUNTP.EQ.1))
      IF (PP+P.LE.COLS-1) THEN
              K2=P
              COUNTP=0
      ELSE
              P=P-1
              COUNTP=1
      END IF
      END DO

      P=100
      COUNTP=1
      DO WHILE ((P.GE.0).AND.(COUNTP.EQ.1))
      IF (QQ-P.GE.2) THEN
              K3=P
              COUNTP=0
      ELSE
              P=P-1
              COUNTP=1
      END IF
      END DO

      P=100
      COUNTP=1
      DO WHILE ((P.GE.0).AND.(COUNTP.EQ.1))
      IF (QQ+P.LE.ROWS-1) THEN
              K4=P
              COUNTP=0
      ELSE
              P=P-1
              COUNTP=1
      END IF
      END DO

      XIND=XIND-XIND
      YIND=YIND-YIND
      SURF_CATCH=SURF_CATCH-SURF_CATCH
      XF=XF-XF
      YF=YF-YF

      ! SEARCHING FOR CATCHMENT CELLS WITHIN THE SEARCH BOX CENTERED
      ! AROUND THE LAKE NUCLEUS. BASICALLY ALL THOSE NON-CREVASSED
      ! CELLS THAT HAVE THE LAKE AS THEIR DESTINATION ARE PART OF THE
      ! LAKE CATCHMENT

      DO 140 I1=PP-K1,PP+K2
      DO 150 J1=QQ-K3,QQ+K4

      S=I1
      K=J1
      NN=1
      COUNTY=1
      COUNTZ=1
      XIND=XIND-XIND
      YIND=YIND-YIND
      XF=XF-XF
      YF=YF-YF
      SURF_CATCH=SURF_CATCH-SURF_CATCH

      DO WHILE ((NN.EQ.1).AND.(S.LE.COLS-1).AND.(K.LE.ROWS-1).AND.
     &(S.GE.2).AND.(K.GE.2))

      IF (COUNTZ.EQ.1) THEN
              XIND(COUNTY)=S
              YIND(COUNTY)=K
              COUNTY=COUNTY+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 160 N=1,9
      A=II(N)
      B=JJ(N)
      VALS(N)=DEM(A,B)
160   CONTINUE
      ICLOW=0
      LOW=999999

      DO 170 N=1,9
      IF (VALS(N).LT.LOW) THEN
              ICLOW=N
              LOW=VALS(N)
      END IF
170   CONTINUE
      IF (VALS(5).EQ.LOW) THEN
              NN=0
              S=II(5)
              K=JJ(5)
      ELSE
              S=II(ICLOW)
              K=JJ(ICLOW)
              NN=1
              XIND(COUNTY)=S
              YIND(COUNTY)=K
              COUNTY=COUNTY+1
      END IF

      END DO

      ! THOSE CELLS THAT HAVE THEIR DESTINATION IN THE LAKE ARE PART OF
      ! THE CATCHMENT. MARK THOSE PIXELS IN THE CATCHMENT FLAG MATRIX

      IF (MARK(S,K).EQ.1) THEN
              NUM1=COUNT(XIND.NE.0)
              DO 180 KK=1,NUM1
              A=XIND(KK)
              B=YIND(KK)
              IF ((A.NE.0).AND.(B.NE.0)) THEN
                      CATCH_FLAG(A,B)=1
              ELSE
                      CATCH_FLAG(A,B)=0
              END IF
180           CONTINUE
      END IF

150   CONTINUE
140   CONTINUE

      COUNTY=1

!---------------------CATCHMENT DELINEATION ENDS--------------------!

!-----------------FILLING LARGE CATCHMENTS STARTS-------------------!
      

      ! IF A CATCHMENT GETS DELINEATED, THE NEXT STEP IS TO LOCATE THE
      ! OUTLET CELL WHICH IS GENERALLY THE LOWEST LYING CATCHMENT
      ! BOUNDARY CELL.

      ! IF THE NUMBER OF CATCHMENT CELLS ARE MORE THAN 1 (LARGE
      ! CATCHMENT)

      IF (SUM(CATCH_FLAG).GT.1) THEN

              SURF_CATCH=SURF_CATCH-SURF_CATCH
              XF=XF-XF
              YF=YF-YF

              ! ISOLATING THE BOUNDARY CATCHMENT CELLS AND SORTING THEM
              ! IN THE INCREASING ORDER OF THEIR RESPECTIVE ELEVATIONS

              DO 190 I1=PP-K1,PP+K2
              DO 200 J1=QQ-K3,QQ+K4
              IF (CATCH_FLAG(I1,J1).EQ.1) THEN
                      II=(/I1-1,I1-1,I1-1,I1,I1,I1,I1+1,I1+1,I1+1/)
                      JJ=(/J1-1,J1,J1+1,J1-1,J1,J1+1,J1-1,J1,J1+1/)
                      DO 210 N=1,9
                      A=II(N)
                      B=JJ(N)
                      VAL1(N)=CATCH_FLAG(A,B)
210                   CONTINUE
                      IF (((VAL1(1).EQ.0).OR.(VAL1(2).EQ.0).OR.
     &(VAL1(3).EQ.0)
     &.OR.(VAL1(4).EQ.0).OR.(VAL1(6).EQ.0).OR.(VAL1(7).EQ.0).OR.
     &(VAL1(8).EQ.0).OR.(VAL1(9).EQ.0)).AND.(VAL1(5).EQ.1)) THEN
                      A=II(5)
                      B=JJ(5)
                      XF(COUNTY)=A
                      YF(COUNTY)=B
                      SURF_CATCH(COUNTY)=DEM(A,B)
                      COUNTY=COUNTY+1 ! BOUNDARY CELLS OF CATCHMENT
                      END IF
              END IF
200           CONTINUE
190           CONTINUE

              ! SORTING THE ELEVATIONS OF CATCHMENT BOUNDARY CELLS

              NUM1=COUNT(SURF_CATCH.NE.0)

              DO 220 J2=0,NUM1-1
              DO 230 I2=1,NUM1-J2-1
              IF ((SURF_CATCH(I2).GT.SURF_CATCH(I2+1)).AND.
     &(SURF_CATCH(I2+1).GT.0)) THEN
              TEMP_SURF=SURF_CATCH(I2) ! SURFACE ELEVATION
              SURF_CATCH(I2)=SURF_CATCH(I2+1)
              SURF_CATCH(I2+1)=TEMP_SURF
              TEMP_XF=XF(I2) ! X COORDINATE
              XF(I2)=XF(I2+1)
              XF(I2+1)=TEMP_XF
              TEMP_YF=YF(I2) ! Y COORDINATE
              YF(I2)=YF(I2+1)
              YF(I2+1)=TEMP_YF
              END IF
230           CONTINUE
220           CONTINUE

              ! FIRST GUESS OF THE LOCATION OF OUTLET CELL

              OUT_I=XF(1)
              OUT_J=YF(1)

              A=OUT_I
              B=OUT_J

              ! IF THE OUTLET CELL IS A SINK CELL, THEN AN ALTERNATE
              ! OUTLET HAS TO BE LOCATED. THAT WORK IS DONE BY THE
              ! FOLLOWING SUBROUTINE

              IF ((A.GE.2).AND.(B.GE.2).AND.(A.LE.COLS-1).AND.
     &(B.LE.ROWS-1)) THEN

              CALL NEW_OUTLET1(XF,YF,OUT_I,OUT_J,DEM,CATCH_FLAG,ROWS,
     &COLS,SX)

              END IF             

              ! NOTING THE SURFACE AND BASE ELEVATION OF THE OUTLET CELL

              SURF_OUT=SURF(OUT_I,OUT_J)
              DEM_OUT=DEM(OUT_I,OUT_J)
              DUM_LAK=0.
              MAX_LAK_EXT=0.

              ! FILLING THE LAKE IN THE CATCHMENT TO MAXIMUM EXTENT

              DO 290 I1=PP-K1,PP+K2
              DO 300 J1=QQ-K3,QQ+K4
              IF (CATCH_FLAG(I1,J1).EQ.1) THEN
                      DUM_LAK(I1,J1)=DEM_OUT-DEM(I1,J1)
                      IF (DUM_LAK(I1,J1).GE.0) THEN
                              MAX_LAK_EXT(I1,J1)=1
                      ELSE
                              DUM_LAK(I1,J1)=0
                              MAX_LAK_EXT(I1,J1)=0
                      END IF
              ELSE
                      DUM_LAK(I1,J1)=0
              END IF
300           CONTINUE
290           CONTINUE              

              IF (SUM(MARK).GE.SUM(MAX_LAK_EXT)) THEN ! IF LAKE ALREADY FULL
                      SUMR=SUM(RUNOFF,MASK=MARK.EQ.1) ! TOTAL RUNOFF IN THE LAKE

              ELSE ! IF LAKE NOT FULL ALREADY
                      MAX_LAKE_VOL=(SUM(DUM_LAK,MASK=MAX_LAK_EXT.EQ.1)
     &)*10000 ! MAX LAKE VOLUME
                      RUNOFF_LAK_VOL=10000*(SUM(RUNOFF,MASK=MARK.EQ.1)
     &) ! AVAILABLE RUNOFF VOLUME IN THE LAKE
                      MAX_LAK_AR=
     &(SUM(MAX_LAK_EXT,MASK=MAX_LAK_EXT.EQ.1))*10000 ! MAXIMUM LAKE AREA
                      AV_LAKE_WATER_VOL=LAKE_VOL+RUNOFF_LAK_VOL ! TOTAL AVAILABLE WATER
                      DIFF_H=(MAX_LAKE_VOL-AV_LAKE_WATER_VOL)/MAX_LAK_AR
! LAKE HEIGHT TO BE DEDUCTED FROM THE MAXIMUM LAKE HEIGHT
                      IF (SUM(MAX_LAK_EXT,MASK=MAX_LAK_EXT.EQ.1).EQ.0) 
     &DIFF_H=0

                      IF (DIFF_H.GE.0) THEN ! IF LAKE CATCHMENT GETS HALF OR JUST FULL
                              ! FILLING TAKES PLACE ONLY OF NET RUNOFF
                              ! ON THE LAKE IS MORE THAN 1 MM

                              IF (SUM(RUNOFF,MASK=MARK.EQ.1).GT.0.001)
     &THEN                         
                              SUMR=SUM(RUNOFF,MASK=MARK.EQ.1)

                              ! CLEAR RUNOFF THAT FILLS THE LAKE
!$OMP CRITICAL
                              DO 330 I1=PP-K1,PP+K2
                              DO 340 J1=QQ-K3,QQ+K4
                              IF (MARK(I1,J1).EQ.1) THEN
                                      RUNOFF(I1,J1)=0
                              END IF
340                           CONTINUE
330                           CONTINUE
!$OMP END CRITICAL
                              
                              ! CATCHMENT PIXELS THAT MIGHT GET 
                              ! FILLED BY THE LAKE ARE ISOLATED

                              COUNTY=0
                              SURF_CATCH=0.
                              DO 700 I1=PP-K1,PP+K2
                              DO 701 J1=QQ-K3,QQ+K4
                              IF ((MAX_LAK_EXT(I1,J1).EQ.1)
     &.AND.(SURF(I1,J1).LE.DEM_OUT).AND.(SURF(I1,J1).GT.0)) THEN
                               COUNTY=COUNTY+1
                               SURF_CATCH(COUNTY)=SURF(I1,J1)
                              END IF
701                           CONTINUE
700                           CONTINUE

                              ! ISOLATED CATCHMENT ELEVATIONS ARRANGED
                              ! IN THE INCREASING ORDER

                              NUM1=COUNT(SURF_CATCH.NE.0)

                              DO 702 J1=0,NUM1-1
                              DO 703 I1=1,NUM1-J1-1
                              IF ((SURF_CATCH(I1).GT.SURF_CATCH(I1+1)) 
     &.AND.(SURF_CATCH(I1+1).GT.0))THEN
                                      TEMP_SURF=SURF_CATCH(I1)
                                      SURF_CATCH(I1)=SURF_CATCH(I1+1)
                                      SURF_CATCH(I1+1)=TEMP_SURF
                              END IF
703                           CONTINUE
702                           CONTINUE

                              ! FIND THE WATER SURFACE ELEVATION USING
                              ! AN ITERATIVE PROCESS

                              HH=SURF(PP,QQ) ! INITIAL GUESS OF WATER LEVEL
                              COND=1
                              

                              DO WHILE ((COND.EQ.1).AND.(HH.LT.DEM_OUT)
     &)                         
                              ! ESTIMATING ADDITIONAL WATER DEPTHS IN
                              ! THE EXISTING LAKE DUE TO EXTRA RUNOFF

                              N_L=0.
                              DO 704 I1=1,NUM1
                              IF ((HH-SURF_CATCH(I1)).GE.0) THEN
                                      N_L(I1)=HH-SURF_CATCH(I1)
                              END IF
704                           CONTINUE

                              X_L=SUM(N_L) ! TOTAL EXTRA LAKE DEPTHS
                              FILL_VOL=X_L*10000 ! VOLUME OF FILLING

                              ! IF FILLING VOLUME LESS THAN WHAT IS
                              ! AVAILABLE, INCREASE THE WATER SURFACE
                              ! ELEVATION AND CONTINUE THE PROCESS

                              IF (FILL_VOL.LT.RUNOFF_LAK_VOL) THEN
                                      IF ((SUMR.GT.0.001).AND.
     &(SUMR.LE.0.01)) THEN
                                      HH=HH+0.0001
                                      END IF

                                      IF ((SUMR.GT.0.01).AND.
     &(SUMR.LE.0.1)) THEN
                                      HH=HH+0.001
                                      END IF

                                      IF ((SUMR.GT.0.1).AND.
     &(SUMR.LE.1)) THEN
                                      HH=HH+0.01
                                      END IF

                                      IF ((SUMR.GT.1)) THEN
                                      HH=HH+0.1
                                      END IF

                                      COND=1
                              ELSE
                                      COND=0
                              END IF
                              END DO

                              SUMR=0

                              ! NEW WATER SURFACE ELEVATION FOUND

                                      IF (HH.GT.DEM_OUT) THEN 
                                              HH=DEM_OUT
                                      END IF
                                      ACT_LEV=HH

                              ! ALL THOSE CATCHMENT CELLS WITH WATER
                              ! ELEVATION LOWER THAN OR EQUAL TO
                              ! 'ACT_LEV' GET FILLED I.E. THEIR
                              ! RESPECTIVE SURFACE ELEVATIONS GET RAISED
                              ! TO ACT_LEV
!$OMP CRITICAL
                              DO 705 I1=PP-K1,PP+K2
                              DO 706 J1=QQ-K3,QQ+K4
                              IF ((SURF(I1,J1).LE.ACT_LEV).AND.
     &(MAX_LAK_EXT(I1,J1).EQ.1)) THEN
                              SURF(I1,J1)=ACT_LEV
                              MARK(I1,J1)=1
                              END IF
706                           CONTINUE
705                           CONTINUE
!$OMP END CRITICAL
                              ! CALCULATE TOTAL RUNOFF PRESENT ON THE
                              ! UPDATED LAKE'S SURFACE

                              SUMR=SUM(RUNOFF,MASK=MARK.EQ.1)

                              ! ESTIMATE UPDATED LAKE AREA

                              LAK_AR=10000*(SUM(MARK,MASK=MARK.EQ.1))

                              ! IF POSITIVE RUNOFF AND THE LAKE IS
                              ! FILLED COMPLETELY
                              ! THEN MARK THE LAKE AS
                              ! OVERFLOWING

                              END IF

                              CATCH_FLAG=0.
                              ACT_LEV=0
                              HH=0
                              N_L=0.
                              SURF_CATCH=0.

                      ELSE ! CATCHMENT IS FULL AND ABOUT TO OVERFLOW

                              ! CLEAR THE RUNOFF THAT HAS BEEN USED TO
                              ! FILL UP THE LAKE
!$OMP CRITICAL
                              DO 390 I1=PP-K1,PP+K2
                              DO 400 J1=QQ-K3,QQ+K4
                              IF (MARK(I1,J1).EQ.1) THEN
                                      RUNOFF(I1,J1)=0
                              END IF
400                           CONTINUE
390                           CONTINUE
!$OMP END CRITICAL
                              ! UPDATE THE LAKE SURFACE AND THE LAKE
                              ! EXTENT FLAG. ADDITIONALLY, ERASE THE
                              ! FLAG SINK MATRIX IN THE MAXIMUM LAKE
                              ! EXTENT
!$OMP CRITICAL
                              DO 370 I1=PP-K1,PP+K2
                              DO 380 J1=QQ-K3,QQ+K4
                              IF (MAX_LAK_EXT(I1,J1).EQ.1) THEN
                                      SURF(I1,J1)=DEM_OUT
                              END IF
380                           CONTINUE
370                           CONTINUE

                              ! SINCE ALL OF THE RUNOFF WAS NOT USED
                              ! THEREFORE, TRANSFER THE REMAINING RUNOFF
                              ! TO THE LAKE NUCLEUS

                              RUNOFF(PP,QQ)=
     &(ABS(AV_LAKE_WATER_VOL-MAX_LAKE_VOL))/10000

!$OMP END CRITICAL
                      END IF
!!$OMP END CRITICAL                      
                      CATCH_FLAG=CATCH_FLAG-CATCH_FLAG ! CLEARING THE CATCHMENT DELINEATION MATRIX
                      MAX_LAK_EXT=MAX_LAK_EXT-MAX_LAK_EXT ! CLEARING THE MAXIMUM LAKE EXTENT MATRIX
                      DUM_LAK=DUM_LAK-DUM_LAK ! CLEARING MAXIMUM LAKE DEPTH MATRIX
                      DIFF_H=DIFF_H-DIFF_H
                      MARK=MARK-MARK
                      AV_LAKE_WATER_VOL=AV_LAKE_WATER_VOL-
     &AV_LAKE_WATER_VOL
                      SURF_CATCH=SURF_CATCH-SURF_CATCH
                      XF=XF-XF
                      YF=YF-YF

              END IF
              CATCH_FLAG=CATCH_FLAG-CATCH_FLAG
              MAX_LAK_EXT=MAX_LAK_EXT-MAX_LAK_EXT
              DUM_LAK=DUM_LAK-DUM_LAK
              DIFF_H=DIFF_H-DIFF_H
              MARK=MARK-MARK
              SURF_CATCH=SURF_CATCH-SURF_CATCH
              XF=XF-XF
              YF=YF-YF
      END IF

!-----------------FILLING LARGE CATCHMENT ENDS-------------------!

!-----------------FILLING SMALL CATCHMENT STARTS-----------------!      

      ! IF THE NUMBER OF CATCHMENT CELLS IS EQUAL TO 1 (SMALL
      ! CATCHMENT)

      IF (SUM(CATCH_FLAG).EQ.1) THEN
              INDI=MAXLOC(CATCH_FLAG,MASK=CATCH_FLAG.EQ.1) ! INDICES OF THE SINGLE CATCHMENT CELL
              MM=INDI(1)
              NN=INDI(2)
              II=(/MM-1,MM-1,MM-1,MM,MM,MM,MM+1,MM+1,MM+1/)
              JJ=(/NN-1,NN,NN+1,NN-1,NN,NN+1,NN-1,NN,NN+1/)

              DO 260 N=1,9
              VALS(N)=DEM(II(N),JJ(N))
              III(N)=N
260           CONTINUE
              DO 270 J1=0,9-1
              DO 280 I1=1,9-J1-1
              IF ((VALS(I1).GT.VALS(I1+1)).AND.(VALS(I1+1).GT.0)) THEN
                      TEMP_SURF=VALS(I1)
                      VALS(I1)=VALS(I1+1)
                      VALS(I1+1)=TEMP_SURF
                      TEMP_II=III(I1)
                      III(I1)=III(I1+1)
                      III(I1+1)=TEMP_II
              END IF
280           CONTINUE
270           CONTINUE

              OUT_I=II(III(2))
              OUT_J=JJ(III(2))
              DUM_LAK=0.
              AV_RUNOFF_VOL=0.0
              MAX_LAKE_VOL=0.
              DIFF_H=0.0

              DUM_LAK(PP,QQ)=DEM(OUT_I,OUT_J)-DEM(PP,QQ) ! MAX LAKE DEPTH REACHED
              AV_RUNOFF_VOL=RUNOFF(PP,QQ)*10000 ! AVAILABLE RUNOFF VOLUME
              MAX_LAKE_VOL=DUM_LAK(PP,QQ)*10000 ! MAXIMUM LAKE VOLUME
              PRESENT_LAK_VOL=(AV_RUNOFF_VOL)+(SURF(PP,QQ)-DEM(PP,QQ))
     &*10000 ! PRESENT LAKE AND LAKE RUNOFF VOLUME

              IF (DUM_LAK(PP,QQ).EQ.(SURF(PP,QQ)-DEM(PP,QQ))) THEN

              ELSE
                      DIFF_H=(MAX_LAKE_VOL-PRESENT_LAK_VOL)/10000
!$OMP CRITICAL                      
                      IF (DIFF_H.GE.0) THEN
                              SURF(PP,QQ)=DEM(OUT_I,OUT_J)-DIFF_H
                              RUNOFF(PP,QQ)=0
                      ELSE
                              SURF(PP,QQ)=DEM(OUT_I,OUT_J)
                              RUNOFF(PP,QQ)=ABS(DIFF_H)
                      END IF
!$OMP END CRITICAL                      
                      MAX_LAK_EXT=MAX_LAK_EXT-MAX_LAK_EXT
                      CATCH_FLAG=CATCH_FLAG-CATCH_FLAG
                      DUM_LAK=DUM_LAK-DUM_LAK
                      DIFF_H=DIFF_H-DIFF_H
              END IF
      END IF

!--------------------FILLING SMALL CATCHMENT ENDS-------------------!
      END IF

100   CONTINUE
!$OMP END PARALLEL     

      RETURN

      END SUBROUTINE FILL_HALF_FULL_DEPRESSION1
