implicit none

 integer, parameter :: ch=8, hour_in_day = 24
 integer, parameter :: num=50000
 integer, parameter :: rain_ch = 7
 real :: eps
 real, parameter :: miss = -9999, miss_eps = 1.0

 real :: data(num, ch),  x
 integer :: year, month, day, hour, minute, second
 integer :: acum_date, datanum
 real   :: fhour(num), dt_hour
 character :: date*20, head1*1024, head2*1024, head3*1024, head4*1024
 character :: file*1024

 integer :: i, j, k, l
 integer :: start_year, start_month, start_day
 integer :: end_year, end_month, end_day
 integer :: avr_hour,   n_in_day, num_day
 real   :: maxhour, minhour

 real :: data_avr(ch)
 integer :: data_num(ch)


 file = 'AWS1_TBL110.dat'
 dt_hour = 10.0/60.0    !  DT (hour) in data file
 eps = 0.1*dt_hour

 start_year = 2007; start_month =7; start_day = 20
 end_year   = 2008; end_month  = 6; end_day = 31

 avr_hour = 1

 if( mod(hour_in_day, avr_hour) /= 0 ) then
   write(*,*) 'avr_hour should be able to measure of 24'
   stop
 else
   n_in_day = hour_in_day/avr_hour
 end if

  call datedif( end_year, end_month, end_day, &
  	start_year, start_month, start_day, num_day )


 i = 1
 open(unit=10, file=file, status='old', err=999)


 read(10, *) head1
 read(10, *) head2
 read(10, *) head3
 read(10, *) head4


 do i=1, num
 read(10, *, end=900, err=900) date, (data(i, j), j=1, ch)
!    write(*,*) date, ':', (data(i, j), j=1, ch)
 read(date, "(I4,1x,I2,1x,I2,1x,I2,1x,I2,1x,I2)") &
 	year, month, day, hour, minute, second

 call datedif( year, month, day, &
 		start_year, start_month, start_day, acum_date )
 fhour(i) = acum_date*hour_in_day + hour + (minute+second/60.0)/60.0 


! write(*,*) year, month, day, hour, minute, second 
! write(*,*) acum_date, fhour(i), ':', (data(i, j), j=1, ch)
 if( i == 1 ) then
   write(0,*) 'data from', year, month, day, hour, minute, second
 end if

 end do
 900 close(10)
   write(0,*) '     to  ', year, month, day, hour, minute, second
  datanum = i-1


! AVERAGING
!   write(*,*)  '"year"',',', '"month"',',', '"day"',',', '"from hour"',',', &
!   	'"end hour"',',', '"count"',',', '"WindSpeed(m/s)"',',', &
!	'"Wind Dir(deg.)"',',', '"Temp(deg.)"',',','"RH(%)"',',', &
!	'"Solar(W/m2)"',',', '"Rain(mm)"',',', '"Press.(hPa)"'
   write(*,*)  '"year"',',', '"month"',',', '"day"',',', '"from hour"',',', &
   	'"end hour"',',', '"WindSpeed(m/s)"',',', &
	'"Wind Dir(deg.)"',',', '"Temp(deg.)"',',','"RH(%)"',',', &
	'"Solar(W/m2)"',',', '"Rain(mm)"',',', '"Press.(hPa)"'






 do i=1, num_day
  do j=1, n_in_day
   maxhour = j*avr_hour + hour_in_day*i + eps
   minhour = (j-1)*avr_hour + hour_in_day*i + dt_hour - eps

   call dateadd( start_year, start_month, start_day, i, year, month, day)



!!!!     fhour <= maxhour && fhour-dt >= minhour 
!!!!       count number and sammention   --> AVR

   do l=1, ch
     data_avr(l) = 0
     data_num(l) = 0
   end do

   do k=1, num
    if( fhour(k) <= maxhour .AND. fhour(k) >= minhour ) then
   ! write(0,*) 'IN', year, month, day, maxhour, minhour, fhour(k)
      do l=1, ch
	if( data(k,l) < miss - miss_eps .OR. data(k,l) > miss + miss_eps) then
         data_avr(l) = data_avr(l) + data(k, l)
	 data_num(l) = data_num(l) + 1
	 ! write(0,*) 'OK data', l, data_num(l), data_avr(l)
	end if
      end do
    end if
   end do

   ! write(*, *) 'ALL datanum = ', (data_num(l), l=1,ch)

   do l=1, ch
     ! write(0,*) 'data_num', data_num(l), 'ch=', l
     if(data_num(l) > 0) then
       if(l /= rain_ch) then
         data_avr(l) = data_avr(l)/data_num(l)
       end if
      else
       data_avr(l) = miss
      end if
   end do

       
   write(*,100)  year,month, day, &
   	(j-1)*avr_hour, j*avr_hour,(data_avr(l),l=2,ch)

100 format(I4,',',I02,',',I02,',',I02,',',I02,',',7(F6.1,','))


  end do
 end do
   


 stop
 999 write(*,*) 'Can not open ', file
 stop

end
