file_utils Module

Various utilities for working with input and output files, as well as some shims/backfills for standard features not present in older compilers.

This module is responsible for setting up the label for the run, run_name, as well as opening the main input file, the error file, and the exit reason file.

Most of the procedures in this module deal with files, and are therefore not 1/thread safe, and you must manually guard any opens/closes.


Uses


Contents


Variables

Type Visibility Attributes Name Initial
character(len=run_name_size), public, pointer :: run_name

Label for the run. Usually this is the input file without any extensions. Set by init_run_name

character(len=run_name_size), private, target :: arun_name

The full input file name, set by get_input_filename

character(len=run_name_size), private, target :: job_name

The current job name. See job_manage

character(len=run_name_size), public, target :: run_name_target

This array replaces the cbuff array in gs2_main. Having the target array in the same scope as the pointer is much better practice in general.

character(len=run_name_size), public :: list_name

Label for the list, taken from the command line

integer, public, parameter :: stdout_unit = 6

Unit associated with stdout FIXME: Use iso_fortran_env::output_unit

integer, private :: input_unit_no

Unit number for main input file

integer, private :: error_unit_no = stdout_unit

Unit number for main error file

integer, private :: exit_reason_unit_no = stdout_unit

Unit number for exit reason file

integer, public :: num_input_lines

Number of lines in input file

logical, public :: initialized = .false.

Is module initialized?


Functions

private function get_input_filename() result(input_filename)

Return the input filename as set by the command line

Arguments

None

Return Value character(len=run_name_size)

private function is_input_file_list(input_filename)

Returns true if input_filename is a list input: that is, if it ends in .list

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: input_filename

Return Value logical

public function input_unit(nml)

Rewind the input file to start of namelist nml, and return the unit of the file opened by init_input_unit

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: nml

Name of namelist to find start of

Return Value integer

public function input_unit_exist(nml, exist)

Similar to input_unit but set exist to true if nml was found in the input file, and false otherwise

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: nml

Name of namelist to find start of

logical, intent(out) :: exist

Was nml was found in the input file?

Return Value integer

public pure function error_unit()

Returns the file unit number associated with the error file

Arguments

None

Return Value integer

public pure function exit_reason_unit()

Returns the file unit number associated with the exit_reason file

Arguments

None

Return Value integer


Subroutines

public subroutine init_file_utils(list, input, error, trin_run, name, n_ensembles, input_file)

Read the run_name from the command line (if not given), and determine from the extension whether this is a list run (i.e. a list of runs has been given). If so or if this is a run with multiple ensembles, open the list description. If not, open the error file and call init_input_unit

Arguments

Type IntentOptional Attributes Name
logical, intent(out) :: list

True if the input file is a list run file (see is_input_file_list)

logical, intent(in), optional :: input

If true, open the input file and strip comments (default: true)

logical, intent(in), optional :: error

If true, open the error output file (default: true)

logical, intent(in), optional :: trin_run

If present, regardless of value, sets list to false

character(len=*), intent(in), optional :: name

Set run_name if input is false or trin_run is present

integer, intent(in), optional :: n_ensembles

Number of run ensembles. If greater than 1, sets list_name and doesn't open any files

character(len=*), intent(in), optional :: input_file

Use given input filename instead of reading from command line

private subroutine init_run_name()

This is called for a non Trinity or list run - it checks that the input file name ends in .in, chops the extension off and stores it in arun_name. It also assigns the pointer run_name to arun_name.

Arguments

None

public subroutine init_job_name(jobname)

Set run_name and job_name to jobname. Used by job_manage

Arguments

Type IntentOptional Attributes Name
character(len=run_name_size), intent(in) :: jobname

Current job name

public subroutine get_unused_unit(unit)

Get an unused unit number for I/O.

Read more…

Arguments

Type IntentOptional Attributes Name
integer, intent(out) :: unit

A new unit not associated with an open file

public subroutine open_output_file(unit, ext, binary)

Open an output file to write (replacing any existing) whose name is <run_name>.<ext>, and set unit to the unit number of that output file. If the binary flag is true, a binary file is opened

Arguments

Type IntentOptional Attributes Name
integer, intent(out) :: unit

Unit number of opened file

character(len=*), intent(in) :: ext

File extension to open, appended to run_name

logical, intent(in), optional :: binary

If true, then open a binary (unformatted) file

public subroutine append_output_file(unit, ext, run_name_in)

Open an output file to write (appending if existing) whose name is <run_name>.<ext>, and set unit to the unit number of that output file. If the optional run_name_in variable is present, this replaces run_name as the root of the output file.

Arguments

Type IntentOptional Attributes Name
integer, intent(out) :: unit

Unit number of opened file, in append mode

character(len=*), intent(in) :: ext

File extension to open, appended to run_name

character(len=*), intent(in), optional :: run_name_in

Optional root name for the output file. If not specified, run_name is used as the root.

public subroutine close_output_file(unit)

Close the file associated with unit

Read more…

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: unit

Unit of file to close

public subroutine init_error_unit(open_it)

Open error file and record associated lun/unit

Arguments

Type IntentOptional Attributes Name
logical, intent(in) :: open_it

If true, open the file, otherwise do nothing

private subroutine init_exit_reason_unit(open_it)

Open exit_reason file and record associated lun/unit

Arguments

Type IntentOptional Attributes Name
logical, intent(in) :: open_it

If true, open the file, otherwise do nothing

public subroutine replace_all_tabs(line)

Replaces each horizontal tab with a single space

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(inout) :: line

Text to modify in-place

private subroutine replace_leading_tabs(line)

Replaces all leading tabs with space. Note we consider any tabs appearing before the first non-space/tab character.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(inout) :: line

Text to modify in-place

private subroutine strip_comments(line)

Remove Fortran comments (!) from line

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(inout) :: line

Text to modify in-place

public subroutine init_input_unit(open_it)

Opens the input file, strip out any comments and write them into the file .<run_name>.in. Check for includes, read any lines from the includes, strip any comments from them and add them to the same file.

Arguments

Type IntentOptional Attributes Name
logical, intent(in) :: open_it

If true, open the file, otherwise do nothing

public subroutine finish_file_utils()

Close any files opened by init_file_utils

Arguments

None

public subroutine get_input_unit(unit)

Returns the file unit number associated with the input file

Read more…

Arguments

Type IntentOptional Attributes Name
integer, intent(out) :: unit

public subroutine get_indexed_namelist_unit(unit, nml, index_in, exist)

Copy namelist, <nml>_<index_in>, from the input file to namelist NML in a temporary file with unit unit

Arguments

Type IntentOptional Attributes Name
integer, intent(out) :: unit

Unit of new temporary file containing the indexed namelist

character(len=*), intent(in) :: nml

Name of indexed namelist to copy

integer, intent(in) :: index_in

Index number of namelist to copy

logical, intent(out), optional :: exist

Does the indexed namelist exist or not?