subroutine timing_calcs(time,par,out)

implicit none
    INTEGER, PARAMETER :: iknd=SELECTED_INT_KIND(8)  ! nobody does this
    INTEGER, PARAMETER :: DP=SELECTED_REAL_KIND(14)  ! this is double precision
    INTEGER, PARAMETER :: SP=SELECTED_REAL_KIND(6)  ! this is single precision
real(DP) :: hourStartMonth(13)
!data hourStartMonth/0,744,1416,2160,2880,3624,4344,5088,5832,6552,7296,8016,8760/
real(DP) :: hourEndMonth(12)
data hourEndMonth/744,1416,2160,2880,3624,4344,5088,5832,6552,7296,8016,8760/ 
logical first_run
integer :: mode,relative,i
real(DP),intent(in) :: par(6)
real(DP),intent(in) :: time
real(DP),intent(out) :: out(18)
real(DP) :: step,Peak_Last,Peak_First,Peak_dur,Summer_First,Summer_Last
real(DP) :: hour_year,month_cal,year,month,day,monthend,temp,yearend,hour_day
real(DP) :: hour_day2,hour_month,day_month,week_month,hour_week,day_week,day_year
real(DP) :: summer,peak,occup,yearmonth
save

!if(.not.first_run)then
    mode = 1
    relative = par(1)
    step =par(2)
    Peak_Last = par(3)
    Peak_First = par(4)	   
    Peak_dur = Peak_Last-Peak_First        
    Summer_First = par(5)
    Summer_Last = par(6)
    
    first_run = .true.
    
    if ((relative < 0 ) .or. (relative > 1 ))  THEN
        write(*,*) "relative is outside bounds in timing"
    ENDIF    
!else
!endif
    !Calculate the first hour of the month
    hourStartMonth(1) = 0
    hourStartMonth(2:13) = hourEndMonth + step
    !Calculate the hour of the year
    hour_year = int(mod(time,8760.0) + 0.01)
    !Calculate the current month of the year 
    do i = 1,12
        if ((hour_year >= hourStartMonth(i)) .and. (hour_year < hourStartMonth(i+1)) ) then
            month_cal = i
            exit
        endif
    enddo 
    year = int((time)/8760.0) +1			!Year of simulation
    yearmonth = int((time-step)/8760.0) +1
    month = (year - 1)*12  + month_cal		!Month of simulation
    day = int(time/24.0) + 1		!Day of simulation  
    
    !calculate the end of the month
!    monthend = 0  !reset this value so that you don't have the same number from the prior time step
!    do i = 1,12
!        temp = hourStartMonth(i+1)+8760*(year-1) !- step
!        if (monthend == 0) then
!            if (time == temp) then
!                monthend = 1
!            elseif ((time /= temp).and.(monthend == 1))then
!                monthend = 0
!            endif
!        else
!            exit
!        endif
!    enddo 
    
    if(time >= 8760)then
        temp = 1
    endif
    monthend = 0
    do i = 1,12
        temp = hourEndMonth(i)+8760*(yearmonth-1)    !- step
            if (time == temp) then
                monthend = 1
            else
                monthend = 0
            endif
            if (monthend == 1)then
                exit
            endif
    enddo 
    
    !calculate the end of the year  
    temp = 8760*(year-1)
    if ((time /= temp).or.(time == 0))then
        yearend = 0
    else
        yearend = 1
    endif
    
    hour_day = int(mod(time,24.0) + 0.01)	!Hour of the day
    if ((time >= 1) .and. (hour_day == 0.d0)) hour_day = 24.0      
    hour_day2 = mod(mod(time,168.),24.)	!Hour of the day accounting for step size
    
    hour_month = int( time - hourStartMonth(month_cal)) !Hour of the month
    day_month = int(mod(hour_month,24.0))  + 1	!Day of the month
    week_month = int(hour_month/168.0) + 1			!Week of the month
    
    hour_week = int(mod(hour_month,168.0))		!Hour of the week
    if ((time >= 1).and.(hour_week == 0.0)) hour_week = 168.0

    day_week = int(hour_week/24.0) + 1			!Day of the week
    day_year = int(hour_year /24.0) + 1			!Day of the year
    
    Summer = abs((month_cal <= Summer_Last)*(month_cal >= Summer_First))
    Peak = abs((hour_day2 <= Peak_Last)*(hour_day2 >= Peak_First))
    TEMP = abs((hour_day2 > 6.0)*(hour_day2 < 18.5))
    Occup = max(0.0,TEMP)
      
!endif

    out(1)  = year
    out(2)  = month
    out(3)  = day
    out(4)  = month_cal
    out(5)  = day_year
    out(6)  = day_month
    out(7)  = day_week
    out(8)  = hour_year
    out(9)  = hour_month
    out(10) = hour_week
    out(11) = hour_day
    out(12) = monthend
    out(13) = yearend
    out(14) = hour_day2
    OUT(15) = Peak_dur
    OUT(16) = Summer
    OUT(17) = Peak
    OUT(18) = Occup

return
end subroutine timing_calcs