def download_coincidences_pair(pair): path = DATAPATH % tuple(pair) tmp_path = path + '_tmp' if os.path.exists(path): print 'Skipping', pair return print 'Starting', pair, datetime.datetime.now() distance = distance_between_stations(*pair) timestamp_ranges = get_timestamp_ranges(pair) total_exposure = get_total_exposure(timestamp_ranges) with tables.open_file(tmp_path, 'w') as data: data.set_node_attr('/', 'total_exposure', total_exposure) data.set_node_attr('/', 'distance', distance) for ts_start, ts_end in timestamp_ranges: download_coincidences(data, stations=list(pair), start=gps_to_datetime(ts_start), end=gps_to_datetime(ts_end), progress=False) try: coin = data.get_node('/coincidences') except tables.NoSuchNodeError: print 'No coincidences for', pair os.rename(tmp_path, path) return rate = coin.coincidences.nrows / total_exposure data.set_node_attr('/', 'n_rate', rate) data.set_node_attr('/', 'n_coincidences', coin.coincidences.nrows) os.rename(tmp_path, path) determine_rate(path) print 'Finished', pair, datetime.datetime.now()
def download_dataset(): delta_data = genfromtxt('time_delta_fixed.tsv', delimiter='\t', dtype=None, names=['ext_timestamp', 'time_delta']) start = gps_to_datetime(delta_data['ext_timestamp'][0] / int(1e9)) end = gps_to_datetime(delta_data['ext_timestamp'][-1] / int(1e9)) with tables.open_file('data.h5', 'w') as data: download_data(data, '/s501', 501, start, end) download_data(data, '/s501_original', 501, start, end) with tables.open_file('data.h5', 'a') as data: events = data.root.s501.events idx = delta_data['ext_timestamp'].tolist().index( events[-1]['ext_timestamp']) + 1 time_delta = delta_data['time_delta'][:idx] t3 = data.root.s501.events.col('t3')[1:] t4 = data.root.s501.events.col('t4')[1:] events.modify_column(start=1, colname='t3', column=where(t3 >= 0, t3 + time_delta, t3)) events.modify_column(start=1, colname='t4', column=where(t4 >= 0, t4 + time_delta, t4)) events.flush()
def download_dataset(): print 'Downloading data . . .' with tables.open_file(DATA, 'w'): # Clear previous data pass for station in STATIONS: delta_data = genfromtxt('data/time_delta_%d.tsv' % station, delimiter='\t', dtype=None, names=['ext_timestamp', 'time_delta']) start = gps_to_datetime(delta_data['ext_timestamp'][0] / int(1e9)) end = gps_to_datetime(delta_data['ext_timestamp'][-1] / int(1e9)) with tables.open_file(DATA, 'a') as data: download_data(data, '/s%d' % station, station, start, end) download_data(data, '/s%d_original' % station, station, start, end) with tables.open_file(DATA, 'a') as data: events = data.get_node('/s%d' % station, 'events') # Data ends before delta list because I got delta data from today delta_ets_list = delta_data['ext_timestamp'].tolist() stop_idx = delta_ets_list.index(events[-1]['ext_timestamp']) + 1 time_delta = delta_data['time_delta'][:stop_idx] event_ets_list = events.col('ext_timestamp').tolist() idx = event_ets_list.index(delta_ets_list[0]) events.remove_rows(0, idx) try: last_idx = event_ets_list[idx::].index( delta_ets_list[-1]) - idx except ValueError: pass else: events.remove_rows(last_idx) events.flush() assert all( events.col('ext_timestamp') == delta_data['ext_timestamp']) t3 = events.col('t3') t4 = events.col('t4') events.modify_column(colname='t3', column=where(t3 >= 0, t3 + time_delta, t3)) events.modify_column(colname='t4', column=where(t4 >= 0, t4 + time_delta, t4)) events.flush()
def plot_histogram(data, timestamps, station_numbers): """Make a 2D histogram plot of the number of events over time per station :param data: list of lists, with the number of events. :param station_numbers: list of station numbers in the data list. """ plot = Plot(width=r'\linewidth', height=r'1.3\linewidth') plot.histogram2d(data.T[::7][1:], timestamps[::7] / 1e9, np.arange(len(station_numbers) + 1), type='reverse_bw', bitmap=True) plot.set_label( gps_to_datetime(timestamps[-1]).date().isoformat(), 'upper left') plot.set_xlimits(min=YEARS_TICKS[0] / 1e9, max=timestamps[-1] / 1e9) plot.set_xticks(YEARS_TICKS / 1e9) plot.set_xtick_labels(YEARS_LABELS) plot.set_yticks(np.arange(0.5, len(station_numbers) + 0.5)) plot.set_ytick_labels(['%d' % s for s in sorted(station_numbers)], style=r'font=\sffamily\tiny') plot.set_axis_options('ytick pos=right') plot.save_as_pdf('eventtime_histogram_network_hour')
def test_gps_to_datetime(self): for date, timestamp, _ in self.combinations: dt = datetime.datetime.strptime(date, '%B %d, %Y') self.assertEqual(clock.gps_to_datetime(timestamp), dt)
STATION = 501 # Select the timestamp with the correct GPS location, # relative to the existing station layout. GOOD_GPS = 1412347557 OTHER_GPS = 1414406502 if __name__ == "__main__": cluster = HiSPARCStations([STATION]) station = cluster.get_station(STATION) # Move detectors of the 'bad' location to the correct and get the new # relative coordinates. cluster.set_timestamp(GOOD_GPS) x0, y0 = station.get_xy_coordinates() cluster.set_timestamp(OTHER_GPS) x1, y1 = station.get_xy_coordinates() print 'Station layout was good on', gps_to_datetime(GOOD_GPS) print 'Station layout to be calculated for', gps_to_datetime(OTHER_GPS) dx = x1 - x0 dy = y1 - y0 print 'GPS position moved by: %.3f m, %.3f m.' % (dx, dy) print 'New relative detector coordinates' cluster.set_timestamp(OTHER_GPS) for d in station.detectors: d.x[d.index] -= dx d.y[d.index] -= dy print cartesian_to_compass(d.x[d.index], d.y[d.index], d.z[d.index])