【发布时间】:2019-10-11 02:05:44
【问题描述】:
这个过程中的主要问题是下面的代码:
precip[:] = orig
产生以下错误:
ValueError: cannot reshape array of size 5732784 into shape (39811,144,144)
我有两个 CSV 文件,一个 CSV 文件包含一个变量(降水)的所有实际数据,每列作为一个站点,它们对应的坐标在第二个单独的 CSV 文件中。 我的样本数据在google drive here。
如果您想查看数据本身,但我的第一个 CSV 文件的形状为 (39811, 144) 而第二个 CSV 文件的形状为 (171, 10) 但请注意;我只将切片数据框用作 (144, 2)。
这是代码:
stations = pd.read_csv(stn_precip)
stncoords = stations.iloc[:,[0,1]][:144]
orig = pd.read_csv(orig_precip, skiprows = 1, names = stations['Code'][:144])
lons = stncoords['X']
lats = stncoords['Y']
ncout = netCDF4.Dataset('Precip_1910-2018_homomod.nc', 'w')
ncout.createDimension('longitude',lons.shape[0])
ncout.createDimension('latitude',lats.shape[0])
ncout.createDimension('precip',orig.shape[1])
ncout.createDimension('time',orig.shape[0])
lons_out = lons.tolist()
lats_out = lats.tolist()
time_out = orig.index.tolist()
lats = ncout.createVariable('latitude',np.dtype('float32').char,('latitude',))
lons = ncout.createVariable('longitude',np.dtype('float32').char,('longitude',))
time = ncout.createVariable('time',np.dtype('float32').char,('time',))
precip = ncout.createVariable('precip',np.dtype('float32').char,('time', 'longitude','latitude'))
lats[:] = lats_out
lons[:] = lons_out
time[:] = time_out
precip[:] = orig
ncout.close()
我的代码主要基于这篇文章:convert-csv-to-netcdf 但不包括变量“时间”作为第三维,所以这就是我失败的地方。 我想我应该期望降水变量具有 (39811, 144, 144) 形式的形状,但错误表明并非如此。
不完全确定如何处理此问题,欢迎提出任何意见。
【问题讨论】:
标签: python pandas csv netcdf netcdf4