def get_tiles_to_flag(self, flaggingname=None): if (self.db is None): logger.error('Cannot retrieve flagging information without database connection') return if (flaggingname is not None): flagged_tiles = dbobj.execute('select flagged_tiles from tile_flags where starttime < %d and stoptime > %d and name = %s' % (self.gpstime, self.gpstime, pgdb.escape_string(flaggingname)), db=self.db) else: flagged_tiles = list(itertools.chain.from_iterable(dbobj.execute('select flagged_tiles from tile_flags where starttime < %d and stoptime > %d' % (self.gpstime, self.gpstime), db=self.db))) # logical or of all of the flagging sets that are returned for x in flagged_tiles: for z in x.replace('{','').replace('}','').split(','): self.tiles_to_flag.add(z) logger.info('Will flag tiles %s' % list(self.tiles_to_flag))
def getTiles(reftime=None, db=None): if db is None: db = getdb() tiles = {} if type(reftime) != int: reftime = int(execute('select gpsnow()', db=db)[0][0]) tileinfo = execute('select ti.tile_id, ti.tile_pos_east, ti.tile_pos_north, ti.tile_altitude, ti.beamformer_id, ' + 'tc.receiver_id, tc.receiver_slot, pc.correlator_index, cpm.corr_product_index, ' 'bi.gain_x, bi.gain_y, cf.flavor, cf.attenuation_offset, bool(tf.tile_id) as flag, ' + 'ci.eleclength - ri.fibre_length/cv2.velocity_factor as tile_electrical_delay, ' + 'digital_gain_multipliers ' + 'from tile_info ti ' + 'left outer join tile_flags tf on (tf.tile_id = ti.tile_id and ' 'tf.starttime < %(reftime)s and ' 'tf.stoptime > %(reftime)s) ' + 'inner join beamformer_info bi on (ti.beamformer_id = bi.beamformer_id and ' 'bi.begintime < %(reftime)s and bi.endtime > %(reftime)s) ' + 'inner join tile_connection tc on (ti.tile_id = tc.tile and ' + 'tc.begintime < %(reftime)s and tc.endtime > %(reftime)s) ' + 'inner join receiver_info ri on (ri.receiver_id = tc.receiver_id and ' + 'ri.begintime < %(reftime)s and ri.endtime > %(reftime)s) ' + 'inner join cable_info ci on (tc.cable_name = ci.name and ' + "ci.begintime < %(reftime)s and ci.endtime > %(reftime)s) " + "inner join cable_velocity_factor cv2 on cv2.type = 'fiber' " + 'inner join cable_flavor cf on (cf.flavor = ci.flavor and ' + 'cf.begintime < %(reftime)s and cf.endtime > %(reftime)s) ' + 'inner join pfb_receiver_connection pr on (pr.rx_id = tc.receiver_id and ' + 'pr.begintime < %(reftime)s and pr.endtime > %(reftime)s) ' + 'inner join pfb_correlator_mapping pc on (pc.pfb_id = pr.pfb_id and pc.pfb_slot = pr.pfb_slot and ' + 'pc.begintime < %(reftime)s and pc.endtime > %(reftime)s) ' + 'inner join siteconfig_tilecorrproductmapping cpm on cpm.rx_slot = tc.receiver_slot ' + 'where ' + 'ti.begintime < %(reftime)s and ti.endtime > %(reftime)s', data={'reftime':reftime}, db=db) for tinf in tileinfo: tid, teast, tnorth, talt, tbfid, rid, rslot, corrindex, corprodindex, tbfgainx, tbfgainy, cflavor, tcatten, flagged, ted, dgains = tinf inputnum = (corrindex - 1) * 8 + corprodindex + 1 # Miriad correlator input numbers start at 1, not 0 tiles[tid] = Tile(id=tid, pos=(teast,tnorth), altitude=talt, bf=tbfid, receiver=rid, slot=rslot, inputnum=inputnum, bfgainx=tbfgainx, bfgainy=tbfgainy, flavor=cflavor, catten=tcatten, flagged=flagged, ted=ted, dgains=dgains) return tiles
def getConnectionInfo(db=None): tiles = {} receivers = {} now = getnow(db=db) recinfo = execute('select receiver_id,sbc_name from receiver_info where begintime <= %s and endtime > %s ', (now,now), db=db) for rec in recinfo: recid,sbc_name = rec[0],rec[1] receivers[recid] = Receiver(id=recid, sbc_name=sbc_name) tileinfo = execute('select tile_id,tile_pos_east,tile_pos_north,tile_altitude,beamformer_id ' + 'from tile_info where begintime <= %s and endtime > %s ', (now,now), db=db) minn,maxn,mine,maxe = 10e3,-10e3,10e3,-10e3 for tinf in tileinfo: tiles[tinf[0]] = Tile(id=tinf[0], pos=(tinf[1],tinf[2]), altitude=tinf[3], bf=tinf[4]) coninfo = execute('select tile,receiver_id,receiver_slot ' + 'from tile_connection where begintime <= %s and endtime > %s', (now,now), db=db) db.commit() for cinf in coninfo: if cinf[0] not in tiles.keys(): print "Error - tile_connection record for tile that isn't in tileinfo" else: tiles[cinf[0]].receiver = cinf[1] tiles[cinf[0]].slot = cinf[2] for tid in tiles.keys(): t = tiles[tid] ds = dipole.DipoleState(keyval=tid, db=db) tiles[tid].ds = ds if (t.receiver is not None) and (t.slot is not None) and t.receiver in receivers.keys(): receivers[t.receiver].slots[t.slot] = t return receivers,tiles
def find_matches(observation, search_subset=None, matchfreq=True, matchdelays=True, dtmax=None, db=None): """ matches=find_matches(observation, matchfreq=True, matchdelays=True,dtmax=None, db=None) returns matches from the observation database that match the supplied observation observation can be specified via observation_number or get_observation_info.MWA_Observation object matches are via frequency and/or delays """ if db is None: logger.error('Must supply a database object') return None if not matchfreq and not matchdelays: logger.error('Must match frequencies and/or delays') return None if (not isinstance(observation, get_observation_info.MWA_Observation)): try: o=get_observation_info.MWA_Observation(observation,db=db) except: logger.error('Unable to retrieve observation information for observation %d' % observation) return None observation=o delays=observation.delays channels=observation.channels #receiver=observation.receivers[0] s='select observation_number from Obsc_Recv_Cmds where ' if not search_subset is None: s+=' observation_number IN ('+','.join(map(str,search_subset))+') and ' if matchfreq: s+='frequency_values = array[%s]' % ( ','.join([str(x) for x in channels])) if matchfreq and matchdelays: s+=' and ' if matchdelays: s+=' xdelaysetting[1:1]=array[array[%s]]' % ( ','.join([str(x) for x in delays])) if dtmax is not None: if matchfreq or matchdelays: s+=' and ' s+=' abs(observation_number - %d) < %d' % (observation.observation_number, dtmax) logger.debug("SQL = %s"%s) res=dbobj.execute(s,db=db) return set([xx[0] for xx in res])
def get_observations(starttime, stoptime, db=None): """ data=get_observations(starttime, stoptime, db=None) data is list of (starttime, stoptime, obsname, projectid, calibration) """ if db is None: logger.error('Must supply database connection') return None logger.info('Searching for observations between %d and %d' % (starttime, stoptime)) try: observations=dbobj.execute('select starttime from mwa_setting where starttime>=%d and starttime<%d order by starttime' % (starttime, stoptime), db=db) except (psycopg2.InternalError, psycopg2.ProgrammingError) , e: logger.warning('Database error=%s' % (e.pgerror)) db.rollback() return None
def getTileStatus(db=None): receivers,tiles = getConnectionInfo(db=db) idle = False sched = None now = getnow(db=db) schedinfo = execute('select starttime from mwa_setting where %s >= starttime and %s <= stoptime', (now,now), db=db) db.commit() status = getRxStatus() if len(schedinfo) == 0: idle = True elif len(schedinfo) == 1: sched = schedule.MWA_Setting(schedinfo[0][0], db=db) idle = False if sched: for tid,trec in tiles.items(): for r in sched.rfstreams.values(): if tid in r.tileset.xlist: trec.xinschedule = True if tid in r.tileset.ylist: trec.yinschedule = True for tid,trec in tiles.items(): if trec.receiver and trec.slot and trec.receiver in status: st = status[trec.receiver] try: trec.BFtemp = float(st.BFs[trec.slot].Temp) except: trec.BFtemp = None try: xAttenUps = map(int,st.ASCs[1].AttenUp.split(' ')) xAttenLos = map(int,st.ASCs[1].AttenLo.split(' ')) yAttenUps = map(int,st.ASCs[2].AttenUp.split(' ')) yAttenLos = map(int,st.ASCs[2].AttenLo.split(' ')) trec.AttenX = xAttenUps[trec.slot-1] + xAttenLos[trec.slot-1] trec.AttenY = yAttenUps[trec.slot-1] + yAttenLos[trec.slot-1] except: trec.AttenX, trec.AttenY = None,None trec.recstatus = st.DigRxStatus return tiles
def getConnectionInfo(reftime=None, db=None): if db is None: db = getdb() receivers = {} corrinputs = {} if type(reftime) != int: reftime = int(execute('select gpsnow()', db=db)[0][0]) tiles = getTiles(reftime=reftime, db=db) for tid,tile in tiles.items(): corrinputs[tile.inputnum] = tile if tile.receiver not in receivers: sbc_name = 'rec%02d' % tile.receiver receivers[tile.receiver] = Receiver(id=tile.receiver, sbc_name=sbc_name) receivers[tile.receiver]['slots'] = {} # Can't pass an empty dict to the constructor, or all receivers would share the same one receivers[tile.receiver].slots[tile.slot] = tile return receivers, tiles, corrinputs
def getnow(db=None): n = execute('select gpsnow()', db=db)[0][0] db.commit() return n
self.elevation = 90 - za self.RA, self.Dec = ephem_utils.azel2radec( self.azimuth, self.elevation, self.observation_number) self.HA = ephem_utils.HA(self.LST, self.RA, self.Dec, self.epoch) / 15.0 else: logger.warning('No coordinate specified in RFstream:\n %s\n' % self._RFstream) if (len(self.delays) == 0): # still need to get the delays # figure out active receivers try: active_receivers = dbobj.execute( 'select receiver_id from receiver_info where active = true and begintime < %d and endtime > %d' % (self.observation_number, self.observation_number), db=db) except (psycopg2.InternalError, psycopg2.ProgrammingError), e: logger.warning('Database error=%s' % (e.pgerror)) db.rollback() self.receivers = [x[0] for x in active_receivers] if (len(active_receivers) == 0): logger.warning( 'Unable to identify active receivers for starttime=%d\n' % (self.observation_number)) if len(self.receivers) > 0: for rx in xrange(len(self.receivers)): recv_cmds = schedule.Obsc_Recv_Cmds(keyval=( self.receivers[rx],
def make_instr_config(self, flaggingname=None): """ based on a recipe from Bryna Hazelton 2012-05 """ if (self.gpstime is None): logger.error('Must supply gpstime for determining instr_config') return if (self.db is None): logger.error('Cannot retrieve configuration information without database connection') return self.get_tiles_to_flag(flaggingname=flaggingname) # figure out which receivers are active active_receivers = dbobj.execute('select receiver_id from receiver_info where active = true and begintime < %d and endtime > %d' % (self.gpstime,self.gpstime), db=self.db) self.active_receivers = [x[0] for x in active_receivers] logger.info('Found active receivers %s' % self.active_receivers) if len(self.active_receivers)<1: logger.error('No active receivers found for gpstime=%d' % self.gpstime) return if 102 in self.active_receivers: # the offset for mapping from each Rx to the slot numbers rxslotoffset={1:0,102:16,3:32,4:48} else: rxslotoffset={1:0,2:16,3:32,4:48} slotispowered={} for receiver in self.active_receivers: slotispowered[int(receiver)]=True try: slot_power=dbobj.execute('select slot_power from obsc_recv_cmds where rx_id=%s and starttime=%d' % ( pgdb.escape_string(receiver),self.gpstime), db=self.db)[0][0] # if any of them is False if 'f' in slot_power: slotispowered[int(receiver)]=False except: logger.warning('Unable to retrieve slot power information for gpstime=%d, Rx=%s' % (self.gpstime,receiver)) pass # loop over each input # figure out which receiver and tile it connects to for inputnumber in xrange(_NINP): for receiver in rxslotoffset.keys(): # get the offset if rxslotoffset[receiver]==(inputnumber / _INPUTSPERRX)*_INPUTSPERRX: break slotoffset=rxslotoffset[receiver] slot=self._inptoslot[inputnumber-slotoffset] pol=self._inptopol[inputnumber-slotoffset] tile=dbobj.execute('select tile from tile_connection where receiver_id = %s and receiver_slot = %d and begintime < %d and endtime > %d' % ( receiver,slot,self.gpstime,self.gpstime), db=self.db)[0][0] cable_electrical_length=dbobj.execute('select physlength from cable_info ci inner join tile_connection tc on ci.name = tc.cable_name where tc.tile=%s and ci.begintime < %d and ci.endtime > %d and tc.begintime < %d and tc.endtime > %d' % ( pgdb.escape_string(tile),self.gpstime,self.gpstime, self.gpstime,self.gpstime), db=self.db)[0][0] # flagging flagthisinput=False if not slotispowered[int(receiver)]: flagthisinput=True if tile in self.tiles_to_flag or str(tile) in self.tiles_to_flag: flagthisinput=True t=tile_config(tile=tile, receiver=receiver, inputnumber=inputnumber, length=cable_electrical_length, pol=pol.upper(), slot=slot, flag=flagthisinput) self.inputs[inputnumber]=t