!
      logical function wrhist(istep,modeltime,
     |  wrprim,save_prim,iseries_prim,
     |  wrsech,save_sech,iseries_sech)
!
! Return true if it is time to write a primary or secondary history, 
!   given current istep and modeltime(4).
! Return wrprim true if its time to write a primary history.
! Return wrsech true if its time to write a secondary history.
! Return save_prim true if its time to save a primary file.
! Return save_sech true if its time to save a secondary file.
! Return newseries_prim true if a new primary time series is beginning.
! Return newseries_sech true if a new secondary time series is beginning.
!
! This routine is informational only. It does not set or increment
! any module data, nor rely on saved locals. It can be called multiple
! times during a single time step if necessary.
!
! These module vars must be *read-only* by this routine:
      use input_module,only: step,start,stop,hist,secstart,secstop,
     |  sechist
      use hist_module,only: nstep_save,nstep_secsave,nsource,
     |  isechist,nseries,nseries_sech
      implicit none
!
! Args:
      integer,intent(in) :: istep,modeltime(4)
      logical,intent(out) :: 
     |  wrprim,  save_prim,
     |  wrsech,  save_sech
      integer,intent(out) :: iseries_prim, iseries_sech
!
! Local:
      integer(kind=8) :: nsecs
      integer :: i,
     |  nsec_start,    nsec_stop,    nstep_hist,
     |  nsec_secstart, nsec_secstop, nstep_sechis
!
! External:
      integer,external :: mtime_to_nstep
      integer(kind=8),external :: mtime_to_nsec
!
      wrhist = .false.
      wrprim = .false. 
      wrsech = .false.
      save_prim = .false. 
      save_sech = .false.
      call modeltime_to_nsecs(modeltime,nsecs) ! sets nsecs
!
! Determine current primary time series index (can be zero):
      iseries_prim = 0
      primloop: do i=1,nseries
        nsec_start = mtime_to_nsec(start(:,i))
        nsec_stop  = mtime_to_nsec(stop (:,i))
        if (nsecs >= nsec_start.and.nsecs <= nsec_stop) then
          iseries_prim = i
          exit primloop
        endif
      enddo primloop
!
! Determine current secondary time series index (can be zero):
      iseries_sech = 0
      sechloop: do i=1,nseries_sech
        nsec_secstart = mtime_to_nsec(secstart(:,i))
        nsec_secstop  = mtime_to_nsec(secstop (:,i))
        if (nsecs >= nsec_secstart.and.nsecs <= nsec_secstop) then
          iseries_sech = i
          exit sechloop
        endif
      enddo sechloop
!
! Beginning of run (no steps taken yet). If source history
! was provided, we need to echo it as primary history.
! If doing secondary histories and history is needed at
! start time, write secondary.
!
      if (istep==0) then
        if (nsource > 0) then
          wrhist = .true.
          wrprim = .true.
        endif
        if (isechist > 0 .and. all(modeltime(1:3)==secstart(:,1))) then
          wrhist = .true.
          wrsech = .true.
        endif
        nstep_hist = mtime_to_nstep(hist(:,1),step)

!       write(6,"('wrhist: istep=',i4,' mtime=',4i3,
!    |    ' wrprim=',l1,' save_p=',l1,' iseries_p=',i2,
!    |    ' wrsech=',l1,' save_s=',l1,' iseries_s=',i2)") 
!    |    istep,modeltime,
!    |    wrprim,save_prim,iseries_prim,
!    |    wrsech,save_sech,iseries_sech,

        return
      endif
!
! Check for primary history write:
      if (iseries_prim > 0) then
        nsec_start = mtime_to_nsec(start(:,iseries_prim))
        nsec_stop  = mtime_to_nsec(stop (:,iseries_prim))
        nstep_hist = mtime_to_nstep(hist(:,iseries_prim),step)
        if ((nsecs >= nsec_start.and.nsecs <= nsec_stop).and.
     |      mod(istep,nstep_hist)==0) then
          wrhist = .true.
          wrprim = .true.
          save_prim = .false.
          if (mod(istep,nstep_save)==0.or.nsecs==nsec_stop) 
     |      save_prim = .true.

!         write(6,"('wrhist save prim: modeltime=',4i3,' iseries=',
!    |      i2,' nsecs=',i8,' stop(:,iseries)=',3i3,' nsec_stop=',
!    |      i8,' save=',l1)") 
!    |      modeltime,iseries_prim,nsecs,stop(:,iseries_prim),
!    |      nsec_stop,save_prim

        endif
      endif
!
! Check secondary histories:
      if (isechist > 0 .and. iseries_sech > 0) then
        nsec_secstart = mtime_to_nsec(secstart(:,iseries_sech))
        nsec_secstop  = mtime_to_nsec(secstop (:,iseries_sech))
        nstep_sechis  = mtime_to_nstep(sechist(:,iseries_sech),step)
!
! Check for secondary history write:
        if (nsecs >= nsec_secstart.and.nsecs <= nsec_secstop.and.
     |    mod(istep,nstep_sechis)==0) then
            wrhist = .true.
            wrsech = .true.
            save_sech = .false.
            if (mod(istep,nstep_secsave)==0.or.nsecs==nsec_stop) 
     |        save_sech = .true.
        endif
      endif

!       write(6,"('wrhist: istep=',i4,' mtime=',4i3,
!    |    ' wrprim=',l1,' save_p=',l1,' iseries_p=',i2,
!    |    ' wrsech=',l1,' save_s=',l1,' iseries_s=',i2)") 
!    |    istep,modeltime,
!    |    wrprim,save_prim,iseries_prim,
!    |    wrsech,save_sech,iseries_sech,

      end function wrhist

