
      subroutine wrgrid(fields,it,plab)
      include 'tigcmproc.h'
      dimension fields(imx,kmx,jmx,nfget),plt(imx,jmx)
      character*(*) plab
      character*56 toplab,proclab
      character*80 msserr,mssfile
c
      lenplab = lenstr(plab)
      if (iwrgrid.gt.0) then
        ifr = 0
        if (iwrgrid.gt.1) write(proclab,"(a)") plab(1:lenplab)
        do ip=1,nftot
          if (ifplt(ip).gt.0) then
            if (iwrgrid.eq.1) then
              call clearstr(proclab)
              write(proclab,"(a,'.',a,'.',3i2.2)") plab(1:lenplab),
     +          labshort(ip)(1:lenstr(labshort(ip))),
     +          (mtimes(i,it),i=1,3)
            endif
            lenproc = lenstr(proclab)
            iwr = 2
            do k=1,kmx
              call getglb(fields,plt,k,0.,ip)
              write(toplab,"('FIELD ',a,' ZP=',f5.1,' MTIME=',3i3)")
     +          labshort(ip),gcmzp(k),(mtimes(i,it),i=1,3)
              ifr = ifr+1
              call wrascii(iwr,luascii,plt,imx,jmx,gcmlon,gcmlat,
     +         'LONGITUDE','LATITUDE',histvol(ivol),labshort(ip),
     +          ifr,toplab(1:lenstr(toplab)),
     +          proclab(1:lenproc),dirascii)
            enddo
            proclab = proclab(1:lenproc)//'.dat'
            lenproc = lenstr(proclab)
            if (iwrgrid.eq.1.or.(iwrgrid.gt.1.and.it.eq.ntms)) then
              if (lenstr(dirascii).gt.0) then
                call rcpfile(luascii,proclab(1:lenproc),dirascii)
              else
                close(luascii)
              endif
              if (lenstr(mssascii).gt.0) then
                lenmss = lenstr(mssascii)
                write(mssfile,"(a,'/',a)") mssascii(1:lenmss),
     +            proclab(1:lenproc)
                lenmss = lenstr(mssfile)
                call mswrite(ier,proclab(1:lenproc),mssfile(1:lenmss),
     +            ' ',367,' ')
                if (ier.ne.0) then
                  write(6,"('>>> error ',i4,' from mswrite of ',
     +              'disk file ',a,' to mss file ',a)")
     +              ier,proclab(1:lenproc),mssfile(1:lenmss)
                  call mserror(msserr)
                  write(6,"('msserror=',a)") msserr
                else
                  write(6,"('Wrote ',a,' to mss file ',a)")
     +              proclab(1:lenproc),mssfile(1:lenmss)
                endif
              endif
            endif
          endif
        enddo
      endif
      return
      end
