comparison 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
comparison
equal deleted inserted replaced
55:ad5942d22f78 56:91b476ebc0f2
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 # SUCH DAMAGE. 25 # SUCH DAMAGE.
26 # 26 #
27 27
28 import calendar 28 import calendar
29 import ConfigParser 29 import configparser
30 import datetime 30 import datetime
31 import exceptions 31 import exceptions
32 import numpy 32 import numpy
33 import os 33 import os
34 import scipy.interpolate 34 import scipy.interpolate
55 self.recurrence = int(v) 55 self.recurrence = int(v)
56 continue 56 continue
57 57
58 try: 58 try:
59 self.opts[k] = int(v) 59 self.opts[k] = int(v)
60 except exceptions.ValueError, e: 60 except exceptions.ValueError as e:
61 try: 61 try:
62 self.opts[k] = float(v) 62 self.opts[k] = float(v)
63 except exceptions.ValueError, e: 63 except exceptions.ValueError as e:
64 self.opts[k] = v 64 self.opts[k] = v
65 65
66 if self.recurrence == None: 66 if self.recurrence == None:
67 raise exceptions.KeyError("Mandatory parameter 'recurrence' is missing") 67 raise exceptions.KeyError("Mandatory parameter 'recurrence' is missing")
68 self.recurrence = datetime.timedelta(seconds = self.recurrence) 68 self.recurrence = datetime.timedelta(seconds = self.recurrence)
125 125
126 #print "Returning " + str(soonestexp) 126 #print "Returning " + str(soonestexp)
127 return soonestexp, soonestdly 127 return soonestexp, soonestdly
128 128
129 def getsweep(inst, conf): 129 def getsweep(inst, conf):
130 print " Sending configuration" 130 print(" Sending configuration")
131 131
132 for k in conf: 132 for k in conf:
133 #time.sleep(0.3) 133 #time.sleep(0.3)
134 inst.setconf(k, conf[k]) 134 inst.setconf(k, conf[k])
135 135
136 # Otherwise the R&S doens't respond.. 136 # Otherwise the R&S doens't respond..
137 #time.sleep(0.3) 137 #time.sleep(0.3)
138 rconf = inst.dumpconf() 138 rconf = inst.dumpconf()
139 fstart = rconf['fstart'] 139 fstart = rconf['fstart']
140 fstop = rconf['fstop'] 140 fstop = rconf['fstop']
141 print " Configuration is " + str(rconf) 141 print(" Configuration is " + str(rconf))
142 142
143 print " Fetching trace" 143 print(" Fetching trace")
144 yaxis = inst.gettrace() 144 yaxis = inst.gettrace()
145 xaxis = numpy.arange(fstart, fstop, (fstop - fstart) / yaxis.shape[0]) 145 xaxis = numpy.arange(fstart, fstop, (fstop - fstart) / yaxis.shape[0])
146 146
147 return xaxis, yaxis, rconf 147 return xaxis, yaxis, rconf
148 148
149 def savesweep(fname, exp, x, y): 149 def savesweep(fname, exp, x, y):
150 print " Saving to " + fname 150 print(" Saving to " + fname)
151 f = open(fname, 'wb') 151 f = open(fname, 'wb')
152 for k in exp: 152 for k in exp:
153 f.write("%s %s\n" % (k.upper(), str(exp[k]))) 153 f.write("%s %s\n" % (k.upper(), str(exp[k])))
154 f.write("XDATA ") 154 f.write("XDATA ")
155 numpy.savetxt(f, [x], delimiter = ', ', fmt = '%.3f') # Produces a trailing \n 155 numpy.savetxt(f, [x], delimiter = ', ', fmt = '%.3f') # Produces a trailing \n
160 def total_seconds(td): 160 def total_seconds(td):
161 return (td.microseconds + (td.seconds + td.days * 24.0 * 3600.0) * 10.0**6) / 10.0**6 161 return (td.microseconds + (td.seconds + td.days * 24.0 * 3600.0) * 10.0**6) / 10.0**6
162 162
163 if __name__ == '__main__': 163 if __name__ == '__main__':
164 # Read in config file(s) 164 # Read in config file(s)
165 conf = ConfigParser.SafeConfigParser(defaults) 165 conf = configparser.SafeConfigParser(defaults)
166 r = conf.read(map(lambda a: os.path.join(a, confname), confpaths)) 166 r = conf.read([os.path.join(a, confname) for a in confpaths])
167 if len(r) == 0: 167 if len(r) == 0:
168 print "Unable to find any configuration file(s)" 168 print("Unable to find any configuration file(s)")
169 sys.exit(1) 169 sys.exit(1)
170 170
171 if not conf.has_section('general'): 171 if not conf.has_section('general'):
172 print "Configuration file doesn't have a 'general' section" 172 print("Configuration file doesn't have a 'general' section")
173 sys.exit(1) 173 sys.exit(1)
174 174
175 if not conf.has_option('general', 'url'): 175 if not conf.has_option('general', 'url'):
176 print "Configuration file doesn't have a 'url' option in the 'general' section" 176 print("Configuration file doesn't have a 'url' option in the 'general' section")
177 sys.exit(1) 177 sys.exit(1)
178 178
179 if not conf.has_option('general', 'type'): 179 if not conf.has_option('general', 'type'):
180 print "Configuration file doesn't have a 'type' option in the 'general' section" 180 print("Configuration file doesn't have a 'type' option in the 'general' section")
181 sys.exit(1) 181 sys.exit(1)
182 182
183 if not conf.has_option('general', 'sequence'): 183 if not conf.has_option('general', 'sequence'):
184 print "Configuration file doesn't have a 'sequence' option in the 'general' section" 184 print("Configuration file doesn't have a 'sequence' option in the 'general' section")
185 sys.exit(1) 185 sys.exit(1)
186 186
187 if not conf.has_option('general', 'fname'): 187 if not conf.has_option('general', 'fname'):
188 print "Configuration file doesn't have a 'fname' option in the 'general' section" 188 print("Configuration file doesn't have a 'fname' option in the 'general' section")
189 sys.exit(1) 189 sys.exit(1)
190 190
191 if conf.has_option('general', 'ampcal'): 191 if conf.has_option('general', 'ampcal'):
192 ampcal = CalFile(conf.get('general', 'ampcal')) 192 ampcal = CalFile(conf.get('general', 'ampcal'))
193 else: 193 else:
207 url = conf.get('general', 'url') 207 url = conf.get('general', 'url')
208 insttype = conf.get('general', 'type') 208 insttype = conf.get('general', 'type')
209 fnamefmt = conf.get('general', 'fname') 209 fnamefmt = conf.get('general', 'fname')
210 210
211 # Connect to the instrument 211 # Connect to the instrument
212 print "Connecting to " + url 212 print("Connecting to " + url)
213 con = scpi.instURL(url) 213 con = scpi.instURL(url)
214 con.write("*IDN?") 214 con.write("*IDN?")
215 idn = con.read() 215 idn = con.read()
216 print "Instrument is a " + idn 216 print("Instrument is a " + idn)
217 217
218 # Get class for this instrument & instantiate it 218 # Get class for this instrument & instantiate it
219 inst = specan.getInst(insttype)(con) 219 inst = specan.getInst(insttype)(con)
220 220
221 while True: 221 while True:
223 exp, dly = getexpt(sequence) 223 exp, dly = getexpt(sequence)
224 224
225 # Sleep if necessary 225 # Sleep if necessary
226 dly = total_seconds(dly) 226 dly = total_seconds(dly)
227 if dly > 1: 227 if dly > 1:
228 print "Sleeping for %.1f seconds" % (dly) 228 print("Sleeping for %.1f seconds" % (dly))
229 time.sleep(dly) 229 time.sleep(dly)
230 230
231 # Run it 231 # Run it
232 print "--> Running experiment " + str(exp) 232 print("--> Running experiment " + str(exp))
233 freqs, power, opts = getsweep(inst, exp.opts) 233 freqs, power, opts = getsweep(inst, exp.opts)
234 234
235 # Adjust power based on amplifier and antenna calibration 235 # Adjust power based on amplifier and antenna calibration
236 if ampcal != None: 236 if ampcal != None:
237 adj = ampcal.interp(freqs) 237 adj = ampcal.interp(freqs)
248 tsepoch = calendar.timegm(exp.last_run.utctimetuple()) 248 tsepoch = calendar.timegm(exp.last_run.utctimetuple())
249 249
250 extras = { 'timestamp' : exp.last_run, 250 extras = { 'timestamp' : exp.last_run,
251 'tag' : exp.name, 251 'tag' : exp.name,
252 } 252 }
253 opts = dict(opts.items() + extras.items()) 253 opts = dict(list(opts.items()) + list(extras.items()))
254 254
255 fmtextras = { 'timestamp_hex' : '%08x' % (tsepoch), 255 fmtextras = { 'timestamp_hex' : '%08x' % (tsepoch),
256 'timestamp_dec' : '%d' % (tsepoch), 256 'timestamp_dec' : '%d' % (tsepoch),
257 'fstart_mhz' : '%.1f' % (float(opts['fstart']) / 1e6), 257 'fstart_mhz' : '%.1f' % (float(opts['fstart']) / 1e6),
258 'fstop_mhz' : '%.1f' % (float(opts['fstop']) / 1e6), 258 'fstop_mhz' : '%.1f' % (float(opts['fstop']) / 1e6),
259 } 259 }
260 fmtopts = dict(opts.items() + fmtextras.items()) 260 fmtopts = dict(list(opts.items()) + list(fmtextras.items()))
261 fname = fnamefmt % fmtopts 261 fname = fnamefmt % fmtopts
262 262
263 # Save data 263 # Save data
264 savesweep(fname, opts, freqs, power) 264 savesweep(fname, opts, freqs, power)