-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflags.py
133 lines (105 loc) · 3.3 KB
/
flags.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
#####################################################################################################
# File containing column names for the various flags in the HSC data,
# and means of retrieving them for analysis.
#####################################################################################################
#flags for masking regions contaminated by bright stars
flags_brightstar = [
'_mask_brightstar_ghost15',
'_mask_brightstar_halo',
'_mask_brightstar_blooming',
#'_mask_brightstar_dip'
]
flag_channelstop = 'y_mask_brightstar_channel_stop'
#flags associated with the primary photometric band
flags_main = [
#'_cmodel_flag_badcentroid',
'_sdsscentroid_flag',
#'_pixelflags_edge',
#'_pixelflags_interpolatedcenter',
#'_pixelflags_saturatedcenter',
#'_pixelflags_crcenter',
#'_pixelflags_bad',
#'_pixelflags_suspectcenter',
#'_pixelflags_clipped',
#'_deblend_skipped'
]
#flags associated with all photometric bands (considered 'strict' cuts)
flags_strict = [
'_sdsscentroid_flag',
'_cmodel_flag',
'_psfflux_flag',
'_pixelflags_edge',
'_pixelflags_interpolatedcenter',
'_pixelflags_saturatedcenter',
'_pixelflags_crcenter',
'_pixelflags_bad'
]
def get_flags(b_primary, b_secondary=[], types=['main'], incl_channelstop=False):
'''
Returns column names of the requested flags.
Parameters
----------
b_primary: str
The primary photometric band.
b_secondary : list
List of other bands included in the analysis.
types: list
Which types of flags to be returned.
incl_channelstop: bool
If True and brightstar flags requested, will include the channel stop
flag in the y-band.
Returns
-------
flags_all: list
List of all requested flag column names.
'''
#list for containing flag column names
flags_all = []
#cycle through requested flag types
for ty in types:
#check if type is valid
if ty not in ['main', 'strict', 'brightstar']:
raise TypeError('Argument "types" must contain only combinations of "main", "strict", or "brightstar".')
if ty == 'main':
flags_all += [f'{b_primary}{fl}' for fl in flags_main]
elif ty == 'strict':
import itertools
flags_all += list(
itertools.chain.from_iterable(
[f'{bs}{fl}' for fl in flags_strict]
for bs in ([b_primary] + b_secondary)
)
)
else:
flags_all += [f'{b_primary}{fl}' for fl in flags_brightstar]
if incl_channelstop:
flags_all += [flag_channelstop]
return list(set(flags_all))
def combine_flags(dataset, flagcols, combine_type='or'):
'''
Combines flag columns with the specified operation ('and' or 'or').
Parameters
----------
dataset: HDF5 Dataset or Group
Dataset containing the relevant flags.
flagcols: list
List of all columns containing the flags to be combined.
combine_type: str
Operation to be used to combine the flags ('and' or 'or').
Returns
-------
flags_comb: array-like
The result of combining the flags.
'''
#see if valid combine_type has been chosen
combine_type = combine_type.lower()
if combine_type not in ['or', 'and']:
raise TypeError('combine_type must be either "and" or "or" (capital letters allowed).')
if combine_type == 'or':
combine = lambda x,y : x + y
else:
combine = lambda x,y : x * y
flags_comb = [dataset[fl][:] for fl in flagcols]
from functools import reduce
flags_comb = reduce(combine, flags_comb)
return flags_comb