annotate sitesurvey.py @ 56:91b476ebc0f2

Run through 2to3
author Daniel O'Connor <doconnor@gsoft.com.au>
date Tue, 08 Dec 2020 14:00:45 +1030
parents 7ba7207df078
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
31
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1 #!/usr/bin/env python
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
3 # Copyright (c) 2011
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
4 # Daniel O'Connor <darius@dons.net.au>. All rights reserved.
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
5 #
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
6 # Redistribution and use in source and binary forms, with or without
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
7 # modification, are permitted provided that the following conditions
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
8 # are met:
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
9 # 1. Redistributions of source code must retain the above copyright
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
10 # notice, this list of conditions and the following disclaimer.
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
11 # 2. Redistributions in binary form must reproduce the above copyright
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
12 # notice, this list of conditions and the following disclaimer in the
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
13 # documentation and/or other materials provided with the distribution.
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
14 #
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
15 # THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
18 # ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
25 # SUCH DAMAGE.
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
26 #
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
27
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
28 import calendar
56
91b476ebc0f2 Run through 2to3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 43
diff changeset
29 import configparser
31
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
30 import datetime
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
31 import exceptions
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
32 import numpy
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
33 import os
42
184ea77c10e7 Use scipys interpolation routines rather than hand rolling.
Daniel O'Connor <darius@dons.net.au>
parents: 32
diff changeset
34 import scipy.interpolate
31
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
35 import scpi
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
36 import specan
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
37 import sys
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
38 import time
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
39
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
40 defaults = {}
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
41 confname = "sitesurvey.ini"
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
42 confpaths = [ ".", os.path.dirname(os.path.realpath(sys.argv[0]))]
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
43
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
44 class Experiment(object):
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
45 def __init__(self, conf, name):
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
46 if not conf.has_section(name):
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
47 raise exceptions.KeyError("No section for experiment " + name)
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
48
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
49 self.name = name
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
50 self.recurrence = None
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
51 self.opts = {}
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
52 for k, v in conf.items(name):
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
53 if k == "recurrence":
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
54 # In seconds
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
55 self.recurrence = int(v)
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
56 continue
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
57
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
58 try:
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
59 self.opts[k] = int(v)
56
91b476ebc0f2 Run through 2to3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 43
diff changeset
60 except exceptions.ValueError as e:
31
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
61 try:
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
62 self.opts[k] = float(v)
56
91b476ebc0f2 Run through 2to3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 43
diff changeset
63 except exceptions.ValueError as e:
31
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
64 self.opts[k] = v
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
65
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
66 if self.recurrence == None:
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
67 raise exceptions.KeyError("Mandatory parameter 'recurrence' is missing")
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
68 self.recurrence = datetime.timedelta(seconds = self.recurrence)
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
69
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
70 self.last_run = None
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
71
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
72 def __repr__(self):
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
73 return "<" + self.name + ">"
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
74
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
75 class CalFile(object):
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
76 def __init__(self, fname):
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
77 f = file(fname)
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
78 if f.readline().strip() != "Frequencies":
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
79 raise exceptions.SyntaxError("Format of cal file incorrect (frequencies header missing)")
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
80 freqs = numpy.fromstring(f.readline().strip(), sep = ',')
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
81 if f.readline().strip() != "Gain":
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
82 raise exceptions.SyntaxError("Format of cal file incorrect (gains header missing)")
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
83 gains = numpy.fromstring(f.readline().strip(), sep = ',')
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
84 if len(gains) != len(freqs):
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
85 raise exceptions.SyntaxError("Format of cal file incorrect (length of gain and freqs aren't equal)")
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
86
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
87 self.calfreqs = freqs
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
88 self.calgains = gains
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
89
42
184ea77c10e7 Use scipys interpolation routines rather than hand rolling.
Daniel O'Connor <darius@dons.net.au>
parents: 32
diff changeset
90 # Create interpolation function
43
7ba7207df078 Manually range check, older scipy's don't have bound_error.
Daniel O'Connor <darius@dons.net.au>
parents: 42
diff changeset
91 self._interp = scipy.interpolate.interp1d(self.calfreqs, self.calgains)
31
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
92
43
7ba7207df078 Manually range check, older scipy's don't have bound_error.
Daniel O'Connor <darius@dons.net.au>
parents: 42
diff changeset
93 def interp(self, freqs):
7ba7207df078 Manually range check, older scipy's don't have bound_error.
Daniel O'Connor <darius@dons.net.au>
parents: 42
diff changeset
94 if numpy.any(freqs < self.calfreqs[0]) or numpy.any(freqs > self.calfreqs[-1]):
7ba7207df078 Manually range check, older scipy's don't have bound_error.
Daniel O'Connor <darius@dons.net.au>
parents: 42
diff changeset
95 raise exceptions.SyntaxError("Frequency out of range of calibration %f - %f" % (calfreqs[0], calfreqs[-1]))
7ba7207df078 Manually range check, older scipy's don't have bound_error.
Daniel O'Connor <darius@dons.net.au>
parents: 42
diff changeset
96 return self._interp(freqs)
7ba7207df078 Manually range check, older scipy's don't have bound_error.
Daniel O'Connor <darius@dons.net.au>
parents: 42
diff changeset
97
31
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
98 def getexpt(sequence):
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
99 '''Given a sequence return the experiment which should be run next and how long until it should start'''
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
100
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
101 now = datetime.datetime.utcnow()
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
102 #print "now is " + str(now)
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
103 soonestdly = None
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
104 soonestexp = None
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
105
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
106 for e in sequence:
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
107 #print "Looking at " + str(e)
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
108 # If an experiment has ever run do it now
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
109 if e.last_run == None:
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
110 return e, datetime.timedelta(0)
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
111
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
112 # Time until this experiment should be run
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
113 nextrun = e.last_run + e.recurrence
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
114 dly = nextrun - now
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
115 #print "Last run was at %s, nextrun at %s, rec = %s, dly = %s / %f" % (str(e.last_run), str(nextrun), str(e.recurrence), str(dly), dly.total_seconds())
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
116 # Haven't looked at an experiment yet or this one is sooner
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
117 if soonestdly == None or dly < soonestdly:
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
118 #print "sooner"
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
119 soonestdly = dly
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
120 soonestexp = e
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
121
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
122 if soonestdly < datetime.timedelta(0):
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
123 #print "Capping " + e.name + " to run now"
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
124 soonestdly = datetime.timedelta(0)
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
125
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
126 #print "Returning " + str(soonestexp)
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
127 return soonestexp, soonestdly
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
128
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
129 def getsweep(inst, conf):
56
91b476ebc0f2 Run through 2to3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 43
diff changeset
130 print(" Sending configuration")
31
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
131
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
132 for k in conf:
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
133 #time.sleep(0.3)
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
134 inst.setconf(k, conf[k])
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
135
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
136 # Otherwise the R&S doens't respond..
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
137 #time.sleep(0.3)
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
138 rconf = inst.dumpconf()
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
139 fstart = rconf['fstart']
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
140 fstop = rconf['fstop']
56
91b476ebc0f2 Run through 2to3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 43
diff changeset
141 print(" Configuration is " + str(rconf))
31
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
142
56
91b476ebc0f2 Run through 2to3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 43
diff changeset
143 print(" Fetching trace")
31
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
144 yaxis = inst.gettrace()
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
145 xaxis = numpy.arange(fstart, fstop, (fstop - fstart) / yaxis.shape[0])
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
146
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
147 return xaxis, yaxis, rconf
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
148
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
149 def savesweep(fname, exp, x, y):
56
91b476ebc0f2 Run through 2to3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 43
diff changeset
150 print(" Saving to " + fname)
31
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
151 f = open(fname, 'wb')
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
152 for k in exp:
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
153 f.write("%s %s\n" % (k.upper(), str(exp[k])))
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
154 f.write("XDATA ")
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
155 numpy.savetxt(f, [x], delimiter = ', ', fmt = '%.3f') # Produces a trailing \n
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
156 f.write("YDATA ")
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
157 numpy.savetxt(f, [y], delimiter = ', ', fmt = '%.3f')
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
158 del f
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
159
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
160 def total_seconds(td):
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
161 return (td.microseconds + (td.seconds + td.days * 24.0 * 3600.0) * 10.0**6) / 10.0**6
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
162
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
163 if __name__ == '__main__':
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
164 # Read in config file(s)
56
91b476ebc0f2 Run through 2to3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 43
diff changeset
165 conf = configparser.SafeConfigParser(defaults)
91b476ebc0f2 Run through 2to3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 43
diff changeset
166 r = conf.read([os.path.join(a, confname) for a in confpaths])
31
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
167 if len(r) == 0:
56
91b476ebc0f2 Run through 2to3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 43
diff changeset
168 print("Unable to find any configuration file(s)")
31
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
169 sys.exit(1)
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
170
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
171 if not conf.has_section('general'):
56
91b476ebc0f2 Run through 2to3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 43
diff changeset
172 print("Configuration file doesn't have a 'general' section")
31
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
173 sys.exit(1)
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
174
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
175 if not conf.has_option('general', 'url'):
56
91b476ebc0f2 Run through 2to3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 43
diff changeset
176 print("Configuration file doesn't have a 'url' option in the 'general' section")
31
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
177 sys.exit(1)
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
178
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
179 if not conf.has_option('general', 'type'):
56
91b476ebc0f2 Run through 2to3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 43
diff changeset
180 print("Configuration file doesn't have a 'type' option in the 'general' section")
31
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
181 sys.exit(1)
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
182
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
183 if not conf.has_option('general', 'sequence'):
56
91b476ebc0f2 Run through 2to3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 43
diff changeset
184 print("Configuration file doesn't have a 'sequence' option in the 'general' section")
31
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
185 sys.exit(1)
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
186
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
187 if not conf.has_option('general', 'fname'):
56
91b476ebc0f2 Run through 2to3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 43
diff changeset
188 print("Configuration file doesn't have a 'fname' option in the 'general' section")
31
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
189 sys.exit(1)
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
190
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
191 if conf.has_option('general', 'ampcal'):
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
192 ampcal = CalFile(conf.get('general', 'ampcal'))
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
193 else:
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
194 ampcal = None
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
195
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
196 if conf.has_option('general', 'antcal'):
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
197 antcal = CalFile(conf.get('general', 'antcal'))
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
198 else:
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
199 antcal = None
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
200
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
201
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
202 sequence = []
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
203 seqnames = conf.get('general', 'sequence').split()
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
204 for e in seqnames:
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
205 sequence.append(Experiment(conf, e))
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
206
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
207 url = conf.get('general', 'url')
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
208 insttype = conf.get('general', 'type')
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
209 fnamefmt = conf.get('general', 'fname')
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
210
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
211 # Connect to the instrument
56
91b476ebc0f2 Run through 2to3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 43
diff changeset
212 print("Connecting to " + url)
31
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
213 con = scpi.instURL(url)
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
214 con.write("*IDN?")
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
215 idn = con.read()
56
91b476ebc0f2 Run through 2to3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 43
diff changeset
216 print("Instrument is a " + idn)
31
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
217
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
218 # Get class for this instrument & instantiate it
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
219 inst = specan.getInst(insttype)(con)
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
220
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
221 while True:
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
222 # Find the next experiment to run
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
223 exp, dly = getexpt(sequence)
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
224
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
225 # Sleep if necessary
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
226 dly = total_seconds(dly)
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
227 if dly > 1:
56
91b476ebc0f2 Run through 2to3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 43
diff changeset
228 print("Sleeping for %.1f seconds" % (dly))
31
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
229 time.sleep(dly)
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
230
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
231 # Run it
56
91b476ebc0f2 Run through 2to3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 43
diff changeset
232 print("--> Running experiment " + str(exp))
31
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
233 freqs, power, opts = getsweep(inst, exp.opts)
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
234
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
235 # Adjust power based on amplifier and antenna calibration
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
236 if ampcal != None:
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
237 adj = ampcal.interp(freqs)
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
238 power = power - adj
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
239
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
240 if antcal != None:
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
241 adj = antcal.interp(freqs)
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
242 power = power - adj
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
243
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
244 # Update last run time
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
245 exp.last_run = datetime.datetime.utcnow()
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
246
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
247 # Add some informative params
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
248 tsepoch = calendar.timegm(exp.last_run.utctimetuple())
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
249
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
250 extras = { 'timestamp' : exp.last_run,
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
251 'tag' : exp.name,
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
252 }
56
91b476ebc0f2 Run through 2to3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 43
diff changeset
253 opts = dict(list(opts.items()) + list(extras.items()))
32
660a2997e720 Don't put timestamp_hex & _dec in the data file.
Daniel O'Connor <darius@dons.net.au>
parents: 31
diff changeset
254
660a2997e720 Don't put timestamp_hex & _dec in the data file.
Daniel O'Connor <darius@dons.net.au>
parents: 31
diff changeset
255 fmtextras = { 'timestamp_hex' : '%08x' % (tsepoch),
660a2997e720 Don't put timestamp_hex & _dec in the data file.
Daniel O'Connor <darius@dons.net.au>
parents: 31
diff changeset
256 'timestamp_dec' : '%d' % (tsepoch),
660a2997e720 Don't put timestamp_hex & _dec in the data file.
Daniel O'Connor <darius@dons.net.au>
parents: 31
diff changeset
257 'fstart_mhz' : '%.1f' % (float(opts['fstart']) / 1e6),
660a2997e720 Don't put timestamp_hex & _dec in the data file.
Daniel O'Connor <darius@dons.net.au>
parents: 31
diff changeset
258 'fstop_mhz' : '%.1f' % (float(opts['fstop']) / 1e6),
660a2997e720 Don't put timestamp_hex & _dec in the data file.
Daniel O'Connor <darius@dons.net.au>
parents: 31
diff changeset
259 }
56
91b476ebc0f2 Run through 2to3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 43
diff changeset
260 fmtopts = dict(list(opts.items()) + list(fmtextras.items()))
32
660a2997e720 Don't put timestamp_hex & _dec in the data file.
Daniel O'Connor <darius@dons.net.au>
parents: 31
diff changeset
261 fname = fnamefmt % fmtopts
31
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
262
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
263 # Save data
c6c86dcb54ba Add code to automate a sitesurvey (to some degree).
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
264 savesweep(fname, opts, freqs, power)