0

I have been fiddling with the netCDF4 package for a while, and no matter what I try, I am unable to load the variable names. I will show you what I mean by it. The simple code I have written is as follows:

import netCDF4 as netcdf
import numpy as np

f = netcdf.Dataset("pressureOutput/QOUT_500.Netcdf","r")
   
variablesList = f.variables

print(variablesList)

The output on the console is:

{'variables0': <class 'netCDF4._netCDF4.Variable'>
float64 variables0(dim0)
    name: u
unlimited dimensions: 
current shape = (163840,)
filling on, default _FillValue of 9.969209968386869e+36 used, 'variables1': <class 'netCDF4._netCDF4.Variable'>
float64 variables1(dim0)
    name: v
unlimited dimensions: 
current shape = (163840,)
filling on, default _FillValue of 9.969209968386869e+36 used, 'variables2': <class 'netCDF4._netCDF4.Variable'>
float64 variables2(dim0)
    name: rho
unlimited dimensions: 
current shape = (163840,)
filling on, default _FillValue of 9.969209968386869e+36 used, 'variables3': <class 'netCDF4._netCDF4.Variable'>
float64 variables3(dim0)
    name: p
unlimited dimensions: 
current shape = (163840,)
filling on, default _FillValue of 9.969209968386869e+36 used, 'variables4': <class 'netCDF4._netCDF4.Variable'>
float64 variables4(dim0)
    name: YH2
unlimited dimensions: 
current shape = (163840,)
filling on, default _FillValue of 9.969209968386869e+36 used, 'variables5': <class 'netCDF4._netCDF4.Variable'>
( MORE OUTPUT)
(...)
(...)
filling on, default _FillValue of 9.969209968386869e+36 used, 'variables24': <class 'netCDF4._netCDF4.Variable'>
float64 variables24(dim0)
    name: heatRelease
unlimited dimensions: 
current shape = (163840,)
filling on, default _FillValue of 9.969209968386869e+36 used}

As one can see, the variable 'variables0' has a property named name, which should output u. I want to retrieve this information for every one of the variables (u, v, rho, p, etc.). However, if I try

f['variables0'].name

I get 'variables0' as an output. How can I retrieve this information from the file?

1 Answer 1

1

You should be able to solve this problem by converting it to a list:

list(variablesList)
Sign up to request clarification or add additional context in comments.

Comments

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.