Skip to content

Commit 14e246e

Browse files
committed
Find forcing files using file templates and dates. COSIMA/access-om2#242
1 parent fc4f4e0 commit 14e246e

File tree

3 files changed

+46
-23
lines changed

3 files changed

+46
-23
lines changed

libforcing/src/forcing_field.F90

+4-9
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ module forcing_field_mod
1717
integer, parameter, public :: FORCING_FIELD_DOMAIN_NONE = 0
1818
integer, parameter, public :: FORCING_FIELD_DOMAIN_ATMOSPHERE = 10
1919
integer, parameter, public :: FORCING_FIELD_DOMAIN_LAND = 20
20-
integer, parameter, private :: DAYS_IN_MONTH = (/ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 /)
2120

2221
type, public :: forcing_field
2322
character(len=64), dimension(:), allocatable :: names
@@ -61,7 +60,7 @@ subroutine forcing_field_init(self, name_list, filename_template_list, cname, do
6160
character(len=9), intent(out) :: calendar
6261

6362
character(len=1024) :: filename
64-
integer :: num_file_inputs, i, end_day
63+
integer :: num_file_inputs, i
6564

6665
num_file_inputs = size(name_list)
6766

@@ -83,10 +82,8 @@ subroutine forcing_field_init(self, name_list, filename_template_list, cname, do
8382
self%product_name = trim(product_name)
8483
self%logger => loggerin
8584

86-
end_day = DAYS_IN_MONTH(start_date%getMonth)
87-
8885
do i=1, num_file_inputs
89-
filename = find_filename_for_date(self%filename_templates(i),
86+
filename = filename_for_date(self%filename_templates(i), &
9087
start_date)
9188
call self%ncvars(i)%init(self%names(i), filename)
9289
enddo
@@ -128,10 +125,8 @@ subroutine forcing_field_update(self, forcing_date, experiment_date)
128125
num_file_inputs = size(self%ncvars)
129126

130127
do i=1, num_file_inputs
131-
filename = filename_for_date(self%filename_templates(i),
132-
forcing_date%getYear(),
133-
forcing_date%getMonth(),
134-
start_day, end_day)
128+
filename = filename_for_date(self%filename_templates(i), &
129+
forcing_date)
135130
call assert(trim(filename) /= '', "File not found: "//filename)
136131
if (trim(filename) /= trim(self%ncvars(i)%filename)) then
137132
call self%ncvars(i)%refresh(filename)

libforcing/src/forcing_perturbation.F90

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module forcing_perturbation_mod
33
use error_handler, only : assert
44
use ncvar_mod, only : ncvar_type => ncvar
55
use datetime_module, only : datetime
6-
use util_mod, only : filename_for_year
6+
use util_mod, only : filename_for_date
77

88
implicit none
99
private
@@ -75,8 +75,8 @@ subroutine forcing_perturbation_load(self, forcing_date, experiment_date, &
7575
date = forcing_date
7676
endif
7777

78+
filename = filename_for_date(self%filename_template, date)
7879
if (.not. self%initialised) then
79-
filename = filename_for_year(self%filename_template, date%getYear())
8080

8181
if (self%dimension_type == FORCING_PERTURBATION_DIMENSION_SPATIAL) then
8282
call self%ncvar%init(self%name, filename, &
@@ -96,7 +96,6 @@ subroutine forcing_perturbation_load(self, forcing_date, experiment_date, &
9696
return
9797
endif
9898

99-
filename = filename_for_year(self%filename_template, date%getYear())
10099
call assert(trim(filename) /= '', "File not found: "//filename)
101100
if (trim(filename) /= trim(self%ncvar%filename)) then
102101
call self%ncvar%refresh(filename)

libforcing/src/util.F90

+40-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
module util_mod
22

33
use netcdf
4+
use datetime_module, only : datetime
45
use error_handler, only : assert
56
use, intrinsic :: iso_fortran_env, only : stderr=>error_unit
67

78
implicit none
89

9-
integer, parameter, private :: DAYS_IN_MONTH = (/ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 /)
10+
integer, dimension(12), parameter, private :: DAYS_IN_MONTH = (/ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 /)
1011

1112
contains
1213

@@ -171,24 +172,52 @@ subroutine get_var_dims(ncid, varid, ndims, nx, ny, time)
171172
! substrings. This is very specifically designed to handle the kinds
172173
! of filenames used for JRA55 and ERA5 atmospheric forcings.
173174

174-
function find_filename_for_year_month(filename_template, year, month)
175+
function filename_for_date(filename_template, date)
175176
character(len=*), intent(in) :: filename_template
176-
integer, intent(in) :: year, month
177+
type(datetime), intent(in) :: date
177178

178-
integer :: start_day, end_day
179+
integer :: year, month, start_day, end_day
179180
character(len=1024) :: filename_for_date
180181
character(len=4) :: year_str, yearp1_str
181182
character(len=2) :: month_str, start_day_str, end_day_str
182183

184+
year = date%getYear()
185+
month = date%getMonth()
186+
183187
write(year_str, "(I4)") year
184188
write(yearp1_str, "(I4)") year+1
185-
186-
filename_for_year = replace_text(filename, "{{ year }}", year_str)
187-
filename_for_year = replace_text(filename_for_year, "{{year}}", year_str)
188-
filename_for_year = replace_text(filename_for_year, "{{ year+1 }}", yearp1_str)
189-
filename_for_year = replace_text(filename_for_year, "{{year+1}}", yearp1_str)
190-
191-
endfunction find_filename_for_year_month
189+
write(month_str, "(I2)") month
190+
191+
start_day = 1
192+
end_day = DAYS_IN_MONTH(month)
193+
write(start_day_str, "(I2)") start_day
194+
write(end_day_str, "(I2)") end_day
195+
196+
filename_for_date = replace_text(filename_template, &
197+
"{{ year }}", year_str)
198+
filename_for_date = replace_text(filename_for_date, &
199+
"{{year}}", year_str)
200+
filename_for_date = replace_text(filename_for_date, &
201+
"{{ year+1 }}", yearp1_str)
202+
filename_for_date = replace_text(filename_for_date, &
203+
"{{year+1}}", yearp1_str)
204+
205+
filename_for_date = replace_text(filename_for_date, &
206+
"{{ month }}", month_str)
207+
filename_for_date = replace_text(filename_for_date, &
208+
"{{month}}", month_str)
209+
210+
filename_for_date = replace_text(filename_for_date, &
211+
"{{ start_day }}", start_day_str)
212+
filename_for_date = replace_text(filename_for_date, &
213+
"{{start_day}}", start_day_str)
214+
215+
filename_for_date = replace_text(filename_for_date, &
216+
"{{ end_day }}", end_day_str)
217+
filename_for_date = replace_text(filename_for_date, &
218+
"{{end_day}}", end_day_str)
219+
220+
endfunction filename_for_date
192221

193222

194223
end module util_mod

0 commit comments

Comments
 (0)