Skip to content

Instantly share code, notes, and snippets.

@wkerzendorf
Created July 18, 2017 12:47
Show Gist options
  • Save wkerzendorf/94ba60d7316a76d15ee1ac8245449e16 to your computer and use it in GitHub Desktop.
Save wkerzendorf/94ba60d7316a76d15ee1ac8245449e16 to your computer and use it in GitHub Desktop.
read cmfgen spectrum
from collections import OrderedDict
from astropy import units as u
import numpy as np
import pandas as pd
def read_cmfgen(fname):
nu = []
flux = []
current_data = nu
with open(fname, 'r') as fh:
for line in fh:
if line.strip() == '':
continue
elif line.strip().startswith('Continuum'):
continue
elif line.strip().startswith('Observed'):
current_data = flux
continue
current_data.append(line.strip())
nu = np.array([float(item) for item in ' '.join(nu).split()]) * 1e15 * u.Hz
lambda_wl = nu.to(u.Angstrom, u.spectral())
f_nu = (np.array([float(item) for item in ' '.join(flux).split()]) * u.Jansky).to(u.erg/u.s/u.cm**2/u.Hz)
f_lambda = ((f_nu * nu)/lambda_wl).to(u.erg/u.s/u.cm**2/u.angstrom)
data = OrderedDict()
data['nu'] = nu
data['lambda'] = lambda_wl
data['f_nu'] = f_nu
data['f_lambda'] = f_lambda
return pd.DataFrame(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment