Soil Water Balance (SWB2)
Loading...
Searching...
No Matches
runoff__gridded_values.F90
Go to the documentation of this file.
1!> @file
2!! Contains the module \ref runoff__gridded_values.
3
4!>
5!! Module \ref runoff__gridded_values
6!! provides support for estimating fog drip given a gridded map
7!! of RUNOFF_ZONE, and a table containing monthly
8!! fog factors.
10
11 use iso_c_binding, only : c_short, c_int, c_float, c_double, c_bool
13 use data_catalog
15 use datetime
16 use dictionary
17 use exceptions
20 use fstring
21 use fstring_list
22
23 implicit none
24
25 private
26
28
29 real (c_float), allocatable :: runoff_table_values(:,:)
30 type ( datetime_t ), allocatable :: runoff_table_dates(:)
31
33 integer (c_int), allocatable :: runoff_zone(:)
34
35 real (c_float), allocatable, public :: runoff_ratios(:)
36
37 contains
38
39 !> Initialize the infiltration grid.
40 !!
41 !! Read in a runoff zone grid.
42 !!
44
45 logical (c_bool), intent(in) :: lactive(:,:)
46
47 ! [ LOCALS ]
48 integer (c_int) :: istat
49 type (fstring_list_t) :: slstring
50
51 ! locate the data structure associated with the gridded fog ratio entries
52 prunoff_zone => dat%find("RUNOFF_ZONE")
53 if ( .not. associated(prunoff_zone) ) &
54 call die("A RUNOFF_ZONE grid must be supplied in order to make use of this option.", __file__, __line__)
55
56 call prunoff_zone%getvalues( )
57
58 allocate ( runoff_zone( count( lactive ) ), stat=istat )
59 call assert(istat==0, "Failed to allocate memory for the RUNOFF_ZONE variable", __file__, __line__)
60
61 runoff_zone = pack( prunoff_zone%pGrdBase%iData, lactive )
62
63 allocate ( runoff_ratios( count( lactive ) ), stat=istat)
64 call assert(istat==0, "Failed to allocate memory for the RUNOFF_RATIOS variable", __file__, __line__)
65
66
67 ! look up the name of the fragments file in the control file dictionary
68 call cf_dict%get_values( skey="RUNOFF_RATIO_MONTHLY_FILE", slstring=slstring )
69
70 ! use the first entry in the string list slString as the filename to open for
71 ! use with the daily fragments routine
72 call read_runoff_ratio_table( slstring%get(1) )
73
75
76!--------------------------------------------------------------------------------------------------
77
78 subroutine read_runoff_ratio_table( sFilename )
79
80 character (len=*), intent(in) :: sFilename
81
82 ! [ LOCALS ]
83 character (len=65536) :: sRecord, sSubstring
84 integer (c_int) :: iStat
85 integer (c_int) :: iLineNum
86 integer (c_int) :: iFieldNum
87 integer (c_int) :: iIndex
88 integer (c_int) :: iNumLines
89 integer (c_int) :: iNumFields
90 type (ASCII_FILE_T), allocatable :: RUNOFF_RATIO_FILE
91
92 allocate(runoff_ratio_file)
93
94 call runoff_ratio_file%open( sfilename = sfilename, &
95 scommentchars = "#%!", &
96 sdelimiters = "WHITESPACE", &
97 lhasheader = .true._c_bool )
98
99 inumlines = runoff_ratio_file%numLines()
100
101 ! read in next line of file
102 srecord = runoff_ratio_file%readLine()
103
104 inumfields = fieldcount( srecord )
105
106 allocate( runoff_table_values( inumlines, inumfields -1 ), stat=istat )
107 call assert( istat == 0, "Problem allocating memory for runoff ratio table values", &
108 __file__, __line__ )
109
110 allocate( runoff_table_dates( inumlines ), stat=istat )
111 call assert( istat == 0, "Problem allocating memory for runoff ratio table date values vector", &
112 __file__, __line__ )
113
114
115 ilinenum = 0
116 ifieldnum = 0
117
118 do
119
120 ! read in next line of file
121 srecord = runoff_ratio_file%readLine()
122
123 if ( runoff_ratio_file%isEOF() ) exit
124
125 ilinenum = ilinenum + 1
126 ifieldnum = 0
127
128 ! read in date
129 call chomp(srecord, ssubstring, runoff_ratio_file%sDelimiters )
130
131 if ( len_trim(ssubstring) == 0 ) &
132 call die( "Missing date in the monthly runoff ratio file", &
133 __file__, __line__, "Problem occured on line number " &
134 //ascharacter(runoff_ratio_file%currentLineNum() ) &
135 //" of file "//dquote(sfilename) )
136
137 call runoff_table_dates( ilinenum )%parseDate( ssubstring )
138
139 do iindex = 2, inumfields
140
141! ! read in fragment for given day of month
142 call chomp(srecord, ssubstring, runoff_ratio_file%sDelimiters )
143
144 if ( len_trim(ssubstring) == 0 ) &
145 call die( "Missing or corrupt value in the runoff ratio file", &
146 __file__, __line__, "Problem occured on line number " &
147 //ascharacter(runoff_ratio_file%currentLineNum() ) &
148 //" of file "//dquote(sfilename) )
149
150 runoff_table_values(ilinenum, iindex - 1 ) = asfloat( ssubstring )
151
152 enddo
153
154 enddo
155
156
157 end subroutine read_runoff_ratio_table
158
159!--------------------------------------------------------------------------------------------------
160
162
163 ! [ LOCALS ]
164 integer (c_int) :: ilinenum
165 integer (c_int) :: ifieldnum
166 logical (c_bool) :: lmatch
167 integer (c_int) :: icount
168
169 lmatch = false
170 icount = 0
171
172 do ilinenum = lbound( runoff_table_dates, 1), ubound( runoff_table_dates, 1) - 1
173
174 ! locate the relevant line in the runoff table in the basis of the date range
175 if ( ( runoff_table_dates( ilinenum ) <= sim_dt%curr ) &
176 .and. ( runoff_table_dates( ilinenum + 1 ) > sim_dt%curr ) ) then
177
178 lmatch = true
179 exit
180 endif
181
182 enddo
183
184 if ( .not. lmatch ) &
185 call die( "Failed to find an appropriate date value in the RUNOFF_RATIO file.", &
186 __file__, __line__ )
187
188 ! now that we have the relevant line of data, distribute values, assuming that
189 ! the runoff zone ids is sequential and corresponds to the field numbers
190
191 runoff_ratios = 0.0_c_float
192
193 do ifieldnum = lbound(runoff_table_values, 2), ubound(runoff_table_values, 2)
194
195 icount = icount + count( runoff_zone == ifieldnum )
196
197 where ( runoff_zone == ifieldnum )
198
199 runoff_ratios = runoff_table_values( ilinenum, ifieldnum )
200
201 end where
202
203 enddo
204
206
207end module runoff__gridded_values
This module contains physical constants and convenience functions aimed at performing unit conversion...
logical(c_bool), parameter, public true
logical(c_bool), parameter, public false
Defines the DATA_CATALOG_T data type, which contains type-bound procedures to add,...
type(data_catalog_t), public dat
DAT is a global to hold data catalog entries.
This module contains the DATETIME_T class and associated time and date-related routines,...
Definition datetime.F90:9
type(dict_t), public cf_dict
subroutine, public die(smessage, smodule, iline, shints, scalledby, icalledbyline)
Module runoff__gridded_values provides support for estimating fog drip given a gridded map of RUNOFF_...
subroutine read_runoff_ratio_table(sfilename)
type(data_catalog_entry_t), pointer prunoff_zone
subroutine, public runoff_gridded_values_initialize(lactive)
Initialize the infiltration grid.
type(datetime_t), dimension(:), allocatable runoff_table_dates
subroutine, public runoff_gridded_values_update_ratios()
real(c_float), dimension(:), allocatable, public runoff_ratios
real(c_float), dimension(:,:), allocatable runoff_table_values
integer(c_int), dimension(:), allocatable runoff_zone
type(date_range_t), public sim_dt