csv_file Derived Type

type, public :: csv_file

the main class for reading and writing CSV files.

Note

A CSV file is assumed to contain the same number of columns in each row. It may optionally contain a header row.


Inherits

type~~csv_file~~InheritsGraph type~csv_file csv_file type~csv_string csv_string type~csv_file->type~csv_string header, csv_data

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

  • 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)

procedure, public :: read => read_csv_file

  • 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 ,)

procedure, public :: destroy => destroy_csv_file

  • private subroutine destroy_csv_file(me)

    Destroy the data in a CSV file.

    Arguments

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

procedure, public :: variable_types

  • 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

generic, public :: get_header => get_header_str, get_header_csv_str

  • 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_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

procedure, private :: get_header_str

  • 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

procedure, private :: get_header_csv_str

  • 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

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

  • 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 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_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

procedure, private :: get_csv_data_as_str

  • 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

procedure, private :: csv_get_value

  • 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

procedure, private :: get_real_sp_column

  • 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

procedure, private :: get_real_wp_column

  • 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

procedure, private :: get_integer_column

  • 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

procedure, private :: get_logical_column

  • 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

procedure, private :: get_character_column

  • 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

procedure, private :: get_csv_string_column

  • 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

procedure, public :: open => open_csv_file

  • 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

generic, public :: add => add_cell, add_vector, add_matrix

  • 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.

procedure, private :: add_cell

  • 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.

procedure, private :: add_vector

  • 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.

procedure, private :: add_matrix

  • 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.

procedure, public :: next_row

  • 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

procedure, public :: close => close_csv_file

  • 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

procedure, private :: tokenize => tokenize_csv_line

  • 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

procedure, private :: read_line_from_file

  • 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

procedure, private :: get_column

  • 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

Source Code

    type,public :: csv_file

        !! the main class for reading and writing CSV files.
        !!
        !!@note A CSV file is assumed to contain the same number
        !!      of columns in each row. It may optionally contain
        !!      a header row.

        private

        logical :: verbose = .false. !! to print error messages

        character(len=1) :: quote     = '"'  !! quotation character
        character(len=1) :: delimiter = ','  !! delimiter character

        ! for reading a csv file:
        integer :: n_rows = 0  !! number of rows in the file
        integer :: n_cols = 0  !! number of columns in the file
        integer :: chunk_size = 1024 !! for expanding vectors
        type(csv_string),dimension(:),allocatable :: header      !! the header
        type(csv_string),dimension(:,:),allocatable :: csv_data  !! the data in the file

        ! for writing a csv file:
        integer :: icol = 0    !! last column written in current row
        integer :: iunit = 0   !! file unit for writing
        logical :: enclose_strings_in_quotes = .true.  !! if true, all string cells
                                                       !! will be enclosed in quotes.
        logical :: enclose_all_in_quotes = .false.     !! if true, *all* cells will
                                                       !! be enclosed in quotes.
        character(len=1) :: 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) :: logical_false_string = 'F' !! when writing a logical `false`
                                                       !! value to a CSV file, this
                                                       !! is the string to use
                                                       !! (default is `F`)

    contains

        private

        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 :: get_header_str
        procedure :: get_header_csv_str

        !>
        ! For getting data from the class
        ! after the file has been read.
        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
        procedure :: get_csv_data_as_str
        procedure :: csv_get_value
        procedure :: get_real_sp_column
        procedure :: get_real_wp_column
        procedure :: get_integer_column
        procedure :: get_logical_column
        procedure :: get_character_column
        procedure :: get_csv_string_column

        procedure,public :: open => open_csv_file

        generic,public :: add => add_cell,&
                                 add_vector,&
                                 add_matrix
        procedure :: add_cell
        procedure :: add_vector
        procedure :: add_matrix

        procedure,public :: next_row
        procedure,public :: close => close_csv_file

        procedure :: tokenize => tokenize_csv_line
        procedure :: read_line_from_file
        procedure :: get_column

    end type csv_file