def setUp(self): """Populate fake guide probes, etc.""" self.verbose = True self.name = 'guider' self.actor = TestHelper.FakeActor(self.name, self.name + 'Actor') super(GuiderTester, self).setUp() myGlobals.actorState = self.actorState self.setPoint_good = -40 self.setPoint_bad = -35 self.gi = guiderImage.GuiderImageAnalysis(self.setPoint_good) gState = GuiderState.GuiderState() self.gState = gState myGlobals.actorState.gState = self.gState # self.config = ConfigParser.ConfigParser() # self.config.read('../etc/guider.cfg') # set_default_pids(self.config, gState) # set_pid_scaling(self.config, gState) self.probeNames = {} for name in gprobeKey: gk = gprobeKey[name] gik = guideInfoKey[name] self.probeNames[name] = gk[1] gState.gprobes[gk[1]] = GuiderState.GProbe( gk[1], gprobeKey=gk, guideInfoKey=gik) if 'disabled' in name: gState.gprobes[gk[1]].disabled = True self.gState = gState
def init_probes(self, mjd, plateid, fscan_mjd, fscan_id, camera='gcam'): """Initialize the "real" guide probes. Use with tests that e.g. call guiderImageAnalysis on the MJD 57356 files. """ guiderTesterData = getGProbeTesterData(mjd=mjd, plateid=plateid, fscan_mjd=fscan_mjd, fscan_id=fscan_id, camera=camera) platedb_gprobe = guiderTesterData['platedb_gprobe'] platedb_guideInfo = guiderTesterData['platedb_guideInfo'] platedb_gprobesInUse = guiderTesterData['platedb_gprobesInUse'] for probe, info, bits in zip(platedb_gprobe, platedb_guideInfo, platedb_gprobesInUse): __, bits = bits.strip('()').split('=') self.gState.gprobes[probe[1]] = GuiderState.GProbe( probe[1], gprobeKey=probe, guideInfoKey=info, gprobeBits=int(bits, 16)) tritium = platedb_gprobe[16] self.gState.gprobes[tritium[1]] = GuiderState.GProbe(tritium[1], gprobeKey=tritium, gprobeBits=2)
def test_FrameInfo(self): plugPlateScale = 10. arcsecPerMM = 3600. / plugPlateScale gcameraPixelSize = 15e-6 gcameraMagnification = 10. guideCameraScale = gcameraMagnification * gcameraPixelSize * 1e-3 frameInfo = GuiderState.FrameInfo(1, arcsecPerMM, guideCameraScale, plugPlateScale) self.assertAlmostEqual(frameInfo.micronsPerArcsec, plugPlateScale / 3.6)
def setUp(self): """Populate fake guide probes, etc.""" self.verbose = True self.name = 'guider' self.attachCmdSets = False super(GuiderTester, self).setUp() myGlobals.actorState = self.actorState self.setPoint_good = -40 self.setPoint_bad = -35 if self.actorState.actor.location is None: self.actorState.actor.location = 'APO' gState = GuiderState.GuiderState() self.gState = gState myGlobals.actorState.gState = self.gState myGlobals.actorState.actorConfig = self.actor.config self.gi = self._call_gi(self.setPoint_good, self.actorState.actor.location) GuiderActor.set_default_pids(self.actor.config, self.gState) GuiderActor.set_pid_scaling(self.actor.config, self.gState) GuiderActor.set_telescope(self.actor.config, self.gState) GuiderActor.set_gcamera(self.actor.config, self.gState) # Initialises default gprobe and guideInfo keys guiderTesterData = getGProbeTesterData() gprobeKey = guiderTesterData['gprobeKey'] guideInfoKey = guiderTesterData['guideInfoKey'] for name in gprobeKey: gk = gprobeKey[name] gik = guideInfoKey[name] gState.gprobes[gk[1]] = GuiderState.GProbe(gk[1], gprobeKey=gk, guideInfoKey=gik) if 'disabled' in name: gState.gprobes[gk[1]].disabled = True
def test_dither_headers(self): """check that the dither keywords get into the header.""" self.actorState.models['guider'] = TestHelper.Model( 'guider', TestHelper.guiderState['guiderOnDecenter']) self.gi.cmd = self.cmd objectname = 'somename' hdu = pyfits.open(self.path(self.inDataFile))[0] frameInfo = GuiderState.FrameInfo(-1, 1, 2, 3) self.gi.fillPrimaryHDU(self.cmd, self.actorState.models, hdu, frameInfo, objectname) self.assertEqual(hdu.header['MGDPOS'], 'N')
def setUp(self): super(TestGuiderState, self).setUp() # have to set up the PID scaling values for APO self.config.read( os.path.expandvars('$GUIDERACTOR_DIR/etc/guider_APO.cfg')) set_default_pids(self.config, self.gState) set_pid_scaling(self.config, self.gState) # myGlobals.actorState.models = {'tcc':Model('axePos',[20,30,40])} for k, v in gprobeKey.items(): guideInfoKey[0] = v[1] self.gState.gprobes[v[1]] = GuiderState.GProbe( gprobeKey=v, guideInfoKey=guideInfoKey, gprobeBits=gprobebits[v[1] - 1]) self.gState.gprobes[v[1]].ugriz = ugriz
def setUp(self): super(TestGuiderState, self).setUp() #myGlobals.actorState.models = {'tcc':Model('axePos',[20,30,40])} for k, v in gprobeKey.items(): self.gState.gprobes[v[1]] = GuiderState.GProbe(gprobeKey=v) self.gState.gprobes[v[1]].ugriz = ugriz
def loadCartridge(self, cmd): """ Load a cartridge. If the cartridge ID is omitted the currently-mounted cartridge is used. Error if cartridge that isn't actually mounted is specified (unless force is also given). """ queue = myGlobals.actorState.queues[guiderActor.MASTER] force = "force" in cmd.cmd.keywords cartridge = cmd.cmd.keywords["cartridge"].values[ 0] if "cartridge" in cmd.cmd.keywords else -1 pointing = cmd.cmd.keywords["pointing"].values[ 0] if "pointing" in cmd.cmd.keywords else "A" # If they specify a plate explicitly, # we'll bypass the active table and give them what they want plate = str(cmd.cmd.keywords["plate"].values[ 0]) if "plate" in cmd.cmd.keywords else None mjd = cmd.cmd.keywords["mjd"].values[ 0] if "mjd" in cmd.cmd.keywords else None fscanId = cmd.cmd.keywords["fscanId"].values[ 0] if "fscanId" in cmd.cmd.keywords else None guideWavelength = (cmd.cmd.keywords['guideWavelength'].values[0] if 'guideWavelength' in cmd.cmd.keywords else None) # Cartridge ID of 0 means that no cartridge is loaded if cartridge == 0: gprobes = {} plate = 0 boresight_ra = float("NaN") boresight_dec = float("NaN") design_ha = ("NaN") # Send that information off to the master thread queue.put( Msg(Msg.LOAD_CARTRIDGE, cmd=cmd, cartridge=cartridge, plate=plate, pointing=pointing, boresight_ra=boresight_ra, boresight_dec=boresight_dec, design_ha=design_ha, gprobes=gprobes)) return # # Check that the claimed cartridge is actually on the telescope # actorState = guiderActor.myGlobals.actorState if force and cartridge != -1: loadedCartridge = cartridge cmd.inform('text="Assuming cartridge {} is on the telescope"' .format(loadedCartridge)) else: instrumentNumKey = actorState.models["mcp"].keyVarDict["instrumentNum"] cmdVar = actorState.actor.cmdr.call( actor="mcp", forUserCmd=cmd, cmdStr="info", keyVars=[instrumentNumKey]) if cmdVar.didFail: cmd.fail("text=\"Failed to ask mcp for info on cartridges\"") return loadedCartridge = cmdVar.getLastKeyVarData(instrumentNumKey)[0] cmd.inform( "text=\"Cartridge %s is on the telescope\"" % loadedCartridge) # Only auto-select the cart if a plate was not specified. if cartridge < 0 and plate is None: cartridge = loadedCartridge if loadedCartridge != cartridge: msg = "Expected cartridge %s, but %s is loaded" % (cartridge, loadedCartridge) if force: cmd.warn("text=\"%s\"" % (msg + "; proceeding")) else: cmd.fail("text=\"%s\"" % msg) return # cart 19 is the engineering camera, and has no info in platedb. if cartridge == 19: # don't do anything but clear the gprobes and output status. gState = actorState.gState gState.deleteAllGprobes() gState.cartridge = cartridge gState.plate = 0 gState.pointing = pointing gState.plateType = 'ecamera' gState.surveyMode = None queue.put(Msg(Msg.STATUS, cmd, finish=True)) return # Get the plate from the plateDB extraArgs = "" if plate: extraArgs += " plate=%s" % (plate) if mjd: extraArgs += " mjd=%s" % (mjd) if fscanId: extraArgs += " fscanId=%s" % (fscanId) pointingInfoKey = actorState.models['platedb'].keyVarDict[ 'pointingInfo'] guideWavelengthKey = actorState.models['platedb'].keyVarDict[ 'guideWavelength'] cmdVar = actorState.actor.cmdr.call(actor="platedb", forUserCmd=cmd, cmdStr="loadCartridge cartridge=%d pointing=%s %s" % \ (cartridge, pointing, extraArgs), keyVars=[pointingInfoKey, guideWavelengthKey]) if cmdVar.didFail: cmd.fail("text=\"Failed to lookup plate corresponding to %d/%s\"" % (cartridge, pointing)) return plate = cmdVar.getLastKeyVarData(pointingInfoKey)[0] boresight_ra = cmdVar.getLastKeyVarData(pointingInfoKey)[3] boresight_dec = cmdVar.getLastKeyVarData(pointingInfoKey)[4] design_ha = cmdVar.getLastKeyVarData(pointingInfoKey)[5] survey = cmdVar.getLastKeyVarData(pointingInfoKey)[8] surveyMode = cmdVar.getLastKeyVarData(pointingInfoKey)[9] # Retrieves the guide wavelength from the DB. If guideWavelength has # not been defined in the command, uses that. if not guideWavelength: dbGuideWavelength = cmdVar.getLastKeyVarData(guideWavelengthKey)[0] guideWavelength = int( dbGuideWavelength) if dbGuideWavelength else None else: guideWavelength = int(guideWavelength) # If the guideWavelength is not defined and the plate is APOGEE-lead, # we set the default guide wavelength if not guideWavelength or guideWavelength == -1: if survey in ['APOGEE', 'APOGEE-2']: guideWavelength = 16600 elif survey in ['APOGEE&MaNGA', 'APOGEE-2&MaNGA']: if surveyMode == 'APOGEE lead': guideWavelength = 16600 if design_ha < 0: design_ha += 360 # Lookup the valid gprobes gprobeKey = actorState.models["platedb"].keyVarDict["gprobe"] gprobesInUseKey = actorState.models["platedb"].keyVarDict[ "gprobesInUse"] cmdVar = actorState.actor.cmdr.call( actor="platedb", forUserCmd=cmd, cmdStr="getGprobes cartridge=%d pointing=%s %s" % (cartridge, pointing, extraArgs), keyVars=[gprobeKey, gprobesInUseKey]) if cmdVar.didFail: cmd.fail("text=\"Failed to lookup gprobes for cartridge %d\"" % (cartridge)) return # Unpack the various platedb guider keys into a Probe instance for each probe # NOTE: ordered so that we first set the gprobebits, then fill in the rest of the values. # as otherwise the gprobebits would overwrite some of the state we set. gprobes = {} for key in cmdVar.getLastKeyVarData(gprobesInUseKey): probeId, flags = key.strip('()').split('=') gprobes[int(probeId)] = GuiderState.GProbe(int(probeId)) gprobes[int(probeId)].gprobebits = int(flags, 16) for key in cmdVar.getKeyVarData(gprobeKey): try: gprobes[key[1]].from_platedb_gprobe(key) except (KeyError, ValueError), e: cmd.warn('text=%s' % e) cmd.warn('text="Unknown probeId %s from platedb.gprobe. %s"' % (probeId, str(key))) continue
def loadFakeGProbe(self, cmd): """Loads a fake cart (99) with one synthetic gprobe.""" queue = myGlobals.actorState.queues[guiderActor.MASTER] gprobeX = (cmd.cmd.keywords['gprobeX'].values[0] if 'gprobeX' in cmd.cmd.keywords else None) gprobeY = (cmd.cmd.keywords['gprobeY'].values[0] if 'gprobeY' in cmd.cmd.keywords else None) gprobeRadius = (cmd.cmd.keywords['gprobeRadius'].values[0] if 'gprobeRadius' in cmd.cmd.keywords else None) gprobeRA = (cmd.cmd.keywords['gprobeRA'].values[0] if 'gprobeRA' in cmd.cmd.keywords else None) gprobeDec = (cmd.cmd.keywords['gprobeDec'].values[0] if 'gprobeDec' in cmd.cmd.keywords else None) if gprobeX is None or gprobeY is None or gprobeRadius is None: cmd.fail('text=%s' % qstr('Missing gprobe information.')) return # Fakes cart, plate, and plugging information. cartridge = self.fakeCartNumber plate = -99 pointing = 'A' fscanMJD = -999 fscanID = 1 design_ha = 0. survey = 'NONE' surveyMode = 'NONE' GOOD_PROBE = '0x00' gprobes = {} gprobes[1] = GuiderState.GProbe(int(1)) gprobes[1].gprobebits = int(GOOD_PROBE, 16) # Fakes the gprobeKey information gprobeKey = [ self.fakeCartNumber, 1, # gprobe_id True, # exists gprobeX, gprobeY, gprobeRadius, 0, # Rotation 0, # x_ferrule_offset 0, # y_ferrule_offset 0, # focus_offset 'ACQUIRE' # fiber_type ] gprobes[1].from_platedb_gprobe(gprobeKey) # Fakes gprobeInfoKey gprobeInfoKey = [ 1, gprobeRA, gprobeDec, 0, # xFocal 0, # yFocal 90, # phi 0 # throughput ] gprobes[1].from_platedb_guideInfo(gprobeInfoKey) queue.put( Msg(Msg.LOAD_CARTRIDGE, cmd=cmd, cartridge=cartridge, plate=plate, pointing=pointing, fscanMJD=fscanMJD, fscanID=fscanID, boresight_ra=gprobeRA, boresight_dec=gprobeDec, design_ha=design_ha, survey=survey, surveyMode=surveyMode, gprobes=gprobes))
def loadCartridge(self, cmd): """ Load a cartridge. If the cartridge ID is omitted the currently-mounted cartridge is used. Error if cartridge that isn't actually mounted is specified (unless force is also given). """ queue = myGlobals.actorState.queues[guiderActor.MASTER] force = 'force' in cmd.cmd.keywords cartridge = cmd.cmd.keywords['cartridge'].values[0] \ if 'cartridge' in cmd.cmd.keywords else -1 pointing = cmd.cmd.keywords['pointing'].values[0] \ if 'pointing' in cmd.cmd.keywords else 'A' # If they specify a plate explicitly, we'll bypass the active table # and give them what they want plate = str(cmd.cmd.keywords['plate'].values[0] ) if 'plate' in cmd.cmd.keywords else None mjd = cmd.cmd.keywords['mjd'].values[ 0] if 'mjd' in cmd.cmd.keywords else None fscanId = cmd.cmd.keywords['fscanId'].values[ 0] if 'fscanId' in cmd.cmd.keywords else None # Cartridge ID of 0 means that no cartridge is loaded if cartridge == 0: gprobes = {} plate = 0 boresight_ra = float('NaN') boresight_dec = float('NaN') design_ha = ('NaN') # Send that information off to the master thread queue.put( Msg(Msg.LOAD_CARTRIDGE, cmd=cmd, cartridge=cartridge, plate=plate, pointing=pointing, boresight_ra=boresight_ra, boresight_dec=boresight_dec, design_ha=design_ha, gprobes=gprobes)) return # # Check that the claimed cartridge is actually on the telescope # actorState = guiderActor.myGlobals.actorState loadedCartridge = self.actor.getLoadedCartridge(cmd, actorState) if loadedCartridge is None: if not force: cmd.fail( 'text="failed retrieving the number of the cart on the telescope."' ) return else: cmd.warn( 'text="failed retrieving the number of the cart on the telescope. ' 'Proceeding with force."') else: cmd.inform("text=\"Cartridge %s is on the telescope\"" % loadedCartridge) # Only auto-select the cart if a plate was not specified. if cartridge is not None and cartridge < 0 and plate is None: cartridge = loadedCartridge if loadedCartridge != cartridge: msg = 'Expected cartridge %s, but %s is loaded' % (cartridge, loadedCartridge) if force: cmd.warn("text=\"%s\"" % (msg + '; proceeding')) else: cmd.fail("text=\"%s\"" % msg) return # cart 19 is the engineering camera, and has no info in platedb. if cartridge == 19: # don't do anything but clear the gprobes and output status. gState = actorState.gState gState.deleteAllGprobes() gState.cartridge = cartridge gState.plate = 0 gState.pointing = pointing gState.plateType = 'ecamera' gState.surveyMode = None queue.put(Msg(Msg.STATUS, cmd, finish=True)) return # Get the plate from the plateDB pointingInfoKey = actorState.models['platedb'].keyVarDict[ 'pointingInfo'] extraArgs = '' if plate: extraArgs += ' plate=%s' % (plate) if mjd: extraArgs += ' mjd=%s' % (mjd) if fscanId: extraArgs += ' fscanId=%s' % (fscanId) cmdVar = actorState.actor.cmdr.call( actor='platedb', forUserCmd=cmd, cmdStr='loadCartridge cartridge=%d pointing=%s %s' % (cartridge, pointing, extraArgs), keyVars=[pointingInfoKey]) if cmdVar.didFail: cmd.fail("text=\"Failed to lookup plate corresponding to %d/%s\"" % (cartridge, pointing)) return plate = cmdVar.getLastKeyVarData(pointingInfoKey)[0] boresight_ra = cmdVar.getLastKeyVarData(pointingInfoKey)[3] boresight_dec = cmdVar.getLastKeyVarData(pointingInfoKey)[4] design_ha = cmdVar.getLastKeyVarData(pointingInfoKey)[5] survey = cmdVar.getLastKeyVarData(pointingInfoKey)[8] surveyMode = cmdVar.getLastKeyVarData(pointingInfoKey)[9] if design_ha < 0: design_ha += 360 # Lookup the valid gprobes gprobeKey = actorState.models['platedb'].keyVarDict['gprobe'] gprobesInUseKey = actorState.models['platedb'].keyVarDict[ 'gprobesInUse'] cmdVar = actorState.actor.cmdr.call( actor='platedb', forUserCmd=cmd, cmdStr='getGprobes cartridge=%d pointing=%s %s' % (cartridge, pointing, extraArgs), keyVars=[gprobeKey, gprobesInUseKey]) if cmdVar.didFail: cmd.fail("text=\"Failed to lookup gprobes for cartridge %d\"" % (cartridge)) return # Unpack the various platedb guider keys into a Probe instance for each probe # NOTE: ordered so that we first set the gprobebits, then fill in the rest of the values. # as otherwise the gprobebits would overwrite some of the state we set. gprobes = {} for key in cmdVar.getLastKeyVarData(gprobesInUseKey): probeId, flags = key.strip('()').split('=') gprobes[int(probeId)] = GuiderState.GProbe(int(probeId)) gprobes[int(probeId)].gprobebits = int(flags, 16) for key in cmdVar.getKeyVarData(gprobeKey): try: gprobes[key[1]].from_platedb_gprobe(key) except (KeyError, ValueError) as e: cmd.warn('text=%s' % e) cmd.warn('text="Unknown probeId %s from platedb.gprobe. %s"' % (probeId, str(key))) continue # Add in the plate/fibre geometry from plPlugMapM plPlugMapMKey = actorState.models['platedb'].keyVarDict['plPlugMapM'] guideInfoKey = actorState.models['platedb'].keyVarDict['guideInfo'] cmdVar = actorState.actor.cmdr.call( actor='platedb', forUserCmd=cmd, cmdStr='getGprobesPlateGeom %s' % (extraArgs), keyVars=[guideInfoKey, plPlugMapMKey]) if cmdVar.didFail: cmd.fail( 'text=%s' % qstr("Failed to lookup gprobes's geometry for cartridge %d" % (cartridge))) return assert int(cmdVar.getLastKeyVarData(plPlugMapMKey)[0]) == plate fscanMJD = cmdVar.getLastKeyVarData(plPlugMapMKey)[1] fscanID = cmdVar.getLastKeyVarData(plPlugMapMKey)[2] # unpack the platedb guideInfo keys into the probe for key in cmdVar.getKeyVarData(guideInfoKey): try: gprobes[key[0]].from_platedb_guideInfo(key) except (KeyError, ValueError) as e: cmd.warn('text=%s' % e) cmd.warn('text="Unknown probeId %d from plugmap file. %s"' % (key[0], str(key))) continue # Add in the refraction functions from plateGeomCoeffs # # I'm not sure how to get numeric pointing IDs, but it turns out that # shared plates will only ever have one pointing. pointingID = 1 if pointing != 'A': cmd.warn( 'text="pointing name is %s, but we are using pointing #1. This is probably OK."' % (pointing)) result = self.addGuideOffsets(cmd, plate, pointingID, gprobes) if result is False: return # LCOHACK: test adding profile (focus) errors based on guide x/yFocal self.add_prof_offsets(cmd, plate, gprobes) # Send that information off to the master thread # queue.put( Msg(Msg.LOAD_CARTRIDGE, cmd=cmd, cartridge=cartridge, plate=plate, pointing=pointing, fscanMJD=fscanMJD, fscanID=fscanID, boresight_ra=boresight_ra, boresight_dec=boresight_dec, design_ha=design_ha, survey=survey, surveyMode=surveyMode, gprobes=gprobes))