I am working with a NetCDF file (.nc) - 600+MB.
import netCDF4
from netCDF4 import num2date
import numpy as np
import os
import pandas as pd
# Open netCDF4 file
file_location = '2m dewpoint temperature.nc'
f = netCDF4.Dataset(file_location)
In order to convert the file to CSV I started by finding its attributes
# Find the attributes
print(f.variables.keys())
Output: dict_keys(['longitude', 'latitude', 'expver', 'time', 'd2m'])
Then, extracted the variable and when trying to get the dimensions
# Extract variable
d2m = f.variables['d2m']
# Get dimensions
time_dim, lat_dim, lon_dim = d2m.get_dims()
time_var = f.variables[time_dim.name]
times = num2date(time_var[:], time_var.units)
latitudes = f.variables[lat_dim.name][:]
longitudes = f.variables[lon_dim.name][:]
I am getting the following error
time_dim, lat_dim, lon_dim = d2m.get_dims()
ValueError: too many values to unpack (expected 3)
What is happening here and how should I solve it?
Edit 1
The output of print(d2m.get_dims())
is
(<class 'netCDF4._netCDF4.Dimension'>: name = 'time', size = 94750, <class 'netCDF4._netCDF4.Dimension'>: name = 'expver', size = 2, <class 'netCDF4._netCDF4.Dimension'>: name = 'latitude', size = 33, <class 'netCDF4._netCDF4.Dimension'>: name = 'longitude', size = 53)
Edit 2
df.head()
for @RobertWilson's suggestion
As @user2856 said, I was getting the number of dims the tuple unpacking code was expecting.
In order to get the right amount of dims, I ran
print(d2m.get_dims())
The output was
(<class 'netCDF4._netCDF4.Dimension'>: name = 'time', size = 94750, <class 'netCDF4._netCDF4.Dimension'>: name = 'expver', size = 2, <class 'netCDF4._netCDF4.Dimension'>: name = 'latitude', size = 33, <class 'netCDF4._netCDF4.Dimension'>: name = 'longitude', size = 53)
Therefore I just adjusted the line that was giving error to
time_dim, expver_dim, lat_dim, lon_dim = d2m.get_dims()
Then everything ran smoothly, including the conversion to .CSV.