1
0
Fork 0

Bring satnogs.py almost into good flake8. Many lines still to long

merge-requests/2/head
wgaylord 2019-02-25 12:24:46 -06:00
parent d36587bb1c
commit 366b19d4bf
1 changed files with 106 additions and 134 deletions

View File

@ -1,12 +1,13 @@
from datetime import datetime, timedelta
import requests
from tqdm import tqdm
from flask import Flask , render_template,redirect,url_for
from flask import Flask, render_template
import json
from collections import defaultdict
import random
from apscheduler.schedulers.background import BackgroundScheduler
from satnogs_api_client import fetch_satellites, DB_BASE_URL,fetch_tle_of_observation ,get_paginated_endpoint
from satnogs_api_client import fetch_satellites
from satnogs_api_client import DB_BASE_URL, get_paginated_endpoint
from satellite_tle import fetch_tles
from skyfield.api import EarthSatellite, utc, load
@ -27,21 +28,19 @@ StationsPasses = defaultdict(list)
SatDescrip = defaultdict(str)
CZML = []
def getFuture():
print "Getting future Passes"
global Sats
global TLEs
observations = defaultdict(dict)
start = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S%z')
end = (datetime.utcnow() + timedelta(hours=4,minutes=30)).strftime('%Y-%m-%dT%H:%M:%S%z')
end = (datetime.utcnow() + timedelta(hours=4, minutes=30))
end = end.strftime('%Y-%m-%dT%H:%M:%S%z')
passes = get_paginated_endpoint("https://network.satnogs.org/api/jobs/")
obs = get_paginated_endpoint("https://network.satnogs.org/api/observations/?end="+end+"&format=json&start="+start)
for x in tqdm(obs):
observations[x["id"]] = x
ground_stations = {}
Sats = defaultdict(list)
for x in tqdm(passes):
if x["id"] in observations:
@ -60,15 +59,9 @@ def getFuture():
print str(len(Sats))+" Future passes found."
def GetGroundStations():
print "Getting Ground Stations"
stations = get_paginated_endpoint("https://network.satnogs.org/api/stations/")
return stations
@ -90,7 +83,6 @@ def updateTLE():
print('\nTLEs for {} of {} requested satellites found ({} satellites with temporary norad ids skipped).'.format(len(tles), len(satnogs_db_norad_ids), len(temporary_norad_ids)))
@scheduler.scheduled_job('interval', days=5)
def updateTransmitters():
global Transmitters
@ -103,16 +95,19 @@ def updateTransmitters():
for y in Transmitters[x].keys():
SatDescrip[x] += '<div class="trans" style="background-color:#'+str("%02x" % Transmitters[x][y][1][0])+str("%02x" % Transmitters[x][y][1][1])+str("%02x" % Transmitters[x][y][1][2])+'";>'+Transmitters[x][y][0]+'</div>'
@scheduler.scheduled_job('interval', hours=1)
def updatePasses():
getFuture()
@scheduler.scheduled_job('interval', hours=1)
def updateStations():
global Stations
print "Updating Stations"
Stations = GetGroundStations()
@scheduler.scheduled_job('interval', minutes=30)
def updateCZML():
global CZML
@ -126,8 +121,6 @@ def updateCZML():
doc["clock"]["step"] = "SYSTEM_CLOCK"
CZML.append(doc)
StationList = {}
for x in tqdm(Stations):
color = [0, 230, 64, 255]
if x["status"] == "Testing":
@ -153,19 +146,12 @@ def updateCZML():
station["description"] += x["qthlocator"] + "</b><br></b>Description: </b>" + x["description"]
CZML.append(station)
for x in tqdm(Sats.keys()):
for y in Sats[x]:
sat = {}
sat["id"] = str(y["id"])
sat["name"] = TLEs[x][0]
sat["show"] = True
#sat["point"] = {}
#sat["point"]["color"] = {}
#sat["point"]["color"]["rgba"] = [255,0,0,255]
#sat["point"]["pixelSize"]=8.0
sat["billboard"] = {"image": "static/sat.png", "scale": 0.50}
sat["position"] = {}
sat["position"]["cartographicDegrees"] = []
@ -174,7 +160,6 @@ def updateCZML():
satObj = EarthSatellite(TLEs[x][1], TLEs[x][2], TLEs[x][0])
time = 0
while temp <= y["end"] + timedelta(minutes=1):
subpoint = satObj.at(ts.utc(temp)).subpoint()
lat = subpoint.latitude.degrees
lng = subpoint.longitude.degrees
@ -186,7 +171,6 @@ def updateCZML():
sat["position"]["interpolationDegree"] = 5
sat["position"]["epoch"] = (y["start"].isoformat()+"Z").replace("+00:00", "")
sat["path"] = {"show": {"interval": (y["start"].isoformat()+"Z").replace("+00:00", "") + "/" + ((y["end"] + timedelta(minutes=1)).isoformat()+"Z").replace("+00:00", ""), "boolean": True}, "width": 2, "material": {"solidColor": {"color": {"rgba": [0, 255, 0, 255]}}}, "leadTime": 100000, "trailTime": 100000}
CZML.append(sat)
for x in tqdm(Sats.keys()):
for y in Sats[x]:
@ -196,34 +180,34 @@ def updateCZML():
CZML.append(sat)
@app.route("/")
def index():
return render_template("index.html", url="/czml")
@app.route("/station/<int:station_id>")
def index_station(station_id):
return render_template("index.html", url="/czml/" + str(station_id))
@app.route('/future_sats')
def api_future_sats():
return json.dumps(Sats)
@app.route("/czml")
def api_czml():
return json.dumps(CZML)
@app.route("/broken")
def api_broken():
output = defaultdict(list)
for x in broken.keys():
output[x] = list(broken[x])
return json.dumps(output)
@app.route("/czml/<int:station_id>")
def api_station(station_id):
czml = []
@ -237,7 +221,6 @@ def api_station(station_id):
doc["clock"]["step"] = "SYSTEM_CLOCK"
czml.append(doc)
for x in Stations:
if x["id"] == station_id:
color = [0, 230, 64, 255]
@ -245,7 +228,6 @@ def api_station(station_id):
color = [248, 148, 6, 255]
if x["status"] == "Offline":
color = [255, 0, 0, 50]
station = {}
station["id"] = str(x["id"])
station["name"] = x["name"]
@ -265,7 +247,6 @@ def api_station(station_id):
czml.append(station)
break
for y in StationsPasses[station_id]:
sat = {}
sat["id"] = str(y["id"])
@ -275,7 +256,6 @@ def api_station(station_id):
sat["point"]["color"] = {}
sat["point"]["color"]["rgba"] = [255, 0, 0, 255]
sat["point"]["pixelSize"] = 8.0
#sat["billboard"] = {"image":"static/sat.png","scale":0.50}
sat["position"] = {}
sat["position"]["cartographicDegrees"] = []
sat["description"] = SatDescrip[y["norad"]]
@ -283,7 +263,6 @@ def api_station(station_id):
satObj = EarthSatellite(TLEs[y["norad"]][1], TLEs[y["norad"]][2], TLEs[y["norad"]][0])
time = 0
while temp <= y["end"]+timedelta(minutes=1):
subpoint = satObj.at(ts.utc(temp)).subpoint()
lat = subpoint.latitude.degrees
lng = subpoint.longitude.degrees
@ -295,25 +274,18 @@ def api_station(station_id):
sat["position"]["interpolationDegree"] = 5
sat["position"]["epoch"] = (y["start"].isoformat() + "Z").replace("+00:00", "")
sat["path"] = {"show": {"interval": (y["start"].isoformat()+"Z").replace("+00:00", "") + "/" + ((y["end"] + timedelta(minutes=1)).isoformat() + "Z").replace("+00:00", ""), "boolean": True}, "width": 2, "material": {"solidColor": {"color": {"rgba": [0, 255, 0, 255]}}}, "leadTime": 100000, "trailTime": 100000}
czml.append(sat)
for y in StationsPasses[station_id]:
sat = {}
sat["id"] = str(y["id"])+"Link"
sat["polyline"] = {"show": {"interval": (y["start"].isoformat()+"Z").replace("+00:00", "") + "/" + ((y["end"] + timedelta(minutes=1)).isoformat()+"Z").replace("+00:00", ""), "boolean": True}, "width": 2, "material": {"solidColor": {"color": {"rgba": y["transmitter"][1]}}}, "followSurface": False, "positions": {"references": [str(y["id"]) + "#position", str(station_id) + "#position"]}}
czml.append(sat)
return json.dumps(czml)
#updatePasses()
updateStations()
updateTransmitters()
getFuture()
updateCZML()
#updateTLE()
#updatePasses()
scheduler.start()
app.run(use_reloader=False, host="0.0.0.0", port=5001)