! Module   : specgrid_to_photgrid
! Purpose  : create photgrid files from specgrid files
!
! Copyright 2021-2022 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 specgrid_to_photgrid

   ! Uses

   use forum_m
   use msg_m

   use ISO_FORTRAN_ENV

   ! No implicit typing

   implicit none

   ! Variables

   character(:), allocatable :: specgrid_file_name
   character(:), allocatable :: passband_file_name
   character(:), allocatable :: photgrid_file_name

   type(hdf5io_t)   :: hdf5io
   type(passband_t) :: passband
   type(specgrid_t) :: specgrid
   type(photgrid_t) :: photgrid

   ! Read command-line arguments

   @:ASSERT(n_arg() == 3, 'Syntax: specgrid_to_photgrid specgrid_file_name passband_file_name photgrid_file_name')

   call get_arg(1, specgrid_file_name)
   call get_arg(2, passband_file_name)
   call get_arg(3, photgrid_file_name)
 
   ! Read the passband

   hdf5io = hdf5io_t(passband_file_name, OPEN_FILE_RO)
   call passband%read(hdf5io)
   call hdf5io%final()

   ! Read the specgrid

   hdf5io = hdf5io_t(specgrid_file_name, OPEN_FILE_RO)
   call specgrid%read(hdf5io)
   call hdf5io%final()

   ! Create the photgrid

   call specgrid%filter(passband, photgrid)

   ! Write the photgrid

   hdf5io = hdf5io_t(photgrid_file_name, CREATE_FILE)
   call photgrid%write(hdf5io)
   call hdf5io%final()

   ! Finish

end program specgrid_to_photgrid
