
 subroutine apply_solar_spectrum( &
   ngeomax,       nwave_fine,             nsmax,                 &
   ns_elas,       dnu,                    wavenr_fine,           &
   nwave_aux,     wavenr_aux,             solar_irradiance_aux,  & 
   R_final_fine,  solar_irradiance_fine,  earth_radiance_fine)
 
!-----------------------------------------------------------
! Subroutine:
!
! Purpose:
! 
! Output:
!
!-----------------------------------------------------------

  implicit none

  integer,parameter::dp=selected_real_kind(8) !double precision

  integer,intent(in):: &
    ngeomax,  &
    nwave_fine, &
    nwave_aux, &
    nsmax, &
    ns_elas

  real(kind=dp),intent(in):: &
    wavenr_fine(nwave_fine),             &
    wavenr_aux(nwave_aux),     &
    solar_irradiance_aux(nwave_aux), &
    dnu,              &
    R_final_fine(0:nsmax,nwave_fine,ngeomax), &
    solar_irradiance_fine(nwave_fine)

  integer::iv,n,ns,nsp,naux

  real(kind=dp):: &
    wavenr_fine_ns(0:nsmax), &
    wavenr_fineshift_ns(0:nsmax), &
    wavelength_ns(0:nsmax), &
    fsun_ns(0:nsmax)

  real(kind=dp),intent(out):: &
    earth_radiance_fine(nwave_fine,ngeomax)

  do n=1,nwave_fine

     naux = n+nsmax !points to naux index for each n

     do ns=0,nsmax

        wavenr_fineshift_ns(ns) = dble(-nsmax/2+ns) * dnu
        wavenr_fine_ns(ns) = wavenr_fine(n) + wavenr_fineshift_ns(ns)

!       nsp = -nsmax/2+ns+n !points to ns index for each n
!       fsun_ns(ns) = 0.d0 !zero if outside range
!       if((nsp.ge.1).and.(nsp.le.nwave_fine)) &
!           fsun_ns(ns) = solar_irradiance_fine(nsp)

        nsp = -nsmax/2+ns+naux !points to ns index for each naux
        fsun_ns(ns) = 0.d0 !zero if outside range
        if((nsp.ge.1).and.(nsp.le.nwave_aux)) &
            fsun_ns(ns) = solar_irradiance_aux(nsp)

     enddo

     do iv=1,ngeomax
        earth_radiance_fine(n,iv) = 0.d0
        do ns=0,nsmax
           earth_radiance_fine(n,iv) = &
              earth_radiance_fine(n,iv) + &
                 R_final_fine(ns,n,iv) * fsun_ns(ns) !* dnu
        enddo
     enddo

!    force elastic scattering
!    do iv=1,ngeomax
!       earth_radiance_fine(n,iv) = 0.d0
!       do ns=ns_elas,ns_elas
!          earth_radiance_fine(n,iv) = &
!            earth_radiance_fine(n,iv) + & 
!              R_final_fine(ns,n,iv) * fsun_ns(ns) !* dnu
!       enddo
!    enddo

  enddo

  return
  end subroutine apply_solar_spectrum
