      program main
      implicit none
!      integer C_flag1,C_flag2,TA1_flag1,TA1_flag2,TA1_flag3,
!     &        Ta1_flag4,P_flag1,P_flag2,P_flag3,P_flag4
!      double precision Year,month,DoY,Hour,seq_day_90,seq_time_90,
!     &        obs_NEE,obs_FCO2,fco2_corr,ustar,nee,Resp_e,gee,
!     &        obs_Ta_27m,Ta_27m_filled,Ta_2_5m,Ta_2_5m_filled,
!     &        PAR_28m,PAR_28m_filled
      double precision daycurrent,gppmax,dataraw(33),datapick(33),
     &    work(50000,33),gppsamp(50),std,fmean
      integer i,j,ntotal,nwindow,nstart,nend
      
      nwindow=7      
      daycurrent=301.0d0
      gppmax=-1.0d+10
      open(unit=1,file='HF_9204_filled')
      read(1,*)
      read(1,*)
      read(1,*)
      read(1,*)
      open(unit=2,file='HF_9204_gppmax')
10    read(1,*,end=100)dataraw
      if(dabs(daycurrent-dataraw(3)).lt.0.01d0)then
        dataraw(13)=dabs(dataraw(13))
        if(dataraw(13).lt.150.0d0)then
          if(dataraw(13).gt.gppmax)then
            gppmax=dataraw(13)
            do i=1,33
              datapick(i)=dataraw(i)
            enddo
          endif
        endif
      else
        if(dabs(gppmax).lt.150.0d0)then
          write(2,310)datapick
        endif
        daycurrent=dataraw(3)
        dataraw(13)=dabs(dataraw(13))
        if(dataraw(13).lt.150.0d0)then
          gppmax=dataraw(13)
          do i=1,33
            datapick(i)=dataraw(i)
          enddo
        else
          gppmax=-1.0d+10
        endif
      endif
      goto 10
100   write(2,310)datapick
      close(1)
      close(2)
      open(unit=1,file='HF_9204_gppmax')
      open(unit=2,file='HF_9204_gppmax_clean')
      i=1
110   read(1,*,end=200)(work(i,j),j=1,33)
      i=i+1
      goto 110
200   close(1)
      ntotal=i-1
      do i=1,ntotal
        nstart=i-nwindow/2
        if(nstart.lt.1)then
          nstart=1
        endif
        nend=i+nwindow/2
        if(nend.gt.ntotal)then
          nend=ntotal
        endif
        if((nend-nstart+1).lt.nwindow)then
          if(nstart.eq.1)then
            nend=nend+nwindow-(nend-nstart+1)
          else
           nstart=nstart-(nwindow-(nend-nstart+1))
          endif
        endif
        fmean=0.0d0
        do j=nstart,nend
          fmean=fmean+work(j,13)
        enddo
        fmean=fmean/dble(nwindow)
        std=0.0d0
        do j=nstart,nend
          std=std+(work(j,13)-fmean)*(work(j,13)-fmean)       
        enddo
        std=dsqrt(std/dble(nwindow-1))
        if((fmean-work(i,13)).lt.std.or.work(i,13).lt.1.0d-8)then
          write(2,310)(work(i,j),j=1,33)
        endif
      enddo
310   format(33f15.7)
      end
