-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinout_mod.f90
323 lines (261 loc) · 12.9 KB
/
inout_mod.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
!***********************************************************************
! DESCRIPTION:
! Module for file I/O
!
! REVISION HISTORY:
! Prototype 01/2015 by Li Zhenkun, SCC
! Revised 10/2018 by Li Zhenkun, SCC
!
!***********************************************************************
module inout_mod
implicit none
public :: get_cfs_field
public :: get_prec_anom
private :: get_prec
public :: write_out
contains
!----------------------------------------------------------------------
! Public subroutine to get CFSv2 forecast in a given date, leading
! days and average days
!----------------------------------------------------------------------
subroutine get_cfs_field(input_dir, path_separator, var_num, var_names, year, mon, day, lead_days, ave_days, west, south, nlon, nlat, var, ierr)
implicit none
!------------------------------------------------------------------
! dummy arguments
!------------------------------------------------------------------
character(len=*), intent(in) :: input_dir
character(len=*), intent(in) :: path_separator
integer, intent(in) :: var_num
character(len=*), intent(in) :: var_names(var_num)
integer, intent(in) :: year, mon, day
integer, intent(in) :: lead_days, ave_days
integer, intent(in) :: west, south
integer, intent(in) :: nlon, nlat
real(4), intent(out) :: var(nlon*nlat*var_num)
integer, intent(out) :: ierr
!------------------------------------------------------------------
! local variables
!------------------------------------------------------------------
character(len=*), parameter :: subname = '(get_cfs_field)'
character(4) :: cyear
character(2) :: cmon, cday
integer :: istart, iend, jstart, jend
character(256) :: filename
integer :: iunit
logical :: lexist
integer :: status
real(4) :: var_whole(360, 181, ave_days)
real(4) :: var_region(nlon, nlat, ave_days)
real(4) :: var_local(nlon, nlat, var_num)
integer :: ivar, iday, irec
write (cyear, '(i4)') year
cmon = char(mon/10 + 48)//char(mod(mon, 10) + 48)
cday = char(day/10 + 48)//char(mod(day, 10) + 48)
istart = west + 1
iend = istart + nlon -1
jstart = south + 90 + 1
jend = jstart + nlat - 1
ierr = 0
iunit = 101
do ivar = 1, var_num
filename = trim(input_dir)//path_separator//trim(var_names(ivar))//path_separator//trim(var_names(ivar))//'-'//cyear//cmon//cday//'-cfs-forecast.dat'
inquire( file = filename, exist = lexist )
if ( .not. lexist ) then
write( 6, '(4a)' ) subname, 'WARNING: ', trim(filename) , ' is NOT available'
ierr = 1
return
end if
open( unit = iunit, file = filename, form = 'unformatted', access = 'direct', recl = 360*181, iostat = status )
if ( status /= 0 ) then
write( 6, '(3a)' ) subname, 'WARNING: can NOT rightly open file ', trim(filename)
ierr = 1
return
end if
irec = lead_days - 1
do iday = 1, ave_days
read(iunit, rec = irec) var_whole(:, :, iday)
irec = irec + 1
end do
var_region(:, :, :) = var_whole(istart:iend, jstart:jend, :)
var_local(:, :, ivar) = sum(var_region, dim=3) / real(ave_days)
close(iunit)
end do
var = reshape(var_local, (/nlon*nlat*var_num/))
end subroutine get_cfs_field
!----------------------------------------------------------------------
! Public subroutine to get observed precipitation anmoly percentage
! in a given date, leading days and average days
!----------------------------------------------------------------------
subroutine get_prec_anom(input_dir, path_separator, stn_num, year, mon, day, lead_days, ave_days, prec_anom, ierr)
use date_mod, only : new_date
implicit none
!------------------------------------------------------------------
! dummy arguments
!------------------------------------------------------------------
character(len=*), intent(in) :: input_dir
character(len=*), intent(in) :: path_separator
integer, intent(in) :: stn_num
integer, intent(in) :: year, mon, day
integer, intent(in) :: lead_days, ave_days
real(4), intent(out) :: prec_anom(stn_num)
integer, intent(out) :: ierr
!------------------------------------------------------------------
! local variables
!------------------------------------------------------------------
integer :: start_year, start_mon, start_day
real(4) :: prec_hist(stn_num, 30)
real(4) :: prec(stn_num)
real(4) :: prec_mean(stn_num)
integer :: iyear
call new_date(year, mon, day, lead_days, start_year, start_mon, start_day)
if ( start_mon == 2 .and. start_day == 29 ) then
start_mon = 3
start_day = 1
end if
do iyear = 1981, 2010
call get_prec(input_dir, path_separator, stn_num, iyear, start_mon, start_day, ave_days, prec_hist(:, iyear-1980), ierr)
if ( ierr /= 0 ) then
return
end if
end do
prec_mean = sum(prec_hist, dim=2) / 30.
call get_prec(input_dir, path_separator, stn_num, year, start_mon, start_day, ave_days, prec, ierr)
if ( ierr /= 0 ) then
return
end if
prec_anom = 100. * (prec - prec_mean) / prec_mean
end subroutine get_prec_anom
!----------------------------------------------------------------------
! Private subroutine to get observed precipitation in a given date,
! leading days and average days
!----------------------------------------------------------------------
subroutine get_prec(input_dir, path_separator, stn_num, year, mon, day, ave_days, prec, ierr)
implicit none
!------------------------------------------------------------------
! dummy arguments
!------------------------------------------------------------------
character(len=*), intent(in) :: input_dir
character(len=*), intent(in) :: path_separator
integer, intent(in) :: stn_num
integer, intent(in) :: year, mon, day
integer, intent(in) :: ave_days
real(4), intent(out) :: prec(stn_num)
integer, intent(out) :: ierr
!------------------------------------------------------------------
! local variables
!------------------------------------------------------------------
character(len=*), parameter :: subname = '(get_prec)'
character(256) :: filename
integer :: iunit, status, line
logical :: lexist, flag
integer :: year_in, mon_in, day_in
real(4) :: prec_in(stn_num), prec_days_in(stn_num, ave_days)
integer :: iday
ierr = 0
iunit = 102
filename = trim(input_dir)//path_separator//'523stn-daily-1980-2013.txt'
inquire( file = filename, exist = lexist )
if ( .not. lexist ) then
write( 6, '(4a)' ) subname, 'ERROR: ', trim(filename) , ' is NOT available, please check whether or not file exist'
ierr = 1
return
end if
open( unit = iunit, file = filename, form = 'formatted', status = 'old', action = 'read', iostat = status )
if ( status /= 0 ) then
write( 6, '(3a)' ) subname, 'ERROR: can NOT rightly open file ', trim(filename)
ierr = 1
return
end if
line = 1
flag = .false.
do while ( .true. )
read( iunit, *, iostat = status ) year_in, mon_in, day_in, prec_in
if ( status /= 0 ) then
write( 6, '(4a, i)' ) subname, 'ERROR: can NOT read precipitation in ', trim(filename), ' for line ', line
ierr = 1
return
end if
if ( year_in == year .and. mon_in == mon .and. day_in == day ) then
prec_days_in(:, 1) = prec_in
flag = .true.
do iday = 2, ave_days
read( iunit, *, iostat = status ) year_in, mon_in, day_in, prec_in
if ( status /= 0 ) then
write( 6, '(4a, i)' ) subname, 'ERROR: can NOT read precipitation in ', trim(filename), ' for line ', line
ierr = 1
return
end if
prec_days_in(:, iday) = prec_in
line = line + 1
end do
exit
end if
line = line + 1
end do
close( iunit )
if ( flag == .false. ) then
write( 6, '(4a, i4, 1x, i2, 1x, i2)' ) subname, 'ERROR: can NOT find precipitation data in ', trim(filename), ' for ', year, mon, day
ierr = 1
return
end if
prec = sum(prec_days_in, dim=2) / real(ave_days)
end subroutine get_prec
!----------------------------------------------------------------------
! Public subroutine to write forecase results to ascii file
!----------------------------------------------------------------------
subroutine write_out(output_dir, path_separator, fct_date, lead_days, ave_days, stn_num, fct_num, prec_fct, ierr)
implicit none
!------------------------------------------------------------------
! dummy arguments
!------------------------------------------------------------------
character(len=*), intent(in) :: output_dir
character(len=*), intent(in) :: path_separator
integer, intent(in) :: fct_date
integer, intent(in) :: lead_days
integer, intent(in) :: ave_days
integer, intent(in) :: stn_num
integer, intent(in) :: fct_num
real(4), intent(in) :: prec_fct(stn_num, fct_num)
integer, intent(out) :: ierr
!------------------------------------------------------------------
! local variables
!------------------------------------------------------------------
character(len=*), parameter :: subname = '(write_out)'
real(4) :: prec_fct_ens(stn_num)
real(4) :: prec_fct_prob(stn_num)
integer :: ifct, istn
integer :: iunit
character(256) :: filename
integer :: status
character(8) :: fct_date_str
character(2) :: lead_days_str, ave_days_str
prec_fct_ens = sum(prec_fct, dim=2) / real(fct_num)
do istn = 1, stn_num
prec_fct_prob(istn) = 0.
do ifct = 1, fct_num
if ( prec_fct(istn, ifct) > 0. ) then
prec_fct_prob(istn) = prec_fct_prob(istn) + 1.
end if
end do
end do
prec_fct_prob = 100. * prec_fct_prob / real(fct_num)
ierr = 0
iunit = 103
write( fct_date_str, '(i8)' ) fct_date
lead_days_str = char(lead_days/10+48)//char(mod(lead_days,10)+48)
ave_days_str = char(ave_days/10+48)//char(mod(ave_days,10)+48)
filename = trim(output_dir)//path_separator//fct_date_str//'-lead-'//lead_days_str//'-for-'//ave_days_str//'-forecast.txt'
open( unit = iunit, file = filename, form = 'formatted', iostat = status )
if ( status /= 0 ) then
write( 6, '(3a)' ) subname, 'ERROR: can NOT rightly open file ', trim(filename)
ierr = 1
return
end if
do ifct = 1, fct_num
write( iunit, '(<stn_num>(f8.2, 1x))' ) prec_fct(:, ifct)
end do
write( iunit, '(<stn_num>(f8.2, 1x))' ) prec_fct_ens
write( iunit, '(<stn_num>(f8.2, 1x))' ) prec_fct_prob
close( iunit )
end subroutine write_out
end module inout_mod