      subroutine SurfWrite(prfx,q,d,h1,h2,n1,n2,mm,nn,t,
     &     npics,dax,maxi,gd)
c.....Write surface for Explorer visualization
c.....If prfx = 'm*', writes in cartesian coordinates (for animation
c.....visualization), otherwise in lat-lon.
c.....Modified 17 Oct 2007 CWM: if dax=1, writes maximum value for eta
c.....                          (calls MaxSlab)
      real*8 d(mm,nn),q(mm,nn)
      real*4 maxi(mm,nn)
      integer*4 dax,n1,n2,mm,nn
      real*8 h1(mm),h2(nn),t,grnd,dry,gd,PI
      character*11 sname
      character*11 count
      character*2 prfx
c      common /run/ t,nstep,npics,grnd,dry,fi,fr
c      common /const/ PI,RE,GA
      PI=3.14159265
      count ='0123456789'
      ihr=t/3600.
      ihr10=(t/3600.)/10.
      ihr1=t/3600.-ihr10*10
      imin=t/60.
      imin10=(t/60.-ihr*60)/10.
      imin1=t/60.-ihr*60-imin10*10
      isec10=(t-60*imin)/10.
      isec1=t-60*imin-isec10*10
      sname=prfx//count(ihr10+1:ihr10+1)//count(ihr1+1:ihr1+1)//'h'
     &         //count(imin10+1:imin10+1)//count(imin1+1:imin1+1)//'m'
     &         //count(isec10+1:isec10+1)//count(isec1+1:isec1+1)//'s'
      write (3,*) n, ' steps computed'
      write (3,*) 'Writing surface into file ', sname
      open (unit=1,file=sname,status='unknown',form='formatted')
      ns1=1
      ns2=1
      ne1=n1
      ne2=n2
      istep=npics
      jstep=npics
      fi1=PI/180.*h2((ne2+ns2)/2)
      if (dax.eq.1) then
         call MaxSlab(q,d,maxi,n1,n2,mm,nn,gd)
      endif
      write (1,*) (ne1-ns1)/istep+1,(ne2-ns2)/jstep+1
      do i=ns1,ne1,istep
         if (prfx(1:1).NE.'m') then
	   write (1,*) h1(i)
	 else
	   write (1,*) (h1(i)-h1(ns1))*111320.*cos(fi1)
	 end if
      end do
      do j=ne2,ns2,-jstep
         if (prfx(1:1).NE.'m') then
           write(1,*) h2(j)
	 else
           write(1,*) (h2(j)-h2(ns2))*111320.
	 end if
      end do
          
 666  format (3000F11.4)
      if (dax.eq.1) then
         do j=ne2,ns2,-jstep
            write(1,666) (maxi(i,j),i=ns1,ne1,istep)
         end do
      else
         do j=ne2,ns2,-jstep
            write(1,666) (q(i,j)-d(i,j),i=ns1,ne1,istep)
         end do
      endif
      close(unit=1,status='keep')
      return
      end
