program main
  ! --------------------------------------------------------------------
  !
  ! This program calls a subroutine OmiReadTrack to read OMI NO2 and
  ! cloud data from HDF4 file "no2trackyyymmdd.hdf".
  !
  !   Updated for HDF4 datafiles for coll3 v1.0.1 DOMINO data
  !
  !   Folkert Boersma, KNMI, 25 April 2008
  !
  !---------------------------------------------------------------------

  use NO2OmiRead, only : OmiNO2Open, OmiNO2Close
  use NO2OmiRead, only : OmiReadTrack, TOmiNO2Track

  implicit none

  character(len=80)  :: file_name, file_out
  type(TOmiNO2Track) :: omiNO2Track
 
  integer :: i, cntorbit, cntmatch

  file_name = '/nobackup/users/boersma/omi/coll3/2004/11/no2track20041112.hdf'
       
  ! Open the HDF file
  call OmiNO2Open( file_name )

  ! loop over tracks
  do
    
    ! Process one orbit of DOMINO data  
    call OmiReadTrack(omiNO2Track)

    if ( omiNO2Track%count == 0 ) exit
      
    cntorbit = cntorbit + 1                 

    cntmatch = 0
                              
    do i=1,omiNO2Track%count
       
       ! Select pixels in whatever lon-lat range
       ! Select unflagged pixels only
       if ( omiNO2Track%longitude(i) >  3.0 .and. &
            omiNO2Track%longitude(i) <  6.0 .and. &
            omiNO2Track%latitude(i)  >  50.0 .and. &
            omiNO2Track%latitude(i)  <  54.0 .and. &
            omiNO2Track%fltrop(i)    == 0    &
              ) then           
          
          cntmatch = cntmatch + 1

          write(*,'(i4,2i2.2,x,3i2.2,2f7.2,f8.3,i7)') &
             omiNO2Track%year(i),omiNO2Track%month(i), &
             omiNO2Track%day(i),omiNO2Track%hour(i), &
             omiNO2Track%minute(i),omiNO2Track%second(i), &
             omiNO2Track%latitude(i),omiNO2Track%longitude(i), &
             omiNO2Track%no2vcdtrop(i),omiNO2Track%gpxflag(i)
          
       end if

    end do
    
    write(*,'(a,i5)') ' Main: Number of pixels inside region = ',cntmatch

  end do

  ! Close the HDF file
  call OmiNO2Close

end program main
