program ghirafile_all
integer :: NST=28,NHT=4,NDATA=6
integer :: ntime(NHT)
character (len=10), dimension(4) :: hientg=(/'KKL','TC','ITCZ','RAT'/)
character (len=10), dimension(6) :: tendata=(/'OBS','CMORPH','GSMaP','PERSIANN','TRMM','VnGP'/)
character (len=50) :: vung='V123', fil
real,dimension(NHT,90000,NST,NDATA) :: dat
integer :: it, ist, iht
! ==== START
Do iht=1,NHT
 fil=trim(vung)//"_"//trim(hientg(iht))//".txt"
 open(1,file=fil,status="old")
 read(1,*) ntime(iht)
  Do it=1,ntime(iht)
  read(1,*) un,un,un,un, ((dat(iht,it,ist,idata),ist=1,NST),idata=1,NDATA)
!(CMO(iht,it,ist),ist=1,NST),(GSM(iht,it,ist),ist=1,NST),(PER(iht,it,ist),ist=1,NST),(TRMM(iht,it,ist),ist=1,NST),(VnGP(iht,it,ist),ist=1,NST)
  enddo
 close(1)
! ==== write cacs tram: 9-12,16-18,20-21,24
  Do idata=1,NDATA
  open(2,file="ALL_"//trim(tendata(idata))//trim(fil),status="unknown")
  Do it=1,ntime(iht)
  write(2,*), dat(iht,it,9,idata)
  write(2,*), dat(iht,it,10,idata)
  write(2,*), dat(iht,it,11,idata)
  write(2,*), dat(iht,it,12,idata)
  write(2,*), dat(iht,it,16,idata)
  write(2,*), dat(iht,it,17,idata)
  write(2,*), dat(iht,it,18,idata)
  write(2,*), dat(iht,it,20,idata)
  write(2,*), dat(iht,it,21,idata)
  write(2,*), dat(iht,it,24,idata)
  Enddo
  close(2)
  Enddo
! write for Excel - Scater plot
  open(3,file="4excell_all_"//trim(fil),status="unknown")
  Do it=1,ntime(iht)
  write(3,9), (dat(iht,it,9,idata),idata=1,NDATA)
  write(3,9), (dat(iht,it,10,idata),idata=1,NDATA)
  write(3,9), (dat(iht,it,11,idata),idata=1,NDATA)
  write(3,9), (dat(iht,it,12,idata),idata=1,NDATA)
  write(3,9), (dat(iht,it,16,idata),idata=1,NDATA)
  write(3,9), (dat(iht,it,17,idata),idata=1,NDATA)
  write(3,9), (dat(iht,it,18,idata),idata=1,NDATA)
  write(3,9), (dat(iht,it,20,idata),idata=1,NDATA)
  write(3,9), (dat(iht,it,21,idata),idata=1,NDATA)
  write(3,9), (dat(iht,it,24,idata),idata=1,NDATA)
9    FORMAT(6(1x,F9.3))
  Enddo
  close(3)
Enddo
End
