! Program Example from "Essential Fortran 90 & 95" by Loren P. Meissner
! Copyright 1996. Copying for sale requires permission from the author.
! Otherwise, distribution is permitted if these three lines are included.

  module G28M
    implicit none
    public :: Temp_In
    type, public :: A_Day
      integer :: Min_Temp, Max_Temp
      integer, dimension(24) :: Temperature
    end type A_Day
    type (A_Day), dimension(:), allocatable, public :: Year
  contains
    subroutine Temp_In( This_Year )
      integer, intent (in) :: This_Year
      integer :: N_Days, Day, Hour
      real :: Random_Term
  ! start subroutine Temp_In
      N_Days = merge( 366, 365, modulo( This_Year, 4 ) == 0 )  ! Is it leap year?
      allocate( Year(N_Days) )
      do Day = 1, N_Days
        do Hour = 1, 24
          call random_number( harvest = Random_Term )  ! Uniform between 0 and 1
          Year(Day) % Temperature(Hour) = anint( 63.0 &  ! Mean annual temperature
            + 20.0 * sin( 6.2832 * (real( Day ) + 250.0) / 365.0 ) &  ! Annual cycle
            + 15.0 * sin( 6.2832 * (real( Hour ) + 15.0) / 24.0 ) &  ! Daily cycle
            + 5.0 * Random_Term )
        end do
        Year(Day) % Max_Temp = maxval( Year(Day) % Temperature )
        Year(Day) % Min_Temp = minval( Year(Day) % Temperature )
      end do
      return
    end subroutine Temp_In
  end module G28M
