Read data from files using a Fortran format specification.
Fortran is great, isn't it? But do you have lots of data sitting in Fortran-formatted files that you would like to access using Swift for your own nefarious purposes? Yes, I thought so. All of the boilerplate you need is here.
let format = try FortranFile.format(from: "1x,4i1,i5,12i3,f15.11,2f18.11,f14.11,f20.11")
let result = try FortranFile.read(input: input, using: format)
The result is an array of any FortranFile.Value
corresponding to the values read from the input.
Create a Reader
instance to read multiple lines:
let reader = FortranFile.makeReader(using: format)
for line in lines {
let result = try reader.read(input: line)
}
Each value read from the input is represented by a type conforming to the FortranFile.Value
protocol. The underlying Swift value can be accessed using the value
property. The type of the value
property varies to match the type of Fortran value read from the input in accordance with the format string.
FortanDouble
: wraps a SwiftDouble
representing a real number.FortranInteger
: wraps a SwiftInt
representing an integer.FortranLogical
: wraps a SwiftBool
representing a logical value.FortranString
: wraps a SwiftString
representing text.FortranArray
: wraps an array ofany FortranFile.Value
representing repeated values.
Note: Arrays of repeated values can be automatically flattened by a reading configuration option.
A reading configuration can be provided to a Reader
instance to allow reading behaviour to be customised.
public struct ReadingConfiguration: Sendable {
public static let common = ReadingConfiguration()
public var defaultZeroiseBlanks = false
public var shouldAllowCommaTermination = false
public var shouldFlattenArrays = false
}
Dw.d
Ew.d
Fw.d
Gw.d
Iw
Lw
Aw
nX
B
BN
BZ
nP