Skip to content

Commit

Permalink
feat: Read in memory NetCDF files (#5)
Browse files Browse the repository at this point in the history
* feat: nc_open_mem support

* refactor

* fix tests

* bump swift version for CI testing
  • Loading branch information
patrick-zippenfenig committed Jun 7, 2024
1 parent b05230e commit 6c75b6b
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 28 deletions.
41 changes: 17 additions & 24 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,14 @@ jobs:
strategy:
fail-fast: false
matrix:
xcode: ["13.3.1", "13.2.1", "13.1", "12.5.1", "12.4"]
xcode: ["15.4", "15.2", "14.3.1"]
include:
- xcode: "13.3.1"
macos: macOS-12
- xcode: "13.2.1"
macos: macOS-11
- xcode: "13.1"
macos: macOS-11
- xcode: "12.5.1"
macos: macOS-11
- xcode: "12.4"
macos: macOS-11
- xcode: "15.4"
macos: macOS-14
- xcode: "15.2"
macos: macOS-14
- xcode: "14.3.1"
macos: macOS-14
runs-on: ${{ matrix.macos }}
name: macOS
steps:
Expand Down Expand Up @@ -62,20 +58,17 @@ jobs:
strategy:
fail-fast: false
matrix:
swift: ["5.6", "5.5", "5.4", "5.3"]
swift: ["5.10", "5.9", "5.8"]
include:
- swift: "5.6"
container: "swift:5.6"
cache-version: 2
- swift: "5.5"
container: "swift:5.5"
cache-version: 2
- swift: "5.4"
container: "swift:5.4"
cache-version: 4
- swift: "5.3"
container: "swift:5.3"
cache-version: 4
- swift: "5.10"
container: "swift:5.10"
cache-version: 1
- swift: "5.9"
container: "swift:5.9"
cache-version: 1
- swift: "5.8"
container: "swift:5.8"
cache-version: 1
runs-on: ubuntu-20.04
container: ${{ matrix.container }}
name: Linux
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ SwiftNetCDF uses a simple data structures to organise access to NetCDF functions
struct NetCDF {
static func create(path: String, overwriteExisting: Bool) -> Group
static func open(path: String, allowUpdate: Bool) -> Group?

/// Opens a NetCDF file from memory in read-only mode
static func open(memory: UnsafeRawBufferPointer)) -> Group?
}

struct Group {
Expand Down
4 changes: 2 additions & 2 deletions Sources/CNetCDF/shim.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#include <netcdf.h>
#include <netcdf.h>
#include <netcdf_mem.h>
18 changes: 16 additions & 2 deletions Sources/SwiftNetCDF/Nc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ public extension Nc {
}
}

/// Open an exsiting NetCDF file
/// Open an existing NetCDF file
static func open(path: String, omode: Int32) throws -> NcId {
var ncid: Int32 = 0
try exec {
Expand All @@ -545,7 +545,21 @@ public extension Nc {
return NcId(ncid)
}

/// Open an exsiting NetCDF file
/// Open an existing NetCDF file from memory
static func open(path: String, memory: UnsafeRawBufferPointer, omode: Int32) throws -> NcId {
var ncid: Int32 = 0
try exec {
nc_open_mem(path, omode, memory.count, UnsafeMutableRawPointer(mutating: memory.baseAddress), &ncid)
}
return NcId(ncid)
}

/// Open an existing NetCDF file from memory
static func open(memory: UnsafeRawBufferPointer, datasetName: String) throws -> NcId {
return try open(path: datasetName, memory: memory, omode: 0)
}

/// Open an existing NetCDF file
static func open(path: String, allowUpdate: Bool) throws -> NcId {
return try open(path: path, omode: allowUpdate ? NC_WRITE : 0)
}
Expand Down
27 changes: 27 additions & 0 deletions Sources/SwiftNetCDF/NetCDF.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,31 @@ public final class NetCDF {
return nil
}
}

/**
Open a netCDF file with the contents taken from a block of memory.
This function opens an existing netCDF dataset for access. It determines the underlying file format automatically. Use the same call to open a netCDF classic or netCDF-4 file.
- Parameters:
- memory: A memory buffer with NetCDF data. Please ensure that the memory adress remaind valid while using the NetCDF handle.
- datasetName: Can be set to specifiy the name of the dataset
- Throws:
- `NetCDFError.noPermissions` Attempting to open a netCDF file in a directory where you do not have permission to open files.
- `NetCDFError.tooManyOpenFiles` Too many files open
- `NetCDFError.outOfMemory` Out of memory
- `NetCDFError.hdf5Error` HDF5 error. (NetCDF-4 files only.)
- `NetCDFError.netCDF4MetedataError` Error in netCDF-4 dimension metadata. (NetCDF-4 files only.)
- Returns: Root group of a NetCDF file or nil if memory cannot be opened as a netcdf handle
*/
public static func open(memory: UnsafeRawBufferPointer, datasetName: String = "dataset") throws -> Group? {
do {
let ncid = try Nc.open(memory: memory, datasetName: datasetName)
return Group(ncid: ncid, parent: nil)
} catch (NetCDFError.noSuchFileOrDirectory) {
return nil
}
}
}
12 changes: 12 additions & 0 deletions Tests/SwiftNetCDFTests/SwiftNetCDFTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ final class SwiftNetCDFTests: XCTestCase {
let data2 = try typedVariable.read(offset: [1,1], count: [2,2])

XCTAssertEqual([6, 7, 11, 12], data2)

// Test in memory access
let memory = try Data(contentsOf: URL(fileURLWithPath: "test.nc"))
try memory.withUnsafeBytes({ memory in
guard let file2 = try NetCDF.open(memory: memory) else {
fatalError("File test.nc does not exist")
}
guard let title: String = try file2.getAttribute("TITLE")?.read() else {
fatalError("TITLE attribute not available or not a String")
}
XCTAssertEqual(title, "My data set")
})
}

func testCreateGroups() throws {
Expand Down

0 comments on commit 6c75b6b

Please sign in to comment.