@@ -10,6 +10,7 @@ export AbstractDataVariable
1010export Project, Instrument, DataSet, LDataSet, Product
1111export Event
1212export AbstractCoordinateSystem, AbstractCoordinateVector, getcsys
13+ export getmeta, setmeta!, setmeta
1314
1415include (" utils.jl" )
1516include (" metadata.jl" )
@@ -25,8 +26,25 @@ include("coord.jl")
2526include (" workload.jl" )
2627
2728# Interface
28- name (v) = @getfield v :name get (meta (v), " name" , " " )
29- meta (v) = @getfield v (:meta , :metadata ) NoMetadata ()
29+ name (v) = @getfield v :name getmeta (v, " name" , " " )
30+
31+ """
32+ getmeta(x)
33+
34+ Get metadata for object `x`. If `x` does not have metadata, return `NoMetadata()`.
35+
36+ """
37+ getmeta (x) = @getfield x (:meta , :metadata ) NoMetadata ()
38+
39+ """
40+ getmeta(x, key, default=nothing)
41+
42+ Get metadata value associated with object `x` for key `key`, or `default` if `key` is not present.
43+ """
44+ getmeta (x, key, default= nothing ) = get (meta (x), key, default)
45+
46+ meta (x) = getmeta (x) # not exported (to be removed)
47+
3048units (v) = @get (v, " units" , nothing )
3149times (v) = @getfield v (:times , :time )
3250
@@ -35,4 +53,43 @@ function unit(v)
3553 allequal (us) ? only (us) : error (" Units are not equal: $us " )
3654end
3755
56+ """
57+ setmeta!(x, key => value, ...; symbolkey => value2, ...)
58+ setmeta!(x, dict::AbstractDict)
59+
60+ Update metadata for object `x` in-place and return `x`. The metadata container must be mutable.
61+
62+ The arguments could be multiple key-value pairs or a dictionary of metadata; keyword arguments are also accepted.
63+
64+ # Examples
65+ ```julia
66+ setmeta!(x, :units => "m/s", :source => "sensor")
67+ setmeta!(x, Dict(:units => "m/s", :quality => "good"))
68+ setmeta!(x; units="m/s", calibrated=true)
69+ ```
70+
71+ Throws an error if the metadata is not mutable. Use `setmeta` for immutable metadata.
72+ """
73+ function setmeta! end
74+
75+ function setmeta! (x, args... ; kw... )
76+ m = meta (x)
77+ ismutable (m) || error (" Metadata is not mutable, use `setmeta` instead" )
78+ set! (m, args... ; kw... )
79+ return x
80+ end
81+
82+ """
83+ setmeta(x, key => value, ...; symbolkey => value2, ...)
84+ setmeta(x, dict::AbstractDict)
85+
86+ Update metadata for object `x` for key `key` to have value `value` and return `x`.
87+ """
88+ function setmeta end
89+
90+ function setmeta (x, args:: Pair... ; kw... )
91+ return @set x. metadata= _merge (meta (x), Dict (args... ), kw)
3892end
93+ setmeta (x, dict:: AbstractDict ) = @set x. metadata= _merge (meta (x), dict)
94+
95+ end
0 commit comments