SwiftEccodes

0.1.6

Read GRIB files in swift
patrick-zippenfenig/SwiftEccodes

What's New

Fix concurrent access to `grib_context`

2024-03-27T17:41:34Z

If multiple files at the same time are opened, it may result in double free internally in eccodes. An additional lock, prevents concurrent access to grib_context

SwiftEccodes

A very basic library to read GRIB files in Swift. This library is a wrapper for eccodes.

WARNING: No stable interface declared yet

Usage

Install eccodes via brew install eccodes or apt install libeccodes-dev

Add SwiftEccodes as a dependency to your Package.swift

  dependencies: [
    .package(url: "https://github.com/patrick-zippenfenig/SwiftEccodes.git", from: "0.0.0")
  ],
  targets: [
    .target(name: "MyApp", dependencies: [
      .product(name: "SwiftEccodes", package: "SwiftEccodes"),
    ])
  ]

Read GRIB messages and data from files:

import SwiftEccodes

let messages = try SwiftEccodes.getMessages(fileName: "test.grib", multiSupport: true)
for message in messages {
    message.iterate(namespace: .ls).forEach({
        print($0)
    })
    print(message.get(attribute: "name")!)
    let data = try message.getDouble()
    print(data[0..<10])
}

Or read directly from memory:

import SwiftEccodes

let data = try Data(contentsOf: URL(fileURLWithPath: "test.grib"))
try data.withUnsafeBytes { ptr in
    let messages = try SwiftEccodes.getMessages(memory: ptr, multiSupport: true)
    for message in messages {
        message.iterate(namespace: .ls).forEach({
            print($0)
        })
        message.iterate(namespace: .geography).forEach({
            print($0)
        })
        print(message.get(attribute: "name")!)
        let data = try message.getDouble()
        print(data.count)
        print(data[0..<10])
    }
}

Description

  • Swift Tools 5.6.0
View More Packages from this Author

Dependencies

  • None
Last updated: Fri Oct 18 2024 03:53:24 GMT-0900 (Hawaii-Aleutian Daylight Time)