Skip to content

Commit 64775ef

Browse files
committed
additional cleanup of routing model code:
- moved initializatio of Q_RES (GEOS_RouteGridComp.F90, reservoir.F90) - renamed "number of" variables for clarity (EASE_pfaf_frac.F90) - clarified *local* "stream" in comments - edited comments - white-space changes for improved vertical alignment
1 parent a366332 commit 64775ef

File tree

7 files changed

+63
-56
lines changed

7 files changed

+63
-56
lines changed

GEOSagcm_GridComp/GEOSphysics_GridComp/GEOSsurface_GridComp/GEOSroute_GridComp/GEOS_RouteGridComp.F90

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,9 +1030,6 @@ subroutine RUN (GC,IMPORT, EXPORT, CLOCK, RC )
10301030
allocate(WTOT_BEFORE( n_pfaf_local))
10311031
allocate(QINFLOW_LOCAL(n_pfaf_local))
10321032

1033-
1034-
QRES_OUT = 0.
1035-
10361033
WTOT_BEFORE = WSTREAM + WRIVER + WRES
10371034

10381035
! Call river_routing_model

GEOSagcm_GridComp/GEOSphysics_GridComp/GEOSsurface_GridComp/GEOSroute_GridComp/reservoir.F90

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ subroutine calc( this, Q_riv_out, Q_res, Wr_res, dt, rc)
117117
real :: Qin_res, alp_res, tmpfac ! Variables for inflow, coefficients, and factors
118118
integer :: i, status
119119

120+
Q_res = 0.
121+
120122
do i = 1, size(this%active_res)
121123

122124
if (this%active_res(i) == 1) then ! reservoir is active

GEOSagcm_GridComp/GEOSphysics_GridComp/GEOSsurface_GridComp/GEOSroute_GridComp/routing_model.F90

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ END FUNCTION RESCONST
2828
! -------------------------------------------------------------------------------------------------------
2929

3030
RECURSIVE SUBROUTINE SEARCH_DNST (K, NCAT_G, DNST, Pfaf_all, DNST_OUT)
31+
32+
! objective: [best guess by rreichle as of 3 Feb 2026]
33+
! find Pfafstetter index of downstream (DNST) catchment, where "downstream" is really "downriver" in the
34+
! jargon of the routing model with "main rivers" and "local streams"
3135

3236
implicit none
3337

@@ -64,24 +68,24 @@ END SUBROUTINE SEARCH_DNST
6468
! Routing Model Input Parameters
6569
! ------------------------------
6670
!**** NCAT = NUMBER OF CATCHMENTS IN THE STUDY DOMAIN
67-
!**** Qrunf0 = RUNOFF PRODUCED BY LAND SURFACE MODEL IN THE CATCHMENT [m^3/s]
68-
!**** llc_ori = MAIN RIVER LENGTH SCALE [m]
69-
!**** lstr = LOCAL STREAMS LENGTH SCALE [m]
70-
!**** qstr_clmt0 = CLIMATOLOGY RUNOFF [m^3/s]
71-
!**** qri_clmt0 = CLIMATOLOGY DISCHAR [m^3/s]
72-
!**** qin_clmt0 = CLIMATOLOGY INFLOW [m^3/s]
73-
!**** K = K PARAMETER FOR MAIN RIVER
74-
!**** Kstr0 = K PARAMETER FOR LOCAL STREAM [m^3/s]
75-
76-
! Routing Model Prognostics
77-
! -------------------------
78-
!**** Ws0 = AMOUNT OF WATER IN "LOCAL STREAM" [m^3]
79-
!**** Wr0 = AMOUNT OF WATER IN RIVER [m^3]
71+
!**** Qrunf0 = RUNOFF PRODUCED BY LAND SURFACE MODEL IN THE CATCHMENT [m^3/s]
72+
!**** llc_ori = MAIN RIVER LENGTH SCALE [m]
73+
!**** lstr = LOCAL STREAMS LENGTH SCALE [m]
74+
!**** qstr_clmt0 = CLIMATOLOGY RUNOFF [m^3/s]
75+
!**** qri_clmt0 = CLIMATOLOGY DISCHAR [m^3/s]
76+
!**** qin_clmt0 = CLIMATOLOGY INFLOW [m^3/s]
77+
!**** K = K PARAMETER FOR MAIN RIVER
78+
!**** Kstr0 = K PARAMETER FOR LOCAL STREAM [m^3/s]
79+
80+
! Routing Model Prognostics
81+
! -------------------------
82+
!**** Ws0 = AMOUNT OF WATER IN "LOCAL STREAM" [m^3]
83+
!**** Wr0 = AMOUNT OF WATER IN RIVER [m^3]
8084

8185
! Routing Model Diagnostics
8286
! -------------------------
83-
!**** QS = TRANSFER OF MOISTURE FROM STREAM VARIABLE TO RIVER VARIABLE [m^3/s]
84-
!**** QOUT = TRANSFER OF RIVER WATER TO THE DOWNSTREAM CATCHMENT [m^3/s]
87+
!**** QS = TRANSFER OF MOISTURE FROM STREAM VARIABLE TO RIVER VARIABLE [m^3/s]
88+
!**** QOUT = TRANSFER OF RIVER WATER TO THE DOWNSTREAM (DOWNRIVER) CATCHMENT [m^3/s]
8589

8690
SUBROUTINE RIVER_ROUTING_HYD ( &
8791
NCAT,ROUTE_DT, &
@@ -137,13 +141,13 @@ SUBROUTINE RIVER_ROUTING_HYD ( &
137141

138142
! Update state variables: ks, Ws, and Qs
139143
where(Qrunf<=small)Qrunf=0. ! Set runoff to zero if it's too small
140-
Qs0=max(0.,alp_s * Ws**(1./(1.-RRM_mm))) ! Initial flow from stream storage (kg/s)
144+
Qs0=max(0.,alp_s * Ws**(1./(1.-RRM_mm))) ! Initial flow from local stream storage (kg/s)
141145
ks =max(0.,(alp_s/(1.-RRM_mm)) * Ws**(RRM_mm/(1.D0-RRM_mm))) ! Flow coefficient (s^-1)
142146
Ws_last=Ws ! Store the current water storage
143147
where(ks>small) Ws=Ws + (Qrunf-Qs0)/ks*(1.-exp(-ks*dt)) ! Update storage (kg)
144148
where(ks<=small) Ws=Ws + (Qrunf-Qs0)*dt ! Simplified update if ks is small
145149
Ws=max(0.,Ws) ! Ensure storage is non-negative
146-
Qs=max(0.,Qrunf-(Ws-Ws_last)/dt) ! Calculate the stream flow (kg/s)
150+
Qs=max(0.,Qrunf-(Ws-Ws_last)/dt) ! Calculate the local stream flow (kg/s)
147151

148152
! Calculate variables related to river routing: Qr0, kr
149153
Wr=Wr+Qs*dt

GEOSagcm_GridComp/GEOSphysics_GridComp/GEOSsurface_GridComp/Utils/Raster/makebcs/EASE_pfaf_frac.F90

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module EASE_pfaf_fracMod
66
! writes information into EASEv[x]_tile2pfaf.nc4
77

88
use MAPL
9-
use LogRectRasterizeMod, only: nc=>SRTM_maxcat
9+
use LogRectRasterizeMod, only: nPfaf=>SRTM_maxcat
1010

1111
implicit none
1212
private
@@ -68,11 +68,11 @@ subroutine EASE_get_Pfaf_frac(file_out, BCS_PATH, GridName)
6868
allocate(lon(nlon), lat(nlat))
6969
allocate(loni(nlon), lati(nlat),loni2(nlon), lati2(nlat))
7070
allocate(lons(nc_ease),lats(nr_ease))
71-
allocate(nsub(nc))
71+
allocate(nsub(nPfaf))
7272

7373
call formatter%open(trim(pfafData_file), PFIO_READ)
74-
call formatter%get_var("latitude", lat)
75-
call formatter%get_var("longitude", lon)
74+
call formatter%get_var("latitude", lat)
75+
call formatter%get_var("longitude", lon)
7676
call formatter%get_var("CatchIndex", catchind)
7777
call formatter%close()
7878

@@ -92,7 +92,7 @@ subroutine EASE_get_Pfaf_frac(file_out, BCS_PATH, GridName)
9292
call formatter%close()
9393
cellarea = cellarea / 1.e6 ! Convert cell area (e.g., from m^2 to km^2)
9494
! Initialize aggregation arrays to zero:
95-
allocate(xsub0(9999, nc), ysub0(9999, nc), asub0(9999, nc))
95+
allocate(xsub0(9999, nPfaf), ysub0(9999, nPfaf), asub0(9999, nPfaf))
9696
nsub = 0;xsub0 = 0;ysub0 = 0;asub0 = 0.
9797
! Loop over all raster grid cells to aggregate cell areas by catchment and sub-area:
9898
do xi = 1, nlon
@@ -124,52 +124,55 @@ subroutine EASE_get_Pfaf_frac(file_out, BCS_PATH, GridName)
124124
end do
125125
nmax = maxval(nsub)
126126
!print *,nmax
127-
allocate(xsub(nmax, nc), ysub(nmax, nc), asub(nmax, nc))
127+
allocate(xsub(nmax, nPfaf), ysub(nmax, nPfaf), asub(nmax, nPfaf))
128128
xsub=xsub0(1:nmax,:)
129129
ysub=ysub0(1:nmax,:)
130130
asub=asub0(1:nmax,:)
131131
deallocate(xsub0,ysub0,asub0)
132132
! Open the catchment definition file for the EASE grid and read the total number of tiles (header)
133133
open(77, file="clsm/catchment.def");read(77, *) ntot
134-
! Allocate arrays with size nt
134+
! Allocate arrays with size ntot
135135
allocate(tid(ntot), catid(ntot), lon_left(ntot), lon_right(ntot), lat_bottom(ntot), lat_top(ntot),latc(ntot),lonc(ntot))
136-
allocate(lati_tile(ntot),loni_tile(ntot),map_tile(nc_ease,nr_ease))
137-
! Loop over each catchment and read: id, catchment id, left/right longitudes, bottom/top latitudes
136+
allocate(lati_tile(ntot),loni_tile(ntot))
137+
! Loop through tiles and read: id, catchment id, left/right longitudes, bottom/top latitudes
138138
do i = 1, ntot
139-
read(77, *) tid(i), catid(i), lon_left(i), lon_right(i), lat_bottom(i), lat_top(i)
139+
read(77, *) tid(i), catid(i), lon_left(i), lon_right(i), lat_bottom(i), lat_top(i)
140140
end do
141+
! get center (not "center-of-mass") lat/lon of each tile based on tile min/max lat/lon
141142
latc = (lat_bottom + lat_top) / 2.
142143
lonc = (lon_left + lon_right) / 2.
143144
call nearest_index_vector(latc,lats,lati_tile)
144145
call nearest_index_vector(lonc,lons,loni_tile)
146+
! record into map_tile
147+
allocate(map_tile(nc_ease,nr_ease))
145148
map_tile=-9999
146149
do i =1, ntot
147150
map_tile(loni_tile(i),lati_tile(i)) = i
148151
enddo
149152

150-
allocate(isub(nmax, nc))
153+
allocate(isub(nmax, nPfaf))
151154
isub=0
152155
! Loop over each catchment
153-
do i = 1, nc
156+
do i = 1, nPfaf
154157
! Loop over each potential sub-area within the current catchment
155158
do j = 1, nmax
156-
xi = xsub(j, i) ! Retrieve the x-coordinate for the sub-area
157-
yi = ysub(j, i) ! Retrieve the y-coordinate for the sub-area
158-
if (xi /= 0) then ! Check if a valid sub-area exists (non-zero x-coordinate)
159+
xi = xsub(j, i) ! Retrieve the x-coordinate for the sub-area
160+
yi = ysub(j, i) ! Retrieve the y-coordinate for the sub-area
161+
if (xi /= 0) then ! Check if a valid sub-area exists (non-zero x-coordinate)
159162
isub(j, i) = map_tile(xi, yi) ! Map the sub-area coordinates to a global tile index using map_tile
160163
endif
161164
enddo
162165
enddo
163166

164-
allocate(area_cat(nc),frac(nmax,nc),nsub_tile(ntot),flag2(nmax,nc))
167+
allocate(area_cat(nPfaf),frac(nmax,nPfaf),nsub_tile(ntot),flag2(nmax,nPfaf))
165168
where(isub==-9999) asub=0.
166169
area_cat=0.
167-
do i=1,nc
170+
do i=1,nPfaf
168171
area_cat(i)=sum(asub(:,i))
169172
enddo
170173
nsub_tile=0
171174
frac=0.
172-
do i=1,nc
175+
do i=1,nPfaf
173176
do j=1,nmax
174177
if(isub(j,i)>0)then
175178
it=isub(j,i)
@@ -184,7 +187,7 @@ subroutine EASE_get_Pfaf_frac(file_out, BCS_PATH, GridName)
184187
csub=0
185188
nsub_tile=0
186189
frac_tile=0.
187-
do i=1,nc
190+
do i=1,nPfaf
188191
do j=1,nmax
189192
if(isub(j,i)>0)then
190193
it=isub(j,i)
@@ -238,19 +241,20 @@ end subroutine EASE_get_Pfaf_frac
238241

239242
subroutine nearest_index_vector(candidates, targets, idx)
240243
! For each targets(k), find argmin_j |candidates(j) - targets(k)|
241-
real*8, intent(in) :: targets(:)
242-
real*8, intent(in) :: candidates(:)
244+
real*8, intent(in) :: targets(:)
245+
real*8, intent(in) :: candidates(:)
243246
integer, intent(out) :: idx(size(candidates)) ! 1-based indices
244-
integer :: k, j, nT, nC, best_j
245-
real*8 :: best_d, d
246247

247-
nT = size(targets)
248-
nC = size(candidates)
248+
integer :: k, j, nTarg, nCand, best_j
249+
real*8 :: best_d, d
249250

250-
do k = 1, nC
251+
nTarg = size(targets)
252+
nCand = size(candidates)
253+
254+
do k = 1, nCand
251255
best_d = huge(1.0d0)
252256
best_j = 1
253-
do j = 1, nT
257+
do j = 1, nTarg
254258
d = abs(candidates(k) - targets(j))
255259
if (d < best_d) then
256260
best_d = d
@@ -260,5 +264,5 @@ subroutine nearest_index_vector(candidates, targets, idx)
260264
idx(k) = best_j ! already 1-based
261265
end do
262266
end subroutine nearest_index_vector
263-
267+
264268
end module EASE_pfaf_fracMod

GEOSagcm_GridComp/GEOSphysics_GridComp/GEOSsurface_GridComp/Utils/Raster/preproc/routing_model/get_latloni_cellarea.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def area_global_rectilinear_grid(lat, lon, rearth=6371.):
5252
lon : numpy.ndarray
5353
Array of longitude values (degrees).
5454
rearth : float
55-
Earth radius in kilometers. Default is 6371.22 km.
55+
Earth radius in kilometers. Default is 6371. km (for consistency with MAPL_Radius as of 3 Feb 2026).
5656
5757
Returns:
5858
area_grid : numpy.ndarray

GEOSagcm_GridComp/GEOSphysics_GridComp/GEOSsurface_GridComp/Utils/Raster/preproc/routing_model/get_river_length.f90

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,13 @@ function spherical_distance(lon_dn, lat_dn, lon_up, lat_up) result(distance)
232232
! distance - Great-circle distance between the two points (kilometers)
233233
!------------------------------------------------------------
234234
real, intent(in) :: lon_dn, lat_dn ! Coordinates of downstream point
235-
real, intent(in) :: lon_up, lat_up ! Coordinates of upstream point
235+
real, intent(in) :: lon_up, lat_up ! Coordinates of upstream point
236236
real :: distance ! Computed distance (km)
237237
real :: R, dlon, dlat, a, c ! Intermediate variables
238238

239-
R = 6371.0 ! Earth's radius in kilometers
239+
R = 6371.0 ! Earth's radius in kilometers
240240
dlon = (lon_up - lon_dn) * (acos(-1.0) / 180.0) ! Delta longitude (radians)
241-
dlat = (lat_up - lat_dn) * (acos(-1.0) / 180.0) ! Delta latitude (radians)
241+
dlat = (lat_up - lat_dn) * (acos(-1.0) / 180.0) ! Delta latitude (radians)
242242

243243
a = sin(dlat / 2.0)**2 + cos(lat_dn * (acos(-1.0) / 180.0)) * &
244244
cos(lat_up * (acos(-1.0) / 180.0)) * sin(dlon / 2.0)**2
@@ -247,4 +247,4 @@ function spherical_distance(lon_dn, lat_dn, lon_up, lat_up) result(distance)
247247

248248
end function spherical_distance
249249

250-
end program main
250+
end program main

GEOSagcm_GridComp/GEOSphysics_GridComp/GEOSsurface_GridComp/Utils/Raster/preproc/routing_model/k_module_cali.f90

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,10 +378,10 @@ subroutine get_station_inf(file_pfafmap, ns, nc, nlat, nlon, lati, loni, catid,
378378
! Calculate modeled K values for all catchments
379379
KImodel_all = (Qclmt_all**(exp_clmt)) * (slp_all**(exp_slp))
380380

381-
! Calculate stream K values using the scaling factor
381+
! Calculate local stream K values using the scaling factor
382382
Kstr_all = (Qstr_all**(exp_clmt)) * (slp_all**(exp_slp))
383383

384-
! Write stream K values to an output file
384+
! Write local stream K values to an output file
385385
open(88, file="temp/Pfaf_Kstr_PR_fac1_0p35_0p45_0p2_n0p2.txt")
386386
do i = 1, nc
387387
write(88, *) Kstr_all(i)
@@ -1026,4 +1026,4 @@ subroutine sort2(arr)
10261026
end subroutine sort2
10271027

10281028
!------------------------------------------------------------
1029-
end module k_module
1029+
end module k_module

0 commit comments

Comments
 (0)