      subroutine read_std_profile(pidx,
     &     Nlay,Nb,Nq,
     &     lambda_lo,lambda_hi,w_quad,
     &     z,pres,temp,x_h2o,T,ck)
      implicit none
      include 'max.inc'
      include 'constants.inc'
c
c     Purpose: to read a given standard atmospheric profile
c
c     Input:
c       + pidx: profile index [1-5]
c
c     Output:
c       + Nlay: number of atmospheric layers
c       + Nb: number of narrowband intervals
c       + Nq: quadrature order
c       + z: altitude of each interface (0-Nlay) (m)
c       + pres: pressure of each interface (0-Nlay) (Pa)
c       + temp: temperature of each interface (0-Nlay) (K)
c       + x_h2o: water vapor concentration of each interface (0-Nlay) (mol/mol)
c       + T: temperature array (0-Nlay+1)
c         - T(0): ground temperature
c         - T(i): temperature defined for layer index i, i=1,Nlay
c         - T(Nlay+1): temperature of space
c       + lambda_lo: lower limit of each spectral band
c       + lambda_hi: higher limit of each spectral band
c       + w_quad: quadrature weights
c       + ck: k-distribution data set
c
c     I/O
      integer pidx
      integer Nlay,Nb,Nq
      double precision z(0:Nlay_mx)
      double precision pres(0:Nlay_mx)
      double precision temp(0:Nlay_mx)
      double precision x_h2o(0:Nlay_mx)
      double precision T(0:Nlay_mx+1)
      double precision lambda_lo(1:Nb_mx)
      double precision lambda_hi(1:Nb_mx)
      double precision w_quad(1:Nq_mx)
      double precision ck(1:Nlay_mx,1:Nb_mx,1:Nq_mx)
c     temp
      integer i,band,quad,Nlev,Nlev2
      integer ios
      character*(Nchar_mx) atm_file,ck_file
      double precision mm_h2o(0:Nlay_mx)
      double precision x_lev_h2o(1:Nlay_mx)
      double precision n_h2o
      double precision raf,sum_w
c     formats
 21   format(5f11.3)
 22   format(1000f14.6)
c     label
      integer strlen
      character*(Nchar_mx) label
      label='subroutine read_std_profile'

      if (pidx.eq.1) then
         atm_file='./data/atms.dat.i1'
         ck_file='./data/kcoef1.dat'
      else if (pidx.eq.2) then
         atm_file='./data/atms.dat.i2'
         ck_file='./data/kcoef2.dat'
      else if (pidx.eq.3) then
         atm_file='./data/atms.dat.i3'
         ck_file='./data/kcoef3.dat'
      else if (pidx.eq.4) then
         atm_file='./data/atms.dat.i4'
         ck_file='./data/kcoef4.dat'
      else if (pidx.eq.5) then
         atm_file='./data/atms.dat.i5'
         ck_file='./data/kcoef5.dat'
      else
         call error(label)
         write(*,*) 'pidx=',pidx
         write(*,*) 'allowed values: 1-5'
         stop
      endif

c     reading altitude and temperature levels
      open(11,file=atm_file(1:strlen(atm_file)),
     &     status='old',iostat=ios)
      if (ios.ne.0) then        ! file not found
         call error(label)
         write(*,*) 'File could not be found:'
         write(*,*) atm_file(1:strlen(atm_file))
         stop
      endif
      read(11,*) Nlev
      Nlay=Nlev-1
      if (Nlay.gt.Nlay_mx) then
         call error(label)
         write(*,*) 'Nlay=',Nlay
         write(*,*) 'while Nlay_mx=',Nlay_mx
         stop
      endif
      do i=Nlay,0,-1
         read(11,21) z(i),pres(i),temp(i),mm_h2o(i) ! mm_h2o: g/m^3
c     Debug
c         write(*,*) i,z(i)
c     Debug
         z(i)=z(i)*1.0D+3       ! km -> m
         pres(i)=pres(i)*1.0D+2 ! hPa -> Pa
      enddo ! i
      close(11)
c     Temperature
      T(0)=temp(0) ! ground temperature (K)
      do i=1,Nlay
         T(i)=(temp(i-1)+temp(i))/2.0D+0 ! average temperature of layer index i (K)
      enddo
      T(Nlay+1)=3.0D+0 ! space temperature (K)
c     H2O concentration
      do i=0,Nlay
         n_h2o=mm_h2o(i)/M_H2O  ! mol/m³
         x_h2o(i)=n_h2o*Rgp*temp(i)/pres(i) ! Pa/Pa
      enddo ! i

c     reading spectral data
      open(12,file=ck_file(1:strlen(ck_file)),
     &     status='old',iostat=ios)
      if (ios.ne.0) then        ! file not found
         call error(label)
         write(*,*) 'File could not be found:'
         write(*,*) ck_file(1:strlen(ck_file))
         stop
      endif
      read(12,*) Nb,raf,raf,Nq,Nlev2
      if (Nb.gt.Nb_mx) then
         call error(label)
         write(*,*) 'Nb=',Nb
         write(*,*) 'while Nb_mx=',Nb_mx
         stop
      endif
      if (Nq.gt.Nq_mx) then
         call error(label)
         write(*,*) 'Nq=',Nq
         write(*,*) 'while Nq_mx=',Nq_mx
         stop
      endif
      if (Nlev2.ne.Nlev) then
         call error(label)
         write(*,*) 'Nlev=',Nlev
         write(*,*) 'Nlev2=',Nlev2
         stop
      endif
      do band=1,Nb
         read(12,*) raf,lambda_lo(band),lambda_hi(band)
c     Debug
         if (band.gt.1) then
            if (lambda_lo(band).ne.lambda_hi(band-1)) then
               call error(label)
               write(*,*) 'lambda_hi(',band-1,')=',lambda_hi(band-1)
               write(*,*) 'lambda_lo(',band,')=',lambda_lo(band)
               stop
            endif
         endif
c     Debug
         do quad=1,Nq
            read(12,22) (ck(i,band,quad),i=1,Nlev),w_quad(quad)
c     Debug
c            write(*,*) 'w_quad(',quad,')=',w_quad(quad)
c     Debug
         enddo ! quad
      enddo ! band
      close(12)

      sum_w=0.0D+0
      do quad=1,Nq
         sum_w=sum_w+w_quad(quad)
      enddo ! quad
c     Debug
c      write(*,*) 'sum_w=',sum_w
c     Debug
      if (dabs(sum_w-1.0D+0).gt.1.0D-3) then
         call error(label)
         write(*,*) 'sum_w=',sum_w
         stop
      else
         if (sum_w.ne.1.0D+0) then
            do quad=1,Nq
               w_quad(quad)=w_quad(quad)/sum_w
            enddo ! quad
         endif
      endif

      return
      end
