2

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.

2 Answers 2

3

This is how it worked for me:

value = ncfile.getncattr('my_attr')
Sign up to request clarification or add additional context in comments.

Comments

2

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')

1 Comment

Thank-you. I did try that, but received another error - probably related to something else in the script I'm working on. I'll accept your answer though. Cheers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.