	program writedata
	integer, parameter :: NST=64, NYR=1, NMN=12
	integer, parameter :: syear=2012
	integer :: ist, iyr, imn, idy, itest
	integer :: NDY, YR
	real, dimension(NST) :: lon, lat
	character ( len = 12 ), dimension(NST) :: stname
	character ( len = 8), dimension(12) :: mon = (/'JAN','FEB','MAR','APR','MAY','JUN','JUL','AUS','SEP','OCT','NOV','DEC'/) 
	real, dimension(NST,NYR,NMN,31) :: regdata
	! -------------------------------------------

	! read Station name
	open(1,file="station_name.txt",status="old")
	do ist=1,NST
	   read(1,*) stname(ist), lon(ist), lat(ist)
	enddo
	close (1)

	! read Grads data
	regdata=-99
	open(3,file="KQ.txt",status="old")
	do ist=1,NST
	   YR=syear
	   do iyr=1,NYR
	      do imn=1,NMN
	         NDY=31
	         if ((imn==4).or.(imn==6).or.(imn==9).or.(imn==11)) then
	            NDY=30
	         endif
	         if (imn==2) then 
	            NDY=28
	            itest=YR/4
	            itest=itest*4
	            if (itest == YR) then
	               NDY=29
	               itest=YR/100
	               itest=itest*100
	               if (itest == YR) then
	                  NDY=28
	                  itest=YR/400
	                  itest=itest*400
	                  if (itest == YR) then
	                     NDY=29
	                  endif
	               endif
	            endif
	         endif
	         if ((YR==2010).and.(imn==12)) then
	            NDY=30
	         endif
	         do idy=1,NDY
	            read(3,*) regdata(ist,iyr,imn,idy)
	            !print*, ist,YR,imn,idy
	         enddo
	      enddo
	      YR=YR+1
	   enddo
	enddo
	close(3)
	
	open(6,file="OUTPUT.txt")
	do ist=1,NST
	   write(6,61) stname(ist),lon(ist),lat(ist)
	   YR=syear
	   do iyr=1,NYR
	      write(6,62) YR, (mon(imn),imn=1,NMN)
	      do idy=1,31
	         write(6,63) idy, (regdata(ist,iyr,imn,idy),imn=1,NMN)
	      enddo
	      YR=YR+1
	   enddo
	enddo
	close (6)
	61 format(1x,A14,F8.3,2X,F8.3)
	62 format(1x,I6,3X,12(A8,2X))
	63 format(1x,I4,2X,12(F8.3,2X))
	end
