        PROGRAM calcDayMax

!***********************************************************************
!  DESCRIPTION:
!
!  PRECONDITIONS REQUIRED:
!       "setenv"s for input/output file
!
!  SUBROUTINES AND FUNCTIONS CALLED:
!
!  REVISION  HISTORY:
!
!***********************************************************************

      IMPLICIT NONE

!...........   INCLUDES:

      INCLUDE 'PARMS3.EXT'      ! I/O API constants
      INCLUDE 'FDESC3.EXT'      ! I/O API file description data structure
      INCLUDE 'IODECL3.EXT'     ! I/O API function declarations

!...........   EXTERNAL FUNCTIONS and their descriptions:

      INTEGER  ENVINT
      EXTERNAL NEXTIME, ENVSTR, ENVINT

!...........   LOCAL VARIABLES and their descriptions:

      CHARACTER*160 MESG
      CHARACTER*16  INFILE, OUTFILE, STACKFILE
      CHARACTER*16  SPECIES
      CHARACTER*16 SCLVAR1, SCLVAR2, SCLVAR3
      
      INTEGER JDATE, ODATE
      INTEGER JTIME, OTIME
      INTEGER JSTEP
      INTEGER LOGDEV
      INTEGER STATUS
      INTEGER T, N, I, J, L, HR, IMIN, IMAX, JMIN, JMAX
      INTEGER NHRS
      INTEGER NLAYS, MXREC
      
      REAL*8 AVERAGE, SCL
      
      REAL, ALLOCATABLE :: INCONC(:,:)
      REAL, ALLOCATABLE :: OUTCONC(:,:)
      REAL, ALLOCATABLE :: LATIT(:,:)
      REAL, ALLOCATABLE :: LONGI(:,:)

      INTEGER, ALLOCATABLE :: COL(:,:)
      INTEGER, ALLOCATABLE :: ROW(:,:)
      REAL, ALLOCATABLE :: XLOCA(:,:)
      REAL, ALLOCATABLE :: YLOCA(:,:)


      CHARACTER*16 :: PROGNAME = 'ScaleWindow'
      
!***********************************************************************
 
!.... Initialize the I/O API ...
      LOGDEV = INIT3()        !  initialization returns unit # for log

!.... Get infile/outfile

      INFILE = 'INFILE'
      OUTFILE = 'OUTFILE'
      STACKFILE = 'STACKFILE'

!.... Read stack input file      

      IF ( .NOT. OPEN3( STACKFILE, FSREAD3, PROGNAME ) ) THEN
         MESG = 'Could not open file "'//TRIM(STACKFILE)//'" for input'
         CALL M3EXIT( PROGNAME, 0, 0, MESG, 2 )
      ENDIF

      IF ( .NOT. DESC3( STACKFILE ) ) THEN
         MESG = 'Could not get file description'
         CALL M3EXIT( PROGNAME, 0, 0, MESG, 2 )
      ENDIF

      ALLOCATE( LATIT( NCOLS3D, NROWS3D),
     &          LONGI( NCOLS3D, NROWS3D),
     &          COL( NCOLS3D, NROWS3D),
     &          ROW( NCOLS3D, NROWS3D),
     &          XLOCA( NCOLS3D, NROWS3D),
     &          YLOCA( NCOLS3D, NROWS3D),
     &          STAT = STATUS )

      JSTEP = TSTEP3D
      NLAYS = NLAYS3D
      MXREC = MXREC3D
      JDATE = SDATE3D
      JTIME = STIME3D

          IF ( .NOT. READ3( STACKFILE, 'LATITUDE',
     &            ALLAYS3, JDATE, JTIME, LATIT ) ) THEN
            MESG = 'Could not read from "'//TRIM(INFILE)//'"'
            CALL M3EXIT(PROGNAME,0,0,MESG,2)
          ENDIF

          IF ( .NOT. READ3( STACKFILE, 'LONGITUDE',
     &            ALLAYS3, JDATE, JTIME, LONGI ) ) THEN
            MESG = 'Could not read from "'//TRIM(INFILE)//'"'
            CALL M3EXIT(PROGNAME,0,0,MESG,2)
          ENDIF

          IF ( .NOT. READ3( STACKFILE, 'COL',
     &            ALLAYS3, JDATE, JTIME, COL ) ) THEN
            MESG = 'Could not read from "'//TRIM(INFILE)//'"'
            CALL M3EXIT(PROGNAME,0,0,MESG,2)
          ENDIF

          IF ( .NOT. READ3( STACKFILE, 'ROW',
     &            ALLAYS3, JDATE, JTIME, ROW ) ) THEN
            MESG = 'Could not read from "'//TRIM(INFILE)//'"'
            CALL M3EXIT(PROGNAME,0,0,MESG,2)
          ENDIF

          IF ( .NOT. READ3( STACKFILE, 'XLOCA',
     &            ALLAYS3, JDATE, JTIME, XLOCA ) ) THEN
            MESG = 'Could not read from "'//TRIM(INFILE)//'"'
            CALL M3EXIT(PROGNAME,0,0,MESG,2)
          ENDIF

          IF ( .NOT. READ3( STACKFILE, 'YLOCA',
     &            ALLAYS3, JDATE, JTIME, YLOCA ) ) THEN
            MESG = 'Could not read from "'//TRIM(INFILE)//'"'
            CALL M3EXIT(PROGNAME,0,0,MESG,2)
          ENDIF


!.... Open input file ...
      IF ( .NOT. OPEN3( INFILE, FSREAD3, PROGNAME ) ) THEN
         MESG = 'Could not open file "'//TRIM(INFILE)//'" for input'
         CALL M3EXIT( PROGNAME, 0, 0, MESG, 2 )
      ENDIF

      IF ( .NOT. DESC3( INFILE ) ) THEN
         MESG = 'Could not get file description'
         CALL M3EXIT( PROGNAME, 0, 0, MESG, 2 )
      ENDIF

!.... Save I/O API variables

      JSTEP = TSTEP3D
      NLAYS = NLAYS3D
      MXREC = MXREC3D
      
!.... Set I/O API variables for outfile
      
      TSTEP3D = 010000
      NLAYS3D = 1
      
!.... Create outfile

      IF ( .NOT. OPEN3( OUTFILE, FSCREA3, PROGNAME ) ) THEN
         MESG = 'Could not open file '//TRIM(OUTFILE)
         CALL M3EXIT( PROGNAME, 0, 0, MESG, 2 )
      ENDIF
      
!.... Allocate arrays

      ALLOCATE( INCONC( NCOLS3D, NROWS3D ),
     &          OUTCONC( NCOLS3D, NROWS3D ),
     &          STAT = STATUS )
      IF( STATUS /= 0 ) STOP "Error allocating arrays"


            DO I = 1, NCOLS3D, 1
              DO J = 1, NROWS3D, 1
              OUTCONC(I,J) = 0.0
              ENDDO
             ENDDO

!.... Scale factor to apply within window
       SCL = 0.0


      DO N = 1, NVARS3D, 1
        JDATE = SDATE3D
        JTIME = STIME3D
        
!.... read in data
        HR = 0
        DO T = 1, MXREC, 1
          HR = HR + 1


          IF ( .NOT. READ3( INFILE, VNAME3D(N), 
     &            ALLAYS3, JDATE, JTIME, INCONC ) ) THEN
            MESG = 'Could not read from "'//TRIM(INFILE)//'"'
            CALL M3EXIT(PROGNAME,0,0,MESG,2)
          ENDIF

            DO I = 1, NCOLS3D, 1
              DO J = 1, NROWS3D, 1
               IF((LATIT(I,J) .GT. 42.9) .AND.
     &            (LATIT(I,J) .LT. 43.2) .AND.
     &            (LONGI(I,J) .LT. -121.8) .AND.
     &            (LONGI(I,J) .GT. -122.2)) THEN
c               IF((LATIT(I,J) .GT. 46.5) .AND.
c     &            (LATIT(I,J) .LT. 47.5) .AND.
c     &            (LONGI(I,J) .LT. -120.5) .AND.
c     &            (LONGI(I,J) .GT. -122.0)) THEN
                 OUTCONC(I,J) = 0.0
                 write(*,*)I,J,LATIT(I,J),LONGI(I,J),XLOCA(I,J),
     &            YLOCA(I,J),COL(I,J),ROW(I,J)
                ELSE
                 OUTCONC(I,J) = INCONC(I,J)
                ENDIF
              ENDDO
            ENDDO
            HR = 0

            IF (.NOT. WRITE3( OUTFILE, VNAME3D(N), JDATE,
     &                        JTIME, OUTCONC ) ) THEN

              MESG = 'Could not write to '//TRIM(OUTFILE)
              CALL M3EXIT( PROGNAME, 0, 0, MESG, 2 )
            ENDIF

        
          CALL NEXTIME( JDATE, JTIME, JSTEP )
        
        ENDDO
      ENDDO
      
!.... Deallocate arrays
!      DEALLOCATE( INCONC, OUTCONC, BUFFER )
      
      CALL M3EXIT( PROGNAME, 0, 0, '', 0)
      
      END
