[英]Total Size of New Array Must Be Unchanged (netCDF)
我在將CSV文件轉換為NetCDF時遇到問題。 我在python中使用numpy和netCDF4嘗試實現這一目標。
錯誤:
Traceback (most recent call last):
File "csv2nc.py", line 47, in <module>
tmaxsfc[:] = temps
File "netCDF4.pyx", line 3167, in netCDF4.Variable.__setitem__ (netCDF4.c:39349)
ValueError: total size of new array must be unchanged
腳本:
import numpy as np
import netCDF4
data = np.loadtxt('/home/weather/Dropbox/d0_tmaxs', delimiter=',', skiprows=0)
temp = data[:,1]
lat = data[:,2]
lon = data[:,3]
with netCDF4.Dataset('tmaxsfc.nc', mode="w", format='NETCDF4') as ds:
# some file-level meta-data attributes:
ds.Conventions = "CF-1.6"
ds.title = 'Maximum Temperature values for Day 0'
ds.institution = 'weather'
ds.source = ''
#print(lon.shape)
lats = np.array(lat,np.float32).reshape(255)
lons = np.array(lon,np.float32).reshape(255)
temps = np.array(temp,np.float32).reshape(255)
ds.createDimension('latitude', 255)
ds.createDimension('longitude', 255)
tmaxsfc = ds.createVariable('tmaxsfc', 'f4', ('latitude', 'longitude',))
tmaxsfc[:] = temps
tmaxsfc.units = 'F'
tmaxsfc.long_name = 'Temperature'
d0_tmaxs文本文件示例:
20160226,40,36.65408,-83.21783
20160226,35.1508,41.00928,-74.73628
20160226,31,43.77714,-71.75598
20160226,23.8302,44.41944,-72.01944
20160226,22,39.5803,-79.3394
對於它的價值,文本文件有255行。 我究竟做錯了什么?
我對netcdf
有點生疏,但沒有
tmaxsfc = ds.createVariable('tmaxsfc', 'f4', ('latitude', 'longitude',))
意思是tmaxsfc
是一個二維數組,255乘255? 您是在經度緯度網格上定義它的。
但是看起來您的輸入將顯示在散點圖上,共255個點,每個點由緯度,經度和溫度定義。
因此,錯誤來自於temp
是具有255個元素的1D數組,但是tmaxsfc
變量被定義為2D變量。 您想要做的是創建一個單一的維度(例如station
),所有3個1D變量都可以是以下函數:
import numpy as np
import netCDF4
data = np.loadtxt('/home/weather/Dropbox/d0_tmaxs', delimiter=',', skiprows=0)
temp = np.array(data[:,1], dtype=np.float32).reshape(255)
lat = np.array(data[:,2], dtype=np.float32).reshape(255)
lon = np.array(data[:,3], dtype=np.float32).reshape(255)
with netCDF4.Dataset('tmaxsfc.nc', mode="w", format='NETCDF4') as ds:
# some file-level meta-data attributes:
ds.Conventions = "CF-1.6"
ds.title = 'Maximum Temperature values for Day 0'
ds.institution = 'weather'
ds.source = ''
ds.createDimension('station', 255)
dims = ('station',)
tmaxsfc = ds.createVariable('tmaxsfc', np.float32, dims)
tmaxsfc[:] = temps
tmaxsfc.units = 'F'
tmaxsfc.long_name = 'Temperature'
lon_var = ds.createVariable('longitude', np.float32, dims)
lon_var[:] = lon
lon.units = 'degrees_east'
lat_var = ds.createVariable('latitude', np.float32, dims)
lat_var[:] = lat
lat.units = 'degress_north'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.