Example #1
0
    def test_scheduler(self):
        dt_format = "%y-%m-%d %H%M"
        print("")
        for vs in self.content.vesselSeasons.values():
            schedule_season(vs)

            # display some results

            for c in vs.cruises:
                print("======== %s" % (c.name))
                visits = c.get_visitations()
                v = visits[0]
                print("%s depart %s" % (v.get_arrival_dt().strftime(dt_format),
                                        v.location.identifier))
                for i in range(1, len(visits) - 1):
                    v = visits[i]
                    if v.is_stopover():
                        print("%s arrive %s, stay %d hours (expected %d)" %
                              (v.get_arrival_dt().strftime(dt_format),
                               v.location.identifier,
                               hours(v.get_computed_duration()),
                               hours(v.get_planned_duration_td())))
                    else:
                        print("%s waypoint %s" %
                              (v.get_arrival_dt().strftime(dt_format),
                               v.location.identifier))
                v = visits[-1]
                print("%s arrive destination  %s" %
                      (v.get_arrival_dt().strftime(dt_format),
                       v.location.identifier))
Example #2
0
def cdlfile_to_MD(content: CdlFile, identifier):

    output = StringIO()

    dt_format = "%Y-%m-%d %H%M"
    dt_short_format = "%d/%m %H%M"
    #dt_short_format = "%m-%d %H%M"
    for vs in content.vesselSeasons.values():
        output.write("# %s\n" % (vs.identifier(), ))
        output.write("Generated from file %s\n" % (identifier, ))
        schedule_season(vs)

        # display some results

        for c in vs.cruises:
            output.write("## %s\n" % (c.name, ))
            output.write(c.get_description())
            output.write("\n")
            output.write("\n")

            output.write("Date/time    | Event | From<br/>To | Comment\n")
            output.write(
                "------------ | ----- | --------- | ------------------\n")
            #            visits = c.get_visitations()
            #            v = visits[0]
            #            print("%s | depart | %s |" % (v.get_arrival_dt().strftime(dt_format), v.location.identifier))

            for leg in c.legs:
                visits = leg.visitations
                from_v = visits[0]
                to_v = visits[-1]
                times = "%s<br/>%s" % (
                    from_v.get_departure_dt().strftime(dt_short_format),
                    to_v.get_arrival_dt().strftime(dt_short_format))

                events = "depart<br/>arrive"
                locs = "%s<br/>%s" % (from_v.location.identifier,
                                      to_v.location.identifier)
                comments = ""
                comments += "<br/>stay in %s is  %s hours (expected %d)" % (
                    to_v.location.identifier,
                    hours(to_v.get_computed_duration()),
                    hours(to_v.get_planned_duration_td()))
                for w in leg.get_warnings():
                    comments += "<br/>&#x1F534; %s" % (w.get_message())

                output.write("%s | %s | %s | %s\n" %
                             (times, events, locs, comments[5:]))
    output.seek(0)
    return output
    return template.render({
        "vessel_seasons":
        context,
        "filename":
        identifier,
        "file_modified_timestamp":
        datetime.fromtimestamp(
            os.path.getmtime(identifier)).strftime("%d/%m/%Y, %H:%M:%S")
    })

    return


if __name__ == '__main__':

    filename = None
    if len(argv) > 1:
        filename = argv[1]
    try:
        analyser = CdlFileAnalyser()
        fin = preprocess_named_file(filename)
        content = analyser.analyse(fin)
        for vs in content.vesselSeasons.values():
            schedule_season(vs)
        output = cdlfile_to_html(content, filename or "stdin")
        for line in output:
            stdout.write(line)
    except Exception as e:
        raise e
        die(str(e))
Example #4
0
def cdlfile_to_KML(content: CdlFile, identifier):
    dt_format = "%Y-%m-%d %H%M"

    # document

    kml = simplekml.Kml(open=1)
    desc = "Generated from CDL file at %s" % (datetime.now().isoformat())
    doc = kml.newdocument(name=identifier, description=desc, open=1)

    # locations

    desc = "All of the locations mentioned in the CDL source file"
    loc_folder = doc.newfolder(name="Locations",
                               description=desc,
                               open=0,
                               visibility=0)

    for loc in content.locations.values():
        pnt = loc_folder.newpoint(name=loc.identifier,
                                  description=loc.name,
                                  coords=[loc.coords])

    # vessel/seasons
    for vs in content.vesselSeasons.values():
        # schedule this season
        schedule_season(vs)

        # prepare a folder for each vessel/season

        description = "Plan for vessel %s, %s season" % (vs.vessel.identifier,
                                                         vs.season)
        vs_folder = doc.newfolder(name=vs.key(), description=desc, open=1)

        # cruises
        for c in vs.cruises:
            desc = "%s cruise, %d nautical miles in %d days" % (
                c.name, round(c.distance_NM()),
                int(ceil(c.elapsed_time_td().total_seconds() / 86400.0)))
            c_folder = vs_folder.newfolder(name=c.name,
                                           description=desc,
                                           open=0,
                                           visibility=1)

            # draw it as legs
            leg_no = 0
            for leg in c.legs:
                leg_no += 1
                desc = "%d nautical miles in %d hours." % (
                    round(leg.distance_NM()),
                    round(leg.sailing_time().total_seconds() / 3600.0))
                leg_folder = c_folder.newfolder(name=leg.name(),
                                                description=desc)

                # draw the route
                route = []
                for v in leg.visitations:
                    route.append(v.location.coords)

                ls = leg_folder.newlinestring(name=leg.name())
                ls.coords = route
                ls.description = "%s<br/>%s" % (leg.name(), desc)
                ls.style.linestyle.width = 10
                ls.style.linestyle.color = simplekml.Color.red

            # stopovers

            stop_folder = c_folder.newfolder(name="stopovers",
                                             visibility=1,
                                             open=0)
            for leg in c.legs:
                ll_comment = ""
                last_leg = leg == c.legs[-1]
                if last_leg:
                    ll_comment = "(end of %s cruise)" % (leg.cruise.name, )
                sv = leg.visitations[-1]  # stopover visitation
                # buiild the stopover description
                desc = "%s stopover %s</br>%s</br>arrived %s</br>depart  %s</br>crew: %s" % (
                    leg.destination().identifier, ll_comment,
                    sv.get_duration_description(),
                    sv.get_arrival_dt().strftime(dt_format),
                    sv.get_departure_dt().strftime(dt_format), str(sv.crew))
                pnt = stop_folder.newpoint(name=leg.destination().identifier,
                                           coords=[leg.destination().coords],
                                           description=desc)
                if last_leg:
                    pnt.style.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/shapes/flag.png'
                else:
                    pnt.style.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/shapes/marina.png'

    # all done, return the kml

    return kml.kml()
def cdlfile_to_HTML(content: CdlFile, identifier):

    output = StringIO()
    output.write("<html>\n")
    output.write("<head>\n")
    output.write("<title>Season Summary</title>\n")
    output.write("</head>\n")

    # framework code for OpenLayers mapping integration

    output.write("<style>#map {\n")
    output.write("width: 900px;\n")
    output.write("height: 500px;\n")
    output.write("}</style>\n")
    output.write(
        '<link rel="stylesheet" href="/cruise_maps/openlayers.e31bb0bc.css">\n'
    )

    output.write("<body>\n")
    output.write("<p>\n")

    #   Source file and version timestamp

    path = Path(identifier)
    content_id = path.name
    file_last_modified = datetime.fromtimestamp(os.path.getmtime(identifier))

    output.write("This cruise summary was generated from the file ")
    output.write('<a href="source.html">%s</a> ' % (content_id))
    # output.write('<a href="%s">%s</a> ' % (content_id, content_id))
    output.write(' last modified at %s ' %
                 file_last_modified.strftime("%d/%m/%Y, %H:%M:%S"))
    output.write("</p>\n")

    for vs in content.vesselSeasons.values():

        output.write("<h1>Summary for season %s on vessel %s</h1> \n" %
                     (vs.season, vs.vessel.name))

        output.write("<h2>Season summary</h2> \n")
        output.write("<p>A total of %d nautical miles</p>\n" %
                     (int(vs.distance_NM())))
        output.write(
            "<p>The following table lists the series of cruises planned for the season<br/></p>\n"
        )

        output.write('<table>\n')
        output.write('<tr>\n')
        output.write('<td>\n')
        output.write('<table border="1">\n')
        output.write(
            "<tr><th>Cruise</th><th>Cruise name</th><th>Departure</th><th>Destination</th><th>Duration</th></tr>\n"
        )

        schedule_season(vs)

        # display some results

        cruise_no = 0
        for c in vs.cruises:
            cruise_no += 1

            departure_text = "%s<br>%s" % (
                c.departure_port.identifier,
                c.get_departure_dt().strftime("%d/%m/%Y"))
            destination_text = "%s<br>%s (stay for %s days)" % (
                c.get_destination_port().identifier,
                c.get_arrival_dt().strftime("%d/%m/%Y"),
                c.get_destination_stay_dt().days)
            if c.elapsed_time_td().days == 0:
                duration = "%s hours" % (round(
                    c.elapsed_time_td().seconds / 3600.0))
            else:
                duration = "%s days" % (c.elapsed_time_td().days)

            output.write("<tr>")
            output.write("<td>%s</td>" % (cruise_no))
            output.write("<td>%s</td>" % (c.name))
            output.write("<td>%s</td>" % (departure_text))
            output.write("<td>%s</td>" % (destination_text))
            output.write("<td>%s</td>" % (duration))
            output.write("</tr>\n")

        output.write("</table>\n")
        output.write('</td>\n')
        output.write('<td>\n')

        # Open layers chart goes here

        output.write('<div id="map"></div>\n')
        output.write(
            '<script src="/cruise_maps/openlayers.e31bb0bc.js"></script>\n')

        output.write('</td>\n')
        output.write('</tr>\n')
        output.write('</table>\n')

        output.write("<h2>Warning</h2> \n")
        output.write(
            "<p>Please note that yacht cruising is subject to weather, crew health and other saftey concerns.\n"
        )
        output.write(
            "The published schedule is subject to change without notice. Please include flexibility in your flight and accommodation bookings</p>\n"
        )
        output.write("<h2>Further information</h2> \n")
        output.write(
            "<p>For further information regarding this season plan please refere to:\n"
        )
        output.write("</p>\n")
        output.write("<ul>\n")
        output.write(
            '<li><a href="./schedule.html">Detailed season schedule</a></li>\n'
        )
        output.write(
            '<li><a href="./crew_movement.html">Crew movements</a></li>\n')
        output.write(
            '<li><a href="./cabins.html">Cabin occupation</a> -- who, when and where</li>\n'
        )
        output.write(
            '<li><a href="./chart.kml">Chart</a> (via Google Earth) -- once the chart.kml file has downloaded, double-click on it to launch Google Earth. '
        )
        output.write(
            'You may need to load <a href="https://support.google.com/earth/answer/21955?hl=en">install Google Earth</a> on your computer</li>\n'
        )
        output.write("</ul>\n")

        output.write("<h2>Goolge Earth link</h2>\n")
        output.write(
            "<p>To view the chart in Google Earth, use menus options <bold>Add ... Network Link</bold> and paste in the following URL.\n"
        )
        output.write('<textarea rows="1" cols="60" readonly>')
        output.write("http://planacruise.online/%s/%s/chart.kml\n" %
                     (vs.vessel.identifier, vs.season))
        output.write("</textarea>")

    output.write("</body>\n")
    output.write("</html>\n")
    output.seek(0)
    return output