c     Copyright (C)
c     2010-2013 Université Bordeaux 1
c     2010-2013 Ecole des Mines d'Albi
c     2014-2018 - Mesostar
c     
c     This program is free software; you can redistribute it and/or modify
c     it under the terms of the GNU General Public License as published by
c     the Free Software Foundation; either version 2, or (at your option)
c     any later version.

c     This program is distributed in the hope that it will be useful,
c     but WITHOUT ANY WARRANTY; without even the implied warranty of
c     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
c     GNU General Public License for more details.

c     You should have received a copy of the GNU General Public License
c     along with this program; if not, write to the Free Software
c     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
c     USA.
c
      subroutine read_opt_prop_file(infile,Nw,Nlev,Nlay,
     &     Tsurf,Tspace,pressure,
     &     temperature,height,x_h2o_nominal,x_h2o_tab,
     &     lw_emissivity,sw_emissivity,
     &     Nb_lw,nu_lo_lw,nu_hi_lw,Ng_lw,w_lw,
     &     Nb_sw,nu_lo_sw,nu_hi_sw,Ng_sw,w_sw,
     &     ka_lw_nominal,ka_lw_tab,
     &     ka_sw_nominal,ka_sw_tab,
     &     ks_sw_nominal,ks_sw_tab)
      implicit none
      include 'max.inc'
      include 'formats.inc'
c     
c     Purpose: to read the optical properties file for the atmosphere
c     
c     Input:
c     + infile: file to generate
c     
c     Output:
c     + Nw: number of tabulation points of water vapor concentration
c     + Nlev: number of atmospheric levels
c     + Nlay: number of atmospheric layers
c     + Tsurf: temperature of the ground [K]
c     + Tspace: temperature of the ground [K]
c     + pressure: pressure at each level [Pa]
c     + temperature: temperature at each level [K]
c     + height: altitude at each level [m]
c     + x_h2o_nominal: molar fraction of H2O at each level for the nominal profile
c     + x_h2o_tab: molar fraction of H2O at each level for each tabulation point
c     + lw_emissivity: LW emissivity
c     + sw_emissivity: SW emissivity
c     + Nb_lw: number of LW spectral intervals
c     + nu_lo_lw: lower wavenumber limit of each LW interval [cm^-1]
c     + nu_hi_lw: upper wavenumber limit of each LW interval [cm^-1]
c     + Ng_lw: number of quadrature points in each LW interval
c     + w_lw: quadrature weights for each LW interval
c     + Nb_sw: number of SW spectral intervals
c     + nu_lo_sw: lower wavenumber limit of each SW interval [cm^-1]
c     + nu_hi_sw: upper wavenumber limit of each SW interval [cm^-1]
c     + Ng_sw: number of quadrature points in each SW interval
c     + w_sw: quadrature weights for each SW interval
c     + ka_lw_nominal: LW absorption coefficient [m^-1] for the nominal profile
c     + ka_lw_tab: LW absorption coefficient [m^-1] for each tabulation point
c     + ka_sw_nominal: SW absorption coefficient [m^-1] for the nominal profile
c     + ka_sw_tab: SW absorption coefficient [m^-1] for each tabulation point
c     + ks_sw_nominal: SW scattering coefficient [m^-1] for the nominal profile
c     + ks_sw_tab: SW scattering coefficient [m^-1] for each tabulation point
c     
c     I/O
      character*(Nchar_mx) infile
      integer Nw,Nlev,Nlay
      double precision pressure(1:Nlev_mx)
      double precision Tsurf,Tspace
      double precision temperature(1:Nlev_mx)
      double precision height(1:Nlev_mx)
      double precision x_h2o_nominal(1:Nlay_mx)
      double precision x_h2o_tab(1:Nlay_mx,1:Nw_mx)
      double precision lw_emissivity,sw_emissivity
      double precision ka_lw_nominal(1:Ngpoints_lw_mx,1:Nlev_mx)
      double precision ka_lw_tab(1:Ngpoints_lw_mx,1:Nlev_mx,1:Nw_mx)
      double precision ka_sw_nominal(1:Ngpoints_sw_mx,1:Nlev_mx)
      double precision ka_sw_tab(1:Ngpoints_sw_mx,1:Nlev_mx,1:Nw_mx)
      double precision ks_sw_nominal(1:Ngpoints_sw_mx,1:Nlev_mx)
      double precision ks_sw_tab(1:Ngpoints_sw_mx,1:Nlev_mx,1:Nw_mx)
      integer Nb_lw
      double precision nu_lo_lw(1:16)
      double precision nu_hi_lw(1:16)
      integer Ng_lw(1:16)
      double precision w_lw(1:16,1:20)
      integer Nb_sw
      double precision nu_lo_sw(1:14)
      double precision nu_hi_sw(1:14)
      integer Ng_sw(1:14)
      double precision w_sw(1:14,1:20)
c     temp
      logical file_ex
      integer ilay,ilev,ib,ig,i,iw
c     label
      character*(Nchar_mx) label
      label='subroutine read_opt_prop_file'

      inquire(file=trim(infile), exist=file_ex)
      if (file_ex) then
         write(*,*) 'Reading file: ',trim(infile)
      else
         call error(label)
         write(*,*) 'File not found:'
         write(*,*) trim(infile)
         stop
      endif
      
      open(11,file=trim(infile))
      read(11,*)
      read(11,*) Nlev
      read(11,*)
      read(11,*) Nlay
      read(11,*)
      read(11,*) Tsurf
      Tspace=3.0D+0             ! K
      read(11,*)
      do ilev=1,Nlev
         read(11,*) pressure(ilev)
      enddo                     ! ilev
      read(11,*)
      do ilev=1,Nlev
         read(11,*) temperature(ilev)
      enddo                     ! ilev
      read(11,*)
      do ilev=1,Nlev
         read(11,*) height(ilev)
      enddo                     ! ilev
      read(11,*)
      do ilay=1,Nlay
         read(11,*) x_h2o_nominal(ilay)
      enddo                     ! ilay
      read(11,*)
      read(11,*) Nw
      do iw=1,Nw
         read(11,*)
         do ilay=1,Nlay
            read(11,*) x_h2o_tab(ilay,iw)
         enddo                  ! ilay
      enddo                     ! iw
      read(11,*)
      read(11,*) lw_emissivity
      read(11,*)
      read(11,*) sw_emissivity
      read(11,*)
      read(11,*) Nb_lw
      read(11,*)
      read(11,*) Nb_sw
      do ib=1,Nb_lw
         read(11,*)
         read(11,*)
         read(11,*) nu_lo_lw(ib)
         read(11,*)
         read(11,*) nu_hi_lw(ib)
         read(11,*)
         read(11,*) Ng_lw(ib)
         read(11,*)
         do ig=1,Ng_lw(ib)
            read(11,*) w_lw(ib,ig)
         enddo                  ! ig
      enddo                     ! ib
c     SW: per interval
      do ib=1,Nb_sw
         read(11,*)
         read(11,*)
         read(11,*) nu_lo_sw(ib)
         read(11,*)
         read(11,*) nu_hi_sw(ib)
         read(11,*)
         read(11,*) Ng_sw(ib)
         read(11,*)
         do ig=1,Ng_sw(ib)
            read(11,*) w_sw(ib,ig)
         enddo                  ! ig
      enddo                     ! ib
c
c     LW: per interval, per g-point
      i=0
      do ib=1,Nb_lw
         do ig=1,Ng_lw(ib)
            i=i+1
            read(11,*)
            do ilay=1,Nlay
               read(11,*) ka_lw_nominal(i,ilay)
            enddo               ! ilay
            do iw=1,Nw
               read(11,*)
               do ilay=1,Nlay
                  read(11,*) ka_lw_tab(i,ilay,iw)
               enddo            ! ilay
            enddo               ! iw
         enddo                  ! ig
      enddo                     ! ib
c     
c     SW: per interval, per g-point
c     absorption coefficient
      i=0
      do ib=1,Nb_sw
         do ig=1,Ng_sw(ib)
            i=i+1
            read(11,*)
            do ilay=1,Nlay
               read(11,*) ka_sw_nominal(i,ilay)
            enddo               ! ilay
            do iw=1,Nw
               read(11,*)
               do ilay=1,Nlay
                  read(11,*) ka_sw_tab(i,ilay,iw)
               enddo            ! ilay
            enddo               ! iw
         enddo                  ! ig
      enddo                     ! ib
c     
c     scattering coefficient
      i=0
      do ib=1,Nb_sw
         do ig=1,Ng_sw(ib)
            i=i+1
            read(11,*)
            do ilay=1,Nlay
               read(11,*) ks_sw_nominal(i,ilay)
            enddo               ! ilay
            do iw=1,Nw
               read(11,*)
               do ilay=1,Nlay
                  read(11,*) ks_sw_tab(i,ilay,iw)
               enddo            ! ilay
            enddo ! iw
         enddo                  ! ig
      enddo                     ! ib
      close(11)
      
      return
      end
      
