!-------------------------------------------------------------------------------
! Description:
!
!   Read observation data from a file into an obs structure.
!
! Copyright:
!
!   This software was developed within the context of the EUMETSAT Satellite
!   Application Facility on Numerical Weather Prediction (NWP SAF), under the
!   Cooperation Agreement dated 7 September 2021, between EUMETSAT and the
!   Met Office, UK, by one or more partners within the NWP SAF. The partners
!   in the NWP SAF are the Met Office, ECMWF, DWD and MeteoFrance.
!
!   Copyright 2025, EUMETSAT, All Rights Reserved.
!
!-------------------------------------------------------------------------------

subroutine radsim_read_obsdata_text(file_name, nprofs, coefs, obs, obstotal, startob)

use radsim_mod_cfg, only : &
  output_mode, &
  max_profs

use radsim_mod_constants, only : &
  output_debug

use radsim_mod_io, only : &
 maxcols

use radsim_mod_types, only : &
  obs_type

use rttov_types, only : &
  rttov_coefs

implicit none

include 'radsim_alloc_obs.interface'
include 'radsim_read_obsdata_check.interface'

character(len=200), intent(in)    :: file_name
integer,            intent(in)    :: nprofs
type(rttov_coefs),  intent(in)    :: coefs
type(obs_type),     intent(inout) :: obs
integer,            intent(out)   :: obstotal
integer, optional,  intent(in)    :: startob

integer :: i, j
integer, save :: file_version
integer :: nobs
integer, save :: nobs_total
integer :: start
integer, save :: ncols
integer, save :: columns(maxcols)
real :: coldata(maxcols)
character(len=80) :: line
integer :: nobs_read = 0

!-------------------------------------------------------------------------------

if ( nobs_read == 0 ) then

  print '(2a)', 'Reading observation data from ', trim(file_name)

  open(unit=20, file=file_name, form='formatted')

  do
    read(20,'(a)') line
    line = adjustl(line)
    if ( line(1:1) == '!' .or. line == '' ) cycle
    backspace(20)
    exit
  end do

  read(20,*) file_version
  read(20,*) nobs_total
  print '(a,i0)', ' File version = ', file_version

  ncols = 5
  columns = 0
  columns(1:5) = (/ 1,2,3,4,5 /)

  if ( file_version == 2 ) then
    read(20,*) ncols
    columns = 0
    read(20,*) columns(1:ncols)
  end if

  call radsim_read_obsdata_check(columns(1:ncols), nobs_total, nprofs, coefs)

end if

! Allocate obs structure

obstotal = nobs_total

start = 1
if ( present(startob) ) then
  if ( startob > 1 ) start = startob
end if

nobs = min(max_profs, obstotal-start+1)

call radsim_alloc_obs(nobs, columns, obs)

! Skip lines until the first ob is reached

if ( start > nobs_read+1 ) then
  if ( output_mode >= output_debug ) then
    print '(a,i0)', 'Skipping to ob number ', start
  end if
  do i = 1, start-(nobs_read+1)
    read(20,*)
  end do
  nobs_read = start-1
end if

! Read data one line at a time and assign to the appropriate fields

do i = 1, obs % nobs
  read(20,*) coldata(1:ncols)
  do j = 1, ncols
    select case(columns(j))
      case(1)
        obs % lon(i) = coldata(j)
      case(2)
        obs % lat(i) = coldata(j)
      case(3)
        obs % zsurf(i) = coldata(j)
      case(4)
        obs % lsm(i) = coldata(j)
      case(5)
        obs % satzen(i) = coldata(j)
      case(6)
        obs % viewid(i) = nint(coldata(j))
      case(7)
        obs % scanline(i) = nint(coldata(j))
      case(8)
        obs % scanpos(i) = nint(coldata(j))
      case(9)
        obs % satazim(i) = coldata(j)
      case(10)
        obs % year(i) = nint(coldata(j))
      case(11)
        obs % month(i) = nint(coldata(j))
      case(12)
        obs % day(i) = nint(coldata(j))
      case(13)
        obs % hour(i) = nint(coldata(j))
      case(14)
        obs % minute(i) = nint(coldata(j))
      case(15)
        obs % solzen(i) = coldata(j)
      case(16)
        obs % solazim(i) = coldata(j)
      case(17)
        obs % footprint_rmajor(i) = coldata(j)
      case(18)
        obs % footprint_rminor(i) = coldata(j)
      case(19)
        obs % rtsurf(i) = coldata(j)
      case(20)
        obs % be(i) = coldata(j)
      case(21)
        obs % cosbk(i) = coldata(j)
      case default
        cycle
    end select
  end do
end do

nobs_read = nobs_read + obs % nobs

if ( nobs_read == obstotal ) close(20)

end subroutine radsim_read_obsdata_text
