!
      logical function wrhist(istep,modeltime,
     |  wrprim, save_prim,  newseries_prim,
     |  wrsech, save_sech,  newseries_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.
!
      use input_module,only: step,start,stop,hist,save,
     |  secstart,secstop,sechist,secsave
      use hist_module,only: nsource,isechist,nseries_prim,
     |  nseries_sech,iseries_prim,iseries_sech
      implicit none
!
! Args:
      integer,intent(in) :: 
     |  istep,          ! current step number in model run (0-nstep)
     |  modeltime(4)    ! current model time (day,hr,min,sec)
      logical,intent(out) :: 
     |  wrprim,  save_prim, newseries_prim,
     |  wrsech,  save_sech, newseries_sech
!
! Local:
      integer :: i, 
     |  nstep_hist,    ! number of steps between history writes
     |  nstep_save,    ! number of steps between prim history saves
     |  istep_prim,    ! current step in current primary series
     |  istep_sech     ! current step in current secondary series
      integer(kind=8) :: 
     |  nsecs,         ! current modeltime in seconds
     |  nsec_hist,     ! number of seconds between history writes
     |  nsec_start,    ! start time of current series (secs)
     |  nsec_stop,     ! stop time of current series (secs)
     |  nsec_stop_prev ! stop time of previous series (secs)
!
! External:
      integer,external :: mtime_to_nstep 
      integer(kind=8),external :: mtime_to_nsec
!
! Init:
!     write(6,"('wrhist: istep=',i4,' modeltime=',4i4)") istep,modeltime
      wrhist = .false.
      wrprim = .false. 
      wrsech = .false.
      save_prim = .false. 
      save_sech = .false.
      newseries_prim = .false.
      newseries_sech = .false.
      call modeltime_to_nsecs(modeltime,nsecs) ! sets nsecs
!
! Determine current primary history time series:
! (can be zero, i.e., model is in between time series)
!
      iseries_prim = 0
      primloop: do i=1,nseries_prim
        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
!
! Determine if this is beginning of a new primary time series:
          if (nsecs==nsec_start) then
            newseries_prim = .true.
            if (i > 1) then
              nsec_stop_prev = mtime_to_nsec(stop(:,i-1))
              if (nsecs == nsec_stop_prev) newseries_prim = .false.
            endif
          else
            if (i > 1) then
              nstep_hist = mtime_to_nstep(hist(:,i),step)
              nsec_hist = nstep_hist*step
              nsec_stop_prev = mtime_to_nsec(stop(:,i-1))
              if (nsecs-nsec_hist == nsec_start.and.
     |            nsec_start == nsec_stop_prev) newseries_prim = .true.
            endif
          endif
          exit primloop
        endif
      enddo primloop
!
! Step 0 of run:
      if (istep==0) then
        if (nsource > 0) then
          wrhist = .true.
          wrprim = .true.
          newseries_prim = .true.
        endif
      else
!
! 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)
          nstep_save = mtime_to_nstep(save(:,iseries_prim),step)
          istep_prim = (nsecs-nsec_start)/step
          if ((nsecs >= nsec_start.and.nsecs <= nsec_stop).and.
     |        mod(istep_prim,nstep_hist)==0) then
            wrhist = .true.
            wrprim = .true.
            save_prim = .false.
            if ((mod(istep_prim,nstep_save)==0.and.nsecs > nsec_start)
     |          .or.nsecs==nsec_stop) save_prim = .true.
          endif
        endif
      endif
!
! Determine current secondary history time series index:
! (can be zero, i.e., model is in between time series)
!
      if (isechist > 0) then ! writing secondary histories this run
        iseries_sech = 0
        sechloop: do i=1,nseries_sech
          nsec_start = mtime_to_nsec(secstart(:,i))
          nsec_stop  = mtime_to_nsec(secstop (:,i))
          if (nsecs >= nsec_start.and.nsecs <= nsec_stop) then
            iseries_sech = i
!
! Determine if this is beginning of a new secondary time series:
            if (nsecs==nsec_start) then
              newseries_sech = .true.
              if (i > 1) then
                nsec_stop_prev = mtime_to_nsec(secstop(:,i-1))
                if (nsecs == nsec_stop_prev) newseries_sech = .false.
              endif
            else
              if (i > 1) then
                nstep_hist = mtime_to_nstep(sechist(:,i),step)
                nsec_hist = nstep_hist*step
                nsec_stop_prev = mtime_to_nsec(secstop(:,i-1))
                if (nsecs-nsec_hist == nsec_start.and.
     |              nsec_start == nsec_stop_prev) newseries_sech=.true.
              endif
            endif
            exit sechloop
          endif
        enddo sechloop
!
! Step 0 of run:
        if (istep==0) then
          if (isechist > 0.and.all(modeltime(1:3)==secstart(:,1))) then
            wrhist = .true.
            wrsech = .true.
            newseries_sech = .true.
          endif
        else
!
! Check secondary histories:
          if (isechist > 0 .and. iseries_sech > 0) then
            nsec_start = mtime_to_nsec(secstart(:,iseries_sech))
            nsec_stop  = mtime_to_nsec(secstop (:,iseries_sech))
            nstep_hist = mtime_to_nstep(sechist(:,iseries_sech),step)
            nstep_save = mtime_to_nstep(secsave(:,iseries_sech),step)
            istep_sech = (nsecs-nsec_start)/step
!
! Check for secondary history write:
            if (nsecs >= nsec_start.and.nsecs <= nsec_stop.and.
     |        mod(istep_sech,nstep_hist)==0) then
                wrhist = .true.
                wrsech = .true.
                save_sech = .false.
                if (mod(istep_sech,nstep_save)==0.or.nsecs==nsec_stop) 
     |            save_sech = .true.
            endif
          endif
        endif
      endif
!     write(6,"(8x,'wrhist=',l1,' wrprim=',l1,' save_prim=',l1,
!    |  ' newseries_prim=',l1,' iseries_prim=',i3)")
!    |  wrhist,wrprim,save_prim,newseries_prim,iseries_prim
      end function wrhist
