Commits
1 1 | from casatasks import casalog |
2 2 | from casatools import quanta |
3 3 | import certifi |
4 + | from datetime import datetime |
4 5 | import json, os, shutil |
5 6 | import ssl |
6 7 | from urllib import request |
7 8 | from urllib.error import HTTPError, URLError |
8 9 | from urllib.parse import urlencode, urlparse |
9 10 | |
10 11 | |
11 12 | def _is_valid_url_host(url): |
12 13 | parsed = urlparse(url) |
13 14 | return bool(parsed.netloc) |
155 156 | |
156 157 | .. _hosts: |
157 158 | |
158 159 | | ``hosts (stringVec=['tbd1.alma.cl', 'tbd2.alma.cl'])`` - Priority-ranked list of hosts to query to obtain positions. Only one server that returns a list of antenna positions is required. That response will be written and no additional hosts will be queried. |
159 160 | | Example: hosts=["server1.alma.cl", "server2.alma.cl"] |
160 161 | |
161 162 | |
162 163 | """ |
163 164 | if not outfile: |
164 165 | raise ValueError("Parameter outfile must be specified") |
166 + | md = {"outfile": outfile} |
165 167 | if not overwrite and os.path.exists(outfile): |
166 168 | raise RuntimeError( |
167 169 | f"A file or directory named {outfile} already exists and overwrite " |
168 170 | "is False, so exiting. Either rename the existing file or directory, " |
169 171 | "change the value of overwrite to True, or both." |
170 172 | ) |
171 173 | if not hosts: |
172 174 | raise ValueError("Parameter hosts must be specified") |
173 175 | if isinstance(hosts, list) and not hosts[0]: |
174 176 | raise ValueError("The first element of the hosts list must be specified") |
177 + | md["hosts"] = hosts |
175 178 | _qa = quanta() |
176 179 | parms = {} |
177 180 | if asdm: |
178 181 | parms['asdm'] = asdm |
179 182 | else: |
180 183 | raise ValueError("parameter asdm must be specified") |
181 184 | if tw: |
182 185 | z = tw.split(",") |
183 186 | if len(z) != 2: |
184 187 | raise ValueError( |
198 201 | raise ValueError( |
199 202 | f"Parameter tw, start time ({z[0]}) must be less than end time ({z[1]})." |
200 203 | ) |
201 204 | parms["tw"] = tw |
202 205 | if snr < 0: |
203 206 | raise ValueError(f"Parameter snr ({snr}) must be non-negative.") |
204 207 | elif snr > 0: |
205 208 | parms["snr"] = snr |
206 209 | if search: |
207 210 | parms['search'] = search |
208 - | """ |
209 - | if search in ["both_latest", "both_closest"]: |
210 - | parms["search"] = search |
211 - | else: |
212 - | raise ValueError( |
213 - | f"Parameter search (={search}) must have a value of either " |
214 - | "'both_latest' or 'both_closest'." |
215 - | ) |
216 - | """ |
217 211 | qs = f"?{urlencode(parms)}" |
212 + | md.update(parms) |
218 213 | antpos = None |
219 214 | for h in hosts: |
220 215 | if not _is_valid_url_host(h): |
221 216 | raise ValueError( |
222 217 | f'Parameter hosts: {h} is not a valid host expressed as a URL.' |
223 218 | ) |
224 219 | url = f"{h}/{qs}" |
225 220 | casalog.post(f"Trying {url} ...", "NORMAL") |
226 221 | antpos = _query(url) |
227 222 | if antpos: |
223 + | md["successful_url"] = url |
228 224 | break |
229 225 | if not antpos: |
230 226 | raise RuntimeError("All URLs failed to return an antenna position list.") |
231 227 | if os.path.exists(outfile): |
232 228 | if overwrite: |
233 229 | if os.path.isdir(outfile): |
234 230 | casalog.post( |
235 231 | f"Removing existing directory {outfile} before writing new " |
236 232 | "file of same name", |
237 233 | "WARN" |
241 237 | casalog.post( |
242 238 | f"Removing existing file {outfile} before writing now file of " |
243 239 | "same name", |
244 240 | "WARN" |
245 241 | ) |
246 242 | os.remove(outfile) |
247 243 | else: |
248 244 | raise RuntimeError( |
249 245 | "Logic Error: shouldn't have gotten to this point with overwrite=False" |
250 246 | ) |
247 + | md["timestamp"] = str(datetime.now()) |
251 248 | with open(outfile, "w") as f: |
252 - | json.dump(antpos, f) |
249 + | json.dump([antpos, md], f) |