csv_module Module

For reading and writing CSV files.


Uses

  • module~~csv_module~~UsesGraph module~csv_module csv_module iso_fortran_env iso_fortran_env module~csv_module->iso_fortran_env module~csv_kinds csv_kinds module~csv_module->module~csv_kinds module~csv_parameters csv_parameters module~csv_module->module~csv_parameters module~csv_utilities csv_utilities module~csv_module->module~csv_utilities module~csv_kinds->iso_fortran_env module~csv_parameters->module~csv_kinds module~csv_utilities->module~csv_kinds module~csv_utilities->module~csv_parameters

Variables

Type Visibility Attributes Name Initial
integer, public, parameter :: csv_type_string = 1

a character string cell

integer, public, parameter :: csv_type_double = 2

a real(wp) cell

integer, public, parameter :: csv_type_integer = 3

an integer(ip) cell

integer, public, parameter :: csv_type_logical = 4

a logical cell

real(kind=wp), private, parameter :: zero = 0.0_wp

Derived Types

type, public ::  csv_string

a cell from a CSV file.

Read more…

Components

Type Visibility Attributes Name Initial
character(len=:), public, allocatable :: str

type, public ::  csv_file

the main class for reading and writing CSV files.

Read more…

Components

Type Visibility Attributes Name Initial
logical, private :: verbose = .false.

to print error messages

character(len=1), private :: quote = '"'

quotation character

character(len=1), private :: delimiter = ','

delimiter character

integer, private :: n_rows = 0

number of rows in the file

integer, private :: n_cols = 0

number of columns in the file

integer, private :: chunk_size = 1024

for expanding vectors

type(csv_string), private, dimension(:), allocatable :: header

the header

type(csv_string), private, dimension(:,:), allocatable :: csv_data

the data in the file

integer, private :: icol = 0

last column written in current row

integer, private :: iunit = 0

file unit for writing

logical, private :: enclose_strings_in_quotes = .true.

if true, all string cells will be enclosed in quotes.

logical, private :: enclose_all_in_quotes = .false.

if true, all cells will be enclosed in quotes.

character(len=1), private :: logical_true_string = 'T'

when writing a logical true value to a CSV file, this is the string to use (default is T)

character(len=1), private :: logical_false_string = 'F'

when writing a logical false value to a CSV file, this is the string to use (default is F)

Type-Bound Procedures

procedure, public :: initialize => initialize_csv_file
procedure, public :: read => read_csv_file
procedure, public :: destroy => destroy_csv_file
procedure, public :: variable_types
generic, public :: get_header => get_header_str, get_header_csv_str
procedure, private :: get_header_str
procedure, private :: get_header_csv_str
generic, public :: get => get_csv_data_as_str, csv_get_value, get_real_sp_column, get_real_wp_column, get_integer_column, get_logical_column, get_character_column, get_csv_string_column ../../

For getting data from the class after the file has been read.

procedure, private :: get_csv_data_as_str
procedure, private :: csv_get_value
procedure, private :: get_real_sp_column
procedure, private :: get_real_wp_column
procedure, private :: get_integer_column
procedure, private :: get_logical_column
procedure, private :: get_character_column
procedure, private :: get_csv_string_column
procedure, public :: open => open_csv_file
generic, public :: add => add_cell, add_vector, add_matrix
procedure, private :: add_cell
procedure, private :: add_vector
procedure, private :: add_matrix
procedure, public :: next_row
procedure, public :: close => close_csv_file
procedure, private :: tokenize => tokenize_csv_line
procedure, private :: read_line_from_file
procedure, private :: get_column

Functions

private function number_of_lines_in_file(iunit) result(n_lines)

Returns the number of lines in a text file.

Read more…

Arguments

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

the file unit number (assumed to be open)

Return Value integer

the number of lines in the file


Subroutines

private subroutine initialize_csv_file(me, quote, delimiter, enclose_strings_in_quotes, enclose_all_in_quotes, logical_true_string, logical_false_string, chunk_size, verbose)

Initialize a csv_file.

Arguments

Type IntentOptional Attributes Name
class(csv_file), intent(out) :: me
character(len=1), intent(in), optional :: quote

(Default is ")

character(len=1), intent(in), optional :: delimiter

(Default is ,)

logical, intent(in), optional :: enclose_strings_in_quotes

if true, all string cells will be enclosed in quotes. (Default is True)

logical, intent(in), optional :: enclose_all_in_quotes

if true, all cells will be enclosed in quotes. (Default is False)

character(len=1), intent(in), optional :: logical_true_string

when writing a logical true value to a CSV file, this is the string to use (default is T)

character(len=1), intent(in), optional :: logical_false_string

when writing a logical false value to a CSV file, this is the string to use (default is F)

integer, intent(in), optional :: chunk_size

factor for expanding vectors (default is 100)

logical, intent(in), optional :: verbose

print error messages to the console (default is False)

private subroutine destroy_csv_file(me)

Destroy the data in a CSV file.

Arguments

Type IntentOptional Attributes Name
class(csv_file), intent(out) :: me

private subroutine read_csv_file(me, filename, header_row, skip_rows, status_ok, delimiter)

Read a CSV file.

Read more…

Arguments

Type IntentOptional Attributes Name
class(csv_file), intent(inout) :: me
character(len=*), intent(in) :: filename

the CSV file to open

integer, intent(in), optional :: header_row

the header row

integer, intent(in), optional, dimension(:) :: skip_rows

rows to skip

logical, intent(out) :: status_ok

status flag

character(len=1), intent(in), optional :: delimiter

(Default is ,)

private subroutine open_csv_file(me, filename, n_cols, status_ok, append)

Open a CSV file for writing.

Read more…

Arguments

Type IntentOptional Attributes Name
class(csv_file), intent(inout) :: me
character(len=*), intent(in) :: filename

the CSV file to open

integer, intent(in) :: n_cols

number of columns in the file

logical, intent(out) :: status_ok

status flag

logical, intent(in), optional :: append

append if file exists

private subroutine close_csv_file(me, status_ok)

Close a CSV file after writing

Arguments

Type IntentOptional Attributes Name
class(csv_file), intent(inout) :: me
logical, intent(out) :: status_ok

status flag

private subroutine add_cell(me, val, int_fmt, real_fmt, trim_str)

Add a cell to a CSV file.

Read more…

Arguments

Type IntentOptional Attributes Name
class(csv_file), intent(inout) :: me
class(*), intent(in) :: val

the value to add

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

if val is an integer, use this format string.

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

if val is a real, use this format string.

logical, intent(in), optional :: trim_str

if val is a string, then trim it.

private subroutine add_vector(me, val, int_fmt, real_fmt, trim_str)

Add a vector to a CSV file. Each element is added as a cell to the current line.

Arguments

Type IntentOptional Attributes Name
class(csv_file), intent(inout) :: me
class(*), intent(in), dimension(:) :: val

the values to add

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

if val is an integer, use this format string.

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

if val is a real, use this format string.

logical, intent(in), optional :: trim_str

if val is a string, then trim it.

private subroutine add_matrix(me, val, int_fmt, real_fmt, trim_str)

Add a matrix to a CSV file. Each row is added as a new line. Line breaks are added at the end of each line (in this way it differs from the other add routines).

Arguments

Type IntentOptional Attributes Name
class(csv_file), intent(inout) :: me
class(*), intent(in), dimension(:,:) :: val

the values to add

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

if val is an integer, use this format string.

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

if val is a real, use this format string.

logical, intent(in), optional :: trim_str

if val is a string, then trim it.

private subroutine next_row(me)

Advance to the next row in the CSV file (write any blank cells that are necessary to finish the row)

Arguments

Type IntentOptional Attributes Name
class(csv_file), intent(inout) :: me

private subroutine get_header_csv_str(me, header, status_ok)

Returns the header as a type(csv_string) array. (read must have already been called to read the file).

Arguments

Type IntentOptional Attributes Name
class(csv_file), intent(inout) :: me
type(csv_string), intent(out), dimension(:), allocatable :: header
logical, intent(out) :: status_ok

private subroutine get_header_str(me, header, status_ok)

Returns the header as a character(len=*) array. (read must have already been called to read the file).

Arguments

Type IntentOptional Attributes Name
class(csv_file), intent(inout) :: me
character(len=*), intent(out), dimension(:), allocatable :: header
logical, intent(out) :: status_ok

private subroutine get_csv_data_as_str(me, csv_data, status_ok)

Returns a character(len=*) array containing the csv data (read must have already been called to read the file).

Arguments

Type IntentOptional Attributes Name
class(csv_file), intent(inout) :: me
character(len=*), intent(out), dimension(:,:), allocatable :: csv_data

the data

logical, intent(out) :: status_ok

status flag

private pure elemental subroutine to_real_sp(str, val, status_ok)

Convert a string to a real(sp)

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: str
real(kind=sp), intent(out) :: val
logical, intent(out) :: status_ok

private pure elemental subroutine to_real_wp(str, val, status_ok)

Convert a string to a real(wp)

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: str
real(kind=wp), intent(out) :: val
logical, intent(out) :: status_ok

private pure elemental subroutine to_integer(str, val, status_ok)

Convert a string to a integer(ip)

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: str
integer(kind=ip), intent(out) :: val
logical, intent(out) :: status_ok

private pure elemental subroutine to_logical(str, val, status_ok)

Convert a string to a logical

Read more…

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: str
logical, intent(out) :: val
logical, intent(out) :: status_ok

private subroutine variable_types(me, itypes, status_ok)

Returns an array indicating the variable type of each columns.

Read more…

Arguments

Type IntentOptional Attributes Name
class(csv_file), intent(inout) :: me
integer, intent(out), dimension(:), allocatable :: itypes
logical, intent(out) :: status_ok

private subroutine infer_variable_type(str, itype)

Infers the variable type, assuming the following precedence:

Read more…

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: str
integer, intent(out) :: itype

private subroutine csv_get_value(me, row, col, val, status_ok)

Get an individual value from the csv_data structure in the CSV class.

Read more…

Arguments

Type IntentOptional Attributes Name
class(csv_file), intent(inout) :: me
integer, intent(in) :: row

row number

integer, intent(in) :: col

column number

class(*), intent(out) :: val

the returned value

logical, intent(out) :: status_ok

status flag

private subroutine get_column(me, icol, r, status_ok)

Return a column from a CSV file vector.

Read more…

Arguments

Type IntentOptional Attributes Name
class(csv_file), intent(inout) :: me
integer, intent(in) :: icol

column number

class(*), intent(out), dimension(:) :: r

assumed to have been allocated to the correct size by the caller. (n_rows)

logical, intent(out) :: status_ok

status flag

private subroutine get_real_sp_column(me, icol, r, status_ok)

Return a column from a CSV file as a real(sp) vector.

Arguments

Type IntentOptional Attributes Name
class(csv_file), intent(inout) :: me
integer, intent(in) :: icol

column number

real(kind=sp), intent(out), dimension(:), allocatable :: r
logical, intent(out) :: status_ok

private subroutine get_real_wp_column(me, icol, r, status_ok)

Return a column from a CSV file as a real(wp) vector.

Arguments

Type IntentOptional Attributes Name
class(csv_file), intent(inout) :: me
integer, intent(in) :: icol

column number

real(kind=wp), intent(out), dimension(:), allocatable :: r
logical, intent(out) :: status_ok

private subroutine get_integer_column(me, icol, r, status_ok)

Return a column from a CSV file as a integer(ip) vector.

Arguments

Type IntentOptional Attributes Name
class(csv_file), intent(inout) :: me
integer, intent(in) :: icol

column number

integer(kind=ip), intent(out), dimension(:), allocatable :: r
logical, intent(out) :: status_ok

private subroutine get_logical_column(me, icol, r, status_ok)

Convert a column from a csv_string matrix to a logical vector.

Arguments

Type IntentOptional Attributes Name
class(csv_file), intent(inout) :: me
integer, intent(in) :: icol

column number

logical, intent(out), dimension(:), allocatable :: r
logical, intent(out) :: status_ok

private subroutine get_character_column(me, icol, r, status_ok)

Convert a column from a csv_string matrix to a character(len=*) vector.

Arguments

Type IntentOptional Attributes Name
class(csv_file), intent(inout) :: me
integer, intent(in) :: icol

column number

character(len=*), intent(out), dimension(:), allocatable :: r
logical, intent(out) :: status_ok

private subroutine get_csv_string_column(me, icol, r, status_ok)

Convert a column from a csv_string matrix to a type(csv_string) vector.

Arguments

Type IntentOptional Attributes Name
class(csv_file), intent(inout) :: me
integer, intent(in) :: icol

column number

type(csv_string), intent(out), dimension(:), allocatable :: r
logical, intent(out) :: status_ok

private subroutine tokenize_csv_line(me, line, cells)

Tokenize a line from a CSV file. The result is an array of csv_string types.

Read more…

Arguments

Type IntentOptional Attributes Name
class(csv_file), intent(inout) :: me
character(len=*), intent(in) :: line
type(csv_string), intent(out), dimension(:), allocatable :: cells

private subroutine read_line_from_file(me, iunit, line, status_ok)

Reads the next line from a file.

Arguments

Type IntentOptional Attributes Name
class(csv_file), intent(in) :: me
integer, intent(in) :: iunit
character(len=:), intent(out), allocatable :: line
logical, intent(out) :: status_ok

true if no problems

private pure subroutine split(str, token, chunk_size, vals)

Split a character string using a token. This routine is inspired by the Python split function.

Read more…

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: str
character(len=*), intent(in) :: token
integer, intent(in) :: chunk_size

for expanding vectors

type(csv_string), intent(out), dimension(:), allocatable :: vals