	program ensomodel
	parameter (lon=179, lat=119)
	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), vort(lon,lat), mask(lon,lat), harm
	common /par/ have(lon,lat), tx_anom(lon,lat), ty_anom(lon,lat)
	common /par/ Ax, Ay, B, By, Ly, Cx0, Cx1, Cx2, Cx3, Cy0, Cy1, Cy2, Cy3
	real Ax, Ay, Ly, Cx0, Cx1, Cx2, Cx3, Cy0, Cy1, Cy2, Cy3
	integer B, By
	namelist /param/ Ax, Ay, B, By, Ly, Cx0, Cx1, Cx2, Cx3, Cy0, Cy1, Cy2, Cy3
	
	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 = 1.0*deg2meter   ! zonal resolution 
	dy = 0.5*deg2meter   ! meridional resolution
	d2x = 2.0*dx         ! twice the dx
	d2y = 2.0*dy	     ! twice the dy
	dx2 = dx*dx 	     ! square of dx
	dy2 = dy*dy	     ! square of dy
	ah = 1.0e3           ! diffusion/viscocity coefficient
	rho = 1023.0         ! density of the layer
	hinitial = 100.0     ! thickness of the layer
	run_days = 360*100.0     ! model running days
	out_put = 10.0       ! model out_put sampling days
	day2sec = 86400.0    ! 1 day in seconds

c
c Read namelist variables
c
	open (1,file='namelist.nml')
	read (nml=param, unit=1) ! Reads the model coupling parameters`
	close(1)
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 as taux-M
c
	do i=1,lon
	do j=1,lat
	do l=1,3
	 taux(i,j) = max(-0.025, -0.05*
     &  exp( -( (j-lat/2.0)**2 / 30 )))
	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
	open(7,file='outp.dat',form='unformatted',access='direct',
     &   recl=lon*lat)           ! output is written to the file
	open(9,file='ouha.dat',form='unformatted',access='direct',
     &   recl=lon*lat)           ! output is written to the file
	open(10,file='outxa.dat',form='unformatted',access='direct',
     &   recl=lon*lat)           ! output is written to the file
	open(11,file='outya.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 generate simple mask
c
	mask(:,:) = 0.0
	do i=3,lon-2
	do j=3,lat-2
	 mask(i,j) = 1.0
	enddo
	enddo

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. 540) taux(:,:) = 0.0   ! switch of taux-M to zeror after a month
	 call model (loop)         ! calling model subroutine

c
c       Harmonic term, if needed
c
        harm = sin((3.14/180)*(2.0*720*6/loop_total)*loop)
	harm = (harm + 1.0)*0.5
	harm = 1.0
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)*harm,i=1,lon),j=1,lat)
	write(5,rec=lrec)((tauy(i,j)*harm,i=1,lon),j=1,lat)
	write(7,rec=lrec)((vort(i,j),i=1,lon),j=1,lat)
	write(9,rec=lrec)((have(i,j),i=1,lon),j=1,lat)
	write(10,rec=lrec)((tx_anom(i,j),i=1,lon),j=1,lat)
	write(11,rec=lrec)((ty_anom(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=179, lat=119)
        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), vort(lon,lat), mask(lon,lat), harm
	common /par/ have(lon,lat), tx_anom(lon,lat), ty_anom(lon,lat)
	common /par/ Ax, Ay, B, By, Ly, Cx0, Cx1, Cx2, Cx3, Cy0, Cy1, Cy2, Cy3
	real Ax, Ay, Ly, Cx0, Cx1, Cx2, Cx3, Cy0, Cy1, Cy2, Cy3
	integer B, By
c
c Ensure the total mass is not changing with time
c
	icount =  0
	rsum0 = 0
	rsum = 0
	do i=2,lon-1
	do j=2,lat-1
	 icount = icount + 1 * mask(i,j)
	 rsum0 = rsum0 + hinitial * mask(i,j) 
	 rsum = rsum + h(i,j,lm) * mask(i,j)
	enddo
	enddo
	 rhsub = (rsum - rsum0)/max(1,icount)
	have(:,:) = h(:,:,lm)

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	integrating momentum (u,v) and layer thickness one step advance
c

c
c Add wind stress anomalies
c
	if (i .ge. 1.0*lon/B .and. i .le. (B-1)*lon/B .and. 
     &      j .ge. 1.0*lat/By .and. j .le. (By-1)*lat/By) then
	tx_anom(i,j) = Ax*exp(-1.0*(j-lat/2)**2/Ly)*(
     &  (have(i+lon/B -1, j) + have(i+lon/B , j) + have(i+lon/B + 1, j))*.333
     & -(have(i-lon/B -1, j) + have(i-lon/B , j) + have(i-lon/B + 1, j))*.333)
	
	ty_anom(i,j) = Ay*exp(-1.0*(j-lat/2)**2/Ly)*(
     &  (have(i, j+lat/By -1) + have(i, j+lat/By) + have(i, j+lat/By + 1))*.333
     & -(have(i, j-lat/By -1) + have(i, j-lat/By) + have(i, j-lat/By + 1))*.333)
	else
	tx_anom(i,j) = 0.0
	ty_anom(i,j) = 0.0
	endif
	if (h(i,j,lm) - hinitial .le. -5.0 .and. h(i,j,lm) - hinitial .gt. -10.0) then
	tx_anom(i,j) = Cx1*tx_anom(i,j)
	ty_anom(i,j) = Cy1*ty_anom(i,j)
	endif
	if (h(i,j,lm) - hinitial .le. -10.0 .and. h(i,j,lm) - hinitial .gt. -20.0) then
	tx_anom(i,j) = Cx2*tx_anom(i,j)
	ty_anom(i,j) = Cy2*ty_anom(i,j)
	endif
	if (h(i,j,lm) - hinitial .le. -20.0 .and. h(i,j,lm) - hinitial .gt. -100.0) then
	tx_anom(i,j) = Cx3*tx_anom(i,j)
	ty_anom(i,j) = Cy3*ty_anom(i,j)
	endif



	u(i,j,lp) = u(i,j,lm) + (
     &     + f(j)*v(i,j,l)
     &     + (taux(i,j)+tx_anom(i,j))*harm/rho/hinitial
     &     - rg*(h(ip,j,l) - h(im,j,l))/d2x
     &     + ah*( (u(ip,j,lm)+u(im,j,lm)-2.0*u(i,j,lm))/dx2 + 
     &            (u(i,jp,lm)+u(i,jm,lm)-2.0*u(i,j,lm))/dy2) 
     &     )*delt * mask(i,j)
	v(i,j,lp) =  v(i,j,lm) + ( 
     &     - f(j)*u(i,j,l)
     &     + (tauy(i,j)+ty_anom(i,j))/rho/hinitial
     &     - rg*(h(i,jp,l) - h(i,jm,l))/d2y
     &     + ah*( (v(ip,j,lm)+v(im,j,lm)-2.0*v(i,j,lm))/dx2 + 
     &            (v(i,jp,lm)+v(i,jm,lm)-2.0*v(i,j,lm))/dy2) 
     &     )*delt* mask(i,j)
	
	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)
     &     + mask(i,j)*ah*( (h(ip,j,lm)+h(im,j,lm)-2.0*h(i,j,lm))/dx2 + 
     &            (h(i,jp,lm)+h(i,jm,lm)-2.0*h(i,j,lm))/dy2) +
     &     - (min(h(i,j,lm) , 10.0) - 10.0)* mask(i,j)/(delt*10.0)
     &     - (max(h(i,j,lm) , 400.0) - 400.0)* mask(i,j)/(delt*10.0)
     &     )*delt - rhsub * mask(i,j)


	vort(i,j) = ((v(ip,j,l) - v(im,j,l))/d2x - 
     &  (u(i,jp,l)-u(i,jm,l))/d2y + f(j))/h(i,j,l)
	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
