	program rgmodel
	parameter (lon=50, lat=25)
	common /var/ u(lon,lat,3), v(lon,lat,3), h(lon,lat,3)
	common /par/ f(lat), taux(lon,lat), dx,d2x,dx2,dy,d2y,dy2,dt,d2t
     &    loop, hinitial, lp,l,lm, delt, rg, ah, rho, day2sec
        common /par/ tauy(lon,lat)
	
	pi = 3.1416          ! pi
	deg2rad = pi/180.0   ! index to convert degree measure to radian
	g = 9.8              ! gravity of earth
	rg = 0.004*g         ! reduced gravity 
	rearth = 6400.e3     ! radius of earth
	deg2meter = (pi/180.0)*rearth  ! equivalent distance for 1 degree
	omega = 0.727e-4     ! rotaiton speed of earth
	dt = 4800.0          ! model time-step (seconds) 
	dx = 2.0*deg2meter   ! zonal resolution 
	dy = 1.0*deg2meter   ! meridional resolution
	d2x = 2.0*dx         
	d2y = 2.0*dy
	dx2 = dx*dx
	dy2 = dy*dy
	ah = 5.0e3           ! diffusion/viscocity coefficient
	rho = 1023.0         ! density of the layer
	hinitial = 100.0     ! thickness of the layer
	run_days = 360*1.0     ! model running days
	out_put = 5.0       ! model out_put sampling days
	day2sec = 86400.0    ! 1 day in seconds

c
c	define a beta-plane f=f0 + beta.dy
c
	theta = 0.0          ! central latitude of the model domain
	f0 = 2.0*omega*sin(theta*deg2rad)  ! coriolis force at theta
	beta = 2.0e-11       ! beta value at theta location
	do j=1,lat    
	y = (j-lat/2)*dy
	f(j) = f0 + beta*y   ! coriolis force in beta-plane
	enddo
	
c
c	initialize ocean as rest
c
	do i=1,lon           ! longitude
	do j=1,lat           ! latitude
	do l=1,3             ! time step indexes (past, present,future)
	 u(i,j,l) = 0.0      ! ocean is in rest at the beginining
	 v(i,j,l) = 0.0      ! ocean is in rest at the beginining
	 h(i,j,l) = hinitial ! initial layer thickness
	enddo
	enddo
	enddo
c
c	define wind forcing
c
	amplitude = 0.1    ! seting initial condition for wind (or thickness)
	do i=1,lon
	do j=1,lat
	do l=1,3
c	 taux(i,j) = amplitude*sin(2.0*(j-lat/2.)*deg2rad) !typical zonal wind
c	  if (i .le. 4 ) then
c	 tau(i,j) = 0.05 !
c	  else
c	 tau(i,j) = 0.0
c	endif
	 taux(i,j) = 0.1*
     &  exp( -( 0.5*(i-lon/3.0)**2 / 25 + (0.5+(j-lat/2.0))**2 / 9 ) ) !gaussian fit
c	 tauy(i,j) = 0.1*
c     &  exp( -( 0.5*(i-lon/3.0)**2 / 25 + (0.5+(j-lat/2.0))**2 / 9 ) ) !gaussian fit
c	 taux(i,j) =-0.1*
c     &  exp( -( (i-lon/1.5)**2 / 275 + (0.5+(j-lat/2.3))**2 / 9 ) ) !IOD forcing
c	 tauy(i,j) =+0.02*
c     &  exp( -( (i-lon/1.5)**2 / 275 + (0.5+(j-lat/2.3))**2 / 9 ) ) !IOD forcing
c	 h(i,j,l) = hinitial + amplitude*1000.0*
c     &  *exp( -( ((i-lon/2.2)/5.0)**2 + (0.5+(j-lat/1.2)/5.0)**2 ) )  !gausial like fit
	enddo
	enddo
	enddo
c
c	use forward-time-difference for first time step
c	
	delt = dt   ! at the begining forward intergration for first time step
	lp = 3      ! future time index (preditction)
	l = 2       ! present time index
	lm = 1      ! past time index
	
c
c	open file for out_put    
c
	open(1,file='outh.dat',form='unformatted',access='direct',
     &   recl=lon*lat)           ! output is written to the file
	open(2,file='outu.dat',form='unformatted',access='direct',
     &   recl=lon*lat)           ! output is written to the file
	open(3,file='outv.dat',form='unformatted',access='direct',
     &   recl=lon*lat)           ! output is written to the file
	open(4,file='outx.dat',form='unformatted',access='direct',
     &   recl=lon*lat)           ! output is written to the file
	open(5,file='outy.dat',form='unformatted',access='direct',
     &   recl=lon*lat)           ! output is written to the file
	lrec = 0                   ! format is binary
	lrec = 0                   ! format is binary
c
c	begine time-stepping
c
	loop_total = int(run_days*day2sec/dt)   ! number of integration for...
	do loop = 1,loop_total                  ! ..model running days
         if (loop .gt. 180) taux(:,:) = 0.0
	 call model (loop)         ! calling model subroutine
c
c	write output
c
	lout_put = int(out_put*day2sec/dt)     ! writing output in specified 
	if (mod(loop,lout_put) .eq. 0.0) then  ! intervel
	lrec = lrec + 1
	write(*,*) 'writing output for day ->  ',loop*dt/day2sec, lrec
	write(1,rec=lrec)((h(i,j,3),i=1,lon),j=1,lat)
	write(2,rec=lrec)((u(i,j,3),i=1,lon),j=1,lat)
	write(3,rec=lrec)((v(i,j,3),i=1,lon),j=1,lat)
	write(4,rec=lrec)((taux(i,j),i=1,lon),j=1,lat)
	write(5,rec=lrec)((tauy(i,j),i=1,lon),j=1,lat)
	endif
	
	enddo      ! end for model integration
	end        ! end for main program
	
	
	subroutine model (loop)   ! subroutine for integration
	parameter (lon=50, lat=25)
        common /var/ u(lon,lat,3), v(lon,lat,3), h(lon,lat,3)
        common /par/ f(lat), taux(lon,lat), dx,d2x,dx2,dy,d2y,dy2,dt,d2t
     &    loop, hinitial, lp,l,lm, delt, rg, ah, rho, day2sec
        common /par/ tauy(lon,lat)
	
c
c	solve mometum and layer thickness
c	                 ! centered space-difference scheme
	do i=2,lon-1     ! zonally
	 ip = i+1        ! one grid to right
	 im = i-1        ! one grid to left
	do j=2,lat-1     ! meridionally
	 jp = j+1        ! one grid to up
	 jm = j-1        ! one grid to down
c
c       Harmonic term, if needed
c
        harm = sin((3.14/180)*2.0*loop)
c
c	integrating momentum (u,v) and layer thickness one step advance
c
	u(i,j,lp) = u(i,j,lm) + (
     &     + f(j)*v(i,j,l)
     &     + taux(i,j)/rho/hinitial
     &     - rg*(h(ip,j,l) - h(im,j,l))/d2x
     &     + ah*( (u(ip,j,l)+u(im,j,l)-2.0*u(i,j,l))/dx2 + 
     &            (u(i,jp,l)+u(i,jm,l)-2.0*u(i,j,l))/dy2) 
     &     )*delt 
	v(i,j,lp) =  v(i,j,lm) + ( 
     &     + tauy(i,j)/rho/hinitial
     &     - f(j)*u(i,j,l)
     &     + tauy(i,j)/rho/hinitial
     &     - rg*(h(i,jp,l) - h(i,jm,l))/d2y
     &     + ah*( (v(ip,j,l)+v(im,j,l)-2.0*v(i,j,l))/dx2 + 
     &            (v(i,jp,l)+v(i,jm,l)-2.0*v(i,j,l))/dy2) 
     &     )*delt
	
	h(i,j,lp) = h(i,j,lm) + (
     &     -hinitial*((u(ip,j,l)-u(im,j,l))/d2x
     &            +(v(i,jp,l)-v(i,jm,l))/d2y)
     &     + ah*( (h(ip,j,l)+h(im,j,l)-2.0*h(i,j,l))/dx2 + 
     &            (h(i,jp,l)+h(i,jm,l)-2.0*h(i,j,l))/dy2) 
     &     )*delt
	enddo
	enddo
c
c	apply zonal boundary conditions (meridional)
c	
	do j=1,lat                  
	 u(1,j,lp) = -u(3,j,lp)     ! no normal flow across the rigid boundary
	 u(2,j,lp) =  0.0           ! no-slip at the boundary
	 v(1,j,lp) = -v(3,j,lp)     ! same as above but for v-velocity
	 v(2,j,lp) =  0.0           !
	 h(1,j,lp) = h(3,j,lp)      ! thickness restored to neighbouring value
	 u(lon,j,lp) = -u(lon-2,j,lp) ! same as above but for west 
	 u(lon-1,j,lp) = 0.0
	 v(lon,j,lp) = -v(lon-2,j,lp)
	 v(lon-1,j,lp) = 0.0
	 h(lon,j,lp) = h(lon-2,j,lp)
	enddo
c
c	apply meridional boundary conditions (zonally)
c	
	do i=1,lon
	 u(i,1,lp) = -u(i,3,lp)    ! at the north
	 u(i,2,lp) = 0.0           ! same as zonal boundary condition.
	 v(i,1,lp) = -v(i,3,lp)
	 v(i,2,lp) = 0.0
	 h(i,1,lp) = h(i,3,lp)
	 u(i,lat,lp) = -u(i,lat-2,lp)
	 u(i,lat-1,lp) = 0.0
	 v(i,lat,lp) = -v(i,lat-2,lp)
	 v(i,lat-1,lp) = 0.0
	 h(i,lat,lp) = h(i,lat-2,lp)
	enddo
c
c	apply asselin-robert filter
c	
	 do i=1,lon               ! filtering computational mode
	 do j=1,lat               ! for stable numerical solutions
	 u(i,j,l) = u(i,j,l) + 0.1*( 
     &   u(i,j,lp) -2.0*u(i,j,l) + u(i,j,lm))
	 v(i,j,l) = v(i,j,l) + 0.1*( 
     &   v(i,j,lp) -2.0*v(i,j,l) + v(i,j,lm))
	 h(i,j,l) = h(i,j,l) + 0.1*( 
     &   h(i,j,lp) -2.0*h(i,j,l) + h(i,j,lm))
	enddo
	enddo
	
c use leap-frog scheme from second time step and rotate the time index
	delt=2.0*dt       ! time step is switched to leap-frog (centered
	ltemp = lm        ! difference ) scheme.
	lm = l            ! time index is rotated once
	l = lp            ! this is equivalent to storing
	lp = ltemp        ! predicted array into present array
	
	return
	end               ! end of subroutine
