! Read data files written from dataset of meteorological and soil data
! of Baltex project in Lindenberg (Germany) 2002-2009.
! Variuos data files contains data about various parameter in soil,
! at the surface and in the surface air layer.
! Program searchs data with the same date and time (date-time coincidence)
! and writes information about this date and time.
!

implicit none

!! 2024, nov.
!integer, parameter :: ndata0=60000, nperiod0=60000, nmin_miss_limit=120, &
! nday_period_min=2

! 2025, gen.
integer, parameter :: ndata0=60000, nperiod0=60000, nmin_miss_limit=720, &
 nday_period_min=30

integer :: unit_inp=21, unit_out=31, ndata_inp
character (len=30) :: file_name_inp="file_inp.txt", file_name_out="file_out.txt"
real, dimension(ndata0) :: datetime=0.
integer*8, dimension(nperiod0) :: istart, ifinish, flag
integer, dimension(ndata0) :: year=0, month, day, hour, minute
integer :: ierr_open=0, ierr_read=0, ifile_inp, nnn, ndyear, i, nperiod, iperiod
character (len=103) :: rline
character (len=1) :: a1
real :: value, nminyear, dtime_limit, dtime, dtime_period

 101 format (i4.4,f9.6,1x,f8.2,5x,i4.4,2i2.2,1x,i2.2,a1,i2.2)

 open (unit_inp, file=file_name_inp, form="formatted", status="old", iostat=ierr_open)

 if (ierr_open /= 0) then
   print *
   print *,"Requested file ",file_name_inp," not found, stop"
   stop
 endif

 read (unit_inp,*) rline

 nnn=0
 ierr_read=0
 do while (ierr_read == 0)
   nnn=nnn+1

   read (unit_inp, 101, iostat=ierr_read) year(nnn),datetime(nnn),value, &
 year(nnn),month(nnn),day(nnn),hour(nnn),a1,minute(nnn)

   if (ierr_read /= 0) exit
 enddo

 ndata_inp=nnn-1

 close (unit_inp)

 nperiod=0
 istart(:)=-999
 ifinish(:)=-999

 nperiod=1
 istart(nperiod)=1

 do i=2,ndata_inp

   ndyear=365
   if (mod(year(i), 4) == 0) then
     ndyear=366
   endif

   nminyear=float(ndyear)*1440.

   dtime_limit=float(nmin_miss_limit)/nminyear

   dtime=datetime(i)-datetime(i-1)

   if (dtime > dtime_limit) then

! Interruption of data period

     ifinish(nperiod)=i-1
     nperiod=nperiod+1
     istart(nperiod)=i

   endif

 enddo

 ifinish(nperiod)=ndata_inp

 print *
 print *,"Total period number ",nperiod

 open (unit_out, file=file_name_out, form="formatted", status="unknown")

 nnn=0
 do iperiod=1,nperiod

   i=istart(iperiod)

   ndyear=365
   if (mod(year(i), 4) == 0) then
     ndyear=366
   endif

   nminyear=float(ndyear)*1440.

   dtime_period=float(nday_period_min)*1440./nminyear

   dtime=datetime(ifinish(iperiod))-datetime(istart(iperiod))

   if (dtime >= dtime_period) then

     nnn=nnn+1

! Continues period is enought long

     write (unit_out, 102) dtime*nminyear/1440., '   ',year(istart(iperiod)),datetime(istart(iperiod)),&
 year(istart(iperiod)),month(istart(iperiod)),day(istart(iperiod)),hour(istart(iperiod)),':',minute(istart(iperiod)), &
 ' - ',year(ifinish(iperiod)),datetime(ifinish(iperiod)), &
 year(ifinish(iperiod)),month(ifinish(iperiod)),day(ifinish(iperiod)),hour(ifinish(iperiod)),':',minute(ifinish(iperiod))

     write (*,*) dtime*nminyear/1440., '   ',year(istart(iperiod)),datetime(istart(iperiod)),&
 year(istart(iperiod)),month(istart(iperiod)),day(istart(iperiod)),hour(istart(iperiod)),':',minute(istart(iperiod)), &
 ' - ',year(ifinish(iperiod)),datetime(ifinish(iperiod)), &
 year(ifinish(iperiod)),month(ifinish(iperiod)),day(ifinish(iperiod)),hour(ifinish(iperiod)),':',minute(ifinish(iperiod))

   endif

 enddo

 102 format (f7.2,2(a,i4.4,f9.6,1x,i4.4,2i2.2,1x,i2.2,a1,i2.2))

 close (unit_out)

 print *
 print *,"Number of useful period ",nnn

stop
end
