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