subroutine add_ancilcrdvar(hst, var)
implicit none
type(HIST_EACHVAR),intent(inout) :: hst
type(GT4_NAMED_REALARY),intent(in) :: var
!
integer :: rank
!
rank = var%rank
call HistoryAddVariable(var%name, var%dims(1:rank), end subroutine add_ancilcrdvar
subroutine put_ancilcrdvar(hst, var)
use dc_error, only: GT_ENOMATCHDIM, StoreError
implicit none
type(HIST_EACHVAR),intent(inout) :: hst
type(GT4_NAMED_REALARY),intent(in) :: var
!
integer :: rank,i,j
integer,allocatable :: idx(:)
real,pointer :: subset(:)
character(len=*), parameter :: subname = 'put_ancilcrdvar'
!
call BeginSub(subname)
rank = var%rank
allocate(idx(rank))
loopi: do i=1,rank
do j=1,hst%sprank
if ( var%dims(i) == hst%dims(j) ) then
idx(i)=j
cycle loopi
endif
enddo
call StoreError(GT_ENOMATCHDIM, subname)
enddo loopi
subset => make_slice(var%ary, rank, (/hst%aryshape(idx)/), (/hst%slfst(idx)/), (/hst%sllst(idx)/), (/hst%slstp(idx)/))
if (associated(subset)) then
call HistoryPut(var%name, subset, hst%h%hs)
else
call HistoryPut(var%name, var%ary, hst%h%hs)
endif
deallocate(idx)
call EndSub(subname)
end subroutine put_ancilcrdvar
subroutine HistoryAutoPut(name, vals, time)
implicit none
character(len=*), intent(in) :: name
real :: vals(*)
real :: time
!
type(HIST_EACHVAR),pointer :: hst
integer :: ith, j, rank
character(len=STRING) :: file_actual
real :: eps=3e-7, newest
type(GT_HISTORY),pointer :: hist
logical :: put_now
integer :: arysize
real,pointer :: subset(:)
character(len = *), parameter:: subname = 'HistoryAutoPut'
!
call BeginSub(subname, 'name=<%c>, time=<%r>', c1=trim(name), r=(/time/))
ith = 1
do while( histpl_find(HISTPOOL, name, ith, hst) )
put_now = whether_to_put_now( time, hst%time_last, if ( put_now ) then
if ( associated(hst%h%hs) ) then
call HistoryInquire(hst%h%hs, newest=newest)
if ( hst%newfile_interval > 0 .and. hst%time_to_start = hst%time_to_start + hst%newfile_interval
call HistoryClose(hst%h%hs)
nullify(hst%h%hs)
endif
endif
if (.not.associated(hst%h%hs)) then
if (hst%newfile_interval > 0) then
file_actual = merge_file_proc_time(hst%file,hst%proc, else
file_actual = merge_file_proc_time(hst%file,hst%proc)
endif
rank = hst%sprank + 1
allocate(hist) ! always new allocataion
call HistoryCreate( file_actual, trim(hst%title), hst%h%hs => hist
call HistoryAddVariable(name, hst%dims(1:rank), trim(hst%longname), trim(hst%units), if (associated(hst%attrs)) then
do j=1,size(hst%attrs)
call add_gt4_attribute(hst, hst%attrs(j))
enddo
endif
if (associated(hst%ancilcrdvars)) then
do j=1,size(hst%ancilcrdvars)
call add_ancilcrdvar(hst, hst%ancilcrdvars(j))
enddo
endif
do j=1,hst%sprank
subset => make_slice(hst%spcoordvars(j)%ary, 1, (/hst%aryshape(j)/), (/hst%slfst(j)/), (/hst%sllst(j)/), (/hst%slstp(j)/) )
if (associated(subset)) then
call HistoryPut(hst%dims(j), else
call HistoryPut(hst%dims(j), endif
enddo
if (associated(hst%ancilcrdvars)) then
do j=1,size(hst%ancilcrdvars)
call put_ancilcrdvar(hst, hst%ancilcrdvars(j))
enddo
endif
call HistorySetTime(time, hst%h%hs)
else
rank = hst%sprank + 1
if ( .not. HistoryHasVariable(hst%h%hs, name) ) then
call HistoryAddVariable(name, hst%dims(1:rank), trim(hst%longname), trim(hst%units), !" ここで HistorySetTime すると問題が起きるので前回に従う
if (associated(hst%attrs)) then
do j=1,size(hst%attrs)
call add_gt4_attribute(hst, hst%attrs(j))
enddo
endif
else
call HistorySetTime(time, hst%h%hs)
endif
endif
arysize = product(hst%aryshape(1:hst%sprank))
subset => make_slice(vals(1:arysize), hst%sprank, (/hst%aryshape/), (/hst%slfst/), (/hst%sllst/), (/hst%slstp/))
if (associated(subset)) then
call HistoryPut(name, subset, hst%h%hs)
else
call HistoryPut(name, vals(1:hst%size), hst%h%hs)
endif
hst%time_last = time
endif
enddo
call EndSub(subname)
end subroutine HistoryAutoPut
function HistoryAutoWhetherPutNow( name, time ) result(result)
implicit none
logical :: result
character(len=*), intent(in) :: name
real, intent(in) :: time
!
integer :: ith
type(HIST_EACHVAR),pointer :: hst
character(len = *), parameter:: subname = 'HistoryAutoWhetherPutNow'
logical :: put_now
!
call BeginSub(subname)
result = .false.
ith = 1
do while( histpl_find(HISTPOOL, name, ith, hst) )
put_now = whether_to_put_now( time, hst%time_last, if (put_now) then
result = .true.
exit
endif
enddo
call EndSub(subname)
end function HistoryAutoWhetherPutNow
function whether_to_put_now( time_now, time_last, time_to_start, put_interval, dt ) result(result)
implicit none
logical :: result
real, intent(in) :: time_now
real, intent(in) :: time_last
real, intent(in) :: time_to_start
real, intent(in) :: put_interval
real, intent(in) :: dt
!
real :: next_put_time
real :: eps
character(len = *), parameter:: subname = 'whether_to_put_now'
call BeginSub(subname)
eps = dt * 1e-3 ! allowable error in time in float
if (time_now < time_to_start - eps) then
result = .false.
return
end if
next_put_time = time_last + put_interval ! initially very small because
! of the init val of time_last
if ( time_now >= (next_put_time - eps) ) then
result = .true.
else
result = .false.
endif
call EndSub(subname)
end function whether_to_put_now
subroutine HistoryAutoCopyCreate( name, longname, units, file )
! use the result of the latest call of HistoryAutoCreate
character(len=*), intent(in) :: name
character(len=*), intent(in) :: longname
character(len=*), intent(in) :: units
character(len=*), intent(in),optional :: file
!
type(HIST_EACHVAR) :: hist
type(HIST_EACHVAR),pointer :: histpt
character(len = *), parameter:: subname = 'HistoryAutoCopyCreate'
!
call BeginSub(subname)
histpt => histpl_last(HISTPOOL)
hist = histpt ! copy the contents
if(present_and_not_empty(file)) then
hist%file = file
allocate(hist%h) ! always new allocation
nullify(hist%h%hs)
else
hist%h => histpt%h
endif
hist%name = name
hist%longname = longname
hist%units = units
call histpl_push(HISTPOOL, hist)
call EndSub(subname)
end subroutine HistoryAutoCopyCreate
subroutine HistoryAutoSetRunInfo( conventions, gt_version )
use dc_error, only: USR_EINT, StoreError
implicit none
character(len=*), intent(in), optional :: title, source, institution
character(len=*), intent(in), optional :: proc
character(len=*), intent(in), optional :: conventions, gt_version
!
character(len = *),parameter :: subname = "HistoryAutoSetRunInfo"
continue
call BeginSub(subname)
if (present(title)) com_title = title
if (present(source)) com_source = source
if (present(institution)) com_institution = institution
if (present(proc)) com_proc = proc
if (present(conventions)) com_conventions = conventions
if (present(gt_version)) com_gt_version = gt_version
call EndSub(subname)
end subroutine HistoryAutoSetRunInfo
subroutine HistoryAutoSetGrid( grid_label, aryshape, dims, axlongnames, axunits, axxtypes, use dc_error, only: USR_EINT, StoreError
implicit none
character(len=*), intent(in) :: grid_label
integer, intent(in) :: aryshape(:) ! size <= 3 (--> sprank)
character(len=*), intent(in) :: dims(:) !size == sprank+1
character(len=*), intent(in) :: axlongnames(:) !size == sprank+1
character(len=*), intent(in) :: axunits(:) !size == sprank+1
character(len=*), intent(in) :: axxtypes(:)
real, intent(in),optional :: coord1(:) ! must present if sprank>=1
real, intent(in),optional :: coord2(:) ! must present if sprank>=2
real, intent(in),optional :: coord3(:) ! must present if sprank>=3
type(GT4_NAMED_REALARY),intent(in),optional :: ancilcrdvars(:)
!
type(HIST_EACHVAR) :: hist
integer :: sprank
character(len = *),parameter :: subname = "HistoryAutoSetGrid"
continue
call BeginSub(subname)
sprank = min( size(aryshape), 3 )
hist%sprank = sprank
hist%name = grid_label
hist%aryshape(1:sprank) = aryshape(1:sprank)
hist%dims(1:sprank+1) = dims(1:sprank+1)
hist%axlongnames(1:sprank+1) = axlongnames(1:sprank+1)
hist%axunits(1:sprank+1) = axunits(1:sprank+1)
hist%axxtypes(1:sprank+1) = axxtypes(1:sprank+1)
if (sprank >= 1) hist%spcoordvars(1) = init_gt4_real1d( coord1 )
if (sprank >= 2) hist%spcoordvars(2) = init_gt4_real1d( coord2 )
if (sprank >= 3) hist%spcoordvars(3) = init_gt4_real1d( coord3 )
if(.not. present(ancilcrdvars)) then
nullify(hist%ancilcrdvars)
else if ( size(ancilcrdvars)==0 )then
nullify(hist%ancilcrdvars)
else
allocate(hist%ancilcrdvars(size(ancilcrdvars))) ! always new alloc
hist%ancilcrdvars = ancilcrdvars
endif
call histpl_push(HISTGRIDPOOL, hist)
call EndSub(subname)
end subroutine HistoryAutoSetGrid