      subroutine write_gridstat5(nlon,nlat,max_cnt,id,ires,
     .     iparm,s_range,satid,iyr,jday,imn,isd,ict,obs_time)
c
c*  Writes gridded output files with asc/des node information
c
c   INPUT:
c      nlat...................number of latitude points
c      nlon...................number of longitude points
c      max_cnt................Maximum number of Obs. for time array
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      iparm
c      s_range
c      satid..................satellite id (int*4)
c      iyr....................year-1900 (int*4)
c      jday...................julian day (int*4)
c      imn(nlon,nlat,node)....TB mean (int*2)
c      isd(nlon,nlat,node)....Standard deviation (int*2)
c      ct(nlon,nlat,node).....counts (int*2)
c      obs_time(nlon,nlat,max_cnt,node)...Observation time grid array
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  CREATION HISTORY:
C                    Darren Jackson CIRES/ETL January 2003
c                       - version 5, adapted from write_gridstat4.f, Jan 2004
c                         gmt has been removed as argument
c
c-----------------------------------------------------------------------------
c
      implicit none

      include 'hirs_grid.inc'

      integer   nlat,nlon,max_cnt
      integer*2 ict(nlon,nlat,node),imn(nlon,nlat,node)
      integer*2 isd(nlon,nlat,node)
      integer   obs_time(nlon,nlat,max_cnt,node)

      integer   irec,id,ires,iyr,jday,itime,blat(4),elat(4)
      integer   i,j,k,ics,s_range(2),iparm, it
      character satid*3,aveid(3)*1
      character*40 mn_file,sd_file,ct_file,ot_file
      character*40 mn_file2,sd_file2,ct_file2,ot_file2
      character    grid_path*13,res(4)*2,csoch(3)*2,parm*3

      data aveid/'D','P','M'/
      data grid_path/'../hirs_grid/'/
      data res/'05','10','20','25'/
      data csoch/'CS','CH','CM'/
      data blat/1,1,1,1/        ! indices set at 90 degrees
      data elat/360,180,90,72/  ! indices set at 90 degrees 
c
c Setting it = 1 places .CS suffix on output file name
c
      it = 1
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 parm
c
      if(iparm .eq. 1) then
         parm = 'T01'
      elseif(iparm .eq. 2) then
         parm = 'T02'
      elseif(iparm .eq. 3) then
         parm = 'T03'
      elseif(iparm .eq. 4) then
         parm = 'T04'
      elseif(iparm .eq. 5) then
         parm = 'T05'
      elseif(iparm .eq. 6) then
         parm = 'T06'
      elseif(iparm .eq. 7) then
         parm = 'T07'
      elseif(iparm .eq. 8) then
         parm = 'T08'
      elseif(iparm .eq. 9) then
         parm = 'T09'
      elseif(iparm .eq. 10) then
         parm = 'T10'
      elseif(iparm .eq. 11) then
         parm = 'T11'
      elseif(iparm .eq. 12) then
         parm = 'T12'
      elseif(iparm .eq. 13) then
         parm = 'T13'
      elseif(iparm .eq. 14) then
         parm = 'T14'
      elseif(iparm .eq. 15) then
         parm = 'T15'
      elseif(iparm .eq. 16) then
         parm = 'T16'
      elseif(iparm .eq. 17) then
         parm = 'T17'
      elseif(iparm .eq. 18) then
         parm = 'T18'
      elseif(iparm .eq. 19) then
         parm = 'T19'
      endif
         
         if(id .eq. 1) then

            write(mn_file,98) 'HIRS.',satid,'.Y',iyr,'.',aveid(id),
     .           itime,'.R',res(ires),'.S',s_range(1),'_',s_range(2),
     .           '.',parm,'.A.MN.',csoch(it)
            write(sd_file,98) 'HIRS.',satid,'.Y',iyr,'.',aveid(id),
     .           itime,'.R',res(ires),'.S',s_range(1),'_',s_range(2),
     .           '.',parm,'.A.SD.',csoch(it)
            write(ct_file,98) 'HIRS.',satid,'.Y',iyr,'.',aveid(id),
     .           itime,'.R',res(ires),'.S',s_range(1),'_',s_range(2),
     .           '.',parm,'.A.CT.',csoch(it)
            write(ot_file,98) 'HIRS.',satid,'.Y',iyr,'.',aveid(id),
     .           itime,'.R',res(ires),'.S',s_range(1),'_',s_range(2),
     .           '.',parm,'.A.OT.',csoch(it)
            write(mn_file2,98) 'HIRS.',satid,'.Y',iyr,'.',aveid(id),
     .           itime,'.R',res(ires),'.S',s_range(1),'_',s_range(2),
     .           '.',parm,'.D.MN.',csoch(it)
            write(sd_file2,98) 'HIRS.',satid,'.Y',iyr,'.',aveid(id),
     .           itime,'.R',res(ires),'.S',s_range(1),'_',s_range(2),
     .           '.',parm,'.D.SD.',csoch(it)
            write(ct_file2,98) 'HIRS.',satid,'.Y',iyr,'.',aveid(id),
     .           itime,'.R',res(ires),'.S',s_range(1),'_',s_range(2),
     .           '.',parm,'.D.CT.',csoch(it)
            write(ot_file2,98) 'HIRS.',satid,'.Y',iyr,'.',aveid(id),
     .           itime,'.R',res(ires),'.S',s_range(1),'_',s_range(2),
     .           '.',parm,'.D.OT.',csoch(it)

 98         format(a5,a3,a2,i2.2,a1,a1,i3.3,a2,a2,a2,i2.2,
     .           a1,i2.2,a1,a3,a6,a2)

         else
	    write(mn_file,99) 'HIRS.',satid,'.Y',iyr,'.',aveid(id),
     .           itime,'.R',res(ires),'.S',s_range(1),'_',s_range(2),
     .           '.',parm,'.A.MN.',csoch(it)
            write(sd_file,99) 'HIRS.',satid,'.Y',iyr,'.',aveid(id),
     .           itime,'.R',res(ires),'.S',s_range(1),'_',s_range(2),
     .           '.',parm,'.A.SD.',csoch(it)
            write(ct_file,99) 'HIRS.',satid,'.Y',iyr,'.',aveid(id),
     .           itime,'.R',res(ires),'.S',s_range(1),'_',s_range(2),
     .           '.',parm,'.A.CT.',csoch(it)
            write(ot_file,99) 'HIRS.',satid,'.Y',iyr,'.',aveid(id),
     .           itime,'.R',res(ires),'.S',s_range(1),'_',s_range(2),
     .           '.',parm,'.A.OT.',csoch(it)
            write(mn_file2,99) 'HIRS.',satid,'.Y',iyr,'.',aveid(id),
     .           itime,'.R',res(ires),'.S',s_range(1),'_',s_range(2),
     .           '.',parm,'.D.MN.',csoch(it)
            write(sd_file2,99) 'HIRS.',satid,'.Y',iyr,'.',aveid(id),
     .           itime,'.R',res(ires),'.S',s_range(1),'_',s_range(2),
     .           '.',parm,'.D.SD.',csoch(it)
            write(ct_file2,99) 'HIRS.',satid,'.Y',iyr,'.',aveid(id),
     .           itime,'.R',res(ires),'.S',s_range(1),'_',s_range(2),
     .           '.',parm,'.D.CT.',csoch(it)
            write(ot_file2,99) 'HIRS.',satid,'.Y',iyr,'.',aveid(id),
     .           itime,'.R',res(ires),'.S',s_range(1),'_',s_range(2),
     .           '.',parm,'.D.OT.',csoch(it)
            
 99         format(a5,a3,a2,i2.2,a1,a1,i2.2,a2,a2,a2,i2.2,
     .           a1,i2.2,a1,a3,a6,a2)

         endif

      irec=1
      open(1,file=grid_path//mn_file,access='direct',
     .     recl=2)
      open(2,file=grid_path//mn_file2,access='direct',
     .     recl=2)
      do j=blat(ires),elat(ires)
         do i=1,nlon
            write(1,rec=irec) imn(i,j,1)
            write(2,rec=irec) imn(i,j,2)
            irec=irec+1   
         enddo
      enddo
      close(1)
      close(2)
      irec=1
      if(iparm .lt. 30) then
         open(1,file=grid_path//sd_file,access='direct',
     .        recl=2)
         open(2,file=grid_path//sd_file2,access='direct',
     .        recl=2)
         do j=blat(ires),elat(ires)
            do i=1,nlon
               write(1,rec=irec) isd(i,j,1)
               write(2,rec=irec) isd(i,j,2)
               irec=irec+1
            enddo
         enddo
         close(1)
         close(2)
      endif
      irec=1
      open(1,file=grid_path//ct_file,access='direct',
     .     recl=2)
      open(2,file=grid_path//ct_file2,access='direct',
     .     recl=2)
      do j=blat(ires),elat(ires)
         do i=1,nlon
            write(1,rec=irec) ict(i,j,1)
            write(2,rec=irec) ict(i,j,2)
            irec=irec+1
         enddo
      enddo
      close(1)
      close(2)
      irec=1
      open(1,file=grid_path//ot_file,access='direct',
     .     recl=4)
      open(2,file=grid_path//ot_file2,access='direct',
     .     recl=4)
      do k=1,max_cnt
         do j=blat(ires),elat(ires)
            do i=1,nlon
               write(1,rec=irec) obs_time(i,j,k,1)
               write(2,rec=irec) obs_time(i,j,k,2)
               irec=irec+1
            enddo
         enddo
      enddo
      close(1)
      close(2)

      return
      end





