! First include the set of model-wide compiler flags 
#include "tm5.inc"

module MObsRead

  ! --------------------------------------------------------------------
  !
  ! This module reads TROPOMI NO2 data,
  !
  !                                      Mark ter Linden, KNMI, sep 2015
  !---------------------------------------------------------------------

  use MTObsTrack
  use datetime,     only : date2tau, tau2date
  use TM5IF_module, only : TM5IF_select, TM5IF_obstrack_read
  use TM5IF_module, only : TM5IF_log_error, FILENAME_LEN
  use TM5IF_module, only : TM5IF_date, TM5IF_time_init

  implicit none
  !
  private
  !
  public :: ObsFileGet

  ! Store list of orbit file names here
  character(FILENAME_LEN), dimension(:), allocatable :: filenamelist
  integer :: filenameindex
  integer :: nfiles

  ! Only read list of filenames at start
  logical :: firstcall_mobsread = .true.


contains

  subroutine ObsFileGet( listfilename, itau1, itau2, obsTrack, dataFound)

    !--------------------------------------------------------------
    ! Returns the retrieved level-2 TROPOMI data with mean orbit time between
    ! "itau1" and "itau2" and stores the result in "obsTrack".
    !
    ! input:
    !    listfilename = the file which contains a list of TROPOMI L2 NO2 files
    !    itau1    = time in TM5 seconds, start of model time interval 
    !    itau2    = time in TM5 seconds, end of model time interval 
    ! output:
    !    obsTrack = structure containing the TROPOMI L2 file read by this routine
    !    If no tracks are found, then "dataFound" will be set to .false.
    !
    ! NOTE: the tracks in the HDF file are assumed to be stored in
    ! chronological order. This allows to start checking the vdata's
    ! from the last one that has been read.
    ! --------------------------------------------------------------

    implicit none
        
    character(*), intent(in)       :: listfilename
    integer, intent(in)            :: itau1, itau2
    type(TObsTrack),intent(inout)  :: obsTrack
    logical,intent(out)            :: dataFound

    integer                        :: idt1(6),idt2(6)
    character(FILENAME_LEN)        :: line
    integer                        :: fileunit, ifl
    integer                        :: status, IOstatus
    integer                        :: before_or_after
    type(TM5IF_date)               :: time_start, time_stop
    
    datafound = .false. 
    call tau2date(itau1,idt1) ; call tau2date(itau2,idt2)
    write(*,'(2(a,i4,"/",i2.2,"/",i2.2,x,i2.2,":",i2.2,":",i2.2))') '  ObsFileGet: itau1 ',idt1,' itau2 ',idt2
        
    call TM5IF_time_init(time_start, idt1(1), idt1(2), idt1(3), idt1(4), idt1(5), idt1(6))
    call TM5IF_time_init(time_stop,  idt2(1), idt2(2), idt2(3), idt2(4), idt2(5), idt2(6)) 

    obstrack%count = 0
    obstrack%norbitparts = 0

    ! Read the list with orbit file names at start
    if ( firstcall_mobsread ) then

       fileunit = 13867
       open(unit=fileunit, file=trim(listfilename), iostat=status)
       if ( status /= 0 ) then
          call TM5IF_log_error("Failed to open '" // trim(listfilename) // "'")
          stop
       end if
       nfiles = 0
       do 
          read(fileunit, '(A)', iostat=IOstatus) line
          if ( IOstatus /= 0 ) exit
          nfiles = nfiles + 1
       end do
       close(fileunit);

       print '(a,i5,a)', ' List file contains ',nfiles,' file names'

       ! create storage
       allocate ( filenamelist(nfiles) )

       ! Open once more ..
       open(unit=fileunit, file=trim(listfilename), iostat=status)
       ! .. and read file names
       ifl = 0
       do 
          read(fileunit, '(A)', iostat=IOstatus) line
          if ( IOstatus /= 0 ) exit
          ifl = ifl + 1
          filenamelist(ifl) = line
       end do
       close(fileunit);

       ! This is done only at start of the run
       firstcall_mobsread = .false.
       ! start with inquiry of first file
       filenameindex = 1

    end if

    ! code below is done every time step
    do        
       ! when last file has been checked: leave
       if ( filenameindex > nfiles ) exit
       ! check if file is within the time interval
       print *,' checking ',trim(filenamelist(filenameindex))
       call TM5IF_select ( trim(filenamelist(filenameindex)), time_start, time_stop, obstrack, before_or_after )
       if ( before_or_after >= 0 ) exit
       ! -1 : orbit time before time interval, try next file
       filenameindex = filenameindex + 1
    end do

    ! read only when data is found for this time interval
    if ( obstrack%norbitparts > 0 ) then
       call TM5IF_obstrack_read(obstrack)
       ! this file is done, move to next
       filenameindex = filenameindex + 1
    end if

    dataFound = obstrack%count /= 0

    if ( .not. dataFound ) print *,' ObsFileGet: no data found for this time interval'

  end subroutine ObsFileGet

end module MObsRead
