subroutine Find_M_files_for_currTime (currTime, &
obsfile_start_time, obsfile_end_time, obsfile_interval, &
epoch_frequency, file_template, M, filenames, &
T_offset_in_file_content, rc)
implicit none
type(ESMF_Time), intent(in) :: currTime
type(ESMF_Time), intent(in) :: obsfile_start_time, obsfile_end_time
type(ESMF_TimeInterval), intent(in) :: obsfile_interval, epoch_frequency
character(len=*), intent(in) :: file_template
integer, intent(out) :: M
character(len=ESMF_MAXSTR), intent(inout) :: filenames(:)
type(ESMF_TimeInterval), intent(in), optional :: T_offset_in_file_content
integer, optional, intent(out) :: rc
type(ESMF_Time) :: T1, Tn
type(ESMF_Time) :: cT1
type(ESMF_Time) :: Ts, Te
type(ESMF_TimeInterval) :: dT1, dT2, dTs, dTe
type(ESMF_TimeInterval) :: Toff
real(ESMF_KIND_R8) :: dT0_s, dT1_s, dT2_s
real(ESMF_KIND_R8) :: s1, s2
character(len=ESMF_MAXSTR) :: test_file
integer :: obsfile_Ts_index, obsfile_Te_index
integer :: n1, n2
integer :: i, j
integer :: status
logical :: exist
!__ s1. Arithmetic index list based on s,e,interval
!
if (present(T_offset_in_file_content)) then
Toff = T_offset_in_file_content
else
call ESMF_TimeIntervalSet(Toff, h=0, m=0, s=60, rc=status)
endif
! T1 = obsfile_start_time + Toff
! Tn = obsfile_end_time + Toff
T1 = obsfile_start_time
Tn = obsfile_end_time
cT1 = currTime
dT1 = currTime - T1
dT2 = currTime + epoch_frequency - T1
call ESMF_TimeIntervalGet(obsfile_interval, s_r8=dT0_s, rc=status)
call ESMF_TimeIntervalGet(dT1, s_r8=dT1_s, rc=status)
call ESMF_TimeIntervalGet(dT2, s_r8=dT2_s, rc=status)
n1 = floor (dT1_s / dT0_s) - 1 ! downshift by 1, as filename does not guarantee accurate time
n2 = floor (dT2_s / dT0_s)
! print*, 'ck dT0_s, dT1_s, dT2_s', dT0_s, dT1_s, dT2_s
! print*, '1st n1, n2', n1, n2
obsfile_Ts_index = n1
obsfile_Te_index = n2
!__ s2. further test file existence
!
j=0
do i= n1, n2
test_file = get_filename_from_template_use_index &
(obsfile_start_time, obsfile_interval, &
i, file_template, exist, rc=rc)
if (exist) then
j=j+1
filenames(j) = test_file
end if
end do
M=j
_ASSERT ( M < size(filenames) , 'code crash, number of files exceeds upper bound')
_ASSERT (M/=0, 'M is zero, no files found for currTime')
_RETURN(_SUCCESS)
end subroutine Find_M_files_for_currTime