    SUBROUTINE SSTHIRES(SST, SM, GLAT1, GLON1, IDAT, IM, JM, NM, ND)
!--------------------------------------------------------------------------------------------------
    IMPLICIT NONE  
!
    INTEGER(KIND=4)                                                       , INTENT(IN)          ::&
    & ND
!
    INTEGER(KIND=4)                                                                             ::&
    & IM      , JM      , NM      , T       , INSST   , INDXST
!
    REAL   (KIND=4)                                                                             ::&
    & ILAT1   , ILON1   ,                                                                         &
    & ILAT2   , ILON2
!
    REAL   (KIND=4)     , PARAMETER :: INCR_LAT = 180.d0 / 2160
    REAL   (KIND=4)     , PARAMETER :: INCR_LON = 360.d0 / 4320
!
    REAL   (KIND=4)     , PARAMETER :: H90  =  90.0
    REAL   (KIND=4)     , PARAMETER :: H360 = 360.0
    REAL   (KIND=4)     , PARAMETER :: D5   =   5.E-1
    REAL   (KIND=4)     , PARAMETER :: D00  =   0.0
    REAL   (KIND=4)     , PARAMETER :: H1   =   1.0
!
    REAL   (KIND=4)     , PARAMETER :: DTR  =   4.0*atan(1.0d0) / 180.d0
!
    INTEGER(KIND=4)     , DIMENSION(4)                                                          ::&
    & IDATE
!
    INTEGER(KIND=4)     , DIMENSION(3)                                    , INTENT(IN)          ::&
    & IDAT
!
    INTEGER(KIND=4)     , DIMENSION(12)                                                         ::&
    & MONTH
!
    REAL   (KIND=4)     , DIMENSION(4321,2160)                                                  ::&
    & SSTLL
!
    REAL   (KIND=4)     , DIMENSION(12)                                                         ::&
    & SALTLK
!------------------------------------------
! DRAGAN, ATUALIZADO POR: GSM EM 2017-02-10
!------------------------------------------
    REAL   (KIND=4)     , DIMENSION(2)                                                          ::&
    & SALTLA  , SALTLO  
!
    REAL   (KIND=4)     , DIMENSION(0:IM+1, 0:JM+1, NM)                                         ::&
    & SST     , SM
!
    REAL   (KIND=4)     , DIMENSION(0:IM+1, 0:JM+1, NM)                   , INTENT(IN)          ::&
    & GLAT1   , GLON1
!
    REAL   (KIND=4)     , DIMENSION(0:IM+1, 0:JM+1, NM)                                         ::&
    & GLAT    , GLON
!------------------------
! IMPLICIT NONE VARIABLES
!------------------------
    INTEGER(KIND=4)                                                                             ::&
    & IERR    , N       , I       , J       , ID1     , ID2     , MARG0   , MNTH0   , MNTH1   ,   &
    & NUMER   , DENOM   , IARG1   , IARG2   , LON1INDX, LON2INDX, LAT1INDX, LAT2INDX
!
    REAL   (KIND=4)                                                                             ::&
    & ELAT    , ELON    , DIF     , W1      , W2      , AR1     , AR2     , AR3     , AR4     ,   &
    & FRAC
!
    DATA   INSST  /39/
    DATA   INDXST / 0/
!
    DATA MONTH    /31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31/
!
    DATA SALTLK   /273.38, 274.27, 278.50, 283.01, 287.33, 293.41,                                &
    &              297.13, 297.73, 294.97, 289.58, 282.31, 275.67/
!---------------------------------------------------------
! CORNERS OF SALT LAKE LATITUDE / LONGITUDE BOX IN DEGREES 
!                     40.0      42.0
!---------------------------------------------------------
    DATA SALTLA   /0.698132, 0.733038/
!---------------------------------------------------------
!                     111.0    114.0
!---------------------------------------------------------
    DATA SALTLO   /1.937315, 1.989675/
!
    GLON = -GLON1
    GLAT =  GLAT1
!
    WRITE(0,*) "CALL GRIDST FOR READING SST"
!
    CALL GRIDST(INSST,SSTLL)
!						
    IERR = 0
!						
    IF (IERR /= 0) GOTO 10
!--------------------------------------------------------------------------------------------------
! INTERPOLATE 1-DEG GLOBAL SST TO ETA GRID  -------
!
! NOTE: THIS SUBROUTINE AND INTERPOLATION ALGORITHM ASSUME A 1-DEG GLOBAL SST FIELD IN THE FOLLOW-
! ING FORMAT:  
!
! I=1 AT  0.5 E, I=2 AT  1.5 E, ..., I=360 AT  0.5 W
! J=1 AT 89.5 S, J=2 AT 88.5 S, ..., J=180 AT 89.5 N
!
! NEW 0.5 DEGREE DATA
!
! I=1 AT  0.25 E, I=720 AT  0.25 W, I=721 AT 0.25 E
! J=1 AT 89.75 S, J=360 AT 39.75 N 
! 
! IN THE INTERPOLATION ALGORITHM BELOW, GLON IS POSITIVE WESTWARD, FROM 0 TO 360, WITH 0 AT THE 
! GREENWICH MERIDIAN. ELON IS POSITIVE EASTWARD, THUS THE NEED TO SUBTRACT GLON FROM 360 TO GET THE
! INDEX OF THE CORRECT OISST POINT. IF YOUR INPUT 1 DEG SST FIELD IS IN A DIFFERENT INDEXING SCHEME
! YOU WILL NEED TO CHANGE THE ALGORITHM BELOW - SEE "GRDETA.OLDOI"
!
! WRITE THE SST MONTHLY DATA N A NEW FILE
!--------------------------------------------------------------------------------------------------
    DO N=1,NM
        DO J=0,JM+1
            DO I=0,IM+1
!    
                ELAT = H90  + GLAT(I,J,N) / DTR
                ELON = H360 - GLON(I,J,N) / DTR
!
                IF (ELON > H360) ELON = ELON - H360
!
                DIF = ELON - INT(ELON)
!
                IF (DIF < INCR_LON)                                      ILON1 = INT(ELON) + ( 0.5 * INCR_LON)
!	
                IF (DIF >= ( 1 * INCR_LON) .AND. DIF < ( 2 * INCR_LON))  ILON1 = INT(ELON) + ( 1.5 * INCR_LON) 
!
                IF (DIF >= ( 2 * INCR_LON) .AND. DIF < ( 3 * INCR_LON))  ILON1 = INT(ELON) + ( 2.5 * INCR_LON) 
!
                IF (DIF >= ( 3 * INCR_LON) .AND. DIF < ( 4 * INCR_LON))  ILON1 = INT(ELON) + ( 3.5 * INCR_LON) 
!
                IF (DIF >= ( 4 * INCR_LON) .AND. DIF < ( 5 * INCR_LON))  ILON1 = INT(ELON) + ( 4.5 * INCR_LON) 
!
                IF (DIF >= ( 5 * INCR_LON) .AND. DIF < ( 6 * INCR_LON))  ILON1 = INT(ELON) + ( 5.5 * INCR_LON) 
!
                IF (DIF >= ( 6 * INCR_LON) .AND. DIF < ( 7 * INCR_LON))  ILON1 = INT(ELON) + ( 6.5 * INCR_LON) 
!
                IF (DIF >= ( 7 * INCR_LON) .AND. DIF < ( 8 * INCR_LON))  ILON1 = INT(ELON) + ( 7.5 * INCR_LON)
!
                IF (DIF >= ( 8 * INCR_LON) .AND. DIF < ( 9 * INCR_LON))  ILON1 = INT(ELON) + ( 8.5 * INCR_LON) 
!
                IF (DIF >= ( 9 * INCR_LON) .AND. DIF < (10 * INCR_LON))  ILON1 = INT(ELON) + ( 9.5 * INCR_LON) 
!
                IF (DIF >= (10 * INCR_LON) .AND. DIF < (11 * INCR_LON))  ILON1 = INT(ELON) + (10.5 * INCR_LON)
!
                IF (DIF >= (11 * INCR_LON))                              ILON1 = INT(ELON) + (11.5 * INCR_LON)
!
!
                IF (ILON1 <= D00) ILON1 = 360.
!
                ILON2 = ILON1 + INCR_LON
!--------------------------------------------------------------------------------------
! NEW APPROACH SETS ILAT1, ILON1 TO POINT ON SST GRID THAT IS SW OF THE ETA GRID POINT.
!--------------------------------------------------------------------------------------
                DIF = ELAT - INT(ELAT)
!
                IF (DIF < INCR_LAT)                                      ILAT1 = INT(ELAT) + ( 0.5 * INCR_LAT)
!	
                IF (DIF >= ( 1 * INCR_LAT) .AND. DIF < ( 2 * INCR_LAT))  ILAT1 = INT(ELAT) + ( 1.5 * INCR_LAT) 
!
                IF (DIF >= ( 2 * INCR_LAT) .AND. DIF < ( 3 * INCR_LAT))  ILAT1 = INT(ELAT) + ( 2.5 * INCR_LAT) 
!
                IF (DIF >= ( 3 * INCR_LAT) .AND. DIF < ( 4 * INCR_LAT))  ILAT1 = INT(ELAT) + ( 3.5 * INCR_LAT) 
!
                IF (DIF >= ( 4 * INCR_LAT) .AND. DIF < ( 5 * INCR_LAT))  ILAT1 = INT(ELAT) + ( 4.5 * INCR_LAT) 
!
                IF (DIF >= ( 5 * INCR_LAT) .AND. DIF < ( 6 * INCR_LAT))  ILAT1 = INT(ELAT) + ( 5.5 * INCR_LAT) 
!
                IF (DIF >= ( 6 * INCR_LAT) .AND. DIF < ( 7 * INCR_LAT))  ILAT1 = INT(ELAT) + ( 6.5 * INCR_LAT) 
!
                IF (DIF >= ( 7 * INCR_LAT) .AND. DIF < ( 8 * INCR_LAT))  ILAT1 = INT(ELAT) + ( 7.5 * INCR_LAT)
!
                IF (DIF >= ( 8 * INCR_LAT) .AND. DIF < ( 9 * INCR_LAT))  ILAT1 = INT(ELAT) + ( 8.5 * INCR_LAT) 
!
                IF (DIF >= ( 9 * INCR_LAT) .AND. DIF < (10 * INCR_LAT))  ILAT1 = INT(ELAT) + ( 9.5 * INCR_LAT) 
!
                IF (DIF >= (10 * INCR_LAT) .AND. DIF < (11 * INCR_LAT))  ILAT1 = INT(ELAT) + (10.5 * INCR_LAT)
!
                IF (DIF >= (11 * INCR_LAT))                              ILAT1 = INT(ELAT) + (11.5 * INCR_LAT)
!
!
                ILAT2 = ILAT1 + INCR_LAT
!
                W1 = ELON - ILON1 + INCR_LON / 2.
!
                IF (W1 < D00) W1 = W1 + H360
!
                W2 = ELAT - ILAT1 + INCR_LAT / 2.
!
                AR1 = W1 * W2
                AR2 = W1 * (H1 - W2)
                AR3 = (H1 - W1) * (H1 - W2)
                AR4 = (H1 - W1) * W2
! 
                LON1INDX = 12 * (ILON1)
                LON2INDX = 12 * (ILON2)
                LAT1INDX = 12 * (ILAT1)
                LAT2INDX = 12 * (ILAT2)
!
                IF (LON2INDX < 1 .OR. LON2INDX > 4321) WRITE(0,*) "N=",N,"  I=",I,"  J=",J,"  LON2INDX=",LON2INDX    ! LUCCI
                IF (LAT2INDX < 1 .OR. LAT2INDX > 2160) WRITE(0,*) "N=",N,"  I=",I,"  J=",J,"  LAT2INDX=",LAT2INDX    ! LUCCI
!-------------------------
! SOMENTE TESTE DE MBOUNDS
!-------------------------
!                IF (LAT2INDX > 2160) LAT2INDX = 2160
!                IF (LAT1INDX <    1) LAT1INDX =    1 
!
                SST(I,J,N) = SSTLL(LON2INDX, LAT2INDX)
!
            END DO         
        END DO
    END DO
!--------------------------------------------
! INSERT TEMPERATURES FOR THE GREAT SALT LAKE
!--------------------------------------------
    ID1 = IDAT(1)
    ID2 = IDAT(2) + ND
!
    MARG0 = ID1 - 1
!
    IF (MARG0 < 1) MARG0 = 12
!
    MNTH0 = MONTH(MARG0)
    MNTH1 = MONTH(ID1)
!
    IF (ID2 < 15) THEN
        NUMER = ID2 + MNTH0 - 15
        DENOM = MNTH0
        IARG1 = MARG0
        IARG2 = ID1
    ELSE
        NUMER = ID2 - 15
        DENOM = MNTH1
        IARG1 = ID1
        IARG2 = ID1 +  1
        IF (IARG2 > 12) IARG2 = 1
    END IF
!
    FRAC = NUMER / DENOM
!
    DO N=1,NM
        DO J=1,JM
            DO I=1,IM
                IF (GLAT(I,J,N) > SALTLA(1) .AND. GLAT(I,J,N) < SALTLA(2)) THEN
                    IF (GLON(I,J,N) > SALTLO(1) .AND. GLON(I,J,N) < SALTLO(2)) THEN
                        IF (SM(I,J,N) > 0.5) SST(I,J,N) = SALTLK(IARG1) + (SALTLK(IARG2) - SALTLK(IARG1)) * FRAC
                    END IF
                END IF
            END DO
        END DO
    END DO
!
    RETURN
!
 10 CONTINUE
!-------------------------------------------
! ERROR OCCURRED WHEN INPUTING SST FROM GRIB
!-------------------------------------------
    WRITE (0, 20) INSST
 20 FORMAT ('0', 'ERROR OCCURRED WHEN READING IN SST        ','ON UNIT', I3, ' GRIB ' /' ', 'EXECUTION TERMINATING.')
!
    STOP 222
!
    END SUBROUTINE SSTHIRES 
