! Module   : make_photon_passband
! Purpose  : create a photon-counting passband
!
! Copyright 2023 Rich Townsend & The MSG Team
!
! This file is part of MSG. MSG is free software: you can redistribute
! it and/or modify it under the terms of the GNU General Public
! License as published by the Free Software Foundation, version 3.
!
! MSG is distributed in the hope that it will be useful, but WITHOUT
! ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
! or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
! License for more details.
!
! You should have received a copy of the GNU General Public License
! along with this program.  If not, see <http://www.gnu.org/licenses/>.

#:include 'forum.inc'

program make_photon_passband

   ! Uses

   use forum_m
   use msg_m

   use cubint_m

   use ISO_FORTRAN_ENV

   ! No implicit typing

   implicit none

   ! Variables

   real(RD)                  :: lam_min
   real(RD)                  :: lam_max
   character(:), allocatable :: passband_file_name

   type(cubint_t)   :: cubint
   real(RD)         :: Delta
   real(RD)         :: F_0
   type(passband_t) :: passband
   type(hdf5io_t)   :: hdf5io

   ! Read command-line arguments

   @:ASSERT(n_arg() == 3, 'Syntax: make_photon_passband lam_min lam_max passband_file_name}')

   call get_arg(1, lam_min)
   call get_arg(2, lam_max)
   call get_arg(3, passband_file_name)

   ! Create the cubic interpolant (a straight line)

   cubint = cubint_t([lam_min, lam_max], [lam_min, lam_max], [1._RD, 1._RD])

   ! Evaluate the normalizing flux

   Delta = cubint%integ(lam_max) - cubint%integ(lam_min)

   F_0 = H_PLANCK*C_LIGHT*1E8_RD/Delta

   ! Create the passband

   passband = passband_t(cubint, F_0)

   ! Write the passband_t

   hdf5io = hdf5io_t(passband_file_name, CREATE_FILE)
   call passband%write(hdf5io)
   call hdf5io%final()

   ! Finish

end program make_photon_passband
