Skip to content

Commit

Permalink
Update legacy parser & Remove '@Assert' (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliascarv committed Mar 28, 2024
1 parent 6c592ae commit 3faebec
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
4 changes: 3 additions & 1 deletion src/extended.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ function load(file::AbstractString)

# domain type
domtype = nextline(io)
@assert domtype DOMTYPES "invalid domain type"
if domtype DOMTYPES
error("invalid domain type")
end

# load data
if domtype == "grid"
Expand Down
39 changes: 19 additions & 20 deletions src/legacy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ function load_legacy(
# handle missing values
replace!(spec.data, na => NaN)

# create data dictionary
values = (; zip(spec.varnames, eachcol(spec.data))...)
# create table and domain
table = (; zip(spec.varnames, eachcol(spec.data))...)
domain = CartesianGrid(dims, origin, spacing)

georef(values, domain)
georef(table, domain)
end

"""
Expand All @@ -62,30 +62,27 @@ end
Load legacy GSLIB `filename` into a point set using the properties in
`coordnames` as coordinates. Optionally set the value used for missings `na`.
"""
function load_legacy(filename::AbstractString, coordnames=(:x, :y, :z); na=-999)
function load_legacy(filename::AbstractString, coordnames=[:x, :y, :z]; na=-999)
spec = parse_legacy(filename)

@assert coordnames spec.varnames && !isempty(coordnames) "invalid coordinate names"
if isempty(coordnames) || coordnames spec.varnames
throw(ArgumentError("invalid coordinate names"))
end

# handle missing values
replace!(spec.data, na => NaN)

# we need to identify and separate coordinates and actual attributes
coordinds = indexin(collect(coordnames), spec.varnames)
coords = spec.data[:, coordinds]'

# create table with varnames not in coordnames
attrinds = setdiff(1:length(spec.varnames), coordinds)
attrnames = spec.varnames[attrinds]
values = (; zip(attrnames, eachcol(spec.data[:, attrinds]))...)
domain = PointSet(coords)
# create table
table = (; zip(spec.varnames, eachcol(spec.data))...)

georef(values, domain)
georef(table, coordnames)
end

# low level function for saving data to a legacy GSLIB format
function save_legacy(filename::AbstractString, data::AbstractMatrix, varnames::NTuple, header::AbstractString, na)
@assert size(data, 2) == length(varnames) "invalid data for the specified variable names"
if size(data, 2) length(varnames)
throw(ArgumentError("invalid data for the specified variable names"))
end
nvars = size(data, 2)

# handle missing values
Expand All @@ -109,17 +106,19 @@ Save spatial `data` to `filename` in legacy GSLIB format. Optionally, specify th
"""
function save_legacy(
filename::AbstractString,
data::AbstractGeoTable;
geotable::AbstractGeoTable;
coordnames=(:x, :y, :z),
header="# This file was generated with GslibIO.jl",
na=-999.0
)
dom = domain(data)
tab = values(data)
dom = domain(geotable)
tab = values(geotable)

if dom isa PointSet
# add coordinates to data and coordnames to varnames
@assert embeddim(dom) == length(coordnames) "the length of coordinate names must be equal to the coordinate dimension"
if embeddim(dom) length(coordnames)
throw(ArgumentError("the length of coordinate names must be equal to the coordinate dimension"))
end
varnames = [coordnames...; propertynames(tab)...]
xs = (coordinates(centroid(dom, i)) for i in 1:nelements(dom))
X = reduce(hcat, xs)'
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ savedir = mktempdir()
@test isequal(por, [0.1, 0.2, 0.3, 0.4])

# test when coordnames are not in varnames
@test_throws AssertionError GslibIO.load_legacy(fname, (:x, :y, :Elevation))
@test_throws ArgumentError GslibIO.load_legacy(fname, (:x, :y, :Elevation))

# test if storing/loading recovers data
fname = tempname() * ".gslib"
Expand Down

0 comments on commit 3faebec

Please sign in to comment.