program ghirafile_all
integer :: NST=7,NHT=2,NDATA=4
integer :: ntime(NHT)
character (len=10), dimension(2) :: hientg=(/'KKL','TC'/)
character (len=10), dimension(4) :: tendata=(/'OBS','CMORPH','GSMaP','TRMM'/)
character (len=50) :: vung='V123', fil
real,dimension(NHT,90000,NST,NDATA) :: dat
integer :: it, ist, iht
! ==== START
Do iht=1,NHT
  fil=trim(hientg(iht))//".txt"
  open(1,file=fil,status="old")
    read(1,*) ntime(iht)
    Do it=1,ntime(iht)
    read(1,*) ((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)
      Do ist=1,NST
      write(2,*), dat(iht,it,ist,idata)
      Enddo
    Enddo 
  close(2)
  Enddo
! write for Excel - Scater plot
  open(3,file="4excell_all_"//trim(fil),status="unknown")
  Do it=1,ntime(iht)
     Do ist=1,NST
      write(3,9), (dat(iht,it,ist,idata),idata=1,NDATA)
9     FORMAT(6(1x,F9.3))
     Enddo
  Enddo
  close(3)
Enddo
End
