Coverage for webgeodyn/obsdata/observatory.py: 0%
74 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-12-18 09:33 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-12-18 09:33 +0000
1class ObservatoryGroup:
2 """ Class handling groups of observatories (Ground magnetic obs, CHAMP or SWARM) """
3 def __init__(self, groupName, display_r, search_radius=None):
4 """
5 Creates the empty dict of Observatories and empty list of coordinates.
7 :param groupName: Name of the group
8 :type groupName: str
9 :param display_r: radius at which the data should be displayed
10 :type display_r: float
11 :param search_radius: TODO
12 :type search_radius:
13 """
14 self.groupName = groupName
15 self.search_radius = search_radius
16 self.display_r = display_r
17 self.observatories = {}
18 self.coordinates = []
20 def addObservatory(self, obscode, r, th, ph):
21 """
22 Add an Observatory to the internal dict with th and ph as keys.
24 :param obscode: code of the observatory
25 :type obscode: str
26 :param r: radius of the observatory
27 :type r: float
28 :param th: colatitude of the observatory
29 :type th: float
30 :param ph: azimuth of the observatory
31 :type ph: float
32 """
33 th = str(th)
34 ph = str(ph)
35 if th not in self.observatories:
36 self.observatories[th] = {}
37 if ph in self.observatories[th]:
38 return
39 self.coordinates.append([th, ph])
40 self.observatories[th][ph] = Observatory(obscode, r, th, ph)
42 def addObservatoryData(self, th, ph, measureName, times, rdata, thdata, phdata):
43 """
44 Adds the data for th, ph.
46 :param th: colatitude at which the data was taken
47 :type th: float
48 :param ph: azimuth at which the data was taken
49 :type ph: float
50 :param measureName: name of the measure to add
51 :type measureName: str
52 :param times: times array
53 :type times: np.array
54 :param rdata: radial data
55 :type rdata: np.array
56 :param thdata: colatitudinal data
57 :type thdata: np.array
58 :param phdata: azimuthal data
59 :type phdata: np.array
60 :return: 0 if everything went well, -1 otherwise
61 :rtype: int
62 """
63 th = str(th)
64 ph = str(ph)
65 if th not in self.observatories:
66 print("setObservatoryData failed, no observatory with th=%s" % th)
67 return -1
68 if ph not in self.observatories[th]:
69 print("setObservatoryData failed, no observatory with ph=%s" % ph)
70 return -1
71 self.observatories[th][ph].addData(measureName, times, rdata, thdata, phdata)
72 return 0
74 def getObservatoryData(self, th, ph, measureName):
75 """
76 Gets the measure data at a given position.
78 :param th: colatitude at which the data should be fetched
79 :type th: float
80 :param ph: azimuth at which the data should be fetched
81 :type ph: float
82 :param measureName: name of the measure to fetch
83 :type measureName: str
84 :return: dict with group name as key and data as value
85 :rtype: dict
86 """
87 th = str(th)
88 ph = str(ph)
89 if th not in self.observatories:
90 print("getObservatoryData failed, no observatory with th=%s" % th)
91 return
92 if ph not in self.observatories[th]:
93 print("getObservatoryData failed, no observatory with ph=%s" % ph)
94 return
95 return {self.groupName: self.observatories[th][ph].getData(measureName)}
97 def getObsR(self, th, ph):
98 """
99 Get the radius of observatory.
101 :param th: colatitude at which the r should be fetched
102 :type th: float
103 :param ph: azimuth at which the r should be fetched
104 :type ph: float
105 :return: radius of observatory if th and ph exist as keys. None otherwise.
106 :rtype: float or None
107 """
108 th = str(th)
109 ph = str(ph)
110 if th not in self.observatories:
111 return None
112 if ph not in self.observatories[th]:
113 return None
114 return self.observatories[th][ph].r
117class Observatory:
118 """ Class representing a single observatory. """
119 def __init__(self, obscode, r, th, ph):
120 """
121 :param obscode: code of the observatory
122 :type obscode: str
123 :param r: radius of the observatory
124 :type r: float
125 :param th: colatitude of the observatory
126 :type th: float
127 :param ph: azimuth of the observatory
128 :type ph: float
129 """
130 self.obscode = obscode
131 self.r = r
132 self.th = th
133 self.ph = ph
134 self.times = {}
135 self.rdata = {}
136 self.thdata = {}
137 self.phdata = {}
139 def addData(self, measureName, times, rdata, thdata, phdata):
140 """
141 Adds data to the observatory.
143 :param measureName: name of the measure to add
144 :type measureName: str
145 :param times: times array
146 :type times: np.array
147 :param rdata: radial data
148 :type rdata: np.array
149 :param thdata: colatitudinal data
150 :type thdata: np.array
151 :param phdata: azimuthal data
152 :type phdata: np.array
153 :return: 0 if everything went well, -1 otherwise
154 :rtype: int
155 """
156 for data in [self.times, self.rdata, self.thdata, self.phdata]:
157 if measureName not in data:
158 data[measureName] = []
159 self.times[measureName].append(times)
160 self.rdata[measureName].append(rdata)
161 self.thdata[measureName].append(thdata)
162 self.phdata[measureName].append(phdata)
164 def getData(self, measureName):
165 """
166 Gets the data of the observatory.
168 :param measureName: name of the measure to fetch
169 :type measureName: str
170 :return: dict with 'obscode', 'times', 'r', 'th' and 'ph' as keys and corresponding data as values
171 :rtype: dict
172 """
173 data = {
174 "obscode": self.obscode
175 }
176 if measureName in self.times:
177 data["times"] = self.times[measureName]
178 if measureName in self.rdata:
179 data["r"] = self.rdata[measureName]
180 if measureName in self.thdata:
181 data["th"] = self.thdata[measureName]
182 if measureName in self.phdata:
183 data["ph"] = self.phdata[measureName]
185 return data