Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jacobwilliams/csv-fortran
Read and Write CSV Files Using Modern Fortran
https://github.com/jacobwilliams/csv-fortran
csv fortran fortran-package-manager
Last synced: about 6 hours ago
JSON representation
Read and Write CSV Files Using Modern Fortran
- Host: GitHub
- URL: https://github.com/jacobwilliams/csv-fortran
- Owner: jacobwilliams
- License: other
- Created: 2017-01-30T03:41:33.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2024-06-22T20:15:55.000Z (5 months ago)
- Last Synced: 2024-06-29T23:21:18.550Z (4 months ago)
- Topics: csv, fortran, fortran-package-manager
- Language: Fortran
- Homepage:
- Size: 1.62 MB
- Stars: 93
- Watchers: 9
- Forks: 43
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
![csv-fortran](media/logo.png)
============[![Language](https://img.shields.io/badge/-Fortran-734f96?logo=fortran&logoColor=white)](https://github.com/topics/fortran)
[![GitHub release](https://img.shields.io/github/release/jacobwilliams/csv-fortran.svg)](https://github.com/jacobwilliams/csv-fortran/releases/latest)
[![Build Status](https://github.com/jacobwilliams/csv-fortran/actions/workflows/CI.yml/badge.svg)](https://github.com/jacobwilliams/csv-fortran/actions)
[![codecov](https://codecov.io/gh/jacobwilliams/csv-fortran/branch/master/graph/badge.svg?token=43HK33CSMY)](https://codecov.io/gh/jacobwilliams/csv-fortran)
[![last-commit](https://img.shields.io/github/last-commit/jacobwilliams/csv-fortran)](https://github.com/jacobwilliams/csv-fortran/commits/master)### Description
A modern Fortran library for reading and writing CSV (comma-separated value) files.
### Latest Release
[![GitHub release](https://img.shields.io/github/release/jacobwilliams/csv-fortran.svg?style=plastic)](https://github.com/jacobwilliams/csv-fortran/releases/latest)
### Documentation
The latest API documentation for the `master` branch can be found [here](https://jacobwilliams.github.io/csv-fortran/). This was generated from the source code using [FORD](https://github.com/Fortran-FOSS-Programmers/ford).
### Getting started
#### Get the code
```bash
git clone https://github.com/jacobwilliams/csv-fortran
cd csv-fortran
```
#### Dependencies
1. Git
2. [fpm](https://github.com/fortran-lang/fpm) or [CMake](https://cmake.org)
3. [FORD](https://github.com/Fortran-FOSS-Programmers/ford) (optional)#### Build with [fortran-lang/fpm](https://github.com/fortran-lang/fpm)
Fortran Package Manager (fpm) is a great package manager and build system for Fortran.
You can build using provided `fpm.toml`:
```bash
fpm build
```
To use `csv-fortran` within your fpm project, add the following to your `fpm.toml` file:
```toml
[dependencies]
csv-fortran = { git="https://github.com/jacobwilliams/csv-fortran.git" }
```### Examples
Everything is handled by an object-oriented `csv_file` class. Here is an example for writing a file:
```fortran
program csv_write_testuse csv_module
use iso_fortran_env, only: wp => real64implicit none
type(csv_file) :: f
logical :: status_ok! set optional inputs:
call f%initialize(verbose = .true.)! open the file
call f%open('test.csv',n_cols=4,status_ok=status_ok)! add header
call f%add(['x','y','z','t'])
call f%next_row()! add some data:
call f%add([1.0_wp,2.0_wp,3.0_wp],real_fmt='(F5.3)')
call f%add(.true.)
call f%next_row()
call f%add([4.0_wp,5.0_wp,6.0_wp],real_fmt='(F5.3)')
call f%add(.false.)
call f%next_row()! finished
call f%close(status_ok)end program csv_write_test
```Which produces the following file:
```
x,y,z,t
1.000,2.000,3.000,T
4.000,5.000,6.000,F
```Real, integer, logical, or character data can be added as scalars, vectors, and matrices.
When reading a CSV file, the data is stored internally in the class as allocatable character strings, which can be retrieved as real, integer, logical or character vectors as necessary. For example, to get the `x`, `y`, `z`, and `t` vectors from the previously-generated file:
```fortran
program csv_read_testuse csv_module
use iso_fortran_env, only: wp => real64implicit none
type(csv_file) :: f
character(len=30),dimension(:),allocatable :: header
real(wp),dimension(:),allocatable :: x,y,z
logical,dimension(:),allocatable :: t
logical :: status_ok
integer,dimension(:),allocatable :: itypes! read the file
call f%read('test.csv',header_row=1,status_ok=status_ok)! get the header and type info
call f%get_header(header,status_ok)
call f%variable_types(itypes,status_ok)! get some data
call f%get(1,x,status_ok)
call f%get(2,y,status_ok)
call f%get(3,z,status_ok)
call f%get(4,t,status_ok)! destroy the file
call f%destroy()end program csv_read_test
```Various options are user-selectable for specifying the format (e.g., changing the quote or delimiter characters). You can choose to enclose strings (or all fields) in quotes or not. The library works pretty well, and there are probably additional improvements that could be made. For one thing, it doesn't properly handle the case of a string that contains the delimiter character (I'll eventually fix this). If anybody has any other improvements, fork it and send me a pull request.
### License
This library is released under a [BSD-3 license](https://github.com/jacobwilliams/csv-fortran/blob/master/LICENSE).