简体   繁体   中英

How to efficiently create a 3D array from 2D arrays in python using numpy in a loop?

I am extracting variables in NetCDF files. These files are very big and I would prefer a minimum number of loops. Each variable is 162x174 and I have around 200 such files. I want a variable in the end like in Matlab which is 3d - 162x174x200.

In the code, apart from append, I tried dstack and vstack but they did not work either. I am also not sure the concatenation will occur like - 2x162x174 or 162x174x2.

 from netCDF4 import Dataset
 import os 
 import wrf 
 import numpy as np 
 import glob

 os.chdir(r'G:\WRF')
 ctt_wrf=[]
 for file in glob.glob('wrfout_*'):
    ncfile = Dataset(file)
    ctt=wrf.getvar(ncfile, "ctt")
    ctt_wrf.append(ctt)

 np.array(ctt_wrf).shape

如果每个文件中的数据都是原子的,则您可能最终希望得到一个数组,该数组的大小为:number_of_files x 162 x 174,在这种情况下,您使用的方法的形状为200 x 162 x 174

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM