/
spec_plotting.py
143 lines (118 loc) · 5.3 KB
/
spec_plotting.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
135
136
137
138
139
140
141
142
143
import numpy as np
import matplotlib.transforms as transforms
import matplotlib.patches as mpatches
from spec_tools import air_conversion
def plot_window(elem, ax, color='lightgray', air=False, path='./windows/'):
# Calculate the vacuum wavelengths of each pixel in the APOGEE chips
chip_start = [4.180476, 4.200510, 4.217064]
n_pixels = [3028, 2495, 1991]
wave_a = chip_start[0] + np.arange(3028) * 6e-6
wave_b = chip_start[1] + np.arange(2495) * 6e-6
wave_c = chip_start[2] + np.arange(1991) * 6e-6
wave_filt = np.concatenate((np.concatenate((wave_a, wave_b)), wave_c))
wave_filt = 10.**wave_filt
if air:
wave_filt = air_conversion(wave_filt)
# Read in the window strengths from the mask files
window_strength = np.genfromtxt(f'{path}{elem}.mask')
# Only select the pixels where the window strength is greater than 0
x_window = wave_filt[window_strength > 0.]
# Set up an axis transformation to plot the windows with x values on the data scale (wavelength)
# but have the y axis tied to the axes (so we can plot across the vertical range of the figure)
trans = transforms.blended_transform_factory(
ax.transData, ax.transAxes)
# Set initial values to make the window finding work and set up the shapes list
# (each window will end up a shape in this list)
end_point = -1
width = 0.3
# Loop through the pixels with window strength > 0 and if the pixels are adjacent expand the current window
# if the next pixel is not adjacent to the last, save the last window as an axis patch and add it to
# the plot
for i in range(len(x_window)):
if i < end_point:
continue
for j in range(len(x_window) - 1 - i):
if x_window[j + 1 + i] - x_window[i + j] < 0.3:
width = x_window[j + 1 + i] - x_window[i]
if len(x_window) - 1 == j + 1 + i:
rect = mpatches.Rectangle((x_window[i] - 0.14, 0), width=width + 0.28, height=1,
transform=trans, color=color, ec='None',
alpha=0.5, zorder=1)
ax.add_patch(rect)
width = 0.3
else:
rect = mpatches.Rectangle((x_window[i] - 0.14, 0), width=width + 0.28, height=1,
transform=trans, color=color, ec='None',
alpha=0.5, zorder=1)
ax.add_patch(rect)
width = 0.3
break
end_point = i + j + 1
if end_point > len(x_window[:]) - 1:
break
def plotly_window(elem, fig, shapes=None, color='lightgray', air=False, path='./windows/'):
# Calculate the vacuum wavelengths of each pixel in the APOGEE chips
chip_start = [4.180476, 4.200510, 4.217064]
n_pixels = [3028, 2495, 1991]
wave_a = chip_start[0] + np.arange(3028) * 6e-6
wave_b = chip_start[1] + np.arange(2495) * 6e-6
wave_c = chip_start[2] + np.arange(1991) * 6e-6
wave_filt = np.concatenate((np.concatenate((wave_a, wave_b)), wave_c))
wave_filt = 10.**wave_filt
if air:
wave_filt = air_conversion(wave_filt)
if shapes is None:
shapes = []
# Read in the window strengths from the mask files
window_strength = np.genfromtxt(f'{path}{elem}.mask')
# Only select the pixels where the window strength is greater than 0
x_window = wave_filt[window_strength > 0.]
# Set initial values to make the window finding work and set up the shapes list
# (each window will end up a shape in this list)
end_point = -1
width = 0.3
# Loop through the pixels with window strength > 0 and if the pixels are adjacent expand the current window
# if the next pixel is not adjacent to the last, save the last window as an axis patch and add it to
# the plot
for i in range(len(x_window)):
if i < end_point:
continue
for j in range(len(x_window) - 1 - i):
if x_window[j + 1 + i] - x_window[i + j] < 0.3:
width = x_window[j + 1 + i] - x_window[i]
if len(x_window) - 1 == j + 1 + i:
shapes.append(dict(
type="rect",
xref="x",
yref="paper",
x0=x_window[i] - 0.14,
y0=0,
x1=x_window[i] + 0.14 + width,
y1=1,
fillcolor=color,
opacity=0.5,
line_width=0,
layer='below'
))
width = 0.3
else:
shapes.append(dict(
type="rect",
xref="x",
yref="paper",
x0=x_window[i] - 0.14,
y0=0,
x1=x_window[i] + 0.14 + width,
y1=1,
fillcolor=color,
opacity=0.5,
line_width=0,
layer='below'
))
width = 0.3
break
end_point = i + j + 1
if end_point > len(x_window[:]) - 1:
break
rect = fig.update_layout(shapes=shapes)
return shapes