-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfilter.py
156 lines (127 loc) · 5.34 KB
/
filter.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
import utilities as utils
import os
import sys
import re
import numpy
import repipy.target as target
from repipy import __path__ as repipy_path
from lemon import passband
repipy_path = repipy_path[0]
class Filter(object):
_system_dict = {'Har': '.*har(ris)?.*',
'': '.*(H(a(lpha)?)?|H(a)?)\d{4}.*',
'Joh': '.*j(oh(nson)?)?.*',
'Gunn': '.*gun(n)?.*',
'sdss': '.*(sdss|sloan).*'
}
def __init__(self, header):
self.header = header
def __str__(self):
""" Short representation of the filter, as filter system + filter, inspired in vterron's passband
:return:
"""
try:
filtersys = self.header.hdr[ self.header.filtersysk ]
except ValueError:
filtersys = ""
filtername = self.filter_name
filter_alias = passband.Passband(filtername)
if filter_alias.system == 'Unknown':
filter_alias = passband.Passband(filtersys + filtername)
return re.sub('[\s\']', "", filter_alias.__str__())
def zero_point(self, target, aperture=None):
""" Return the zero point, given this target """
if target.objtype == 'standards':
return 2.5 * (numpy.log10(target.counts(aperture=aperture) / self.header.hdr[self.header.exptimek]) -
numpy.log10(target.flux()))
@property
def filter_ID(self):
""" Identify the filter ID.
Some telescopes identify the filter ID in the header of the images. Unfortunately, the filters in
CAHA don't use the same ID in the filter curves in the webpages and in the headers, so it's difficult to link
them. """
if self.header.telescope.lower() == "caha":
return self.header.telescope.lower() + "_" + str(self.filter_wavelength) + "." + str(self.filter_width)
else:
if self.header.telescope and self._get_filterID():
return self.header.telescope.lower() + "_" + str(self._get_filterID())
#@property
#def filter_sys(self):
# """ Return the filter system of the filter: Gunn, SDSS, Johnson, Harris, ...
# :return:
# """
# return self._get_filterSYS()
@property
def filter_name(self):
""" Give the name of the filter as it appears in the header.
:return:
"""
return self.header.hdr[self.header.filterk]
@property
def filter_wavelength(self):
""" IF the wavelength of the filter is present, try to find it.
The wavelengths are usually in nanometers with one decimal digit.
"""
wav = self._get_filterwav()[0]
if wav: # If not None
wav = int(round(float(wav)))
return wav
@property
def filter_width(self):
""" Find width of filter. """
width = self._get_filterwav()[1]
if width: # If not None
width = int(round(float(width)))
return width
@property
def filter_curve(self):
""" Read the filter curve from the collection in the repipy/filters folder"""
dir = os.path.join(repipy_path, 'filters')
file = os.path.join(dir, self.filter_ID)
if os.path.exists(file):
wav, trans = numpy.genfromtxt(file).transpose()
# In case the transmissivity is in % instead of normalized to 1
if trans.max() > 1:
trans /= 100
# In case the wavelength is in nanometers, not Angstroms
if wav.max() < 1000:
wav *= 10
return numpy.array([wav, trans]).transpose()
else:
sys.exit("File {0} does not exist. Find the filter curve and include it".format(file) +\
"in the folder with the exact same name")
@property
def filter_integral(self):
""" Integral of the filter curve"""
@utils.memoize
def _get_filterwav(self):
""" Try to find the central wavelength of the filter used for the image.
I know, right? Crazy to uniquely identify a filter in the header of the image...
"""
wavelength = self.header._get_value(self.header._KEYWORDS_ALIASES['FILTER_WAVELENGTH'])
width = self.header._get_value(self.header._KEYWORDS_ALIASES['FILTER_WIDTH'])
return wavelength, width
@utils.memoize
def _get_filterID(self):
""" Try to find the ID of the filter used in the image.
"""
return self.header._get_value(self.header._KEYWORDS_ALIASES['FILTER_ID'])
#@utils.memoize
#def _get_filterSYS(self):
# """ Find the filter system: SDSS, Gunn, Johnson, Harris
# :return:
# """
# filter_sys = self.header._get_value(self.header._KEYWORDS_ALIASES['FILTER_SYS'])
# filter_name = self.filter_name
#
#
# # The order of the filter checking is important, if 'Har' is present, the filter is Harris, not Halpha
# # and if sloan is pressent, the filter is SDSS, no matter if the header says "sloan Gunn r" for historical
# # reasons. Therefore, we sort the search to beat those problems.
# order = ['Har', '', 'sdss', 'Gunn', 'John']
# for system in order:
# regexp = self._system_dict[system]
# string = filter_sys + filter_name
# if re.match(regexp, string, re.I):
# return system
# return ''