From 2d2af24b67a211ec7c0b1f8d0f360d7c215246a2 Mon Sep 17 00:00:00 2001 From: Goutte Date: Thu, 23 Mar 2017 21:51:07 +0100 Subject: [PATCH] Add a basic orbits section. --- res/dummy_jupiter_coordinates.nc | Bin 0 -> 99060 bytes res/dummy_jupiter_coordinates.txt | 12 ++++++++++++ web/run.py | 36 +++++++++++++++++++++++++++++------- web/view/home.html.jinja2 | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 135 insertions(+), 9 deletions(-) create mode 100644 res/dummy_jupiter_coordinates.nc create mode 100644 res/dummy_jupiter_coordinates.txt diff --git a/res/dummy_jupiter_coordinates.nc b/res/dummy_jupiter_coordinates.nc new file mode 100644 index 0000000..951b678 Binary files /dev/null and b/res/dummy_jupiter_coordinates.nc differ diff --git a/res/dummy_jupiter_coordinates.txt b/res/dummy_jupiter_coordinates.txt new file mode 100644 index 0000000..5dba645 --- /dev/null +++ b/res/dummy_jupiter_coordinates.txt @@ -0,0 +1,12 @@ +Name,Shape,Length +Time,"(744, 17)",12648 +XYZ_HCI,"(744, 3)",2232 +XYZ_IAU_SUN,"(744, 3)",2232 +XYZ_HEE,"(744, 3)",2232 +LON_HCI,"(744,)",744 +LAT_HCI,"(744,)",744 +LON_IAU_SUN,"(744,)",744 +LAT_IAU_SUN,"(744,)",744 +R,"(744,)",744 +StartTime,"(17,)",17 +StopTime,"(17,)",17 \ No newline at end of file diff --git a/web/run.py b/web/run.py index 623c66a..49e07a0 100755 --- a/web/run.py +++ b/web/run.py @@ -133,6 +133,13 @@ def render_view(view, context=None): # dict(tpl_global_vars.items() + context.items()) # ) +def datetime_from_list(time_list): + # Day Of Year starts at 0, but for our datetime parser it starts at 1 + doy = '{:03d}'.format(int(''.join(time_list[4:7])) + 1) + return datetime.datetime.strptime( + "%s%s%s" % (''.join(time_list[0:4]), doy, ''.join(time_list[7:])), + "%Y%j%H%M%S%f" + ) # ROUTING ##################################################################### @@ -146,6 +153,7 @@ def home(): @app.route("/inspect") def analyze_cdf(): cdf_to_inspect = get_path("../res/dummy.nc") + cdf_to_inspect = get_path("../res/dummy_jupiter_coordinates.nc") si = StringIO.StringIO() cw = csv.DictWriter(si, fieldnames=['Name', 'Shape', 'Length']) @@ -179,14 +187,8 @@ def get_csv(): data_p = cdf_handle.variables['P_dyn'] cw.writerow(('time', 'vrad', 'vtan', 'magn', 'pdyn')) for time, datum_v, datum_b, datum_p in zip(times, data_v, data_b, data_p): - # Day Of Year starts at 0, but for our datetime parser it starts at 1 - doy = '{:03d}'.format(int(''.join(time[4:7])) + 1) - d = datetime.datetime.strptime( - "%s%s%s" % (''.join(time[0:4]), doy, ''.join(time[7:])), - "%Y%j%H%M%S%f" - ) cw.writerow(( - d.strftime("%Y-%m-%dT%H:%M:%S+00:00"), + datetime_from_list(time).strftime("%Y-%m-%dT%H:%M:%S+00:00"), datum_v[0], datum_v[1], datum_b, datum_p )) @@ -194,6 +196,26 @@ def get_csv(): return si.getvalue() + +@app.route("/astral_coordinates.csv") +def get_astral_coordinates_csv(): + si = StringIO.StringIO() + cw = csv_writer(si) + + # Time, StartTime, StopTime, XYZ_HCI, XYZ_IAU_SUN, XYZ_HEE + cdf_handle = Dataset(get_path("../res/dummy_jupiter_coordinates.nc"), "r", format="NETCDF4") + times = cdf_handle.variables['Time'] + data_xyz_hci = cdf_handle.variables['XYZ_HCI'] + cw.writerow(('time', 'x_hci', 'y_hci')) + for time, datum_xyz_hci in zip(times, data_xyz_hci): + cw.writerow(( + datetime_from_list(time).strftime("%Y-%m-%dT%H:%M:%S+00:00"), + datum_xyz_hci[0], datum_xyz_hci[1] + )) + cdf_handle.close() + + return si.getvalue() + # MAIN ######################################################################## if __name__ == "__main__": diff --git a/web/view/home.html.jinja2 b/web/view/home.html.jinja2 index 85dbaed..50355f3 100755 --- a/web/view/home.html.jinja2 +++ b/web/view/home.html.jinja2 @@ -12,6 +12,7 @@ TODO

+
{% endblock %} @@ -30,6 +31,11 @@ TODO stroke: steelblue; stroke-width: 1px; } + path.orbit { + fill: none; + stroke: steelblue; + stroke-width: 2px; + } {% endblock %} @@ -41,8 +47,8 @@ TODO var swApp = (function (window, d3, $) { var $time_series = $("#time_series"); + var $orbits = $("#orbits"); - // fixme var plotTimeSeries = function(data, options) { console.log("Init time series with data", data, options); @@ -119,8 +125,86 @@ var swApp = (function (window, d3, $) { }; + var plotOrbits = function(data, options) { + console.log("Init orbits with data", data, options); + + var xScale, yScale, xAxis, yAxis; + var line; + var svg, plotWrapper, path, sun; + var width, height; + var margin = { + top: 30, + right: 20, + bottom: 30, + left: 60 + }; + + // INIT SCALES + var extremum = 1.11 * d3.max(data, function (d) { + return Math.max(Math.abs(d.x), Math.abs(d.y)); + }); + + xScale = d3.scaleLinear().domain([-1 * extremum, extremum]); + yScale = d3.scaleLinear().domain([-1 * extremum, extremum]); + + xAxis = d3.axisBottom().ticks(10); + yAxis = d3.axisLeft().ticks(10); + + // INIT LINE + line = d3.line() + .x(function (d) { return xScale(d.x) }) + .y(function (d) { return yScale(d.y) }); + + // INIT SVG + svg = d3.select('#orbits').append('svg'); + + plotWrapper = svg.append('g'); + + // Sun + sun = plotWrapper.append("svg:circle"); + sun.append('svg:title').text("Sol"); + + // Orbit + path = plotWrapper.append('path') + .datum(data) + .classed('orbit', true); + + plotWrapper.append('g').classed('x axis', true); + plotWrapper.append('g').classed('y axis', true); + + width = $orbits.width() - margin.left - margin.right; + height = width * 1.0; + + xScale.range([0, width]); + yScale.range([height, 0]); + + svg.attr('width', width + margin.right + margin.left) + .attr('height', height + margin.top + margin.bottom); + + plotWrapper.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')'); + + path.attr('d', line); + + sun.attr("cx", width / 2).attr("cy", height / 2).attr("r", 17).style("fill", "yellow"); + + xAxis.scale(xScale); + yAxis.scale(yScale); + + //if (width < 600) { xAxis.ticks(3); } + + svg.select('.x.axis') + .attr('transform', 'translate(0,' + height + ')') + .call(xAxis); + + svg.select('.y.axis') + .call(yAxis); + + + }; + return { - plotTimeSeries: plotTimeSeries + plotTimeSeries: plotTimeSeries, + plotOrbits: plotOrbits }; })(window, d3, jQuery); @@ -136,6 +220,14 @@ jQuery().ready(function($){ swApp.plotTimeSeries(data['pdyn'], {}); swApp.plotTimeSeries(data['magn'], {}); }); + d3.csv("{{ url_for('get_astral_coordinates_csv') }}", function(csv){ + var data = []; + csv.forEach(function (d) { + data.push({x: parseFloat(d['x_hci']), y: parseFloat(d['y_hci'])}); + }); + swApp.plotOrbits(data, {}); + }); + }); -- libgit2 0.21.2