!$$$  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 PARMCONF
    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)                                                    :: OUTNREC
    INTEGER(KIND=I4KIND)                                                    :: INLST
    INTEGER(KIND=I4KIND)                                                    :: IOstatus
    INTEGER(KIND=I4KIND)                                                    :: NREC
    INTEGER(KIND=I4KIND)                                                    :: NFields
    INTEGER(KIND=I4KIND)                                                    :: NRECF
    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 
    CHARACTER(LEN=20)                                                       :: FILE_ACCESS
!   
    REAL   (KIND=R4KIND)                                                    :: SST_MINVAL
    REAL   (KIND=R4KIND)                                                    :: SST_MAXVAL
!
    NAMELIST /SSTGRD_IN/                                                                        &
    & INCR_LAT          ,                                                                       &
    & INCR_LON          ,                                                                       &
    & NX                ,                                                                       &
    & NY                ,                                                                       & 
    & FILE_ACCESS       ,                                                                       &
    & 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 =59
    OUTSST2=57
    OUTSST=55
    OUTNREC=53

    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
    open(10,file='etatopo.dat',status='unknown',form='unformatted')
    READ(10) HGT,SM
    DEALLOCATE(HGT)

 !            OPEN UNIT FOR READING GRID FILE
 !            OPEN UNIT FOR WRITING EGRID FILE
     OPEN (95,file='sstin.bin',form='unformatted',convert='big_endian')
     OPEN (96,file='sstinfilled.bin',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)
    WRITE(6,*)'FILE_ACCESS ',TRIM(FILE_ACCESS)
    SELECT CASE (TRIM(FILE_ACCESS))
      CASE ('DIRECT')
        OPEN (INSST,form='unformatted',access='direct',recl=nx*ny*4)
      CASE ('SEQUENTIAL')
        OPEN (INSST,form='unformatted',convert='big_endian')
    END SELECT
    NREC=0
    NrecF=1
    DO nfields=1,NrecF
      GSST=UNDEF
      write(6,*)'READ REC: ',nrec+1
      SELECT CASE (TRIM(FILE_ACCESS))
        CASE ('DIRECT')
          READ(INSST,REC=NREC+1)((gsst(I,J),I=1,nx),J=1,ny)
        CASE ('SEQUENTIAL')
          READ(INSST)((gsst(I,ny+1-J),I=1,nx),J=1,ny)
      END SELECT 
      NREC=NREC+1
      WRITE(6,*)'READ SST REC ',NREC, 'NX:',NX,'NY:',NY           
!...   add greenich to right side of grid
!
      GSST(NXP1,1:NY) = GSST(1,1:NY)
      SST_MINVAL=MINVAL(GSST(1:NX,1:NY)) 
      SST_MAXVAL=MAXVAL(GSST(1:NX,1:NY))
       
      WRITE(6,*)'SSTIN MINVAL: ',SST_MINVAL
      WRITE(6,*)'SSTIN MAXVAL: ',SST_MAXVAL
      WRITE(95)GSST
      CALL SST_FILL(GSST,NXP1,NY,UNDEF)
      WRITE(96)GSST
      SST2=-9.99E+20
      SST=-9.99E+20
!      DO CONCURRENT (I=1:IM, J=1:JM, SM(I,J) > .5)
      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))
         ENDDO
       ENDDO
       DEALLOCATE(GSST)
      WRITE(OUTSST)SST
      WRITE(OUTSST2)SST2
       
      SST_MINVAL=MINVAL(SST2,SM>0.5) 
      SST_MAXVAL=MAXVAL(SST2,SM>0.5)
       
      WRITE(6,*)'SST EGRD MINVAL: ',SST_MINVAL
      WRITE(6,*)'SST EGRD MAXVAL: ',SST_MAXVAL
      WRITE(6,*)'-------------------------------------------------------------------'
      WRITE(6,*)'-------------------------------------------------------------------'

    END DO       
 1001   CLOSE(OUTSST2) 
    WRITE(OUTNREC,"('Number of SST REC: ',I4)")NREC
    WRITE(6,*)'NUMBER OF SST REC ',NREC           
    WRITE(6,*) 'leaving GRIDST'
   END PROGRAM READ_SST
