def _try_tune(cart, lo_ghz, voltage, msg, skip_servo_pa, lock_only, log): '''Helper function used by tune() to catch and log exceptions. NOTE: Any error except BadLock should set power to safe levels. ''' log.info('cart.tune %.3f ghz, %s', lo_ghz, msg) try: cart.tune(lo_ghz, voltage, skip_servo_pa=skip_servo_pa, lock_only=lock_only) except namakanui.cart.BadLock as e: log.error('tune failed at %.3f ghz, %s', lo_ghz, msg) cart.update_all()
def iv(target, rows): if target == 'hot': p_index = hot_p_index else: p_index = sky_p_index load.move('b%d_%s' % (band, target)) if target == 'hot': cart.tune(lo_ghz, 0.0, skip_servo_pa=True) cart._set_pa([pas[0], pas[1]]) cart.update_all() if namakanui.util.iftask_setup(2, 1000, 6, dcms): # level only return 1 sys.stderr.write('%s: ' % (target)) sys.stderr.flush() mult = 1.0 if band == 6: mult = -1.0 cart._ramp_sis_bias_voltages( [mult * mvs[0], mvs[0], mult * mvs[0], mvs[0]]) for i, mv in enumerate(mvs): if (i + 1) % 20 == 0: sys.stderr.write('%.2f%% ' % (0.0 + 50 * i / len(mvs))) sys.stderr.flush() cart.update_all() # for anyone monitoring for po in range(2): cart.femc.set_sis_voltage(cart.ca, po, 0, mult * mv) cart.femc.set_sis_voltage(cart.ca, po, 1, mv) rows[i][mv_index] = mv # start IFTASK action while we average the mixer current readings transid = drama.obey("IFTASK@if-micro", "WRITE_TP2", FILE="NONE", ITIME=0.1) # TODO: separate hot/cold mixer currents, or only calc hot for j in range(ua_n): for po in range(2): for sb in range(2): ua = cart.femc.get_sis_current(cart.ca, po, sb) * 1e3 rows[i][ua_avg_index + po * 2 + sb] += abs( ua) # for band 6 rows[i][ua_dev_index + po * 2 + sb] += ua * ua # get IFTASK reply msg = transid.wait(5) if msg.reason != drama.REA_COMPLETE or msg.status != 0: logging.error('bad reply from IFTASK.WRITE_TP2: %s', msg) return 1 for j, dcm in enumerate(dcms): rows[i][p_index + j] = msg.arg['POWER%d' % (dcm)] sys.stderr.write('\n') sys.stderr.flush() return 0
def MAIN(msg): # TODO obey/kick check try: if_arg = [1, 2][int(args.level_only)] if namakanui.util.iftask_setup(if_arg, 1000, 6, dcms): return # we want to level close to the max power for each pair of mixers. # use a coarse, full 0-2.5 PA sweep, find max power for each DCM. # we'll get a set of 4 PAs; do a finer sweep around these. # repeat again, then avg PA for 01/02 and 11/12 and do final level. if iter_adjust_levels(): return # need to save output rows since they have both hot and sky data. rows = [None] * len(pas) for i in range(len(rows)): rows[i] = [0.0] * (yf_index + len(powers)) if ip('hot', rows, pas): return if ip('sky', rows, pas): return n = ua_n * 2 for r in rows: for j in range(4): # calculate mixer current avg/dev. # iv just saves sum(x) and sum(x^2); # remember stddev is sqrt(E(x^2) - E(x)^2) avg = r[ua_avg_index + j] / n dev = (r[ua_dev_index + j] / n - avg**2)**.5 r[ua_avg_index + j] = avg r[ua_dev_index + j] = dev for j in range(len(powers)): # calculate y-factors r[yf_index + j] = r[hot_p_index + j] / r[sky_p_index + j] # write out the data sys.stdout.write(' '.join('%g' % x for x in r) + '\n') sys.stdout.flush() finally: # final timestamp sys.stdout.write( time.strftime('# %Y%m%d %H:%M:%S HST\n', time.localtime())) sys.stdout.flush() # retune the receiver to get settings back to nominal cart.tune(lo_ghz, 0.0) drama.Exit('MAIN done')
def loop(): global i, prev_powers while i < 23: time.sleep(1) i += 1 if i < 0: sys.stderr.write('.') else: sys.stderr.write('%d ' % (i)) sys.stderr.flush() if i % 5 == 4: # retune the cart; make sure it loses the lock dbm = agilent.state['dbm'] while dbm > agilent.safe_dbm and not cart.state['pll_unlock']: agilent.set_dbm(dbm) cart.update_all() dbm -= 0.1 dbm = agilent.safe_dbm agilent.set_dbm(dbm) agilent.set_output(0) time.sleep(0.05) agilent.set_output(1) agilent.set_dbm(orig_dbm) time.sleep(0.05) cart.tune(lo_ghz, 0.0) time.sleep(0.05) transid = drama.obey("IFTASK@if-micro", "WRITE_TP2", FILE="NONE", ITIME=0.1) cart.update_all() msg = transid.wait(5) if msg.reason != drama.REA_COMPLETE or msg.status != 0: logging.error('bad reply from IFTASK.WRITE_TP2: %s', msg) return if cart.state['pll_unlock']: logging.error('failed to tune') return powers = [] for dcm in dcms: powers.append(msg.arg['POWER%d' % (dcm)]) for j, (prev, curr) in enumerate(zip(prev_powers, powers)): pdiff = abs((prev - curr) / min(prev, curr)) * 100.0 if pdiff > 1.5: logging.info('%.2f%% jump in DCM %d', pdiff, dcms[j]) # let's write to the output file too, might come in handy sys.stdout.write('# jump DCM %d, %.2f%%\n' % (dcms[j], pdiff)) if i < 0: i = 0 # collect a bit more data, then quit prev_powers = powers output(powers)
def MAIN(msg): # TODO obey/kick check try: if_arg = [1, 2][int(args.level_only)] if namakanui.util.iftask_setup(if_arg, 1000, 6, dcms): return for k, pa in enumerate(pas): logging.info('========= PA: %g (%.2f%%) =========', pa, 100 * k / len(pas)) #cart._set_pa([pa,pa]) since iv retunes we do this there # need to save output rows since they have both hot and sky data. rows = [None] * len(mvs) for i in range(len(rows)): rows[i] = [0.0] * (yf_index + len(powers)) rows[i][pa_index] = pa if iv('hot', rows, pa): break if iv('sky', rows, pa): break n = ua_n * 2 for r in rows: for j in range(4): # calculate mixer current avg/dev. # iv just saves sum(x) and sum(x^2); # remember stddev is sqrt(E(x^2) - E(x)^2) avg = r[ua_avg_index + j] / n dev = (r[ua_dev_index + j] / n - avg**2)**.5 r[ua_avg_index + j] = avg r[ua_dev_index + j] = dev for j in range(len(powers)): # calculate y-factors r[yf_index + j] = r[hot_p_index + j] / r[sky_p_index + j] # write out the data sys.stdout.write(' '.join('%g' % x for x in r) + '\n') sys.stdout.flush() finally: # final timestamp sys.stdout.write( time.strftime('# %Y%m%d %H:%M:%S HST\n', time.localtime())) sys.stdout.flush() # retune the receiver to get settings back to nominal cart.tune(lo_ghz, 0.0) drama.Exit('MAIN done')
def TUNE(msg): ''' Takes three arguments, LO_GHZ, VOLTAGE, and LOCK_ONLY. If VOLTAGE is not given, PLL control voltage will not be adjusted following the initial lock. If LOCK_ONLY is True, bias voltage, PA, LNA, and magnets will not be adjusted after locking the receiver. The reference signal and IF switch must already be set externally. ''' log.debug('TUNE(%s)', msg.arg) args, kwargs = drama.parse_argument(msg.arg) lo_ghz, voltage, lock_only = tune_args(*args, **kwargs) vstr = '' if voltage is not None: vstr += ', %g V' % (voltage) if lock_only: vstr += ', LOCK_ONLY' log.info('tuning to LO %g GHz%s...', lo_ghz, vstr) cart.tune(lo_ghz, voltage, lock_only=lock_only) log.info('tuned.')
time.sleep(0.05) cart.update_all() logging.info('- dbm %.2f, pll_if %.3f', dbm, cart.state['pll_if_power']) lo_dbm = dbm + 0.2 logging.info('dbm limits: [%.2f, %.2f]', lo_dbm, hi_dbm) if lo_dbm >= hi_dbm: logging.error('bad dbm limits, bailing out.') agilent.set_dbm(agilent.safe_dbm) photonics.set_attenuation(photonics.max_att) if photonics else None sys.exit(1) # relock the receiver dbm = orig_dbm agilent.set_dbm(dbm) time.sleep(0.05) cart.tune(lo_ghz, 0.0) cart.update_all() if cart.state['pll_unlock']: logging.error('failed to retune') agilent.set_dbm(agilent.safe_dbm) photonics.set_attenuation(photonics.max_att) if photonics else None sys.exit(1) # write out a header for our output file sys.stdout.write(time.strftime('# %Y%m%d %H:%M:%S HST\n', time.localtime())) sys.stdout.write('# %s\n'%(sys.argv)) sys.stdout.write('#\n') sys.stdout.write('#dbm pll_if_power') mixers = ['01', '02', '11', '12'] uw = ['U','W'][args.band-6] dcm_0U = util.get_dcms('N%s0U'%(uw))
def iv(target, rows, pa): if target == 'hot': p_index = hot_p_index else: p_index = sky_p_index load.move('b%d_%s' % (band, target)) # TODO Maybe it's wrong to relevel for each PA; it makes it harder # to compare power between PAs if the leveling is slightly different. # Ambient temperature shouldn't be changing much compared to the # difference between hot load and sky, either. # at the start of a HOT row, re-tune and re-level the power meters # at the nominal values. do not relevel on SKY or y-factor won't work. # actually re-leveling makes it difficult to compare power levels # across sweeps, so skip it. retuning is fine though. # 20200221 but ACTUALLY we're having problems with saturating power levels, # so DO relevel the detectors here. we won't be able to see # relative power levels, but we mostly only do 2 PAs these days and care # more about Y-factor values anyway. if target == 'hot': # dbm/att should already be set from namakanui.util.tune cart.tune(lo_ghz, 0.0, skip_servo_pa=True) cart._set_pa([pa, pa]) cart.update_all() if namakanui.util.iftask_setup(2, 1000, 6, dcms): # level only return 1 sys.stderr.write('%s: ' % (target)) sys.stderr.flush() # NOTE: The two SIS mixers in each polarization module are not really USB and LSB. # Rather the input to one is phase-shifted relative to the other, and their # signals are then combined to produce USB and LSB outputs. So to check the # power output and Y-factor from each mixer individually, the other one needs # to have its output disabled by setting its bias voltage to zero. # Since we still need to smoothly ramp SIS bias voltage for large changes, # we therefore do two separate loops for sis1 and sis2. # TODO: Once we have the mixers optimized individually, we might still need # to optimize their combined outputs. This will require a 2D scan of # mixer bias voltage for each PA setting. # sis1 sb = 0 mult = 1.0 if band == 6: mult = -1.0 if args.zero: cart._ramp_sis_bias_voltages([mult * mvs[0], 0.0, mult * mvs[0], 0.0]) else: cart._ramp_sis_bias_voltages( [mult * mvs[0], nom_v[1], mult * mvs[0], nom_v[3]]) for i, mv in enumerate(mvs): if (i + 1) % 20 == 0: sys.stderr.write('%.2f%% ' % (0.0 + 50 * i / len(mvs))) sys.stderr.flush() cart.update_all() # for anyone monitoring for po in range(2): cart.femc.set_sis_voltage(cart.ca, po, sb, mult * mv) rows[i][mv_index] = mv # start IFTASK action while we average the mixer current readings transid = drama.obey("IFTASK@if-micro", "WRITE_TP2", FILE="NONE", ITIME=0.1) for j in range(ua_n): for po in range(2): ua = cart.femc.get_sis_current(cart.ca, po, sb) * 1e3 rows[i][ua_avg_index + po * 2 + sb] += abs(ua) # for band 6 rows[i][ua_dev_index + po * 2 + sb] += ua * ua # get IFTASK reply msg = transid.wait(5) if msg.reason != drama.REA_COMPLETE or msg.status != 0: logging.error('bad reply from IFTASK.WRITE_TP2: %s', msg) return 1 for j, dcm in enumerate(dcm_0): rows[i][p_index + j + 0] = msg.arg['POWER%d' % (dcm)] for j, dcm in enumerate(dcm_1): rows[i][p_index + j + 16] = msg.arg['POWER%d' % (dcm)] # sis2 sb = 1 if args.zero: cart._ramp_sis_bias_voltages([0.0, mvs[0], 0.0, mvs[0]]) else: cart._ramp_sis_bias_voltages([nom_v[0], mvs[0], nom_v[2], mvs[0]]) for i, mv in enumerate(mvs): if (i + 1) % 20 == 0: sys.stderr.write('%.2f%% ' % (50.0 + 50 * i / len(mvs))) sys.stderr.flush() cart.update_all() # for anyone monitoring for po in range(2): cart.femc.set_sis_voltage(cart.ca, po, sb, mv) rows[i][mv_index] = mv # start IFTASK action while we average the mixer current readings transid = drama.obey("IFTASK@if-micro", "WRITE_TP2", FILE="NONE", ITIME=0.1) for j in range(ua_n): for po in range(2): ua = cart.femc.get_sis_current(cart.ca, po, sb) * 1e3 rows[i][ua_avg_index + po * 2 + sb] += ua rows[i][ua_dev_index + po * 2 + sb] += ua * ua # get IFTASK reply msg = transid.wait(5) if msg.reason != drama.REA_COMPLETE or msg.status != 0: logging.error('bad reply from IFTASK.WRITE_TP2: %s', msg) return 1 for j, dcm in enumerate(dcm_0): rows[i][p_index + j + 8] = msg.arg['POWER%d' % (dcm)] for j, dcm in enumerate(dcm_1): rows[i][p_index + j + 24] = msg.arg['POWER%d' % (dcm)] sys.stderr.write('\n') sys.stderr.flush() return 0
# break loop pa = 3.0 done = True pa += step if pa == 2.5: done = True if pa > 2.5 and not done: pa = 2.5 done = True #cart.femc.set_cartridge_lo_pa_pol_drain_voltage_scale(cart.ca, 0, 0.0) #cart.femc.set_cartridge_lo_pa_pol_drain_voltage_scale(cart.ca, 1, 0.0) # just tune again to restore nominal values logging.info('sweep done, retuning...') try: cart.tune(args.lo_ghz, 0.0) except Exception as e: agilent.set_dbm(agilent.safe_dbm) photonics.set_attenuation(photonics.max_att) if photonics else None logging.error('tune error: %s, IF power: %g', e, cart.state['pll_if_power']) logging.info('done, plotting...') for po in range(2): for sb in range(2): plot(pas[po], mc[po * 2 + sb], '-', label='%d%d' % (po, sb + 1)) title('PA Sweep, band %d at %g GHz' % (args.band, args.lo_ghz)) xlabel('PA Drain Voltage Scale') ylabel('Mixer Current uA')