      subroutine writerecs(ncid,TIME_id,VAR_id,nvar,T_stp,
     & VAR,q,u,v,d,LON_len,LAT_len,nx,ny,tsi,tsj,numts,tsid,latimg)

c.....netCDF id
      integer*4  ncid
c.....variable ids
      integer*4  TIME_id
      integer*4  VAR_id
      integer*4  T_stp
       
      include 'netcdf.inc'
c.....error status return
      integer*4  iret
c.....variable number: 1 - HA, 2 - UA, 3 - VA
      integer nvar
c.....netCDF dimension sizes for dimensions used with record variables
      integer*4  LON_len,nx
      integer*4  LAT_len,ny
       
c.....rank (number of dimensions) for each variable
      integer*4  TIME_rank
      integer*4  VAR_rank
      parameter (TIME_rank = 1)
      parameter (VAR_rank = 3)
c.....starts and counts for array sections of record variables
      integer*4  TIME_start(TIME_rank), TIME_count(TIME_rank)
      data TIME_count /1/
      integer*4  VAR_start(VAR_rank), VAR_count(VAR_rank)
      data VAR_start /1, 1, 1/
      
c..... timeseries
      integer*4 numts, tsid
      integer*4 tsi(numts), tsj(numts)
      real*4 ts(1),tst(numts,1)
      integer*4 TS_start(2),TS_count(2)
      data TS_start/1, 1/
c.....data variables
       
      integer*4  TIME_nr
      parameter (TIME_nr = 1)
      double precision  TIME(TIME_nr)
       
      integer*4  VAR_nr
      parameter (VAR_nr = 1)
      real*4 VAR(LON_len, LAT_len, VAR_nr)
      real*8 Q(nx, ny)
      real*8 U(nx, ny)
      real*8 V(nx, ny)
      real*8 D(nx, ny),t,grnd,dry,fi,fr
c..... switch when finding image-ordered bathymetry files
      logical latimg
      integer latstart,latstep

      common /run/ t,nstep,npics,grnd,dry,fi,fr
      common /nc/ ns1,ne1,ns2,ne2,istep,jstep,scl_m2cm
      
c      write(3,*) ' npics(wr): ', npics
      if (latimg) then
        latstart=LAT_len
        latstep= -1
      else
        latstart=1
        latstep=1
      endif


      jj=latstart
      do j=1,ny,npics
c        write(3,*) 'jj: ', jj
c        call flush(3)
        ii=1
        do i=1,nx,npics
          if (ii.LE.LON_len.AND.jj.GE.1.AND.jj.LE.LAT_len) then
            if (q(i,j).GE.dry) then
              if(nvar.EQ.1) then
                VAR(ii,jj,1)=(q(i,j)-d(i,j))*scl_m2cm
              else if(nvar.EQ.2) then
                VAR(ii,jj,1)=u(i,j)*scl_m2cm
              else if(nvar.EQ.3) then
                VAR(ii,jj,1)=v(i,j)*scl_m2cm
              endif
              ii=ii+1
            else
              VAR(ii,jj,1)=-9.9999998e+33
              ii=ii+1
            end if
          end if
        end do
        jj=jj+latstep
      end do
c.....store TIME
      TIME_start(1) = T_stp
      TIME_count(1) = TIME_nr
      TIME(1)=t
c      write(3,*) ' writerecs, t:',T_stp,TIME_start,TIME_count,TIME(1)
      iret = nf_put_vara_double(ncid,TIME_id,TIME_start,TIME_count,TIME)
      call check_err(iret)
c.....timeseries
!      TS_start(2) = T_stp
!      TS_count(1) = numts
!      TS_count(2) = 1
!      if (numts.gt.0) then
!        do k=1,numts
!          tst(k,1)=(q(tsi(k),tsj(k))-d(tsi(k),tsj(k)))*scl_m2cm
!        end do
!        iret = nf_put_vara_real(ncid,tsid,TS_start,TS_count,tst)
!        call check_err(iret)
!      endif
c.....store VAR
      VAR_start(3) = T_stp
      VAR_count(1) = LON_len
      VAR_count(2) = LAT_len
      VAR_count(3) = VAR_nr
c      write(3,*) ' store VAR:',ncid,VAR_id,VAR_start,VAR_count
c      write(3,*) ' VAR_start:',(VAR_start(i),i=1,VAR_rank)
c      write(3,*) ' VAR_count:',(VAR_count(i),i=1,VAR_rank)
      iret = nf_put_vara_real(ncid,VAR_id,VAR_start,VAR_count,VAR)
      call check_err(iret)
      iret =  nf_sync(ncid)
      call check_err(iret)
      end
