For reading and writing CSV files.
Type | Visibility | Attributes | Name | Initial | |||
---|---|---|---|---|---|---|---|
integer, | public, | parameter | :: | csv_type_string | = | 1 |
a character string cell |
integer, | public, | parameter | :: | csv_type_double | = | 2 |
a |
integer, | public, | parameter | :: | csv_type_integer | = | 3 |
an |
integer, | public, | parameter | :: | csv_type_logical | = | 4 |
a logical cell |
real(kind=wp), | private, | parameter | :: | zero | = | 0.0_wp |
a cell from a CSV file.
Type | Visibility | Attributes | Name | Initial | |||
---|---|---|---|---|---|---|---|
character(len=:), | public, | allocatable | :: | str |
the main class for reading and writing CSV files.
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 |
|
character(len=1), | private | :: | logical_false_string | = | 'F' |
when writing a logical |
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 |
Returns the number of lines in a text file.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer, | intent(in) | :: | iunit |
the file unit number (assumed to be open) |
the number of lines in the file
Initialize a csv_file.
Type | Intent | Optional | 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 |
|
character(len=1), | intent(in), | optional | :: | logical_false_string |
when writing a logical |
|
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) |
Destroy the data in a CSV file.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(out) | :: | me |
Read a CSV file.
Type | Intent | Optional | 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 |
Open a CSV file for writing.
Type | Intent | Optional | 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 |
Close a CSV file after writing
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
logical, | intent(out) | :: | status_ok |
status flag |
Add a cell to a CSV file.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
class(*), | intent(in) | :: | val |
the value to add |
||
character(len=*), | intent(in), | optional | :: | int_fmt |
if |
|
character(len=*), | intent(in), | optional | :: | real_fmt |
if |
|
logical, | intent(in), | optional | :: | trim_str |
if |
Add a vector to a CSV file. Each element is added as a cell to the current line.
Type | Intent | Optional | 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 |
|
character(len=*), | intent(in), | optional | :: | real_fmt |
if |
|
logical, | intent(in), | optional | :: | trim_str |
if |
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).
Type | Intent | Optional | 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 |
|
character(len=*), | intent(in), | optional | :: | real_fmt |
if |
|
logical, | intent(in), | optional | :: | trim_str |
if |
Advance to the next row in the CSV file (write any blank cells that are necessary to finish the row)
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me |
Returns the header as a type(csv_string)
array.
(read
must have already been called to read the file).
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
type(csv_string), | intent(out), | dimension(:), allocatable | :: | header | ||
logical, | intent(out) | :: | status_ok |
Returns the header as a character(len=*)
array.
(read
must have already been called to read the file).
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
character(len=*), | intent(out), | dimension(:), allocatable | :: | header | ||
logical, | intent(out) | :: | status_ok |
Returns a character(len=*)
array containing the csv data
(read
must have already been called to read the file).
Type | Intent | Optional | 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 |
Convert a string to a real(sp)
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | str | |||
real(kind=sp), | intent(out) | :: | val | |||
logical, | intent(out) | :: | status_ok |
Convert a string to a real(wp)
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | str | |||
real(kind=wp), | intent(out) | :: | val | |||
logical, | intent(out) | :: | status_ok |
Convert a string to a integer(ip)
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | str | |||
integer(kind=ip), | intent(out) | :: | val | |||
logical, | intent(out) | :: | status_ok |
Convert a string to a logical
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | str | |||
logical, | intent(out) | :: | val | |||
logical, | intent(out) | :: | status_ok |
Returns an array indicating the variable type of each columns.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
integer, | intent(out), | dimension(:), allocatable | :: | itypes | ||
logical, | intent(out) | :: | status_ok |
Infers the variable type, assuming the following precedence:
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | str | |||
integer, | intent(out) | :: | itype |
Get an individual value from the csv_data
structure in the CSV class.
Type | Intent | Optional | 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 |
Return a column from a CSV file vector.
Type | Intent | Optional | 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.
( |
|
logical, | intent(out) | :: | status_ok |
status flag |
Return a column from a CSV file as a real(sp)
vector.
Type | Intent | Optional | 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 |
Return a column from a CSV file as a real(wp)
vector.
Type | Intent | Optional | 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 |
Return a column from a CSV file as a integer(ip)
vector.
Type | Intent | Optional | 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 |
Convert a column from a csv_string
matrix to a logical
vector.
Type | Intent | Optional | 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 |
Convert a column from a csv_string
matrix to a character(len=*)
vector.
Type | Intent | Optional | 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 |
Convert a column from a csv_string
matrix to a type(csv_string)
vector.
Type | Intent | Optional | 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 |
Tokenize a line from a CSV file. The result is an array of csv_string
types.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
character(len=*), | intent(in) | :: | line | |||
type(csv_string), | intent(out), | dimension(:), allocatable | :: | cells |
Reads the next line from a file.
Type | Intent | Optional | 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 |
Split a character string using a token. This routine is inspired by the Python split function.
Type | Intent | Optional | 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 |