# Number of unique emission-line ratios:
n_ratios = lratio.shape[0]

# Loop over unique line-ratio combinations to populate template array:
n_line = len(ltable['lambda'])
sdss_line_array = n.zeros((n_line, naxis1), dtype=float)
emline_templates = n.zeros((n_ratios, naxis1), dtype=float)
for i in xrange(n_ratios):
    # Velocity width, in Angstroms:
    lsigma_velocity = ltable['lambda'] * lsigma[i] / c_kms
    # Add in quadrature with instrumental value:
    lsigma_angstroms = n.sqrt(lsigma_instrument**2 + lsigma_velocity**2)
    # Build array holding normalized individual line profiles:
    for j in xrange(n_line):
        sdss_line_array[j] = misc.gaussflux(wavebound_sdss, ltable['lambda'][j],
                                            lsigma_angstroms[j])
    # Generate template spectrumfor this set of line ratios:
    emline_templates[i] = n.dot(lratio[i], sdss_line_array)

# Test normalization (for last array of lines):
wave_sdss = 10.**loglam_sdss
deltawave_sdss = wavebound_sdss[1:] - wavebound_sdss[:-1]

n.dot(sdss_line_array, deltawave_sdss)
# Checks out as a bunch of 1's

# QA plotting:
# holdvec = n.ones(n_ratios)
# holdvec[0] = 0
# for i in xrange(n_ratios):
#     p.plot(wave_sdss, emline_templates[i], drawstyle='steps-mid', hold=holdvec[i])
# line in Angstroms:
# Instrumental line width:
linesigma_instrument = linedata['lambda'] * coeff1 * n.log(10.)
# Velocity linewidth:
vline = 100. # assume 100 km/s
linesigma_velocity = linedata['lambda'] * vline / c_kms
# Add them in quadrature:
linesigma = n.sqrt(linesigma_instrument**2 + linesigma_velocity**2)

# Make an array to hold the vectors for each line independently:
n_line = len(linedata['lambda'])
sdss_line_array = n.zeros((n_line, naxis1), dtype=float)

# Import functions and compute array values:
for i in xrange(n_line):
    sdss_line_array[i] = misc.gaussflux(wavebound_sdss, linedata['lambda'][i], linesigma[i])

# Test normalization:
wave_sdss = 10.**loglam_sdss
deltawave_sdss = wavebound_sdss[1:] - wavebound_sdss[:-1]

n.dot(sdss_line_array, deltawave_sdss)
# Checks out as a bunch of 1's

# Generate combined spectrum:
sdss_line_spec = n.dot(linedata['fratio'], sdss_line_array)

#p.plot(wave_sdss, sdss_line_spec, drawstyle='steps-mid', hold=False)

# Average spectrum over velocity dispersions for each age:
sdss_meanspec = ssp_sdssbin.mean(axis=1)
# Number of unique emission-line ratios:
n_ratios = lratio.shape[0]

# Loop over unique line-ratio combinations to populate template array:
n_line = len(ltable['lambda'])
sdss_line_array = n.zeros((n_line, naxis1), dtype=float)
emline_templates = n.zeros((n_ratios, naxis1), dtype=float)
for i in xrange(n_ratios):
    # Velocity width, in Angstroms:
    lsigma_velocity = ltable['lambda'] * lsigma[i] / c_kms
    # Add in quadrature with instrumental value:
    lsigma_angstroms = n.sqrt(lsigma_instrument**2 + lsigma_velocity**2)
    # Build array holding normalized individual line profiles:
    for j in xrange(n_line):
        sdss_line_array[j] = misc.gaussflux(wavebound_sdss, ltable['lambda'][j],
                                            lsigma_angstroms[j])
    # Generate template spectrumfor this set of line ratios:
    emline_templates[i] = n.dot(lratio[i], sdss_line_array)

# Test normalization (for last array of lines):
wave_sdss = 10.**loglam_sdss
deltawave_sdss = wavebound_sdss[1:] - wavebound_sdss[:-1]

n.dot(sdss_line_array, deltawave_sdss)
# Checks out as a bunch of 1's

# QA plotting:
# holdvec = n.ones(n_ratios)
# holdvec[0] = 0
# for i in xrange(n_ratios):
#     p.plot(wave_sdss, emline_templates[i], drawstyle='steps-mid', hold=holdvec[i])