Alison Bennett
Alison Bennett

Reputation: 315

Retrieve value of netCDF global attribute by name in Python

How do I retrieve the value of a global attribute from a netCDF file in Python by its name?

I have tried:

value = ncfile.getncattr(ncfile, global_attribute_name)

but I get the error:

NameError: name 'global_attribute_name' is not defined

I have also read the section of the netCDF4 API documentation that deals with global attributes, but I'm sure there's something I don't understand with the syntax.

Can anyone help? I'm an R user moving over to Python, please be gentle.

Upvotes: 2

Views: 3790

Answers (2)

Thomas Hearty
Thomas Hearty

Reputation: 46

This is how it worked for me:

value = ncfile.getncattr('my_attr')

Upvotes: 3

titusjan
titusjan

Reputation: 5546

global_attribute_name is a parameter. You should replace it with the name of the attribute you are trying to retrieve, wrapped in quotes. So, let's say the name of the attribute is my_attr, then use

value = ncfile.getncattr(ncfile, 'my_attr')

Upvotes: 2

Related Questions