Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: accept NamedTuple as attrib ? #241

Closed
ryofurue opened this issue Dec 7, 2023 · 2 comments
Closed

Feature Request: accept NamedTuple as attrib ? #241

ryofurue opened this issue Dec 7, 2023 · 2 comments

Comments

@ryofurue
Copy link

ryofurue commented Dec 7, 2023

Describe the problem

NCDatasets v0.13.1

I've been finding OrderedDict( . . .) a bit too bulky and also you have to always use OrderedCollections, and so I use Dict instead as I don't care about the order of the attributes. But Dict is still a bit bulky including the fact you have to quote the name of the attribute. I would think that NamedTuple is ideally suited for attributes:

defVar(ds,...,attrib=(units="m/s", _FillValue=NaN))

Because we wouldn't have thousands of attributes, we don't need the performance of Dict. (If anything, I guess that NamedTuple is a bit more performant for a small number of attributes. Not that performance matters, though.)

(But, looking at the error message when I used a NamedTuple, I guess I should request OrderedDict to be able to be initialized with a NamedTuple?)

[As an aside, it would also be nice if Dict(:units => "m/s") be accepted. Your IDE will color a Symbol differently from a String, which makes the key-value pair more readable (unless you have a Symbol value :-). ]

@Alexander-Barth
Copy link
Member

Yes, I agree. This is a good idea.

It is implemented in JuliaGeo/CommonDataModel.jl@6eb7d80

The following code should work now with the main branch:

# NCDatasets issue #241

filename = tempname()
ds = NCDataset(filename,"c")
data = randn(4,5)

defVar(ds,"temperature",data,("lon","lat"),
       attrib = OrderedDict(:long_name => "Temperature",
                 :test_vector_attrib => [1,2,3]))

defVar(ds,:temperature2,data,(:lon,:lat),
       attrib = OrderedDict(:long_name => "Temperature",
                 :test_vector_attrib => [1,2,3]))


defVar(ds,:temperature3,data,(:lon,:lat),
       attrib = Dict(:long_name => "Temperature",
                 :test_vector_attrib => [1,2,3]))

defVar(ds,:temperature4,data,(:lon,:lat),
       attrib = (long_name = "Temperature",
                 test_vector_attrib = [1,2,3]))

for (vn,var) in ds
    @test startswith(vn,"temperature")
    @test var.attrib["long_name"] == "Temperature"
    @test var.attrib["test_vector_attrib"] == [1,2,3]
end

@Alexander-Barth
Copy link
Member

closing as completed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants