c THIS ROUTINE FILTERS THE ARIES SYSTEM CODE OUTPUT TO REDUCE
c THE NUMBER OF POINTS POST-PROCESSED BY VASST
c It reads each line as a character array, and then converts
c to real numbers using an internal read statement

      PROGRAM MAIN

      character*12 names(64)
      character*14 cparam(84)
      dimension param(84), range(64,2)
      integer datapoint

      open (8, file='postranges.dat')
      open (9, file='SysoutFinalSiC.nan')
      open (3, file='SysoutFinalSiC.reduced', status='replace')

C READ FILTERS FROM FILE 'postranges.dat' (up through COE)

      read(8,*) nphys
      do i=1,64
        read(8,*) k, range(k,1), range(k,2)
        enddo
      icount = 0

C READ ONE LINE AT A TIME and PLACE INTO REAL ARRAY
      do j=1,nphys
        read(9,55) (cparam(k), k=1,84)
        
        do k=1,84
          read(cparam(k),*,END=999) param(k)
          enddo
        
        itest = 1

c main filters
        do i=1,64
        if(param(i).lt.range(i,1) .or. param(i).gt.range(i,2)) itest=0
          enddo

c other (more complex) filters
c        if(param(26).gt.param(27)) itest=0    ! Pcd can't be > Paux
c        pfus = param(12) * param(27)
c        if(pfus.lt.1500.0 .or. pfus.gt.4000.0) itest=0  ! 1500<Pfus<4000

c if all tests pass, then write the line and increment counter
        if(itest.eq.1) then
          write(3,55) (cparam(k), k=1,84)
          icount = icount + 1
        endif
55      format(84a14)

      enddo

999   write(*,*) nphys, " points reduced to ", icount
      write(*,*) " j= ", j
      call exit
      end