Giter Site home page Giter Site logo

sostock / fortranfiles.jl Goto Github PK

View Code? Open in Web Editor NEW

This project forked from traktofon/fortranfiles.jl

0.0 0.0 0.0 507 KB

Julia support for reading/writing Fortran unformatted files

License: Other

Julia 84.24% Makefile 2.47% Perl 0.82% Fortran 12.47%

fortranfiles.jl's Introduction

FortranFiles.jl

A Julia package for reading/writing Fortran unformatted (i.e. binary) files.

Documentation Build Status coveralls Status codecov.io Status

Supported Julia versions:

  • 1.0: working fine
  • 0.7: working fine
  • 0.6: working fine
  • 0.5 and earlier: not supported

Quickstart

Full documentation is available at https://traktofon.github.io/FortranFiles.jl/latest/.

Installation: In julia-0.6,

Pkg.add("FortranFiles")

or from julia-0.7:

# type ] to enter package managing mode
pkg> add FortranFiles

The following examples use julia-0.7 syntax. They also work in julia-0.6 with using Compat.

Example usage for reading files:

using FortranFiles

# opening a file for reading
f = FortranFile("data.bin")

# reading a single scalar from the file
# (if there is more data in the record, it will be skipped -- this is Fortran behavior)
x = read(f, Float64)

# reading a 1D array (here of length 10)
vector = read(f, (Float64,10))

# reading into an already allocated array
vector = zeros(10)
read(f, vector)

# reading a 2D array -- alternative syntaxes
matrix = read(f, (Float64,10,10))
matrix = read(f, (Float64,(10,10)))

# reading a CHARACTER*20 string
fstr = read(f, FString{20})
# convert this string to a Julia String, discarding trailing spaces
jstr = trimstring(fstr)

# reading a record with multiple data
i, strings, zmatrix = read(f, Int32, (FString{20},10), (ComplexF64,10,10))

# macro for reading a record where the size is not known ahead
@fread f n::Int32 vector::(Float64,n)

# skipping over a record
read(f)

# go back to the beginning of the file
rewind(f)

Example usage for writing files:

# opening a file for writing
f = FortranFile("data.bin", "w")

# take care when defining the Julia data to be written into the file,
# noting the correspondence between Julia and Fortran datatypes
i = Int32(1)                 # INTEGER(KIND=4)
x = 1.0                      # REAL(KIND=REAL64), usually the same as DOUBLE PRECISION
A = zeros(Float32, 10, 10)   # REAL,DIMENSION(10,10)
s = FString(20, "blabla")    # CHARACTER(LEN=20)

# write all these data into a single record
write(f, i, x, A, s)

# close the file
close(f)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.