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 invert_narrowband_order(Nt,Nx,Np,Nb,Nq,
     &     lambda_lo,lambda_hi,sigma)
      implicit none
      include 'max.inc'
c
c     Purpose: to invert the order of the spectral narrowbands when needed (low wavelength first)
c
c     I/O
c       + Nt: number of temperature levels
c       + Nx: number of concentration levels
c       + Np: number of pressure levels
c       + Nb: number of spectral narrowbands
c       + Nq: quadrature order
c       + lambda_lo: lower limit of each narrowband (Âµm)
c       + lambda_hi: higher limit of each narrowband (Âµm)
c       + sigma: absorption cross-section (cm^2/molecule)
c
c     I/O
      integer Nt,Nx,Np,Nb,Nq
      double precision lambda_lo(1:Nb_mx)
      double precision lambda_hi(1:Nb_mx)
      double precision sigma(1:Nt_mx,1:Nx_mx,1:Np_mx,1:Nb_mx,1:Nq_mx)
c     temp
      integer it,ix,ip,band,quad,b
      double precision tlambda_lo(1:Nb_mx)
      double precision tlambda_hi(1:Nb_mx)
      double precision tsigma(1:Nt_mx,1:Nx_mx,1:Np_mx,1:Nb_mx,1:Nq_mx)
c     label
      integer strlen
      character*(Nchar_mx) label
      label='subroutine invert_narrowband_order'

      write(*,*) 'Inverting narrowband order...'

c     1 - copy everything into temporary arrays
      do band=1,Nb
         tlambda_lo(band)=lambda_lo(band)
         tlambda_hi(band)=lambda_hi(band)
         do it=1,Nt
            do ix=1,Nx
               do ip=1,Np
                  do quad=1,Nq
                     tsigma(it,ix,ip,band,quad)=
     &                    sigma(it,ix,ip,band,quad)
                  enddo ! quad
               enddo ! ip
            enddo ! ix
         enddo ! it
      enddo ! band

c     1 - invert narrowband order
      do band=1,Nb
         b=Nb-band+1
         lambda_lo(band)=tlambda_lo(b)
         lambda_hi(band)=tlambda_hi(b)
         do it=1,Nt
            do ix=1,Nx
               do ip=1,Np
                  do quad=1,Nq
                     sigma(it,ix,ip,band,quad)=
     &                    tsigma(it,ix,ip,b,quad)
                  enddo ! quad
               enddo ! ip
            enddo ! ix
         enddo ! it
      enddo ! band

      write(*,*) '...done'

      return
      end
