MOM6
MOM_get_input.F90
Go to the documentation of this file.
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
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  input_filename = ' ' !< A string that indicates the input files or how
26  !! the run segment should be started.
27 end type directories
28 
29 contains
30 
31 !> Get the names of the I/O directories and initialization file.
32 !! Also calls the subroutine that opens run-time parameter files.
33 subroutine get_mom_input(param_file, dirs, check_params, default_input_filename, ensemble_num)
34  type(param_file_type), optional, intent(out) :: param_file !< A structure to parse for run-time parameters.
35  type(directories), optional, intent(out) :: dirs !< Container for paths and parameter file names.
36  logical, optional, intent(in) :: check_params !< If present and False will stop error checking for
37  !! run-time parameters.
38  character(len=*), optional, intent(in) :: default_input_filename !< If present, is the value assumed for
39  !! input_filename if input_filename is not listed
40  !! in the namelist MOM_input_nml.
41  integer, optional, intent(in) :: ensemble_num !< The ensemble id of the current member
42  ! Local variables
43  integer, parameter :: npf = 5 ! Maximum number of parameter files
44 
45  character(len=240) :: &
46  parameter_filename(npf), & ! List of files containing parameters.
47  output_directory, & ! Directory to use to write the model output.
48  restart_input_dir, & ! Directory for reading restart and input files.
49  restart_output_dir, & ! Directory into which to write restart files.
50  input_filename ! A string that indicates the input files or how
51  ! the run segment should be started.
52  character(len=240) :: output_dir
53  integer :: unit, io, ierr, valid_param_files
54 
55  namelist /mom_input_nml/ output_directory, input_filename, parameter_filename, &
56  restart_input_dir, restart_output_dir
57 
58  ! Default values in case parameter is not set in file input.nml
59  parameter_filename(:) = ' '
60  output_directory = ' '
61  restart_input_dir = ' '
62  restart_output_dir = ' '
63  input_filename = ' '
64  if (present(default_input_filename)) input_filename = trim(default_input_filename)
65 
66  ! Open namelist
67  if (file_exists('input.nml')) then
68  unit = open_namelist_file(file='input.nml')
69  else
70  call mom_error(fatal,'Required namelist file input.nml does not exist.')
71  endif
72 
73  ! Read namelist parameters
74  ierr=1 ; do while (ierr /= 0)
75  read(unit, nml=mom_input_nml, iostat=io, end=10)
76  ierr = check_nml_error(io, 'MOM_input_nml')
77  enddo
78 10 call close_file(unit)
79 
80  ! Store parameters in container
81  if (present(dirs)) then
82  if (present(ensemble_num)) then
83  dirs%output_directory = slasher(ensembler(output_directory,ensemble_num))
84  dirs%restart_output_dir = slasher(ensembler(restart_output_dir,ensemble_num))
85  dirs%restart_input_dir = slasher(ensembler(restart_input_dir,ensemble_num))
86  dirs%input_filename = ensembler(input_filename,ensemble_num)
87  else
88  dirs%output_directory = slasher(ensembler(output_directory))
89  dirs%restart_output_dir = slasher(ensembler(restart_output_dir))
90  dirs%restart_input_dir = slasher(ensembler(restart_input_dir))
91  dirs%input_filename = ensembler(input_filename)
92  endif
93  endif
94 
95  ! Open run-time parameter file(s)
96  if (present(param_file)) then
97  output_dir = slasher(ensembler(output_directory))
98  valid_param_files = 0
99  do io = 1, npf
100  if (len_trim(trim(parameter_filename(io))) > 0) then
101  if (present(ensemble_num)) then
102  call open_param_file(ensembler(parameter_filename(io),ensemble_num), param_file, &
103  check_params, doc_file_dir=output_dir)
104  else
105  call open_param_file(ensembler(parameter_filename(io)), param_file, &
106  check_params, doc_file_dir=output_dir)
107  endif
108  valid_param_files = valid_param_files + 1
109  endif
110  enddo
111  if (valid_param_files == 0) call mom_error(fatal, "There must be at "//&
112  "least 1 valid entry in input_filename in MOM_input_nml in input.nml.")
113  endif
114 
115 end subroutine get_mom_input
116 
117 end module mom_get_input
mom_file_parser::open_param_file
subroutine, public open_param_file(filename, CS, checkable, component, doc_file_dir)
Make the contents of a parameter input file availalble in a param_file_type.
Definition: MOM_file_parser.F90:117
mom_get_input::directories
Container for paths and parameter file names.
Definition: MOM_get_input.F90:20
mom_error_handler::mom_mesg
subroutine, public mom_mesg(message, verb, all_print)
This provides a convenient interface for writing an informative comment.
Definition: MOM_error_handler.F90:53
mom_file_parser::param_file_type
A structure that can be parsed to read and document run-time parameters.
Definition: MOM_file_parser.F90:54
mom_io::ensembler
character(len=len(name)) function, public ensembler(name, ens_no_in)
Returns a name with "%#E" or "%E" replaced with the ensemble member number.
Definition: MOM_io.F90:763
mom_io
This module contains I/O framework code.
Definition: MOM_io.F90:2
mom_get_input
Reads the only Fortran name list needed to boot-strap the model.
Definition: MOM_get_input.F90:6
mom_file_parser
The MOM6 facility to parse input files for runtime parameters.
Definition: MOM_file_parser.F90:2
mom_get_input::get_mom_input
subroutine, public get_mom_input(param_file, dirs, check_params, default_input_filename, ensemble_num)
Get the names of the I/O directories and initialization file. Also calls the subroutine that opens ru...
Definition: MOM_get_input.F90:34
mom_error_handler::is_root_pe
logical function, public is_root_pe()
This returns .true. if the current PE is the root PE.
Definition: MOM_error_handler.F90:44
mom_io::file_exists
Indicate whether a file exists, perhaps with domain decomposition.
Definition: MOM_io.F90:68
mom_error_handler::mom_error
subroutine, public mom_error(level, message, all_print)
This provides a convenient interface for writing an mpp_error message with run-time filter based on a...
Definition: MOM_error_handler.F90:72
mom_error_handler
Routines for error handling and I/O management.
Definition: MOM_error_handler.F90:2