Coverage for python/lsst/sims/survey/fields/fields_database.py : 25%

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
3import os
4import sqlite3
6import numpy
8__all__ = ["FieldsDatabase"]
10class FieldsDatabase(object):
12 FIELDS_DB = "Fields.db"
13 """Internal file containing the standard 3.5 degree FOV survey field
14 information."""
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))
23 def __del__(self):
24 """Delete the class.
25 """
26 self.connect.close()
28 def get_field_set(self, query):
29 """Get a set of Field instances.
31 Parameters
32 ----------
33 query : str
34 The query for field retrieval.
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))
46 return field_set
48 def get_opsim3_userregions(self, query, precision=2):
49 """Get a formatted string of OpSim3 user regions.
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):
55 userRegion = XXX.XX,YYY.YY,0.03
56 ...
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.
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.
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))
82 def get_ra_dec_arrays(self, query):
83 """Retrieve lists of RA and Dec.
85 Parameters
86 ----------
87 query : str
88 The query for field retrieval.
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])
102 return numpy.array(ra), numpy.array(dec)
104 def get_id_ra_dec_arrays(self, query):
105 """Retrieve lists of fieldId, RA and Dec.
107 Parameters
108 ----------
109 query : str
110 The query for field retrieval.
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])
126 return numpy.array(fieldId, dtype=int), numpy.array(ra), numpy.array(dec)
128 def get_rows(self, query):
129 """Get the rows from a query.
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.
135 Parameters
136 ----------
137 query : str
138 The query for field retrieval.
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()