      PROGRAM DIFFV2
C
C    This program reads the MM5 version 2 modeling system outputs
C    And generates difference files for hmv.
C

C
C    IX : Output domain grid dimension in I direction (north-south)
C    JX : Output domain grid dimension in J direction (east-west)
C    KX : Output domain grid dimension in K direction (vertical)
C
      PARAMETER(IX=200,JX=200,KX=26)

      INTEGER MIF(1000,20,2)
      REAL MRF(1000,20,2)
      CHARACTER*80 MIFC(1000,20,2),MRFC(1000,20,2)
      CHARACTER*8 ID
      CHARACTER*80 FILE1, FILE2, ARG3
      DIMENSION DUM3D(IX,JX,KX+1,2),DUM2D(IX,JX,2)
      LOGICAL Efound
      LOGICAL Justplot
      INTEGER LEVLIM

      Efound = .false.
      Justplot = .false.
      LEVLIM = -1

C get arguments
      if ( iargc() .ge. 2 ) then
        call getarg(1,file1)
        call getarg(2,file2)
        ierr = 0
        open(10,file=file1,status="old",form="unformatted",err=922)
        open(20,file=file2,status="old",form="unformatted",err=923)
        goto 924
 922    print*,'error opening ',file1
        stop
C bounce here if second name is not openable -- this would mean that
C it's a field name instead.
 923    continue
        Justplot = .true.
 924    continue
        if ( iargc() .eq. 3 ) then
          call getarg(3,arg3)
          read(arg3,*)levlim
          print*,'LEVLIM = ',LEVLIM
        endif
      else
        print*,'Usage: command file1 file2'
        stop
      endif

      N=0    ! time period counter
 5    CONTINUE
      N=N+1
C
C    MIF : Array store integer variables information
C    MIFC: Character array describs the MIF array
C    MRF : Array store real variables information
C    MRFC: Character array describs the MRF array
C
      READ(10,END=999)
     $         ((MIF(I,J,1),  I=1,1000),J=1,20),
     $         ((MRF(I,J,1),  I=1,1000),J=1,20),
     $         ((MIFC(I,J,1), I=1,1000),J=1,20),
     $         ((MRFC(I,J,1), I=1,1000),J=1,20)
      IF ( .NOT. JUSTPLOT ) READ(20,END=999)
     $         ((MIF(I,J,2),  I=1,1000),J=1,20),
     $         ((MRF(I,J,2),  I=1,1000),J=1,20),
     $         ((MIFC(I,J,2), I=1,1000),J=1,20),
     $         ((MRFC(I,J,2), I=1,1000),J=1,20)
C
C  get record header information
C
      PRINT *,'MM5 version 1 record header information'
      PRINT *,'   '
      PRINT *,'   '
      IF ( .NOT. JUSTPLOT ) CALL GETGIST(MIF,MRF,MIFC,MRFC)
      PRINT *,'   '
      PRINT *,'   '
      PRINT *,'   '
C
C  INDEX : output is generated by which program?
C           1 TERRAIN
C           2 DATAGRID
C           3 RAWINS 3-D analysis
C           4 RAWINS surface 4DDA
C           5 MM5 initial condition from INTERP
C           6 MM5 
C           7 Interpolated model output on pressure levels from INTERP
C
      INDEX=MIF(1,1,1)
      IF(INDEX.EQ.1) THEN
         PRINT *,'READING TERRAIN OUTPUT'
      ELSEIF(INDEX.EQ.2) THEN
         PRINT *,'READING DATAGRID OUTPUT'
      ELSEIF(INDEX.EQ.3) THEN
         PRINT *,'READING RAWINS 3-D ANALYSIS OUTPUT'
      ELSEIF(INDEX.EQ.4) THEN
         PRINT *,'READING RAWINS SURFACE 4DDA OUTPUT'
      ELSEIF(INDEX.EQ.5) THEN
         PRINT *,'READING MODEL INITIAL CONDITION'
      ELSEIF(INDEX.EQ.6) THEN
         PRINT *,'READING MM5 OUTPUT'
      ELSEIF(INDEX.EQ.7) THEN
         PRINT *,'READING INTERPOLATED MODEL OUTPUT ON P'
      END IF
      PRINT *,'DATE = ',MIF(1,INDEX,1)
      PRINT *,'   '
      PRINT *,'   '
C
C  NUM3D : Number of 3-D fields in this output
C  NUM2D : Number of 2-D fields in this output
C  NUM1D : Number of 1-D fields in this output
C  NUM0D : Number of 0-D fields in this output
C
      NUM3D = MIF(201,INDEX,1)
      NUM2D = MIF(202,INDEX,1)
      NUM1D = MIF(203,INDEX,1)
      NUM0D = MIF(204,INDEX,1)
C
C  ID : Description of the 3-D fields
C  IDOT  : Is this field defined on cross or dot points
C          1 DOT POINT
C          2 CROSS POINT
C
      IL = MIF(104,1,1)
      JL = MIF(105,1,1)
      KL = MRF(101,6,1)
      print*,"IL, JL = ",IL,JL,KL
C
      IF ( JUSTPLOT ) GOTO 10000
C
      DO 120 IFLD=1,NUM3D
         IDOT=1-MIF(204+IFLD,INDEX,1)/10
         ID=MIFC(204+IFLD,INDEX,1)(1:8)
         IF(ID.EQ.'W       ' .AND. MIF(  10,1,1).EQ.2) THEN
           KR = KL + 1
         ELSE
           KR = KL
         ENDIF
         READ(10) (((DUM3D(I,J,K,1),I=1,IL),J=1,JL),K=1,KR)
         READ(20) (((DUM3D(I,J,K,2),I=1,IL),J=1,JL),K=1,KR)
         IFDIFFS = 0
         DO K = 1,KR
         IKDIFFS = 0
         DO J = 1,JL-IDOT
         DO I = 1,IL-IDOT
           IF (DUM3D(I,J,K,1) .NE. DUM3D(I,J,K,2)) THEN
             IKDIFFS = IKDIFFS + 1
             IFDIFFS = IFDIFFS + 1
           ENDIF
         ENDDO
         ENDDO
         IF ( IKDIFFS .NE. 0 ) THEN
c          EFOUND = .TRUE.
           PRINT*,'LEVEL ',K,' WITH ',IKDIFFS,' DIFFS'
           IF ( LEVLIM.EQ.-1.OR.K.EQ.LEVLIM) THEN
             write(88,*)JL,IL,ID,' LEVEL ',K,' TIME ',N
             write(98,*)JL,IL,ID,' LEVEL ',K,' TIME ',N
             DO I = 1,IL
               DO J = 1,JL
                 WRITE(88,*)DUM3D(I,J,K,1)
                 WRITE(98,*)DUM3D(I,J,K,2)
               ENDDO
             ENDDO
           ENDIF
         ENDIF
         ENDDO
         IF (IFDIFFS .NE. 0 ) THEN
           PRINT *,'3D FIELD ID= ',ID,'    IDOT= ',IDOT,
     *             '     N DIFFS= ',IFDIFFS
         ENDIF
 120  CONTINUE
      PRINT *,'   '
      PRINT *,'   '
      DO 140 IFLD=1,NUM2D
         IDOT=1-MIF(204+NUM3D+IFLD,INDEX,1)/10
         ID=MIFC(204+NUM3D+IFLD,INDEX,1)(1:8)
         READ (10) ((DUM2D(I,J,1),I=1,IL),J=1,JL)
         READ (20) ((DUM2D(I,J,2),I=1,IL),J=1,JL)
         IFDIFFS = 0
         DO J = 1,JL-IDOT
         DO I = 1,IL-IDOT
           IF (DUM2D(I,J,1) .NE. DUM2D(I,J,2)) THEN
             IFDIFFS = IFDIFFS + 1
           ENDIF
         ENDDO
         ENDDO
         IF ( IFDIFFS .NE. 0 ) THEN
c          EFOUND = .TRUE.
           write(88,*)JL,IL,ID,' TIME ',N
           write(98,*)JL,IL,ID,' TIME ',N
           DO I = 1,IL
             DO J = 1,JL
               WRITE(88,*)DUM2D(I,J,1) 
               WRITE(98,*)DUM2D(I,J,2) 
             ENDDO
           ENDDO
         ENDIF
         PRINT *,'2D FIELD ID= ',ID,'    IDOT= ',IDOT,
     *         '     N DIFFS = ',IFDIFFS
 140  CONTINUE

      IF (.NOT. EFOUND ) GO TO 5
      GO TO 999

10000 CONTINUE
      DO 220 IFLD=1,NUM3D
         IDOT=1-MIF(204+IFLD,INDEX,1)/10
         ID=MIFC(204+IFLD,INDEX,1)(1:8)
         IF ( N .EQ. 1 ) PRINT*,IFLD,MIFC(204+IFLD,INDEX,1)(1:80)
         IF(ID.EQ.'W       ' .AND. MIF(  10,1,1).EQ.2) THEN
           KR = KL + 1
         ELSE
           KR = KL
         ENDIF
         READ(10) (((DUM3D(I,J,K,1),I=1,IL),J=1,JL),K=1,KR)
         IFDIFFS = 0

         IF ( ID .EQ. FILE2 ) THEN
         DO K = 1,KR
           IF ( LEVLIM.EQ.-1.OR.K.EQ.LEVLIM) THEN
             write(88,*)JL-IDOT,IL-IDOT,ID,' LEVEL ',K
             DO I = 1,IL-IDOT
               DO J = 1,JL-IDOT
                 WRITE(88,*)DUM3D(I,J,K,1)
               ENDDO
             ENDDO
           ENDIF
         ENDDO
         ENDIF
 220  CONTINUE
      DO 240 IFLD=1,NUM2D
         IDOT=1-MIF(204+NUM3D+IFLD,INDEX,1)/10
         ID=MIFC(204+NUM3D+IFLD,INDEX,1)(1:8)
         IF ( N .EQ. 1 ) PRINT*,IFLD,MIFC(204+NUM3D+IFLD,INDEX,1)(1:80)
         READ (10) ((DUM2D(I,J,1),I=1,IL),J=1,JL)
         IF ( ID .EQ. FILE2 ) THEN
           write(88,*)JL-IDOT,IL-IDOT,ID
           DO I = 1,IL-IDOT
             DO J = 1,JL-IDOT
               WRITE(88,*)DUM2D(I,J,1) 
             ENDDO
           ENDDO
         ENDIF
 240  CONTINUE

      IF (.NOT. EFOUND ) GO TO 5
999   CONTINUE
      END

      SUBROUTINE GETGIST(MIF,MRF,MIFC,MRFC)
C
C     PURPOSE  INTERPRET THE VERSION 1 HEADER INFO
C
      INTEGER MIF(1000,20,2)
      REAL MRF(1000,20,2)
      CHARACTER*80 MIFC(1000,20,2),MRFC(1000,20,2)

      DO 30 J=1,20
      DO 10 I=1,1000
         IF(MIF(I,J,1).NE.MIF(I,J,2).AND.MIF(I,J,1).NE.-999) THEN
           PRINT 100,I,J,MIF(I,J,1),MIF(I,J,2),MIFC(I,J,1)
         ENDIF
  10  CONTINUE
      DO 20 I=1,1000
         IF(MRF(I,J,1).NE.MRF(I,J,2).AND.MRF(I,J,1).NE.-999.) THEN
           PRINT 110,I,J,MRF(I,J,1),MRF(I,J,2),MRFC(I,J,1)
         ENDIF
  20  CONTINUE
  30  CONTINUE

 100  FORMAT('MIF(',I3,',',I1,') = ',I10,' <> ',I10,' : ',A80)
 110  FORMAT('MRF(',I3,',',I1,') = ',F10.2,' <> ',F10.2,' : ',A80)
      RETURN
      END
