Skip to content

Instantly share code, notes, and snippets.

@mraspaud
Created June 9, 2014 10:36
Show Gist options
  • Save mraspaud/c344f4de6a43cda1b931 to your computer and use it in GitHub Desktop.
Save mraspaud/c344f4de6a43cda1b931 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2014 Martin Raspaud
# Author(s):
# Martin Raspaud <martin.raspaud@smhi.se>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""generate a report.
"""
import logging
from mpop.projector import get_area_def
from trollsched.schedule import get_next_passes, Boundary
from datetime import datetime, timedelta
def read_sched(filename, allpasses, name):
with open(filename) as fp_:
while True:
line = fp_.readline()
try:
if line[0] in ["\n", " ", "!", "#"]:
continue
except IndexError:
break
elts = line[16:].split()
sat = line[:16].strip().lower()
if sat == "npp":
sat = "suomi npp"
rise, fall = "".join(elts[1:3]), "".join(elts[3:5])
rec = elts[8]
rise = datetime.strptime(rise, "%Y%m%d%H%M%S")
fall = datetime.strptime(fall, "%Y%m%d%H%M%S")
for opass in allpasses:
if (opass.satellite == sat and
(abs(opass.risetime - rise) < timedelta(seconds=60) or
abs(opass.falltime - fall) < timedelta(seconds=60))):
setattr(opass, name, rec=="Y")
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG,
format='[%(asctime)s %(levelname)s] %(message)s')
logger = logging.getLogger("generate_report")
utctime = datetime(2014, 1, 18, 14, 20)
satellites = ["noaa 19", "noaa 18", "noaa 16", "noaa 15",
"metop-a", "metop-b",
"terra", "aqua",
"suomi npp"]
logger.info("Computing next satellite passes")
tle_file = "/local_disk/usr/src/satsa-tools/scheduler/tle_20140120.txt"
forward = 24 * 9
coords = (16.148649, 58.581844, 0.052765)
allpasses = get_next_passes(satellites, utctime, forward, coords, tle_file)
logger.info("Computation of next overpasses done")
read_sched("schedule_20140120141352.txt", allpasses, "scisys")
read_sched("newsched9days.sched", allpasses, "satsa")
area = get_area_def("euron1")
lons, lats = area.get_boundary_lonlats()
area_boundary = Boundary((lons.side1, lats.side1),
(lons.side2, lats.side2),
(lons.side3, lats.side3),
(lons.side4, lats.side4))
area_boundary.decimate(500)
area.poly = area_boundary.contour_poly()
with open("/tmp/imgs.gv", "w") as fd_:
for passage in sorted(allpasses):
labels = []
try:
if passage.scisys:
labels.append(((0.1, 0.2, "Scisys"),
{"bbox": {'facecolor':'red', 'alpha':0.5, 'pad':10}}))
except AttributeError:
logger.warning("Missing pass in scisys sched: " + str(passage))
try:
if passage.satsa:
labels.append(((0.1, 0.25, "Satsa"),
{"bbox": {'facecolor':'green', 'alpha':0.5, 'pad':10}}))
except AttributeError:
logger.warning("Missing pass in satsa sched: " + str(passage))
#passage.show(area.poly, labels=labels)
filename = passage.save_fig(area.poly, "./plots", labels=labels)
fd_.write(' "' + str(passage) + '" [ image="' + filename
+ '"];\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment