C Writing a station data set in GrADS format
C by Ooi See Hai on 30 January 2009
C
      character*8 stid
C
      open (10, file='d:\stnplot\aug2003.txt')
      open (20, file='d:\stnplot\aug2003.dat',form='unformatted')
C
      undef=-999.0
      d2r=3.141592654/180.0
C
C If flag is zero, there are no surface variables following the header.
C
      iflag = 0
C
C Read and write
C
      read(10,*)
C
      do 30 n=1,20
C
         read(10,40) iyear,imonth,iday,ihr,stid,rlat,rlon,rt,rp,iwd,rws
  40     format(i4,4x,i2,6x,i2,6x,i2,6x,a8,4f8.1,i3,5x,f8.1)
C
         if (iflag .eq. 0) then
	     iflag = 1
	     iyrold = iyear
	     imnold = imonth
	     idyold = iday
	     ihrold = ihr
	   endif
C
C If new time group, write time group terminator. Number of data groups
C (nlev) is set to zero. Assuming no empty time groups. 
C
         if (iyrold .ne. iyear .or. imnold .ne. imonth .or. idyold .ne.
     &     iday .or. ihrold .ne. ihr) then
	     nlev = 0
	     write(20) stid,rlat,rlon,tim,nlev,nflag
	   endif
           iyrold = iyear
	     imnold = imonth
	     idyold = iday
	     ihrold = ihr
C
C Write this report.Report time is set to zero for exactly on the hour.
C
           tim = 0.0
	     nlev = 1
	     nflag = 1
           write(20) stid,rlat,rlon,tim,nlev,nflag
C
           if (rt .eq. 888.8) then
	       rt = undef
	     endif
C
           if (rp .eq. 8888.8) then
	       rp = undef
	     endif
C
           if (iwd .eq. 888) then
	       iwd = undef
	       rws = undef
	       ru = undef
	       rv = undef
	     else
	       ru = -rws*sin(iwd*d2r)
	       rv = -rws*cos(iwd*d2r)
	     endif
	     write(20) rt,rp,ru,rv
C
  30   continue
C
C On end of file write last time group terminator.
C
       nlev = 0
	 write(20) stid,rlat,rlon,tim,nlev,nflag
	 stop
	 end