我正在尝试使用 xarray 或 netCDF4 库将 netCDF 加载到数据框中。通常这不会成为问题,因为我的 netCDF 大部分都带有 Float32 格式的纬度、经度和数据值。我假设我的错误是因为我有一些数据类型作为 Float64 传递。
我目前在加载时从两个库中收到相同的错误,大概是因为它们都使用了 numpy。我没有做任何数学计算 - 只是加载。
numpy.core._exceptions.UFuncTypeError: Cannot cast ufunc 'multiply' output from dtype('<U32')
to dtype('float32') with casting rule 'same_kind'
使用 print(netCDF4.Dataset("d: etdcdf.nc") 产生以下描述:
dimensions(sizes): time(1), lon(841), lat(681)
variables(dimensions): float64 time(time), float64 lon(lon),
float64 lat(lat), int32 crs(), float32 deadpool(time, lat, lon)
我的脚本如下,包括 xarray 和 netCDF4 的加载示例。
#This file is designed to convert netcdf files to the BOM standard format.
import netCDF4
import pandas as pd
import xarray as xr
def main():
pass
if __name__ == '__main__':
inputfile = 'D:\\Temp\\WeatherDownloads\\Weather\\deadpool.aus.nc'
#xarray setup, debug and load
ncx = xr.open_dataset(inputfile)
ncdf = ncx.deadpool.to_dataframe() #fails here if we use xarray
print(ncdf.head(10))
#NetCDF4 setup, debug and load
nc = netCDF4.Dataset(inputfile, mode='r')
nc.variables.keys()
lat = nc.variables['lat'][:]
lon = nc.variables['lon'][:]
time = nc.variables['time']
datavar = nc.variables['deadpool'][:] #fails here if we use netCDF4
print("The dtype of lat is: " + str(dtype(lat)))
print("The dtype of lon is: " + str(dtype(lon)))
print("The dtype of time is: " + str(dtype(time)))
print("The dtype of datavar is: " + str(dtype(datavar)))
data_ts = pd.Series(datavar, index=time)
print(data_ts.head(10))
我刚刚在查看 netCDF 文件 + xarray 时遇到了类似的问题。我得到的错误是
UFuncTypeError: ufunc 'multiply' did not contain a loop with signature matching types (dtype('float32'), dtype('<U5')) -> None here
虽然错误是相同的,但我不能 100% 声明我得到的正是你得到的。但我可以描述我发现的内容,也许它可以解决您的问题。
我的问题与解码有关。根据 CF 约定,某些字段变量的属性中带有
_FillValue
、scale
和 add_offset
。这些通常是 int 或 float 类型。您可以通过执行以下操作来检查 xarray 中的值:
ds = xr.open_dataset(filename)
ds[varName].attributes # this returns a dictionary
其中 varName 是一些字段数值变量,例如温度、速度等。
我的情况的问题,也可能是您的情况,是该变量属性中
_FillValue
或 scale
的值是字符串。它们应该是数字...
xarray
有时对于调试此类问题可能会很棘手,因为xarray
成功创建了数据集的惰性视图,并且只有在您尝试加载/绘制数据时才会触发scale/fill_value/add_offset
操作尽管用户端没有执行数学运算,但还是出现了错误。
就我而言,这有效:
ds = xr.open_dataset(filename, mask_and_scale=False) #
ds[var].attributes[attr] = float(ds[var].attributes[attr])
nds = xr.decode(ds)
其中
attr
是 [Fill_value, scale, add_offset]
相关操作之一,或全部。您想要检查数据变量的 datatype
以及哪些属性具有字符串类型而不是数字类型。在这种情况下,将 string
转换为 float
是可行的,但您应该可能匹配数据类型。