Skip to content

Commit

Permalink
Add and test oxideconversion for tuple datasets
Browse files Browse the repository at this point in the history
  • Loading branch information
brenhinkeller committed Mar 3, 2024
1 parent 03d2bb8 commit a6e14b3
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 1,009 deletions.
49 changes: 44 additions & 5 deletions src/utilities/Geochemistry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,15 @@

## --- Oxide conversions

function fillifnan!(dest::AbstractArray, source::AbstractArray, factor::Number=1.0)
@inbounds for i in eachindex(dest, source)
if isnan(dest[i]) && !isnan(source[i])
dest[i] = source[i] * factor
end
end
return dest
end

"""
```julia
dataset = oxideconversion(dataset::Dict; unitratio::Number=10000)
Expand All @@ -209,6 +218,7 @@
return oxideconversion!(result)
end
export oxideconversion

"""
```julia
dataset = oxideconversion!(dataset::Dict; unitratio::Number=10000)
Expand All @@ -225,18 +235,47 @@
# end

# Array of elements to convert
source = ["Si","Ti","Al","Fe","Fe","Mg","Ca","Mn","Li","Na","K","P","Cr","Ni","Co","C","S","H"]
dest = ["SiO2","TiO2","Al2O3","FeOT","Fe2O3T","MgO","CaO","MnO","Li2O","Na2O","K2O","P2O5","Cr2O3","NiO","CoO","CO2","SO2","H2O"]
conversionfactor = [2.13932704290547,1.66847584248889,1.88944149488507,1.28648836426407,1.42973254639611,1.65825961736268,1.39919258253823,1.29121895771597,2.1526657060518732,1.34795912485574,1.20459963614796,2.29133490474735,1.46154369861159,1.27258582901258,1.27147688434143,3.66405794688203,1.99806612601372,8.93601190476191]
source = ("Si","Ti","Al","Fe","Fe","Mg","Ca","Mn","Li","Na","K","P","Cr","Ni","Co","C","S","H")
dest = ("SiO2","TiO2","Al2O3","FeOT","Fe2O3T","MgO","CaO","MnO","Li2O","Na2O","K2O","P2O5","Cr2O3","NiO","CoO","CO2","SO2","H2O")
conversionfactor = (2.13932704290547,1.66847584248889,1.88944149488507,1.28648836426407,1.42973254639611,1.65825961736268,1.39919258253823,1.29121895771597,2.1526657060518732,1.34795912485574,1.20459963614796,2.29133490474735,1.46154369861159,1.27258582901258,1.27147688434143,3.66405794688203,1.99806612601372,8.93601190476191)

# If source field exists, fill in destination from source
for i eachindex(source)
if haskey(dataset, source[i])
if ~haskey(dataset, dest[i]) # If destination field doesn't exist, make it.
dataset[dest[i]] = fill(NaN, size(dataset[source[i]]))
if haskey(dataset, "elements")
dataset["elements"] = dataset["elements"] (dest[i],)
end
end
oxide = dataset[dest[i]]
metal = dataset[source[i]]
fillifnan!(oxide, metal, conversionfactor[i]/unitratio)
end
end

return dataset
end

function oxideconversion!(dataset::NamedTuple; unitratio::Number=10000)
# Convert major elements (Ti, Al, etc.) into corresponding oxides (TiO2, Al2O3)...
# for i ∈ eachindex(source)
# conversionfactor(i)=mass.percation.(dest[i])./mass.(source[i]);
# end

# Array of elements to convert
source = (:Si, :Ti, :Al, :Fe, :Fe, :Mg, :Ca, :Mn, :Li, :Na, :K, :P, :Cr, :Ni, :Co, :C, :S, :H)
dest = (:SiO2, :TiO2, :Al2O3, :FeOT, :Fe2O3T, :MgO, :CaO, :MnO, :Li2O, :Na2O, :K2O, :P2O5, :Cr2O3, :NiO, :CoO, :CO2, :SO2, :H2O)
conversionfactor = (2.13932704290547,1.66847584248889,1.88944149488507,1.28648836426407,1.42973254639611,1.65825961736268,1.39919258253823,1.29121895771597,2.1526657060518732,1.34795912485574,1.20459963614796,2.29133490474735,1.46154369861159,1.27258582901258,1.27147688434143,3.66405794688203,1.99806612601372,8.93601190476191)

# If source field exists, fill in destination from source
for i eachindex(source)
if haskey(dataset, source[i])
if haskey(dataset, dest[i]) # If destination field doesn't exist, make it.
oxide = dataset[dest[i]]
metal = dataset[source[i]]
fillifnan!(oxide, metal, conversionfactor[i]/unitratio)
end
t = isnan.(dataset[dest[i]]) .& (.~ isnan.(dataset[source[i]]))
dataset[dest[i]][t] = dataset[source[i]][t] .* (conversionfactor[i] / unitratio)
end
end

Expand Down
11 changes: 11 additions & 0 deletions test/testGeochemistry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@
@test all(D["CaO"] .≈ (molarmass["Ca"]+molarmass["O"])/molarmass["Ca"])
@test all(D["P2O5"] .≈ (molarmass["P"]+2.5*molarmass["O"])/molarmass["P"])


DT = elementify(unelementify(D), importas=:Tuple)
for e in (:FeOT, :MgO, :CaO, :P2O5)
DT[e] .= NaN
end
oxideconversion!(DT)
@test all(DT.FeOT .≈ (molarmass["Fe"]+molarmass["O"])/molarmass["Fe"])
@test all(DT.MgO .≈ (molarmass["Mg"]+molarmass["O"])/molarmass["Mg"])
@test all(DT.CaO .≈ (molarmass["Ca"]+molarmass["O"])/molarmass["Ca"])
@test all(DT.P2O5 .≈ (molarmass["P"]+2.5*molarmass["O"])/molarmass["P"])

# Weathering indices
@test CIA(14.8577, 4.5611, 3.29641, 2.3992) 47.66582778067264
@test WIP(3.2964, 4.5611, 2.3992, 5.9121) 78.40320264846837
Expand Down
10 changes: 8 additions & 2 deletions test/testImport.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
@test importdataset("tupledataset.csv", ',', importas=:Tuple, elements=(elements...,), skipstart=1) == datatuple

@test exportdataset(datatuple, "tupledataset.tsv") == nothing
@test importdataset("tupledataset.csv", importas=:Tuple) == datatuple
@test importdataset("tupledataset.tsv", '\t', importas=:Tuple) == datatuple

@test exportdataset(datatuple, "tupledataset.csv", ',', digits=6) == nothing
@test importdataset("tupledataset.csv", ',', importas=:Tuple).Lv == round.(datatuple.Lv, digits=6)
Expand Down Expand Up @@ -151,4 +151,10 @@
@test isa(darray, Array{Float64,2})
@test size(darray) == (5, length(keys(d)))

## ---
## --- Clean up

rm("dictdataset.csv")
rm("tupledataset.csv")
rm("tupledataset.tsv")

## --- End of File
2 changes: 1 addition & 1 deletion test/testUtilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
@test data == [-9999 -9999 5 2; -9999 20 100 36; 3 8 35 10; 32 42 50 6; 88 75 27 9; 13 5 1 -9999]
@test metadata["nodata"] == -9999


rm("grid.asc")

# Random lat-lon generation
@test isa(randlatlon(), Tuple{Float64,Float64})
Expand Down
Loading

2 comments on commit a6e14b3

@brenhinkeller
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error while trying to register: Version 0.6.5 already exists

Please sign in to comment.