Skip to content

Commit

Permalink
Use DataFrames instead of MetadataArrays (#17)
Browse files Browse the repository at this point in the history
* Use DataFrames instead of MetadataArrays

* Update LICENSE.md
  • Loading branch information
briochemc authored Jun 8, 2021
1 parent 1041c25 commit 373b67d
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 33 deletions.
2 changes: 1 addition & 1 deletion LICENSE → LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 Benoit Pasquier
Copyright (c) 2021 Benoit Pasquier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
9 changes: 5 additions & 4 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
name = "WorldOceanAtlasTools"
uuid = "04f20302-f1b9-11e8-29d9-7d841cb0a64a"
authors = ["Benoit Pasquier <briochemc@gmail.com>"]
version = "0.4.4"
version = "0.5.0"

[deps]
DataDeps = "124859b0-ceae-595e-8997-d05f6a7a8dfe"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
Match = "7eb4fadd-790c-5f42-8a69-bfa0b872bfbf"
MetadataArrays = "49441bc9-da82-574f-b07c-a0d10dd4ac13"
NCDatasets = "85f8d34a-cbdd-5861-8df4-14fed0d494ab"
NearestNeighbors = "b8a86587-4115-5ab1-83bc-aa920d37bbce"
OceanGrids = "cfe838f4-859f-11e9-2ea1-df7d4e7c3537"
Expand All @@ -16,11 +17,11 @@ Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[compat]
DataDeps = "0.7"
DataFrames = "1"
Match = "1"
MetadataArrays = "0.1"
NCDatasets = "0.9, 0.10, 0.11"
NearestNeighbors = "0.4"
OceanGrids = "0.1, 0.2, 0.3"
OceanGrids = "0.1, 0.2, 0.3, 0.4"
StatsBase = "0.33"
Unitful = "1"
julia = "1"
Expand Down
12 changes: 10 additions & 2 deletions src/WorldOceanAtlasTools.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
module WorldOceanAtlasTools

using DataDeps, NCDatasets, Match, Statistics, StatsBase, Unitful
using OceanGrids, NearestNeighbors
using DataDeps
using Downloads
using NCDatasets
using DataFrames
using Match
using Statistics
using StatsBase
using Unitful
using OceanGrids
using NearestNeighbors

include("names.jl")
include("citations.jl")
Expand Down
32 changes: 9 additions & 23 deletions src/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,33 +161,19 @@ end



#==================================
More general API functions
==================================#
# GEOTRACES.jl inspired functions.
# Maybe faster that what I previously had.
# Anyway it would be better to compare interpolated model to obs,
# rather than interpolated obs to model.
# So this is the format of obs I should use.
# obs, MD
# with obs and units
# and MD a named tuple with fields Depth, Latitude, and Longitude
# If MD is used a lot, maybe use depth, lat, lon?
# TODO make this the basic API and let AIBECS use it
# TODO push new version with new API


#=========================================
observations function returns a DataFrames
=========================================#

using MetadataArrays


function observations(ds::Dataset, tracer::String; metadatakeys)
var, v, ikeep = indices_and_var(ds, tracer)
u = _unit(var)
WOAmetadatakeys = varname.(metadatakeys)
metadata = [metadatakeyvaluepair(ds[k], ikeep) for k in WOAmetadatakeys]
metadata = (name="Observed $(WOA_path_varname(tracer))", WOAvarname=name(var), metadata...)
return MetadataVector(float.(v[ikeep]) .* u, metadata)
metadata = (metadatakeyvaluepair(ds[k], ikeep) for k in WOAmetadatakeys)
df = DataFrame(metadata..., Symbol(tracer)=>float.(view(v, ikeep))*u)
return df
end
"""
observations(tracer::String; metadatakeys=("lat", "lon", "depth"))
Expand Down Expand Up @@ -216,9 +202,9 @@ end
_unit(v) = convert_to_Unitful(get(v.attrib, "units", "nothing"))
_fillvalue(v) = get(v.attrib, "_FillValue", NaN)
metadatakeyvaluepair(v, idx) = @match name(v) begin
"lon" => (:lon, float.(v.var[:][[i.I[1] for i in idx]]) * u"°")
"lat" => (:lat, float.(v.var[:][[i.I[2] for i in idx]]) * u"°")
"depth" => (:depth, float.(v.var[:][[i.I[3] for i in idx]]) * u"m")
"lon" => (:lon => float.(v.var[:][[i.I[1] for i in idx]]))
"lat" => (:lat => float.(v.var[:][[i.I[2] for i in idx]]))
"depth" => (:depth => float.(v.var[:][[i.I[3] for i in idx]]) * u"m")
end


Expand Down
2 changes: 1 addition & 1 deletion src/names.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function fallback_download(remotepath, localdir)
@assert(isdir(localdir))
filename = basename(remotepath) # only works for URLs with filename as last part of name
localpath = joinpath(localdir, filename)
Base.download(remotepath, localpath)
Downloads.download(remotepath, localpath)
return localpath
end

Expand Down
5 changes: 3 additions & 2 deletions test/test_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
# new functionality
@testset "observations function" begin
σSW = 1.035u"kg/L" # approximate mean sea water density to convert mol/kg to mol/m^3
PO₄obs = WorldOceanAtlasTools.observations(tracer) * σSW
@test 0.1u"μM" < mean(PO₄obs) < 10u"μM"
obs = WorldOceanAtlasTools.observations(tracer)
obs.value = obs.p .* σSW .|> u"μM"
@test 0.1u"μM" < mean(obs.value) < 10u"μM"
end
end

2 comments on commit 373b67d

@briochemc
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.

Registration pull request created: JuliaRegistries/General/38408

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.0 -m "<description of version>" 373b67d76379997cf5e8b728d6f304c40a11728b
git push origin v0.5.0

Please sign in to comment.