微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

NetCDF Python 无法将 ufunc 'multiply' 输出从 dtype('<U32') 转换为 dtype('float32')

如何解决NetCDF Python 无法将 ufunc 'multiply' 输出从 dtype('<U32') 转换为 dtype('float32')

我正在尝试使用 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:\netdcdf.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))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。