def get_current_time(request): '''Get the current time. This could be 'now' or stored in the session''' epoch = None tz_offset = 0 if 'object_list_form' in request.session: epoch = str(request.session['object_list_form']['epoch']) tz_offset = float(request.session['object_list_form']['tz_offset']) if tz_offset is None: tz_offset = 0 if not epoch: date = ephem.now() else: date = ephem.Date(epoch) - tz_offset*ephem.hour else: date = ephem.now() return date
def plot_sky_map(objs, date=None, new_window=False, airmass_high=None, tel_alt=90, tel_az=45, imsize=5, crop=90): '''Plots the objects for a given night for the given objects (expected to be of type Objects). Returns two strings: the first is the binary PNG file that is the graph, the second is the <map> HTML that will be used to make the points in the graph "hot spots"''' if date is None: date = ephem.now() else: date = ephem.Date(date) MWO = genMWO(date) # Setup the graph fig = Figure((imsize,imsize), frameon=False, subplotpars=SubplotParams(left=0.00,right=1.0, bottom=0.0, top=1.)) fig.subplots_adjust(left=0, right=1, top=1, bottom=0) canvas = FigureCanvasAgg(fig) ax = fig.add_subplot(111) lines = [] names = [] ids = [] maxrho = 0 for obj in objs: obj.epoch = date theta = obj.azimuth()*pi/180 rho = 90 - obj.altitude() maxrho = max(rho, maxrho) x = rho*sin(theta) y = rho*cos(theta) tobj = ax.text(x, y, osymb(obj.objtype), va='center', ha='center') lines.append(tobj) names.append(obj.name + "*"*obj.rating) ids.append(obj.pk) # Telescope pos theta = -(float(tel_az)*pi/180-pi/2) rho = 90 - float(tel_alt) ax.plot([rho*cos(theta)],[rho*sin(theta)], "o", ms=15, mfc='none', mec='red') ax.plot([rho*cos(theta)],[rho*sin(theta)], "o", ms=10, mfc='none', mec='red') # draw grid lines: for rho in arange(15,maxrho+16,15): c = Circle((0,0), radius=rho, ec='0.5', fc='none', zorder=0) ax.add_artist(c) for theta in arange(0,pi,pi/6): ax.plot([rho*cos(theta), rho*cos(theta+pi)], [rho*sin(theta),rho*sin(theta+pi)], '-', color='0.5', zorder=0) clipper=c ax.axis('off') ax.set_xlim(-100,100) ax.set_ylim(-100,100) # Try some constellations for cons in d: ras,decs = d[cons] xs,ys = [],[] draw = False for i in range(len(ras)): if ras[i] is not None: x,y,clip = RAhDecd2xy(ras[i],decs[i],MWO) if not clip: # at least part of constellation is visible draw = True else: x,y = None,None xs.append(x); ys.append(y) if draw: ax.plot(xs,ys, '-', color='0.8', zorder=0, clip_path=clipper) # Now we save to a string and also convert to a PIL image, so we can get # the size. output = StringIO.StringIO() canvas.print_figure(output, dpi=150, pad_inches=0) output.seek(0) img = Image.open(output) output2 = StringIO.StringIO() xsize,ysize = img.size img = img.crop((crop,crop,xsize-crop,ysize-crop)) img.save(output2, 'PNG') img_str = 'data:image/png,' + urllib.quote(output2.getvalue()) #output.seek(0) # Get the window coordinates of the points bboxes = [o.get_window_extent().inverse_transformed(fig.transFigure) \ for o in lines] coords = [(b.x0, 1-b.y1, b.x1, 1-b.y0) for b in bboxes] HTML = "<img style=\"margin-left:-10px;\" src=\"%s\" usemap=\"#map\" >" % img_str HTML += "<map name=\"map\">\n" for i in range(len(names)): HTML += "<area shape=rect coords=\"%d %d %d %d\" title=\"%s\" href=\"../navigator/%d/\"" \ % (int(coords[i][0]*xsize)-crop, int(coords[i][1]*ysize)-crop, int(coords[i][2]*xsize)-crop, int(coords[i][3]*ysize)-crop, names[i], ids[i]) if new_window: HTML += " target=\"object_window\">\n" else: HTML += ">\n" HTML += ''' </map> </body> </html>''' return(HTML)
def plot_alt_map(objs, date=None, toff=0, new_window=False): '''Plots the altitude for a given night for the given objects (expected to be of type Objects). Returns two strings: the first is the binary PNG file that is the graph, the second is the <map> HTML that will be used to make the points in the graph "hot spots"''' if date is None: date = ephem.now() toff = toff/24. sun = ephem.Sun() MWO = genMWO(date=date) sun.compute(MWO) # Figure out the start and stop time for the night. sunset = MWO.next_setting(sun) # In DJD sunrise = MWO.next_rising(sun) if sunset > sunrise: sunset = MWO.previous_setting(sun) #MWO.date = sunset # Setup the graph fig = Figure(subplotpars=SubplotParams(left=0.07, right=0.99)) canvas = FigureCanvasAgg(fig) ax = fig.add_subplot(111) ax.xaxis.set_major_locator(mdates.HourLocator(interval=2)) ax.xaxis.set_minor_locator(mdates.MinuteLocator(byminute=[0,60])) ax.set_xlabel('UTC time') ax.set_ylabel('Altitude') ax.grid(True, which='both', linestyle='-', color='gray') lines = [] names = [] ids = [] sunset = sunset - 60*ephem.minute sunrise = sunrise + 60*ephem.minute for obj in objs: eobj = obj.genobj() t0 = obj.rise_time() if t0 is None or t0 < sunset: t0 = sunset if t0 > sunrise: continue t1 = obj.set_time() if t1 is None or t1 > sunrise: t1 = sunrise if t1 < sunset: continue tt = date if tt < t0: tt = t0 if tt > t1: tt = t1 tt = tt*1 + 693595.5 saved_epoch = obj.epoch obj.epoch = date aa = obj.altitude() ts = arange(t0,t1+ephem.minute,10*ephem.minute) alts = [] for t in ts: #obj.epoch = t #alts.append(obj.altitude()) MWO.date = t eobj.compute(MWO) alts.append(eobj.alt*180.0/pi) alts = array(alts) mid = argmax(alts) merid = ephem.Date(ts[mid]+toff) maxalt = alts[mid] ts = ts + 693595.5 # convert to matplotlib epochs title = "Meridian @ %s (%.1fd)" % (str(merid).split()[1], maxalt) ax.text(0.5, 1.1, title, transform=ax.transAxes, ha='center', va='top', fontsize=18) ax.plot_date(ts+toff, alts, '-') pobj = ax.plot_date([tt+toff],[aa], 'o', mfc='k') lines.append(pobj[0]) names.append(obj.name) ids.append(obj.pk) obj.epoch = saved_epoch ax.set_ylim(0, 90.) ax.set_xlim(sunset*1+693595.5+toff, sunrise*1+693595.5+toff) ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M')) for tick in ax.xaxis.get_major_ticks(): tick.label2On = True ax.axvline(date*1+693595.5+toff, color='red') ax.axvline(sunset+60*ephem.minute+693595.5+toff, linestyle='--', linewidth=2, color='red') ax.axvline(sunrise-60*ephem.minute+693595.5+toff, linestyle='--', linewidth=2, color='red') ax.fill_between([sunset*1+693595.5+toff,sunrise*1+1+693595.5+toff], 0, 30, color='0.7', zorder=0) # Now we save to a string and also convert to a PIL image, so we can get # the size. output = StringIO.StringIO() canvas.print_figure(output) img_str = 'data:image/png,' + urllib.quote(output.getvalue()) output.seek(0) img = Image.open(output) output.close() xsize,ysize = img.size # Get the window coordinates of the points bboxes = [o.get_window_extent(0).inverse_transformed(fig.transFigure) \ for o in lines] coords = [(b.x0, 1-b.y1, b.x1, 1-b.y0) for b in bboxes] HTML = "<img src=\"%s\" style=\"width: 280px\" usemap=\"#map\" >" % img_str HTML += "<map name=\"map\">\n" for i in range(len(names)): HTML += "<area shape=rect coords=\"%d %d %d %d\" title=\"%s\" href=\"../object/%d/\"" \ % (int(coords[i][0]*xsize), int(coords[i][1]*ysize), int(coords[i][2]*xsize), int(coords[i][3]*ysize), names[i], ids[i]) if new_window: HTML += " target=\"object_window\">\n" else: HTML += ">\n" HTML += ''' </map> </body> </html>''' return(HTML)
def index(request): only_visible = None ha_high = settings.HA_SOFT_LIMIT epoch = None rating_low = None new_window = False tz_offset = 0 #Default: nothing posted and no session info cur_tel_obj = request.session.get('cur_tel_obj', 'Park') prev_tel_obj = request.session.get('prev_tel_obj', 'Park') selected_tab = request.session.get('selected_tab', 'table') show_types = None form = FilterForm() if request.method == "POST": if request.POST.get('action','') == 'Update': form = FilterForm(request.POST) # save this form info into the session cache request.session['object_list_form'] = request.POST.copy() elif request.POST.get('action','') == 'Park': request.session['cur_tel_obj'] = 'Park' cur_tel_obj = 'Park' else: # The reset button was called form = FilterForm() if 'object_list_form' in request.session: del request.session['object_list_form'] if 'object_list_form' in request.session: form = FilterForm(request.session['object_list_form']) if form.is_valid(): only_visible = form.cleaned_data.get('only_visible',None) ha_high = form.cleaned_data.get('ha_high',settings.HA_SOFT_LIMIT) show_types = form.cleaned_data.get('show_types', None) rating_low = form.cleaned_data.get('rating_low',None) epoch = form.cleaned_data.get('epoch',None) tz_offset =form.cleaned_data.get('tz_offset', 0) new_window = form.cleaned_data.get('new_window',False) if tz_offset is None: tz_offset = 0 if epoch: date = ephem.Date(epoch) - tz_offset*ephem.hour else: date = ephem.now() # Now deal with telescope position tel_RA,tel_DEC,tel_ha,tel_alt,tel_az = telescope_position(cur_tel_obj, date) # Also see if window is to be displayed: module_display = request.session.get('module_display', {}); obs = genMWO(date) sid_time = str(obs.sidereal_time()) obj_list = Object.objects.all() for obj in obj_list: obj.epoch = date obj.tel_az = float(tel_az) obj_list = sorted(obj_list, key=lambda a: float(a.PrecRAh())) new_list = [] for obj in obj_list: #obj.epoch = date if only_visible is not None and only_visible and not obj.visible(): continue if ha_high is not None and \ abs(obj.hour_angle()) > ha_high: continue if rating_low is not None and obj.rating < rating_low: continue if show_types is not None and obj.type not in show_types: continue new_list.append(obj) obj_list = new_list if epoch: sdate = epoch.strftime('%m/%d/%y %H:%M:%S') else: sdate = ephem.Date(ephem.now()+tz_offset*ephem.hour) sdate = sdate.datetime().strftime('%m/%d/%y %H:%M:%S') if tz_offset != 0: stz_offset = "%.1f" % (tz_offset) else: stz_offset = "" embed_image = plot_skyview.plot_sky_map(obj_list, date=date, tel_az=tel_az, tel_alt=tel_alt) alt_plot = plot_objs.plot_alt_map(obj_list, date=date, toff=tz_offset) t = loader.get_template('navigator/object_list.sortable.html') c = RequestContext(request, { 'object_list': obj_list, 'form':form, 'date':sdate, 'method':request.method, 'new_window':new_window, 'tz_offset':stz_offset, 'tel_RA':tel_RA,'tel_DEC':tel_DEC,'tel_ha':tel_ha,'tel_alt':tel_alt, 'tel_az':tel_az,'sid_time':sid_time,'embed_image':embed_image, 'module_display':module_display, 'alt_plot':alt_plot, 'selected_tab':selected_tab, }) return HttpResponse(t.render(c))
def finder(request, objectid): obj = Object.objects.get(id=objectid) flip = request.GET.get('flip',None) rotate = request.GET.get('rotate',None) size = request.GET.get('size',None) low = int(request.GET.get('low',0)) high = int(request.GET.get('high',255)) reverse = request.GET.get('reverse',None) if obj.objtype == 'SS': if 'object_list_form' in request.session: epoch = str(request.session['object_list_form']['epoch']) tz_offset = float(request.session['object_list_form']['tz_offset']) if tz_offset is None: tz_offset = 0 if not epoch: date = ephem.now() else: date = ephem.Date(ephem.Date(epoch) - tz_offset*ephem.hour) else: date = ephem.now() content = get_planets.get_image(obj.name, date, size) if content: response = HttpResponse(content, content_type="image/png") response['Content-Disposition'] = 'inline; filename=%s' % \ (obj.name+".png") return response else: response = HttpResponse(obj.finder.read(), content_type="image/png") response['Content-Disposition'] = 'inline; filename=%s' % \ (obj.name+".png") return response elif obj.objtype == "PARK": response = HttpResponse(obj.finder.read(), content_type="image/png") response['Content-Disposition'] = 'inline; filename=%s' % \ (obj.name+".png") return response obj.finder.open() # If needed do some transformations outstr = StringIO.StringIO() img = Image.open(obj.finder) if size and int(size) < settings.FINDER_BASE_SIZE: size = int(size) scale = 1.0*settings.FINDER_BASE_SIZE/img.size[0] # arc-min per pixel new_size = int(size/scale) # New size in pixels img = img.crop((int(img.size[0]-new_size)/2, int(img.size[1]-new_size)/2, int(img.size[0]+new_size)/2, int(img.size[1]+new_size)/2)) if flip and (flip == 'leftright' or flip == "both"): img = img.transpose(Image.FLIP_LEFT_RIGHT) if flip and (flip == "topbottom" or flip == "both"): img = img.transpose(Image.FLIP_TOP_BOTTOM) if rotate and rotate in rotations: img = img.transpose(rotations[rotate]) if (low != 0 or high !=255 or reverse) and (high > low): pal = palette(low, high, reverse) p = img.convert('P') p.putpalette(pal) img = p.convert('L') mid = img.size[0]/2 if reverse: fill=0 else: fill=255 img.save(outstr,'PNG') content = outstr.getvalue() outstr.close() obj.finder.close() response = HttpResponse(content, content_type="image/png") response['Content-Disposition'] = 'inline; filename=%s' % obj.finder.name return response