routing_interface.f90 43.2 KB
Newer Older
1

2
SUBROUTINE initatmgrid(rank, nbcore, nbpt, nbseg, polygons_in, centre_in, area_in, contfrac_in, neighbours_in)
3 4
  !
  USE grid
5
  USE ioipsl
6 7 8 9
  !
  IMPLICIT NONE
  !
  !! INPUT VARIABLES
10 11
  INTEGER, INTENT(in) :: rank   !! rank of processor
  INTEGER, INTENT(in) :: nbcore
12 13 14 15 16 17 18 19 20 21 22
  INTEGER, INTENT(in) :: nbpt   !! Atmospheric Domain size (unitless)
  INTEGER, INTENT(in) :: nbseg  !! Number of segments in the polygone
  REAL, INTENT(in) :: polygons_in(nbpt,2*nbseg+1,2)
  REAL, INTENT(in) :: centre_in(nbpt,2)
  REAL, INTENT(in) :: contfrac_in(nbpt)
  REAL, INTENT(in) :: area_in(nbpt)
  INTEGER, INTENT(in) :: neighbours_in(nbpt,nbseg*2)
  !
  !! Local
  INTEGER :: i
  INTEGER :: neighbours_loc(nbpt,nbseg*2)
23
  CHARACTER(LEN=33) :: outfname
24 25 26 27 28 29 30 31
  !
  DO i=1,nbpt
     ! Move from C to F indexing.
     neighbours_loc(i,:) = neighbours_in(i,:)+1
  ENDDO
  !
  CALL grid_init(nbpt, nbseg, polygons_in, centre_in, area_in, contfrac_in, neighbours_loc)
  !
32 33 34 35
  numout=100+rank
  WRITE(outfname,'(A15,I4.4,A6,I4.4,A4)') "Out_RoutingReg_",rank,"_from_",nbcore,".txt"
  OPEN(unit=numout, file=outfname)
  !
36 37
END SUBROUTINE initatmgrid

38 39 40 41 42 43 44 45
SUBROUTINE closeinterface()
  !
  USE ioipsl
  !
  CLOSE(unit=numout)
  !
END SUBROUTINE closeinterface

46
SUBROUTINE gethydrogrid(nbpt, nbvmax_in, ijdimmax, sub_pts, sub_index, sub_area,  max_basins, min_topoind, &
47 48
     & lon_rel, lat_rel, trip, basins, topoindex, fac, hierarchy,orog,floodp, nbi, nbj, area_bx, trip_bx, basin_bx, &
     & topoind_bx, fac_bx, hierarchy_bx, orog_bx, floodp_bx, lon_bx, lat_bx, lshead_bx)
49 50 51 52 53 54 55 56
  !
  USE ioipsl
  USE grid
  USE routing_tools
  USE routing_reg
  !
  IMPLICIT NONE
  !
57
  INTEGER, INTENT(in) :: nbvmax_in, ijdimmax
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
  !
  INTEGER, INTENT(in) :: nbpt !! Domain size (unitless)
  INTEGER, INTENT(in) :: sub_pts(nbpt) !! Number of high resolution points on this grid (unitless)
  INTEGER, INTENT(in) :: sub_index(nbpt,nbvmax_in,2) !! Indices of the points we need on the fine grid (unitless)
  REAL, INTENT(inout) :: max_basins !! The current maximum of basins
  REAL, INTENT(in) :: min_topoind !! The current minimum of topographic index (m)
  REAL, INTENT(in) :: sub_area(nbpt,nbvmax_in) !! Area on the fine grid (m^2)
  !
  REAL, INTENT(in) :: lon_rel(nbpt,nbvmax_in) !!
  REAL, INTENT(in) :: lat_rel(nbpt,nbvmax_in) !! coordinates of the fine grid
  !
  REAL, INTENT(inout) :: trip(nbpt,nbvmax_in) !! The trip field (unitless)
  REAL, INTENT(inout) :: basins(nbpt,nbvmax_in) !! data on the fine grid
  REAL, INTENT(inout) :: topoindex(nbpt,nbvmax_in) !! Topographic index of the residence time (m)
  REAL, INTENT(inout) :: hierarchy(nbpt,nbvmax_in) !! data on the fine grid
  REAL, INTENT(inout) :: fac(nbpt,nbvmax_in) !! data on the fine grid
74 75
  REAL, INTENT(inout) :: orog(nbpt,nbvmax_in) !! data on the fine grid
  REAL, INTENT(inout) :: floodp(nbpt,nbvmax_in) !! data on the fine grid
76 77 78
  !
  !! OUTPUT VARIABLES
  INTEGER, INTENT(out) :: nbi(nbpt), nbj(nbpt) !! Number of point in x and y within the grid (unitless)
79 80 81 82 83 84 85 86 87 88 89
  REAL, INTENT(out) :: area_bx(nbpt,ijdimmax,ijdimmax) !! Area of each small box in the grid box (m^2)
  REAL, INTENT(out) :: hierarchy_bx(nbpt,ijdimmax,ijdimmax) !! Level in the basin of the point
  REAL, INTENT(out) :: fac_bx(nbpt,ijdimmax,ijdimmax) !! Flow accumulation
  REAL, INTENT(out) :: lon_bx(nbpt,ijdimmax,ijdimmax) !!
  REAL, INTENT(out) :: lat_bx(nbpt,ijdimmax,ijdimmax) !!
  REAL, INTENT(out) :: lshead_bx(nbpt,ijdimmax,ijdimmax) !! Large scale heading for outflow points.
  REAL, INTENT(out) :: topoind_bx(nbpt,ijdimmax,ijdimmax) !! Topographic index of the residence time for each of the smaller boxes (m)
  INTEGER, INTENT(out) :: trip_bx(nbpt,ijdimmax,ijdimmax) !! The trip field for each of the smaller boxes (unitless)
  INTEGER, INTENT(out) :: basin_bx(nbpt,ijdimmax,ijdimmax) !!
  REAL, INTENT(out) :: orog_bx(nbpt,ijdimmax,ijdimmax) !!  Orography (m)
  REAL, INTENT(out) :: floodp_bx(nbpt,ijdimmax,ijdimmax) !! floodplains (m^2)
90 91 92
  !
  INTEGER :: ii, ib
  REAL :: resolution(nbpt,2)
93
  LOGICAL        :: debug = .FALSE.
94
  !
95 96
  ! nbvmax is still used to dimension wome variables in routing_reg.f90.
  ! It is transfered here but should be argument to the various subroutines.
97 98
  !
  nbvmax = nbvmax_in
99
  !
100
  IF ( debug ) WRITE(numout,*) "Memory Mgt getgrid : nbvmax, ijdimmax = ", nbvmax, ijdimmax
101 102 103 104 105
  !
  DO ii=1,nbpt
     resolution(ii,1) = SQRT(area(ii))
     resolution(ii,2) = SQRT(area(ii))
  ENDDO
Anthony's avatar
Anthony committed
106

107
  DO ib=1,nbpt
108
     CALL routing_reg_getgrid(nbpt, ib, ijdimmax, sub_pts, sub_index, sub_area, max_basins, min_topoind, &
109
          & lon_rel, lat_rel, lalo, resolution, contfrac, trip, basins, topoindex, fac, hierarchy, &
110 111 112
          & orog, floodp, nbi(ib), nbj(ib), area_bx(ib,:,:), trip_bx(ib,:,:), basin_bx(ib,:,:), &
          & topoind_bx(ib,:,:), fac_bx(ib,:,:), hierarchy_bx(ib,:,:),orog_bx(ib,:,:),floodp_bx(ib,:,:),&
          & lon_bx(ib,:,:), lat_bx(ib,:,:), lshead_bx(ib,:,:))
113 114 115 116 117
  ENDDO
  !
END SUBROUTINE gethydrogrid


118
SUBROUTINE findbasins(nbpt, nb_htu, nbv, ijdimmax, nbi, nbj, trip_bx, basin_bx, fac_bx, hierarchy_bx, topoind_bx, lshead_bx, &
119 120 121 122 123 124 125 126 127 128
     &                nb_basin, basin_inbxid, basin_outlet, basin_outtp, basin_sz, basin_bxout, basin_bbout, basin_pts, &
     &                basin_lshead, coast_pts, lontmp, lattmp)
  !
  USE ioipsl
  USE grid
  USE routing_tools
  USE routing_reg
  !
  ! Arguments
  !
129
  INTEGER, INTENT(in)    :: nb_htu, nbv, ijdimmax
130 131
  INTEGER, INTENT(in)    :: nbpt !! Domain size (unitless)
  INTEGER, INTENT(in)    :: nbi(nbpt), nbj(nbpt) !! Number of point in x and y within the grid (unitless)
132 133 134 135 136 137
  INTEGER, INTENT(inout) :: trip_bx(nbpt,ijdimmax,ijdimmax) !! The trip field for each of the smaller boxes (unitless)
  INTEGER, INTENT(inout) :: basin_bx(nbpt,ijdimmax,ijdimmax) !!
  REAL, INTENT(in)       :: fac_bx(nbpt,ijdimmax,ijdimmax) !! Flow accumulation
  REAL, INTENT(in)       :: hierarchy_bx(nbpt,ijdimmax,ijdimmax) !! Level in the basin of the point
  REAL, INTENT(inout)    :: topoind_bx(nbpt,ijdimmax,ijdimmax) !! Topographic index of the residence time for each of the smaller boxes (m)
  REAL, INTENT(in)       :: lshead_bx(nbpt,ijdimmax,ijdimmax) !! Large scale heading for outflow points.
138 139 140 141
  !
  !
  ! OUTPUT VARIABLES
  INTEGER, INTENT(out) :: nb_basin(nbpt)               !! Number of sub-basins (unitless)
142 143 144 145 146 147 148 149 150
  INTEGER, INTENT(out) :: basin_inbxid(nbpt,nb_htu)   !!
  REAL, INTENT(out)    :: basin_outlet(nbpt,nb_htu,2) !!
  REAL, INTENT(out)    :: basin_outtp(nbpt,nb_htu)    !!
  INTEGER, INTENT(out) :: basin_sz(nbpt,nb_htu)       !!
  INTEGER, INTENT(out) :: basin_bxout(nbpt,nb_htu)    !!
  INTEGER, INTENT(out) :: basin_bbout(nbpt,nb_htu)    !!
  INTEGER, INTENT(out) :: basin_pts(nbpt,nb_htu, nbv, 2) !!
  REAL, INTENT(out)    :: basin_lshead(nbpt,nb_htu)   !!
  INTEGER, INTENT(out) :: coast_pts(nbpt,nb_htu)      !! The coastal flow points (unitless)
151 152 153
  !
  ! For debug and get coordinate of river outlet
  !
154 155
  REAL, INTENT(in) :: lontmp(nbpt,ijdimmax,ijdimmax) !! Longitude
  REAL, INTENT(in) :: lattmp(nbpt,ijdimmax,ijdimmax) !! Latitude
156 157 158 159
  !
  ! Local
  !
  INTEGER :: ib
160
  LOGICAL        :: debug = .FALSE.
161
  !
162
  !diaglalo(1,:) = (/ 39.6791, 2.6687 /)
163
  !
164
  IF ( debug) WRITE(numout,*) "Memory Mgt findbasin : nbvmax, nb_htu, nbv = ", nbvmax, nb_htu, nbv
165

166
  DO ib=1,nbpt
167 168
     CALL routing_reg_findbasins(nb_htu, nbv, ib, nbi(ib), nbj(ib), trip_bx(ib,:,:), &
          & basin_bx(ib,:,:), fac_bx(ib,:,:), hierarchy_bx(ib,:,:), &
169 170 171 172
          & topoind_bx(ib,:,:), lshead_bx(ib,:,:), diaglalo, &
          & nb_basin(ib), basin_inbxid(ib,:), basin_outlet(ib,:,:), basin_outtp(ib,:), basin_sz(ib,:), basin_bxout(ib,:), &
          & basin_bbout(ib,:), basin_pts(ib,:,:,:), basin_lshead(ib,:), coast_pts(ib,:), lontmp(ib,:,:), lattmp(ib,:,:))
  ENDDO
173

174 175
END SUBROUTINE findbasins

176
SUBROUTINE globalize(nbpt, nb_htu, nbv, ijdimmax, area_bx, lon_bx, lat_bx, trip_bx, hierarchy_bx, orog_bx, floodp_bx, &
177
          & fac_bx, topoind_bx, &
178
          & min_topoind, nb_basin, basin_inbxid, basin_outlet, basin_outtp, basin_sz, basin_pts, basin_bxout, &
179
          & basin_bbout, lshead, coast_pts, nwbas, basin_count, basin_notrun, basin_area, basin_cg, basin_hierarchy, &
180
          & basin_orog_mean, basin_orog_min, basin_orog_max, basin_floodp, &
181 182 183 184 185 186 187 188 189 190 191 192
          & basin_fac, basin_topoind, basin_id, basin_coor, basin_type, basin_flowdir, basin_lshead, &
          & outflow_grid, outflow_basin, nbcoastal, coastal_basin)
  !
  USE ioipsl
  USE grid
  USE routing_tools
  USE routing_reg
  !
  ! Arguments
  !
  !! INPUT VARIABLES
  INTEGER(i_std), INTENT (in) :: nbpt !! Domain size (unitless)
193 194 195 196 197 198 199 200 201 202
  INTEGER(i_std), INTENT(in)  :: nbv, nb_htu, ijdimmax
  REAL(r_std), INTENT (in), DIMENSION(nbpt,ijdimmax,ijdimmax)    :: area_bx      !! Area of each small box in the grid box (m^2)
  REAL(r_std), INTENT (in), DIMENSION(nbpt,ijdimmax,ijdimmax)    :: lon_bx       !! Longitude of each small box in the grid box
  REAL(r_std), INTENT (in), DIMENSION(nbpt,ijdimmax,ijdimmax)    :: lat_bx       !! Latitude of each small box in the grid box
  INTEGER(i_std), INTENT (in), DIMENSION(nbpt,ijdimmax,ijdimmax) :: trip_bx      !! The trip field for each of the smaller boxes (unitless)
  REAL(r_std), INTENT (in), DIMENSION(nbpt,ijdimmax,ijdimmax)    :: hierarchy_bx !! Level in the basin of the point
  REAL(r_std), INTENT (in), DIMENSION(nbpt,ijdimmax,ijdimmax)    :: orog_bx      !! Orography (m)
  REAL(r_std), INTENT (in), DIMENSION(nbpt,ijdimmax,ijdimmax)    :: floodp_bx    !! Floodplains area (m^2)
  REAL(r_std), INTENT (in), DIMENSION(nbpt,ijdimmax,ijdimmax)    :: fac_bx !! Level in the basin of the point
  REAL(r_std), INTENT (in), DIMENSION(nbpt,ijdimmax,ijdimmax)    :: topoind_bx   !! Topographic index of the residence time for each of the smaller boxes (m)
203 204
  REAL(r_std), INTENT (in)                                         :: min_topoind  !! The current minimum of topographic index (m)
  INTEGER(i_std), INTENT (in), DIMENSION(nbpt)                     :: nb_basin     !! Number of sub-basins (unitless)
205 206 207 208 209 210 211 212
  INTEGER(i_std), INTENT (in), DIMENSION(nbpt,nb_htu)     :: basin_inbxid, basin_sz !! ID of basin, number of points in the basin
  REAL(r_std), INTENT (in), DIMENSION(nbpt,nb_htu,2)      :: basin_outlet !! Outlet coordinate of each subgrid basin
  REAL(r_std), INTENT (in), DIMENSION(nbpt,nb_htu)        :: basin_outtp  !!
  INTEGER(i_std), INTENT (in), DIMENSION(nbpt,nb_htu,nbv,2) :: basin_pts  !! Points in each basin
  INTEGER(i_std), INTENT (in), DIMENSION(nbpt,nb_htu)     :: basin_bxout  !! outflow direction
  INTEGER(i_std), INTENT (in), DIMENSION(nbpt,nb_htu)     :: basin_bbout  !! outflow sub-basin
  REAL(r_std), INTENT (in), DIMENSION(nbpt,nb_htu)        :: lshead       !! Large scale heading of outflow.
  INTEGER(i_std), INTENT (in), DIMENSION(nbpt,nb_htu)     :: coast_pts !! The coastal flow points (unitless)
213 214 215 216 217 218 219 220 221
  !! Output VARIABLES
  INTEGER(i_std), INTENT (in) :: nwbas !!
  INTEGER(i_std), INTENT (out), DIMENSION(nbpt)       :: basin_count         !!
  INTEGER(i_std), INTENT (out), DIMENSION(nbpt)       :: basin_notrun        !!
  INTEGER(i_std), INTENT (out), DIMENSION(nbpt,nwbas) :: basin_id      !!
  REAL(r_std), INTENT (out), DIMENSION(nbpt,nwbas,2)  :: basin_coor     !!
  REAL(r_std), INTENT (out), DIMENSION(nbpt,nwbas)    :: basin_type       !!
  INTEGER(i_std), INTENT (out), DIMENSION(nbpt,nwbas) :: basin_flowdir !! Water flow directions in the basin (unitless)
  REAL(r_std), INTENT (out), DIMENSION(nbpt,nwbas)    :: basin_area       !!
222
  REAL(r_std), INTENT (out), DIMENSION(nbpt,nwbas,2)  :: basin_cg         !! Centre of gravity of the HTU
223
  REAL(r_std), INTENT (out), DIMENSION(nbpt,nwbas)    :: basin_hierarchy  !!
224 225 226
  REAL(r_std), INTENT (out), DIMENSION(nbpt,nwbas)    :: basin_orog_mean  !!
  REAL(r_std), INTENT (out), DIMENSION(nbpt,nwbas)    :: basin_orog_min  !!
  REAL(r_std), INTENT (out), DIMENSION(nbpt,nwbas)    :: basin_orog_max  !!
227
  REAL(r_std), INTENT (out), DIMENSION(nbpt,nwbas)    :: basin_floodp  !!
228 229 230 231
  REAL(r_std), INTENT (out), DIMENSION(nbpt,nwbas)    :: basin_fac  !!
  REAL(r_std), INTENT (out), DIMENSION(nbpt,nwbas)    :: basin_topoind    !! Topographic index of the residence time for a basin (m)
  REAL(r_std), INTENT (out), DIMENSION(nbpt,nwbas)    :: basin_lshead     !! Large scale heading out of the grid box.
  INTEGER(i_std), INTENT (out), DIMENSION(nbpt,nwbas) :: outflow_grid  !! Type of outflow on the grid box (unitless)
Anthony's avatar
Anthony committed
232
  INTEGER(i_std), INTENT (out), DIMENSION(nbpt,nwbas) :: outflow_basin !!
233 234 235 236 237 238
  INTEGER(i_std), INTENT (out), DIMENSION(nbpt)       :: nbcoastal           !!
  INTEGER(i_std), INTENT (out), DIMENSION(nbpt,nwbas) :: coastal_basin !!
  !!
  INTEGER(i_std) :: ib
  !!
  DO ib=1,nbpt
239
     CALL routing_reg_globalize(nbpt, nb_htu, nbv, ib, ijdimmax, neighbours, area_bx(ib,:,:),&
240
          & lon_bx(ib,:,:), lat_bx(ib,:,:), trip_bx(ib,:,:), &
241
          & hierarchy_bx(ib,:,:), orog_bx(ib,:,:), floodp_bx(ib,:,:), fac_bx(ib,:,:), &
242 243
          & topoind_bx(ib,:,:), min_topoind, nb_basin(ib), basin_inbxid(ib,:), basin_outlet(ib,:,:), basin_outtp(ib,:), &
          & basin_sz(ib,:), basin_pts(ib,:,:,:), basin_bxout(ib,:), basin_bbout(ib,:), lshead(ib,:), coast_pts(ib,:), nwbas, &
244
          & basin_count, basin_notrun, basin_area, basin_cg, basin_hierarchy,&
245 246
          & basin_orog_mean, basin_orog_min, basin_orog_max, basin_floodp, basin_fac,&
          & basin_topoind, basin_id, basin_coor, &
247 248 249 250 251
          & basin_type, basin_flowdir, basin_lshead, outflow_grid, outflow_basin, nbcoastal, coastal_basin)
  ENDDO
  !
END SUBROUTINE globalize

252
SUBROUTINE linkup(nbpt, ijdimmax, nwbas, inflowmax, basin_count, basin_area, basin_id, basin_flowdir, &
253 254 255 256 257 258 259 260 261 262 263 264
     & basin_lshead, basin_hierarchy, basin_fac, outflow_grid, outflow_basin, inflow_number, inflow_grid, &
     & inflow_basin, nbcoastal, coastal_basin, invented_basins)
  !
  USE ioipsl
  USE grid
  USE routing_tools
  USE routing_reg
  !
  ! Arguments
  !
  !! INPUT VARIABLES
  INTEGER(i_std), INTENT (in) :: nbpt !! Domain size (unitless)
265
  INTEGER(i_std), INTENT (in) :: ijdimmax, inflowmax
266 267 268 269 270 271 272 273 274 275 276 277 278
  REAL(r_std), INTENT(in) :: invented_basins !!
  !
  INTEGER(i_std), INTENT (in) :: nwbas !!
  INTEGER(i_std), INTENT (in), DIMENSION(nbpt) :: basin_count !!
  INTEGER(i_std), INTENT (in), DIMENSION(nbpt,nwbas) :: basin_id !!
  INTEGER(i_std), INTENT (in), DIMENSION(nbpt,nwbas) :: basin_flowdir !! Water flow directions in the basin (unitless)
  REAL(r_std), INTENT (in), DIMENSION(nbpt,nwbas) :: basin_lshead  !! Large scale flow direction out of the basin.
  REAL(r_std), INTENT (in), DIMENSION(nbpt,nwbas) :: basin_area !!
  REAL(r_std), INTENT (inout), DIMENSION(nbpt,nwbas) :: basin_hierarchy !!
  REAL(r_std), INTENT (in), DIMENSION(nbpt,nwbas) :: basin_fac !!
  INTEGER(i_std), INTENT(inout), DIMENSION(nbpt,nwbas) :: outflow_grid !! Type of outflow on the grid box (unitless)
  INTEGER(i_std), INTENT(inout), DIMENSION(nbpt,nwbas) :: outflow_basin !!
  !
279 280 281
  INTEGER(i_std), INTENT(out), DIMENSION(nbpt,nwbas) :: inflow_number !!
  INTEGER(i_std), INTENT(out), DIMENSION(nbpt,nwbas,inflowmax) :: inflow_basin !!
  INTEGER(i_std), INTENT(out), DIMENSION(nbpt,nwbas,inflowmax) :: inflow_grid !!
282 283 284 285 286
  !
  INTEGER(i_std), INTENT(inout), DIMENSION(nbpt) :: nbcoastal !!
  INTEGER(i_std), INTENT(inout), DIMENSION(nbpt,nwbas) :: coastal_basin !!
  !
  !
287 288 289
  LOGICAL        :: debug = .FALSE.
  !
  IF ( debug ) WRITE(numout,*) "Memory Mgt Linkup : nbvmax, ijdimmax, nwbas, inflowmax = ", nbvmax, ijdimmax, nwbas, inflowmax
290

291
  CALL routing_reg_linkup(nbpt, neighbours, nwbas, ijdimmax, inflowmax, basin_count, basin_area, basin_id, basin_flowdir, &
292 293 294 295 296
       & basin_lshead, basin_hierarchy, basin_fac, diaglalo, outflow_grid, outflow_basin, inflow_number, inflow_grid, &
       & inflow_basin, nbcoastal, coastal_basin, invented_basins)
  !
END SUBROUTINE linkup

297
SUBROUTINE areanorm(nbpt, nwbas, basin_count, basin_area, outflow_grid, basin_area_norm)
298 299 300 301 302 303 304 305 306
  !
  USE grid
  !
  !! INPUT VARIABLES
  INTEGER(i_std), INTENT (in)                     :: nbpt !! Domain size (unitless)
  INTEGER(i_std), INTENT (in)                     :: nwbas !!
  INTEGER(i_std), DIMENSION(nbpt), INTENT(in)     :: basin_count !!
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(in)  :: basin_area !!
  !
307 308
  INTEGER(i_std), DIMENSION(nbpt,nwbas), INTENT(inout) :: outflow_grid !! Type of outflow on the grid box (unitless)
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(out)      :: basin_area_norm !!
309 310
  !
  !! LOCAL VARIABLES
311
  INTEGER(i_std) :: ib, ig
312 313 314 315 316
  REAL(r_std) :: contarea !!
  REAL(r_std) :: totbasins !!
  !
  ! Normalize the area of all basins
  !
317
  DO ig=1,nbpt
318
     !
319 320
     totbasins = SUM(basin_area(ig,1:basin_count(ig)))
     contarea = area(ig)*contfrac(ig)
321
     !
322 323
     DO ib=1,basin_count(ig)
        basin_area_norm(ig,ib) = basin_area(ig,ib)/totbasins*contarea
324 325 326 327 328
        !
        ! Simplify the outflow condition. Large rivers will be reset later in rivclassification.
        ! We set all outflow points to coastal flow. This will be adjusted later once all procs have
        ! exchanged their information. So we will have outflow_grid = -2.
        !
329 330
        IF ( outflow_grid(ig,ib) .EQ. -1) THEN
           outflow_grid(ig,ib) = -2
331 332
        ENDIF

333 334 335 336 337
     ENDDO
     !
  ENDDO
  !
END SUBROUTINE areanorm
Anthony's avatar
Anthony committed
338

339
SUBROUTINE fetch(nbpt, nwbas, nbcore, nbhalo, fhalopts, corepts, basin_count, basin_area, basin_id, basin_hierarchy, basin_fac, &
340
     & outflow_grid, outflow_basin, partial_sum, fetch_basin, outflow_uparea)
341 342 343 344 345 346 347 348 349 350 351
  !
  USE ioipsl
  USE grid
  USE routing_tools
  USE routing_reg
  !
  ! Arguments
  !
  !! INPUT VARIABLES
  INTEGER(i_std), INTENT (in) :: nbpt !! Domain size (unitless)
  INTEGER(i_std), INTENT (in) :: nwbas !!
352
  INTEGER(i_std), INTENT (in) :: nbcore
353 354
  INTEGER(i_std), INTENT (in) :: nbhalo
  INTEGER(i_std), DIMENSION(nbhalo), INTENT(in)        :: fhalopts
355
  INTEGER(i_std), DIMENSION(nbcore), INTENT(in)        :: corepts
356
  INTEGER(i_std), DIMENSION(nbpt), INTENT(in)          :: basin_count !!
357
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(in)       :: basin_area !!
358
  INTEGER(i_std), DIMENSION(nbpt,nwbas), INTENT(in)    :: basin_id !!
359 360
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(in)       :: basin_hierarchy
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(in)       :: basin_fac
361
  INTEGER(i_std), DIMENSION(nbpt,nwbas), INTENT(in)    :: outflow_grid !! Type of outflow on the grid box (unitless)
362
  INTEGER(i_std), DIMENSION(nbpt,nwbas), INTENT(in)    :: outflow_basin !!
363
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(in)       :: partial_sum
364
  !
365 366 367
  !! IN-OUTPUT VARIABLES
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(inout) :: fetch_basin !!
  ! Output
368
  REAL(r_std), DIMENSION(nbpt*nwbas), INTENT(out) :: outflow_uparea
369
  !
370 371 372 373 374 375 376 377 378 379
  !
  !! Local
  INTEGER(i_std)                                  :: ic
  INTEGER(i_std), DIMENSION(nbcore)               :: fcorepts
  !
  !
  DO ic=1,nbcore
     fcorepts(ic) = corepts(ic)+1
  ENDDO
  !
380 381
  CALL routing_reg_fetch(nbpt, area, contfrac, nwbas, nbcore,nbhalo, fhalopts, &
       & fcorepts, basin_count, basin_area, basin_id, &
382
       & basin_hierarchy, basin_fac, outflow_grid, outflow_basin, partial_sum, fetch_basin, outflow_uparea)
Anthony's avatar
Anthony committed
383

384 385 386
  !
END SUBROUTINE fetch

387 388
SUBROUTINE rivclassification(nbpt, nwbas, nbcore, corepts, basin_count, outflow_grid, outflow_basin, fetch_basin, &
     & largest_rivarea, num_largest)
389 390 391 392 393 394
  !
  USE defprec
  !
  !! INPUT VARIABLES
  INTEGER(i_std), INTENT (in) :: nbpt !! Domain size (unitless)
  INTEGER(i_std), INTENT (in) :: nwbas !!
395 396
  INTEGER(i_std), INTENT (in) :: nbcore
  INTEGER(i_std), DIMENSION(nbcore), INTENT(in)        :: corepts
397 398
  INTEGER(i_std), DIMENSION(nbpt), INTENT(in)          :: basin_count !!
  INTEGER(i_std), DIMENSION(nbpt,nwbas), INTENT(inout) :: outflow_grid
399
  INTEGER(i_std), DIMENSION(nbpt,nwbas), INTENT(inout) :: outflow_basin !!
400 401 402 403 404
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(in)       :: fetch_basin !!
  REAL(r_std), INTENT(in) :: largest_rivarea
  INTEGER(i_std), INTENT (out) :: num_largest
  !
  !! LOCAL VARIABLES
405 406 407
  INTEGER(i_std) :: i, ib, ig
  INTEGER(i_std), DIMENSION(nbcore)               :: fcorepts
  !
408
  !
409 410 411
  DO i=1,nbcore
     fcorepts(i) = corepts(i)+1
  ENDDO
412 413 414
  !
  num_largest = 0
  !
415 416 417
  ! Just a dummy use to avoid f2py warnings
  outflow_basin(1,1) = outflow_basin(1,1)
  !
418 419
  DO i=1,nbcore
     ig = fcorepts(i)
420
     !
421
     DO ib=1,basin_count(ig)
422
        !
423
        IF (outflow_grid(ig,ib) .LT. 0 .AND. outflow_grid(ig,ib) .GT. -3  .AND. fetch_basin(ig,ib) .GE. largest_rivarea) THEN
424
           num_largest = num_largest + 1
425
           outflow_grid(ig,ib) = -1
426 427 428 429 430 431 432
        ENDIF
        !
     ENDDO
  ENDDO
  !
END SUBROUTINE rivclassification

Anthony's avatar
Anthony committed
433

434
SUBROUTINE finish_truncate(nbpt, inflowmax, nbasmax, nwbas, num_largest, gridarea, cfrac, gridcenters, basin_count, &
435 436
     & basin_notrun, basin_area, basin_orog_mean, basin_orog_min, basin_orog_max, &
     & basin_floodp, basin_cg, basin_topoind, fetch_basin, &
437
     & basin_id, basin_coor, basin_type, basin_flowdir, outflow_grid, outflow_basin, &
438 439
     & inflow_number, inflow_grid, inflow_basin, routing_area, routing_orog_mean, &
     & routing_orog_min, routing_orog_max, routing_floodp, &
440 441
     & routing_cg, topo_resid, route_nbbasin, route_togrid, route_tobasin, route_nbintobas, &
     & global_basinid, route_outlet, route_type, origin_nbintobas, routing_fetch)
442 443 444 445 446 447 448 449 450 451
  !
  USE ioipsl
  USE grid
  USE routing_tools
  USE routing_reg
  !
  ! Arguments
  !
  !! INPUT VARIABLES
  INTEGER(i_std), INTENT (in) :: nbpt !! Domain size (unitless)
452
  INTEGER(i_std), INTENT (in) :: inflowmax, nbasmax
453
  INTEGER(i_std), INTENT (in) :: nwbas !!
454
  INTEGER(i_std), INTENT (in) :: num_largest
Anthony's avatar
Anthony committed
455 456
  REAL(r_std), DIMENSION(nbpt), INTENT(in)          :: gridarea
  REAL(r_std), DIMENSION(nbpt), INTENT(in)          :: cfrac
457
  REAL(r_std), DIMENSION(nbpt,2), INTENT(in)          :: gridcenters
458
  !
459 460 461 462 463 464 465
  INTEGER(i_std), DIMENSION(nbpt), INTENT(inout)       :: basin_count !!
  INTEGER(i_std), DIMENSION(nbpt), INTENT(inout)       :: basin_notrun !!
  INTEGER(i_std), DIMENSION(nbpt,nwbas), INTENT(inout) :: basin_id !!
  REAL(r_std), DIMENSION(nbpt,nwbas,2), INTENT(inout)  :: basin_coor !!
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(inout)    :: basin_type !!
  INTEGER(i_std), DIMENSION(nbpt,nwbas), INTENT(inout) :: basin_flowdir !! Water flow directions in the basin (unitless)
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(inout)    :: basin_area !!
466 467 468
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(inout)    :: basin_orog_mean !!
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(inout)    :: basin_orog_min !!
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(inout)    :: basin_orog_max !!
469
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(inout)    :: basin_floodp !!
470
  REAL(r_std), DIMENSION(nbpt,nwbas,2), INTENT(inout)  :: basin_cg
471 472 473 474 475 476
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(inout)    :: basin_topoind !! Topographic index of the residence time for a basin (m)
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(inout)    :: fetch_basin !!
  INTEGER(i_std), DIMENSION(nbpt,nwbas), INTENT(inout) :: outflow_grid !! Type of outflow on the grid box (unitless)
  INTEGER(i_std), DIMENSION(nbpt,nwbas), INTENT(inout) :: outflow_basin !!
  !
  !
477 478 479 480
  !
  INTEGER(i_std), DIMENSION(nbpt,nwbas), INTENT(inout)          :: inflow_number !!
  INTEGER(i_std), DIMENSION(nbpt,nwbas,inflowmax), INTENT(inout) :: inflow_basin !!
  INTEGER(i_std), DIMENSION(nbpt,nwbas,inflowmax), INTENT(inout) :: inflow_grid !!
481 482 483
  !
  ! Output variables
  !
484
  REAL(r_std), DIMENSION(nbpt,nbasmax), INTENT(out)    :: routing_area !! Surface of basin (m^2)
485 486 487
  REAL(r_std), DIMENSION(nbpt,nbasmax), INTENT(out)    :: routing_orog_mean !! Mean Orography (m)
  REAL(r_std), DIMENSION(nbpt,nbasmax), INTENT(out)    :: routing_orog_min !! Mean Orography (m)
  REAL(r_std), DIMENSION(nbpt,nbasmax), INTENT(out)    :: routing_orog_max !! Mean Orography (m)
488
  REAL(r_std), DIMENSION(nbpt,nbasmax), INTENT(out)    :: routing_floodp !! Surface of Floodplains (m^2)
489
  REAL(r_std), DIMENSION(nbpt,nbasmax), INTENT(out)    :: routing_fetch !! Upstream are flowing into HTU (m^2)
490 491
  REAL(r_std), DIMENSION(nbpt,nbasmax,2), INTENT(out)  :: routing_cg !! Centre of gravity of HTU (Lat, Lon)
  REAL(r_std), DIMENSION(nbpt,nbasmax), INTENT(out)    :: topo_resid !! Topographic index of the retention time (m)
Anthony's avatar
Anthony committed
492
  INTEGER(i_std), DIMENSION(nbpt), INTENT(out)         :: route_nbbasin !!
493 494
  INTEGER(i_std), DIMENSION(nbpt,nbasmax), INTENT(out) :: route_togrid !! Grid into which the basin flows (unitless)
  INTEGER(i_std), DIMENSION(nbpt,nbasmax), INTENT(out) :: route_tobasin !! Basin in to which the water goes (unitless)
495 496
  INTEGER(i_std), DIMENSION(nbpt), INTENT(out)         :: route_nbintobas !! Number of basin into current one (unitless)
  INTEGER(i_std), DIMENSION(nbpt), INTENT(out)         :: origin_nbintobas !! Number of sub-grid basin into current one before truncation (unitless)
497
  INTEGER(i_std), DIMENSION(nbpt,nbasmax), INTENT(out) :: global_basinid !! ID of basin (unitless)
498 499
  REAL(r_std), DIMENSION(nbpt,nbasmax,2), INTENT(out)  :: route_outlet !! Coordinate of outlet (-)
  REAL(r_std), DIMENSION(nbpt,nbasmax), INTENT(out)    :: route_type !! Coordinate of outlet (-)
500
  !
501 502
  !
  !
Anthony's avatar
Anthony committed
503

504
  CALL routing_reg_end_truncate(nbpt, nbasmax, gridarea, cfrac, gridcenters, nwbas, inflowmax, num_largest, &
505 506
       & basin_count, basin_notrun, basin_area, basin_orog_mean, basin_orog_min, basin_orog_max, basin_floodp,&
       & basin_cg, basin_topoind, fetch_basin, basin_id, basin_coor, basin_type, basin_flowdir, &
507
       & outflow_grid, outflow_basin, inflow_number, inflow_grid, inflow_basin)
508 509

  routing_area(:,:) = routing_area_glo(:,:)
510 511 512
  routing_orog_mean(:,:) = routing_orog_mean_glo(:,:)
  routing_orog_min(:,:) = routing_orog_min_glo(:,:)
  routing_orog_max(:,:) = routing_orog_max_glo(:,:)
513
  routing_floodp(:,:) = routing_floodp_glo(:,:)
514
  routing_cg(:,:,:) = routing_cg_glo(:,:,:)
515 516
  topo_resid(:,:) = topo_resid_glo(:,:)
  route_nbbasin(:) = route_count_glo(:)
517 518
  route_togrid(:,:) = route_togrid_glo(:,:)
  route_tobasin(:,:) = route_tobasin_glo(:,:)
519 520

  routing_fetch(:,:) = route_fetch_glo(:,:)
Anthony's avatar
Anthony committed
521

522 523 524 525 526 527
  route_nbintobas(:) = route_nbintobas_glo(:)
  global_basinid(:,:) = global_basinid_glo(:,:)
  route_outlet(:,:,:) = route_outlet_glo(:,:,:)
  route_type(:,:) = route_type_glo(:,:)
  origin_nbintobas(:) = origin_nbintobas_glo(:)

528 529 530
END SUBROUTINE finish_truncate


531
SUBROUTINE finish_inflows(nbpt, nwbas, nbasmax, inf_max, basin_count, inflow_number, inflow_grid, inflow_basin,&
532 533 534 535 536 537 538 539 540 541
       & route_innum, route_ingrid, route_inbasin)
  !
  USE ioipsl
  USE grid
  USE routing_tools
  USE routing_reg
  !
  ! Arguments
  !
  INTEGER(i_std), INTENT (in) :: nbpt !! Domain size (unitless)
542
  INTEGER(i_std), INTENT (in) :: nwbas, nbasmax
543 544
  INTEGER(i_std), INTENT (in) :: inf_max !!
  !
545 546 547 548
  INTEGER(i_std), DIMENSION(nbpt), INTENT(inout)             :: basin_count !!
  INTEGER(i_std), DIMENSION(nbpt,nwbas), INTENT(in)          :: inflow_number !!
  INTEGER(i_std), DIMENSION(nbpt,nwbas,inf_max), INTENT(in)  :: inflow_basin !!
  INTEGER(i_std), DIMENSION(nbpt,nwbas,inf_max), INTENT(in)  :: inflow_grid !!
549
  !
550
  REAL(r_std), DIMENSION(nbpt,nbasmax), INTENT(out)            :: route_innum
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
  REAL(r_std), DIMENSION(nbpt,nbasmax,inf_max), INTENT(out)    :: route_ingrid
  REAL(r_std), DIMENSION(nbpt,nbasmax,inf_max), INTENT(out)    :: route_inbasin
  !
  !
  !
  ! inflow_number -> route_innum
  route_innum(:,:) = 0
  DO ig=1,nbpt
    nbas = basin_count(ig)
    route_innum(ig,:nbas) = inflow_number(ig,:nbas)
  END DO

  ! inflow_grid -> route_ingrid
  ! inflow_basin -> route_inbasin
  route_ingrid(:,:,:) = 0
  route_inbasin(:,:,:) = 0
  DO ig=1,nbpt
    nbas = basin_count(ig)
    DO ib=1,nbas
      nin = route_innum(ig,ib)
      route_ingrid(ig,ib,:nin) = inflow_grid(ig,ib,:nin)
      route_inbasin(ig,ib,:nin) = inflow_basin(ig,ib,:nin)
    END DO
  END DO

END SUBROUTINE finish_inflows


579
SUBROUTINE killbas(nbpt, inflowmax, nbasmax, nwbas, ops, tokill, totakeover, numops, basin_count, basin_area, &
580
     & basin_orog_mean, basin_orog_min, basin_orog_max, basin_floodp, &
581 582 583 584 585 586 587 588 589 590 591
     & basin_cg, basin_topoind, fetch_basin, basin_id, basin_coor, basin_type, basin_flowdir, outflow_grid, outflow_basin, &
     & inflow_number, inflow_grid, inflow_basin)
  !
  !
  USE ioipsl
  USE grid
  USE routing_tools
  USE routing_reg
  !
  !! INPUT VARIABLES
  INTEGER(i_std), INTENT (in) :: nbpt !! Domain size (unitless)
592
  INTEGER(i_std), INTENT (in) :: inflowmax, nbasmax
593 594 595 596 597 598 599 600 601 602 603
  INTEGER(i_std), INTENT (in) :: nwbas !!
  INTEGER(i_std), INTENT (in) :: ops !!
  INTEGER(i_std), DIMENSION(nbpt, ops), INTENT (in) :: tokill, totakeover
  INTEGER(i_std), DIMENSION(nbpt), INTENT (in) :: numops
  !
  INTEGER(i_std), DIMENSION(nbpt), INTENT(inout)       :: basin_count !!
  INTEGER(i_std), DIMENSION(nbpt,nwbas), INTENT(inout) :: basin_id !!
  REAL(r_std), DIMENSION(nbpt,nwbas,2), INTENT(inout)  :: basin_coor !!
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(inout)    :: basin_type !!
  INTEGER(i_std), DIMENSION(nbpt,nwbas), INTENT(inout) :: basin_flowdir !! Water flow directions in the basin (unitless)
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(inout)    :: basin_area !!
604 605 606 607
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(inout)    :: basin_orog_mean !!
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(inout)    :: basin_orog_min !!
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(inout)    :: basin_orog_max !!

608
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(inout)    :: basin_floodp !!
609 610 611 612 613 614
  REAL(r_std), DIMENSION(nbpt,nwbas,2), INTENT(inout)  :: basin_cg
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(inout)    :: basin_topoind !! Topographic index of the residence time for a basin (m)
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(inout)    :: fetch_basin !!
  INTEGER(i_std), DIMENSION(nbpt,nwbas), INTENT(inout) :: outflow_grid !! Type of outflow on the grid box (unitless)
  INTEGER(i_std), DIMENSION(nbpt,nwbas), INTENT(inout) :: outflow_basin !!
  !
615
  !
616
  !
617 618 619
  INTEGER(i_std), DIMENSION(nbpt,nwbas), INTENT(inout)          :: inflow_number !!
  INTEGER(i_std), DIMENSION(nbpt,nwbas,inflowmax), INTENT(inout) :: inflow_basin !!
  INTEGER(i_std), DIMENSION(nbpt,nwbas,inflowmax), INTENT(inout) :: inflow_grid !!
620 621

  !! LOCAL
622
  INTEGER(i_std) :: ib, op, tok, totak, igrif, ibasf
623 624 625 626
  LOGICAL        :: debug = .FALSE.
  !
  !
  !
627 628 629 630 631
  DO ib=1,nbpt
    DO op=1,numops(ib)
      IF (basin_count(ib) > nbasmax) THEN
         tok = tokill(ib,op)
         totak = totakeover(ib,op)
632
         IF (tok .GT. 0) THEN
633 634 635
            IF ( debug ) THEN
               WRITE(numout,*) "nbpt", ib, "tokill", tok, "totakover", totak
            ENDIF
636 637 638 639 640 641 642 643 644 645 646 647
            ! Test if tokill is downstream of totakeover (avoid loop)
            igrif = outflow_grid(ib,totak)
            ibasf = outflow_basin(ib,totak)
            DO WHILE ( igrif >0 )
              IF ((igrif .EQ. ib) .AND. (ibasf .EQ. tok)) THEN
                !CALL ipslerr_p(3,'killbas','tokill is downstream totakeover','','')
                igrif = 0
                it = totak
                totak = tok
                tok = it
              ELSE
                it = outflow_grid(igrif,ibasf)
648
                ibasf = outflow_basin(igrif,ibasf)
649 650 651
                igrif = it
              END IF
            END DO
652
            CALL routing_reg_killbas(nbpt, ib, tok, totak, nwbas, inflowmax, basin_count, basin_area, &
653
               & basin_orog_mean, basin_orog_min, basin_orog_max, basin_floodp, basin_cg, basin_topoind,&
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
               & fetch_basin, basin_id, basin_coor, basin_type, basin_flowdir, outflow_grid, outflow_basin, inflow_number,&
               & inflow_grid, inflow_basin)
         END IF
      END IF
    END DO
  END DO

END SUBROUTINE killbas


SUBROUTINE checkrouting(nbpt, nwbas, outflow_grid, outflow_basin, basin_count)
  !
  USE ioipsl
  USE grid
  USE routing_tools
  USE routing_reg
  !
  !! INPUT VARIABLES
  INTEGER(i_std), INTENT (in) :: nbpt !! Domain size (unitless)
  INTEGER(i_std), INTENT (in) :: nwbas !!

  INTEGER(i_std), DIMENSION(nbpt,nwbas), INTENT(in)       :: outflow_grid !! Type of outflow on the grid box (unitless)
  INTEGER(i_std), DIMENSION(nbpt,nwbas), INTENT(in)       :: outflow_basin !!
677

678 679 680 681
  INTEGER(i_std), DIMENSION(nbpt),       INTENT(in)       :: basin_count !!
  ! Local
  INTEGER(i_std), DIMENSION(nbpt,nwbas)       :: flag !!
  INTEGER(i_std) :: ig, ib, it, igrif, ibasf, basnum, test
682 683
  LOGICAL        :: debug = .FALSE.
  !
684
  flag(:,:) = 0
685 686
  !
  IF ( debug ) WRITE(numout,*) "Checking routing"
687 688 689 690
  test = 0

  DO ig=1,nbpt
    basnum = basin_count(ig)
691
    DO ib=1,basnum
692 693 694 695 696 697 698
      IF (flag(ig,ib) .EQ. 0) THEN
        flag(ig,ib) = -1
        igrif = outflow_grid(ig,ib)
        ibasf = outflow_basin(ig,ib)
      !
        DO WHILE ((igrif .GT. 0) .AND. (flag(igrif,ibasf) .NE. -1) .AND. (flag(igrif,ibasf) .NE. -99))
          flag(igrif, ibasf) = -1
699
          it = outflow_grid(igrif,ibasf)
700 701 702 703 704 705 706 707 708 709 710 711
          ibasf = outflow_basin(igrif,ibasf)
          igrif = it
        END DO


        IF ((flag(igrif,ibasf) .EQ. -99) .OR. (igrif .LE. 0)) THEN
          flag(ig,ib) = -99
          igrif = outflow_grid(ig,ib)
          ibasf = outflow_basin(ig,ib)

          DO WHILE ((igrif .GT. 0) .AND. (flag(igrif,ibasf) .NE. -99))
            flag(igrif, ibasf) = -99
712
            it = outflow_grid(igrif,ibasf)
713 714 715 716 717 718 719 720 721 722 723 724
            ibasf = outflow_basin(igrif,ibasf)
            igrif = it
          END DO

        ELSE IF (flag(igrif,ibasf) .EQ. -1) THEN
          test = test + 1

          flag(ig,ib) = -99
          igrif = outflow_grid(ig,ib)
          ibasf = outflow_basin(ig,ib)
          DO WHILE (flag(igrif,ibasf) .EQ. -1)
            flag(igrif, ibasf) = -99
725
            it = outflow_grid(igrif,ibasf)
726 727 728 729 730 731 732 733
            ibasf = outflow_basin(igrif,ibasf)
            igrif = it
          END DO
        END IF
      END IF
    END DO
  END DO

734
  IF ( debug ) WRITE(numout,*) "**** Fetch has",test, "loop errors"
735 736 737 738


END SUBROUTINE checkrouting

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
SUBROUTINE correct_outflows(nbpt, nwbas,nbhalo, outflow_grid, outflow_basin, &
                &basin_count, hg, hb, halopts)
  !
  USE ioipsl
  USE grid
  USE routing_tools
  USE routing_reg
  !
  !! INPUT VARIABLES
  INTEGER(i_std), INTENT (in) :: nbpt !! Domain size (unitless)
  INTEGER(i_std), INTENT (in) :: nwbas !!
  INTEGER(i_std), INTENT (in) :: nbhalo !!
  INTEGER(i_std), DIMENSION(nbhalo) :: halopts
  INTEGER(i_std), DIMENSION(nbpt,nwbas), INTENT(inout)       :: outflow_grid !! Type of outflow on the grid box (unitless)
  INTEGER(i_std), DIMENSION(nbpt,nwbas), INTENT(inout)       :: outflow_basin !!
  INTEGER(i_std), DIMENSION(nbpt), INTENT(in)       :: basin_count
  INTEGER(i_std), DIMENSION(nbpt,nwbas), INTENT(in)       :: hg !! Type of outflow on the grid box (unitless)
  INTEGER(i_std), DIMENSION(nbpt,nwbas), INTENT(in)       :: hb !!
  !! LOCAL
  INTEGER(i_std) ::ih, ig, ib, nbas

! AJOUT BASIN8COUNT
  DO ih=1,nbhalo
    ig = halopts(ih)
    nbas = basin_count(ig)
    DO ib=1,nbas
      outflow_grid(ig,ib) = hg(ig,ib)
      outflow_basin(ig,ib) = hb(ig,ib)
    END DO
  END DO
769

770 771 772 773 774 775
END SUBROUTINE correct_outflows


SUBROUTINE correct_inflows(nbpt, nwbas, inflowmax, outflow_grid,&
            & outflow_basin, basin_count, inflow_number, inflow_grid, &
            & inflow_basin)
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790

  !
  USE ioipsl
  USE grid
  USE routing_tools
  USE routing_reg
  !
  !! INPUT VARIABLES
  INTEGER(i_std), INTENT (in) :: nbpt !! Domain size (unitless)
  INTEGER(i_std), INTENT (in) :: nwbas !!
  INTEGER(i_std), INTENT (in) :: inflowmax

  INTEGER(i_std), DIMENSION(nbpt,nwbas), INTENT(in)       :: outflow_grid !! Type of outflow on the grid box (unitless)
  INTEGER(i_std), DIMENSION(nbpt,nwbas), INTENT(in)       :: outflow_basin !!
  INTEGER(i_std), DIMENSION(nbpt),       INTENT(in)       :: basin_count !!
791 792 793
  INTEGER(i_std), DIMENSION(nbpt,nwbas), INTENT(inout)          :: inflow_number
  INTEGER(i_std), DIMENSION(nbpt,nwbas,inflowmax), INTENT(inout) :: inflow_basin
  INTEGER(i_std), DIMENSION(nbpt,nwbas,inflowmax), INTENT(inout) :: inflow_grid
794 795

  ! LOCAL
796
  INTEGER(i_std) :: ig, nbas, ib, og, ob
797 798 799 800
  LOGICAL        :: debug = .FALSE.
  !
  IF ( debug ) WRITE(numout,*) "Checking if the HTUs are in the inflows of their outflow"
  !
801 802 803
  inflow_number(:,:) = 0
  inflow_basin(:,:,:)=0
  inflow_grid(:,:,:)=0
804
  !
805 806 807 808 809 810
  DO ig=1,nbpt
    nbas = basin_count(ig)
    DO ib=1,nbas
      og = outflow_grid(ig,ib)
      ob = outflow_basin(ig,ib)
      if (og .GT. 0) THEN
811 812 813 814
        inflow_number(og,ob) = inflow_number(og,ob) +1
        inflow_basin(og,ob,inflow_number(og,ob)) = ib
        inflow_grid(og,ob,inflow_number(og,ob)) = ig
      END IF
815 816 817 818 819
    END DO
  END DO



820
END SUBROUTINE correct_inflows
821

822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
SUBROUTINE checkfetch(nbpt, nwbas, fetch_basin, outflow_grid, outflow_basin, basin_count)
  !
  USE ioipsl
  USE grid
  USE routing_tools
  USE routing_reg
  !
  !! INPUT VARIABLES
  INTEGER(i_std), INTENT (in) :: nbpt !! Domain size (unitless)
  INTEGER(i_std), INTENT (in) :: nwbas !!

  REAL(r_std), DIMENSION(nbpt,nwbas),    INTENT(in)       :: fetch_basin !!
  INTEGER(i_std), DIMENSION(nbpt,nwbas), INTENT(in)       :: outflow_grid !! Type of outflow on the grid box (unitless)
  INTEGER(i_std), DIMENSION(nbpt,nwbas), INTENT(in)       :: outflow_basin !!
  INTEGER(i_std), DIMENSION(nbpt),       INTENT(in)       :: basin_count !!
  ! Local
  INTEGER(i_std) :: ig, ib, it, bt, igrif, ibasf, basnum, test
839 840 841
  LOGICAL        :: debug = .FALSE.
  !
  IF ( debug ) WRITE(numout,*) "Checking Fetch coherence"
842 843 844 845 846
  test = 0

  DO ig=1,nbpt
    basnum = basin_count(ig)

847
    DO ib=1,basnum
848 849
      igrif = outflow_grid(ig,ib)
      ibasf = outflow_basin(ig,ib)
850 851

      IF (igrif .GT. 0) THEN
852 853 854 855 856 857 858 859 860 861 862 863 864 865
        IF (fetch_basin(ig,ib) .GT. fetch_basin(igrif,ibasf)) THEN
          WRITE(numout,*) "****ERROR Fetch, nbpt:",ig
          WRITE(numout,*) igrif, ibasf
          WRITE(numout,*) it, bt
          test = 1
        END IF
      END IF
    END DO
  END DO
  IF (test .EQ. 0) WRITE(numout,*) "**** Fetch has no error"

END SUBROUTINE checkfetch


Anthony Schrapffer's avatar
Anthony Schrapffer committed
866 867
SUBROUTINE finalfetch(nbpt, nbasmax, nbcore, nbhalo, in_core, halopts,  corepts, &
     & routing_area, basin_count, route_togrid, route_tobasin, partial_sum, &
868 869 870 871
     & fetch_basin, outflow_uparea)
  !
  USE defprec
  USE ioipsl
872
  USE constantes_var
873 874 875 876 877
  !
  !! INPUT VARIABLES
  INTEGER(i_std), INTENT(in)   :: nbpt !! Domain size (unitless)
  INTEGER(i_std), INTENT(in)   :: nbasmax !!
  INTEGER(i_std), INTENT (in)  :: nbcore
Anthony Schrapffer's avatar
Anthony Schrapffer committed
878 879 880
  INTEGER(i_std), INTENT (in)  :: nbhalo
  INTEGER(i_std), DIMENSION(nbpt), INTENT(in)         :: in_core
  INTEGER(i_std), DIMENSION(nbhalo), INTENT(in)       :: halopts
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
  INTEGER(i_std), DIMENSION(nbcore), INTENT(in)       :: corepts
  INTEGER(i_std), DIMENSION(nbpt), INTENT(in)         :: basin_count
  REAL(r_std), DIMENSION(nbpt,nbasmax), INTENT(in)    :: routing_area !! Surface of basin (m^2)
  INTEGER(i_std), DIMENSION(nbpt,nbasmax), INTENT(in) :: route_togrid !! Grid into which the basin flows (unitless)
  INTEGER(i_std), DIMENSION(nbpt,nbasmax), INTENT(in) :: route_tobasin !! Basin in to which the water goes (unitless)
  REAL(r_std), DIMENSION(nbpt,nbasmax), INTENT(in)    :: partial_sum
  !
  !! INPUT/OUTPUT VARIABLES
  REAL(r_std), DIMENSION(nbpt,nbasmax), INTENT(inout)    :: fetch_basin !!
  ! Out
  REAL(r_std), DIMENSION(nbpt*nbasmax), INTENT(out)      :: outflow_uparea
  !
  ! LOCAL
  INTEGER(i_std) :: ic, ib, ig, igrif, ibasf, itt, ff(1)
  INTEGER(i_std), PARAMETER          :: maxiterations=10000
  INTEGER(i_std), DIMENSION(nbcore)  :: fcorepts
  !
  !
  DO ic=1,nbcore
     fcorepts(ic) = corepts(ic)+1
  ENDDO
  !
  ! Propagate first the partial sums from the halo into the domain
Anthony Schrapffer's avatar
Anthony Schrapffer committed
904 905
  ! We propagate the HTUs in the halo flowing into