!$$$  sub  PROGRAM DOCUMENTATION BLOCK
!                .      .    .                                       .
! sub  PROGRAM:  gridst   READ GLOBAL Grib SST
!                            
!
! ABSTRACT: SUBSTITUTE THE GRIBST SUB PROGRAM
!           READ THE SST FIELD IN A BINARY FORMAT 
!           INSTED A GRIB FORMAT
!           IN THIS CASE THE BINARY FILE IS GENERATE
!           FROM SST GRIB2 FILE WITH WGRIB2 PROGRAM: 
!           wgrib2 -d 1 sst2dvar_grb_0.5.grib2 -v \
!           -no_header big_endian -bin ssthiresgrd.bin
!
! PROGRAM HISTORY LOG:
!   08-11-03  JORGE LUIS GOMES
!
!
    PROGRAM READ_SST
!  
    USE PARMETA
    USE DIAGNOSTIC
!
    IMPLICIT NONE 
!------------------------------------------------------- 
! SET PRIMARY GRID DIMENSIONS AND PRESSURE OUTPUT LEVELS
!------------------------------------------------------- 

    INTEGER(KIND=R4KIND)                                                    :: NX
    INTEGER(KIND=R4KIND)                                                    :: NXP1
    INTEGER(KIND=R4KIND)                                                    :: NY
    INTEGER(KIND=I4KIND)                                                    :: jj
    INTEGER(KIND=I4KIND)                                                    :: kk
    INTEGER(KIND=I4KIND)                                                    :: I
    INTEGER(KIND=I4KIND)                                                    :: J
    INTEGER(KIND=I4KIND)                                                    :: insst
    INTEGER(KIND=I4KIND)                                                    :: outsst
    INTEGER(KIND=I4KIND)                                                    :: outsst2
    INTEGER(KIND=I4KIND)                                                    :: INLST
    INTEGER(KIND=I4KIND)                                                    :: IOstatus
    INTEGER(KIND=I4KIND)                                                    :: NREC
    REAL   (KIND=R4KIND), DIMENSION(:,:)       , ALLOCATABLE                :: SSTIN
    REAL   (KIND=R4KIND), DIMENSION(:,:)       , ALLOCATABLE                :: GSST
    REAL   (KIND=R4KIND), DIMENSION(:,:)       , ALLOCATABLE                :: GLAT
    REAL   (KIND=R4KIND), DIMENSION(:,:)       , ALLOCATABLE                :: GLON    
    REAL   (KIND=R4KIND), DIMENSION(:,:)       , ALLOCATABLE                :: SST
    REAL   (KIND=R4KIND), DIMENSION(:,:)       , ALLOCATABLE                :: SST2
    REAL   (KIND=R4KIND), DIMENSION(:,:)       , ALLOCATABLE                :: HGT
    REAL   (KIND=R4KIND), DIMENSION(:,:)       , ALLOCATABLE                :: SM
    REAL   (KIND=R4KIND), DIMENSION(:,:)       , ALLOCATABLE                :: AR1
    REAL   (KIND=R4KIND), DIMENSION(:,:)       , ALLOCATABLE                :: AR2
    REAL   (KIND=R4KIND), DIMENSION(:,:)       , ALLOCATABLE                :: AR3
    REAL   (KIND=R4KIND), DIMENSION(:,:)       , ALLOCATABLE                :: AR4 
    INTEGER(KIND=R4KIND), DIMENSION(:,:)       , ALLOCATABLE                :: LON1INDX 
    INTEGER(KIND=R4KIND), DIMENSION(:,:)       , ALLOCATABLE                :: LON2INDX
    INTEGER(KIND=R4KIND), DIMENSION(:,:)       , ALLOCATABLE                :: LAT1INDX
    INTEGER(KIND=R4KIND), DIMENSION(:,:)       , ALLOCATABLE                :: LAT2INDX 
    REAL   (KIND=R4KIND)                                                    :: INCR_LAT
    REAL   (KIND=R4KIND)                                                    :: INCR_LON 
    REAL   (KIND=R4KIND)                                                    :: UNDEF 
!   
    REAL   (KIND=R4KIND)                                                    :: SST_MINVAL
    REAL   (KIND=R4KIND)                                                    :: SST_MAXVAL
!
    NAMELIST /SSTGRD_IN/                                                                        &
    & INCR_LAT          ,                                                                       &
    & INCR_LON          ,                                                                       &
    & NX                ,                                                                       &                    
    & NY                ,                                                                       &
    & UNDEF
!---------------------------------------------------------------------------------------
! READ NAMELIST SSTGRD WHICH DEFINE THE SST INPUT PARAMETERS
!---------------------------------------------------------------------------------------
    INLST = 11
    OPEN(INLST, FILE='NAMELIST_SSTGR', STATUS='OLD')
    REWIND INLST
    READ(INLST, SSTGRD_IN)

    NXP1=NX+1
    
    INSST =39
    OUTSST=15
    OUTSST2=17

    ALLOCATE(SSTIN(NX,NY))
    ALLOCATE(GSST(NXP1,NY))
    ALLOCATE(SST(IM,JM))
    ALLOCATE(SST2(IM,JM))
    ALLOCATE(GLAT(IM,JM))
    ALLOCATE(GLON(IM,JM))
    ALLOCATE(AR1(IM,JM))
    ALLOCATE(AR2(IM,JM))
    ALLOCATE(AR3(IM,JM))
    ALLOCATE(AR4(IM,JM))
    ALLOCATE(LON1INDX(IM,JM))
    ALLOCATE(LON2INDX(IM,JM))
    ALLOCATE(LAT1INDX(IM,JM))
    ALLOCATE(LAT2INDX(IM,JM))
    ALLOCATE(HGT(IM,JM))
    ALLOCATE(SM(IM,JM))


!    READ SEA-LAND MASK ETA-GRID
    READ(10) HGT,SM
 !            OPEN UNIT FOR READING GRID FILE
!    OPEN (INSST,form='unformatted',convert='big_endian')
     OPEN (INSST,form='unformatted',access='direct',recl=nx*ny*4)
 !            OPEN UNIT FOR WRITING EGRID FILE
     OPEN (95,form='unformatted',convert='big_endian')
     OPEN (96,form='unformatted',convert='big_endian')
  
    CALL Geographic(GLAT, GLON,IM,JM,DLMD,DPHD,TLM0D,TPH0D)
! 
    CALL SSTG2EGRD                                                                                &       
    &(GLAT,GLON,INCR_LAT,INCR_LON,NX,AR1,AR2,AR3,AR4,LON1INDX,LON2INDX,LAT1INDX,LAT2INDX)

    NREC=0
    DO
!      READ(INSST,IOSTAT=IOstatus,rec=NREC+1)((sstin(I,ny+1-J),I=1,nx),J=1,ny)
      READ(INSST,IOSTAT=IOstatus,REC=NREC+1)((sstin(I,J),I=1,nx),J=1,ny)
     WRITE(6,*)'IOSTAT=',IOstatus
     IF (IOstatus/=0) EXIT
      NREC=NREC+1
      WRITE(6,*)'READ SST REC ',NREC           
      GSST(1:NX,1:NY) = SSTIN(1:NX,1:NY)
!...   add greenich to right side of grid
!
      GSST(NXP1,1:NY) = GSST(1,1:NY)
      write(96)GSST
      CALL SST_FILL(GSST,NXP1,NY,UNDEF)
      write(95)GSST
      DO J=1,JM
        DO I=1,IM
          SST2(I,J)=AR1(I,J)*GSST(LON1INDX(I,J),LAT1INDX(I,J))+                                    &
          &         AR2(I,J)*GSST(LON1INDX(I,J),LAT2INDX(I,J))+                                    &
          &         AR3(I,J)*GSST(LON2INDX(I,J),LAT2INDX(I,J))+                                    &
          &         AR4(I,J)*GSST(LON2INDX(I,J),LAT1INDX(I,J))
          SST(I,J)=GSST(LON1INDX(I,J),LAT1INDX(I,J))
          IF ((SST(I,J).LT.100).AND.(SM(I,J).GT.0.5)) THEN
            WRITE(6,*)'SST(',I,',',J,')=',SST(I,J),'SSTG(',LON2INDX(I,J),',',LAT2INDX(I,J),')=',GSST(LON2INDX(I,J),LAT2INDX(I,J))
          END IF
          IF ((SST2(I,J).LT.100).AND.(SM(I,J).GT.0.5)) THEN
            WRITE(6,*)'SST2(',I,',',J,')=',SST2(I,J),'SM(',I,',',J,')=',SM(I,J)
          END IF
         ENDDO
       ENDDO
      write(OUTSST)SST
      write(OUTSST2)SST2
      DO I=1,IM
        DO J=1,JM
         IF ((SST(I,J).LT.100).AND.(SM(I,J).GT.0.5)) THEN
           WRITE(6,*)'SST(',I,',',J,')=',SST(I,J),'SM(',I,',',J,')=',SM(I,J)
         END IF
       END DO
      END DO
      SST_MINVAL=MINVAL(SST(1:NX,1:NY),SM>0.5) 
      SST_MAXVAL=MAXVAL(SST,SM>0.5)
       
      WRITE(6,*)'SST MINVAL: ',SST_MINVAL
      WRITE(6,*)'SST MAXVAL: ',SST_MAXVAL
      WRITE(6,*)'SST LOC MINVAL ',MINLOC(SST,MASK=SST.EQ.SST_MINVAL)
      WRITE(6,*)'SST LOC MAXVAL ',MINLOC(SST,MASK=SST.EQ.SST_MAXVAL)
    END DO       
  WRITE(6,*)'NUMBER OF SST REC ',NREC           
  write(6,*) 'leaving GRIDST'
   END PROGRAM READ_SST
