!
      integer function numfiles(type,nseries,nsource,nhist_total)
!
! Determine number of history files needed (primary if type=='prim',
! or secondary if type=='sech'). A new file will be needed whenever 
! a new time series is started, or a file is full (i.e., has 
! mxhist_[prim,sech] histories). This is called from input.
!
      use input_module,only: step,
     |  mxhist_prim,start   ,stop,   hist,
     |  mxhist_sech,secstart,secstop,sechist
      implicit none
!
! Args:
      character(len=4),intent(in) :: type  ! 'prim' or 'sech'
      integer,intent(in) :: nseries        ! number of time series
      integer,intent(in) :: nsource        ! number of source files (0/1)
      integer,intent(out) :: nhist_total   ! total histories to be written
!
! Local:
      integer :: i,mtime(3),nhfile,nhseries,modeltime(4)
      integer(kind=8) :: nsecs,idelsecs,nsecstop
      integer(kind=8),external :: mtime_to_nsec
!
! Primary histories:
      if (type=='prim') then
        numfiles = 0      ! return var
        nhist_total = 0   ! total number of histories (output arg)
!
! Iterate through each time series:
        do i=1,nseries
          idelsecs = mtime_to_nsec(hist(:,i)) ! delta secs between histories
          nsecstop = mtime_to_nsec(stop(:,i)) ! stop secs
          numfiles = numfiles+1 ! start new file at each new time series
          if (i==1) then
            nsecs = mtime_to_nsec(start(:,i)) ! time in secs
          else
!
! If start history of a new time series is the same as last history
! of the previous time series, we do not rewrite the start history 
! on the new file (nsecs was incremented at end of previous series).
! Otherwise, set nsecs to first history in the new series:
!
            if (any(start(:,i) /= stop(:,i-1)))
     |        nsecs = mtime_to_nsec(start(:,i))
          endif
!
! Iterate over histories in this time series:
          nhfile = 0   ! number of histories in current file
          nhseries = 0 ! number of histories in current time series
          primloop: do ! loop over histories in this time series
            call nsecs_to_modeltime(nsecs,modeltime)
            nhseries = nhseries+1
            nhfile = nhfile+1
            if (nhfile > mxhist_prim) then
              numfiles = numfiles+1
              nhfile = 1
            endif
            nsecs = nsecs+idelsecs
            if (nsecs > nsecstop) exit primloop
          enddo primloop
          nhist_total = nhist_total+nhseries
        enddo ! i=1,nseries
!
! If a source file was not provided, then the source history is to
! be found on the 1st output file, so add 1 needed file. This may
! not be needed in all cases, but better over than under -- its not
! worth it to wait until after reading the source to determine how
! many histories are on the source file to get numfiles exactly right 
! for all cases.
!
        if (nsource==0) numfiles = numfiles+1
!
! Secondary histories:
      elseif (type=='sech') then
        numfiles = 0     ! return var
        nhist_total = 0  ! total number of histories (output arg)
        do i=1,nseries
          idelsecs = mtime_to_nsec(sechist(:,i))
          nsecstop = mtime_to_nsec(secstop(:,i))
          numfiles = numfiles+1 ! start new file at each new time series
          if (i==1) then
            nsecs = mtime_to_nsec(secstart(:,i)) ! sets nsecs
          else ! i > 1
!
! If start history of a new time series is the same as last history
! of the previous time series, we do not rewrite the start history 
! on the new file:
!
            if (any(secstart(:,i) /= secstop(:,i-1)))
     |        nsecs = mtime_to_nsec(secstart(:,i)) ! sets nsecs
          endif
          nhfile = 0   ! number of histories in current file
          nhseries = 0 ! number of histories in current time series
          sechloop: do ! loop over histories in this time series
            call nsecs_to_modeltime(nsecs,modeltime)
            nhseries = nhseries+1
            nhfile = nhfile+1
            if (nhfile > mxhist_sech) then
              numfiles = numfiles+1
              nhfile = 1
            endif
            nsecs = nsecs+idelsecs
            if (nsecs > nsecstop) exit sechloop
          enddo sechloop
          nhist_total = nhist_total+nhseries
        enddo ! i=1,nseries
!
! Type must be either "prim" or "sech":
      else
        write(6,"('>>> numfiles: unrecognized type = ',a)") trim(type)
        stop 'numfiles' 
      endif
      end function numfiles
