当我从一组目录中读取文件时,我想创建一个新的numpy数组,用于连接我在每个目录中找到的所有数据......我希望每个目录都有一个很好的唯一命名数组:
schaererPath = '../Software-Models/Schaerer/'
schaererDirs = np.array(['pop3_TA/','pop3_TE/','e-70_mar08/','e-50_mar08/'])
Zs = np.array([0.0, 0.0, 1.0e-7, 1.0e-5])
schaererPopFilePattern = '*.is2'
pop3TA = pop3TE = Zem7 = Zem5 = np.array([])
arrayNames = [pop3TA, pop3TE, Zem7, Zem5]
for i, (Z, schaererDir,array) in enumerate(zip(Zs,schaererDirs,arrayName)):
schaererFilePattern = schaererPath + schaererDir + schaererPopFilePattern
schaererFiles = glob.glob(schaererFilePattern) # All the files in the dir...
schaererAges = np.array([linecache.getline(file,13) for file in schaererFiles]) # Get the line with the (log) age...
schaererAges = np.array([float(sa[30:]) for sa in schaererAges],dtype=float) # Log age starts at position 30
schaererData = np.array([np.loadtxt(file,skiprows=16) for file in schaererFiles])
ageSortIndxes = schaererAges.argsort() # Array of indices to sort things by age...
# The following builds an array of arrays (one for each age) with each array's entries:
# log age, Z, waveln, lum/A
schaererDataSorted = np.array([np.insert(sed[:,[0,2]],[0],[[age, Z] for ii in range(0,len(sed))], axis=1) for age,sed in zip(schaererAges[ageSortIndxes], schaererData[ageSortIndxes])])
schaererDataSorted = schaererDataSorted.reshape(len(schaererDataSorted)*len(schaererDataSorted[0]),4)
# We now have:
# [[log age, Z, waveln, flux], [], ...]
arrayNames[i] = schaererDataSorted
最后一行是我将我刚刚构建的每个数组(从单个目录中的所有文件)分配到一个numpy数组的微弱尝试,该数组具有在arrayNames列表中找到的名称。这显然不起作用......
我意识到必须有一个简单的方法来做到这一点,但它让我望而却步!