def merge_pulsar(src_pulsar_id, dest_pulsar_id, existdb=None): """Merge one pulsar entry into another. Inputs: src_pulsar_id: The ID of the pulsar entry that will be merged. NOTE: This entry will no longer exist following the merge. dest_pulsar_id: The ID of the pulsar entry that will be merged into. existdb: A (optional) existing database connection object. (Default: Establish a db connection) Outputs: None """ notify.print_info("Merging pulsar '%s' (ID: %d) into '%s' (ID: %d)" % (cache.get_pulsarname(src_pulsar_id), src_pulsar_id, cache.get_pulsarname(dest_pulsar_id), dest_pulsar_id), 2) # Connect to the database db = existdb or database.Database() db.connect() trans = db.begin() try: # Update all relevant entries in the database tables = [db.pulsar_aliases, db.timfiles, db.rawfiles, db.templates, db.parfiles, db.master_parfiles, db.master_templates, db.toas] values = {'pulsar_id': dest_pulsar_id} for table in tables: update = table.update().\ where(table.c.pulsar_id == src_pulsar_id) results = db.execute(update, values) results.close() # Remove now unused entry in the pulsars table delete = db.pulsars.delete().\ where(db.pulsars.c.pulsar_id == src_pulsar_id) results = db.execute(delete) results.close() except: trans.rollback() raise else: trans.commit() finally: if existdb is None: db.close()
def plot_cadence(toas): """Given a list of TOAs (as returned by create_timfile.get_toas(...) make a plot of observing cadence. Input: toas: A list of TOAs. Output: fig: The newly created matplotlib Figure object. """ import matplotlib.pyplot as plt fig = plt.figure(figsize=(10,6)) # Summarize TOA info pulsars = {} for toa in toas: psr = pulsars.setdefault(toa['pulsar_id'], []) psr.append(toa['mjd']) indices = [] labels = [] ax = plt.axes() for ii, (psrid, mjds) in enumerate(pulsars.iteritems()): indices.append(ii) labels.append(cache.get_pulsarname(psrid)) ax.plot(mjds, ii*np.ones_like(mjds), 'k,') ax.set_xlabel("MJD") ax.yaxis.set_ticklabels(labels) ax.yaxis.set_ticks(np.array(indices)) ax.set_ylim(-0.5, len(pulsars)-0.5) return fig
def is_curator(user_id, pulsar_id, existdb=None): """Return whether user has curator privileges for the given pulsar. Inputs: user_id: The ID of the user to check privileges for. pulsar_id: The ID of the pulsar in question. existdb: A (optional) existing database connection object. (Default: Establish a db connection) Output: curator: True if the user has curator privileges. False otherwise. """ # Check if user_id and pulsar_id are valid # Exceptions will be raise if no matches are found cache.get_userinfo(user_id) cache.get_pulsarname(pulsar_id) # Connect to the DB if necessary db = existdb or database.Database() db.connect() select = db.select([db.curators.c.user_id], \ from_obj=[db.curators.\ outerjoin(db.users, \ onclause=db.curators.c.user_id == \ db.users.c.user_id)]).\ where((db.curators.c.pulsar_id == pulsar_id) & \ db.curators.c.user_id.in_((user_id,None)) & \ db.users.c.active) result = db.execute(select) rows = result.fetchall() result.close() curator = bool(rows) return curator
def dump_pulsars(pulsar_ids=None): """Dump pulsar names and aliases to screen. Input: pulsar_ids: list of pulsar IDs to display. (Default: dump all pulsars) Outputs: None """ # Grab the pulsar alias cache once rather than accessing it multiple times pulsaralias_cache = cache.get_pulsaralias_cache() if pulsar_ids is None: pulsar_ids = sorted(pulsaralias_cache.keys()) for psrid in sorted(pulsar_ids): psrname = cache.get_pulsarname(psrid) print psrname for alias in pulsaralias_cache[psrid]: if alias == psrname: continue print alias
def show_procjobs(procjobs): print "--" * 25 for procjob in procjobs: print colour.cstring("Process Id:", underline=True, bold=True) + colour.cstring( " %d" % procjob.process_id, bold=True ) print "\nPulsar name: %s" % cache.get_pulsarname(procjob.pulsar_id) print "Rawfile (ID=%d): %s" % (procjob.rawfile_id, procjob.rawfn) if procjob.replacement_rawfile_id is not None: colour.cprint("Rawfile has been superseded by rawfile_id=%d" % procjob.replacement_rawfile_id, "warning") print "Manipulator: %s" % procjob.manipulator print " Args: %s" % procjob.manipulator_args print "Number of freq. chunks: %d" % procjob.nchan print "Number of time chunks: %d" % procjob.nsub print "Uploaded by: %s (%s)" % (procjob.real_name, procjob.email_address) print "Date and time job completed: %s" % procjob.add_time.isoformat(" ") if config.cfg.verbosity >= 1: lines = ["Template (ID=%d): %s" % (procjob.template_id, procjob.tempfn)] if procjob.parfile_id is not None: lines.append("Parfile (ID=%d): %s" % (procjob.parfile_id, procjob.parfn)) else: lines.append("No parfile installed during processing.") notify.print_info("\n".join(lines), 1) print "--" * 25
def plot_toa_histogram(toas): """Given a list of TOAs (as returned by create_timfile.get_toas(...) make histogram plots. Input: toas: A list of TOAs. Output: fig: The newly created matplotlib Figure object. """ import matplotlib.pyplot as plt fig = plt.figure(figsize=(10, 6)) # Summarize TOA info pulsars = {} telescopes = set() bands = set() psrnames = set() for toa in toas: psr = pulsars.setdefault(toa['pulsar_id'], {}) obssysid = toa['obssystem_id'] obssysinfo = cache.get_obssysinfo(obssysid) telid = obssysinfo['telescope_id'] telname = cache.get_telescope_info(telid)['telescope_name'] band = toa['band_descriptor'] bands.add(band) telescopes.add(telname) psr[telname] = psr.get(telname, 0)+1 psr[band] = psr.get(band, 0)+1 band_colours = dict(zip(sorted(bands), ['r', 'b', 'g', 'c'])) telescope_colours = {'Effelsberg': '#FFCE00', 'Jodrell': '#CE1124', 'Nancay': '#0055A4', 'WSRT': '#FF7F00', 'Sardinia': '#007FFF', 'Parkes': '#EDC9AF', 'GBT': '#22BB22', 'Arecibo': '#00BFFF'} indices = [] labels = [] telleg = {} bandleg = {} telax = plt.axes((0.15, 0.1, 0.39, 0.8)) bandax = plt.axes((0.56, 0.1, 0.39, 0.8), sharey=telax) for ii, (psrid, info) in enumerate(pulsars.iteritems()): indices.append(ii) labels.append(cache.get_pulsarname(psrid)) total = 0 for telname in sorted(telescopes): if telname in info: count = info[telname] bb = telax.barh(ii, count, height=1, left=total, color=telescope_colours[telname]) telleg[telname] = bb total += count total = 0 for band in ['P-band', 'L-band', 'S-band']: if band in info: count = info[band] bb = bandax.barh(ii, count, height=1, left=total, color=band_colours[band]) bandleg[band] = bb total += count telax.yaxis.set_ticks(np.array(indices)+0.5) telax.yaxis.set_ticklabels(labels) telax.set_ylim(-0.5, len(pulsars)+2.5) plt.setp((telax.get_xticklabels(), bandax.get_xticklabels()), rotation=30, ha='right') telax.set_xlabel("Number of TOAs") bandax.set_xlabel("Number of TOAs") telax.set_title("By telescope") bandax.set_title("By observing band") labels, handles = zip(*sorted(telleg.items())) telax.legend(handles, labels, prop=dict(size='small')) handles, labels = zip(*bandleg.items()) bandax.legend(labels, handles, prop=dict(size='small')) plt.setp(bandax.yaxis.get_ticklabels(), visible=False) return fig