      subroutine write_gridstat(nlon,nlat,nchn,id,ires,satid,iyr,jday,
     .     imn,isd,ict,gmt,angle)
c
c*  Writes gridded output files
c
c   INPUT:
c      nlat...................number of latitude points
c      nlon...................number of longitude points
c      nchn...................number of channels (if nchn = 8, assume CM data
c                             if nchn < 8 assume CH data)
c      id.....................desired averaging period (int*4)
c                             1-daily,2-pentad,3-monthly
c      ires...................resolution
c                             1-0.5, 2-1.0, 3-2.0, 4-2.5
c      satid..................satellite id (int*4)
c      iyr....................year-1900 (int*4)
c      jday...................julian day (int*4)
c      imn(nlon,nlat,nchn)....TB mean (int*2)
c      isd(nlon,nlat,nchn)....Standard deviation (int*2)
c      ct(nlon,nlat,nchn).....counts (int*2)
c      gmt....................Time of observation for CH files (int*4)
c      angle..................Angle of observation for CH files (ch*2)
c
c   OUTPUT:
c
c      writes desired grid files.  This program assumes for
c      pentad and monthly mean data that jday is the final day of the
c      specified period.  I have hard coded in the removal of grid boxes 
c      poleward of 70 degrees.
c
c-----------------------------------------------------------------------------
c
      implicit none

      include 'hirs_grid.inc'

      integer   nlat,nlon,nchn
      integer*2 ict(nlon,nlat,nchn),imn(nlon,nlat,nchn)
      integer*2 isd(nlon,nlat,nchn)

      integer   irec,id,ires,iyr,jday,itime,blat(4),elat(4)
      integer   i,j,k,ics,gmt
      character satid*3,aveid(3)*1
      character*35 mn_file,sd_file,ct_file
      character    grid_path*13,res(4)*2,csoch(3)*2,angle*2

      data aveid/'D','P','M'/
      data grid_path/'../hirs_grid/'/
      data res/'05','10','20','25'/
      data csoch/'CS','CH','CM'/
c      data blat/41,21,11,9/  ! indices set at 70 degrees (assumes lat centered on 1.25)
c      data elat/320,160,80,64/ ! indices set at 70 degrees 
c      data blat/41,21,11,9/    ! indices set at 70 degrees (assumes lat centered on EQ)
c      data elat/321,161,81,65/ ! indices set at 70 degrees 
      data blat/1,1,1,1/        ! indices set at 90 degrees
      data elat/360,180,90,72/  ! indices set at 90 degrees 
c
c* Calculate time variable
c
      if(id .eq. 1) then
         itime=jday
      elseif(id .eq. 2) then
         itime=int(jday/5)
      elseif(id .eq. 3) then
         itime=ifix((jday+1)/30.)
      endif
c
c Set output file name to CS file type unless nchn < 10
c
      ics = 1
      if(nchn .eq. 4) ics = 2
      if(nchn .eq. 8) ics = 3
       
      if(id .eq. 1) then
         if(ics .ge. 2) then
            write(mn_file,96) 'HIRS.',satid,'.Y',iyr,'.',aveid(id),
     .           itime,'.R',res(ires),'.',gmt,'Z.A',angle,'.',
     .           csoch(ics),'.MN'
            write(sd_file,96) 'HIRS.',satid,'.Y',iyr,'.',aveid(id),
     .           itime,'.R',res(ires),'.',gmt,'Z.A',angle,'.',
     .           csoch(ics),'.SD'
            write(ct_file,96) 'HIRS.',satid,'.Y',iyr,'.',aveid(id),
     .           itime,'.R',res(ires),'.',gmt,'Z.A',angle,'.',
     .           csoch(ics),'.CT'
         else
            write(mn_file,98) 'HIRS.',satid,'.Y',iyr,'.',aveid(id),
     .           itime,'.R',res(ires),'.',csoch(ics),'.MN'
            write(sd_file,98) 'HIRS.',satid,'.Y',iyr,'.',aveid(id),
     .           itime,'.R',res(ires),'.',csoch(ics),'.SD'
            write(ct_file,98) 'HIRS.',satid,'.Y',iyr,'.',aveid(id),
     .           itime,'.R',res(ires),'.',csoch(ics),'.CT'
         endif

      else
         if(ics .ge. 2) then
            write(mn_file,97) 'HIRS.',satid,'.Y',iyr,'.',aveid(id),
     .           itime,'.R',res(ires),'.',gmt,'Z.A',angle,'.',
     .           csoch(ics),'.MN'
            write(sd_file,97) 'HIRS.',satid,'.Y',iyr,'.',aveid(id),
     .           itime,'.R',res(ires),'.',gmt,'Z.A',angle,'.',
     .           csoch(ics),'.SD'
            write(ct_file,97) 'HIRS.',satid,'.Y',iyr,'.',aveid(id),
     .           itime,'.R',res(ires),'.',gmt,'Z.A',angle,'.',
     .           csoch(ics),'.CT'
         else
            write(mn_file,99) 'HIRS.',satid,'.Y',iyr,'.',aveid(id),
     .           itime,'.R',res(ires),'.',csoch(ics),'.MN'
            write(sd_file,99) 'HIRS.',satid,'.Y',iyr,'.',aveid(id),
     .           itime,'.R',res(ires),'.',csoch(ics),'.SD'
            write(ct_file,99) 'HIRS.',satid,'.Y',iyr,'.',aveid(id),
     .           itime,'.R',res(ires),'.',csoch(ics),'.CT'
         endif
      endif
 96   format(a5,a3,a2,i2.2,a1,a1,i3.3,a2,a2,a1,i2.2,a3,a2,a1,a2,a3)
 97   format(a5,a3,a2,i2.2,a1,a1,i2.2,a2,a2,a1,i2.2,a3,a2,a1,a2,a3)
 98   format(a5,a3,a2,i2.2,a1,a1,i3.3,a2,a2,a1,a2,a3)
 99   format(a5,a3,a2,i2.2,a1,a1,i2.2,a2,a2,a1,a2,a3)

      irec=1
      open(1,file=grid_path//mn_file,access='direct',
     .     recl=2)
      do k=1,nchn
         do j=blat(ires),elat(ires)
            do i=1,nlon
               write(1,rec=irec) imn(i,j,k)
               irec=irec+1
             enddo
         enddo
      enddo
      close(1)
      irec=1
      open(1,file=grid_path//sd_file,access='direct',
     .     recl=2)
      do k=1,nchn
         do j=blat(ires),elat(ires)
            do i=1,nlon
               write(1,rec=irec) isd(i,j,k)
               irec=irec+1
             enddo
         enddo
      enddo
      close(1)
      irec=1
      open(1,file=grid_path//ct_file,access='direct',
     .     recl=2)
      do k=1,nchn
         do j=blat(ires),elat(ires)
            do i=1,nlon
               write(1,rec=irec) ict(i,j,k)
               irec=irec+1
             enddo
         enddo
      enddo
      close(1)

      return
      end





