! Module  : fmsg_m
! Purpose : Fortran public interface for MSG
!
! 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'

module fmsg_m

   ! Uses

   use forum_m
   use msg_m

   use ISO_FORTRAN_ENV

   ! No implicit typing

   implicit none

   ! Access specifiers

   private

   public :: LABEL_LEN
   public :: STAT_OK
   public :: STAT_OUT_OF_BOUNDS_AXIS_LO
   public :: STAT_OUT_OF_BOUNDS_AXIS_HI
   public :: STAT_OUT_OF_BOUNDS_LAM_LO
   public :: STAT_OUT_OF_BOUNDS_LAM_HI
   public :: STAT_OUT_OF_BOUNDS_MU_LO
   public :: STAT_OUT_OF_BOUNDS_MU_HI
   public :: STAT_UNAVAILABLE_DATA
   public :: STAT_INVALID_ARGUMENT
   public :: STAT_FILE_NOT_FOUND
   public :: STAT_INVALID_FILE_TYPE
   public :: STAT_INVALID_GROUP_TYPE
   public :: STAT_INVALID_GROUP_REVISION
   public :: axis_t
   public :: photgrid_t
   public :: specgrid_t
   public :: get_version
   public :: load_specgrid
   public :: load_photgrid
   public :: load_photgrid_from_specgrid

contains

   subroutine load_specgrid(specgrid_file_name, specgrid, stat)

      character(*), intent(in)       :: specgrid_file_name
      type(specgrid_t), intent(out)  :: specgrid
      integer, intent(out), optional :: stat

      type(hdf5io_t) :: hdf5io

      ! Load the specgrid from file

      call open_file(specgrid_file_name, hdf5io, stat)
      if (PRESENT(stat)) then
         if (stat /= STAT_OK) return
      endif

      call specgrid%read(hdf5io, stat)
      call hdf5io%final()
      if (PRESENT(stat)) then
         if (stat /= STAT_OK) return
      endif

      ! Finish

      return

   end subroutine load_specgrid

   !****

   subroutine load_photgrid(photgrid_file_name, photgrid, stat)

      character(*), intent(in)       :: photgrid_file_name
      type(photgrid_t), intent(out)  :: photgrid
      integer, intent(out), optional :: stat

      type(hdf5io_t) :: hdf5io

      ! Load the photgrid from file

      call open_file(photgrid_file_name, hdf5io, stat)
      if (PRESENT(stat)) then
         if (stat /= STAT_OK) return
      endif

      call photgrid%read(hdf5io, stat)
      call hdf5io%final()

      ! Finish

      return

   end subroutine load_photgrid

   !****
      
   subroutine load_photgrid_from_specgrid(specgrid_file_name, passband_file_name, &
                                          photgrid, stat)

      character(*), intent(in)       :: specgrid_file_name
      character(*), intent(in)       :: passband_file_name
      type(photgrid_t), intent(out)  :: photgrid
      integer, intent(out), optional :: stat

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

      ! Load the photgrid_t from specgrid_t and passband_t files

      call open_file(passband_file_name, hdf5io, stat)
      if (PRESENT(stat)) then
         if (stat /= STAT_OK) return
      endif

      call passband%read(hdf5io, stat)
      call hdf5io%final()
      if (PRESENT(stat)) then
         if (stat /= STAT_OK) return
      endif

      call load_specgrid(specgrid_file_name, specgrid, stat)
      if (PRESENT(stat)) then
         if (stat /= STAT_OK) return
      endif

      call specgrid%set_cache_limit(0)

      call specgrid%filter(passband, photgrid, stat)

      ! Finish

      return

   end subroutine load_photgrid_from_specgrid
         
end module fmsg_m
