MODULE output
    USE definitions
    IMPLICIT NONE
CONTAINS
  SUBROUTINE write_output(alpha, northerness, q, p, x, obs)
    IMPLICIT NONE

    TYPE(variables_real), INTENT(IN) :: alpha
    TYPE(variables_int), INTENT(IN) :: northerness
    TYPE(variables_int), INTENT(IN) :: q
    TYPE(variables_real), INTENT(IN) :: p
    TYPE(variables_int), INTENT(IN) :: x
    TYPE(variables_int), INTENT(IN) :: obs    

    ! output the parameters which the user wants to output
    ! (the parameters to output and the file name are hard-coded here)
    INTEGER :: i
    LOGICAL, SAVE :: written = .FALSE.
    
    IF(.NOT. written) THEN
       ! HARD-CODED: the parameters to write and their order
       WRITE(15, *) '# x_1_1 x_2_1 x_3_1 x_4_1 x_5_1 ... '
       written = .TRUE.
    END IF
    
    ! HARD-CODED: which parameters to write
    WRITE(15, '(F6.2, 84(I2))') alpha % one_dim(1) % value, x % two_dim(:, :) % value
        
  END SUBROUTINE write_output

  SUBROUTINE write_summary(alpha, northerness, q, p, x, obs)
    IMPLICIT NONE

    TYPE(variables_real), INTENT(IN) :: alpha
    TYPE(variables_int), INTENT(IN) :: northerness
    TYPE(variables_int), INTENT(IN) :: q
    TYPE(variables_real), INTENT(IN) :: p
    TYPE(variables_int), INTENT(IN) :: x
    TYPE(variables_int), INTENT(IN) :: obs   

    INTEGER :: iostatus, i, j
    
    ! output the summary wanted
    ! HARD-CODED: the file name of the summary file
    CHARACTER(LEN=*), PARAMETER :: filename='output_summary.txt'

    OPEN(UNIT=21, FILE=filename, IOSTAT=iostatus)

      IF (iostatus /= 0) THEN
       WRITE (*, *) 'Could not open ', filename, '. Exiting program.'
       STOP
    END IF

    ! Quote from the SRS - requirement OP5:
    ! The program should write a summary about the simulation
    ! into a separate file. The summary contains the number of
    ! proposed changes and the frequency of successful changes
    ! for each parameter.
    
    ! if the update strategy is sequential:
    ! ? variable 1:max
    ! 10000, REAL(variable % one_dim(index) % successful_changes)/10000
    ! ...
    ! if it's random:
    ! ? variable 1:max
    ! variable % one_dim(index) % update_count, 
    ! REAL(variable % one_dim(index) % successful_changes)/variable % one_dim(index) % update_count
    ! ...

    WRITE(21, '(A)', IOSTAT=iostatus) '?? simulation summary'
    
    IF (iostatus /= 0) THEN
       WRITE (*, *) 'Could not write to ', filename, '. Exiting program.'
       STOP
    END IF
    
    ! alpha
    WRITE(21, '(A)', IOSTAT=iostatus) '? alpha'
    
    IF (iostatus /= 0) THEN
       WRITE (*, *) 'Could not write to ', filename, '. Exiting program.'
       STOP
    END IF
    
    WRITE(21, FMT=*, IOSTAT=iostatus)  10000, REAL(alpha % one_dim(1) % successful_changes)/10000
    
     IF (iostatus /= 0) THEN
        WRITE (*, *) 'Could not write to ', filename, '. Exiting program.'
        STOP
     END IF
     
     !and x
     WRITE(21, '(A)', IOSTAT=iostatus) '? x'

     IF (iostatus /= 0) THEN
        WRITE (*, *) 'Could not write to ', filename, '. Exiting program.'
        STOP
     END IF

     DO j=1, 4
        DO i=1, 21
           WRITE(21, FMT=*, IOSTAT=iostatus) '? x', i,', ', j
           
           IF (iostatus /= 0) THEN
              WRITE (*, *) 'Could not write to ', filename, '. Exiting program.'
              STOP
           END IF
           

           WRITE(21, FMT=*, IOSTAT=iostatus) 10000, REAL(x % two_dim(i, j) % successful_changes)/10000
        
           IF (iostatus /= 0) THEN
              WRITE (*, *) 'Could not write to ', filename, '. Exiting program.'
              STOP
           END IF
        END DO
     END DO
     
  END SUBROUTINE write_summary

 SUBROUTINE write_last_values(alpha, northerness, q, p, x, obs)
    IMPLICIT NONE

    TYPE(variables_real), INTENT(IN) :: alpha
    TYPE(variables_int), INTENT(IN) :: northerness
    TYPE(variables_int), INTENT(IN) :: q
    TYPE(variables_real), INTENT(IN) :: p
    TYPE(variables_int), INTENT(IN) :: x
    TYPE(variables_int), INTENT(IN) :: obs   

    INTEGER :: iostatus, i
    CHARACTER (LEN=30) :: filename
    filename = "last_values.txt"
    ! HARD-CODED: the file name

    OPEN(UNIT=20, FILE=filename, IOSTAT=iostatus)
    
    IF (iostatus /= 0) THEN
       WRITE (*, *) 'Could not open ', filename, '. Exiting program.'
       STOP
    END IF
    
    ! HARD-CODED: variable names and legends as read 
    ! from the initial values file
    
    WRITE(20, '(A)', IOSTAT=iostatus) '? alpha'

    IF (iostatus /= 0) THEN
       WRITE (*, *) 'Could not write to ', filename, '. Exiting program.'
       STOP
    END IF


    WRITE(20, *, IOSTAT=iostatus) alpha % one_dim(1) % value

    IF (iostatus /= 0) THEN
       WRITE (*, *) 'Could not write to ', filename, '. Exiting program.'
       STOP
    END IF

    WRITE(20, '(A)', IOSTAT=iostatus) '? x 1:21 1:4'

    IF (iostatus /= 0) THEN
       WRITE (*, *) 'Could not write to ', filename, '. Exiting program.'
       STOP
    END IF


    DO i=1, 21
       WRITE(20, *, IOSTAT=iostatus) x % two_dim(i, :) % value

       IF (iostatus /= 0) THEN
          WRITE (*, *) 'Could not write to ', filename, '. Exiting program.'
          STOP
       END IF
              
    END DO

    CLOSE(20)


  END SUBROUTINE write_last_values
  
END MODULE output
