@@ -10,6 +10,7 @@ export AbstractDataVariable
1010export Project, Instrument, DataSet, LDataSet, Product
1111export Event
1212export AbstractCoordinateSystem, AbstractCoordinateVector, getcsys
13+ export setmeta!, setmeta
1314
1415include (" utils.jl" )
1516include (" metadata.jl" )
@@ -26,7 +27,14 @@ include("workload.jl")
2627
2728# Interface
2829name (v) = @getfield v :name get (meta (v), " name" , " " )
29- meta (v) = @getfield v (:meta , :metadata ) NoMetadata ()
30+
31+ """
32+ meta(x)
33+
34+ Get metadata for object `x`. If `x` does not have metadata, return `NoMetadata()`.
35+ """
36+ meta (x) = @getfield x (:meta , :metadata ) NoMetadata ()
37+
3038units (v) = @get (v, " units" , nothing )
3139times (v) = @getfield v (:times , :time )
3240
@@ -35,4 +43,43 @@ function unit(v)
3543 allequal (us) ? only (us) : error (" Units are not equal: $us " )
3644end
3745
46+ """
47+ setmeta!(x, key => value, ...; symbolkey => value2, ...)
48+ setmeta!(x, dict::AbstractDict)
49+
50+ Update metadata for object `x` in-place and return `x`. The metadata container must be mutable.
51+
52+ The arguments could be multiple key-value pairs or a dictionary of metadata; keyword arguments are also accepted.
53+
54+ # Examples
55+ ```julia
56+ setmeta!(x, :units => "m/s", :source => "sensor")
57+ setmeta!(x, Dict(:units => "m/s", :quality => "good"))
58+ setmeta!(x; units="m/s", calibrated=true)
59+ ```
60+
61+ Throws an error if the metadata is not mutable. Use `setmeta` for immutable metadata.
62+ """
63+ function setmeta! end
64+
65+ function setmeta! (x, args... ; kw... )
66+ m = meta (x)
67+ ismutable (m) || error (" Metadata is not mutable, use `setmeta` instead" )
68+ set! (m, args... ; kw... )
69+ return x
70+ end
71+
72+ """
73+ setmeta(x, key => value, ...; symbolkey => value2, ...)
74+ setmeta(x, dict::AbstractDict)
75+
76+ Update metadata for object `x` for key `key` to have value `value` and return `x`.
77+ """
78+ function setmeta end
79+
80+ function setmeta (x, args:: Pair... ; kw... )
81+ return @set x. metadata= _merge (meta (x), Dict (args... ), kw)
3882end
83+ setmeta (x, dict:: AbstractDict ) = @set x. metadata= _merge (meta (x), dict)
84+
85+ end
0 commit comments