Reading HDF5 files with F#

Hi All,

I’m brand new to F# and .NET. One of the first tasks that I would like to learn is how to read HDF5 files with F#. I found quite a few resources on how to tackle that task with C#, but fewer (and older) resources for F#. Can anybody point me to a beginner-friendly resource for reading attributes and arrays from HDF5 files?

Thanks,

Travis

Welcome to the forum :slight_smile:

I don’t know of a particular F#-based resource (I’ve never heard of this file type) but it’s usually possible to convert any C# code sample into F# as long as you know enough C# and F#. I’ll often just paste C# code into F# and modify it until it compiles. This usually doesn’t result in good F# code but it’s a good starting place. Can you show us some C# example code that you’d like to translate?

1 Like

You could try to use HDF5-CSharp library.

I’ll show how to rewrite sample from the ReadMe to F#:

[<CLIMutable>]
type TestRecordWithArray = {
     TestDoubles : float[]
     TestStrings : string[]
     TestInteger : int
     TestDouble  : float
     TestBoolean : bool
     TestString  : string
 }

let sample = {
    TestInteger = 2
    TestDouble = 1.1
    TestBoolean = true
    TestString = "test string"
    TestDoubles = [| 1.1; 1.2; -1.1; -1.2 |]
    TestStrings = [| "one"; "two"; "three"; "four" |]
}

open HDF5CSharp

let fileId = Hdf5.CreateFile("testFile.H5")

Hdf5.WriteObject(fileId, sample, "sample") |> ignore

let v = Hdf5.ReadObject<TestRecordWithArray>(fileId, "sample")

printfn "%A" v

Hdf5.CloseFile(fileId) |> ignore
2 Likes
1 Like