Skip to content

Commit

Permalink
Improve ISL reading checks
Browse files Browse the repository at this point in the history
  • Loading branch information
snkas committed Oct 13, 2020
1 parent 3fa2732 commit c9265ce
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 9 deletions.
2 changes: 1 addition & 1 deletion satgenpy/satgen/dynamic_state/helper_dynamic_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ def help_dynamic_state(
# Variables (load in for each thread such that they don't interfere)
ground_stations = read_ground_stations_extended(output_generated_data_dir + "/" + name + "/ground_stations.txt")
tles = read_tles(output_generated_data_dir + "/" + name + "/tles.txt")
list_isls = read_isls(output_generated_data_dir + "/" + name + "/isls.txt")
satellites = tles["satellites"]
list_isls = read_isls(output_generated_data_dir + "/" + name + "/isls.txt", len(satellites))
list_gsl_interfaces_info = read_gsl_interfaces_info(
output_generated_data_dir + "/" + name + "/gsl_interfaces_info.txt",
len(satellites),
Expand Down
27 changes: 24 additions & 3 deletions satgenpy/satgen/isls/read_isls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,38 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from exputil import parse_positive_int

def read_isls(filename_isls):

def read_isls(filename_isls, num_satellites):
"""
Read ISLs file into a list of undirected edges
:param filename_isls: Filename of ISLs (typically /path/to/isls.txt)
:param filename_isls: Filename of ISLs (typically /path/to/isls.txt)
:param num_satellites: Number of satellites (to verify indices)
:return: List of all undirected ISL edges
"""
isls_list = []
with open(filename_isls, 'r') as f:
isls_set = set()
for line in f:
isls_list.append((int(line.split()[0]), int(line.split()[1])))
line_spl = line.split()
a = parse_positive_int(line_spl[0])
b = parse_positive_int(line_spl[1])

# Verify the input
if a >= num_satellites:
raise ValueError("Satellite does not exist: %d" % a)
if b >= num_satellites:
raise ValueError("Satellite does not exist: %d" % b)
if b < a:
raise ValueError("The second satellite index must be higher than the first")
if (a, b) in isls_set:
raise ValueError("Duplicate ISL: (%d, %d)" % (a, b))
isls_set.add((a, b))

# Finally add it to the list
isls_list.append((a, b))

return isls_list
2 changes: 1 addition & 1 deletion satgenpy/satgen/post_analysis/analyze_rtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ def analyze_rtt(
# Variables (load in for each thread such that they don't interfere)
ground_stations = read_ground_stations_extended(satellite_network_dir + "/ground_stations.txt")
tles = read_tles(satellite_network_dir + "/tles.txt")
list_isls = read_isls(satellite_network_dir + "/isls.txt")
satellites = tles["satellites"]
list_isls = read_isls(satellite_network_dir + "/isls.txt", len(satellites))
epoch = tles["epoch"]
description = exputil.PropertiesConfig(satellite_network_dir + "/description.txt")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ def print_graphical_routes_and_rtt(
# Variables (load in for each thread such that they don't interfere)
ground_stations = read_ground_stations_extended(satellite_network_dir + "/ground_stations.txt")
tles = read_tles(satellite_network_dir + "/tles.txt")
list_isls = read_isls(satellite_network_dir + "/isls.txt")
satellites = tles["satellites"]
list_isls = read_isls(satellite_network_dir + "/isls.txt", len(satellites))
epoch = tles["epoch"]
description = exputil.PropertiesConfig(satellite_network_dir + "/description.txt")

Expand Down
2 changes: 1 addition & 1 deletion satgenpy/satgen/post_analysis/print_routes_and_rtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ def print_routes_and_rtt(base_output_dir, satellite_network_dir, dynamic_state_u
# Variables (load in for each thread such that they don't interfere)
ground_stations = read_ground_stations_extended(satellite_network_dir + "/ground_stations.txt")
tles = read_tles(satellite_network_dir + "/tles.txt")
list_isls = read_isls(satellite_network_dir + "/isls.txt")
satellites = tles["satellites"]
list_isls = read_isls(satellite_network_dir + "/isls.txt", len(satellites))
epoch = tles["epoch"]
description = exputil.PropertiesConfig(satellite_network_dir + "/description.txt")

Expand Down
68 changes: 66 additions & 2 deletions satgenpy/tests/test_isls.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
import unittest
from math import floor
import os
import exputil


class TestIsls(unittest.TestCase):

def test_isls_empty(self):
satgen.generate_empty_isls("isls_empty.txt.tmp")
isls_list = satgen.read_isls("isls_empty.txt.tmp")
isls_list = satgen.read_isls("isls_empty.txt.tmp", 0)
self.assertEqual(0, len(isls_list))
os.remove("isls_empty.txt.tmp")

Expand All @@ -40,7 +41,7 @@ def test_isls_plus_grid(self):
num_sat_per_orbit = values[1]
isl_shift = values[2]
satgen.generate_plus_grid_isls("isls.txt.tmp", num_orbits, num_sat_per_orbit, isl_shift)
isls_list = satgen.read_isls("isls.txt.tmp")
isls_list = satgen.read_isls("isls.txt.tmp", num_orbits * num_sat_per_orbit)
os.remove("isls.txt.tmp")
self.assertEqual(len(isls_list), num_orbits * num_sat_per_orbit * 2)
self.assertEqual(len(set(isls_list)), num_orbits * num_sat_per_orbit * 2)
Expand Down Expand Up @@ -85,3 +86,66 @@ def test_isls_plus_grid_invalid(self):
self.fail()
except ValueError:
self.assertTrue(True)

def test_isls_invalid_file(self):
local_shell = exputil.LocalShell()

# Invalid left index
local_shell.write_file(
"isls.txt.tmp",
"2 3\n5 6\n9 0"
)
try:
satgen.read_isls("isls.txt.tmp", 9)
self.fail()
except ValueError:
self.assertTrue(True)
os.remove("isls.txt.tmp")

# Invalid right index
local_shell.write_file(
"isls.txt.tmp",
"2 3\n5 6\n6 9\n3 99"
)
try:
satgen.read_isls("isls.txt.tmp", 50)
self.fail()
except ValueError:
self.assertTrue(True)
os.remove("isls.txt.tmp")

# Invalid left index
local_shell.write_file(
"isls.txt.tmp",
"2 3\n5 6\n6 8\n-3 3"
)
try:
satgen.read_isls("isls.txt.tmp", 50)
self.fail()
except ValueError:
self.assertTrue(True)
os.remove("isls.txt.tmp")

# Invalid right index
local_shell.write_file(
"isls.txt.tmp",
"2 3\n5 6\n1 -3\n6 8"
)
try:
satgen.read_isls("isls.txt.tmp", 50)
self.fail()
except ValueError:
self.assertTrue(True)
os.remove("isls.txt.tmp")

# Duplicate
local_shell.write_file(
"isls.txt.tmp",
"2 3\n5 6\n3 9\n5 6\n2 9"
)
try:
satgen.read_isls("isls.txt.tmp", 10)
self.fail()
except ValueError:
self.assertTrue(True)
os.remove("isls.txt.tmp")

0 comments on commit c9265ce

Please sign in to comment.