Reduce complexity of main() function
Signed-off-by: Pierros Papadeas <pierros@papadeas.gr>merge-requests/21/head
parent
fee88c5fc1
commit
19d1c4d235
|
@ -9,9 +9,9 @@ import lxml.html
|
||||||
import argparse
|
import argparse
|
||||||
import logging
|
import logging
|
||||||
from utils import get_active_transmitter_info, get_transmitter_stats, \
|
from utils import get_active_transmitter_info, get_transmitter_stats, \
|
||||||
get_groundstation_info, get_last_update, get_scheduled_passes_from_network, ordered_scheduler, \
|
get_groundstation_info, get_scheduled_passes_from_network, ordered_scheduler, \
|
||||||
efficiency, find_passes, schedule_observation, read_priorities_transmitters, \
|
efficiency, find_passes, schedule_observation, read_priorities_transmitters, \
|
||||||
get_satellite_info
|
get_satellite_info, update_needed, get_priority_passes
|
||||||
import settings
|
import settings
|
||||||
from tqdm import tqdm
|
from tqdm import tqdm
|
||||||
import sys
|
import sys
|
||||||
|
@ -101,7 +101,8 @@ def main():
|
||||||
"--wait",
|
"--wait",
|
||||||
help="Wait time between consecutive observations (for setup and slewing)" +
|
help="Wait time between consecutive observations (for setup and slewing)" +
|
||||||
" [seconds; default: 0.0]",
|
" [seconds; default: 0.0]",
|
||||||
type=float,
|
type=int,
|
||||||
|
choices=range(0, 3600),
|
||||||
default=0)
|
default=0)
|
||||||
parser.add_argument("-n",
|
parser.add_argument("-n",
|
||||||
"--dryrun",
|
"--dryrun",
|
||||||
|
@ -140,8 +141,6 @@ def main():
|
||||||
length_hours = args.duration
|
length_hours = args.duration
|
||||||
wait_time_seconds = args.wait
|
wait_time_seconds = args.wait
|
||||||
min_horizon_arg = args.min_horizon
|
min_horizon_arg = args.min_horizon
|
||||||
if wait_time_seconds < 0:
|
|
||||||
wait_time_seconds = 0.0
|
|
||||||
cache_dir = "/tmp/cache"
|
cache_dir = "/tmp/cache"
|
||||||
schedule = not args.dryrun
|
schedule = not args.dryrun
|
||||||
search_transmitters = args.search_transmitters
|
search_transmitters = args.search_transmitters
|
||||||
|
@ -155,25 +154,12 @@ def main():
|
||||||
# Get ground station information
|
# Get ground station information
|
||||||
ground_station = get_groundstation_info(ground_station_id)
|
ground_station = get_groundstation_info(ground_station_id)
|
||||||
|
|
||||||
# Exit if ground station is empty
|
|
||||||
if not ground_station:
|
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
# Create cache
|
# Create cache
|
||||||
if not os.path.isdir(cache_dir):
|
if not os.path.isdir(cache_dir):
|
||||||
os.mkdir(cache_dir)
|
os.mkdir(cache_dir)
|
||||||
|
|
||||||
# Get last update
|
|
||||||
tlast = get_last_update(os.path.join(cache_dir, "last_update_%d.txt" % ground_station_id))
|
|
||||||
|
|
||||||
# Update logic
|
# Update logic
|
||||||
update = False
|
update = update_needed(tnow, ground_station_id, cache_dir)
|
||||||
if tlast is None or (tnow - tlast).total_seconds() > settings.CACHE_AGE * 3600:
|
|
||||||
update = True
|
|
||||||
if not os.path.isfile(os.path.join(cache_dir, "transmitters_%d.txt" % ground_station_id)):
|
|
||||||
update = True
|
|
||||||
if not os.path.isfile(os.path.join(cache_dir, "tles_%d.txt" % ground_station_id)):
|
|
||||||
update = True
|
|
||||||
|
|
||||||
# Update
|
# Update
|
||||||
if update:
|
if update:
|
||||||
|
@ -259,13 +245,7 @@ def main():
|
||||||
# Find passes
|
# Find passes
|
||||||
passes = find_passes(satellites, observer, tmin, tmax, minimum_altitude)
|
passes = find_passes(satellites, observer, tmin, tmax, minimum_altitude)
|
||||||
|
|
||||||
# Priorities and favorite transmitters
|
priorities, favorite_transmitters = read_priorities_transmitters(priority_filename)
|
||||||
# read the following format
|
|
||||||
# 43017 1. KgazZMKEa74VnquqXLwAvD
|
|
||||||
if priority_filename is not None and os.path.exists(priority_filename):
|
|
||||||
priorities, favorite_transmitters = read_priorities_transmitters(priority_filename)
|
|
||||||
else:
|
|
||||||
priorities, favorite_transmitters = {}, {}
|
|
||||||
|
|
||||||
# List of scheduled passes
|
# List of scheduled passes
|
||||||
scheduledpasses = get_scheduled_passes_from_network(ground_station_id, tmin, tmax)
|
scheduledpasses = get_scheduled_passes_from_network(ground_station_id, tmin, tmax)
|
||||||
|
@ -273,26 +253,8 @@ def main():
|
||||||
(len(scheduledpasses), tmin, tmax, ground_station_id))
|
(len(scheduledpasses), tmin, tmax, ground_station_id))
|
||||||
|
|
||||||
# Get passes of priority objects
|
# Get passes of priority objects
|
||||||
prioritypasses = []
|
prioritypasses, normalpasses = get_priority_passes(passes, priorities, favorite_transmitters,
|
||||||
normalpasses = []
|
search_transmitters)
|
||||||
for satpass in passes:
|
|
||||||
# Get user defined priorities
|
|
||||||
if satpass['id'] in priorities:
|
|
||||||
satpass['priority'] = priorities[satpass['id']]
|
|
||||||
if satpass['id'] in favorite_transmitters:
|
|
||||||
satpass['uuid'] = favorite_transmitters[satpass['id']]
|
|
||||||
prioritypasses.append(satpass)
|
|
||||||
elif search_transmitters:
|
|
||||||
# Find satellite transmitter with highest number of good observations
|
|
||||||
max_good_count = max([s['good_count'] for s in passes if s["id"] == satpass["id"]])
|
|
||||||
if max_good_count > 0:
|
|
||||||
satpass['priority'] = \
|
|
||||||
(float(satpass['altt']) / 90.0) \
|
|
||||||
* satpass['success_rate'] \
|
|
||||||
* float(satpass['good_count']) / max_good_count
|
|
||||||
else:
|
|
||||||
satpass['priority'] = (float(satpass['altt']) / 90.0) * satpass['success_rate']
|
|
||||||
normalpasses.append(satpass)
|
|
||||||
|
|
||||||
# Priority scheduler
|
# Priority scheduler
|
||||||
prioritypasses = sorted(prioritypasses, key=lambda satpass: -satpass['priority'])
|
prioritypasses = sorted(prioritypasses, key=lambda satpass: -satpass['priority'])
|
||||||
|
@ -306,7 +268,7 @@ def main():
|
||||||
|
|
||||||
# Compute scheduling efficiency
|
# Compute scheduling efficiency
|
||||||
dt, dttot, eff = efficiency(scheduledpasses)
|
dt, dttot, eff = efficiency(scheduledpasses)
|
||||||
logging.info("%d passes scheduled out of %d, %.0f s out of %.0f s at %.3f%% efficiency" %
|
logging.info("%d passes selected out of %d, %.0f s out of %.0f s at %.3f%% efficiency" %
|
||||||
(len(scheduledpasses), len(passes), dt, dttot, 100 * eff))
|
(len(scheduledpasses), len(passes), dt, dttot, 100 * eff))
|
||||||
|
|
||||||
# Find unique objects
|
# Find unique objects
|
||||||
|
|
72
utils.py
72
utils.py
|
@ -7,6 +7,8 @@ import ephem
|
||||||
import lxml
|
import lxml
|
||||||
import settings
|
import settings
|
||||||
from tqdm import tqdm
|
from tqdm import tqdm
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
def get_paginated_endpoint(url, max_entries=None):
|
def get_paginated_endpoint(url, max_entries=None):
|
||||||
|
@ -27,18 +29,24 @@ def get_paginated_endpoint(url, max_entries=None):
|
||||||
|
|
||||||
|
|
||||||
def read_priorities_transmitters(filename):
|
def read_priorities_transmitters(filename):
|
||||||
with open(filename, "r") as fp:
|
# Priorities and favorite transmitters
|
||||||
satprio = {}
|
# read the following format
|
||||||
sattrans = {}
|
# 43017 1. KgazZMKEa74VnquqXLwAvD
|
||||||
lines = fp.readlines()
|
if filename is not None and os.path.exists(filename):
|
||||||
for line in lines:
|
with open(filename, "r") as fp:
|
||||||
parts = line.strip().split(" ")
|
satprio = {}
|
||||||
sat = parts[0]
|
sattrans = {}
|
||||||
prio = parts[1]
|
lines = fp.readlines()
|
||||||
transmitter = parts[2]
|
for line in lines:
|
||||||
satprio[sat] = float(prio)
|
parts = line.strip().split(" ")
|
||||||
sattrans[sat] = transmitter
|
sat = parts[0]
|
||||||
return (satprio, sattrans)
|
prio = parts[1]
|
||||||
|
transmitter = parts[2]
|
||||||
|
satprio[sat] = float(prio)
|
||||||
|
sattrans[sat] = transmitter
|
||||||
|
return (satprio, sattrans)
|
||||||
|
else:
|
||||||
|
return ({}, {})
|
||||||
|
|
||||||
|
|
||||||
def get_satellite_info():
|
def get_satellite_info():
|
||||||
|
@ -264,6 +272,30 @@ def find_passes(satellites, observer, tmin, tmax, minimum_altitude):
|
||||||
return passes
|
return passes
|
||||||
|
|
||||||
|
|
||||||
|
def get_priority_passes(passes, priorities, favorite_transmitters, search):
|
||||||
|
priority = []
|
||||||
|
normal = []
|
||||||
|
for satpass in passes:
|
||||||
|
# Get user defined priorities
|
||||||
|
if satpass['id'] in priorities:
|
||||||
|
satpass['priority'] = priorities[satpass['id']]
|
||||||
|
if satpass['id'] in favorite_transmitters:
|
||||||
|
satpass['uuid'] = favorite_transmitters[satpass['id']]
|
||||||
|
priority.append(satpass)
|
||||||
|
elif search:
|
||||||
|
# Find satellite transmitter with highest number of good observations
|
||||||
|
max_good_count = max([s['good_count'] for s in passes if s["id"] == satpass["id"]])
|
||||||
|
if max_good_count > 0:
|
||||||
|
satpass['priority'] = \
|
||||||
|
(float(satpass['altt']) / 90.0) \
|
||||||
|
* satpass['success_rate'] \
|
||||||
|
* float(satpass['good_count']) / max_good_count
|
||||||
|
else:
|
||||||
|
satpass['priority'] = (float(satpass['altt']) / 90.0) * satpass['success_rate']
|
||||||
|
normal.append(satpass)
|
||||||
|
return (priority, normal)
|
||||||
|
|
||||||
|
|
||||||
def get_groundstation_info(ground_station_id):
|
def get_groundstation_info(ground_station_id):
|
||||||
|
|
||||||
logging.info("Requesting information for ground station %d" % ground_station_id)
|
logging.info("Requesting information for ground station %d" % ground_station_id)
|
||||||
|
@ -285,7 +317,8 @@ def get_groundstation_info(ground_station_id):
|
||||||
return o
|
return o
|
||||||
else:
|
else:
|
||||||
logging.info('No ground station information found!')
|
logging.info('No ground station information found!')
|
||||||
return {}
|
# Exit if no ground station found
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
|
||||||
def get_last_update(fname):
|
def get_last_update(fname):
|
||||||
|
@ -298,6 +331,19 @@ def get_last_update(fname):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def update_needed(tnow, ground_station_id, cache_dir):
|
||||||
|
# Get last update
|
||||||
|
tlast = get_last_update(os.path.join(cache_dir, "last_update_%d.txt" % ground_station_id))
|
||||||
|
|
||||||
|
if tlast is None or (tnow - tlast).total_seconds() > settings.CACHE_AGE * 3600:
|
||||||
|
return True
|
||||||
|
if not os.path.isfile(os.path.join(cache_dir, "transmitters_%d.txt" % ground_station_id)):
|
||||||
|
return True
|
||||||
|
if not os.path.isfile(os.path.join(cache_dir, "tles_%d.txt" % ground_station_id)):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def schedule_observation(session, norad_cat_id, uuid, ground_station_id, starttime, endtime):
|
def schedule_observation(session, norad_cat_id, uuid, ground_station_id, starttime, endtime):
|
||||||
|
|
||||||
obsURL = '{}/observations/new/'.format(settings.NETWORK_BASE_URL) # Observation URL
|
obsURL = '{}/observations/new/'.format(settings.NETWORK_BASE_URL) # Observation URL
|
||||||
|
|
Loading…
Reference in New Issue