5

How do I programmatically list all variables of a NetCDF file that I have read in using netCDF4 and Python?

import netCDF4
dset = netCDF4.Dataset('test.nc')

4 Answers 4

11
dset.variables.keys() 

would provide you with a list of the variables.

Sign up to request clarification or add additional context in comments.

1 Comment

That only lists variables in the root group.
5

From the documentation, here, it looks like it would just be:

import netCDF4
dset = netCDF4.Dataset('test.nc')
dset.variables

1 Comment

That only lists variables in the root group.
2

To list all variable names as strings just do:

list(dset.variables.keys())

or simple

list(dset.variables)

1 Comment

nice! this was the answer i was looking for
2

For a NetCDF file that contains a group structure, it's necessary to access each group. In those cases, to generate and show a list of all the variables, the following has worked for me:

import netCDF4

def expand_var_list(var_list, group):
    for var_key, _ in group.variables.items():
        var_list.append(var_key)
    
    for _, sub_group in group.groups.items():
        expand_var_list(var_list, sub_group)

all_vars = []
with netCDF4.Dataset("test.nc", "r") as root_group:
    expand_var_list(all_vars, root_group)
print(all_vars)

This code adds each group's variables to the overall variable list, before checking to see if there are sub-groups. If there are sub-groups, it does the same with each of those, recursively.

Updated to show group membership (01 May 2023)

As the first commenter pointed out, the initial answer does not show which group each variable was contained within. Here is updated code, that puts the variable names into a dictionary so you can see which variable belongs to which group:

def expand_var_dict(var_dict, group):
    for var_key, _ in group.variables.items():
      if group.name not in var_dict.keys():
        var_dict[group.name] = list()
      var_dict[group.name].append(var_key)
    
    for _, sub_group in group.groups.items():
      expand_var_dict(var_dict, sub_group)

all_vars = dict()
with netCDF4.Dataset("test.nc", "r") as root_group:
  expand_var_dict(all_vars, root_group)
print(all_vars)

The result should be something like this:

{'/': ['root_level_var1', 'root_level_var2'], 
 'group1': ['x', 'y', 'z'],
 'group2': ['k', 'm']
}

1 Comment

Your answer is the first one here which works for a netcdf with a group structure. But your solution will print all variables from all subgroups into a single list, without indicating which variables are from which subgroup. I will consider updating it if I am able to, of if I have time.

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.