      subroutine fgennc(h1,h2,ncfn,nvar,n_restart,ncid,TIME_id,VAR_id,
     &       MAX_id, LON_len,LAT_len,nx,ny,hst,ttl,latimg)
      include 'netcdf.inc'
c.....error status return
      real*8 h1(nx), h2(ny)
      character*(*) ncfn
      integer*4  iret
c.....variable number: 1 - HA, 2 - UA, 3 - VA
      integer nvar
c.....netCDF id
      integer*4  ncid
c.....restart number & number of output timesteps so far
      integer*4 n_restart
c.....dimension ids
      integer*4  LON_dim
      integer*4  LAT_dim
      integer*4  TIME_dim, TS_dim
c.....dimension lengths
      integer*4  LON_len
      integer*4  LAT_len
      integer*4  TIME_len
      parameter (TIME_len = NF_UNLIMITED)
c.....variable ids
      integer*4  LON_id
      integer*4  LAT_id
      integer*4  TIME_id
      integer*4  VAR_id
      integer*4  MAX_id
c.....rank (number of dimensions) for each variable
      integer*4  LON_rank
      integer*4  LAT_rank
      integer*4  TIME_rank
      integer*4  VAR_rank
      parameter (LON_rank = 1)
      parameter (LAT_rank = 1)
      parameter (TIME_rank = 1)
      parameter (VAR_rank = 3)
c.....variable shapes
      integer*4  LON_dims(LON_rank)
      integer*4  LAT_dims(LAT_rank)
      integer*4  TIME_dims(TIME_rank)
      integer*4  TS_dims(2)
      integer*4  VAR_dims(VAR_rank)
      integer*4  MAX_dims(2)
c.....data variables
      real*8  LON(LON_len)
      real*8  LAT(LAT_len)
c.....attribute vectors
      real*4  realval(1)
      real*8  t,grnd,dry,fi,fr
c.....history and title
      character hst*250, hsthold*250, ttl*250
      integer*4 lns
c..... switch when finding image-ordered bathymetry files
      logical latimg

      common /run/ t,nstep,npics,grnd,dry,fi,fr
      common /nc/ ns1,ne1,ns2,ne2,istep,jstep,scl_m2cm
c.....define axis
      ii=1
      do i=1,nx,npics
         lon(ii)=h1(i)
         ii=ii+1
      end do
      jj=1
      do j=1,ny,npics
        if (latimg) then
          lat(jj)=h2(ny-j+1)
        else
          lat(jj)=h2(j)
        endif
         jj=jj+1
      end do
c.....add check for existing output (restart)
      if (n_restart .gt. 0) then
        iret = nf_open(ncfn, NF_WRITE, ncid)
        call check_err(iret)
        iret = nf_inq_varid(ncid, 'TIME', TIME_id)
        if (nvar.EQ.1) then
          iret = nf_inq_varid(ncid, 'HA', VAR_id)
        else if (nvar.eq.2) then
          iret = nf_inq_varid(ncid, 'UA', VAR_id)
        else if (nvar.eq.3) then
          iret = nf_inq_varid(ncid, 'VA', VAR_id)
        endif
        return
      endif
      icnt = 0
      realval(1) = -9.9999998e+33
c.....enter define mode
      iret = nf_create(ncfn, NF_CLOBBER, ncid)
      call check_err(iret)
c.....define dimensions
      iret = nf_def_dim(ncid, 'LON', LON_len, LON_dim)
      call check_err(iret)
      iret = nf_def_dim(ncid, 'LAT', LAT_len, LAT_dim)
      call check_err(iret)
      iret = nf_def_dim(ncid, 'TIME', NF_UNLIMITED, TIME_dim)
      call check_err(iret)
c.....define variables
      LON_dims(1) = LON_dim
      iret = nf_def_var(ncid, 'LON', NF_DOUBLE, LON_rank, LON_dims, 
     &  LON_id)
      call check_err(iret)
      LAT_dims(1) = LAT_dim
      iret = nf_def_var(ncid, 'LAT', NF_DOUBLE, LAT_rank, LAT_dims, 
     &  LAT_id)
      call check_err(iret)
      TIME_dims(1) = TIME_dim
      iret = nf_def_var(ncid, 'TIME', NF_DOUBLE, TIME_rank, TIME_dims, 
     &  TIME_id)
      call check_err(iret)
      VAR_dims(3) = TIME_dim
      VAR_dims(2) = LAT_dim
      VAR_dims(1) = LON_dim
c      write(3,*) 'VAR_dims:',(VAR_dims(i),i=1,VAR_rank)
      if(nvar.EQ.1) then
        iret = nf_def_var(ncid, 'HA', NF_REAL, VAR_rank, VAR_dims,
     &    VAR_id)
        call check_err(iret)
        iret = nf_put_att_text(ncid, VAR_id, 'long_name', 14,
     &   'Wave Amplitude')
        call check_err(iret)
        iret = nf_put_att_text(ncid, VAR_id, 'units', 11, 'CENTIMETERS')
        call check_err(iret)
      else if(nvar.EQ.2) then
        iret = nf_def_var(ncid, 'UA', NF_REAL, VAR_rank, VAR_dims,
     &    VAR_id)
        call check_err(iret)
        iret = nf_put_att_text(ncid, VAR_id, 'units', 18,
     &    'CENTIMETERS/SECOND')
        call check_err(iret)
        iret = nf_put_att_text(ncid, VAR_id, 'long_name', 34,
     &    'Velocity Component along Longitude')
        call check_err(iret)
      else if(nvar.EQ.3) then
        iret = nf_def_var(ncid, 'VA', NF_REAL, VAR_rank, VAR_dims,
     &    VAR_id)
        call check_err(iret)
        iret = nf_put_att_text(ncid, VAR_id, 'units', 18,
     &    'CENTIMETERS/SECOND')
        call check_err(iret)
        iret = nf_put_att_text(ncid, VAR_id, 'long_name', 33,
     &    'Velocity Component along Latitude')
        call check_err(iret)
      endif
c.....assign attributes
      iret = nf_put_att_text(ncid, LON_id, 'units', 12, 'degrees_east')
      call check_err(iret)
c       iret = nf_put_att_text(ncid, LON_id, 'point_spacing', 4, 'even')
c       call check_err(iret)
      iret = nf_put_att_text(ncid, LAT_id, 'units', 13, 'degrees_north')
      call check_err(iret)
c       iret = nf_put_att_text(ncid, LAT_id, 'point_spacing', 4, 'even')
c       call check_err(iret)
      iret = nf_put_att_text(ncid, TIME_id, 'long_name', 4, 'time')
      iret = nf_put_att_text(ncid, TIME_id, 'units', 7, 
     &     'seconds')
      iret = nf_put_att_text(ncid, TIME_id, 'calendar', 9, 'gregorian')
      call check_err(iret)
      iret = nf_put_att_real(ncid, VAR_id, 'missing_value', NF_REAL, 1,
     &   realval)
      call check_err(iret)
      iret = nf_put_att_real(ncid, VAR_id, '_FillValue', NF_REAL, 1, 
     &   realval)
      call check_err(iret)
c      iret = nf_put_att_text(ncid, VAR_id, 'history', 19, 
c     &  'MOST inundation run')
c      call check_err(iret)
      lns = lnblnk(ttl)
      iret = nf_put_att_text(ncid, NF_GLOBAL, 'title', lns, ttl)
      call check_err(iret)
c..... remove svn $ from hst variable
      idollar = index(hst,'$')
      hsthold = hst(1:idollar-1)//hst(idollar+1:lnblnk(hst))
      idollar = index(hsthold,'$')
      hst = hsthold(1:idollar-1)//hsthold(idollar+1:lnblnk(hsthold))
      lns = lnblnk(hst)
      iret = nf_put_att_text(ncid, NF_GLOBAL, 'history', lns, hst)
      call check_err(iret)
c.....leave define mode
      iret = nf_enddef(ncid)
      call check_err(iret)
c.....store LON
      iret = nf_put_var_double(ncid, LON_id, LON)
      call check_err(iret)
c.....store LAT
      iret = nf_put_var_double(ncid, LAT_id, LAT)
      call check_err(iret)
      end
