
           Program pluviototal

c          this program calculates preciprate and precitable

           Integer i,j,nx,ny
           Parameter(nx=143000) 
           Real day(nx),p3(nx)
           Real p4(nx),p4a(nx)
           Integer jday(nx),hr(nx),mn(nx)
           Integer iyear(nx)

           Character*1 ff
           Character*3 mon
           Character*2 af
           

           open(10,file='apu27_pluvio400_202304_precip.impacts2023',
     *     status='unknown')
        open(20,file='apu27_pluvio400_202304_preciprate.impacts2023',
     *     status='unknown')
        open(22,file='apu27_pluvio400_202304_preciptable.impacts2023',
     *     status='unknown')


           total=0.
           rainmax=0.
           ikk=0
           raint=0.
           rtotal=0.
           revent=0.
           tot1=0.
           tot2=0.
           ff=':'

c          precip event is defined by 60-min precip free period

           timegap=60./(60.*24.)

            
           k1=1
           rtot=0.
           do i=1,nx
           read(10,30,err=999,end=999)iyear(k1),jday(k1),
     *     hr(k1),mn(k1),p1,p2,p3(k1),p4(k1)
 30        format(4i5,4f7.2)
           print*,k1
           time=float(hr(k1))+float(mn(k1))/60.
           day(k1)=float(jday(k1))+time/24.
           if(k1.gt.1)p4a(k1)=p4(k1)-p4(k1-1)
           if(p4a(k1).lt.0)p4a(k1)=p4(k1)
           if(p3(k1).gt.0.)then
           p4rate=p4a(k1)*60.
           p3rate=p3(k1)*60.

c          write out fourth precip measurement

           write(20,20,err=999)iyear(k1),jday(k1),hr(k1),mn(k1),p3(k1),
     *     p3rate
  20       format(4i5,2f10.2)
           endif
           k1=k1+1
 999       enddo
           k1=k1-1
           print*,k1
           
           ik=1

c          total is 3rd and rtotal is 4th precip measurement

           jsave=jday(1)
           jhr=hr(1)
           jmn=mn(1)
           total=p3(1)
           rtotal=p4a(1) 
           do i=2,k1
           tday=day(i-1)+timegap
           if(day(i) .le. tday .and. day(i).gt.day(i-1))then
           total=total + p3(i)
           rtotal=rtotal + p4a(i)
           ik=ik+1

c          write out 3rd and 4th precip measurement

           if(i.eq.k1)then
           write(22,22)iyear(i),jsave,jhr,ff,jmn,jday(i),hr(i),ff,mn(i),
     *     ik,total,rtotal
           ikk=ikk+ik
           revent=revent+1.
           raint=raint+total
           endif
           else
           if(total.gt.0.)then

c          write out 3rd and 4th precip measurement

           write(22,22)iyear(i),jsave,jhr,ff,jmn,jday(i-1),hr(i-1),ff,
     *     mn(i-1),ik,total,rtotal
           endif    
           ikk=ikk+ik
           revent=revent+1.
           raint=raint+total
 22        format(2i5,i5.2,a1,i2.2,i5,i5.2,a1,i2.2,i5,2f8.2)
           ik=1
           total=p3(i)
           rtotal=p4a(i)
           jsave=jday(i)
           jhr=hr(i)
           jmn=mn(i)

c          write out 3rd and 4th precip measurement

           if(i.eq.k1)then
           write(22,22)iyear(i),jsave,jhr,ff,jmn,jday(i),hr(i),ff,mn(i),
     *     ik,total,rtotal
           ikk=ikk+ik
           revent=revent+1.
           raint=raint+total
           endif
           endif
           enddo

           stop
           end


