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

    TYPE(variables_real), INTENT(IN) :: alpha
    TYPE(variables_int), INTENT(IN) :: northerness
    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
    INTEGER :: iostatus = 0
    CHARACTER (LEN=*) :: output_filename
    LOGICAL, SAVE :: written = .FALSE.
    
    IF(.NOT. written) THEN
       ! HARD-CODED: the parameters to write and their order
       WRITE(15, FMT=*, IOSTAT=iostatus) '# alpha x_1_1 x_2_1 x_3_1 x_4_1 x_5_1 ... '
       IF (iostatus /= 0) THEN
          WRITE (*, *) 'Could not write to ', output_filename, '. Exiting.'
          STOP
       END IF
       written = .TRUE.
    END IF
    
    ! HARD-CODED: which parameters to write
    WRITE(15, FMT='(F6.2, 84(I2))', IOSTAT=iostatus) alpha % one_dim(1) % value, x % two_dim(:, :) % value
    ! works but difficult (maybe impossible) to generate

    !WRITE(15, FMT=*, IOSTAT=iostatus) alpha % one_dim(1) % value, x % two_dim(:, :) % value
    ! doesn't work: adds spaces and newlines.



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

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

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

    INTEGER :: iostatus
    CHARACTER (LEN=*) :: summary_filename

    ! unit 14
    WRITE(14, FMT=*, IOSTAT=iostatus) 'Summary:'
    IF (iostatus /= 0) THEN
       WRITE (*, *) 'Could not write to ', summary_filename, '. Exiting.'
       STOP
    END IF
    ! output the summary wanted
    ! HARD-CODED: the file name of the summary file

  END SUBROUTINE write_summary

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

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

    INTEGER :: iostatus
    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: a legend describing the values written
    
    WRITE(20, '(A)', IOSTAT=iostatus) 'alpha, x_1_1, x_2_1, ... x_21_4'

    IF (iostatus /= 0) THEN
       WRITE (*, *) 'Could not write to ', filename, '. Exiting program.'
       STOP
    END IF
    
    ! output the summary wanted
    
    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, '(84(I2))', IOSTAT=iostatus) x % two_dim(:, :) % value    

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

    
    CLOSE(20)


  END SUBROUTINE write_last_values
  
END MODULE output
