NetCDF get_dims 太多值无法解包
NetCDF get_dims too many values to unpack
我正在使用 NetCDF 文件 (.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)
为了将文件转换为 CSV,我首先找到它的属性
# Find the attributes
print(f.variables.keys())
Output: dict_keys(['longitude', 'latitude', 'expver', 'time', 'd2m'])
然后,提取变量并在尝试获取维度时
# 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][:]
我收到以下错误
time_dim, lat_dim, lon_dim = d2m.get_dims()
ValueError: too many values to unpack (expected 3)
这是怎么回事,我该如何解决?
编辑 1
print(d2m.get_dims())
的输出是
(<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)
编辑 2
df.head()
对于
你似乎有超过 3 个维度。因此,这应该会失败:
time_dim, lat_dim, lon_dim = d2m.get_dims()
您只需要检查 d2m.get_dims()
给您的内容,然后修改该行。
转换为 csv 的更快方法是使用 xarray:
import xarray as xr
import pandas as pd
ds = xr.open_dataset(file_location)
df = ds.to_dataframe().reset_index()
# subset the dataframe etc.
df.to_csv(filename, index=False)
作为 @user2856 said,我得到了元组解包代码所期望的暗淡数。
为了得到合适的调光量,我运行
print(d2m.get_dims())
输出是
(<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)
因此我只是将出错的行调整为
time_dim, expver_dim, lat_dim, lon_dim = d2m.get_dims()
然后一切运行顺利,包括转换为.CSV。
我正在使用 NetCDF 文件 (.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)
为了将文件转换为 CSV,我首先找到它的属性
# Find the attributes
print(f.variables.keys())
Output: dict_keys(['longitude', 'latitude', 'expver', 'time', 'd2m'])
然后,提取变量并在尝试获取维度时
# 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][:]
我收到以下错误
time_dim, lat_dim, lon_dim = d2m.get_dims()
ValueError: too many values to unpack (expected 3)
这是怎么回事,我该如何解决?
编辑 1
print(d2m.get_dims())
的输出是
(<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)
编辑 2
df.head()
对于
你似乎有超过 3 个维度。因此,这应该会失败:
time_dim, lat_dim, lon_dim = d2m.get_dims()
您只需要检查 d2m.get_dims()
给您的内容,然后修改该行。
转换为 csv 的更快方法是使用 xarray:
import xarray as xr
import pandas as pd
ds = xr.open_dataset(file_location)
df = ds.to_dataframe().reset_index()
# subset the dataframe etc.
df.to_csv(filename, index=False)
作为 @user2856 said,我得到了元组解包代码所期望的暗淡数。
为了得到合适的调光量,我运行
print(d2m.get_dims())
输出是
(<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)
因此我只是将出错的行调整为
time_dim, expver_dim, lat_dim, lon_dim = d2m.get_dims()
然后一切运行顺利,包括转换为.CSV。