def gps_to_mjd(gps_time): """ Convert a floating-point GPS time in seconds to a modified Julian day. Parameters ---------- gps_time : float Time in seconds since GPS epoch Returns ------- mjd : float Modified Julian day Example ------- >>> '%.9f' % round(gps_to_mjd(1129501781.2), 9) '57316.937085648' """ gps_seconds_fraction, gps_seconds = math.modf(gps_time) mjd = lal.ConvertCivilTimeToMJD(lal.GPSToUTC(int(gps_seconds))) return mjd + gps_seconds_fraction / 86400.
def gps_to_iso8601(gps_time): """Convert a floating-point GPS time in seconds to an ISO 8601 date string.""" gps_seconds_fraction, gps_seconds = math.modf(gps_time) year, month, day, hour, minute, second, _, _, _ = lal.GPSToUTC(int(gps_seconds)) ret = '{0:04d}-{1:02d}-{2:02d}T{3:02d}:{4:02d}:{5:02d}'.format(year, month, day, hour, minute, second, gps_seconds_fraction) if gps_seconds_fraction: ret += '{0:g}'.format(gps_seconds_fraction).lstrip('0') return ret
def make_xticks(segment): # generate tick locations and labels values = list(date.UTCMidnights(*(lal.LIGOTimeGPS(t) for t in segment))) labels = [] for tm in map(lambda t: time.struct_time(lal.GPSToUTC(t)), values): if tm.tm_wday == 1: # tuesday labels.append(time.strftime("%H h, %a %b %d, %Y", tm)) else: labels.append("") return map(float, values), labels
def utc_midnight(gps): """ Truncate a LIGOTimeGPS to UTC midnight. """ # convert to UTC (as list so we can edit it) tm = list(lal.GPSToUTC(int(gps))) # truncate to midnight tm[3] = 0 # hours tm[4] = 0 # minutes tm[5] = 0 # seconds # convert back to LIGOTimeGPS return lal.LIGOTimeGPS(lal.UTCToGPS(tuple(tm)))
def gps_to_iso8601(gps_time): """ Convert a floating-point GPS time in seconds to an ISO 8601 date string. Parameters ---------- gps : float Time in seconds since GPS epoch Returns ------- iso8601 : str ISO 8601 date string (with fractional seconds) Example ------- >>> gps_to_iso8601(1000000000.01) '2011-09-14T01:46:25.010000' >>> gps_to_iso8601(1000000000) '2011-09-14T01:46:25.000000' >>> gps_to_iso8601(1000000000.999999) '2011-09-14T01:46:25.999999' >>> gps_to_iso8601(1000000000.9999999) '2011-09-14T01:46:26.000000' >>> gps_to_iso8601(1000000814.999999) '2011-09-14T01:59:59.999999' >>> gps_to_iso8601(1000000814.9999999) '2011-09-14T02:00:00.000000' """ gps_seconds_fraction, gps_seconds = math.modf(gps_time) gps_seconds_fraction = '{0:6f}'.format(gps_seconds_fraction) if gps_seconds_fraction[0] == '1': gps_seconds += 1 else: assert gps_seconds_fraction[0] == '0' assert gps_seconds_fraction[1] == '.' gps_seconds_fraction = gps_seconds_fraction[1:] year, month, day, hour, minute, second, _, _, _ = lal.GPSToUTC( int(gps_seconds)) return '{0:04d}-{1:02d}-{2:02d}T{3:02d}:{4:02d}:{5:02d}{6:s}'.format( year, month, day, hour, minute, second, gps_seconds_fraction)
"abcde", "", None, sts, 0, None) assert (ptr_ptr == sts) assert (ptr_null_ptr == sts) assert (null_ptr_ptr == None) del sts del ptr_ptr del ptr_null_ptr del null_ptr_ptr lal.CheckMemoryLeaks() print("PASSED typemaps for strings and double pointers") # check 'tm' struct conversions print("checking 'tm' struct conversions ...") gps0 = 989168284 utc0 = [2011, 5, 11, 16, 57, 49, 2, 131, 0] assert (lal.GPSToUTC(gps0) == tuple(utc0)) assert (lal.UTCToGPS(utc0) == gps0) for i in range(0, 10): gps = gps0 + i * 86400 utc = list(utc0) utc[2] = utc[2] + i utc[6] = (utc[6] + i) % 7 utc[7] = utc[7] + i utc[8] = -1 + (i % 3) assert (lal.GPSToUTC(gps)[0:8] == tuple(utc[0:8])) assert (lal.UTCToGPS(utc) == gps) utc = lal.GPSToUTC(lal.UTCToGPS(utc)) dt = datetime.datetime(*utc[0:6]) assert (utc[6] == dt.weekday()) lal.CheckMemoryLeaks() print("PASSED 'tm' struct conversions")
def plottimeseries(series, outfile, t0=0, zeroline=False, **kwargs): """ Plot a REALXTimeSeries. """ # construct list of series if hasattr(series, "__contains__"): serieslist = series else: serieslist = [series] # get limits xlim = kwargs.pop("xlim", None) ylim = kwargs.pop("ylim", None) if xlim: start, end = xlim else: start = min(float(s.epoch) for s in serieslist) end = max( float(s.epoch + s.data.length * s.deltaT) for s in serieslist) # get axis scales logx = kwargs.pop("logx", False) logy = kwargs.pop("logy", False) # get legend loc loc = kwargs.pop("loc", 0) alpha = kwargs.pop("alpha", 0.8) # get colorbar options hidden_colorbar = kwargs.pop("hidden_colorbar", False) # get savefig option bbox_inches = kwargs.pop("bbox_inches", None) # # get labels # xlabel = kwargs.pop("xlabel", None) if xlabel: unit = 1 if not xlabel: unit, timestr = plotutils.time_axis_unit(end - start) if not t0: t0 = start t0 = lal.LIGOTimeGPS(t0) if int(t0.gpsNanoSeconds) == 0: xlabel = datetime.datetime(*lal.GPSToUTC(int(t0))[:6])\ .strftime("%B %d %Y, %H:%M:%S %ZUTC") xlabel = "Time (%s) since %s (%s)" % (timestr, xlabel, int(t0)) else: xlabel = datetime.datetime(*lal.GPSToUTC(t0.gpsSeconds)[:6])\ .strftime("%B %d %Y, %H:%M:%S %ZUTC") xlabel = "Time (%s) since %s (%s)"\ % (timestr, xlabel.replace(" UTC",".%.3s UTC" % t0.gpsNanoSeconds),\ t0) t0 = float(t0) ylabel = kwargs.pop("ylabel", "Amplitude (counts)") title = kwargs.pop("title", "") subtitle = kwargs.pop("subtitle", "") # # set default line params # color = kwargs.pop("color", None) if isinstance(color, str): color = color.split(',') if len(color) == 1: color = [color[0]] * len(serieslist) kwargs2 = dict() kwargs2.update(kwargs) if kwargs.has_key("marker"): kwargs.setdefault("markersize", 5) kwargs2.setdefault("markersize", 2) else: kwargs.setdefault("linestyle", "-") kwargs.setdefault("linewidth", "1") kwargs2.setdefault("linewidth", "0.1") # # make plot # allnames = [s.name for s in serieslist] namedseries = [ s for s in serieslist if not re.search("(min|max)\Z", s.name) ] plot = plotutils.SimplePlot(xlabel, ylabel, title, subtitle) for i,(series,c) in enumerate(itertools.izip(namedseries,\ plotutils.default_colors())): if color: c = color[serieslist.index(series)] x = numpy.arange(series.data.length) * series.deltaT +\ float(series.epoch) - float(t0) x = x.astype(float) x /= unit d = series.data.data if logy and ylim: numpy.putmask(d, d == 0, ylim[0] - abs(ylim[0]) * 0.01) plot.add_content(x, d, color=c,\ label=plotutils.display_name(series.name), **kwargs) # find min/max and plot for i, name in enumerate(allnames): for ext in ["min", "max"]: if re.match("%s[- _]%s" % (re.escape(series.name), ext), name): if color: c = color[i] series2 = serieslist[i] x2 = numpy.arange(series2.data.length) * series2.deltaT\ + float(series2.epoch) - float(t0) x2 /= unit d2 = series2.data.data if logy and ylim: numpy.putmask(d2, d2 == 0, ylim[0] - abs(ylim[0]) * 0.01) plot.ax.plot(x2, d2, color=c, **kwargs2) plot.ax.fill_between(x2, d, d2, alpha=0.1,\ color=c) # finalize plot.finalize(loc=loc, alpha=alpha) if hidden_colorbar: plotutils.add_colorbar(plot.ax, visible=False) # add zero line axis_lims = plot.ax.get_ylim() if zeroline: plot.ax.plot([0, 0], [axis_lims[0], axis_lims[1]], 'r--', linewidth=2) plot.ax.set_ylim([axis_lims[0], axis_lims[1]]) # set logscale if logx: plot.ax.xaxis.set_scale("log") if logy: plot.ax.yaxis.set_scale("log") plot.ax._update_transScale() # format axes if xlim: xlim = (numpy.asarray(xlim).astype(float) - t0) / unit plot.ax.set_xlim(xlim) if ylim: plot.ax.set_ylim(ylim) plotutils.set_time_ticks(plot.ax) plotutils.set_minor_ticks(plot.ax, x=False) # save and close plot.savefig(outfile, bbox_inches=bbox_inches,\ bbox_extra_artists=plot.ax.texts) plot.close()
def plotspectrogram(sequencelist, outfile, epoch=0, deltaT=1, f0=0, deltaF=1,\ t0=0, ydata=None, **kwargs): """ Plots a list of REAL?VectorSequences on a time-frequency-amplitude colour map. The epochand deltaT arguments define the spacing in the x direction for the given VectorSequence, and similarly f0 and deltaF in the y-direction. If a list of VectorSequences is given, any of these arguments can be in list form, one for each sequence. ydata can be given as to explicitly define the frequencies at which the sequences are sampled. """ # construct list of series if not hasattr(sequencelist, "__contains__"): sequencelist = [sequencelist] numseq = len(sequencelist) # format variables if isinstance(epoch, numbers.Number) or isinstance(epoch, lal.LIGOTimeGPS): epoch = [epoch] * numseq epoch = map(float, epoch) if not len(epoch) == numseq: raise ValueError("Wrong number of epoch arguments given.") if isinstance(deltaT, numbers.Number): deltaT = [deltaT] * numseq deltaT = map(float, deltaT) if not len(deltaT) == numseq: raise ValueError("Wrong number of deltaT arguments given.") if not ydata is None: if isinstance(f0, numbers.Number): f0 = [f0] * numseq f0 = map(float, f0) if not len(f0) == numseq: raise ValueError("Wrong number of f0 arguments given.") if isinstance(deltaF, numbers.Number): deltaF = [deltaF] * numseq deltaF = map(float, deltaF) if not len(deltaF) == numseq: raise ValueError("Wrong number of deltaF arguments given.") # get limits xlim = kwargs.pop("xlim", None) ylim = kwargs.pop("ylim", None) colorlim = kwargs.pop("colorlim", None) if xlim: start, end = xlim else: start = min(epoch) end = max(e + l.length * dt\ for e,l,dt in zip(epoch, sequencelist, deltaT)) if not ydata is None and not ylim: ylim = [ydata.min(), ydata.max()] # get axis scales logx = kwargs.pop("logx", False) logy = kwargs.pop("logy", False) logcolor = kwargs.pop("logcolor", False) # get legend loc loc = kwargs.pop("loc", 0) alpha = kwargs.pop("alpha", 0.8) # get colorbar options hidden_colorbar = kwargs.pop("hidden_colorbar", False) # get savefig option bbox_inches = kwargs.pop("bbox_inches", None) # # get labels # xlabel = kwargs.pop("xlabel", None) if xlabel: unit = 1 if not xlabel: unit, timestr = plotutils.time_axis_unit(end - start) if not t0: t0 = start t0 = lal.LIGOTimeGPS(t0) if int(t0.gpsNanoSeconds) == 0: xlabel = datetime.datetime(*lal.GPSToUTC(int(t0))[:6])\ .strftime("%B %d %Y, %H:%M:%S %ZUTC") xlabel = "Time (%s) since %s (%s)" % (timestr, xlabel, int(t0)) else: xlabel = datetime.datetime(*lal.GPSToUTC(t0.gpsSeconds)[:6])\ .strftime("%B %d %Y, %H:%M:%S %ZUTC") xlabel = "Time (%s) since %s (%s)"\ % (timestr, xlabel.replace(" UTC",".%.3s UTC" % t0.gpsNanoSeconds),\ t0) t0 = float(t0) ylabel = kwargs.pop("ylabel", "Frequency (Hz)") colorlabel = kwargs.pop("colorlabel", "Amplitude") title = kwargs.pop("title", "") subtitle = kwargs.pop("subtitle", "") # # restrict data to the correct limits for plotting # interpolate = logy and ydata is None for i, sequence in enumerate(sequencelist): if interpolate: # interpolate the data onto a log-scale sequence, ydata = loginterpolate(sequence, f0[i], deltaF[i]) if logy and ylim: plotted = (ydata > ylim[0]) & (ydata <= ylim[1]) newVectorLength = int(plotted.sum()) newsequence = lal.CreateREAL8VectorSequence(sequence.length,\ newVectorLength) for j in range(sequence.length): newsequence.data[j, :] = sequence.data[j, :][plotted] del sequence sequencelist[i] = newsequence if len(sequencelist) and logy and ylim: ydata = ydata[plotted] # # format bins # xbins = [] for i in range(numseq): xmin = epoch[i] xmax = epoch[i] + sequencelist[i].length * deltaT[i] xbins.append(rate.LinearBins(float(xmin-t0)/unit, float(xmax-t0)/unit,\ 2)) ybins = [] for i in range(numseq): if ydata is not None: ydata = numpy.asarray(ydata) ymin = ydata.min() ymax = ydata.max() else: ymin = f0[i] ymax = f0[i] + sequencelist[i].vectorLength * deltaF[i] if logy: if ymin == 0: ymin = deltaF[i] ybins.append(rate.LogarithmicBins(ymin, ymax, 2)) else: ybins.append(rate.LinearBins(ymin, ymax, 2)) # # plot # kwargs.setdefault("interpolation", "kaiser") plot = plotutils.ImagePlot(xlabel=xlabel, ylabel=ylabel, title=title,\ subtitle=subtitle, colorlabel=colorlabel) for sequence, x, y in zip(sequencelist, xbins, ybins): data = numpy.ma.masked_where(numpy.isnan(sequence.data), sequence.data,\ copy=False) plot.add_content(data.T, x, y, **kwargs) # finalize plot.finalize(colorbar=True, logcolor=logcolor, minorticks=True,\ clim=colorlim) if hidden_colorbar: plotutils.add_colorbar(plot.ax, visible=False) # set logscale if logx: plot.ax.xaxis.set_scale("log") if logy: plot.ax.yaxis.set_scale("log") plot.ax._update_transScale() # format axes if xlim: xlim = (numpy.asarray(xlim).astype(float) - t0) / unit plot.ax.set_xlim(xlim) if ylim: plot.ax.set_ylim(ylim) # set grid and ticks plot.ax.grid(True, which="both") plotutils.set_time_ticks(plot.ax) plotutils.set_minor_ticks(plot.ax) # save and close plot.savefig(outfile, bbox_inches=bbox_inches,\ bbox_extra_artists=plot.ax.texts) plot.close()
def gps_to_mjd(gps_time): """Convert a floating-point GPS time in seconds to a modified Julian day.""" gps_seconds_fraction, gps_seconds = math.modf(gps_time) jd = lal.JulianDayUTC(lal.GPSToUTC(int(gps_seconds))) return jd - lal.MJD_REF + gps_seconds_fraction / 86400.
def plot_segments_history( seglistdicts, segments_to_plot=['trigger buffers', 'h(t) gate', 'state vector'], t_max=None, length=86400., labelspacing=10800., colors={ 'H1': numpy.array((1.0, 0.0, 0.0)), 'L1': numpy.array((0.0, 0.8, 0.0)), 'V1': numpy.array((1.0, 0.0, 1.0)), 'H1L1': numpy.array((.5, .5, .5)) }, fig=None, axes=None): if fig is None: fig = plt.figure(figsize=(15, 5), ) if axes is None: axes = fig.add_subplot(111) # If t_max is specified, cut the segments so they end at t_max, # otherwise set it to the current time if t_max is None: t_max = float(lal.GPSTimeNow()) else: seglist_to_drop = segments.segmentlist( [segments.segment(lal.LIGOTimeGPS(t_max), segments.PosInfinity)]) for seglistdict in seglistdicts.values(): for seglist in seglistdict.values(): seglist -= seglist_to_drop t_min = t_max - length bottom = [] width = [] left_edge = [] y_ticks = [] y_tick_labels = [] color_list = [] color_dict = { 'H1': numpy.array((1.0, 0.0, 0.0)), 'L1': numpy.array((0.0, 0.8, 0.0)), 'V1': numpy.array((1.0, 0.0, 1.0)), 'H1L1': numpy.array((.5, .5, .5)) } x_format = tkr.FuncFormatter(lambda x, pos: datetime.datetime( *lal.GPSToUTC(int(x))[:7]).strftime('%Y-%m-%d, %H:%M:%S UTC')) x_ticks = numpy.arange(t_min, t_max + labelspacing, labelspacing) for j, segtype in enumerate(segments_to_plot): for row, (ifo, segmentlist) in enumerate(seglistdicts[segtype].items()): y_ticks.append(row + 2 * j + 0.5) y_tick_labels.append('%s %s' % (ifo, segtype)) bottom.extend([row + 2 * j + 0.25] * len(segmentlist)) color_list.extend([color_dict[ifo]] * len(segmentlist)) for segment in segmentlist: width.append(float(segment[1]) - float(segment[0])) left_edge.append(float(segment[0])) edgecolor_list = ['w'] * len(bottom) fig.patch.set_alpha(0.0) axes.barh(bottom, width, height=0.5, left=left_edge, color=color_list, edgecolor=edgecolor_list) plt.xlim([t_min, t_max]) axes.xaxis.set_major_formatter(x_format) plt.yticks(y_ticks, y_tick_labels) plt.xticks(x_ticks, rotation=10.) axes.tick_params(axis='y', which='both', left='off', right='off') axes.grid(color=(0.1, 0.4, 0.5), linewidth=2) # FIXME use this grid() when we have a newer matplotlib # axes.grid(color=(0.1,0.4,0.5), linewidth=2, which='both', axis='x') # FIXME Switch to tight_layout() when matplotlib is updated try: fig.tight_layout(pad=.8) return fig, axes except AttributeError: return fig, axes
def gps_to_mjd(gps_time): """Convert a floating-point GPS time in seconds to an ISO 8601 date string.""" gps_seconds_fraction, gps_seconds = math.modf(gps_time) jd = lal.JulianDay(lal.GPSToUTC(int(gps_seconds))) return jd - lal.XLAL_MJD_REF + gps_seconds_fraction
def find_daily_cache(start, end, ifo, clustering=None, check_files=False, **kwargs): """Find Daily ihope files from the daily runs for the given span @param start GPS start time for search @param end GPS end time for search @param ifo observatory for search @param clustering tag for clustering stage to search, default: unclustered @param check_files check that the returned files can be read on disk, default False @param kwargs UNDOCUMENTED """ out = Cache() # set clustering tag if clustering == None or clustering.upper() == 'UNCLUSTERED': file_tag = 'INSPIRAL_UNCLUSTERED' elif clustering.upper() in ["100MS", "100MILLISEC"]: file_tag = 'INSPIRAL_100MILLISEC_CLUSTERED' elif clustering.upper() in ["30MS", "30MILLISEC"]: file_tag = 'INSPIRAL_30MILLISEC_CLUSTERED' elif clustering.upper() in ["16S", "16SECOND"]: file_tag = 'INSPIRAL_16SEC_CLUSTERED' # set base directory directory = kwargs.pop("directory", os.path.expanduser("~cbc/ihope_daily")) # work out days span = Segment(start, end) start = int(start) start_d = lal.UTCToGPS( datetime(*lal.GPSToUTC(start)[:6]).replace(hour=0, minute=0, second=0).timetuple()) days = [] day = start_d while day <= end: days.append(day) day += 86400 # optimise append = out.append splitext = os.path.splitext isfile = os.path.isfile pjoin = os.path.join intersects = span.intersects from_T050017 = CacheEntry.from_T050017 # loop over days gathering files for day in days: utc = datetime(*lal.GPSToUTC(day)[:6]) day_path = pjoin(directory, utc.strftime("%Y%m"), utc.strftime("%Y%m%d")) day_cache = os.path.join(day_path, "%s-%s.cache" % (ifo, file_tag)) if isfile(day_cache): with open(day_cache, "r") as f: filenames = Cache.fromfile(f).pfnlist() else: filenames = glob( os.path.join(day_path, ("%s-%s-*.xml.gz" % (ifo, file_tag)))) for filename in filenames: e = from_T050017(filename) if intersects(e.segment): append(e) out.sort(key=lambda e: e.path) return out