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_atm(data_file,
     &     Nlay,z,pres,temp,Tground,Tspace)
      implicit none
      include 'max.inc'
      include 'param.inc'
      include 'formats.inc'
c
c     Purpose: to read the atmospheric temperature profile
c
c     Input:
c       + data_file: input data file
c
c     Output:
c       + Nlay: number of atmospheric layers
c       + z: altitude at the top of each layer (m)
c       + pres: pressure at the top of each layer (Pa)
c       + temp: temperature at the top of each layer (K)
c       + Tground: temperature of the ground
c       + Tspace: temperature of space
c       
c     I/O
      character*(Nchar_mx) data_file
      integer Nlay
      double precision z(0:Nlay_mx)
      double precision pres(0:Nlay_mx)
      double precision temp(0:Nlay_mx)
      double precision Tground
      double precision Tspace
c     temp
      integer Nlev
      integer ios
      integer lay
      double precision x_h2o(0:Nlay_mx)
      double precision T(0:Nlay_mx+1)
c     label
      integer strlen
      character*(Nchar_mx) label
      label='subroutine read_atm'

      write(*,*) 'Opening file: ',trim(data_file)
      open(10,file=data_file(1:strlen(data_file)),
     &     status='old',iostat=ios)
      if (ios.ne.0) then        ! file not found
         call error(label)
         write(*,*) 'File could not be found:'
         write(*,*) data_file(1:strlen(data_file))
         stop
      else
c     Read the data
         read(10,*) Nlev
         Nlay=Nlev-1
         do lay=0,Nlay
            read(10,48) z(lay), ! m
     &           pres(lay), ! Pa
     &           temp(lay), ! K
     &           x_h2o(lay),
     &           T(lay)
         enddo                  ! lay
         Tground=T(0)
         Tspace=3.0D+0 ! temperature of space is set to 3K
c     Consistency check
         if (Nlay.lt.1) then
            call error(label)
            write(*,*) 'Nlay=',Nlay
            write(*,*) 'is < 1'
            stop
         endif
         if (Nlay.gt.Nlay_mx) then
            call error(label)
            write(*,*) 'Nlay=',Nlay
            write(*,*) 'is > Nlay_mx=',Nlay_mx
            stop
         endif
         if (z(0).ne.0.0D+0) then
            call error(label)
            write(*,*) 'z(0)=',z(0)
            write(*,*) 'is not = 0'
            stop
         endif
         do lay=1,Nlay
            if (z(lay).le.z(lay-1)) then
               call error(label)
               write(*,*) 'z(',lay,')=',z(lay)
               write(*,*) 'is < z(',lay+1,')=',z(lay+1)
               stop
            endif
         enddo ! lay
      endif
      close(10)

c     For SW: set very low temperatures
      Tground=0.0D+0
      do lay=0,Nlay
         temp(lay)=0.0D+0
      enddo                     ! lay
      Tspace=T_sun      

      return
      end
