Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding datetime in rpointer file with backward compatibility #486

Merged
merged 3 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions route/build/cpl/RtmMod.F90
Original file line number Diff line number Diff line change
Expand Up @@ -811,29 +811,41 @@ SUBROUTINE restFile_read_pfile( pnamer )
USE RtmFileUtils, ONLY: relavu, getavu, opnfil
USE RtmVar, ONLY: inst_suffix
USE public_var, ONLY: rpntfil
USE globalData, ONLY: simDatetime

implicit none
! Arguments:
character(len=*), intent(out) :: pnamer ! full path of restart file
! Local variables:
integer :: i ! indices
integer :: nio ! restart unit
integer :: status ! substring check status
character(len=256) :: locfn ! Restart pointer file name
integer :: i ! indices
integer :: nio ! restart unit
integer :: sec_in_day ! time in second
character(len=17) :: timestamp ! datetime string in yyyy-mm-dd-sssss
integer :: status ! substring check status
character(len=256) :: locfn ! Restart pointer file name
!--------------------------------------------------------

if (masterproc) then
write(iulog,*) 'Reading restart pointer file....'
endif

nio = getavu()
locfn = './'// trim(rpntfil)//trim(inst_suffix)
nio = getavu() ! get available unit number
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note, that new FORTRAN constructs allow you to NOT need getavu() anymore. There's a way to open a file with an available unit.

This would be a good modernization effort to do. But, not completely required.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the only thing in RtmFileUtils that is maybe useful is opnfil, but the use of getavu and relavu could be removed and handled within opnfil.

Copy link
Collaborator Author

@nmizukami nmizukami Oct 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for suggestions on ascii file handling (#490). I will clean this up with another PR. This RtmFileUtils and ascii_utils could be merged and several routines (getavu and relavu, getSpareUnit) can be removed.


! construct rpoint file name with datetime - simDatetime has three datetime at previous(0), current(1) and next(2) time stamp
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change rpoint to rpointer

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

sec_in_day = simDatetime(1)%hour()*60*60+simDatetime(1)%minute()*60+nint(simDatetime(1)%sec())
write(timestamp,'(".",I4.4,"-",I2.2,"-",I2.2,"-",I5.5)') &
simDatetime(1)%year(),simDatetime(1)%month(),simDatetime(1)%day(),sec_in_day
locfn = './'// trim(rpntfil)//trim(inst_suffix)//timestamp
inquire (file=locfn,exist=lexist)
if (.not. lexist) then ! backward compatibility - rpointer file w/o datetime
locfn = './'// trim(rpntfil)//trim(inst_suffix)
end if
call opnfil (locfn, nio, 'f')
read (nio,'(a256)') pnamer
call relavu (nio)

if (masterproc) then
write(iulog,*) 'Reading restart data.....'
write(iulog,*) 'Reading restart data: ', trim(pnamer)
write(iulog,'(72a1)') ("-",i=1,60)
end if

Expand Down
26 changes: 20 additions & 6 deletions route/build/src/io_rpointfile.f90
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ MODULE io_rpointfile
USE globalData, ONLY: masterproc
USE globalData, ONLY: hfileout, hfileout_gage
USE globalData, ONLY: rfileout
USE datetime_data, ONLY: datetime ! datetime data class

implicit none

Expand All @@ -18,20 +19,33 @@ MODULE io_rpointfile
! *********************************************************************
! public subroutine: main routine to define new output file
! *********************************************************************
SUBROUTINE io_rpfile(mode, ierr, message)
SUBROUTINE io_rpfile(mode, ierr, message, curDatetime)

implicit none
! argument variables
character(1), intent(in) :: mode ! io mode: 'r' or 'w'
integer(i4b), intent(out) :: ierr ! error code
character(*), intent(out) :: message ! error message
character(1), intent(in) :: mode ! io mode: 'r' or 'w'
integer(i4b), intent(out) :: ierr ! error code
character(*), intent(out) :: message ! error message
type(datetime), intent(in), optional :: curDatetime ! current datetime
integer :: sec_in_day ! time in second
character(len=17) :: timestamp ! datetime string in yyyy-mm-dd-sssss
character(len=strLen) :: rpntfil_path ! rpointer file path

ierr=0; message='io_rpfile/'

if (present(curDatetime)) then
sec_in_day = curDatetime%hour()*60*60+curDatetime%minute()*60+nint(curDatetime%sec())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change 60*60 to nint(secphour) and 60 to nint(secpmin) in public_vars

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

write(timestamp,'(".",I4.4,"-",I2.2,"-",I2.2,"-",I5.5)') &
curDatetime%year(),curDatetime%month(),curDatetime%day(),sec_in_day
rpntfil_path = trim(restart_dir)//trim(rpntfil)//timestamp
else
rpntfil_path = trim(restart_dir)//trim(rpntfil)
end if

select case(mode)
case("w")
if (masterproc) then
open(1, file = trim(restart_dir)//trim(rpntfil), status='replace', action='write')
open(1, file = trim(rpntfil_path), status='replace', action='write')
write(1,'(a)') trim(rfileout)
write(1,'(a)') trim(hfileout)
if (outputAtGage) then
Expand All @@ -40,7 +54,7 @@ SUBROUTINE io_rpfile(mode, ierr, message)
close(1)
end if
case("r")
open(1, file = trim(restart_dir)//trim(rpntfil), status='old', action='read')
open(1, file = trim(rpntfil_path), status='old', action='read')
read(1, '(A)') rfileout
read(1, '(A)') hfileout
if (outputAtGage) then
Expand Down
8 changes: 7 additions & 1 deletion route/build/src/write_restart_pio.f90
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ END SUBROUTINE restart_alarm
! *********************************************************************
SUBROUTINE restart_output(ierr, message)

USE globalData, ONLY: simDatetime ! previous and current model time
USE globalData, ONLY: runMode ! run options: standalone or cesm-coupling
USE io_rpointfile, ONLY: io_rpfile

implicit none
Expand All @@ -188,7 +190,11 @@ SUBROUTINE restart_output(ierr, message)
call write_state_nc(rfileout, pioFileDescState, ierr, cmessage)
if(ierr/=0)then; message=trim(message)//trim(cmessage); return; endif

call io_rpfile('w', ierr, cmessage)
if (trim(runMode)=='cesm-coupling') then
call io_rpfile('w', ierr, cmessage, curDatetime=simDatetime(1))
else
call io_rpfile('w', ierr, cmessage)
end if
if(ierr/=0)then; message=trim(message)//trim(cmessage); return; endif

END SUBROUTINE restart_output
Expand Down