PROGRAM Pollution_Report
!-----------------------------------------------------------------------
! This program reads the elements of the two-dimensional array
! Pollution_Table from a file and produces a report showing a table
! of pollution levels, the average pollution level for each day,
! and the average pollution level for each sampling time.  The
! first line of the file contains the number of days and the number
! of times when readings were made.  Variables used are:
!   FileName        : name of pollution file
!   NumDays         : the number of rows (days)
!   NumTimes        : the number of columns (times)
!   Pollution_Table : an NumDays X NumTimes allocatable array of 
!                     pollution levels
!   Day, Time       : row, column subscripts
!
! Input (file):    NumDays, NumTimes, and the pollution levels
! Output (screen): The array Pollution_Table in table format, the
!                  average pollution level for each day, and the
!                  average pollution level for each sampling time
!-----------------------------------------------------------------------

  IMPLICIT NONE

  INTEGER, DIMENSION(:, :), ALLOCATABLE :: Pollution_Table
  INTEGER :: NumDays, NumTimes, Day, Time
  CHARACTER(20) :: FileName

  ! Open the file and read the number of days and number of times 
  ! per day when pollution levels were read. Then allocate the 
  ! array Pollution_Table

  WRITE (*, '(1X, A)', ADVANCE = "NO") &
        "Enter the name of the pollution file: "
  READ *, FileName
  OPEN (UNIT = 15, FILE = FileName, STATUS = "OLD")
  READ (15, *) NumDays, NumTimes
  ALLOCATE(Pollution_Table(NumDays, NumTimes))

  ! Read the pollution levels, store them in Pollution_Table, and display 
  ! them in a table of the required form

  DO Day = 1, NumDays
     READ (15,*) (Pollution_Table(Day, Time), Time = 1, NumTimes)
  END DO

  PRINT '(T30, "Time" / 1X, "Day:", 12I4 / 1X, 53("-"))', &
    (Time, Time = 1, NumTimes)

  DO Day = 1, NumDays
     PRINT '(1X, I2, " :", 15I4)', &
        Day, (Pollution_Table(Day,Time), Time = 1, NumTimes)
  END DO

! Calculate average pollution level for each day (row averages)

  PRINT *
  DO Day = 1, NumDays
     PRINT 10,  "for day", Day, &
        REAL(SUM(Pollution_Table(Day,:))) / REAL(NumTimes)
     10 FORMAT(1X, "Average pollution level ", A7, I3, ":", F6.1)
  END DO

! Calculate average pollution level for each time (column averages)

  PRINT *
  DO Time = 1, NumTimes
     PRINT 10, "at time", Time, &
        REAL(SUM(Pollution_Table(:,Time))) / REAL(NumDays)
  END DO

  CLOSE (15)
END PROGRAM Pollution_Report
