Docker-in-Docker (DinD) capabilities of public runners deactivated. More info

routing_interface.f90 50.7 KB
Newer Older
1

2
SUBROUTINE initatmgrid(rankmpi, 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
  INTEGER, INTENT(in) :: rankmpi   !! rank of processor
11
  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
  numout=100+rankmpi
33
  WRITE(outfname,'(A15,I4.4,A6,I4.4,A4)') "Out_RoutingReg_",INT(rankmpi),"_from_",INT(nbcore),".txt"
34 35
  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, maxpercent_in, sub_pts, sub_index, sub_area,  max_basins, &
47
     & min_topoind, meanrlen, meanrdz, minrdz, lon_rel, lat_rel, trip, basins, topoindex, rlen, rdz, fac, hierarchy, &
48 49
     & orog, floodp, nbi, nbj, area_bx, trip_bx, basin_bx, topoind_bx, rlen_bx, rdz_bx, rweight_bx, fac_bx, &
     & hierarchy_bx, orog_bx, floodp_bx, lon_bx, lat_bx, lshead_bx)
50 51 52 53 54 55 56 57
  !
  USE ioipsl
  USE grid
  USE routing_tools
  USE routing_reg
  !
  IMPLICIT NONE
  !
58
  INTEGER, INTENT(in) :: nbvmax_in, ijdimmax, maxpercent_in
59 60 61 62 63
  !
  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
64
  REAL, INTENT(in) :: min_topoind !! The current minimum of topographic index (km)
65
  REAL, INTENT(in) :: meanrlen, meanrdz, minrdz !! Mean length and altitude change in the original grid (m)
66 67 68 69 70 71 72 73
  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)
74 75
  REAL, INTENT(inout) :: rlen(nbpt,nbvmax_in) !!
  REAL, INTENT(inout) :: rdz(nbpt,nbvmax_in) !!
76 77
  REAL, INTENT(inout) :: hierarchy(nbpt,nbvmax_in) !! data on the fine grid
  REAL, INTENT(inout) :: fac(nbpt,nbvmax_in) !! data on the fine grid
78 79
  REAL, INTENT(inout) :: orog(nbpt,nbvmax_in) !! data on the fine grid
  REAL, INTENT(inout) :: floodp(nbpt,nbvmax_in) !! data on the fine grid
80 81 82
  !
  !! OUTPUT VARIABLES
  INTEGER, INTENT(out) :: nbi(nbpt), nbj(nbpt) !! Number of point in x and y within the grid (unitless)
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)
90 91 92
  REAL, INTENT(out) :: rlen_bx(nbpt,ijdimmax,ijdimmax) !!
  REAL, INTENT(out) :: rdz_bx(nbpt,ijdimmax,ijdimmax) !!
  REAL, INTENT(out) :: rweight_bx(nbpt,ijdimmax,ijdimmax) !!
93 94 95 96
  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)
97 98 99
  !
  INTEGER :: ii, ib
  REAL :: resolution(nbpt,2)
100
  LOGICAL        :: debug = .FALSE.
101
  !
102 103
  ! nbvmax is still used to dimension wome variables in routing_reg.f90.
  ! It is transfered here but should be argument to the various subroutines.
104 105
  !
  nbvmax = nbvmax_in
106
  maxpercent = maxpercent_in
107
  !
108
  IF ( debug ) WRITE(numout,*) "Memory Mgt getgrid : nbvmax, ijdimmax = ", nbvmax, ijdimmax
109 110 111 112 113
  !
  DO ii=1,nbpt
     resolution(ii,1) = SQRT(area(ii))
     resolution(ii,2) = SQRT(area(ii))
  ENDDO
Anthony's avatar
Anthony committed
114

115
  DO ib=1,nbpt
116
     CALL routing_reg_getgrid(nbpt, ib, ijdimmax, sub_pts, sub_index, sub_area, max_basins, min_topoind, &
117
          & meanrlen, meanrdz, minrdz, lon_rel, lat_rel, lalo, resolution, contfrac, trip, basins, topoindex, &
118 119 120 121
          & rlen, rdz, fac, hierarchy, orog, floodp, nbi(ib), nbj(ib), area_bx(ib,:,:), trip_bx(ib,:,:), &
          & basin_bx(ib,:,:), topoind_bx(ib,:,:), rlen_bx(ib,:,:), rdz_bx(ib,:,:), fac_bx(ib,:,:), &
          & hierarchy_bx(ib,:,:), orog_bx(ib,:,:), floodp_bx(ib,:,:), lon_bx(ib,:,:), lat_bx(ib,:,:), &
          & lshead_bx(ib,:,:))
122 123
  ENDDO
  !
124 125
  rweight_bx(:,:,:) = zero
  !
126 127 128
END SUBROUTINE gethydrogrid


129
SUBROUTINE findbasins(nbpt, nb_htu, nbv, ijdimmax, nbi, nbj, nbasmax, trip_bx, basin_bx, fac_bx, hierarchy_bx, topoind_bx, &
130
     &                rlen_bx, rdz_bx, rweight_bx, lshead_bx, nb_basin, basin_inbxid, basin_outlet, basin_outtp, &
131
     &                basin_sz, basin_bxout, basin_bbout, basin_pts, basin_lshead, coast_pts, lontmp, lattmp, orog_bx, &
132
     &                area_bx, basin_topoindex_stream, basin_rlen_stream, basin_dz_stream)
133 134 135 136 137 138 139 140
  !
  USE ioipsl
  USE grid
  USE routing_tools
  USE routing_reg
  !
  ! Arguments
  !
141
  INTEGER, INTENT(in)    :: nb_htu, nbv, ijdimmax
142 143
  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)
144
  INTEGER, INTENT(in)    :: nbasmax
145 146
  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) !!
147 148
  REAL, INTENT(inout)    :: fac_bx(nbpt,ijdimmax,ijdimmax) !! Flow accumulation
  REAL, INTENT(inout)    :: hierarchy_bx(nbpt,ijdimmax,ijdimmax) !! Level in the basin of the point
149
  REAL, INTENT(inout)    :: topoind_bx(nbpt,ijdimmax,ijdimmax) !! Topographic index of the residence time for each of the smaller boxes (m)
150 151 152
  REAL, INTENT(inout)    :: rlen_bx(nbpt,ijdimmax,ijdimmax) !!
  REAL, INTENT(inout)    :: rdz_bx(nbpt,ijdimmax,ijdimmax) !!
  REAL, INTENT(inout)    :: rweight_bx(nbpt,ijdimmax,ijdimmax) !! Weight of each river within the HTU
153
  REAL, INTENT(in)       :: lshead_bx(nbpt,ijdimmax,ijdimmax) !! Large scale heading for outflow points.
Anthony's avatar
Anthony committed
154
  REAL, INTENT(in)       :: orog_bx(nbpt,ijdimmax,ijdimmax) !! 
155
  REAL, INTENT(in)       :: area_bx(nbpt,ijdimmax,ijdimmax) !!
156 157 158
  !
  !
  ! OUTPUT VARIABLES
159
  INTEGER, INTENT(out) :: nb_basin(nbpt)              !! Number of sub-basins (unitless)
160 161 162 163 164 165 166 167 168
  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)
169
  REAL, INTENT(out)    :: basin_topoindex_stream(nbpt,nb_htu)   !!
170 171
  REAL, INTENT(out)    :: basin_rlen_stream(nbpt,nb_htu)   !!
  REAL, INTENT(out)    :: basin_dz_stream(nbpt,nb_htu)   !!
172 173 174
  !
  ! For debug and get coordinate of river outlet
  !
175 176
  REAL, INTENT(in) :: lontmp(nbpt,ijdimmax,ijdimmax) !! Longitude
  REAL, INTENT(in) :: lattmp(nbpt,ijdimmax,ijdimmax) !! Latitude
177 178 179 180
  !
  ! Local
  !
  INTEGER :: ib
181
  LOGICAL        :: debug = .FALSE.
182
  !
183
  !diaglalo(1,:) = (/ 39.6791, 2.6687 /)
184
  !
185
  IF ( debug) WRITE(numout,*) "Memory Mgt findbasin : nbvmax, nb_htu, nbv = ", nbvmax, nb_htu, nbv
186

187
  DO ib=1,nbpt
188
     CALL routing_reg_findbasins(nb_htu, nbv, ib, ijdimmax, nbi(ib), nbj(ib), nbasmax, trip_bx(ib,:,:), &
189 190 191
          & basin_bx(ib,:,:), fac_bx(ib,:,:), hierarchy_bx(ib,:,:), topoind_bx(ib,:,:), &
          & rlen_bx(ib,:,:), rdz_bx(ib,:,:), rweight_bx(ib,:,:), lshead_bx(ib,:,:), diaglalo, nb_basin(ib), &
          & basin_inbxid(ib,:), basin_outlet(ib,:,:), basin_outtp(ib,:), basin_sz(ib,:), basin_bxout(ib,:), &
Anthony's avatar
Anthony committed
192
          & basin_bbout(ib,:), basin_pts(ib,:,:,:), basin_lshead(ib,:), coast_pts(ib,:), lontmp(ib,:,:), lattmp(ib,:,:),&
193
          & orog_bx(ib,:,:), area_bx(ib,:,:),basin_topoindex_stream(ib,:), basin_rlen_stream(ib,:), basin_dz_stream(ib,:))
194
  ENDDO
195 196

  WRITE(numout,*) "XXXX range of nb_basin after findbasin : ", MINVAL(nb_basin), sum(nb_basin)/nbpt, MAXVAL(nb_basin)
197
  
198 199
END SUBROUTINE findbasins

200
SUBROUTINE globalize(nbpt, nb_htu, nbv, ijdimmax, area_bx, lon_bx, lat_bx, trip_bx, hierarchy_bx, orog_bx, floodp_bx, &
201
          & fac_bx, topoind_bx, rlen_bx, rdz_bx, rweight_bx, &
202
          & min_topoind, nb_basin, basin_inbxid, basin_outlet, basin_outtp, basin_sz, basin_pts, basin_bxout, &
203 204
          & basin_bbout, lshead, coast_pts, nwbas, basin_rlen_stream, basin_dz_stream, basin_count, &
          & basin_notrun, basin_area, basin_cg, basin_hierarchy, &
205 206
          & basin_orog_mean, basin_orog_min, basin_orog_max, basin_floodp, basin_fac, basin_topoind, &
          & basin_rlen, basin_rdz, basin_id, basin_coor, basin_type, basin_flowdir, basin_lshead, &
207
          & basin_beta_fp, outflow_grid, outflow_basin, nbcoastal, coastal_basin)
208 209 210 211 212 213 214 215 216 217
  !
  USE ioipsl
  USE grid
  USE routing_tools
  USE routing_reg
  !
  ! Arguments
  !
  !! INPUT VARIABLES
  INTEGER(i_std), INTENT (in) :: nbpt !! Domain size (unitless)
218 219 220 221 222 223 224 225
  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)
226 227 228 229 230 231 232
  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 (km)
  REAL(r_std), INTENT (in), DIMENSION(nbpt,ijdimmax,ijdimmax)    :: rlen_bx      !!
  REAL(r_std), INTENT (in), DIMENSION(nbpt,ijdimmax,ijdimmax)    :: rdz_bx       !!
  REAL(r_std), INTENT (in), DIMENSION(nbpt,ijdimmax,ijdimmax)    :: rweight_bx   !!
  REAL(r_std), INTENT (in)                                       :: min_topoind  !! The current minimum of topographic index (km)
  INTEGER(i_std), INTENT (in), DIMENSION(nbpt)                   :: nb_basin     !! Number of sub-basins (unitless)
233 234 235 236 237 238 239 240
  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)
241
  REAL(r_std), INTENT (in), DIMENSION(nbpt,nb_htu)        :: basin_rlen_stream, basin_dz_stream
242 243 244 245 246 247 248 249 250
  !! 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       !!
251
  REAL(r_std), INTENT (out), DIMENSION(nbpt,nwbas,2)  :: basin_cg         !! Centre of gravity of the HTU
252
  REAL(r_std), INTENT (out), DIMENSION(nbpt,nwbas)    :: basin_hierarchy  !!
253 254 255
  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  !!
256
  REAL(r_std), INTENT (out), DIMENSION(nbpt,nwbas)    :: basin_floodp  !!
257
  REAL(r_std), INTENT (out), DIMENSION(nbpt,nwbas)    :: basin_fac  !!
258 259 260
  REAL(r_std), INTENT (out), DIMENSION(nbpt,nwbas)    :: basin_topoind    !! Topographic index of the residence time for a basin (km)
  REAL(r_std), INTENT (out), DIMENSION(nbpt,nwbas)    :: basin_rlen
  REAL(r_std), INTENT (out), DIMENSION(nbpt,nwbas)    :: basin_rdz
261
  REAL(r_std), INTENT (out), DIMENSION(nbpt,nwbas)    :: basin_lshead     !! Large scale heading out of the grid box.
262
  REAL(r_std), INTENT (out), DIMENSION(nbpt,nwbas)    :: basin_beta_fp  !! Beta parameter for floodplains
263
  INTEGER(i_std), INTENT (out), DIMENSION(nbpt,nwbas) :: outflow_grid  !! Type of outflow on the grid box (unitless)
Anthony's avatar
Anthony committed
264
  INTEGER(i_std), INTENT (out), DIMENSION(nbpt,nwbas) :: outflow_basin !!
265 266 267 268 269 270
  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
271
     CALL routing_reg_globalize(nbpt, nb_htu, nbv, ib, ijdimmax, neighbours, area_bx(ib,:,:),&
272
          & lon_bx(ib,:,:), lat_bx(ib,:,:), trip_bx(ib,:,:), &
273
          & hierarchy_bx(ib,:,:), orog_bx(ib,:,:), floodp_bx(ib,:,:), fac_bx(ib,:,:), &
274 275 276 277
          & topoind_bx(ib,:,:), rlen_bx(ib,:,:), rdz_bx(ib,:,:), rweight_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, basin_count, basin_notrun, basin_area, basin_cg, basin_hierarchy,&
278
          & basin_orog_mean, basin_orog_min, basin_orog_max, basin_floodp, basin_fac,&
279 280
          & basin_topoind, basin_rlen, basin_rdz, basin_id, basin_coor, basin_type, &
          & basin_flowdir, basin_lshead, basin_beta_fp, outflow_grid, outflow_basin, nbcoastal, coastal_basin)
281 282 283 284
     ! Once all possible operation on topoindex have been performed, we keep
     ! rlen_stream and rdz_stream as rlen and rdz
     basin_rlen(ib,:) = basin_rlen_stream(ib,:nwbas)
     basin_rdz(ib,:) = basin_dz_stream(ib,:nwbas)
285 286 287 288
  ENDDO
  !
END SUBROUTINE globalize

289
SUBROUTINE linkup(nbpt, ijdimmax, nwbas, inflowmax, basin_count, basin_area, basin_id, basin_flowdir, &
290 291 292 293 294 295 296 297 298 299 300 301
     & 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)
302
  INTEGER(i_std), INTENT (in) :: ijdimmax, inflowmax
303 304 305 306 307 308 309 310 311 312 313 314 315
  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 !!
  !
316 317 318
  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 !!
319 320 321 322 323
  !
  INTEGER(i_std), INTENT(inout), DIMENSION(nbpt) :: nbcoastal !!
  INTEGER(i_std), INTENT(inout), DIMENSION(nbpt,nwbas) :: coastal_basin !!
  !
  !
324 325 326
  LOGICAL        :: debug = .FALSE.
  !
  IF ( debug ) WRITE(numout,*) "Memory Mgt Linkup : nbvmax, ijdimmax, nwbas, inflowmax = ", nbvmax, ijdimmax, nwbas, inflowmax
327

328
  CALL routing_reg_linkup(nbpt, neighbours, nwbas, ijdimmax, inflowmax, basin_count, basin_area, basin_id, basin_flowdir, &
329 330 331 332 333
       & 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

334
SUBROUTINE areanorm(nbpt, nwbas, basin_count, basin_area, outflow_grid, basin_area_norm)
335 336 337 338 339 340 341 342 343
  !
  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 !!
  !
344 345
  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 !!
346 347
  !
  !! LOCAL VARIABLES
348
  INTEGER(i_std) :: ib, ig
349 350 351 352 353
  REAL(r_std) :: contarea !!
  REAL(r_std) :: totbasins !!
  !
  ! Normalize the area of all basins
  !
354
  DO ig=1,nbpt
355
     !
356 357
     totbasins = SUM(basin_area(ig,1:basin_count(ig)))
     contarea = area(ig)*contfrac(ig)
358
     !
359 360
     DO ib=1,basin_count(ig)
        basin_area_norm(ig,ib) = basin_area(ig,ib)/totbasins*contarea
361 362 363 364 365
        !
        ! 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.
        !
366 367
        IF ( outflow_grid(ig,ib) .EQ. -1) THEN
           outflow_grid(ig,ib) = -2
368 369
        ENDIF

370 371 372 373 374
     ENDDO
     !
  ENDDO
  !
END SUBROUTINE areanorm
Anthony's avatar
Anthony committed
375

376
SUBROUTINE fetch(nbpt, nwbas, nbcore, nbhalo, fhalopts, corepts, basin_count, basin_area, basin_id, basin_hierarchy, basin_fac, &
377
     & outflow_grid, outflow_basin, partial_sum, fetch_basin, outflow_uparea)
378 379 380 381 382 383 384 385 386 387 388
  !
  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 !!
389
  INTEGER(i_std), INTENT (in) :: nbcore
390 391
  INTEGER(i_std), INTENT (in) :: nbhalo
  INTEGER(i_std), DIMENSION(nbhalo), INTENT(in)        :: fhalopts
392
  INTEGER(i_std), DIMENSION(nbcore), INTENT(in)        :: corepts
393
  INTEGER(i_std), DIMENSION(nbpt), INTENT(in)          :: basin_count !!
394
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(in)       :: basin_area !!
395
  INTEGER(i_std), DIMENSION(nbpt,nwbas), INTENT(in)    :: basin_id !!
396 397
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(in)       :: basin_hierarchy
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(in)       :: basin_fac
398
  INTEGER(i_std), DIMENSION(nbpt,nwbas), INTENT(in)    :: outflow_grid !! Type of outflow on the grid box (unitless)
399
  INTEGER(i_std), DIMENSION(nbpt,nwbas), INTENT(in)    :: outflow_basin !!
400
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(in)       :: partial_sum
401
  !
402 403 404
  !! IN-OUTPUT VARIABLES
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(inout) :: fetch_basin !!
  ! Output
405
  REAL(r_std), DIMENSION(nbpt*nwbas), INTENT(out) :: outflow_uparea
406
  !
407 408 409 410 411 412 413 414 415 416
  !
  !! Local
  INTEGER(i_std)                                  :: ic
  INTEGER(i_std), DIMENSION(nbcore)               :: fcorepts
  !
  !
  DO ic=1,nbcore
     fcorepts(ic) = corepts(ic)+1
  ENDDO
  !
417 418
  CALL routing_reg_fetch(nbpt, area, contfrac, nwbas, nbcore,nbhalo, fhalopts, &
       & fcorepts, basin_count, basin_area, basin_id, &
419
       & basin_hierarchy, basin_fac, outflow_grid, outflow_basin, partial_sum, fetch_basin, outflow_uparea)
Anthony's avatar
Anthony committed
420

421 422 423
  !
END SUBROUTINE fetch

424 425
SUBROUTINE rivclassification(nbpt, nwbas, nbcore, corepts, basin_count, outflow_grid, outflow_basin, fetch_basin, &
     & largest_rivarea, num_largest)
426 427 428 429 430 431
  !
  USE defprec
  !
  !! INPUT VARIABLES
  INTEGER(i_std), INTENT (in) :: nbpt !! Domain size (unitless)
  INTEGER(i_std), INTENT (in) :: nwbas !!
432 433
  INTEGER(i_std), INTENT (in) :: nbcore
  INTEGER(i_std), DIMENSION(nbcore), INTENT(in)        :: corepts
434 435
  INTEGER(i_std), DIMENSION(nbpt), INTENT(in)          :: basin_count !!
  INTEGER(i_std), DIMENSION(nbpt,nwbas), INTENT(inout) :: outflow_grid
436
  INTEGER(i_std), DIMENSION(nbpt,nwbas), INTENT(inout) :: outflow_basin !!
437 438 439 440 441
  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
442 443 444
  INTEGER(i_std) :: i, ib, ig
  INTEGER(i_std), DIMENSION(nbcore)               :: fcorepts
  !
445
  !
446 447 448
  DO i=1,nbcore
     fcorepts(i) = corepts(i)+1
  ENDDO
449 450 451
  !
  num_largest = 0
  !
452 453 454
  ! Just a dummy use to avoid f2py warnings
  outflow_basin(1,1) = outflow_basin(1,1)
  !
455 456
  DO i=1,nbcore
     ig = fcorepts(i)
457
     !
458
     DO ib=1,basin_count(ig)
459
        !
460
        IF (outflow_grid(ig,ib) .LT. 0 .AND. outflow_grid(ig,ib) .GT. -3  .AND. fetch_basin(ig,ib) .GE. largest_rivarea) THEN
461
           num_largest = num_largest + 1
462
           outflow_grid(ig,ib) = -1
463 464 465 466 467 468 469
        ENDIF
        !
     ENDDO
  ENDDO
  !
END SUBROUTINE rivclassification

Anthony's avatar
Anthony committed
470

471
SUBROUTINE finish_truncate(nbpt, inflowmax, nbasmax, nwbas, num_largest, gridarea, cfrac, gridcenters, basin_count, &
472
     & basin_notrun, basin_area, basin_orog_mean, basin_orog_min, basin_orog_max, &
473 474
     & basin_floodp, basin_cg, basin_topoind, basin_topoindex_stream, basin_rlen, &
     & basin_rdz, basin_beta_fp, fetch_basin, &
475
     & basin_id, basin_coor, basin_type, basin_flowdir, outflow_grid, outflow_basin, &
476
     & inflow_number, inflow_grid, inflow_basin, floodcri, routing_area, routing_orog_mean, &
477 478 479
     & routing_orog_min, routing_orog_max, routing_floodp, routing_beta, &
     & routing_cg, topo_resid, topo_resid_stream, topo_rlen, topo_rdz, & 
     & route_nbbasin, route_togrid, route_tobasin, route_nbintobas, &
480 481
     & global_basinid, route_outlet, route_type, origin_nbintobas, routing_fetch,routing_floodcri, &
     & rfillval, ifillval)
482 483 484 485 486 487 488 489 490 491
  !
  USE ioipsl
  USE grid
  USE routing_tools
  USE routing_reg
  !
  ! Arguments
  !
  !! INPUT VARIABLES
  INTEGER(i_std), INTENT (in) :: nbpt !! Domain size (unitless)
492
  INTEGER(i_std), INTENT (in) :: inflowmax, nbasmax
493
  INTEGER(i_std), INTENT (in) :: nwbas !!
494
  INTEGER(i_std), INTENT (in) :: num_largest
Anthony's avatar
Anthony committed
495 496
  REAL(r_std), DIMENSION(nbpt), INTENT(in)          :: gridarea
  REAL(r_std), DIMENSION(nbpt), INTENT(in)          :: cfrac
497
  REAL(r_std), DIMENSION(nbpt,2), INTENT(in)          :: gridcenters
498
  !
499 500 501 502 503 504 505
  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 !!
506 507 508
  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 !!
509
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(inout)    :: basin_floodp !!
510
  REAL(r_std), DIMENSION(nbpt,nwbas,2), INTENT(inout)  :: basin_cg
511
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(inout)    :: basin_topoind !! Topographic index of the residence time for a basin (km)
512
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(inout)    :: basin_topoindex_stream !! Topographic index of the residence time for a basin (km) - stream res.
513 514
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(inout)    :: basin_rlen
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(inout)    :: basin_rdz
515
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(inout)    :: basin_beta_fp
516 517 518 519
  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 !!
  !
520 521 522
  REAL(r_std), INTENT(in)    :: rfillval
  INTEGER(i_std), INTENT(in) :: ifillval
  !
523
  !
524 525 526 527
  !
  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 !!
528
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(inout)    :: floodcri !!
529 530 531
  !
  ! Output variables
  !
532
  REAL(r_std), DIMENSION(nbpt,nbasmax), INTENT(out)    :: routing_area !! Surface of basin (m^2)
533 534 535
  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)
536
  REAL(r_std), DIMENSION(nbpt,nbasmax), INTENT(out)    :: routing_floodp !! Surface of Floodplains (m^2)
537
  REAL(r_std), DIMENSION(nbpt,nbasmax), INTENT(out)    :: routing_beta
538
  REAL(r_std), DIMENSION(nbpt,nbasmax), INTENT(out)    :: routing_fetch !! Upstream are flowing into HTU (m^2)
539
  REAL(r_std), DIMENSION(nbpt,nbasmax,2), INTENT(out)  :: routing_cg !! Centre of gravity of HTU (Lat, Lon)
540
  REAL(r_std), DIMENSION(nbpt,nbasmax), INTENT(out)    :: topo_resid !! Topographic index of the retention time (km)
541
  REAL(r_std), DIMENSION(nbpt,nbasmax), INTENT(out)    :: topo_resid_stream !! Topographic index of the retention time (km)
542 543
  REAL(r_std), DIMENSION(nbpt,nbasmax), INTENT(out)    :: topo_rlen !!
  REAL(r_std), DIMENSION(nbpt,nbasmax), INTENT(out)    :: topo_rdz  !!
Anthony's avatar
Anthony committed
544
  INTEGER(i_std), DIMENSION(nbpt), INTENT(out)         :: route_nbbasin !!
545 546
  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)
547 548
  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)
549
  INTEGER(i_std), DIMENSION(nbpt,nbasmax), INTENT(out) :: global_basinid !! ID of basin (unitless)
550 551
  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 (-)
552
  REAL(r_std), DIMENSION(nbpt,nbasmax), INTENT(out)    :: routing_floodcri
553
  !
554
  !
555
  INTEGER(i_std) :: ib, ij
556
  !
557
  CALL routing_reg_end_truncate(nbpt, nbasmax, gridarea, cfrac, gridcenters, nwbas, inflowmax, num_largest, &
558
       & basin_count, basin_notrun, basin_area, basin_orog_mean, basin_orog_min, basin_orog_max, basin_floodp,&
559 560
       & basin_cg, basin_topoind, basin_topoindex_stream, basin_rlen, basin_rdz, & 
       & basin_beta_fp, fetch_basin, basin_id, basin_coor, &
561
       & basin_type, basin_flowdir, outflow_grid, outflow_basin, inflow_number, inflow_grid, inflow_basin, floodcri)
562

563 564 565 566 567 568 569
  routing_area(:,:) = rfillval
  routing_orog_mean(:,:) = rfillval
  routing_orog_min(:,:) = rfillval
  routing_orog_max(:,:) = rfillval
  routing_floodp(:,:) = rfillval
  routing_cg(:,:,:) = rfillval
  topo_resid(:,:) = rfillval
570
  topo_resid_stream(:,:) = rfillval
571 572
  topo_rlen(:,:) = rfillval
  topo_rdz(:,:) = rfillval
573 574 575 576 577 578 579 580 581 582
  route_nbbasin(:) = ifillval
  route_togrid(:,:) = ifillval
  route_tobasin(:,:) = ifillval
  routing_fetch(:,:) = rfillval
  routing_beta(:,:) = rfillval
  route_nbintobas(:) = ifillval
  global_basinid(:,:) = ifillval
  route_outlet(:,:,:) = rfillval
  route_type(:,:) = rfillval
  origin_nbintobas(:) = ifillval
583

584 585 586 587 588 589 590 591 592
  DO ij=1,nbpt
     DO ib=1,basin_count(ij)
        !
        routing_area(ij,ib) = routing_area_glo(ij,ib)
        routing_orog_mean(ij,ib) = routing_orog_mean_glo(ij,ib)
        routing_orog_min(ij,ib) = routing_orog_min_glo(ij,ib)
        routing_orog_max(ij,ib) = routing_orog_max_glo(ij,ib)
        routing_floodp(ij,ib) = routing_floodp_glo(ij,ib)
        routing_cg(ij,ib,:) = routing_cg_glo(ij,ib,:)
593
        !
594
        topo_resid(ij,ib) = topo_resid_glo(ij,ib)
595
        topo_resid_stream(ij,ib) = topo_resid_stream_glo(ij,ib)
596 597 598
        topo_rlen(ij,ib) = topo_rlen_glo(ij,ib)
        topo_rdz(ij,ib) = topo_rdz_glo(ij,ib)
        !
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
        route_nbbasin(:) = route_count_glo(:)
        route_togrid(ij,ib) = route_togrid_glo(ij,ib)
        route_tobasin(ij,ib) = route_tobasin_glo(ij,ib)
        routing_floodcri(ij,ib) = routing_floodcri_glo(ij,ib)
        routing_fetch(ij,ib) = route_fetch_glo(ij,ib)
        routing_beta(ij,ib) = routing_beta_glo(ij,ib)
        !
        route_nbintobas(ij) = route_nbintobas_glo(ij)
        global_basinid(ij,ib) = global_basinid_glo(ij,ib)
        route_outlet(ij,ib,:) = route_outlet_glo(ij,ib,:)
        route_type(ij,ib) = route_type_glo(ij,ib)
        origin_nbintobas(ij) = origin_nbintobas_glo(ij)
     ENDDO
  ENDDO
  !
614 615 616
END SUBROUTINE finish_truncate


617
SUBROUTINE finish_inflows(nbpt, nwbas, nbasmax, inf_max, basin_count, inflow_number, inflow_grid, inflow_basin,&
618 619 620 621 622 623 624 625 626 627
       & 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)
628
  INTEGER(i_std), INTENT (in) :: nwbas, nbasmax
629 630
  INTEGER(i_std), INTENT (in) :: inf_max !!
  !
631 632 633 634
  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 !!
635
  !
636
  REAL(r_std), DIMENSION(nbpt,nbasmax), INTENT(out)            :: route_innum
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
  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


665
SUBROUTINE killbas(nbpt, inflowmax, nbasmax, nwbas, ops, tokill, totakeover, numops, basin_count, basin_area, &
666
     & basin_orog_mean, basin_orog_min, basin_orog_max, basin_floodp, basin_cg, &
667 668
     & basin_topoind, basin_topoindex_stream, basin_rlen, basin_rdz, basin_beta_fp, &
     & fetch_basin, basin_id, basin_coor, basin_type, &
669
     & basin_flowdir, outflow_grid, outflow_basin, inflow_number, inflow_grid, inflow_basin)
670 671 672 673 674 675 676 677 678
  !
  !
  USE ioipsl
  USE grid
  USE routing_tools
  USE routing_reg
  !
  !! INPUT VARIABLES
  INTEGER(i_std), INTENT (in) :: nbpt !! Domain size (unitless)
679
  INTEGER(i_std), INTENT (in) :: inflowmax, nbasmax
680 681 682 683 684 685 686 687 688 689 690
  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 !!
691 692 693 694
  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 !!

695
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(inout)    :: basin_floodp !!
696
  REAL(r_std), DIMENSION(nbpt,nwbas,2), INTENT(inout)  :: basin_cg
697
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(inout)    :: basin_topoind !! Topographic index of the residence time for a basin (km)
698
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(inout)    :: basin_topoindex_stream !! Topographic index of the residence time for a basin (km)
699 700
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(inout)    :: basin_rlen
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(inout)    :: basin_rdz
701
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(inout)    :: basin_beta_fp
702 703 704 705
  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 !!
  !
706
  !
707
  !
708 709 710
  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 !!
711 712

  !! LOCAL
713
  INTEGER(i_std) :: ib, op, tok, totak, igrif, ibasf
714 715 716 717
  LOGICAL        :: debug = .FALSE.
  !
  !
  !
718 719 720 721 722
  DO ib=1,nbpt
    DO op=1,numops(ib)
      IF (basin_count(ib) > nbasmax) THEN
         tok = tokill(ib,op)
         totak = totakeover(ib,op)
723
         IF (tok .GT. 0) THEN
724
            IF ( debug ) THEN
725 726 727 728 729 730 731 732
               WRITE(numout,*) " "
               WRITE(numout,*) "numop", op
               WRITE(numout,*) "nbpt", ib, "tokill", tok, "totakover", totak, "basin_count", basin_count(ib)
               WRITE(numout,*) "outflow tokill", outflow_grid(ib,tok), outflow_basin(ib,tok)
               WRITE(numout,*) "inflows totakeover", inflow_number(ib,totak)
               DO it=1,inflow_number(ib,totak)
                 WRITE(numout,*) "->", inflow_grid(ib,totak,it), inflow_basin(ib,totak,it)
               END DO
733
            ENDIF
734 735 736 737 738 739 740 741 742 743 744 745
            ! 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)
746
                ibasf = outflow_basin(igrif,ibasf)
747 748 749
                igrif = it
              END IF
            END DO
750
            CALL routing_reg_killbas(nbpt, ib, tok, totak, nwbas, inflowmax, basin_count, basin_area, &
751 752
               & basin_orog_mean, basin_orog_min, basin_orog_max, basin_floodp, basin_cg,&
               & basin_topoind, basin_topoindex_stream, basin_rlen, &
753 754
               & basin_rdz, basin_beta_fp, fetch_basin, basin_id, basin_coor, basin_type, basin_flowdir, &
               & outflow_grid, outflow_basin, inflow_number, inflow_grid, inflow_basin)
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
         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 !!
776

777 778 779 780
  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
781 782
  LOGICAL        :: debug = .FALSE.
  !
783
  flag(:,:) = 0
784 785
  !
  IF ( debug ) WRITE(numout,*) "Checking routing"
786 787 788 789
  test = 0

  DO ig=1,nbpt
    basnum = basin_count(ig)
790
    DO ib=1,basnum
791 <