       Subroutine sststp(sstupstep)                   ! taken from regional ETA and modified, DRAGAN, September 2011
!C       Implicit none									   
       use parmeta								   
       use parm_tbl								   
       use ctlblk_comm								   
       use loops_comm								   
       use masks_comm								   
       use phys_comm								   
       use mppstaff_comm
       
       INTEGER, PARAMETER    :: la=13							   
       INTEGER, PARAMETER    :: imjm=im*jm-jm/2,jam=6+2*(jm-10), 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
      ! lp1=lm+1
      
       integer,intent(in)	    :: sstupstep					   
       integer  		    :: day						   
       integer  		    :: mon						   
       integer  		    :: yr						   
       integer  		    :: utc						   
       integer  		    :: ma						   
       integer  		    :: mb
       integer                      :: imes
       integer                      :: imes2
       integer			    :: nmessst						   
       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(0:im+1,0:jm+1):: monbefore, monafter
      integer::X,ntsdday,calc_mon
       
     
     
       				   
       iyr=IDAT(3)  ! 
       imo=IDAT(1)  ! initial date, day, month, year
       idy=IDAT(2)  !
       imo=8
       iyr=2011
       idy=13
   
      print*,"IDY=",idy,"IMO=",imo
      
      
        iutc=NTSD*DT/3600     


      CALL getdate(iyr,imo,idy,iutc,yr,mon,day,utc)
       
   !    data monl /30,30,30,30,30,30,30,30,30,30,30,30/!!!!!!!!!!!!!!!!	
    
    
        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.(float(monl(mon))/2.0)) then   
       
       
      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
	
	
      print*,"MA",ma,"DAY",yday, "YR",yr
	

       

!CGAJSST MUDANDO O MES DA LEITURA DA SST NO DIA 16 DE CADA MES PARA UM dt=90      
	
      print*,'NTSD=',ntsd
	
	
	


                                                   ! the archive consists of the monthly SST data for the period December 1981 - July 2011
	
!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       






!++++++++++++++++++++++++++++++++++++++++

!       ntsdday=86400/DT 

!     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
!       X=(float(monl(imo))-idy)*ntsdday
!     else
!       X=-(float(monl(imo))-idy)*ntsdday
!     endif   




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

!        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


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

	
	   sst=fa*monbefore(:,:)+fb*monafter(:,:)
        
 
       
       RETURN										   
       END
