Skip to content

Commit

Permalink
Draw target circles/arrow client side.
Browse files Browse the repository at this point in the history
  • Loading branch information
pchote committed Sep 2, 2016
1 parent 544dec6 commit 08bac1e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 37 deletions.
44 changes: 8 additions & 36 deletions findingchart.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,14 @@
import numpy
import os
import sys
from PIL import Image, ImageOps, ImageDraw, ImageFont
from PIL import Image, ImageOps
from astropy import wcs
from astropy.io import fits

from flask import abort
from flask import Flask
from flask import jsonify
from flask import render_template
from flask import request
from flask import send_file

import sep

def estimate_half_radius(image, fallback):
Expand Down Expand Up @@ -147,46 +144,23 @@ def generate_finding_chart(out_year, in_ra, in_dec, in_year, ra_pm, dec_pm, widt
old_x, old_y = w.wcs_world2pix(numpy.array([frame_coords], numpy.float_), 0, ra_dec_order=True)[0]
new_x, new_y = w.wcs_world2pix(numpy.array([[ra_target, dec_target]], numpy.float_), 0, ra_dec_order=True)[0]

delta_x = new_x - old_x
delta_y = new_y - old_y
delta_l = math.sqrt(delta_x * delta_x + delta_y * delta_y)
dir_x = delta_x / delta_l
dir_y = delta_y / delta_l

fluxrad = estimate_half_radius(frame.data.astype(float), 2)
scaled = rescale_image_data(frame.data, 1, 99.5)
png = Image.fromarray(scaled).convert('RGB').resize((512, 512), Image.BICUBIC)
png = ImageOps.flip(Image.fromarray(scaled).convert('RGB').resize((512, 512), Image.BICUBIC))
scale_x = float(png.width) / scaled.shape[0]
scale_y = float(png.height) / scaled.shape[1]

circle_r = circle_r2 = 3 * fluxrad * scale_x

line_start_x = old_x + circle_r2 * dir_x / scale_y
line_start_y = old_y + circle_r2 * dir_y / scale_y
line_end_x = new_x - circle_r * dir_x / scale_y
line_end_y = new_y - circle_r * dir_y / scale_y

arrow_a_x = line_end_x - circle_r * (dir_y + dir_x) / scale_x
arrow_a_y = line_end_y + circle_r * (-dir_y + dir_x) / scale_y
arrow_b_x = line_end_x - circle_r * (-dir_y + dir_x) / scale_x
arrow_b_y = line_end_y + circle_r * (-dir_y - dir_x) / scale_y

draw = ImageDraw.Draw(png)
draw.ellipse((scale_x * new_x-circle_r, scale_y * new_y-circle_r, scale_x * new_x + circle_r, scale_y * new_y + circle_r), outline='red')
draw.ellipse((scale_x * old_x-circle_r2, scale_y * old_y-circle_r2, scale_x * old_x + circle_r2, scale_y * old_y + circle_r2), outline='blue')

if delta_l * scale_x > circle_r + circle_r2:
draw.line((scale_x * line_start_x, scale_y * line_start_y, scale_x * line_end_x, scale_y * line_end_y), 'blue')
draw.line((scale_x * arrow_a_x, scale_y * arrow_a_y, scale_x * line_end_x, scale_y * line_end_y), 'blue')
draw.line((scale_x * arrow_b_x, scale_y * arrow_b_y, scale_x * line_end_x, scale_y * line_end_y), 'blue')
indicator_size = 3 * fluxrad * scale_x

# Generate JSON output
output = io.BytesIO()
ImageOps.flip(png).save(output, format='PNG')
png.save(output, format='PNG')
output.seek(0)
return jsonify(
ra=sexagesimal(ra_target / 15),
dec=sexagesimal(dec_target),
data_pos=[old_x * scale_x, 512 - old_y * scale_y],
observing_pos=[new_x * scale_x, 512 - new_y * scale_y],
indicator_size=indicator_size,
data='data:image/png;base64,' + base64.b64encode(output.read()).decode())
finally:
os.remove(filename)
Expand All @@ -199,9 +173,7 @@ def input_display():

@app.route('/generate')
def generate_chart_json():
print(request.args)
try:
return generate_finding_chart(request.args['outepoch'], request.args['ra'], request.args['dec'], request.args['epoch'], request.args['rapm'], request.args['decpm'], request.args['size'], request.args['size'], request.args['survey'])
except Exception as e:
print(e)
abort(404)
abort(500)
46 changes: 45 additions & 1 deletion static/findingchart.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function generateChart(t) {
type: "GET",
url: url,
statusCode: {
404: function() {
500: function() {
chartContext.fillStyle = '#fff';
chartContext.fillRect(chartImageX+2, chartImageY+2, 508, 508);
chartContext.font = '28px serif';
Expand Down Expand Up @@ -97,6 +97,50 @@ function generateChart(t) {
chartContext.stroke();
chartContext.fillText('1\'', chartImageX + 10 + 256 / t.size, chartImageY + 497);

// Original source position
var oldX = chartImageX + json.data_pos[0];
var oldY = chartImageY + json.data_pos[1];
chartContext.lineWidth = 1;
chartContext.strokeStyle = '#0000FF';
chartContext.beginPath();
chartContext.arc(oldX, oldY, json.indicator_size, 0, 2 * Math.PI);
chartContext.stroke();

// New source position
var newX = chartImageX + json.observing_pos[0];
var newY = chartImageY + json.observing_pos[1];
chartContext.fillStyle = 'rgba(255, 0, 0, 0.5)';
chartContext.beginPath();
chartContext.arc(newX, newY, json.indicator_size, 0, 2 * Math.PI);
chartContext.fill();

// Connecting arrow
var deltaX = newX - oldX;
var deltaY = newY - oldY;
var deltaL = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
if (deltaL > 2.5 * json.indicator_size) {
var dirX = deltaX / deltaL;
var dirY = deltaY / deltaL;

var lineStartX = oldX + json.indicator_size * dirX;
var lineStartY = oldY + json.indicator_size * dirY;
var lineEndX = newX - json.indicator_size * dirX;
var lineEndY = newY - json.indicator_size * dirY;

var arrowAX = lineEndX - json.indicator_size * (dirY + dirX)
var arrowAY = lineEndY + json.indicator_size * (-dirY + dirX)
var arrowBX = lineEndX - json.indicator_size * (-dirY + dirX)
var arrowBY = lineEndY + json.indicator_size * (-dirY - dirX)

chartContext.beginPath();
chartContext.moveTo(lineStartX, lineStartY);
chartContext.lineTo(lineEndX, lineEndY);
chartContext.moveTo(arrowAX, arrowAY);
chartContext.lineTo(lineEndX, lineEndY);
chartContext.lineTo(arrowBX, arrowBY);
chartContext.stroke();
}

if (t.annotate) {
chartContext.font = '18px serif';
chartContext.fillStyle = '#000';
Expand Down

0 comments on commit 08bac1e

Please sign in to comment.