2022-09-28 15:10:59 +02:00
|
|
|
#!/usr/bin/env python3
|
2022-11-14 20:49:35 +01:00
|
|
|
# vim: fdm=indent ts=4
|
|
|
|
|
|
|
|
"""
|
|
|
|
Add a uniformly sampled time offset
|
|
|
|
to the clock of each antenna.
|
|
|
|
"""
|
2022-09-28 15:10:59 +02:00
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
import json
|
|
|
|
import h5py
|
|
|
|
|
2022-11-10 12:04:31 +01:00
|
|
|
import aa_generate_beacon as beacon
|
2022-09-28 15:10:59 +02:00
|
|
|
|
|
|
|
clocks_fname = 'clocks.csv'
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
from os import path
|
|
|
|
import sys
|
|
|
|
|
2023-01-13 17:07:18 +01:00
|
|
|
from argparse import ArgumentParser
|
|
|
|
|
|
|
|
parser = ArgumentParser()
|
|
|
|
parser.add_argument('max_clock_offset', nargs='?', type=float, default=25, help='(Default: %(default)d)')
|
2023-02-02 18:09:10 +01:00
|
|
|
parser.add_argument('-s', '--seed', type=int, nargs='?', default=None, help='Fix seed if supplied.')
|
2023-01-13 17:07:18 +01:00
|
|
|
parser.add_argument('--uniform', action='store_const', const='uniform', dest='dist_type')
|
|
|
|
parser.add_argument('--gaussian', action='store_const', const='gauss', dest='dist_type')
|
2023-02-09 13:53:10 +01:00
|
|
|
parser.add_argument('-r','--read-clocks-file', action='store_true', dest='read_clocks_file')
|
2023-02-13 14:56:49 +01:00
|
|
|
parser.add_argument('--no-save-clocks', action='store_false', dest='save_clocks')
|
2023-01-13 17:07:18 +01:00
|
|
|
parser.set_defaults(dist_type='gauss')
|
|
|
|
|
2023-02-02 17:55:37 +01:00
|
|
|
parser.add_argument('--data-dir', type=str, default="./data", help='Path to Data Directory. (Default: %(default)s)')
|
|
|
|
|
2023-01-13 17:07:18 +01:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
max_clock_offset = args.max_clock_offset # ns
|
2022-11-14 20:49:35 +01:00
|
|
|
remake_clock_offsets = True
|
2022-09-28 15:10:59 +02:00
|
|
|
|
2023-02-02 18:09:10 +01:00
|
|
|
seed = args.seed
|
2022-09-28 15:10:59 +02:00
|
|
|
rng = np.random.default_rng(seed)
|
|
|
|
|
|
|
|
####
|
2023-02-02 17:55:37 +01:00
|
|
|
fname_dir = args.data_dir
|
2022-09-28 15:10:59 +02:00
|
|
|
clocks_fname = path.join(fname_dir, clocks_fname)
|
|
|
|
antennas_fname = path.join(fname_dir, beacon.antennas_fname)
|
|
|
|
|
|
|
|
|
|
|
|
if path.isfile(clocks_fname) and not remake_clock_offsets:
|
|
|
|
print("Clock offsets previously generated")
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
if not path.isfile(antennas_fname):
|
|
|
|
print("Antenna file cannot be found, did you try generating a beacon?")
|
|
|
|
sys.exit(1)
|
2022-11-25 11:49:55 +01:00
|
|
|
|
2022-09-28 15:10:59 +02:00
|
|
|
# read in antennas
|
|
|
|
with h5py.File(antennas_fname, 'a') as fp:
|
|
|
|
if 'antennas' not in fp.keys():
|
2022-09-28 15:54:11 +02:00
|
|
|
print("Antenna file corrupted? no antennas")
|
2022-09-28 15:10:59 +02:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
group = fp['antennas']
|
|
|
|
|
|
|
|
N_antennas = len(group.keys())
|
|
|
|
|
2023-02-09 13:53:10 +01:00
|
|
|
if args.read_clocks_file and path.isfile(clocks_fname): # read clock deviations from file
|
|
|
|
print(f"Reading clocks from {clocks_fname}.")
|
|
|
|
clock_offsets = np.loadtxt(clocks_fname)
|
|
|
|
|
|
|
|
elif True: # random clock deviations
|
2022-12-15 15:08:55 +01:00
|
|
|
print(f"Modifying clocks upto {max_clock_offset}ns.")
|
|
|
|
clock_offsets = np.zeros( N_antennas )
|
2023-01-13 17:07:18 +01:00
|
|
|
if args.dist_type == 'uniform': # uniform
|
2022-12-15 15:08:55 +01:00
|
|
|
print("Uniform distribution")
|
|
|
|
clock_offsets = max_clock_offset * (2*rng.uniform(size=N_antennas) - 1)
|
|
|
|
else: # normal
|
|
|
|
print("Gaussian distribution")
|
|
|
|
clock_offsets = max_clock_offset * rng.normal(0, 1, size=N_antennas)
|
|
|
|
else: # Hardcoded offsets
|
|
|
|
print("Hardcoded offsets")
|
|
|
|
|
|
|
|
f_beacon = 51.53e-3 # GHz
|
|
|
|
|
|
|
|
clock_offsets = np.zeros( N_antennas )
|
|
|
|
if False:
|
|
|
|
clock_offsets[59] = np.pi/2 / (2*np.pi*f_beacon)
|
|
|
|
elif True:
|
|
|
|
clock_offsets += 1/8 * 1/f_beacon
|
|
|
|
print(clock_offsets)
|
|
|
|
|
2022-09-28 15:10:59 +02:00
|
|
|
|
|
|
|
# modify time values of each antenna
|
2023-02-01 14:12:12 +01:00
|
|
|
trace_key = 'filtered_traces'
|
2022-09-28 15:10:59 +02:00
|
|
|
for i, name in enumerate(group.keys()):
|
2022-11-22 11:14:53 +01:00
|
|
|
h5ant = group[name]
|
2022-09-28 15:10:59 +02:00
|
|
|
clk_offset = clock_offsets[i]
|
|
|
|
|
2023-02-01 14:12:12 +01:00
|
|
|
|
|
|
|
if trace_key not in h5ant.keys():
|
2022-09-28 15:54:11 +02:00
|
|
|
print(f"Antenna file corrupted? no 'traces' in {name}")
|
|
|
|
sys.exit(1)
|
|
|
|
|
2022-11-22 11:14:53 +01:00
|
|
|
h5ant_attrs = h5ant.attrs
|
|
|
|
if 'clock_offset' in h5ant_attrs:
|
|
|
|
tmp_offset = h5ant_attrs['clock_offset']
|
2022-09-28 15:10:59 +02:00
|
|
|
if remake_clock_offsets:
|
2023-02-01 14:12:12 +01:00
|
|
|
h5ant[trace_key][0, :] -= tmp_offset
|
2022-12-15 15:09:44 +01:00
|
|
|
h5ant['E_AxB'][0, :] -= tmp_offset
|
2022-09-28 15:10:59 +02:00
|
|
|
else:
|
|
|
|
clock_offsets[i] = tmp_offset
|
|
|
|
continue
|
|
|
|
|
2022-11-22 11:14:53 +01:00
|
|
|
h5ant_attrs['clock_offset'] = clk_offset
|
2023-02-01 14:12:12 +01:00
|
|
|
h5ant[trace_key][0, :] += clk_offset
|
2022-12-15 15:09:44 +01:00
|
|
|
h5ant['E_AxB'][0, :] += clk_offset
|
2022-09-28 15:10:59 +02:00
|
|
|
|
|
|
|
# save to simple csv
|
2023-02-13 14:56:49 +01:00
|
|
|
if args.save_clocks:
|
|
|
|
np.savetxt(clocks_fname, clock_offsets)
|
2022-09-28 15:10:59 +02:00
|
|
|
|
|
|
|
print("Antenna clocks modified in " + str(antennas_fname))
|