MODULE COMPUTE_DIFF

INTERFACE DIFF
  MODULE PROCEDURE DIFF3
  MODULE PROCEDURE DIFF2
END INTERFACE DIFF

CONTAINS

SUBROUTINE DIFF3 (CDNAME, PREF, POUT, LLSTAT, LLCHECK, NPROMA, LLCHECKDIFF, LLDIFF)
IMPLICIT NONE

CHARACTER (LEN=*) :: CDNAME
REAL, INTENT(IN) :: PREF (:,:,:)
REAL, INTENT(IN) :: POUT (:,:,:)
LOGICAL, INTENT(IN) :: LLSTAT, LLCHECK, LLCHECKDIFF
INTEGER, INTENT(IN) :: NPROMA
LOGICAL, INTENT(OUT) :: LLDIFF

INTEGER :: JLON, JLEV, KLEV

KLEV=SIZE(PREF, 3)

PRINT *, CDNAME
IF (LLSTAT) THEN
  PRINT *, MINVAL (PREF), MAXVAL (PREF), SUM (PREF) / SIZE (PREF)
  PRINT *, MINVAL (POUT), MAXVAL (POUT), SUM (POUT) / SIZE (POUT)
ENDIF

IF (LLCHECK) THEN
  IF (SUM (ABS (POUT) + ABS (PREF)) > 0) THEN
  WRITE (*, '(A4)', ADVANCE='NO') ""
  DO JLON = 1, NPROMA
    WRITE (*, '("|",I12,"..",A12)', ADVANCE='NO') JLON, ""
  ENDDO
  WRITE (*, '("|")')
  DO JLEV = 1, KLEV
    WRITE (*, '(I4)', ADVANCE='NO') JLEV
    DO JLON = 1, NPROMA
      IF (ABS (PREF (JLON, 1, JLEV)) + ABS (POUT (JLON, 1, JLEV)) == 0.) THEN
        WRITE (*, '("|",A12,"..",A12)', ADVANCE='NO') "", ""
      ELSE
        IF(ABS(POUT (JLON, 1, JLEV)-PREF (JLON, 1, JLEV))>0.001 * ABS(PREF (JLON, 1, JLEV))) THEN
          WRITE (*, '("|",E12.5,"!=",E12.5)', ADVANCE='NO') PREF (JLON, 1, JLEV), POUT (JLON, 1, JLEV)
        ELSE
          WRITE (*, '("|",E12.5,"~=",E12.5)', ADVANCE='NO') PREF (JLON, 1, JLEV), POUT (JLON, 1, JLEV)
        ENDIF
      ENDIF
    ENDDO
    WRITE (*, '("|")')
  ENDDO
  ENDIF
ENDIF

IF (LLCHECKDIFF) THEN
  IF (SUM(ABS(POUT-PREF)) > 0.) THEN
    PRINT*, "THERE ARE DIFF"
    LLDIFF = .TRUE.
  ELSE
    PRINT*, "THERE IS NO DIFF"
  ENDIF
ENDIF

END SUBROUTINE

SUBROUTINE DIFF2 (CDNAME, PREF, POUT, LLSTAT, LLCHECK, NPROMA, LLCHECKDIFF, LLDIFF)
IMPLICIT NONE

CHARACTER (LEN=*) :: CDNAME
REAL, INTENT(IN) :: PREF (:,:)
REAL, INTENT(IN) :: POUT (:,:)
LOGICAL, INTENT(IN) :: LLSTAT, LLCHECK, LLCHECKDIFF
INTEGER, INTENT(IN) :: NPROMA
LOGICAL, INTENT(OUT) :: LLDIFF

INTEGER :: JLON

PRINT *, CDNAME
IF (LLSTAT) THEN
  PRINT *, MINVAL (PREF), MAXVAL (PREF), SUM (PREF) / SIZE (PREF)
  PRINT *, MINVAL (POUT), MAXVAL (POUT), SUM (POUT) / SIZE (POUT)
ENDIF

IF (LLCHECK) THEN
  IF (SUM (ABS (POUT) + ABS (PREF)) > 0) THEN
  WRITE (*, '(A4)', ADVANCE='NO') ""
  DO JLON = 1, NPROMA
    WRITE (*, '("|",I12,A12)', ADVANCE='NO') JLON, ""
  ENDDO
  WRITE (*, '("|")')
  WRITE (*, '(I4)', ADVANCE='NO') 0
  DO JLON = 1, NPROMA
    IF (ABS (PREF (JLON, 1)) + ABS (POUT (JLON, 1)) == 0.) THEN
    WRITE (*, '("|",2A12)', ADVANCE='NO') "", ""
    ELSE
    WRITE (*, '("|",2E12.5)', ADVANCE='NO') PREF (JLON, 1), POUT (JLON, 1)
    ENDIF
  ENDDO
  WRITE (*, '("|")')
  ENDIF
ENDIF

IF (LLCHECKDIFF) THEN
  IF (SUM(ABS(POUT-PREF)) > 0.) THEN
    PRINT*, "THERE ARE DIFF"
    LLDIFF = .TRUE.
  ELSE
    PRINT*, "THERE IS NO DIFF"
  ENDIF
ENDIF

END SUBROUTINE

END MODULE COMPUTE_DIFF
