Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

try: 

from itertools import zip_longest 

except ImportError: 

from itertools import izip_longest as zip_longest 

 

__all__ = ["CUT_TYPEMAP", "FieldSelection"] 

 

CUT_TYPEMAP = { 

"RA": "fieldRA", 

"Dec": "fieldDec", 

"GL": "fieldGL", 

"GB": "fieldGB", 

"EL": "fieldEL", 

"EB": "fieldEB" 

} 

"""Mapping of short names to field database column names.""" 

 

class FieldSelection(object): 

"""Class for constructing SQL queries on the survey fields database. 

 

This class is for creating SQL queries to perform on the survey fields 

database. It does not actually perform the queries. 

""" 

 

def base_select(self): 

"""Return the base field query. 

 

Returns 

------- 

str 

""" 

return "select * from Field" 

 

def combine_queries(self, *queries, **kwargs): 

"""Combine a set of queries. 

 

Parameters 

---------- 

queries : str instances 

A set of queries to join via the given operators. 

combiners : tuple of str 

A set of logical operations (and, or etc.) to join the queries 

with. Defaults is an empty tuple. NOTE: A tuple with one logical 

operator must look like ('and',). 

order_by : str, optional 

Set the order by clause. Default is fieldId. 

 

Returns 

------- 

str: 

The fully combined query. 

""" 

combiners = kwargs.get("combiners", ()) 

if len(combiners) != len(queries) - 1: 

raise RuntimeError("Number of combiners must be one less than " 

"number of queries!") 

 

order_by = kwargs.get("order_by", "fieldId") 

 

final_query = [] 

final_query.append(self.base_select()) 

final_query.append("where") 

for combine, query in zip_longest(combiners, queries): 

final_query.append(query) 

if combine is not None: 

final_query.append(combine) 

final_query.append("order by {}".format(order_by)) 

 

return self.finish_query(" ".join(final_query)) 

 

def finish_query(self, query): 

"""Put a semicolon at the end of a query. 

 

Parameters 

---------- 

query : str 

The SQL query to finish. 

 

Returns 

------- 

str 

The finished SQl query. 

""" 

return query + ";" 

 

def galactic_region(self, maxB, minB, endL, exclusion=False): 

"""Create a galactic region. 

 

This function creates a sloping region around the galactic plane to 

either include or exclude fields. 

 

Parameters 

---------- 

maxB : float 

The maximum galactic latitude at the galactic longitude of zero. 

minB : float 

The minimum galactic latitude at the galactic longitude of endL. 

endL : float 

The galactic longitude for the end of the envelope region. 

exclusion : bool, optional 

Flag to construct the query as an exclusion. Default is False. 

 

Returns 

------- 

str 

The appropriate query. 

""" 

region_select = ">" if exclusion else "<=" 

band = maxB - minB 

sql = '(abs(fieldGB) {0} ({1} - ({2} * '\ 

'abs(fieldGL)) / {3}))'.format(region_select, maxB, band, endL) 

 

return sql 

 

def get_all_fields(self): 

"""Return query for all fields. 

 

Returns 

------- 

str 

The query for all the fields. 

""" 

return self.finish_query(self.base_select()) 

 

def select_region(self, region_type, start_value, end_value): 

"""Create a simple bounded region. 

 

This function creates a bounded cut query based on the input values as 

bounds for a given region. If start_value < end_value, the cut looks 

like [start_value, end_value]. If start_value > end_value, the bounded 

cut is or'd between the following cuts: [start_value, 360] and 

[0, end_value]. 

 

Parameters 

---------- 

region_type : str 

The name of the region to cut on. 

start_value : float 

The starting value (degrees) of the cut region. 

end_value : float 

The ending value (degrees) of the cut region. 

 

Returns 

------- 

str 

The appropriate query. 

""" 

column_name = CUT_TYPEMAP[region_type] 

if end_value > start_value: 

sql = '{0} between {1} and {2}'.format(column_name, start_value, 

end_value) 

else: 

sql = '({0} between {1} and 360 or '\ 

'{0} between 0 and {2})'.format(column_name, start_value, 

end_value) 

 

return sql 

 

def select_user_regions(self, id_list): 

"""Create a query for a list of fields.of 

 

This function creates a query focusing on field Ids. It is recommended 

not to use this with more than a dozen Ids. 

 

Parameters 

---------- 

id_list : list[int] 

A set of field Ids to construct a query for.query 

 

Returns 

------- 

str 

The appropriate query. 

""" 

sql = [] 

for fid in id_list: 

sql.append("fieldId={}".format(fid)) 

sql.append("or") 

 

# Don't need last or 

del sql[-1] 

 

return " ".join(sql)