def create_mesh(spectrum, mplTime): total = len(spectrum) width = len(spectrum[min(spectrum)]) x = numpy.empty((width, total + 1)) * numpy.nan y = numpy.empty((width, total + 1)) * numpy.nan z = numpy.empty((width, total + 1)) * numpy.nan j = 1 for ys in spectrum: time = utc_to_mpl(ys) if mplTime else ys xs, zs = split_spectrum(spectrum[ys]) for i in range(len(xs)): x[i, j] = xs[i] y[i, j] = time z[i, j] = zs[i] j += 1 x[:, 0] = x[:, 1] if mplTime: y[:, 0] = y[:, 1] - seconds(1) else: y[:, 0] = y[:, 1] - 1 z[:, 0] = z[:, 1] return x, y, z
def plotData(card, label, data, ax): x = [] y = [] for datum in data: if datum["data"] != None and label in datum["data"]: x.append((dt.seconds(datum["datetime"]) + 365.25 * 1969)) y.append(datum["data"][label]) ax.plot_date(x, y, "-o", label=card + " " + label)
def pps_import_file(filename): fsize = os.path.getsize(filename) # size of the file in bytes numheaders = int(fsize / (PAGESIZE + HEADERSIZE)) + 1 # number of headers in the data (+1 for first header that is always present) rawsize = fsize - (numheaders * HEADERSIZE) # size of sample data without headers in bytes samples = int(rawsize / SAMPLESIZE) # number of samples in the sample data data = np.recarray((samples,), dtype=desc_pps) f = open(filename, "rb") i = 0 # overall sample index j = 0 # page sample index k = 0 # header index headernext = True # header indicator while i < samples: if headernext: bs = f.read(HEADERSIZE) bs = unpack("%sB"%len(bs), bs) tme = pps_convtime(bs[0], bs[1], bs[2], bs[3]) l1 = (bs[4]<<8)+(bs[5]) l2 = (bs[6]<<8)+(bs[7]) temp = (bs[8]<<24)+(bs[9]<<16)+(bs[10]<<8)+(bs[11]) press = (bs[12]<<24)+(bs[13]<<16)+(bs[14]<<8)+(bs[15]) hum = (bs[16]<<24)+(bs[17]<<16)+(bs[18]<<8)+(bs[19]) headernext = False k += 1 elif k < numheaders: bs = f.read(PAGESIZE) bs = unpack("%sB"%len(bs), bs) for x in range(int(PAGESIZE/SAMPLESIZE)): data[i] = np.array((tme + seconds(x * (1 / SAMPLERATE)), (bs[0+(x*SAMPLESIZE)]<<8)+(bs[1+(x*SAMPLESIZE)]), (bs[2+(x*SAMPLESIZE)]<<8)+(bs[3+(x*SAMPLESIZE)]), (bs[4+(x*SAMPLESIZE)]<<8)+(bs[5+(x*SAMPLESIZE)]), (bs[6+(x*SAMPLESIZE)]<<8)+(bs[7+(x*SAMPLESIZE)]), (bs[8+(x*SAMPLESIZE)]<<8)+(bs[9+(x*SAMPLESIZE)]), (bs[10+(x*SAMPLESIZE)]<<8)+(bs[11+(x*SAMPLESIZE)]), l1, l2, temp, press, hum), dtype=desc_pps) i += 1 headernext = True else: bs = f.read(SAMPLESIZE) bs = unpack("%sB"%len(bs), bs) data[i] = np.array((tme + seconds(j * (1 / SAMPLERATE)), (bs[0]<<8)+(bs[1]), (bs[2]<<8)+(bs[3]), (bs[4]<<8)+(bs[5]), (bs[6]<<8)+(bs[7]), (bs[8]<<8)+(bs[9]), (bs[10]<<8)+(bs[11]), l1, l2, temp, press, hum), dtype=desc_pps) i += 1 j += 1 progress(i, samples) f.close() if i > 0: return data else: return []
def bridge_historical_and_present_ticks(stocks_data_arr, ticks_data, end_time_obj): min_length = 0 for stock_index, stock_data in enumerate(stocks_data_arr): chart_ds = stock_data.get_historical_short_term_chart_data_set() ticks_series = ticks_data[stock_index] most_recent_price = chart_ds.get_most_recent_price() most_recent_date = chart_ds.get_most_recent_dt() most_recent_dt = dates.num2date(most_recent_date).replace(tzinfo=None) seconds_left = (end_time_obj - most_recent_dt).seconds stock_data.set_is_storing_short_term() for i in range(1, seconds_left + 1): tick_date = most_recent_date + dates.seconds(i) next_tick_date = most_recent_date + dates.seconds(i + 1) # Find price of tick series with same second if ticks_series is not None: recent_ticks_data = ticks_series[ticks_series[:, 0] >= tick_date] if len(recent_ticks_data) != 0: recent_ticks_data = recent_ticks_data[ recent_ticks_data[:, 0] < next_tick_date] if len(recent_ticks_data) != 0: most_recent_price = recent_ticks_data[0][1] # Store price as stick new_tick = np.array([tick_date, most_recent_price]) chart_ds.add_tick_with_datetime_tick(new_tick) stock_data.set_finished_storing() ticks_data[stock_index] = None stock_data.set_bootstrap_is_completed() length = len(chart_ds.get_ticks()) if min_length == 0 or length < min_length: min_length = length truncate_short_term_ticks_to_length(stocks_data_arr, min_length)
def bridge_historical_and_present_ticks(stocks_data_arr, ticks_data, end_time_obj): min_length = 0 for stock_index, stock_data in enumerate(stocks_data_arr): chart_ds = stock_data.get_historical_short_term_chart_data_set() ticks_series = ticks_data[stock_index] most_recent_price = chart_ds.get_most_recent_price() most_recent_date = chart_ds.get_most_recent_dt() most_recent_dt = dates.num2date(most_recent_date).replace(tzinfo=None) seconds_left = (end_time_obj - most_recent_dt).seconds stock_data.set_is_storing_short_term() for i in range(1, seconds_left+1): tick_date = most_recent_date + dates.seconds(i) next_tick_date = most_recent_date + dates.seconds(i+1) # Find price of tick series with same second if ticks_series is not None: recent_ticks_data = ticks_series[ticks_series[:,0] >= tick_date] if len(recent_ticks_data) != 0: recent_ticks_data = recent_ticks_data[recent_ticks_data[:,0] < next_tick_date] if len(recent_ticks_data) != 0: most_recent_price = recent_ticks_data[0][1] # Store price as stick new_tick = np.array([tick_date, most_recent_price]) chart_ds.add_tick_with_datetime_tick(new_tick) stock_data.set_finished_storing() ticks_data[stock_index] = None stock_data.set_bootstrap_is_completed() length = len(chart_ds.get_ticks()) if min_length==0 or length < min_length: min_length = length truncate_short_term_ticks_to_length(stocks_data_arr, min_length)
def test_seconds(self): self.assertEqual(dates.seconds(self.seconds), float(self.expectedDays))
idx_init = idx_init[:-1] idx_stop = idx_init + t_opt + n_extr dd0 = ['01 march 2011, 05:46:00', '06 march 2011'] label1 = 'Fit residuals' label2 = 'Reduced gravity signals A' fig, ax1 = plt.subplots(1, figsize=(15,8)) ax1.grid() ax2 = ax1.twiny() ax1.plot(24*3600*(t[idx_thk - t_opt + 1: idx_thk + 1 + n_extr] - tEQ), res, 'y') ax2.plot(t[idx_thk - t_opt + 1: idx_thk + 1 + n_extr], res, 'y') for iw, wd0 in enumerate(dd0): tt = np.array([dates.seconds(item) for item in idx_stop]) tt += dates.datestr2num(wd0) ax2.plot(tt, opt_full[:,iw], lw=2, c='b') ax2.axvline(x=tEQ, c='k', ymin=0.1, ymax=0.9, lw=2) ax2.axhline(y=a_thk, c='k', lw=1, ls='--') ax2.axhline(y=-a_thk, c='k', lw=1, ls='--') locator = dates.AutoDateLocator() formatter = dates.AutoDateFormatter(locator) formatter.scaled = {1. : '%d/%m', 1./24. : '%H:%M:%S', 1./(24.*60.) : '%H:%M:%S'} ax2.xaxis.set_major_locator(locator) ax2.xaxis.set_major_formatter(formatter) ax1.set_xlabel('Time (in seconds from main shock)', fontsize=17, labelpad=12)