       Subroutine sststp(sstupstep)
C       Implicit none									   
       INCLUDE "parmeta"								   
       INCLUDE "parm.tbl"								   
       INTEGER, PARAMETER    :: la=13							   
       INTEGER, PARAMETER    :: imjm=im*jm-jm/2,jam=6+2*(jm-10)  			   
     +      , lp1=lm+1,ltop=1,lbot=lm , lscrch=4*lm+1+la +1  				   
     +      , l1=la+lm+1,l2=la+2*lm+1 , l3=la+3*lm+1,l4=la+4*lm+1			   
       INCLUDE "CTLBLK.comm"								   
       INCLUDE "LOOPS.comm"								   
       INCLUDE "MASKS.comm"								   
       INCLUDE "PHYS.comm"								   
       
       integer,intent(in)	    :: sstupstep					   
       integer  		    :: day						   
       integer  		    :: mon						   
       integer  		    :: yr						   
       integer  		    :: utc						   
       integer  		    :: ma						   
       integer  		    :: mb
       integer                      :: imes
       integer                      :: imes2						   
       real*4			    :: yday						   
       real*4			    :: add						   
       real*4			    :: fa						   
       real*4			    :: fb						   
       integer, dimension(12)	    :: monl
       character(len=10)            :: chardate	
       					   
      character(len=256):: m_before, m_after
      character(len=2)::cn,cn2
      real,dimension(im,jm):: monbefore, monafter
      integer::X,ntsdday,calc_mon
      
      iyr=IDAT(3)  ! 
      imo=IDAT(1)  ! initial date, day, month, year
      idy=IDAT(2)  !
      
      iutc=NTSD*DT/3600     ! chou dt eh 90seg
      
      CALL getdate(iyr,imo,idy,iutc,yr,mon,day,utc)
       
       data monl /31,28,31,30,31,30,31,31,30,31,30,31/					   
       
       if (mod(yr,4) .eq. 0) then
        monl(2)=29 
      endif					   

        yday=day 									   
        ma=mon-1
     
      if (yday.ge.(1.0+float(monl(mon))/2.0)) then       ! changing the month of SST from the archive,  
	                                            
						              ! each 16th of the month, for the months with 30 days,
						              ! each 17th of the month, for the months with 31 days,
                                                              ! 15th of the month each February, 
						              ! beside when it has 29 days, then it changes at the day 16th 
         
	ma=mon
      endif
 
        mb=ma+1	 
       
      if (ma .lt. 1) then
        ma=12
      endif								   
       
      if (mb .gt. 12)then
        mb=1
      endif
	
	IF(MYPE.EQ.0) THEN
      	    print*,"MA",ma,"DAY",yday, "YR",yr
	ENDIF
	
       
CGAJSST MUDANDO O MES DA LEITURA DA SST NO DIA 16 DE CADA MES PARA UM dt=90      
	
C      print*,'NTSD=',ntsd
	

                                                   ! the archive consists of the monthly SST data for the period December 1981 - July 2011
	
C!VERSION DRAGAN	 initsst=(iyr-1982)*12+1+imo               ! this line calculates the initial SST
            initsst=imo
						   ! if the day is in the first half of each month, it will be recalculated below, in calc_mon,
						   ! it will use ma=mon-1, and calc_mon will start from -1


      if (ma.eq.12.and.yday.lt.(float(monl(mon))/2.0+1.0)) then       ! defines the problem when the year is changing, for the first 15 days of each January
         calc_mon=(yr-iyr)*12+(ma-imo)-12                        ! calculates the current month of SST taken from archive, for the period 1-15th of January each year
      else
         calc_mon=(yr-iyr)*12+(ma-imo)                           ! calculates the current month of SST taken from archive for all the other dates
      endif

         imes=initsst+calc_mon                      ! "previous" month from the archive, for making interpolation of monthly SST for specific date 
         imes2=imes+1                               ! "future" month from the archive, for making interpolation of monthly SST for specific date  
	 

C!GSM- 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     


C!++++++++++++++++++++++++++++++++++++++++

C!       ntsdday=86400/DT 

C!     if (idy.ge.(1.0+float(monl(imo))/2.0)) then    ! a part of the code added to fix the old version used in regional ETA, in the case that each month has 30 days
C!       X=(float(monl(imo))-idy)*ntsdday
C!     else
C!       X=-(float(monl(imo))-idy)*ntsdday
C!     endif   


C!+++++++++++++++++++++++++++++++++++++++++
C!	 initsst=297                      
C!	 ntsd1=(ntsd-X+(nmessst/2))
	
C!        imes=int(ntsd1/nmessst)+initsst             ! old part of the code taken from regional ETA, which will work correctly only in the case 
C!        imes2=imes+1                                ! that each month has 30 days, but together with the definition of X, declared in the code above

C!        print*,'X=',X
!++++++++++++++++++++++++++++++++++++++++++
					   
          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

C          print*,"IMES=",imes,"IMES2=",imes2
	
C       write(cn,'(i2.2)')imes
	
C           m_before='monthly_sst'//cn//'.bin'
C           open(11,file=m_before,form='unformatted',status='unknown',
C     &      access='sequential')
C           read(11) monbefore
C           close(11)
      
C       write(cn2,'(i2.2)')imes2
  
C           m_after='monthly_sst'//cn2//'.bin'
C           open(22,file=m_after,form='unformatted',status='unknown',
C     &      access='sequential')
C           read(22) monafter
C	   close(22)
	
CDRAGAN        sst=fa*monbefore(:,:)+fb*monafter(:,:)
         sst=fa*sstm(:,:,imes)+fb*sstm(:,:,imes2)

C        print*,"MONB",monbefore(1,1),"MONA",monafter(1,1)

C	do l=1,im
C	  do k=1,jm
C	   sst=fa*monbefore(l,k)+fb*monafter(l,k)
C          enddo
C	enddo
       
       RETURN										   
       END
