1
我使用的是for
循环在目录中打开12个文件中,每个代表一个月份的一年,形状如图所示:结合numpy的阵列循环
(31, 180, 140)
(28, 180, 140)
(31, 180, 140)
(30, 180, 140)
(31, 180, 140)
(30, 180, 140)
(31, 180, 140)
(31, 180, 140)
(30, 180, 140)
(31, 180, 140)
(30, 180, 140)
(31, 180, 140)
我试图用append
来这些文件合并成一个列表,如图所示:
directory = r"C:\Users\matth\Downloads\TRMM_3B42RT"
for root, dirs, filenames in os.walk(directory):
precip_subsetland2010 = []
for f in filenames:
if f.startswith("3B42RT_Daily.2010"):
log = open(os.path.join(root, f), 'r')
datapath2 = (("C:\\Users\\matth\\Downloads\\TRMM_3B42RT\\") + f)
f = Dataset(datapath2)
latbounds = [ -45 , -10 ]
lonbounds = [ 105, 150 ]
lats = f.variables['lat'][:]
lons = f.variables['lon'][:]
# latitude lower and upper index
latli = np.argmin(np.abs(lats - latbounds[0]))
latui = np.argmin(np.abs(lats - latbounds[1]))
# longitude lower and upper index
lonli = np.argmin(np.abs(lons - lonbounds[0]))
lonui = np.argmin(np.abs(lons - lonbounds[1]))
precip_subset = f.variables['precipitation'][ : , lonli:lonui , latli:latui ]
precip_subsetland2010.append(precip_subset)
precipsubsetland2010 = np.asarray(precip_subsetland2010)
print(precipsubsetland2010.shape)
不过,我得到的输出如下所示:
OUTPUT:
(1, 31, 180, 140)
(2,)
(3,)
(4,)
(5,)
(6,)
(7,)
(8,)
(9,)
(10,)
(11,)
(12,)
我最终希望将数据添加到列表中,然后转换为形状为(365, 180, 140)
的数组。我怎样才能做到这一点?这是append
的正确应用吗?
你们需要更多的信息来解答吗? –
'precip_subset'有你提到的形状吗?打印出它的形状。 –
@cᴏʟᴅsᴘᴇᴇᴅ:我在开头打印的形状列表是12个文件的“沉淀物集”的形状。 –