-
Notifications
You must be signed in to change notification settings - Fork 0
/
wavel.py
52 lines (40 loc) · 1.5 KB
/
wavel.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
# function to create array containing all wavelength values
# of the grid where a spectrum has been sampled to
# from a fits header object
from astropy.io import fits
import numpy as np
def wavel(header,naxis=3,cdel_key='CD3_3'):
"""
xax = wavel(header,naxis=3,cdel_key='CD3_3')
header ... a pyfits header object containing naxis, crval, crpix & crdel values
needed for creation of wavelength grid array
naxis ... the wavelength axis (used to determine CRVALn, NAXISn & CRPIXn values)
cdel_key ... the key word that contains the wavelength increment array
"""
nax = naxis
naxis = header['NAXIS'+str(nax)]
crval = header['CRVAL'+str(nax)]
crpix = header['CRPIX'+str(nax)]
crdel = header[cdel_key]
xax = crval + (np.arange(naxis) - (crpix - 1))*crdel
return xax
def wavel_to_index(xax,lambda_in):
"""
index_out = wavel_to_index(xax, lambda_in)
"""
return np.abs(xax - lambda_in).argmin()
def wavel_old(header,dim=3):
"""
xax = wavel_old(header,dim=3)
header ... a pyfits header object containing naxis, crval, crpix & crdel values
needed for creation of wavelength grid array
dim ... dimension which correpsonds to wavelength (default: 3)
"""
assert type(dim) is int
assert type(header) is fits.header.Header
naxis = header['NAXIS'+str(dim)]
crval = header['CRVAL'+str(dim)]
crpix = header['CRPIX'+str(dim)]
crdel = header['CDELT'+str(dim)]
xax = crval + (np.arange(naxis) - (crpix - 1))*crdel
return xax