Soil Water Balance (SWB2)
Loading...
Searching...
No Matches
main.F90
Go to the documentation of this file.
1!> @file
2!> Main program which references all other modules; execution begins here.
3
4
5!> Main program which references all other modules; execution begins here.
6!>
7!> Accepts command-line arguments and makes a single call
8!> to the control_setModelOptions routine in module \ref control.
9program main
10
11 use iso_c_binding, only : c_short, c_int, c_float, c_double, c_bool, c_long
15 use logfiles, only : logs, log_debug
17 use model_domain, only : model
19
21
22 use fstring, only : operator(.containssimilar.), &
24 use version_control, only : swb_version, git_commit_hash_string, &
25 git_branch_string, compile_date, &
26 compile_time, system_name
28 use timer, only : timer_t
29 use iso_fortran_env
30
31 implicit none
32
33 type (fstring_list_t) :: slcontrolfiles
34
35 character (len=256) :: sbuf
36 character (len=256) :: soutputprefixname
37 character (len=256) :: soutputdirectoryname
38 character (len=256) :: slogfiledirectoryname
39 character (len=256) :: sdatadirectoryname
40 character (len=256) :: slookuptabledirectoryname
41 character (len=256) :: sweatherdatadirectoryname
42 integer (c_int) :: inumargs
43 character (len=1024) :: scompileroptions
44 character (len=256) :: scompilerversion
45 character (len=:), allocatable :: scompilername
46 character (len=256) :: sversionstring
47 character (len=256) :: scompilationdatestring
48 character (len=256) :: scompilationsystemstring
49 character (len=256) :: sexecutabledescription
50 character (len=256) :: sgithashstring
51 integer (c_int) :: icount
52 integer (c_int) :: iindex
53 integer (c_int) :: ilen
54 integer (c_int) :: number_of_simulations
55
56 type (timer_t) :: runtimer
57
58 soutputprefixname = ""
59 soutputdirectoryname = ""
60 sdatadirectoryname = ""
61 slogfiledirectoryname = ""
62 slookuptabledirectoryname = ""
63 sweatherdatadirectoryname = ""
64 number_of_simulations = 1
65
66 call runtimer%start()
67
68 inumargs = command_argument_count()
69
70 sversionstring = trim( swb_version )
71 scompilationdatestring = trim(compile_date)//" "//trim(compile_time)
72 scompilationsystemstring = trim(system_name)
73 scompileroptions = compiler_options()
74 scompilerversion = compiler_version()
75 sexecutabledescription = "USGS Soil-Water-Balance Code version "//trim( sversionstring )
76
77 if ( system_name == "Windows" ) then
79 else
81 endif
82
83 sgithashstring = trim( adjustl(git_branch_string ) )//", "//trim( git_commit_hash_string )
84
85#ifdef __GFORTRAN__
86 scompilername = "gfortran" // trim(scompilerversion)
87#endif
88
89#ifdef __INTEL_COMPILER
90 ! populate with empty string, since for Intel the COMPILER_VERSION string contains
91 ! the compiler name already
92 scompilername = "Intel Fortran,"//right(string=scompilerversion, substring=",")
93 scompilerversion = ""
94! write(UNIT=*,FMT="(a,/)") "compiler build date:"//TRIM(asCharacter(__INTEL_COMPILER_BUILD_DATE))
95#endif
96
97 icount = max( len_trim( sversionstring ), len_trim( sgithashstring ) )
98 icount = max( icount, len_trim( scompilationsystemstring) )
99 icount = max( icount, len_trim( scompilername) + len_trim( scompilerversion ) )
100 icount = icount + 24
101 icount = max( icount, len_trim( sexecutabledescription ) )
102
103 write(unit=*, fmt="(/,a)") repeat("-",icount + 4)
104 write(unit=*,fmt="(2x,a,/)") trim(sexecutabledescription)
105 write(unit=*,fmt="(a24,a)") "compiled on : ", trim( scompilationdatestring )
106 write(unit=*,fmt="(a24,a)") "compiled with : ", trim( scompilername )
107 write(unit=*,fmt="(a24,a)") "compiled for : ",trim( scompilationsystemstring )
108 write(unit=*,fmt="(a24,a)") "git hash and branch : ", trim( sgithashstring )
109 write(unit=*, fmt="(a,/)") repeat("-",icount + 4)
110
111 if(inumargs == 0 ) then
112
113 write(unit=*,fmt="(a)") " Compiler options : "
114 write(unit=*,fmt="(a,/,a,/)") "==================== ", trim( scompileroptions )
115
117
118 write(unit=*,fmt="(//,a,/,/,6(a,/))") "Usage: swb2 [ options ] control_file_name ", &
119 "[ --output_prefix= ] :: text to use as a prefix for output filenames", &
120 "[ --output_dir= ] :: directory to place output in (may be relative or absolute)", &
121 "[ --lookup_dir= ] :: directory to search for lookup tables", &
122 "[ --logfile_dir= ] :: directory to write logfiles to", &
123 "[ --data_dir= ] :: directory to search for input data grids", &
124 "[ --weather_data_dir= ] :: directory to search for weather data grids"
125! "[ --random_start= ] :: advance random number generator to this position in the series", &
126! "[ --number_of_sims= ] :: number of simulations to run when 'method of fragments' is used"
127 stop
128
129 end if
130
131 do iindex=1, inumargs
132
133 call get_command_argument( iindex, sbuf )
134
135 if ( sbuf(1:13) .eq. "--output_dir=" ) then
136
137 soutputdirectoryname = trim( sbuf(14:) )
138 ilen = len_trim( soutputdirectoryname )
139
140 ! if there is no trailing "/", append one so we can form (more) fully
141 ! qualified filenames later
142 if ( .not. soutputdirectoryname(ilen:ilen) .eq. os_native_path_delimiter ) &
143 soutputdirectoryname = trim(soutputdirectoryname)//os_native_path_delimiter
144
145 elseif( sbuf(1:15) .eq."--random_start=" ) then
146
147 random_start = int( asint( sbuf(16:) ), c_long )
148
149 elseif( sbuf(1:17) .eq."--number_of_sims=" ) then
150
151 number_of_simulations = asint( sbuf(18:) )
152
153 elseif ( sbuf(1:16) .eq. "--output_prefix=" ) then
154
155 soutputprefixname = trim( sbuf(17:) )
156 ilen = len_trim( soutputprefixname )
157
158 elseif ( sbuf(1:11) .eq. "--data_dir=" ) then
159
160 sdatadirectoryname = sbuf(12:)
161 ilen = len_trim( sdatadirectoryname )
162
163 ! if there is no trailing "/", append one so we can form (more) fully
164 ! qualified filenames later
165 if ( .not. sdatadirectoryname(ilen:ilen) .eq. os_native_path_delimiter ) &
166 sdatadirectoryname = trim(sdatadirectoryname)//os_native_path_delimiter
167
168 elseif ( sbuf(1:14) .eq. "--logfile_dir=" ) then
169 slogfiledirectoryname = sbuf(15:)
170 ilen = len_trim( slogfiledirectoryname )
171
172 ! if there is no trailing "/", append one so we can form (more) fully
173 ! qualified filenames later
174 if ( .not. slogfiledirectoryname(ilen:ilen) .eq. os_native_path_delimiter ) &
175 slogfiledirectoryname = trim(slogfiledirectoryname)//os_native_path_delimiter
176
177 elseif ( sbuf(1:13) .eq. "--lookup_dir=" ) then
178
179 slookuptabledirectoryname = sbuf(14:)
180 ilen = len_trim( slookuptabledirectoryname )
181
182 ! if there is no trailing "/", append one so we can form (more) fully
183 ! qualified filenames later
184 if ( .not. slookuptabledirectoryname(ilen:ilen) .eq. os_native_path_delimiter ) &
185 slookuptabledirectoryname = trim(slookuptabledirectoryname)//os_native_path_delimiter
186
187 elseif ( sbuf(1:19) .eq. "--lookup_table_dir=" ) then
188
189 slookuptabledirectoryname = sbuf(20:)
190 ilen = len_trim( slookuptabledirectoryname )
191
192 ! if there is no trailing "/", append one so we can form (more) fully
193 ! qualified filenames later
194 if ( .not. slookuptabledirectoryname(ilen:ilen) .eq. os_native_path_delimiter ) &
195 slookuptabledirectoryname = trim(slookuptabledirectoryname)//os_native_path_delimiter
196
197 elseif ( sbuf(1:19) .eq. "--weather_data_dir=" ) then
198
199 sweatherdatadirectoryname = sbuf(20:)
200 ilen = len_trim( sweatherdatadirectoryname )
201
202 ! if there is no trailing "/", append one so we can form (more) fully
203 ! qualified filenames later
204 if ( .not. sweatherdatadirectoryname(ilen:ilen) .eq. os_native_path_delimiter ) &
205 sweatherdatadirectoryname = trim(sweatherdatadirectoryname)//os_native_path_delimiter
206
207 else
208
209 ! no match on the command-line argument flags; this must be a control file
210 call slcontrolfiles%append( trim( sbuf ) )
211
212 endif
213
214 enddo
215
216 ! open and initialize logfiles
217
218 ! allow for modification of the location in which logfiles will be written
219 if ( len_trim( slogfiledirectoryname ) /= 0 ) then
220 call logs%set_output_directory( trim( slogfiledirectoryname ) )
221 elseif ( len_trim( soutputdirectoryname ) /= 0 ) then
222 call logs%set_output_directory( trim( soutputdirectoryname ) )
223 endif
224
225 call logs%initialize( iloglevel = log_debug )
226
228
229 call logs%write( smessage='Grid data directory name set to:',lecho=.true._c_bool )
230 call logs%write( smessage='"'//trim( sdatadirectoryname )//'"', itab=4,lecho=.true._c_bool )
231 call logs%write( smessage='Lookup table directory name set to:',lecho=.true._c_bool )
232 call logs%write( smessage='"'//trim( slookuptabledirectoryname )//'"', itab=4,lecho=.true._c_bool )
233 call logs%write( smessage='Weather data directory (precip, tmin, tmax grids) name set to: ',lecho=.true._c_bool )
234 call logs%write( '"'//trim( sweatherdatadirectoryname )//'"',itab=4,lecho=.true._c_bool )
235
236 call logs%write( smessage='Output will be written to: ',lecho=.true._c_bool )
237 call logs%write( '"'//trim( soutputdirectoryname )//'"',itab=4,lecho=.true._c_bool )
238 call logs%write( smessage='Output file prefix set to:',lecho=.true._c_bool )
239 call logs%write( smessage='"'//trim( soutputprefixname )//'"', itab=4, lecho=.true._c_bool )
240
241 if (random_start >0) &
242 call logs%write(smessage="Pseudo-random numbers will be pulled from index number " &
243 //trim(ascharacter(random_start))//" in the generated series.", &
244 lecho=.true._c_bool )
245
246 if ( number_of_simulations > 1) &
247
248 call logs%write(smessage="Running multiple simulations; assuming method of fragments " &
249 //"was selected as the precipitation method.", &
250 lecho=.true._c_bool )
251
252 ! idea is that there may be multiple control files fed to SWB; all of the files
253 ! will be munged and the entries added to the CF_DICT dictionary
254 do iindex=1, slcontrolfiles%count
255 ! read control file
256 call read_control_file( slcontrolfiles%get( iindex ) )
257 enddo
258
259 call slcontrolfiles%clear()
260
261 call initialize_all( soutputprefixname, soutputdirectoryname, &
262 sdatadirectoryname, slookuptabledirectoryname, &
263 sweatherdatadirectoryname )
264
265 call runtimer%stop()
266 call runtimer%calc_time_values("split")
267 call logs%write(smessage="Time spent initializing simulation: " &
268 //trim( runtimer%get_pretty()), lecho=.true._c_bool, &
269 ilinesbefore=1, ilinesafter=1 )
270
271 call runtimer%start()
272
273 if ( number_of_simulations > 1) then
274
275 call iterate_over_multiple_simulation_days(model, number_of_simulations)
276
277 else
278
280
281 endif
282
283 call runtimer%stop()
284 call runtimer%calc_time_values("split")
285 call logs%write(smessage="Time spent running simulation: " &
286 //trim( runtimer%get_pretty()), lecho=.true._c_bool, &
287 ilinesbefore=1)
288 call runtimer%calc_time_values("elapsed")
289 call logs%write(smessage="Total runtime : " &
290 //trim( runtimer%get_pretty()), lecho=.true._c_bool, &
291 ilinesafter=1 )
292 call logs%close()
293
294end program main
program main
Main program which references all other modules; execution begins here.
Definition main.F90:9
This module contains physical constants and convenience functions aimed at performing unit conversion...
logical(c_bool), parameter, public true
subroutine log_provisional_disclaimer()
subroutine write_provisional_disclaimer()
type(logfile_t), public logs
Definition logfiles.F90:62
type(model_domain_t), public model
subroutine, public read_control_file(sfilename)
subroutine, public initialize_all(output_prefix, output_dirname, data_dirname, lookup_table_dirname, weather_data_dirname)
subroutine, public iterate_over_multiple_simulation_days(cells, number_of_simulations)
subroutine, public iterate_over_simulation_days(cells)
Definition timer.F90:1