def applyPb( visMatrix, pbMatrix ) : pbCorrectedVis = visMatrix * numpy.conjugate( pbMatrix ) print "\npassband-corrected phases:" print " C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 C11 C12 C13 C14 C15" try : print numpy.array_str( numpy.angle(pbCorrectedVis, deg=True ), precision=0, suppress_small=True, max_line_width=200 ) except : print "\nsorry\n" for nband in range(0,8) : bw = SAC.queryDouble("Control.SpectralLineCorrelator.SlcBand" + str(nband+1) + ".ControlBandPoints.bandwidth") if (bw < 125.) : pbCorrectedVis[nband][0:15] = 0. pbCorrectedVis[nband+8][0:15] = 0. # ... ditch any narrowband windows antVec = numpy.nansum( pbCorrectedVis, axis=0 ) #print "\nvector-averaged antenna phases" #print " C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 C11 C12 C13 C14 C15" #print " " + numpy.array_str( numpy.angle(antVec, deg=True ), precision=0, suppress_small=True, max_line_width=200 ) amps = numpy.abs(antVec) for n in range (0,15) : if amps[n] == 0. : amps[n] = 1. return [ pbCorrectedVis, antVec/amps ]
def refocus( antList, outfile, startNominal=True, apply=False ) : """ Syntax: refocus( antList, outfile, startNominal=True, apply=False ) Performs refocusing on all antennas included in 'antList'. Allows outputting the results to a text file 'outfile'. If startNominal is chosen 'False', the script starts from the most recent focus values (the nominal focus values are fixed, hardcoded numbers for the moment). If apply=False, measurements will be taken (and output), but no changes will be made to the focus of any antenna. Example: refocus([1,2,3,10,15],"refocus-2013-03-19.txt",startNominal=False,apply=True) """ pylab.clf() pylab.ion() outfile="/array/rt/focus/focusCurves" offsets = numpy.array( [ -.9, -.6, -.3, 0., .3, .6, .9 ] ) offsets = numpy.array( [ -3., -2., -1.5, -1., -.5, 0., .5, 1., 1.5, 2.0, 3.0 ] ) timeout = 20. numpy.set_printoptions(precision=2) # for ease in querying focus values and focus status, make list of monitor point names focusNameList = [] focusStatusNameList = [] focusOK = [] ampNameList = [] for ant in antList : if (ant < 7) : focusNameList.append( "Ovro%d.Secondary.zPosition" % ant ) focusStatusNameList.append( "Ovro%d.Secondary.zMovementStatus" % ant ) focusOK.append( 1 ) else : focusNameList.append( "Bima%d.AntennaCommon.Secondary.focusZ" % (ant-6) ) focusStatusNameList.append( "Bima%d.AntennaCommon.Secondary.focusState" % (ant-6) ) focusOK.append( 0 ) ampNameList.append( 'Astro.Antenna%d.MedianAmp' % ant ) focusOK = numpy.array( focusOK ) #print focusNameList #print focusStatusNameList #print ampNameList #print focusOK # save starting focus values for all ants in subarray if startNominal : startFocus = numpy.zeros( len(antList), dtype=float ) j = 0 for ant in antList: startFocus[j] = f0[ant-1] j = j + 1 else : startFocus = numpy.array( SAC.queryMpValues( focusNameList, nothrow=True ) ) print "startfocus : ", startFocus fout=open( outfile, "a" ) fout.write("\n# %s\n" % (time.strftime( "%Y-%b-%d %H:%M", time.gmtime())) ) elev = SAC.queryDouble("Ovro1.AntennaCommon.Drive.Track.actualElevation", 20) fout.write("# elev = %.2f\n" % elev) source = SAC.queryString("Control.Subarray1.source", 20) fout.write("# source = %s\n" % source) lo1 = SAC.queryDouble("Control.Subarray1.loFreq", 20) fout.write("# LO1 = %.2f\n#\n" % lo1) fout.write("# "); for ant in antList : fout.write(" %7d" % ant) fout.write("\n") # now generate array of target focus positions zTarg = numpy.empty( [len(offsets), len(antList)], dtype=float ) amps = numpy.zeros( [len(offsets), len(antList)], dtype=float ) # Preload all focus positions for i in range(0, len(offsets)) : j = 0 for ant in antList: zTarg[i][j] = startFocus[j] + offsets[i] j = j + 1 # now step through the focus ranges, recording selfcal amps for i in range(0, len(offsets)) : j = 0 for ant in antList: #zTarg[i][j] = startFocus[j] + offsets[i] #print "focus %d to %.3f" % (ant, zTarg[i][j]) SAC.focus( None, None, zTarg[i][j], ant ) j = j + 1 print "focus to:", zTarg[i] t0 = time.time() stillTuning = True while ( (time.time() - t0) < timeout) and stillTuning : time.sleep(4.) zState = numpy.array( SAC.queryMpValues( focusStatusNameList, nothrow=True ), dtype=int ) print "waiting for : ", (zState - focusOK) if numpy.array_equiv( zState, focusOK ) : stillTuning = False print "begin integration" SAC.integrate( integTime=10., reps=1 ) time.sleep(1) amps[i] = SAC.queryMpValues( ampNameList, nothrow=True ) print amps ######## Plot amps retrieved so far ########### plotamps(zTarg,amps,antList) ############################################### fout.write(" %5.2f" % offsets[i] ) j = 0 for ant in antList : fout.write(" %7.3f" % amps[i][j] ) j = j + 1 fout.write("\n") bestGaussians = plotamps(zTarg, amps, antList, fitGaussian=True, plotCurves=True, returnSolutions=True) print bestGaussians bestFocus = numpy.zeros( [len(antList)], dtype=float ) fout.write("#\n# orig") j = 0 for ant in antList: fout.write(" %7.3f" % startFocus[j] ) j = j+1 fout.write("\n") fout.write("# gfit") j = 0 for ant in antList: fout.write(" %7.3f" % bestGaussians[j] ) j = j+1 fout.write("\n") fout.write("# best") j = 0 for ant in antList: try: bestFocus[j] = numpy.average( zTarg[:,j], weights=amps[:,j] ) except: print "Cannot average when all weights are zero, leaving focus position alone for antenna " + str(ant) bestFocus[j] = startFocus[j] bestGaussians[j] = startFocus[j] # We now have the weighted average of amplitudes for a best focus estimate, # as well as the gaussian fits. Time to judge the gaussian fits and see if they make sense, # if so: adopt them if not: stick with the weighted average. thresholdFocusChange = 0.7 if (numpy.abs(bestGaussians[j] - startFocus[j]) < thresholdFocusChange): # We'll trust the Gaussian fit bestFocus[j] = bestGaussians[j] else: # Can't trust the Gaussian fit print "FOCUS: Antenna: %2d, Current focus: %2.1f, Gaussian fit: %2.1f" % (float(ant),startFocus[j],bestGaussians[j]) print "FOCUS: Gaussian fit recommends a change in focus above the threshold of %1.1f mm! Not using that value." % thresholdFocusChange if (numpy.abs(bestFocus[j] - startFocus[j]) > thresholdFocusChange): # Weighted average is too far off as well! print "FOCUS: Antenna: %2d, Current focus: %2.1f, Weighted average: %2.1f" % (float(ant),startFocus[j],bestFocus[j]) print "FOCUS: Weighted average focus is also above threshold!" print "FOCUS: Not changing focus." bestFocus[j] = startFocus[j] if (apply == True): SAC.focus( None, None, bestFocus[j], ant ) fout.write(" %7.3f" % bestFocus[j] ) j = j+1 fout.write("\n") fout.write("# chng") j = 0 for ant in antList: if (apply == True): fout.write(" %7.3f" % (bestFocus[j]-startFocus[j]) ) else: fout.write(" %7.3f" % 0.) j = j+1 fout.write("\n\n") fout.close() print "" print "startFocus = ", startFocus print "bestFocus = ", bestFocus print "change = ", (bestFocus-startFocus)