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

1from __future__ import unicode_literals 

2 

3import os 

4import sqlite3 

5 

6import numpy 

7 

8__all__ = ["FieldsDatabase"] 

9 

10class FieldsDatabase(object): 

11 

12 FIELDS_DB = "Fields.db" 

13 """Internal file containing the standard 3.5 degree FOV survey field 

14 information.""" 

15 

16 def __init__(self): 

17 """Initialize the class. 

18 """ 

19 self.db_name = self.FIELDS_DB 

20 self.connect = sqlite3.connect(os.path.join(os.path.dirname(__file__), 

21 self.db_name)) 

22 

23 def __del__(self): 

24 """Delete the class. 

25 """ 

26 self.connect.close() 

27 

28 def get_field_set(self, query): 

29 """Get a set of Field instances. 

30 

31 Parameters 

32 ---------- 

33 query : str 

34 The query for field retrieval. 

35 

36 Returns 

37 ------- 

38 set 

39 The collection of Field instances. 

40 """ 

41 field_set = set() 

42 rows = self.get_rows(query) 

43 for row in rows: 

44 field_set.add(tuple(row)) 

45 

46 return field_set 

47 

48 def get_opsim3_userregions(self, query, precision=2): 

49 """Get a formatted string of OpSim3 user regions. 

50 

51 This function gets a formatted string of OpSim3 user regions suitable 

52 for an OpSim3 configuration file. The format looks like 

53 (RA,Dec,Width): 

54 

55 userRegion = XXX.XX,YYY.YY,0.03 

56 ... 

57 

58 The last column is unused in OpSim3. The precision argument can be 

59 used to control the formatting, but OpSim3 configuration files use 2 

60 digits as standard. 

61 

62 Parameters 

63 ---------- 

64 query : str 

65 The query for field retrieval. 

66 precision : int, optional 

67 The precision used for the RA and Dec columns. Default is 2. 

68 

69 Returns 

70 ------- 

71 str 

72 The OpSim3 user regions formatted string. 

73 """ 

74 format_str = "userRegion = "\ 

75 "{{:.{0}f}},{{:.{0}f}},0.03".format(precision) 

76 rows = self.get_rows(query) 

77 result = [] 

78 for row in rows: 

79 result.append(format_str.format(row[2], row[3])) 

80 return str(os.linesep.join(result)) 

81 

82 def get_ra_dec_arrays(self, query): 

83 """Retrieve lists of RA and Dec. 

84 

85 Parameters 

86 ---------- 

87 query : str 

88 The query for field retrieval. 

89 

90 Returns 

91 ------- 

92 numpy.array, numpy.array 

93 The arrays of RA and Dec. 

94 """ 

95 rows = self.get_rows(query) 

96 ra = [] 

97 dec = [] 

98 for row in rows: 

99 ra.append(row[2]) 

100 dec.append(row[3]) 

101 

102 return numpy.array(ra), numpy.array(dec) 

103 

104 def get_id_ra_dec_arrays(self, query): 

105 """Retrieve lists of fieldId, RA and Dec. 

106 

107 Parameters 

108 ---------- 

109 query : str 

110 The query for field retrieval. 

111 

112 Returns 

113 ------- 

114 numpy.array, numpy.array, numpy.array 

115 The arrays of fieldId, RA and Dec. 

116 """ 

117 rows = self.get_rows(query) 

118 fieldId = [] 

119 ra = [] 

120 dec = [] 

121 for row in rows: 

122 fieldId.append(int(row[0])) 

123 ra.append(row[2]) 

124 dec.append(row[3]) 

125 

126 return numpy.array(fieldId, dtype=int), numpy.array(ra), numpy.array(dec) 

127 

128 def get_rows(self, query): 

129 """Get the rows from a query. 

130 

131 This function hands back all rows from a query. This allows one to 

132 perform other operations on the information than those provided by 

133 this class. 

134 

135 Parameters 

136 ---------- 

137 query : str 

138 The query for field retrieval. 

139 

140 Returns 

141 ------- 

142 list 

143 The set of field information queried. 

144 """ 

145 cursor = self.connect.cursor() 

146 cursor.execute(query) 

147 return cursor.fetchall()