def try_plots(object_id = 8400199): results = orbit_determination.Parameters.load_h5('./tests/tmp_test_data/test_sim/master/orbits/8400199_orbit_determination.h5') obs_dates = None with h5py.File('./tests/tmp_test_data/test_sim/master/orbits/8400199_obs_data.h5') as hf: for key in hf: if obs_dates is None: obs_dates = [dpt.mjd2npdt(hf['{}/mjd'.format(key)].value)] else: obs_dates += [dpt.mjd2npdt(hf['{}/mjd'.format(key)].value)] pop = plib.master_catalog() pop_ind = np.argwhere(pop['oid'] == int(object_id))[0,0] space_o = pop.get_object(pop_ind) orbit_determination.plot_orbit_uncertainty(results, true_object = space_o, time = 24.0, obs_dates = obs_dates, num=len(results.trace), symmetric = True, ) plt.show() exit() orbit_determination.plot_trace(results) orbit_determination.plot_MC_cov(results) orbit_determination.plot_scatter_trace(results) orbit_determination.plot_autocorrelation(results) orbit_determination.print_covariance(results) plt.show()
def test_master(self): pop = plib.master_catalog() assert len(pop) > 0 assert isinstance(pop, Population) so = pop.get_object(0) assert isinstance(so, SpaceObject) states = so.get_state(0) assert states.shape == (6, 1) self.assertListEqual( pop.header, ['oid', 'a', 'e', 'i', 'raan', 'aop', 'mu0', 'mjd0'] + ['A', 'm', 'd', 'C_D', 'C_R', 'Factor'])
def try_od(object_id = 8400199): #object_id = 121800762 pop = plib.master_catalog() pop_ind = np.argwhere(pop['oid'] == int(object_id))[0,0] space_o = pop.get_object(pop_ind) res = orbit_determination.orbit_determination_fsystem( path='./tests/tmp_test_data/test_sim/master/', variables=['x', 'y', 'z', 'vx', 'vy', 'vz', 'A'], params={ 'A': 1.0, }, object_id = object_id, true_object = space_o, prior_truncate = slice(None,None,50), #tracklet_truncate = slice(None,None,50), steps = 3000, tune = 1000, output_folder = './tests/tmp_test_data/test_sim/master/orbits', ) if comm.rank != 0: exit() orbit_determination.plot_orbits(res['mcmc'], start=res['start']) results = res['results'] orbit_determination.plot_trace(results) orbit_determination.plot_MC_cov(results) orbit_determination.plot_scatter_trace(results) orbit_determination.plot_autocorrelation(results) orbit_determination.print_covariance(results) styles = ['-r','-b','-g','-k'] labels = ['MAP MCMC-SCAM', 'MAP Nelder-Mead', 'Estimated Prior', 'Start value'] states = [res['mcmc'].results.MAP, res['MAP-estimation'].results.MAP, res['prior'].results.MAP, res['start']] orbit_determination.plot_residuals(res['mcmc'], states, labels, styles) plt.show()
import dpt_tools as dpt import ccsds_write ### # PART ## part = 2 #Project 5: Lets simulate our system! from DEV_demo1 import SC from DEV_demo2 import radar radar.set_scan(SC) pop = plib.master_catalog() pop.delete(slice(100, None)) #46 was detecable, but lets use the 100 since we didnt save the results SIM_TIME = 24.0*1 sim_root = '/home/danielk/IRF/E3D_PA/DEMO' sim = Simulation( radar = radar, population = pop, root = sim_root, scheduler = schlib.dynamic_scheduler, simulation_name = 'Demo simulation', )
def test_tracklets(): # Unit test # # Create tracklets and perform orbit determination # import population_library as plib import radar_library as rlib import simulate_tracking import simulate_tracklet as st import os os.system("rm -Rf /tmp/test_tracklets") os.system("mkdir /tmp/test_tracklets") m = plib.master_catalog(sort=False) # Envisat o = m.get_object(145128) print(o) e3d = rlib.eiscat_3d(beam='gauss') # time in seconds after mjd0 t_all = n.linspace(0, 24 * 3600, num=1000) passes, _, _, _, _ = simulate_tracking.find_pass_interval(t_all, o, e3d) print(passes) for p in passes[0]: # 100 observations of each pass mean_t = 0.5 * (p[1] + p[0]) print("duration %1.2f" % (p[1] - p[0])) if p[1] - p[0] > 10.0: t_obs = n.linspace(mean_t - 10, mean_t + 10, num=10) print(t_obs) meas, fnames, ecef_stdevs = st.create_tracklet( o, e3d, t_obs, hdf5_out=True, ccsds_out=True, dname="/tmp/test_tracklets") fl = glob.glob("/tmp/test_tracklets/*") for f in fl: print(f) fl2 = glob.glob("%s/*.h5" % (f)) print(fl2) fl2.sort() start_times = [] for f2 in fl2: start_times.append(re.search("(.*/track-.*)-._..h5", f2).group(1)) start_times = n.unique(start_times) print("n_tracks %d" % (len(start_times))) for t_pref in start_times[0:1]: fl2 = glob.glob("%s*.h5" % (t_pref)) n_static = len(fl2) if n_static == 3: print("Fitting track %s" % (t_pref)) f0 = "%s-0_0.h5" % (t_pref) f1 = "%s-0_1.h5" % (t_pref) f2 = "%s-0_2.h5" % (t_pref) print(f0) print(f1) print(f2) h0 = h5py.File(f0, "r") h1 = h5py.File(f1, "r") h2 = h5py.File(f2, "r") r_meas0 = h0["m_range"].value rr_meas0 = h0["m_range_rate"].value r_meas1 = h1["m_range"].value rr_meas1 = h1["m_range_rate"].value r_meas2 = h2["m_range"].value rr_meas2 = h2["m_range_rate"].value n_t = len(r_meas0) if len(r_meas1) != n_t or len(r_meas2) != n_t: print( "non-overlapping measurements, tbd, align measurement") continue p_rx = n.zeros([3, 3]) p_rx[:, 0] = h0["rx_loc"].value / 1e3 p_rx[:, 1] = h1["rx_loc"].value / 1e3 p_rx[:, 2] = h2["rx_loc"].value / 1e3 for ti in range(n_t): if h0["m_time"][ti] != h1["m_time"][ti] or h2["m_time"][ ti] != h0["m_time"][ti]: print("non-aligned measurement") continue m_r = n.array([r_meas0[ti], r_meas1[ti], r_meas2[ti]]) m_rr = n.array([rr_meas0[ti], rr_meas1[ti], rr_meas2[ti]]) ecef_state = orbital_estimation.estimate_state( m_r, m_rr, p_rx) true_state = h0["true_state"].value[ti, :] r_err = 1e3 * n.linalg.norm(ecef_state[0:3] - true_state[0:3]) v_err = 1e3 * n.linalg.norm(ecef_state[3:6] - true_state[3:6]) print("pos error %1.3f (m) vel error %1.3f (m/s)" % (1e3 * n.linalg.norm(ecef_state[0:3] - true_state[0:3]), 1e3 * n.linalg.norm(ecef_state[3:6] - true_state[3:6]))) assert r_err < 100.0 assert v_err < 50.0 h0.close() h1.close() h2.close() os.system("rm -Rf /tmp/test_tracklets")
def gen_tracklet_prior(fout, index = 10): pop = plib.master_catalog() space_o = pop.get_object(index)
def gen_simulation(part = 4): import time import sys import os import logging import glob import shutil import pymc3 as pm import h5py import scipy from mpi4py import MPI import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D # SORTS imports CORE import population_library as plib from simulation import Simulation from population import Population #SORTS Libraries import radar_library as rlib import population_library as plib import radar_scan_library as rslib import scheduler_library as schlib import antenna_library as alib import rewardf_library as rflib import plothelp import dpt_tools as dpt import ccsds_write sim_root = './tests/tmp_test_data/test_sim2' np.random.seed(12345) #NEEDED SIM_TIME = 24.0*4 pop = plib.master_catalog() pop.objs = pop.objs[:10] #initialize the radar setup radar = rlib.eiscat_3d(beam='interp', stage=1) radar.set_FOV(max_on_axis=90.0, horizon_elevation=30.0) radar.set_SNR_limits(min_total_SNRdb=10.0, min_pair_SNRdb=1.0) radar.set_TX_bandwith(bw = 1.0e6) sim = Simulation( radar = radar, population = pop, root = sim_root, scheduler = schlib.dynamic_scheduler, simulation_name = 'OD validation', ) #sim.set_log_level(logging.DEBUG) sim.observation_parameters( duty_cycle=0.25, SST_fraction=1.0, tracking_fraction=1.0, SST_time_slice=0.2, ) sim.simulation_parameters( tracklet_noise=True, max_dpos=50e3, auto_synchronize=True, ) #We know all! sim.catalogue.maintain(slice(None)) ################## RUNNING ##################### if part == 1: sim.run_observation(SIM_TIME) sim.status(fout='{}h_to_{}h'.format(0, int(SIM_TIME))) sim.print_maintenance() sim.print_detections() sim.set_scheduler_args( reward_function = rflib.rewardf_exp_peak_SNR_tracklet_len, reward_function_config = { 'sigma_t': 60.0*5.0, 'lambda_N': 50.0, }, logger = sim.logger, ) sim.run_scheduler() sim.print_tracks() sim.print_tracklets() sim.status(fout='{}h_to_{}h_scheduler'.format(0, int(SIM_TIME))) if part == 2: sim.load() sim.generate_tracklets() if part == 3: sim.load() sim.generate_priors( frame_transformation='TEME', tracklet_truncate = slice(None, None, 20), ) if part == 4: sim.load() sim.run_orbit_determination( frame_transformation = 'TEME', error_samp = 500, steps = 10000, max_zenith_error = 0.9, tracklet_truncate = slice(None, None, 400), tune = 1000, )
def wls_state_est(mcmc=False, n_tracklets=1, track_length=600.0, n_points=3, oid=145128, N_samples=5000): """ Weighted linear least squares estimation of orbital elements Simulate measurements using create tracklet and estimate orbital parameters, which include six keplerian and area to mass ratio. Use fmin search. Optionally utilize MCMC to sample the distribution of parameters. number of tracklets, tracklet length, and number of tracklet points per tracklet are user definable, allowing one to try out different measurement strategies. """ # first we shall simulate some measurement # Envisat m = plib.master_catalog(sort=False) o = m.get_object(oid) dname="./test_tracklets_%d"%(oid) print(o) # figure out epoch in unix seconds t0_unix = dpt.jd_to_unix(dpt.mjd_to_jd(o.mjd0)) if rank == 0: os.system("rm -Rf %s"%(dname)) os.system("mkdir %s"%(dname)) e3d = rlib.eiscat_3d(beam='gauss') # time in seconds after mjd0 t_all = n.linspace(0, 24*3600, num=1000) passes, _, _, _, _ = simulate_tracking.find_pass_interval(t_all, o, e3d) print(passes) if n_tracklets == None: n_tracklets = len(passes[0]) n_tracklets = n.min([n_tracklets,len(passes[0])]) for pi in range(n_tracklets): p = passes[0][pi] mean_t=0.5*(p[1]+p[0]) print("duration %1.2f"%(p[1]-p[0])) if p[1]-p[0] > 50.0: if n_points == 1: t_obs=n.array([mean_t]) else: t_obs=n.linspace(n.max([p[0],mean_t-track_length/2]), n.min([p[1],mean_t+track_length/2]),num=n_points) print(t_obs) meas, fnames, ecef_stdevs = st.create_tracklet(o, e3d, t_obs, hdf5_out=True, ccsds_out=True, dname=dname) # then we read these measurements comm.Barrier() fl=glob.glob("%s/*"%(dname)) for f in fl: print(f) fl2=glob.glob("%s/*.h5"%(f)) print(fl2) fl2.sort() true_states=[] all_r_meas=[] all_rr_meas=[] all_t_meas=[] all_true_states=[] tx_locs=[] rx_locs=[] range_stds=[] range_rate_stds=[] for mf in fl2: h=h5py.File(mf,"r") all_r_meas.append(n.copy(h["m_range"].value)) all_rr_meas.append(n.copy(h["m_range_rate"].value)) all_t_meas.append(n.copy(h["m_time"].value-t0_unix)) all_true_states.append(n.copy(h["true_state"].value)) tx_locs.append(n.copy(h["tx_loc"].value)) rx_locs.append(n.copy(h["rx_loc"].value)) range_stds.append(n.copy(h["m_range_rate_std"].value)) range_rate_stds.append(h["m_range_std"].value) h.close() # determine orbital elements o_prior = m.get_object(oid) # get best fit space object o_fit=mcmc_od(all_t_meas, all_r_meas, all_rr_meas, range_stds, range_rate_stds, tx_locs, rx_locs, o_prior, mcmc=mcmc, odir=dname, N_samples=N_samples)
def plot_chain(oid=8): dname="test_tracklets_%d"%(oid) m = plib.master_catalog(sort=False) o = m.get_object(oid) fl=glob.glob("%s/chain*.h5"%(dname)) chains=[] lens=[] for fi,f in enumerate(fl): print(f) ho=h5py.File(f,"r") c=n.copy(ho["chain"].value) clen=c.shape[0] # chains.append(c[(clen/2):clen,:]) chains.append(c) lens.append(c.shape[0]) ho.close() c=n.vstack(chains) print(c.shape) d={"a":c[:,0], "log10(e)":c[:,1], "i":c[:,2], "raan":c[:,3], "aop":c[:,4], "mu0":c[:,5], "log10(A/m)":c[:,6] } print(" a %f +/- %f\n e %f +/- %f\n i %f +/- %f\n raan %f +/- %f\n aop %f +/- %f\n mu0 %f +/- %f\n log10(A/m) %f +/- %f\n"%(n.mean(c[:,0]),n.std(c[:,0]), n.mean(c[:,1]),n.std(c[:,1]), n.mean(c[:,2]),n.std(c[:,2]), n.mean(c[:,3]),n.std(c[:,3]), n.mean(c[:,4]),n.std(c[:,4]), n.mean(c[:,5]),n.std(c[:,5]), n.mean(c[:,6]),n.std(c[:,6]))) df = pd.DataFrame(d) pairs=[["a","log10(e)"], ["a","i"], ["a","mu0"], ["a","aop"], ["a","raan"], ["a","log10(A/m)"], ["aop","mu0"], ["log10(e)","i"]] true_vals={"a":o.a, "log10(e)":n.log10(o.e), "i":o.i, "raan":o.raan, "aop":o.aop, "mu0":o.mu0, "log10(A/m)":n.log10(o.A/o.m)} for p in pairs: g = sb.jointplot(x=p[0], y=p[1], data=df, kind="kde") g.plot_joint(plt.scatter, c="b", s=30, linewidth=1, marker="+", alpha=0.05) plt.axvline(true_vals[p[0]]) plt.axhline(true_vals[p[1]]) plt.show() sb.pairplot(df) plt.show()
#!/usr/bin/env python import numpy as n import h5py import matplotlib.pyplot as plt import population_library as plib """ Plot hard target filter statistics """ hard_target_lim = 40.0 h = h5py.File("master/EISCAT_3D_filter.h5", "r") mo = plib.master_catalog() m = plib.master_catalog() m.filter('i', lambda x: x >= 50.0) det = n.copy(h["detectable"].value) snr = n.copy(h["peak_snr"].value) h.close() def plot_detectable_fraction(): count = 0.0 plt.figure(figsize=(10, 10)) plt.subplot(221) plt.loglog(mo.objs["a"], mo.objs["e"], ".", label="All") plt.loglog(m.objs["a"][det], m.objs["e"][det], ".", label="Detectable") plt.legend() plt.xlabel("Apogee (km)") plt.ylabel("Eccentricity")
#Project 4: Lets see how much of MASTER 2009 this carzy radar can see import population_library import population_filter from DEV_demo1 import SC from DEV_demo2 import radar radar.set_scan(SC) pop = population_library.master_catalog() pop.delete(slice(100, None)) #OOOPS! Lets only keep the first 100 objects #Todo: add return parameters to documentation :) detectable, n_rx_dets, peak_snrs = population_filter.filter_objects( radar, pop, ofname=None, prop_time=24.0, )
def test_master_factor_cnt(self): popf = plib.master_catalog_factor(treshhold=0.01) pop = plib.master_catalog() pop.filter('d', lambda x: x >= 0.01) self.assertEqual(len(popf), int(n.sum(n.round(pop['Factor']))))