routing_interface.f90 19.6 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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
SUBROUTINE gethydrogrid(nbpt, nbvmax_in, nbxmax_in, sub_pts, sub_index, sub_area,  max_basins, min_topoind, &
     & lon_rel, lat_rel, trip, basins, topoindex, fac, hierarchy, nbi, nbj, area_bx, trip_bx, basin_bx, &
     & topoind_bx, fac_bx, hierarchy_bx, lon_bx, lat_bx, lshead_bx, nwbas)
  !
  USE ioipsl
  USE grid
  USE routing_tools
  USE routing_reg
  !
  IMPLICIT NONE
  !
  INTEGER, INTENT(in) :: nbvmax_in, nbxmax_in
  !
  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
  !
  !! OUTPUT VARIABLES
  INTEGER, INTENT(out) :: nbi(nbpt), nbj(nbpt) !! Number of point in x and y within the grid (unitless)
  REAL, INTENT(out) :: area_bx(nbpt,nbxmax_in,nbxmax_in) !! Area of each small box in the grid box (m^2)
  REAL, INTENT(out) :: hierarchy_bx(nbpt,nbxmax_in,nbxmax_in) !! Level in the basin of the point
  REAL, INTENT(out) :: fac_bx(nbpt,nbxmax_in,nbxmax_in) !! Flow accumulation
  REAL, INTENT(out) :: lon_bx(nbpt,nbxmax_in,nbxmax_in) !!
  REAL, INTENT(out) :: lat_bx(nbpt,nbxmax_in,nbxmax_in) !!
  REAL, INTENT(out) :: lshead_bx(nbpt,nbxmax_in,nbxmax_in) !! Large scale heading for outflow points.
  REAL, INTENT(out) :: topoind_bx(nbpt,nbxmax_in,nbxmax_in) !! Topographic index of the residence time for each of the smaller boxes (m)
  INTEGER, INTENT(out) :: trip_bx(nbpt,nbxmax_in,nbxmax_in) !! The trip field for each of the smaller boxes (unitless)
  INTEGER, INTENT(out) :: basin_bx(nbpt,nbxmax_in,nbxmax_in) !!
  INTEGER, INTENT(out) :: nwbas
  !
  INTEGER :: ii, ib
  REAL :: resolution(nbpt,2)
  
  !
  ! These two parameters are part of the routing_reg module saved variables. They are transfered here.
  !
  nbvmax = nbvmax_in
  nbxmax = nbxmax_in
  !
  DO ii=1,nbpt
     resolution(ii,1) = SQRT(area(ii))
     resolution(ii,2) = SQRT(area(ii))
  ENDDO
  
  nwbas = MAXVAL(sub_pts)
  
  DO ib=1,nbpt
     CALL routing_reg_getgrid(nbpt, ib, sub_pts, sub_index, sub_area, max_basins, min_topoind, &
          & lon_rel, lat_rel, lalo, resolution, contfrac, trip, basins, topoindex, fac, hierarchy, &
          & nbi(ib), nbj(ib), area_bx(ib,:,:), trip_bx(ib,:,:), basin_bx(ib,:,:), topoind_bx(ib,:,:), &
          & fac_bx(ib,:,:), hierarchy_bx(ib,:,:), lon_bx(ib,:,:), lat_bx(ib,:,:), lshead_bx(ib,:,:))
  ENDDO
  !
END SUBROUTINE gethydrogrid


SUBROUTINE findbasins(nbpt, nbvmax_in, nbxmax_in, nbi, nbj, trip_bx, basin_bx, fac_bx, hierarchy_bx, topoind_bx, lshead_bx, &
     &                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
  !
  INTEGER, INTENT(in)    :: nbvmax_in, nbxmax_in
  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)
  INTEGER, INTENT(inout) :: trip_bx(nbpt,nbxmax_in,nbxmax_in) !! The trip field for each of the smaller boxes (unitless)
  INTEGER, INTENT(inout) :: basin_bx(nbpt,nbxmax_in,nbxmax_in) !!
  REAL, INTENT(in)       :: fac_bx(nbpt,nbxmax_in,nbxmax_in) !! Flow accumulation
  REAL, INTENT(in)       :: hierarchy_bx(nbpt,nbxmax_in,nbxmax_in) !! Level in the basin of the point
  REAL, INTENT(inout)    :: topoind_bx(nbpt,nbxmax_in,nbxmax_in) !! Topographic index of the residence time for each of the smaller boxes (m)
  REAL, INTENT(in)       :: lshead_bx(nbpt,nbxmax_in,nbxmax_in) !! Large scale heading for outflow points.
  !
  !
  ! OUTPUT VARIABLES
  INTEGER, INTENT(out) :: nb_basin(nbpt)               !! Number of sub-basins (unitless)
  INTEGER, INTENT(out) :: basin_inbxid(nbpt,nbvmax_in)   !!
  REAL, INTENT(out)    :: basin_outlet(nbpt,nbvmax_in,2) !!
  REAL, INTENT(out)    :: basin_outtp(nbpt,nbvmax_in)    !!  
  INTEGER, INTENT(out) :: basin_sz(nbpt,nbvmax_in)       !!
  INTEGER, INTENT(out) :: basin_bxout(nbpt,nbvmax_in)    !!
  INTEGER, INTENT(out) :: basin_bbout(nbpt,nbvmax_in)    !!
  INTEGER, INTENT(out) :: basin_pts(nbpt,nbvmax_in, nbvmax_in, 2) !!
  REAL, INTENT(out)    :: basin_lshead(nbpt,nbvmax_in)   !!
  INTEGER, INTENT(out) :: coast_pts(nbpt,nbvmax_in)      !! The coastal flow points (unitless)
  !
  ! For debug and get coordinate of river outlet
  !
  REAL, INTENT(in) :: lontmp(nbpt,nbxmax_in,nbxmax_in) !! Longitude
  REAL, INTENT(in) :: lattmp(nbpt,nbxmax_in,nbxmax_in) !! Latitude
  !
  ! Local
  !
  INTEGER :: ib
  !
157
  diaglalo(1,:) = (/ 38.25, -7.25 /)
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
  !
  IF ( nbvmax_in .NE. nbvmax .OR. nbxmax_in .NE. nbxmax ) THEN
     WRITE(*,*) "nbvmax or nbxmax have changed !!"
     STOP
  ENDIF
  
  DO ib=1,nbpt
     CALL routing_reg_findbasins(ib, nbi(ib), nbj(ib), trip_bx(ib,:,:), basin_bx(ib,:,:), fac_bx(ib,:,:), hierarchy_bx(ib,:,:), &
          & 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

END SUBROUTINE findbasins

173
SUBROUTINE globalize(nbpt, nbvmax_in, nbxmax_in, area_bx, lon_bx, lat_bx, trip_bx, hierarchy_bx, fac_bx, topoind_bx, &
174
          & min_topoind, nb_basin, basin_inbxid, basin_outlet, basin_outtp, basin_sz, basin_pts, basin_bxout, &
175
          & basin_bbout, lshead, coast_pts, nwbas, basin_count, basin_notrun, basin_area, basin_cg, basin_hierarchy, &
176 177 178 179 180 181 182 183 184 185 186 187
          & 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)
188
  INTEGER(i_std), INTENT(in)  :: nbvmax_in, nbxmax_in
189
  REAL(r_std), INTENT (in), DIMENSION(nbpt,nbxmax_in,nbxmax_in)    :: area_bx      !! Area of each small box in the grid box (m^2)
190 191
  REAL(r_std), INTENT (in), DIMENSION(nbpt,nbxmax_in,nbxmax_in)    :: lon_bx       !! Longitude of each small box in the grid box
  REAL(r_std), INTENT (in), DIMENSION(nbpt,nbxmax_in,nbxmax_in)    :: lat_bx       !! Latitude of each small box in the grid box
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
  INTEGER(i_std), INTENT (in), DIMENSION(nbpt,nbxmax_in,nbxmax_in) :: trip_bx      !! The trip field for each of the smaller boxes (unitless)
  REAL(r_std), INTENT (in), DIMENSION(nbpt,nbxmax_in,nbxmax_in)    :: hierarchy_bx !! Level in the basin of the point
  REAL(r_std), INTENT (in), DIMENSION(nbpt,nbxmax_in,nbxmax_in)    :: fac_bx !! Level in the basin of the point
  REAL(r_std), INTENT (in), DIMENSION(nbpt,nbxmax_in,nbxmax_in)    :: topoind_bx   !! Topographic index of the residence time for each of the smaller boxes (m)
  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)
  INTEGER(i_std), INTENT (in), DIMENSION(nbpt,nbvmax_in)     :: basin_inbxid, basin_sz !! ID of basin, number of points in the basin
  REAL(r_std), INTENT (in), DIMENSION(nbpt,nbvmax_in,2)      :: basin_outlet !! Outlet coordinate of each subgrid basin
  REAL(r_std), INTENT (in), DIMENSION(nbpt,nbvmax_in)        :: basin_outtp  !! 
  INTEGER(i_std), INTENT (in), DIMENSION(nbpt,nbvmax_in,nbvmax_in,2) :: basin_pts  !! Points in each basin
  INTEGER(i_std), INTENT (in), DIMENSION(nbpt,nbvmax_in)     :: basin_bxout  !! outflow direction
  INTEGER(i_std), INTENT (in), DIMENSION(nbpt,nbvmax_in)     :: basin_bbout  !! outflow sub-basin
  REAL(r_std), INTENT (in), DIMENSION(nbpt,nbvmax_in)        :: lshead       !! Large scale heading of outflow.
  INTEGER(i_std), INTENT (in), DIMENSION(nbpt,nbvmax_in)     :: coast_pts !! The coastal flow points (unitless)
  !! 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       !!
215
  REAL(r_std), INTENT (out), DIMENSION(nbpt,nwbas,2)  :: basin_cg         !! Centre of gravity of the HTU
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
  REAL(r_std), INTENT (out), DIMENSION(nbpt,nwbas)    :: basin_hierarchy  !!
  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)
  INTEGER(i_std), INTENT (out), DIMENSION(nbpt,nwbas) :: outflow_basin !! 
  INTEGER(i_std), INTENT (out), DIMENSION(nbpt)       :: nbcoastal           !!
  INTEGER(i_std), INTENT (out), DIMENSION(nbpt,nwbas) :: coastal_basin !!
  !!
  INTEGER(i_std) :: ib
  !!
  IF ( nbvmax_in .NE. nbvmax .OR. nbxmax_in .NE. nbxmax ) THEN
     WRITE(*,*) "GLOBALIZE : nbvmax or nbxmax have changed !!"
     STOP
  ENDIF
  !!
  DO ib=1,nbpt
233 234
     CALL routing_reg_globalize(nbpt, ib, neighbours, area_bx(ib,:,:), lon_bx(ib,:,:), lat_bx(ib,:,:), trip_bx(ib,:,:), &
          & hierarchy_bx(ib,:,:), fac_bx(ib,:,:), &
235 236
          & 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, &
237
          & basin_count, basin_notrun, basin_area, basin_cg, basin_hierarchy, basin_fac, basin_topoind, basin_id, basin_coor, &
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
          & basin_type, basin_flowdir, basin_lshead, outflow_grid, outflow_basin, nbcoastal, coastal_basin)
  ENDDO
  !
END SUBROUTINE globalize

SUBROUTINE linkup(nbpt, nbxmax_in, nwbas, basin_count, basin_area, basin_id, basin_flowdir, &
     & 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)
  INTEGER(i_std), INTENT (in) :: nbxmax_in
  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 !!
  !
  INTEGER(i_std), INTENT(out), DIMENSION(nbpt,nbxmax_in*8) :: inflow_number !!
  INTEGER(i_std), INTENT(out), DIMENSION(nbpt,nbxmax_in*8,nbxmax_in*8) :: inflow_basin !!
  INTEGER(i_std), INTENT(out), DIMENSION(nbpt,nbxmax_in*8,nbxmax_in*8) :: inflow_grid !!
  !
  INTEGER(i_std), INTENT(inout), DIMENSION(nbpt) :: nbcoastal !!
  INTEGER(i_std), INTENT(inout), DIMENSION(nbpt,nwbas) :: coastal_basin !!
  !
  !
  IF ( nbxmax_in .NE. nbxmax ) THEN
     WRITE(*,*) "LINKUP : nbxmax has changed !!"
     STOP
  ENDIF
  
  CALL routing_reg_linkup(nbpt, neighbours, nwbas, basin_count, basin_area, basin_id, basin_flowdir, &
       & 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

SUBROUTINE fetch(nbpt, nwbas, basin_count, basin_area, basin_id, basin_hierarchy, basin_fac,&
290
     & outflow_grid, outflow_basin, fetch_basin, fetch_basin_min, fetch_basin_max)
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
  !
  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 !!
  INTEGER(i_std), DIMENSION(nbpt), INTENT(in) :: basin_count !!
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(inout) :: basin_area !!
  INTEGER(i_std), DIMENSION(nbpt,nwbas), INTENT(in) :: basin_id !!
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(in)       :: basin_hierarchy
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(in)       :: basin_fac
  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(in) :: outflow_basin !!
  !
310 311 312 313
  !! IN-OUTPUT VARIABLES
  REAL(r_std), DIMENSION(nbpt,nwbas), INTENT(inout) :: fetch_basin !!
  ! Output
  REAL(r_std), INTENT(out) :: fetch_basin_min, fetch_basin_max
314 315 316
  
  CALL routing_reg_fetch(nbpt, area, contfrac, nwbas, basin_count, basin_area, basin_id, basin_hierarchy, basin_fac,&
       & outflow_grid, outflow_basin, fetch_basin)
317 318 319
  fetch_basin_min = MINVAL(fetch_basin)
  fetch_basin_max = MAXVAL(fetch_basin)
  
320 321 322 323
  !
END SUBROUTINE fetch


324
SUBROUTINE truncate(nbpt, nbxmax_in, nbasmax, nwbas, basin_count, basin_notrun, basin_area, basin_cg, basin_topoind,&
325
     & fetch_basin, basin_id, basin_coor, basin_type, basin_flowdir, outflow_grid, outflow_basin, inflow_number,&
326
     & inflow_grid, inflow_basin, routing_area, routing_cg, topo_resid, route_togrid, route_tobasin, route_nbintobas, &
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
     & global_basinid, route_outlet, route_type, origin_nbintobas)
  !
  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) :: nbasmax
  INTEGER(i_std), INTENT (in) :: nwbas !!

  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 !!
348
  REAL(r_std), DIMENSION(nbpt,nwbas,2), INTENT(inout)  :: basin_cg
349 350 351 352 353 354 355 356 357 358 359 360 361
  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 !!
  !
  ! Changed nwbas to nbxmax*4 and *8 to save the memory
  !
  INTEGER(i_std), DIMENSION(nbpt,nbxmax_in*8), INTENT(inout)          :: inflow_number !!
  INTEGER(i_std), DIMENSION(nbpt,nbxmax_in*8,nbxmax_in*8), INTENT(inout) :: inflow_basin !!
  INTEGER(i_std), DIMENSION(nbpt,nbxmax_in*8,nbxmax_in*8), INTENT(inout) :: inflow_grid !!
  !
  ! Output variables
  !
362 363 364
  REAL(r_std), DIMENSION(nbpt,nbasmax), INTENT(out)    :: routing_area !! Surface of basin (m^2)
  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)
365 366
  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)
367 368
  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)
369
  INTEGER(i_std), DIMENSION(nbpt,nbasmax), INTENT(out) :: global_basinid !! ID of basin (unitless)
370 371
  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 (-)
372 373 374 375 376 377
  !
  IF ( nbxmax_in .NE. nbxmax ) THEN
     WRITE(*,*) "TRUNCATE : nbxmax has changed !!"
     STOP
  ENDIF

378 379
  CALL routing_reg_truncate(nbpt, nbasmax, area, contfrac, nwbas, basin_count, basin_notrun, basin_area, basin_cg, &
       & basin_topoind, fetch_basin, basin_id, basin_coor, basin_type, basin_flowdir, outflow_grid, outflow_basin, inflow_number,&
380 381 382
       & inflow_grid, inflow_basin)

  routing_area(:,:) = routing_area_glo(:,:)
383
  routing_cg(:,:,:) = routing_cg_glo(:,:,:)
384 385 386 387 388 389 390 391 392 393
  topo_resid(:,:) = topo_resid_glo(:,:) 
  route_togrid(:,:) = route_togrid_glo(:,:)
  route_tobasin(:,:) = route_tobasin_glo(:,:)
  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(:)

END SUBROUTINE truncate