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) :: RUNOFF_RATIO_FILE
91
92 call runoff_ratio_file%open( sfilename = sfilename, &
93 scommentchars = "#%!", &
94 sdelimiters = "WHITESPACE", &
95 lhasheader = .false._c_bool )
96
97 inumlines = runoff_ratio_file%numLines()
98
99 ! read in next line of file
100 srecord = runoff_ratio_file%readLine()
101
102 inumfields = fieldcount( srecord )
103
104 allocate( runoff_table_values( inumlines, inumfields -1 ), stat=istat )
105 call assert( istat == 0, "Problem allocating memory for runoff ratio table values", &
106 __file__, __line__ )
107
108 allocate( runoff_table_dates( inumlines ), stat=istat )
109 call assert( istat == 0, "Problem allocating memory for runoff ratio table date values vector", &
110 __file__, __line__ )
111
112
113 ilinenum = 0
114 ifieldnum = 0
115
116 do
117
118 ! read in next line of file
119 srecord = runoff_ratio_file%readLine()
120
121 if ( runoff_ratio_file%isEOF() ) exit
122
123 ilinenum = ilinenum + 1
124 ifieldnum = 0
125
126 ! read in date
127 call chomp(srecord, ssubstring, runoff_ratio_file%sDelimiters )
128
129 if ( len_trim(ssubstring) == 0 ) &
130 call die( "Missing date in the monthly runoff ratio file", &
131 __file__, __line__, "Problem occured on line number " &
132 //ascharacter(runoff_ratio_file%currentLineNum() ) &
133 //" of file "//dquote(sfilename) )
134
135 call runoff_table_dates( ilinenum )%parseDate( ssubstring )
136
137 do iindex = 2, inumfields
138
139! ! read in fragment for given day of month
140 call chomp(srecord, ssubstring, runoff_ratio_file%sDelimiters )
141
142 if ( len_trim(ssubstring) == 0 ) &
143 call die( "Missing or corrupt value in the runoff ratio file", &
144 __file__, __line__, "Problem occured on line number " &
145 //ascharacter(runoff_ratio_file%currentLineNum() ) &
146 //" of file "//dquote(sfilename) )
147
148 runoff_table_values(ilinenum, iindex - 1 ) = asfloat( ssubstring )
149
150 enddo
151
152 enddo
153
154
155 end subroutine read_runoff_ratio_table
156
157!--------------------------------------------------------------------------------------------------
158
160
161 ! [ LOCALS ]
162 integer (c_int) :: ijulianday
163 integer (c_int) :: imonth
164 integer (c_int) :: iday
165 integer (c_int) :: iyear
166 integer (c_int) :: idaysinmonth
167 integer (c_int) :: inumdaysfromorigin
168 integer (c_int) :: ilinenum
169 integer (c_int) :: ifieldnum
170 real (c_float) :: ffactor
171 logical (c_bool) :: lmatch
172 integer (c_int) :: icount
173
174 lmatch = false
175 icount = 0
176
177 do ilinenum = lbound( runoff_table_dates, 1), ubound( runoff_table_dates, 1) - 1
178
179 ! locate the relevant line in the runoff table in the basis of the date range
180 if ( ( runoff_table_dates( ilinenum ) <= sim_dt%curr ) &
181 .and. ( runoff_table_dates( ilinenum + 1 ) > sim_dt%curr ) ) then
182
183 lmatch = true
184 exit
185 endif
186
187 enddo
188
189 if ( .not. lmatch ) &
190 call die( "Failed to find an appropriate date value in the RUNOFF_RATIO file.", &
191 __file__, __line__ )
192
193 ! now that we have the relevant line of data, distribute values, assuming that
194 ! the runoff zone ids is sequential and corresponds to the field numbers
195
196 runoff_ratios = 0.0_c_float
197
198 do ifieldnum = lbound(runoff_table_values, 2), ubound(runoff_table_values, 2)
199
200 icount = icount + count( runoff_zone == ifieldnum )
201
202 where ( runoff_zone == ifieldnum )
203
204 runoff_ratios = runoff_table_values( ilinenum, ifieldnum )
205
206 end where
207
208 enddo
209
211
212end 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