import calendar from tawhiri import solver, models, kml from tawhiri.dataset import Dataset as WindDataset from ruaumoko import Dataset as ElevationDataset lat0 = 52.5563 lng0 = 360 - 3.1970 alt0 = 0.0 t0 = calendar.timegm(datetime(2014, 2, 19, 15).timetuple()) tE = calendar.timegm(datetime(2014, 2, 20, 6, 1).timetuple()) wind = WindDataset.open_latest() stages = models.float_profile(2.0, 10000, tE, wind) rise, float = solver.solve(t0, lat0, lng0, alt0, stages) assert rise[-1] == float[0] with open("test_prediction_data.js", "w") as f: f.write("var data = ") json.dump([(lat, lon) for _, lat, lon, _ in rise + float], f, indent=4) f.write(";\n") markers = [ {'name': 'launch', 'description': 'TODO', 'point': rise[0]}, {'name': 'reached float', 'description': 'TODO', 'point': float[0]} ] kml.kml([rise, float], markers, 'test_prediction.kml')
def run_prediction(req): """ Run the prediction. """ # Response dict resp = { "request": req, "prediction": [], } # Dataset try: if req['dataset'] == LATEST_DATASET_KEYWORD: tawhiri_ds = WindDataset.open_latest(persistent=True) else: tawhiri_ds = WindDataset(datetime.fromtimestamp(req['dataset'])) except IOError: raise InvalidDatasetException("No matching dataset found.") except ValueError as e: raise InvalidDatasetException(*e.args) # Note that hours and minutes are set to 00 as Tawhiri uses hourly datasets resp['request']['dataset'] = tawhiri_ds.ds_time.strftime( "%Y-%m-%dT%H:00:00Z") # Stages if req['profile'] == PROFILE_STANDARD: stages = models.standard_profile(req['ascent_rate'], req['burst_altitude'], req['descent_rate'], tawhiri_ds, ruaumoko_ds()) elif req['profile'] == PROFILE_FLOAT: stages = models.float_profile(req['ascent_rate'], req['float_altitude'], req['stop_datetime'], tawhiri_ds) else: raise InternalException("No implementation for known profile.") # Run solver try: result = solver.solve(req['launch_datetime'], req['launch_latitude'], req['launch_longitude'], req['launch_altitude'], stages) except Exception as e: raise PredictionException("Prediction did not complete: '%s'." % str(e)) # Format trajectory if req['profile'] == PROFILE_STANDARD: resp['prediction'] = _parse_stages(["ascent", "descent"], result) elif req['profile'] == PROFILE_FLOAT: resp['prediction'] = _parse_stages(["ascent", "float"], result) else: raise InternalException("No implementation for known profile.") # Convert request UNIX timestamps to RFC3339 timestamps for key in resp['request']: if "datetime" in key: resp['request'][key] = _timestamp_to_rfc3339(resp['request'][key]) return resp
import calendar from tawhiri import solver, models, kml from tawhiri.dataset import Dataset as WindDataset from ruaumoko import Dataset as ElevationDataset lat0 = 52.5563 lng0 = 360 - 3.1970 alt0 = 0.0 t0 = calendar.timegm(datetime(2014, 2, 19, 15).timetuple()) wind = WindDataset.open_latest() elevation = ElevationDataset() stages = models.standard_profile(5.0, 30000, 5.0, wind, elevation) rise, fall = solver.solve(t0, lat0, lng0, alt0, stages) assert rise[-1] == fall[0] with open("test_prediction_data.js", "w") as f: f.write("var data = ") json.dump([(lat, lon) for _, lat, lon, _ in rise + fall], f, indent=4) f.write(";\n") markers = [{ 'name': 'launch', 'description': 'TODO', 'point': rise[0] }, { 'name': 'landing', 'description': 'TODO',
def run_prediction(req): """ Run the prediction. """ # Response dict resp = { "request": req, "prediction": [], } warningcounts = WarningCounts() # Find wind data location ds_dir = app.config.get('WIND_DATASET_DIR', WindDataset.DEFAULT_DIRECTORY) # Dataset try: if req['dataset'] == LATEST_DATASET_KEYWORD: tawhiri_ds = WindDataset.open_latest(persistent=True, directory=ds_dir) else: tawhiri_ds = WindDataset(datetime.fromtimestamp(req['dataset']), directory=ds_dir) except IOError: raise InvalidDatasetException("No matching dataset found.") except ValueError as e: raise InvalidDatasetException(*e.args) # Note that hours and minutes are set to 00 as Tawhiri uses hourly datasets resp['request']['dataset'] = \ tawhiri_ds.ds_time.strftime("%Y-%m-%dT%H:00:00Z") # Stages if req['profile'] == PROFILE_STANDARD: stages = models.standard_profile(req['ascent_rate'], req['burst_altitude'], req['descent_rate'], tawhiri_ds, ruaumoko_ds(), warningcounts) elif req['profile'] == PROFILE_FLOAT: stages = models.float_profile(req['ascent_rate'], req['float_altitude'], req['stop_datetime'], tawhiri_ds, warningcounts) else: raise InternalException("No implementation for known profile.") # Run solver try: result = solver.solve(req['launch_datetime'], req['launch_latitude'], req['launch_longitude'], req['launch_altitude'], stages) except Exception as e: raise PredictionException("Prediction did not complete: '%s'." % str(e)) # Format trajectory if req['profile'] == PROFILE_STANDARD: resp['prediction'] = _parse_stages(["ascent", "descent"], result) elif req['profile'] == PROFILE_FLOAT: resp['prediction'] = _parse_stages(["ascent", "float"], result) else: raise InternalException("No implementation for known profile.") # Convert request UNIX timestamps to RFC3339 timestamps for key in resp['request']: if "datetime" in key: resp['request'][key] = _timestamp_to_rfc3339(resp['request'][key]) resp["warnings"] = warningcounts.to_dict() return resp
import time from datetime import timedelta import calendar from tawhiri import solver, models from tawhiri.dataset import Dataset as WindDataset from tawhiri.warnings import WarningCounts from ruaumoko import Dataset as ElevationDataset elevation = ElevationDataset() warningcounts = WarningCounts() lat0 = 52.0 lng0 = 0.0 alt0 = 0.0 n_repeats = 100 start_time = time.time() for i in range(n_repeats): wind = WindDataset.open_latest(persistent=True) t0 = wind.ds_time + timedelta(hours=12) t0 = calendar.timegm(t0.timetuple()) stages = models.standard_profile(5.0, 30000, 5.0, wind, elevation, warningcounts) result = solver.solve(t0, lat0, lng0, alt0, stages) end_time = time.time() print("Averaged {:.1f}ms per prediction".format( ((end_time - start_time)/n_repeats)*1000.0))
import calendar from tawhiri import solver, models, kml from tawhiri.dataset import Dataset as WindDataset from ruaumoko import Dataset as ElevationDataset lat0 = 52.5563 lng0 = 360 - 3.1970 alt0 = 0.0 t0 = calendar.timegm(datetime(2014, 2, 19, 15).timetuple()) wind = WindDataset.open_latest() elevation = ElevationDataset() stages = models.standard_profile(5.0, 30000, 5.0, wind, elevation) rise, fall = solver.solve(t0, lat0, lng0, alt0, stages) assert rise[-1] == fall[0] with open("test_prediction_data.js", "w") as f: f.write("var data = ") json.dump([(lat, lon) for _, lat, lon, _ in rise + fall], f, indent=4) f.write(";\n") markers = [ {'name': 'launch', 'description': 'TODO', 'point': rise[0]}, {'name': 'landing', 'description': 'TODO', 'point': fall[-1]}, {'name': 'burst', 'description': 'TODO', 'point': fall[0]} ] kml.kml([rise, fall], markers, 'test_prediction.kml')
import calendar from tawhiri import solver, models, kml from tawhiri.dataset import Dataset as WindDataset from ruaumoko import Dataset as ElevationDataset lat0 = 52.5563 lng0 = 360 - 3.1970 alt0 = 0.0 t0 = calendar.timegm(datetime(2014, 2, 19, 15).timetuple()) tE = calendar.timegm(datetime(2014, 2, 20, 6, 1).timetuple()) wind = WindDataset.open_latest() stages = models.float_profile(2.0, 10000, tE, wind) rise, float = solver.solve(t0, lat0, lng0, alt0, stages) assert rise[-1] == float[0] with open("test_prediction_data.js", "w") as f: f.write("var data = ") json.dump([(lat, lon) for _, lat, lon, _ in rise + float], f, indent=4) f.write(";\n") markers = [{ 'name': 'launch', 'description': 'TODO', 'point': rise[0] }, { 'name': 'reached float', 'description': 'TODO',