      subroutine save4restart(fname,grid,nstart,tstop,icnt,
     & mma,nna,ma,na,mmb1,nnb1,mb1,nb1,mmc1,nnc1,mc1,nc1,
     & mmr,nnr,mr,nr,uar)
      implicit none
      include 'netcdf.inc'
	
      character*(*) fname,grid
c.....grid and array sizes
      integer mmr,nnr
      integer mr,nr
      integer mma,nna,mmb1,nnb1,mmc1,nnc1
      integer ma,na,mb1,nb1,mc1,nc1
c.....restart number
      integer nstart, icnt
c.....snapshot time
      real*8 tstop
c.....data
      real*8 uar(mmr,nnr)
      real*8 uas(mr,nr)
c.....nc stuff
      integer iret, ncid, ipnt_id,dpnt_id,alon_id,alat_id
      integer blon_id, blat_id, clon_id, clat_id
      integer ldims(2),mdims(1)
      integer n_id, t_id, ic_id
      integer ua_id, va_id, qa_id
      integer ub1_id, vb1_id, qb1_id
      integer uc1_id, vc1_id, qc1_id
c.....index
      integer i,j

      save ncid,va_id,qa_id,ub1_id,vb1_id,qb1_id,uc1_id,vc1_id,qc1_id

c.....save data into small arrays
      do i=1,mr
        do j=1,nr
          uas(i,j) = uar(i,j)
        end do
      end do

c.....first time thru, create file and save uar, else save uar
      if (grid.eq.'ua') then
        nstart=nstart+1
c.......enter define mode
        call check_err(nf_create(fname, NF_CLOBBER, ncid))

c.......define dimensions
        call check_err(nf_def_dim(ncid, 'ipoint', 1, ipnt_id))
        call check_err(nf_def_dim(ncid, 'dpoint', 1, dpnt_id))
        call check_err(nf_def_dim(ncid, 'alon', ma, alon_id))
        call check_err(nf_def_dim(ncid, 'alat', na, alat_id))
        call check_err(nf_def_dim(ncid, 'blon', mb1, blon_id))
        call check_err(nf_def_dim(ncid, 'blat', nb1, blat_id))
        call check_err(nf_def_dim(ncid, 'clon', mc1, clon_id))
        call check_err(nf_def_dim(ncid, 'clat', nc1, clat_id))
	
c.......define variables
        mdims(1)=ipnt_id
        call check_err(nf_def_var(ncid,'nstart',NF_INT,1,mdims,n_id))
        call check_err(nf_def_var(ncid,'icnt',NF_INT,1,mdims,ic_id))
        mdims(1)=dpnt_id
        call check_err(nf_def_var(ncid,'tstop',NF_DOUBLE,1,mdims,t_id))

        ldims(1)=alon_id
        ldims(2)=alat_id
        call check_err(nf_def_var(ncid,'ua',NF_DOUBLE,2,ldims,ua_id))
        call check_err(nf_def_var(ncid,'va',NF_DOUBLE,2,ldims,va_id))
        call check_err(nf_def_var(ncid,'qa',NF_DOUBLE,2,ldims,qa_id))
	
        ldims(1)=blon_id
        ldims(2)=blat_id
        call check_err(nf_def_var(ncid,'ub1',NF_DOUBLE,2,ldims,ub1_id))
        call check_err(nf_def_var(ncid,'vb1',NF_DOUBLE,2,ldims,vb1_id))
        call check_err(nf_def_var(ncid,'qb1',NF_DOUBLE,2,ldims,qb1_id))

        ldims(1)=clon_id
        ldims(2)=clat_id
        call check_err(nf_def_var(ncid,'uc1',NF_DOUBLE,2,ldims,uc1_id))
        call check_err(nf_def_var(ncid,'vc1',NF_DOUBLE,2,ldims,vc1_id))
        call check_err(nf_def_var(ncid,'qc1',NF_DOUBLE,2,ldims,qc1_id))
		
c.......leave define mode
        call check_err(nf_enddef(ncid))


c.......store data
        call check_err(nf_put_var_int(ncid, n_id, nstart))
        call check_err(nf_put_var_int(ncid, ic_id, icnt))
        call check_err(nf_put_var_double(ncid, t_id, tstop))
        call check_err(nf_put_var_double(ncid, ua_id, uas))
      else if (grid.eq.'va') then
        call check_err(nf_put_var_double(ncid, va_id, uas))
      else if (grid.eq.'qa') then
        call check_err(nf_put_var_double(ncid, qa_id, uas))
      else if (grid.eq.'ub1') then
        call check_err(nf_put_var_double(ncid, ub1_id, uas))
      else if (grid.eq.'vb1') then
        call check_err(nf_put_var_double(ncid, vb1_id, uas))
      else if (grid.eq.'qb1') then
        call check_err(nf_put_var_double(ncid, qb1_id, uas))
      else if (grid.eq.'uc1') then
        call check_err(nf_put_var_double(ncid, uc1_id, uas))
      else if (grid.eq.'vc1') then
        call check_err(nf_put_var_double(ncid, vc1_id, uas))
      else if (grid.eq.'qc1') then
        call check_err(nf_put_var_double(ncid, qc1_id, uas))
c.......close restart file
        call check_err(nf_close(ncid))
      endif

      return
      end
