! 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

integer, parameter :: ndata0=200000

integer, dimension(2) :: unit_inp=(/21, 22/), ndata_inp
integer :: unit_out=31, ndata_out
character (len=30), dimension(2) :: file_name_inp=(/"file_inp1.txt", "file_inp2.txt"/)
character (len=30) :: file_name_out="file_out.txt"
real, dimension(ndata0,3) :: datetime=0.
integer*8, dimension(3) :: idatetime=0
integer, dimension(ndata0,3) :: year=0, month, day, hour, minute
integer :: ierr_open=0, ierr_read=0, ifile_inp, nnn, i1, i2, i3 
character (len=103) :: rline
character (len=1) :: a1
real :: value, value_std=999.

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

 do ifile_inp=1,2

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

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

   read (unit_inp(ifile_inp),*) rline

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

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

     if (ierr_read /= 0) exit
   enddo

   ndata_inp(ifile_inp)=nnn-1

   close (unit_inp(ifile_inp))

 enddo

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

 write (unit_out,*) rline

 ndata_out=0

 do i1=1,ndata_inp(1)
   do i2=1,ndata_inp(2)

     idatetime(1)=year(i1,1)*1000000+nint(datetime(i1,1)*1000000)
     idatetime(2)=year(i2,2)*1000000+nint(datetime(i2,2)*1000000)

     if (idatetime(1) == idatetime(2)) then

! Coincidence of date and time for the data in 2 input data files
     
       if (ndata_out == 0 ) then

         write (unit_out, 101) year(i1,1),datetime(i1,1),value_std, &
 year(i1,1),month(i1,1),day(i1,1),hour(i1,1),':',minute(i1,1)
         ndata_out=ndata_out+1
         year(ndata_out,3)=year(i1,1)
         datetime(ndata_out,3)=datetime(i1,1)

       else

         do i3=1,ndata_out

           idatetime(3)=year(i3,3)*1000000+nint(datetime(i3,3)*1000000)

           if (idatetime(3) /= idatetime(1)) then

             write (unit_out, 101) year(i1,1),datetime(i1,1),value_std, &
 year(i1,1),month(i1,1),day(i1,1),hour(i1,1),':',minute(i1,1)
             flush (unit_out)
             ndata_out=ndata_out+1
             year(ndata_out,3)=year(i1,1)
             datetime(ndata_out,3)=datetime(i1,1)
             exit ! loop i3
           endif

         enddo

       endif ! ndata_out

       exit ! loop i2

     endif

   enddo
 enddo

 close (unit_out)

stop
end
