program main
  use constants_module
  use datastruct_module
  use read_hdfeos_module
  use metadata_module
  implicit none

  integer,parameter                                    :: NMetadataFields = 3
  integer                                              :: status,n_elements,nxtrack,nimages,i,npressurelevels
  integer,dimension(max_nxtrack,max_ntimes)            :: index_array
  character(len=200)                                   :: filename
  type(Tdatastruct),dimension(max_nxtrack,max_ntimes)  :: temp_no2data
  type(Tdatastruct),dimension(:),allocatable           :: selected_data
  type(Tdatastruct),dimension(:,:),allocatable         :: no2data
  type(Tmetadata),dimension(:),allocatable             :: metadata
  real                                                 :: lat_limits(2),lon_limits(2)
  
!  lat_limits  = (/ 45.5 , 48.0 /)
!  lon_limits = (/ 6.0  , 11.0 /)
  lat_limits = (/ 45.5   , 48.0 /)
  lon_limits = (/ -160.0 , 161.0 /)
  
  !filename = '/usr/people/dirksen/programs/fortran/DOMINO_he5_datareader/data.he5'
  filename = '/usr/people/boersma/omi/test_v1.01/hdf5_reader/OMI-Aura_L2-OMDOMINO_2004m1112t1028-o01750_v003-2008m0326t190238.he5'
  call inquire_dimensions(filename,nxtrack,nimages,npressurelevels,.false.,status)
  if ( status /= 0) then
    write(*,'(2a)') 'Error reading dimensions from the file ',trim(filename)
    stop 
  end if
  
  allocate(no2data(nxtrack,nimages))
  call read_omi_tropno2(filename,no2data,nxtrack,nimages,npressurelevels,.false.,status)
  if ( status /= 0 ) then
    write(*,'(2a)') 'Error reading data from the file ',trim(filename)
    stop
  end if

  allocate(metadata(NMetadataFields))
  write(metadata(1)%fieldname,'(a)') 'PGE_version'
  write(metadata(2)%fieldname,'(a)') 'Data_version'
  write(metadata(3)%fieldname,'(a)') 'NO2_L2_file'

  call read_omi_tropno2_metadata(filename,metadata,NMetadataFields,status)
  if ( status /= 0 ) then
    write(*,'(2a)') 'Error reading metadata from the file ',trim(filename)
    stop
  end if

!  write(*,*)
!  do i=1,NMetadataFields ; write(*,'(a,": ",a)') trim(metadata(i)%fieldname),trim(metadata(i)%value) ; end do
!  write(*,*)

!  write(*,*)
!  write(*,'(a,i3)') 'return status of read_omi_tropno2 ',status
!  write(*,*)
  
  call printdata(no2data,nxtrack,nimages,npressurelevels)
 
!  call find_selected_data(no2data,lat_limits,lon_limits,index_array,nxtrack,nimages)
   
!  n_elements = sum(index_array(:,1))
!  write(*,'(a,i)') 'main: n_elements ',n_elements
!  allocate(selected_data(n_elements))   
  
!  call copy_selected_data(no2data,selected_data,index_array) 
  
!  write(*,'(a)') 'array content' 
!  do i=1,n_elements ; write(*,'(i4,2x,f8.3)') i,selected_data(i)%latitude ; end do
  
  if (allocated(selected_data))  deallocate(selected_data)
  if (allocated(no2data))        deallocate(no2data)
  if (allocated(metadata))       deallocate(metadata)
end program main

!*******************************************

subroutine printdata(no2data,nxtrack,ntimes,npressurelevels)
  use datastruct_module
  implicit none
  integer,intent(in)                                      :: nxtrack,ntimes,npressurelevels
  type(Tdatastruct),dimension(nxtrack,ntimes),intent(in)  :: no2data

!   write(*,'(a,f)') 'SolarZenithAngle                           ', no2data(22,555)%SolarZenithAngle
!   write(*,'(a,f)') 'SolarAzimuthAngle                          ', no2data(22,555)%SolarAzimuthAngle
!   write(*,'(a,f)') 'longitude                                  ', no2data(22,555)%longitude                                    
!   write(*,'(a,f)') 'latitude                                   ', no2data(22,555)%latitude
!   write(*,'(a,f)') 'ViewingZenithAngle                         ', no2data(22,555)%ViewingZenithAngle
!   write(*,'(a,f)') 'ViewingAzimuthAngle                        ', no2data(22,555)%ViewingAzimuthAngle                          
!   write(*,'(a,f)') 'TM4SurfacePressure                         ', no2data(22,555)%TM4SurfacePressure
!   write(*,'(a,f)') 'CloudPressure                              ', no2data(22,555)%CloudPressure
!   write(*,'(a,f)') 'CloudPressureStd                           ', no2data(22,555)%CloudPressureStd                             
!   write(*,'(a,f)') 'CloudFraction                              ', no2data(22,555)%CloudFraction
!   write(*,'(a,f)') 'CloudFractionStd                           ', no2data(22,555)%CloudFractionStd
!   write(*,'(a,f)') 'SpacecraftAltitude                         ', no2data(22,555)%SpacecraftAltitude                           
!   write(*,'(a,f)') 'SpacecraftLatitude                         ', no2data(22,555)%SpacecraftLatitude
!   write(*,'(a,f)') 'SpacecraftLongitude                        ', no2data(22,555)%SpacecraftLongitude
!   write(*,'(a,f)') 'GhostColumn                                ', no2data(22,555)%GhostColumn                                  
!   write(*,'(a,f)') 'AirMassFactor                              ', no2data(22,555)%AirMassFactor
!   write(*,'(a,f)') 'AirMassFactorTropospheric                  ', no2data(22,555)%AirMassFactorTropospheric
!   write(*,'(a,f)') 'AirMassFactorGeometric                     ', no2data(22,555)%AirMassFactorGeometric                       
   write(*,'(a,t30,e,t45,f)') 'SurfaceAlbedo',             minval(no2data%SurfaceAlbedo),maxval(no2data%SurfaceAlbedo)
!   write(*,'(a,f)') 'VCDTropErrorUsingAvKernel                  ', no2data(22,555)%VCDTropErrorUsingAvKernel
!   write(*,'(a,f)') 'VCDErrorUsingAvKernel                      ', no2data(22,555)%VCDErrorUsingAvKernel                        
   write(*,'(a,t30,e,t45,f)') 'CloudRadianceFraction',     minval(no2data%CloudRadianceFraction),maxval(no2data%CloudRadianceFraction)
!   write(*,'(a,f)') 'OriginalSlantColumnAmountNO2               ', no2data(22,555)%OriginalSlantColumnAmountNO2
!   write(*,'(a,f)') 'TotalVerticalColumn                        ', no2data(22,555)%TotalVerticalColumn                          
!   write(*,'(a,f)') 'TotalVerticalColumnError                   ', no2data(22,555)%TotalVerticalColumnError
  write(*,'(a,t30,e,t45,f)') 'TroposphericVerticalColumn', minval(no2data%TroposphericVerticalColumn),maxval(no2data%TroposphericVerticalColumn)
!   write(*,'(a,f)') 'TroposphericVerticalColumnError            ', no2data(22,555)%TroposphericVerticalColumnError              
!   write(*,'(a,f)') 'AssimilatedStratosphericSlantColumn        ', no2data(22,555)%AssStratSlantColumn
!   write(*,'(a,f)') 'AssimilatedStratosphericVerticalColumn     ', no2data(22,555)%AssStratVerticalColumn
!   write(*,'(a,f)') 'AssimilatedStratosphericVerticalColumnError', no2data(22,555)%AssStratVerticalColumnError  
!   write(*,'(a,e)') 'SlantColumnAmountNO2                       ', no2data(22,555)%SlantColumnAmountNO2
!   write(*,'(a,e)') 'SlantColumnAmountNO2Std                    ', no2data(22,555)%SlantColumnAmountNO2Std
!   write(*,'(a,f)') 'TroposphericVerticalColumnModel            ', no2data(22,555)%TroposphericVerticalColumnModel              
!   write(*,'(a,4f)')'LatitudeCornerpoints                       ', no2data(22,555)%LatitudeCornerpoints(:)
!   write(*,'(a,4f)')'LongitudeCornerpoints                      ', no2data(22,555)%LongitudeCornerpoints(:)
!   write(*,'(a,e)') 'TAI Time                                   ', no2data(22,555)%TAITime
!   write(*,'(a,i)') 'human readable Date                        ', no2data(22,555)%hdate  
!   write(*,'(a,i)') 'human readable Time                        ', no2data(22,555)%hTime
  write(*,'(a,t30,i6,t40,i6)')'InstrumentConfigurationId', minval(no2data%InstrumentConfigurationId),maxval(no2data%InstrumentConfigurationId)
  write(*,'(a,t30,i6,t40,i6)')'GroundPixelQualityFlags',   minval(no2data%GroundPixelQualityFlags),maxval(no2data%GroundPixelQualityFlags)
  write(*,'(a,t30,i6,t40,i6)')'MeasurementQualityFlags',   minval(no2data%MeasurementQualityFlags),maxval(no2data%MeasurementQualityFlags)
  write(*,'(a,t30,i6,t40,i6)')'TM4TropoPauseLevel',        minval(no2data%TM4TropoPauseLevel),     maxval(no2data%TM4TropoPauseLevel)
  write(*,'(a,t30,i6,t40,i6)')'TM4TerrainHeight',          minval(no2data%TM4TerrainHeight),       maxval(no2data%TM4TerrainHeight)
  write(*,'(a,t30,i6,t40,i6)')'TroposphericColumnFlag',    minval(no2data%TroposphericColumnFlag), maxval(no2data%TroposphericColumnFlag)
  write(*,'(a,t30,i6,t40,i6)') 'TerrainHeight',            minval(no2data%TerrainHeight), maxval(no2data%TerrainHeight)
  write(*,'(a,t30,f,t45,f)') 'AveragingKernel', minval(no2data(22,555)%averagingkernel),maxval(no2data(22,555)%averagingkernel)  
!  write(*,'(f)') no2data(22,555)%averagingkernel(:)
end subroutine printdata
