-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHMC_Module_Data_Restart_Point.f90
369 lines (295 loc) · 20.3 KB
/
HMC_Module_Data_Restart_Point.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
!------------------------------------------------------------------------------------------
! File: HMC_Module_Data_Restart_Point.f90
! Author(s): Fabio Delogu, Francesco Silvestro, Simone Gabellani
!
! Created on May 20, 2015, 10:15 AM
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Module Header
module HMC_Module_Data_Restart_Point
!------------------------------------------------------------------------------------------
! External module(s) for all subroutine in this module
use HMC_Module_Namelist, only: oHMC_Namelist
use HMC_Module_Vars_Loader, only: oHMC_Vars
use HMC_Module_Tools_Debug
use HMC_Module_Tools_Generic, only: HMC_Tools_Generic_ReplaceText, &
HMC_Tools_Generic_CreateFolder
! Implicit none for all subroutines in this module
implicit none
!------------------------------------------------------------------------------------------
contains
!------------------------------------------------------------------------------------------
! Subroutine to manage restart point data
subroutine HMC_Data_Restart_Point_Cpl( iID, sTime, &
iNDam, iNLake)
!------------------------------------------------------------------------------------------
! Variable(s)
integer(kind = 4) :: iID
integer(kind = 4) :: iNDam, iNLake
integer(kind = 4) :: iFlagRestart, iFlagTypeData_Restart
character(len = 700) :: sPathData_Restart
character(len = 700) :: sFileNameData_Restart
character(len = 700) :: sCommandCreateFolder
character(len = 19), intent(in) :: sTime
integer(kind = 4), dimension(iNDam, 2) :: a2iVarXYDam
real(kind = 4), dimension(iNDam) :: a1dVarVDam, a1dVarVMaxDam, a1dVarCodeDam
integer(kind = 4), dimension(iNLake, 2) :: a2iVarXYLake
real(kind = 4), dimension(iNLake) :: a1dVarVLake, a1dVarVMinLake, a1dVarCodeLake
logical :: bCheckRestart
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Initialize variable(s)
a2iVarXYDam = -9999; a2iVarXYLake = -9999;
a1dVarVDam = -9999.0; a1dVarVMaxDam = -9999.0; a1dVarCodeDam = -9999.0;
a1dVarVLake = -9999.0; a1dVarVMinLake = -9999.0; a1dVarCodeLake = -9999.0;
sFileNameData_Restart = "";
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Get global information
iFlagRestart = oHMC_Namelist(iID)%iFlagRestart
sPathData_Restart = oHMC_Namelist(iID)%sPathData_Restart_Point
iFlagTypeData_Restart = oHMC_Namelist(iID)%iFlagTypeData_Restart_Point
sCommandCreateFolder = oHMC_Namelist(iID)%sCommandCreateFolder
! Info start
call mprintf(.true., iINFO_Extra, ' Data :: Restart point ... ' )
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Check restart flag value
if (iFlagRestart == 1) then
!------------------------------------------------------------------------------------------
! Check dam or lake availability
if ( (iNDam.gt.0) .or. (iNLake.gt.0) ) then
!------------------------------------------------------------------------------------------
! Replace general path with specific time feature(s)
call HMC_Tools_Generic_ReplaceText(sPathData_Restart, '$yyyy', sTime(1:4))
call HMC_Tools_Generic_ReplaceText(sPathData_Restart, '$mm', sTime(6:7))
call HMC_Tools_Generic_ReplaceText(sPathData_Restart, '$dd', sTime(9:10))
call HMC_Tools_Generic_ReplaceText(sPathData_Restart, '$HH', sTime(12:13))
call HMC_Tools_Generic_ReplaceText(sPathData_Restart, '$MM', sTime(15:16))
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Create output folder
call HMC_Tools_Generic_CreateFolder(sCommandCreateFolder, sPathData_Restart, .true.)
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Subroutine for getting ASCII point data restart
if (iFlagTypeData_Restart == 1) then
!------------------------------------------------------------------------------------------
! Choosing data type
call mprintf(.true., iINFO_Extra, ' Using ASCII data restart point')
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Filename (StateStorage)
sFileNameData_Restart = trim(sPathData_Restart)//"hmc.state-point."// &
sTime(1:4)//sTime(6:7)//sTime(9:10)// &
sTime(12:13)//sTime(15:16)// &
".txt"
! Info filename
call mprintf(.true., iINFO_Verbose, ' Get filename (state point): '//trim(sFileNameData_Restart) )
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Call subroutine to get data point in ASCII format
call HMC_Data_Restart_Point_ASCII(iID, sTime, &
sFileNameData_Restart, &
iNDam, iNLake, &
a2iVarXYDam, &
a1dVarVDam, a1dVarVMaxDam, a1dVarCodeDam, &
a2iVarXYLake, &
a1dVarVLake, a1dVarVMinLake, a1dVarCodeLake, &
.false., bCheckRestart)
!------------------------------------------------------------------------------------------
endif
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Subroutine to write data point in UNKWOWN format
if (iFlagTypeData_Restart == 2) then
!------------------------------------------------------------------------------------------
! Choosing data type
call mprintf(.true., iERROR, ' Writing UNKNOWN data point state. Check settings file!')
!------------------------------------------------------------------------------------------
endif
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Check restart flag on data availability
if (bCheckRestart .eqv. .true.) then
!------------------------------------------------------------------------------------------
! Pass variable(s) to global workspace
oHMC_Vars(iID)%a1dVDam = a1dVarVDam
oHMC_Vars(iID)%a1dVMaxDam = a1dVarVMaxDam
oHMC_Vars(iID)%a1dCodeDam = a1dVarCodeDam
oHMC_Vars(iID)%a1dVLake = a1dVarVLake
oHMC_Vars(iID)%a1dVMinLake = a1dVarVMinLake
oHMC_Vars(iID)%a1dCodeLake = a1dVarCodeLake
! Info end
call mprintf(.true., iINFO_Extra, ' Data :: Restart point ... OK' )
!------------------------------------------------------------------------------------------
else
!------------------------------------------------------------------------------------------
! Exit message for not using restart data
call mprintf(.true., iINFO_Verbose, ' Restart flag selected but data are N/A (point data)')
! Info end
call mprintf(.true., iINFO_Extra, ' Data :: Restart point ... SKIPPED ' )
!------------------------------------------------------------------------------------------
endif
!------------------------------------------------------------------------------------------
else
!------------------------------------------------------------------------------------------
! Choosing data type
call mprintf(.true., iINFO_Verbose, ' Dam(s) and lake(s) are not available for this run!')
! Info end
call mprintf(.true., iINFO_Extra, ' Data :: Restart point ... SKIPPED' )
!------------------------------------------------------------------------------------------
endif
!------------------------------------------------------------------------------------------
else
!------------------------------------------------------------------------------------------
! Exit message for not using restart data
call mprintf(.true., iINFO_Verbose, ' No restart run selected (point data)')
! Info end
call mprintf(.true., iINFO_Extra, ' Data :: Restart point ... SKIPPED' )
!------------------------------------------------------------------------------------------
endif
!------------------------------------------------------------------------------------------
end subroutine HMC_Data_Restart_Point_Cpl
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Subroutine to write state point data in ASCII format
subroutine HMC_Data_Restart_Point_ASCII(iID, sTime, &
sFileNameData_State, &
iNDam, iNLake, &
a2iVarXYDam, &
a1dVarVDam, a1dVarVMaxDam, a1dVarCodeDam, &
a2iVarXYLake, &
a1dVarVLake, a1dVarVMinLake, a1dVarCodeLake, &
bFatalError, bCheckRestart)
!------------------------------------------------------------------------------------------
! Variable(s)
integer(kind = 4) :: iID, iD, iL
integer(kind = 4) :: iNDam, iNLake
character(len = 700) :: sFileNameData_State
character(len = 19) :: sTime
integer(kind = 4), dimension(iNDam, 2) :: a2iVarXYDam
real(kind = 4), dimension(iNDam) :: a1dVarVDam, a1dVarVMaxDam, a1dVarCodeDam
integer(kind = 4), dimension(iNLake, 2) :: a2iVarXYLake
real(kind = 4), dimension(iNLake) :: a1dVarVLake, a1dVarVMinLake, a1dVarCodeLake
logical :: bFatalError, bFileExist, bCheckRestart
integer(kind = 4) :: iRet, iErr
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Initialize variable(s)
bCheckRestart = .false.
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Info start
call mprintf(.true., iINFO_Extra, ' Data :: Restart point :: ASCII ... ' )
! Info filename(s) at each time step
call mprintf(.true., iINFO_Basic, ' Get (restart point) at time '//trim(sTime)//' ... ')
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Open file
open(unit = 20, file = trim(sFileNameData_State), status = 'old', iostat = iRet)
inquire (file = trim(sFileNameData_State), exist = bFileExist, iostat = iRet)
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Check file availability
if (.not. bFileExist ) then
!------------------------------------------------------------------------------------------
! Check fatal error condition
if (bFatalError) then
call mprintf(.true., iERROR, &
' INQUIRE FAILED :: state point ASCII ---> file not readable (filename: ' &
//trim(sFileNameData_State)//')')
else
call mprintf(.true., iWARN, &
' INQUIRE FAILED :: state point ASCII ---> file not readable (filename: ' &
//trim(sFileNameData_State)//')')
iErr = iRet
endif
bCheckRestart = .false.
!------------------------------------------------------------------------------------------
elseif ( bFileExist ) then
!------------------------------------------------------------------------------------------
! Check file opening
if (iRet == 0) then
!------------------------------------------------------------------------------------------
! Info
call mprintf(.true., iINFO_Extra, &
' READ OK :: state point ASCII ---> file readable (filename: '//trim(sFileNameData_State)//')')
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Write dam(s) data point
do iD = 1, iNDam
! Dam index coordinates (i,j)
read(20,*) a2iVarXYDam(iD,2), a2iVarXYDam(iD,1)
! Dam code (on choice matrix)
read(20,*) a1dVarCodeDam(iD)
! Dam maximum volume
read(20,*) a1dVarVMaxDam(iD)
! Dam volume
read(20,*) a1dVarVDam(iD)
! Blank space
read(20,*)
write(*,'(I3,1X, I3, 1X, I3, 1X, F30.11,1X, F30.11)') iD, a2iVarXYDam(iD,2), a2iVarXYDam(iD,1), &
a1dVarVDam(iD), a1dVarVMaxDam(iD)
enddo
! Write lake(s) data point
do iL = 1, iNLake
! Blank space
read(20,*)
! Lake index coordinates (i,j)
read(20,*)a2iVarXYLake(iL,2), a2iVarXYLake(iL,1)
! Lake code (on choice matrix)
read(20,*)a1dVarCodeLake(iL)
! Lake minimum volume (to guarantee discharge)
read(20,*)a1dVarVMinLake(iL)
! Lake volume
read(20,*)a1dVarVLake(iL)
write(*,'(I3,1X, I3, 1X, I3, 1X, F30.11,1X, F30.11)') iL, a2iVarXYLake(iL,2), a2iVarXYLake(iL,1), &
a1dVarVLake(iL), a1dVarVMinLake(iL)
enddo
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Close point state file
close(20)
! Info end
bCheckRestart = .true.
!------------------------------------------------------------------------------------------
elseif (iRet /= 0) then
!------------------------------------------------------------------------------------------
! Check fatal error
if (bFatalError) then
call mprintf(.true., iERROR, &
' READ FAILED :: state point ASCII ---> file not readable (filename: '//trim(sFileNameData_State)//')')
else
call mprintf(.true., iWARN, &
' READ FAILED :: state point ASCII ---> file not readable (filename: '//trim(sFileNameData_State)//')')
iErr = iRet
endif
bCheckRestart = .false.
!------------------------------------------------------------------------------------------
endif
!------------------------------------------------------------------------------------------
endif
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Info filename(s) at each time step
call mprintf(.true., iINFO_Basic, ' Get (restart point) at time '//trim(sTime)//' ... OK')
! Info start
call mprintf(.true., iINFO_Extra, ' Data :: Restart point :: ASCII ... OK' )
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Check restart
if (bCheckRestart .eqv. .true.) then
call mprintf(.true., iINFO_Basic, ' Data :: Restart point :: ASCII :: All variable(s) are loaded! ' )
bCheckRestart = .true.
else
call mprintf(.true., iINFO_Basic, ' Data :: Restart point :: ASCII :: Some/All variable(s) are N/A! ' )
call mprintf(.true., iWARN, ' Restart flag activated but some data restart are not available! ')
call mprintf(.true., iWARN, ' Restart point conditions are null! ')
bCheckRestart = .false.
endif
!------------------------------------------------------------------------------------------
end subroutine HMC_Data_Restart_Point_ASCII
!------------------------------------------------------------------------------------------
end module HMC_Module_Data_Restart_Point
!------------------------------------------------------------------------------------------