-
Notifications
You must be signed in to change notification settings - Fork 2
/
spec_extract.py
executable file
·134 lines (107 loc) · 3.64 KB
/
spec_extract.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python
import os, sys
from astropy.io import fits
import numpy
from scipy.ndimage.filters import median_filter
import bottleneck
import scipy.interpolate
numpy.seterr(divide='ignore', invalid='ignore')
# Disable nasty and useless RankWarning when spline fitting
import warnings
warnings.simplefilter('ignore', numpy.RankWarning)
# also ignore some other annoying warning
warnings.simplefilter('ignore', RuntimeWarning)
import bottleneck
import scipy.spatial
import pysalt.mp_logging
import logging
from optparse import OptionParser
import matplotlib.pyplot as pl
def extract_spectra(filename, extname, specs):
hdulist = fits.open(filename)
wl_data = hdulist['WAVELENGTH'].data
obj_data = None
try:
obj_data = hdulist[extname].data
logger.info("Using data from %s" % (extname))
except:
logger.error("Can not access/find extension %s" % (extname))
return
var_data = None
var_valid = False
try:
var_data = hdulist['VAR'].data
var_valid = True
except:
pass
for ispec, spec in enumerate(specs):
try:
items = spec.split(",")
from_line = int(items[0])
to_line = int(items[1])
output_fn = items[2]
logger.info("Extracting lines %d -- %d to %s" % (
from_line, to_line, output_fn
))
except:
logger.error("Error processing parameter %d: %s" % (ispec+1, spec))
continue
#
# Get wavelength scale
#
# spec_wl = numpy.average(
spec_wl = wl_data[from_line:to_line + 1, :] # , axis=1)
spec_wl_1d = numpy.average(spec_wl, axis=0)
print spec_wl.shape, spec_wl_1d.shape
#
# Also extract fluxes
#
spec_fluxes = obj_data[from_line:to_line + 1, :]
spec_fluxes_1d = numpy.sum(spec_fluxes, axis=0)
print spec_fluxes_1d.shape
#
# Compute variance data (this is noise^2), so need to take sqrt to get real noise
#
var_1d = numpy.zeros((spec_wl_1d.shape[0]))
if (var_valid):
var_2d = var_data[from_line:to_line + 1, :]
var_1d = numpy.sqrt(numpy.sum(var_2d, axis=0))
print "Found VAR extension"
#
# Now merge fluxes and wavelength scale
#
combined = numpy.zeros((spec_wl_1d.shape[0], 4))
combined[:, 0] = numpy.arange(combined.shape[0]) + 1
combined[:, 1] = spec_wl_1d[:]
combined[:, 2] = spec_fluxes_1d[:]
combined[:, 3] = var_1d[:]
# numpy.append(spec_wl_1d.reshape((-1,1)),
# spec_fluxes_1d.reshape((-1,1)),
# axis=1)
print combined.shape
numpy.savetxt(
output_fn,
combined,
header="""\
Column 1: x-coordinate [pixels]
Column 2: wavelength [angstroems]
Column 3: integrated flux [ADU]
Column 4: variance of sum (=sqrt(sum_i(var_i))) [ADU]
--------------------------------------------------------\
""",
)
if __name__ == "__main__":
logger_setup = pysalt.mp_logging.setup_logging()
logger = logging.getLogger("SpecExtract")
parser = OptionParser()
parser.add_option("-e", "--ext", dest="extname",
help="name of extension to be extracted from",
default="SKYSUB.OPT")
(options, cmdline_args) = parser.parse_args()
filename = cmdline_args[0]
extract_spectra(
filename=filename,
extname=options.extname,
specs=cmdline_args[1:],
)
pysalt.mp_logging.shutdown_logging(logger_setup)