! ----------------------------------------------------------------------------------- !
! The Research LINE source (R-LINE) model is in continuous development by various     !
! groups and is based on information from these groups: Federal Government employees, !
! contractors working within a United States Government contract, and non-Federal     !
! sources including research institutions.  These groups give the Government          !
! permission to use, prepare derivative works of, and distribute copies of their work !
! in the R-LINE model to the public and to permit others to do so.  The United States !
! Environmental Protection Agency therefore grants similar permission to use the      !
! R-LINE model, but users are requested to provide copies of derivative works or      !
! products designed to operate in the R-LINE model to the United States Government    !
! without restrictions as to use by others.  Software that is used with the R-LINE    !
! model but distributed under the GNU General Public License or the GNU Lesser        !
! General Public License is subject to their copyright restrictions.                  !
! ----------------------------------------------------------------------------------- !

      subroutine Read_Met_Inputs(SizeSurf)
!-----------------------------------------------------------------------------------------------
!     Written by AV
!     RLINE v1.2, November 2013
!
!     Reads inputs from surface meteorological file
!
!     2025/06 MGD - Parallelization
!
!----------------------------------------------------------------------------

      Use mpi_params
      Use Data_Structures
      Use Line_Source_Data
      Implicit None

      Integer    :: SizeSurf   ! Size of surface file
      Integer    :: AllocateStatus, ReadStatus, Index

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

! ----Calculate size of surface file, only Master I/O
      if (my_rank==0)  then
        Call Compute_File_Size(SizeSurf,InputSurfaceFile)
        SizeSurf=SizeSurf-1
      end if
      
      
      ! Pass SizeSurf to all PROCS
      if (nb_procs /= 1) call mpi_bcast(SizeSurf, 1, MPI_integer, 0, MPI_COMM_WORLD, ierr)
      
      
      Allocate(SurfaceMet(SizeSurf),Stat=AllocateStatus)
      
      
      ! Read InputSurfaceFile, only for MASTER I/O
      if (my_rank==0)  then   
        Open(Unit=10, File=InputSurfaceFile, Status="Old", Action="Read", Iostat=ReadStatus)
        Read(10,*)
        Do Index = 1,SizeSurf   
          Read(10,*)SurfaceMet(Index)
        End Do
      Close(Unit=10)
      end if
      
      
      !Pass all met variables to all PROCS
      if (nb_procs /= 1) then      
         index=1
         do while (index.LE.SizeSurf)           
            call mpi_bcast(SurfaceMet(Index)%Year,  1, MPI_integer, 0, MPI_COMM_WORLD, ierr)
            call mpi_bcast(SurfaceMet(Index)%Month,  1, MPI_integer, 0, MPI_COMM_WORLD, ierr)
            call mpi_bcast(SurfaceMet(Index)%Day,  1, MPI_integer, 0, MPI_COMM_WORLD, ierr)
            call mpi_bcast(SurfaceMet(Index)%J_Day,  1, MPI_integer, 0, MPI_COMM_WORLD, ierr)
            call mpi_bcast(SurfaceMet(Index)%Hour,  1, MPI_integer, 0, MPI_COMM_WORLD, ierr)
            call mpi_bcast(SurfaceMet(Index)%Hs,  1, MPI_DOUBLE_PRECISION, 0, MPI_COMM_WORLD, ierr)
            call mpi_bcast(SurfaceMet(Index)%Ustar,  1, MPI_DOUBLE_PRECISION, 0, MPI_COMM_WORLD, ierr)
            call mpi_bcast(SurfaceMet(Index)%Wstar,  1, MPI_DOUBLE_PRECISION, 0, MPI_COMM_WORLD, ierr)
            call mpi_bcast(SurfaceMet(Index)%VPTG,  1, MPI_DOUBLE_PRECISION, 0, MPI_COMM_WORLD, ierr)
            call mpi_bcast(SurfaceMet(Index)%CBL,  1, MPI_DOUBLE_PRECISION, 0, MPI_COMM_WORLD, ierr)
            call mpi_bcast(SurfaceMet(Index)%SBL,  1, MPI_DOUBLE_PRECISION, 0, MPI_COMM_WORLD, ierr)
            call mpi_bcast(SurfaceMet(Index)%Lmon,  1, MPI_DOUBLE_PRECISION, 0, MPI_COMM_WORLD, ierr)
            call mpi_bcast(SurfaceMet(Index)%z0,  1, MPI_DOUBLE_PRECISION, 0, MPI_COMM_WORLD, ierr)
            call mpi_bcast(SurfaceMet(Index)%Bo,  1, MPI_DOUBLE_PRECISION, 0, MPI_COMM_WORLD, ierr)
            call mpi_bcast(SurfaceMet(Index)%Alb,  1, MPI_DOUBLE_PRECISION, 0, MPI_COMM_WORLD, ierr)
            call mpi_bcast(SurfaceMet(Index)%Ws,  1, MPI_DOUBLE_PRECISION, 0, MPI_COMM_WORLD, ierr)
            call mpi_bcast(SurfaceMet(Index)%Wd,  1, MPI_DOUBLE_PRECISION, 0, MPI_COMM_WORLD, ierr)
            call mpi_bcast(SurfaceMet(Index)%zref,  1, MPI_DOUBLE_PRECISION, 0, MPI_COMM_WORLD, ierr)
            call mpi_bcast(SurfaceMet(Index)%Temp,  1, MPI_DOUBLE_PRECISION, 0, MPI_COMM_WORLD, ierr)
            call mpi_bcast(SurfaceMet(Index)%ztemp,  1, MPI_DOUBLE_PRECISION, 0, MPI_COMM_WORLD, ierr)             
            index=index+1 
         end do
      end if

      End Subroutine
