             SUBROUTINE DATA_SST
       
       integer, parameter           :: dt=90
       integer  		    :: yr,imo
       integer  		    :: utc,idy,day
       integer  		    :: ma,calc_mon
       integer  		    :: mb
       integer                      :: imes
       integer                      :: imes2
       integer                      :: ntsd
       real*4			    :: yday,teste
       real*4			    :: add
       real*4			    :: fa
       real*4			    :: fb
       integer, dimension(12)	    :: monl
       
       data monl /31,28,31,30,31,30,31,31,30,31,30,31/
       
       imo=11
       iyr=2007
       idy=15
       
      do ntsd=1,132480
      
      iutc=NTSD*DT/3600
      
      CALL getdate(iyr,imo,idy,iutc,yr,mon,day,utc)
      
      if (mod(yr,4) .eq. 0) then
        monl(2)=29 
      endif
        
	yr=iyr
        yday=day
	ma=mon-1
	
	if (yday.ge.(1.0+float(monl(mon))/2.0)) then
          ma=mon
        endif
        
C	teste=1.0+float(monl(mon))/2.0
C	Este resultado é 16
	

        mb=ma+1	
	print*,"MA2",ma,"MB",mb
	
       
      if (ma .lt. 1) then
        ma=12
      endif								   
       
      if (mb .gt. 12)then
        mb=1
      endif
	
      print*,"MA",ma,"MB",mb,"YR",yr,"MON",mon,"DAY",yday
	
       
	
      print*,'NTSD=',ntsd
           
	    initsst=imo


      if (ma.eq.12.and.yday.lt.(float(monl(mon))/2.0+1.0)) then       
         calc_mon=(yr-iyr)*12+(ma-imo)-12 
      else	                        
         calc_mon=(yr-iyr)*12+(ma-imo)                           
      endif

         imes=initsst+calc_mon                     
         imes2=imes+1  
      print*,"IMESS",imes,"IMESSS",imes2,"INITSST",initsst,"CALC",calc_mon                             
	 

CGSM- Foi alterado pois rodadas no mes de Janeiro estava dando erro 	 
	 
	 if (imes .lt. 1) then
           imes=12
         endif								   
       
         if (imes2 .gt. 12)then
          imes2=1
         endif     

					   
          add=float(monl(ma))/2.0-1.0
       
       if (ma.eq.mon) then
         add=-add-2.
       endif 
       
       
          fb=2.0*(yday+add)/float(monl(ma)+monl(mb))					   
          fa=1.0-fb
	  
	  sst=fa*sstm(:,:,imes)+fb*sstm(:,:,imes2)

          print*,"IMES=",imes,"IMES2=",imes2,"FA",fa,"FB",fb
	
	stop
       enddo
       END     
C&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
      SUBROUTINE GETDATE(iyr,imo,idy,iutc,cyr,cmon,cday,cutc)
C-----------------------------------------------------------------
      IMPLICIT NONE
      INTEGER,INTENT(IN)                 ::iyr,imo,idy,iutc
      INTEGER, INTENT(OUT)               ::cyr,cmon,cday,cutc
      INTEGER                            ::yr,mon,day,utc
      INTEGER, DIMENSION(12)             ::monl
      INTEGER dystep
C*
      yr=iyr
      mon=imo
      day=idy
      utc=iutc
      DATA monl /31,28,31,30,31,30,31,31,30,31,30,31/
      dystep=0
      
C*
     IF (mod(yr,4) .eq. 0) monl(2)=29
C      utc=m
      IF (utc.GE.24) THEN
        dystep=dystep+(utc/24)
        utc=(MOD(utc,24))
      ENDIF
      day=day+dystep
      DO
        IF (day.GT.monl(mon)) THEN
          day=day-monl(mon)
          mon=mon+1
          IF (mon.eq.13) THEN
            mon=1
            yr=yr+1
            IF (MOD(yr,4).EQ.0) monl(2)=29
          ENDIF
        ELSE
          exit
        ENDIF
      ENDDO
      cyr=yr
      cmon=mon
      cday=day
      cutc=utc
C      IF (mon .LT. 10) cmon(1:1)='0'
C      IF (day .LT. 10) cday(1:1)='0'

      RETURN
      END SUBROUTINE GETDATE
      
