Hi all.
I have a binary file that I need to load to julia. I have a python scrip that reads it, can anyone help me a bit to turn this to julia, it is not working for me
# omit the first 4 values (header information) and reshape
dtype = np.dtype([
("x", "<f4"),
("y", "<f4"),
("z", "<f4"),
("pdf", "<f4")])
data = np.fromfile(filename, dtype=dtype)[4:]
if coordinate_converter:
data["x"], data["y"], data["z"] = coordinate_converter(
data["x"], data["y"], data["z"])
return data```
I donβt have some test data to be certain, but perhaps this could work:
struct Data{T}
x::T
y::T
z::T
pdf::T
end
function read_file(path; coordinate_conversion=nothing)
data = Data{Float32}[]
open(path, "r") do io
# buffer of raw bytes for each item of data
buffer = Vector{UInt8}(undef, sizeof(Data{Float32}))
while !eof(io)
readbytes!(io, buffer)
push!(data, (reinterpret(Data{Float32}, buffer)[1]))
end
end
data = @views data[5:end]
if !isnothing(coordinate_conversion)
data .= coordinate_conversion.(data) # convert in-place
end
return data
end
I believe reinterpret is little endian for the most part (see this post).
This isnβt the most performant implementation as it keeps resizing the array, but it should get you part of the way there.
Hi Joaquim!. The last column should be between 0 and 1 if the manual es correct. Quote from the quotes manual:
Scatter file (Binary , FileExtension=*.scat )The Scatter file contains the x,y,z locations and PDF value of each sample of the location PDF. The number of samples to save is specified in the LOCSEARCH statement in the NLLoc Statements section of the Input Control File.Header: (required ) one integer and 3float valuesnSamples dummy dummy dummyFields: nSamples (integer )
number of PDF samples in the following buffer dummy (float )
unusedBuffer: (required ) Sequence of four float values for each PDF samplex(N), y(N), z(N), pdf(N) (N = 0, nSamples - 1)Fields: x(N), y(N), z(N) (float ) Non-GLOBAL: x, y and z location of the sample in kilometers relative to the geographic origin. GLOBAL: longitude and latitude in degrees, and z location in kilometers of the sample. pdf(N) (float )
PDF value of the sample, normalized so that the volume integral over the corresponding search grid of the PDF = 1.0