/*
* CUSF Landing Prediction Version 2
* Jon Sowman 2010
* jon@hexoc.com
* http://www.hexoc.com
*
* http://github.com/jonsowman/cusf-standalone-predictor
*
*/
$(document).ready(function() {
// are we trying to display an old prediction?
if(window.location.hash != "") {
var ln = window.location.hash.split("=");
var posteq = ln[1];
if ( posteq.length != 40 ) {
throwError("The supplied hashstring was not a valid UUID.");
appendDebug("The hashstring was not the expected length");
} else {
current_uuid = posteq;
}
}
initMap(52, 0, 8);
populateLaunchSite();
setupEventHandlers();
// Make launch card draggable
$("#input_form").draggable({containment: '#map_canvas', handle: 'img.handle', snap: '#map_canvas'});
$("#scenario_info").draggable({containment: '#map_canvas', handle: 'img.handle', snap: '#map_canvas'});
$("#location_save").draggable({containment: '#map_canvas', handle: 'img.handle', snap: '#map_canvas'});
$("#location_save_local").draggable({containment: '#map_canvas', handle: 'img.handle', snap: '#map_canvas'});
$("#run_pred_btn").button();
$("#req_sub_btn").button();
$("#burst-calc-hide").button();
// see if we want an old prediction displayed
if ( current_uuid != '0' ) {
appendDebug("Got an old UUID to plot: " + current_uuid);
appendDebug("Trying to populate form with scenario data...");
populateFormByUUID(current_uuid);
appendDebug("Trying to get progress JSON");
$.getJSON("preds/"+current_uuid+"/progress.json", function(progress) {
appendDebug("Got progress JSON from server for UUID");
if ( progress['error'] || !progress['pred_complete'] ) {
appendDebug("The prediction was not completed correctly, quitting");
} else {
writePredictionInfo(current_uuid, progress['run_time'], progress['gfs_timestamp']);
getCSV(current_uuid);
}
});
}
// plot the initial launch location
plotClick();
// Initialise the burst calculator
calc_init();
});
function predSub() {
appendDebug(null, 1); // clear debug window
appendDebug("Sending data to server...");
// initialise progress bar
$("#prediction_progress").progressbar({ value: 0 });
$("#prediction_status").html("Sending data to server...");
$("#status_message").fadeIn(250);
}
function populateFormByUUID(pred_uuid) {
$.get("ajax.php", { "action":"getModelByUUID", "uuid":pred_uuid }, function(data) {
if ( !data.valid ) {
appendDebug("Populating form by UUID failed");
appendDebug("The server said the model it made was invalid");
} else {
// we're good to go, populate the form
$("#lat").val(data.latitude);
$("#lon").val(data.longitude);
$("#initial_alt").val(data.altitude);
$("#hour").val(data.hour);
// we need to make minutes be "04" instead of "4"
var scenario_minute = data.minute;
if ( scenario_minute < 10 ) scenario_minute = "0" + scenario_minute;
$("#min").val(scenario_minute);
$("#second").val(data.second);
$("#day").val(data.day);
$("#month").attr("selectedIndex", data.month-1);
$("#year").val(data.year);
// we have to use [] notation for
// values that have -s in them
$("#ascent").val(data['ascent-rate']);
$("#drag").val(data['descent-rate']);
$("#burst").val(data['burst-altitude']);
$("#software").val(data.software);
$("#delta_lat").val(data['lat-delta']);
$("#delta_lon").val(data['lon-delta']);
// now sort the map out
SetSiteOther();
plotClick();
}
}, 'json');
}
function addHashLink(link) {
var ln = "#!/" + link;
window.location = ln;
}
function showMousePos(GLatLng) {
var curr_lat = GLatLng.lat().toFixed(4);
var curr_lon = GLatLng.lng().toFixed(4);
$("#cursor_lat").html(curr_lat);
$("#cursor_lon").html(curr_lon);
// if we have a prediction displayed
// show range from launch and land:
if ( current_uuid != 0 && map_items['launch_marker'] != null ) {
var launch_pt = map_items['launch_marker'].position;
var land_pt = map_items['land_marker'].position;
var range_launch = distHaversine(launch_pt, GLatLng, 1);
var range_land = distHaversine(land_pt, GLatLng, 1);
$("#cursor_pred_launchrange").html(range_launch);
$("#cursor_pred_landrange").html(range_land);
}
}
function populateLaunchSite() {
$("#site > option").remove();
$.getJSON("sites.json", function(sites) {
$.each(sites, function(sitename, site) {
$("