def test_overlapping_entire_segment(self): """ Test that a segment overlapping the entire time period of the segment does not get added to the DB. """ sat = Satellite(platform_name="TEST") sat.save() DB.session.commit() orbit_data = [] # time, posx, posy, posz, velx, vely, velz segment_start = [0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5] segment_end = [1.0, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5] orbit_tuple = OrbitPoint(segment_start[0], segment_start[1:4], segment_start[4:7]) orbit_data.append(orbit_tuple) orbit_tuple = OrbitPoint(segment_end[0], segment_end[1:4], segment_end[4:7]) orbit_data.append(orbit_tuple) add_segment_to_db(orbit_data, sat.platform_id) # try to add a segment overlapping the start time of the above segment orbit_data = [] # time, posx, posy, posz, velx, vely, velz segment_start = [0.3, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5] segment_end = [1.2, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5] orbit_tuple = OrbitPoint(segment_start[0], segment_start[1:4], segment_start[4:7]) orbit_data.append(orbit_tuple) orbit_tuple = OrbitPoint(segment_end[0], segment_end[1:4], segment_end[4:7]) orbit_data.append(orbit_tuple) add_segment_to_db(orbit_data, sat.platform_id) self.assertTrue(len(OrbitSegment.query.all()) == 1)
def test_duplicate_segments(self): """ Test that a duplicated segment for a satellite does not get added to the DB """ sat = Satellite(platform_name="TEST") sat.save() DB.session.commit() orbit_data = [] for i in range(0, 20): orbit_point = [float(j) for j in range(i, i + 7)] orbit_tuple = OrbitPoint(orbit_point[0], orbit_point[1:4], orbit_point[4:7]) orbit_data.append(orbit_tuple) add_segment_to_db(orbit_data, sat.platform_id) # try to add the same segment again orbit_data = [] for i in range(0, 20): orbit_point = [float(j) for j in range(i, i + 7)] orbit_tuple = OrbitPoint(orbit_point[0], orbit_point[1:4], orbit_point[4:7]) orbit_data.append(orbit_tuple) add_segment_to_db(orbit_data, sat.platform_id) self.assertTrue(len(OrbitSegment.query.all()) == 1)
def setUpClass(cls): super(TestInterpolator, cls).setUpClass() # create a fake satellite cls.platform_id = 1 satellite = Satellite(platform_id=cls.platform_id, platform_name="testsat") satellite.save() DB.session.commit() # add a segment for this satellite cls.segment1 = OrbitSegment(platform_id=cls.platform_id, start_time=1., end_time=3.) cls.segment1.save() DB.session.commit() # simple segment data for record in range(1, 4): t = float(record) orbit_record = OrbitRecord(segment_id=cls.segment1.segment_id, platform_id=cls.platform_id, time=t, position=(t, t, t), velocity=(t, t, t)) orbit_record.save() # another segment cls.segment2 = OrbitSegment(platform_id=cls.platform_id, start_time=4., end_time=10.) cls.segment2.save() DB.session.commit() # more complex data this time times = np.arange(4, 11) positions = np.column_stack( (np.sin(times), np.sin(times), np.sin(times))) velocities = np.column_stack( (np.exp(0.2 * times), np.exp(0.2 * times), np.exp(0.2 * times))) for i in range(times.size): orbit_record = OrbitRecord(segment_id=cls.segment2.segment_id, platform_id=cls.platform_id, time=times[i], position=tuple(positions[i]), velocity=tuple(velocities[i])) orbit_record.save() DB.session.commit()
def test_db_add_correct_num_rows(self): """Test that the add_segment_to_db adds the correct number of rows to the DB. """ sat = Satellite(platform_name="TEST") sat.save() DB.session.commit() orbit_data = [] for i in range(0, 20): orbit_point = [float(j) for j in range(i, i + 7)] orbit_tuple = OrbitPoint(orbit_point[0], orbit_point[1:4], orbit_point[4:7]) orbit_data.append(orbit_tuple) add_segment_to_db(orbit_data, sat.platform_id) self.assertTrue(len(OrbitRecord.query.all()) == 20)
def test_single_segment_min_max(self): """Test that a single orbital segment has the correct start and end time.""" sat = Satellite(platform_name="TEST") sat.save() DB.session.commit() start = 0.0 end = 1000.0 orbit_segment = OrbitSegment(platform_id=sat.platform_id, start_time=start, end_time=end) orbit_segment.save() DB.session.commit() query_min = orbit_segment.query.filter( orbit_segment.start_time == start).all() query_max = orbit_segment.query.filter( orbit_segment.end_time == end).all() for q_min, q_max in zip(query_min, query_max): self.assertTrue(q_min.start_time == start) self.assertTrue(q_max.end_time == end)
def test_db_add_num_segments(self): """Test that the add_segment_to_db adds the correct number of "segments" to the db. Each call to add_segment_to_db should create only one segment at a time. """ # create and add first segment to DB sat = Satellite(platform_name="TEST1") sat.save() DB.session.commit() orbit_data = [] for i in range(0, 20): orbit_point = [float(j) for j in range(i, i + 7)] orbit_tuple = OrbitPoint(orbit_point[0], orbit_point[1:4], orbit_point[4:7]) orbit_data.append(orbit_tuple) add_segment_to_db(orbit_data, sat.platform_id) self.assertTrue(len(OrbitSegment.query.all()) == 1) # create and add second segment sat2 = Satellite(platform_name="TEST2") sat2.save() DB.session.commit() orbit_data = [] for i in range(40, 60): orbit_point = [float(j) for j in range(i, i + 7)] orbit_tuple = OrbitPoint(orbit_point[0], orbit_point[1:4], orbit_point[4:7]) orbit_data.append(orbit_tuple) add_segment_to_db(orbit_data, sat2.platform_id) # make sure we have two distincts segments in the DB self.assertTrue(len(OrbitSegment.query.all()) == 2)
def test_db_add_correct_orbit_data(self): """Test that the add_segment_to_db adds the correct row data to the DB. Validates time, position, and velocity for each DB row added.""" sat = Satellite(platform_name="TEST") sat.save() DB.session.commit() orbit_data = [] for i in range(0, 20): orbit_point = [float(j) for j in range(i, i + 7)] orbit_tuple = OrbitPoint(orbit_point[0], orbit_point[1:4], orbit_point[4:7]) orbit_data.append(orbit_tuple) add_segment_to_db(orbit_data, sat.platform_id) orbit_db = OrbitRecord() self.assertTrue(len(orbit_db.query.all()) == 20) orbits = orbit_db.query.all() for orbit_point, orbit in zip(orbit_data, orbits): self.assertTrue(orbit_point.time == orbit.time) self.assertTrue(orbit_point.pos == orbit.position) self.assertTrue(orbit_point.vel == orbit.velocity)
def parse_ephemeris_file(filename): """Parse the given ephemeris file and store the orbital data in OrbitRecords. We assume that each row in the ephemeris file is a 7-tuple containing an orbital point, formatted as: time posx posy posz velx vely velz Calculate the maximum distance from earth center to the position if the satellite. Insert it in Satellite. """ sat_name = os.path.splitext(os.path.basename(filename))[0].split('.')[0] existing_sat = Satellite.get_by_name(sat_name) if not existing_sat: sat = Satellite(platform_name=sat_name) sat.save() DB.session.commit() sat_id = -1 else: sat = existing_sat[0] max_distance = 0 with open(filename, "rU") as f: segment_boundaries = [] segment_tuples = [] start_time = float(0) read_segment_boundaries = False read_orbital_data = False # Remember the last seen segment boundary while reading the ephemeris rows. Needed to # differentiate between the beginning of a new segment and the end of en existing segment of # data last_seen_segment_boundary = 0 for line in f: line = line.rstrip('\n') if "Epoch in JDate format:" in line: start_time = float(line.split(':')[1]) start_time = jdate_to_unix(start_time) last_seen_segment_boundary = start_time # For now, we assume that the coord system will always be J2000 # if "CoordinateSystem" in line: # coord_system = str(line.split()[1]) if "END SegmentBoundaryTimes" in line: read_segment_boundaries = False if read_segment_boundaries: line = line.strip() if line: segment_boundaries.append(start_time + float(line)) if "BEGIN SegmentBoundaryTimes" in line: read_segment_boundaries = True if "END Ephemeris" in line: add_segment_to_db(segment_tuples, sat.platform_id) read_orbital_data = False if read_orbital_data: line = line.strip() if line: ephemeris_row = [float(num) for num in line.split()] orbit_tuple = OrbitPoint(start_time + ephemeris_row[0], ephemeris_row[1:4], ephemeris_row[4:7]) segment_tuples.append(orbit_tuple) # Keep track of the magnitude of the position vector and update with a bigger # value max_distance = max(max_distance, np.linalg.norm(ephemeris_row[1:4])) # The line we just read is a segment boundary, So first check that this is the # *end* of a segment, not the beginning of a new one, and then add this segment # to the db. if (orbit_tuple.time in segment_boundaries and last_seen_segment_boundary != orbit_tuple.time): last_seen_segment_boundary = orbit_tuple.time sat_id = add_segment_to_db(segment_tuples, sat.platform_id) segment_tuples = [] if "EphemerisTimePosVel" in line: read_orbital_data = True # After getting the q_max, insert it into Satellite""" sat.maximum_altitude = max_distance sat.save() DB.session.commit() return sat_id