我在不同的10个NETCDF文件中有年度时间序列数据。文件形状是:
value.shape =
(365, 310, 250)
我尝试制作一个形状如下的数组:
(3650, 310,250)
将年度数据附加到其中。我使用了这个功能,但没有奏效:
files = glob.glob('/home/user/data/*.nc')
time_series = np.array([[]])
for i in files:
yearly = Dataset(i,'r')
value = yearly.variables['AOD'][:,:,:]
time_series = np.append(time_series,value)
任何帮助都会受到高度关注。
答案 0 :(得分:2)
如果您在开始时知道合并数组的大小,则创建正确大小的Numpy数组要容易得多。例如(我缩小了尺寸..):
import numpy as np
merged_values = np.empty((3650, 31, 25))
for i in range(10):
value = np.random.random((365, 31, 25))
merged_values[i*365:(i+1)*365] = value
答案 1 :(得分:2)
以下是我将如何更改您的代码:
def make_array(loc, shape = (365, 31, 25)):
# loc is string, can have any number of files, can change shape of data
files = glob.glob(str(loc) + '/*.nc')
time_series = np.empty((len(files),) + shape) # create an extra dimension for files, 'np.empty' doesn't waste time initializing
for i, j in enumerate(files): # enumerate gives you indices
yearly = Dataset(j, 'r')
time_series[i] = yearly.variables['AOD'][:, :, :]
return time_series.reshape(*((-1,) + shape[1:])) # -1 allows size to change