MOM6
MOM_get_input.F90
1 !> \brief Reads the only Fortran name list needed to boot-strap the model.
2 !!
3 !! The name list parameters indicate which directories to use for
4 !! certain types of input and output, and which files to look in for
5 !! the full parsable input parameter file(s).
7 
8 ! This file is part of MOM6. See LICENSE.md for the license.
9 
10 use mom_error_handler, only : mom_mesg, mom_error, fatal, warning, is_root_pe
11 use mom_file_parser, only : open_param_file, param_file_type
12 use mom_io, only : file_exists, close_file, slasher, ensembler
13 use mom_io, only : open_namelist_file, check_nml_error
14 
15 implicit none ; private
16 
17 public get_mom_input
18 
19 !> Container for paths and parameter file names.
20 type, public :: directories
21  character(len=240) :: &
22  restart_input_dir = ' ',& !< The directory to read restart and input files.
23  restart_output_dir = ' ',&!< The directory into which to write restart files.
24  output_directory = ' ' !< The directory to use to write the model output.
25  character(len=2048) :: &
26  input_filename = ' ' !< A string that indicates the input files or how
27  !! the run segment should be started.
28 end type directories
29 
30 contains
31 
32 !> Get the names of the I/O directories and initialization file.
33 !! Also calls the subroutine that opens run-time parameter files.
34 subroutine get_mom_input(param_file, dirs, check_params, default_input_filename, ensemble_num)
35  type(param_file_type), optional, intent(out) :: param_file !< A structure to parse for run-time parameters.
36  type(directories), optional, intent(out) :: dirs !< Container for paths and parameter file names.
37  logical, optional, intent(in) :: check_params !< If present and False will stop error checking for
38  !! run-time parameters.
39  character(len=*), optional, intent(in) :: default_input_filename !< If present, is the value assumed for
40  !! input_filename if input_filename is not listed
41  !! in the namelist MOM_input_nml.
42  integer, optional, intent(in) :: ensemble_num !< The ensemble id of the current member
43  ! Local variables
44  integer, parameter :: npf = 5 ! Maximum number of parameter files
45 
46  character(len=240) :: &
47  parameter_filename(npf), & ! List of files containing parameters.
48  output_directory, & ! Directory to use to write the model output.
49  restart_input_dir, & ! Directory for reading restart and input files.
50  restart_output_dir ! Directory into which to write restart files.
51  character(len=2048) :: &
52  input_filename ! A string that indicates the input files or how
53  ! the run segment should be started.
54  character(len=240) :: output_dir
55  integer :: unit, io, ierr, valid_param_files
56 
57  namelist /mom_input_nml/ output_directory, input_filename, parameter_filename, &
58  restart_input_dir, restart_output_dir
59 
60  ! Default values in case parameter is not set in file input.nml
61  parameter_filename(:) = ' '
62  output_directory = ' '
63  restart_input_dir = ' '
64  restart_output_dir = ' '
65  input_filename = ' '
66  if (present(default_input_filename)) input_filename = trim(default_input_filename)
67 
68  ! Open namelist
69  if (file_exists('input.nml')) then
70  unit = open_namelist_file(file='input.nml')
71  else
72  call mom_error(fatal,'Required namelist file input.nml does not exist.')
73  endif
74 
75  ! Read namelist parameters
76  ierr=1 ; do while (ierr /= 0)
77  read(unit, nml=mom_input_nml, iostat=io, end=10)
78  ierr = check_nml_error(io, 'MOM_input_nml')
79  enddo
80 10 call close_file(unit)
81 
82  ! Store parameters in container
83  if (present(dirs)) then
84  if (present(ensemble_num)) then
85  dirs%output_directory = slasher(ensembler(output_directory,ensemble_num))
86  dirs%restart_output_dir = slasher(ensembler(restart_output_dir,ensemble_num))
87  dirs%restart_input_dir = slasher(ensembler(restart_input_dir,ensemble_num))
88  dirs%input_filename = ensembler(input_filename,ensemble_num)
89  else
90  dirs%output_directory = slasher(ensembler(output_directory))
91  dirs%restart_output_dir = slasher(ensembler(restart_output_dir))
92  dirs%restart_input_dir = slasher(ensembler(restart_input_dir))
93  dirs%input_filename = ensembler(input_filename)
94  endif
95  endif
96 
97  ! Open run-time parameter file(s)
98  if (present(param_file)) then
99  output_dir = slasher(ensembler(output_directory))
100  valid_param_files = 0
101  do io = 1, npf
102  if (len_trim(trim(parameter_filename(io))) > 0) then
103  if (present(ensemble_num)) then
104  call open_param_file(ensembler(parameter_filename(io),ensemble_num), param_file, &
105  check_params, doc_file_dir=output_dir)
106  else
107  call open_param_file(ensembler(parameter_filename(io)), param_file, &
108  check_params, doc_file_dir=output_dir)
109  endif
110  valid_param_files = valid_param_files + 1
111  endif
112  enddo
113  if (valid_param_files == 0) call mom_error(fatal, "There must be at "//&
114  "least 1 valid entry in input_filename in MOM_input_nml in input.nml.")
115  endif
116 
117 end subroutine get_mom_input
118 
119 end module mom_get_input
Reads the only Fortran name list needed to boot-strap the model.
A structure that can be parsed to read and document run-time parameters.
This module contains I/O framework code.
Definition: MOM_io.F90:2
The MOM6 facility to parse input files for runtime parameters.
Routines for error handling and I/O management.
Indicate whether a file exists, perhaps with domain decomposition.
Definition: MOM_io.F90:68
Container for paths and parameter file names.