samedi 25 juin 2016

Copy a numpy array into a list of empty arrays


As I read in files from a set of directories I want to create a new numpy array for a concatenation of all the data I find in each one of the directories... and I want a nice uniquely named array for each:

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

The last line is my feeble attempt to assign each of the arrays I've just built (from all the files in a single directory) to a single numpy array with the names found in the arrayNames list. This obviously doesn't work ...

I realize there must be an easy way to do this, but it elludes me!


Aucun commentaire:

Enregistrer un commentaire