forked from agoga/nreshk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc_del_lambda.py
217 lines (169 loc) · 7.25 KB
/
calc_del_lambda.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#imports from a lot of data the lab frame spectra so we may cross-correlate onto this and
#find better locations of the ca hk lines
#taken from Ricky Egeland's code, probably could be optimized since he
#did more with this code than we need
def import_lab_frame_spectra(fluxdir, minwl=None, maxwl=None, resolution=1, residual=False):
import numpy as np
import matplotlib as mpl
from matplotlib import pyplot as plt
import os
import os.path
import re
import scipy
import scipy.signal
import scipy.ndimage.filters
import helpers as h#for constants
wavelength = []
res_flux = []
irradiance = []
for f in os.listdir(fluxdir):
if not re.match('^lm', f): continue
fpath = os.path.join(fluxdir, f)
d = np.genfromtxt(fpath, unpack=True)
wavelength = np.append(wavelength, d[0]) # in nm
res_flux = np.append(res_flux, d[1]) # from a normalized spectra
irradiance = np.append(irradiance, d[2]) # from a regular spectra; mu-W / cm^2 / nm
wavelength = np.array(wavelength)
res_flux = np.array(res_flux)
irradiance = np.array(irradiance)
sort = np.argsort(wavelength)
wavelength = wavelength[sort]
res_flux = res_flux[sort]
irradiance = irradiance[sort]
angstroms = wavelength * 10 # in Angstroms
if minwl is None:
minwl = angstroms[0]
if maxwl is None:
maxwl = angstroms[-1]
sel = (angstroms >= minwl) & (angstroms <= maxwl)
dw = angstroms[1] - angstroms[0]
if not residual:
series = irradiance / 10. # nm^-1 => Ang.^-1
else:
series = res_flux
if resolution > 0:
series = scipy.ndimage.filters.gaussian_filter(series, resolution/dw)
#print('degrading source: ' + str(resolution/dw))
return angstroms[sel], series[sel]
#############################################
#This is the big function we calculate our delta lambe value to place this current
#spectra in the pipeline in the lab reference frame.
#First we will interpolate to get these on the same grid
#Then we do a cross correlation
#With the cross correlation function we will fit a parabola right around the peak
#We do this because we want to have more precision for offset than our grid will give us
#the peak of this new parabola is the real offset
#lots of plotting function commented out for debugging since this function does a lot and
#could certainly be improved on.
def calc_del_lam(labGrid, lab, tarGrid, targ, smooth) :
import scipy as sc
#import astropy.io.fits
import numpy as np
import helpers as h#for constants
import numpy.polynomial.polynomial as poly
#import os
#from calc_shk import calc_shk
#from calc_shk import calc_targOlapf
#from mk_flatolap import mk_flatolap
from matplotlib import pyplot as plt
from astropy.convolution import convolve, Box1DKernel
from scipy import interpolate
tmpGridScale = 1
dLam = tarGrid[1] - tarGrid[0]
#print(np.shape(targ))
gausdTarg = sc.ndimage.filters.gaussian_filter(targ,smooth/dLam/2)
dLabLam = labGrid[1] - labGrid[0]
#TODO SHOULD 2.55 be h.sigToFWHM?
gausedLab = sc.ndimage.filters.gaussian_filter(lab,(dLam/dLabLam)/2.55)
#get the lab spectrum into angs div by 10 on angstrom grid for our purposes
interpfunc = interpolate.interp1d(labGrid, gausedLab, kind='linear')#,fill_value='extrapolate')
labInterp=interpfunc(tarGrid)
#ZERO OUT THE EDGES OF OUR LAB SPECTRA
#TODO do this with strict values to remove edge errors which may affect correlation
#from 0 to first nonzero element of targolap
labInterp[:targ.nonzero()[0][0]]=0
#from last nonzero element to end
labInterp[targ.nonzero()[0][-1]:]=0
#do not consider 0's while taking mean
labInterp[labInterp==0]=np.nan
targ[targ==0]=np.nan
rmsx = np.nanmean(labInterp)
rmsy = np.nanmean(targ)
#place the 0's back
labInterp[np.isnan(labInterp)]=0
targ[np.isnan(targ)]=0
#plt.figure(figsize=(12,6))
#plt.plot(1000*labInterp[targ!=0]-rmsx,'k-')
#plt.plot(targ[targ!=0]-rmsy,'g-')
#plt.show()
#plt.close()
#THIS IS THE CROSS CORRELATION SECTION
##
##correlate must have same sized arrays input
correlation = np.correlate(targ[targ!=0]-rmsy,(labInterp[targ!=0]-rmsx),'full')
#length of the correlation array is length input array times 2 plus 1
#if the two arrays are already aligned then the peak of correlation function should be middle
middle = int((len(correlation)-1)/2)
#width is used for local maximum finding
#1 is a number that worked here for smarts and NRES data. MAY NEED TO BE ADJUSTED for new data
width = int(1/dLam)
#max value is the index of the maximum value(local max around middle of array if width used)
mval = middle-width+np.argmax(correlation[middle-width:middle+width])
#want to make a quadratic around mval to be more precise with 'peak' of correlation
#if we just use max val, precision is limited to grid spacing.
#need to do poly only in certain range around center because wings will take over the fit
fitWidth = 5
xRange = mval + np.arange(2*fitWidth)- fitWidth
polyFunc = np.polyfit(xRange, correlation[mval-fitWidth:mval+fitWidth],2)
#take the derivative of the function and set equal to 0 for the peak.
#f=a*x^2 +b*x+c
#f' = 2*a*x+b = 0 -> x = -b/2a
xVal = -polyFunc[1]/(2*polyFunc[0])
#print(xVal)
#ffit = np.polyval(polyFunc,xRange)
#plt.figure()
#plt.xlim(mval-fitWidth*3,mval+fitWidth*3)
#plt.plot(xRange, ffit,'g-')
#plt.plot(range(len(correlation)), correlation, 'k-')
#plt.show()
#plt.close()
#mval= np.argmax(ffit)
#the actual lamda offset is how far from middle we are in pixel space times the
#pixel to grid ratio
offset = (xVal-middle)*(tarGrid[1]-tarGrid[0])
#print('offset: ' + str(offset))
#used for printing/debuging
#scale = np.mean(gausdTarg)/np.mean(labInterp)
#tmpMax = np.argmax(out)
#print('index of maximum: ' + str(tmpMax) + ' and adjusted delLam: ' + str(tmpMax/len(out)))
#print(out)
#fig, ax = plt.subplots(figsize=(25,5))
#ax.ticklabel_format(useOffset=False)
#plt.title("Unadjusted stellar spectra over reference spectra")
#plt.xlabel("Wavelength (nm)")
#plt.ylabel("Scaled irradiance")
#plt.xlim([392,398])
#plt.ylim([0,2800])
#plt.plot(tarGrid, gausdTarg, 'g-')
#plt.axvline(x=393.369, color='blue')
#plt.axvline(x=396.85, color='blue')
#SCALE JUST FOR VIEWING
#plt.plot(tarGrid,labInterp*scale, 'k-')
#plt.show()
#plt.close()
#fig, ax = plt.subplots(figsize=(25,5))
#ax.ticklabel_format(useOffset=False)
#plt.title("Correlated stellar spectra over reference spectra")
#plt.xlabel("Wavelength (nm)")
#plt.ylabel("Scaled irradiance")
#plt.xlim([392,398])
#plt.ylim([0,2800])
#plt.plot(tarGrid-offset, gausdTarg, 'g-')
#plt.axvline(x=393.369, color='blue')
#plt.axvline(x=396.85, color='blue')
#plt.plot(tarGrid-offset, gausdTarg, 'k-',color='green')
#SCALE JUST FOR VIEWING
#plt.plot(tarGrid,labInterp*scale, 'k-')
#plt.show()
#plt.close()
return offset,targ,labInterp,gausdTarg