def choose_templates(templates, age_lim = 20.0, max_nonzero = 5): #start out by loading in the template files as a table import numpy as np import astropy.table as table import SPaCT ssp_rows = [] for template in templates: template = template.rstrip('.fits').split('/')[1] spectral_range = template[0] IMF_type = template[1:3] IMF_slope = float(template[3:7]) Z = SPaCT.plusminus(template[8])*float(template[9:13]) T = float(template[14:]) #print template + ':', spectral_range, IMF_type, IMF_slope, Z, T ssp_i = [template, spectral_range, IMF_type, IMF_slope, Z, T] ssp_rows.append(ssp_i) ssps = table.Table(map(list, zip(*ssp_rows)), names = ['name', 'spectral range', 'IMF type', 'IMF slope', 'Z', 't']) ssps = ssps[ssps['t'] <= age_lim] #then pick up to `max_nonzero` number of templates to be nonzero nonzero_templates = np.random.choice(ssps['name'], np.random.randint(1, max_nonzero + 1), replace = False) template_weights = np.random.rand(len(ssps['name'])) * [1. if i in nonzero_templates else 0. for i in ssps['name']] template_weights /= template_weights.sum() ssps.add_column(table.Column(name = 'weight', data = template_weights)) return ssps