【发布时间】:2018-07-18 01:03:59
【问题描述】:
我正在尝试从包含三列(纬度、经度和降水)值的 excel 文件创建一个 nc 文件。数据是 0.05 度分辨率的网格值。网格总数为 1694。数据范围为 57 列和 52 行。我不知道如何加载降水变量?
我试过这样。
from netCDF4 import *
from numpy import *
from openpyxl import *
import time
#Load the data sheet
wb = load_workbook('D:\\R_Workspace\\UB_Try.xlsx')
ws = wb['UB_details']
#To get the prec variable
ppt = []
for i in range(2,1696):
ppt.append(ws.cell(row=i,column=3).value)
ppt_arr = asarray(ppt)
#Writing nc file
nc_file = Dataset('D:\\R_Workspace\\Test.nc','w',format='NETCDF4_CLASSIC')
nc_file.description = 'Example dataset'
nc_file.history = 'Created on ' +time.ctime(time.time())
#Defining dimensions
lon = nc_file.createDimension('lon',57)
lat = nc_file.createDimension('lat',52)
#Creating variables
latitude = nc_file.createVariable('Latitude',float32,('lat',))
latitude.units = 'Degree_North'
longitude = nc_file.createVariable('Longitude',float32,('lon',))
longitude.units = 'degree_East'
prec = nc_file.createVariable('prec',float32,('lon','lat'),fill_value = -9999.0)
prec.units = 'mm'
#Writing data to variables
lats = arange(16.875,19.425,0.05)
lat_reverse = lats[::-1]
lons = arange(73.325,76.175,0.05)
latitude[:] = lat_reverse
longitude[:] = lons
prec[:] = ppt_arr
nc_file.close()
我收到了错误
Traceback (most recent call last):
File "D:\R_Workspace\Try.py", line 45, in <module>
prec[:] = ppt_arr
File "netCDF4\_netCDF4.pyx", line 4358, in netCDF4._netCDF4.Variable.__setitem__
ValueError: cannot reshape array of size 1694 into shape (57,52)
【问题讨论】:
-
请指定它不工作。显示回溯(如果有)或错误的输出等,即提供Minimal, Complete, and Verifiable example。
-
谢谢@MikeMüller 我在查询中提到了引用。实际上我的 excel 表有三列和 1964 行,不包括标题。
-
网格总数为1694,数据范围为57列52行。但是:
57 * 52 = 2964 -
是的@MikeMüller。实际上它是一个空间数据,只有 1694 个网格有值。没有数据
-
@MikeMüller 我只是用你的行替换,但是先生,这写的都是零值