Beispiel #1
0
    def detector_video(self,
                       dt,
                       savevdir,
                       radius=10.0,
                       source=None,
                       points=None,
                       good=False,
                       projection='moll',
                       lat_0=0,
                       lon_0=180.,
                       ax=None,
                       show_bodies=False,
                       style='A'):
        pole = SkyCoord([0, 0], [90, -90], frame='icrs', unit='deg')
        name_list = self.detectors.name_list
        color_list = self.detectors.color_list

        if good and source:
            index_list, centor_list = self.get_good_detector_centers(source)
        else:
            index_list = self.get_detector_index()
            centor_list = self.get_detector_centers()
        if show_bodies and self.sc_pos is not None:
            sc_pos = self.sc_pos
            earth_points_list = self.get_earth_point()
        else:
            sc_pos = None
            earth_points_list = None
        if self.time is not None:
            time = self.time
        else:
            time = None
        #fig,ax = plt.subplots()
        fig = plt.figure(figsize=(20, 10))
        ax = fig.add_subplot(111)
        duration = dt * len(self.index)

        def make_frame(t):
            n = int(t / dt)
            ax.clear()
            ax.set_title(str(n))
            map = Basemap(projection=projection,
                          lat_0=lat_0,
                          lon_0=lon_0,
                          resolution='l',
                          area_thresh=1000.0,
                          celestial=True,
                          ax=ax)
            index_, centor = index_list[n], centor_list[n]

            if source:
                ra, dec = map(source.ra.value, source.dec.value)
                map.plot(ra, dec, '*', color='#f36c21', markersize=20.)
            if points:
                ra, dec = map(points.ra.value, points.dec.value)
                map.plot(ra, dec, '*', color='#c7a252', markersize=20.)
            if show_bodies and sc_pos is not None:
                if projection in ['moll']:

                    postion, r, lon, lat = earth_points_list[n]
                    lon_lis, lat_lis = get_poly(postion, r, lon, lat, pole,
                                                lon_0)
                    for i in range(len(lon_lis)):
                        x, y = map(lon_lis[i], lat_lis[i])
                        earth = Polygon(list(zip(x, y)),
                                        facecolor='#90d7ec',
                                        edgecolor='#90d7ec',
                                        linewidth=0,
                                        alpha=1)
                        ax.add_patch(earth)
                else:
                    postion, r, lon, lat = earth_points_list[n]
                    lon, lat = map(lon, lat)
                    earth = Polygon(list(zip(lon, lat)),
                                    facecolor='#90d7ec',
                                    edgecolor='#90d7ec',
                                    linewidth=2,
                                    alpha=1)
                    ax.add_patch(earth)
                if time is not None:
                    earth_r = get_body_barycentric('earth', time[n])
                    moon_r = get_body_barycentric('moon', time[n])
                    r_e_m = moon_r - earth_r
                    r = sc_pos[n] - np.array(
                        [r_e_m.x.value, r_e_m.y.value, r_e_m.z.value]) * u.km
                    moon_point_d = cartesian_to_spherical(-r[0], -r[1], -r[2])
                    moon_ra, moon_dec = moon_point_d[2].deg, moon_point_d[
                        1].deg
                    moon_point = SkyCoord(moon_ra,
                                          moon_dec,
                                          frame='icrs',
                                          unit='deg')
                    moon_ra, moon_dec = map(moon_point.ra.deg,
                                            moon_point.dec.deg)
                    map.plot(moon_ra,
                             moon_dec,
                             'o',
                             color='#72777b',
                             markersize=20)
                    plt.text(moon_ra, moon_dec - 800000, 'moon', size=20)
                if show_bodies and time is not None:
                    tmp_sun = get_sun(time[n])
                    sun_position = SkyCoord(tmp_sun.ra.deg,
                                            tmp_sun.dec.deg,
                                            unit='deg',
                                            frame='icrs')
                    sun_ra, sun_dec = map(sun_position.ra.value,
                                          sun_position.dec.value)
                    map.plot(sun_ra,
                             sun_dec,
                             'o',
                             color='#ffd400',
                             markersize=40)
                    plt.text(sun_ra - 550000, sun_dec - 200000, 'sun', size=20)
            fovs = get_fov(centor, radius)
            if projection in ['moll']:
                for i, v in enumerate(index_):
                    r, ra, dec = fovs[i]
                    lon_lis, lat_lis = get_poly(centor[i], r, ra, dec, pole,
                                                lon_0)
                    for ij in range(len(lon_lis)):
                        x, y = map(lon_lis[ij], lat_lis[ij])
                        detec = Polygon(list(zip(x, y)),
                                        facecolor=color_list[v],
                                        edgecolor=color_list[v],
                                        linewidth=2,
                                        alpha=0.5)
                        ax.add_patch(detec)
                    ra_x, dec_y = map(centor[i].ra.value + 2.5,
                                      centor[i].dec.value - 1)
                    plt.text(ra_x,
                             dec_y,
                             str(name_list[v]),
                             color=color_list[v],
                             size=22)
            else:
                for i, v in enumerate(index_):
                    r, ra, dec = fovs[i]
                    detec = Polygon(list(zip(ra, dec)),
                                    facecolor=color_list[v],
                                    edgecolor=color_list[v],
                                    linewidth=2,
                                    alpha=0.5)
                    ax.add_patch(detec)
                    ra_x, dec_y = map(centor[i].ra.value + 2.5,
                                      centor[i].dec.value - 1)
                    plt.text(ra_x,
                             dec_y,
                             str(name_list[v]),
                             color=color_list[v],
                             size=22)

            if projection == 'moll':
                az1 = np.arange(0, 360, 30)
                zen1 = np.zeros(az1.size) + 2
                azname = []
                for i in az1:
                    azname.append(r'${\/%s\/^{\circ}}$' % str(i))
                x1, y1 = map(az1, zen1)
                for index1, value in enumerate(az1):
                    plt.text(x1[index1], y1[index1], azname[index1], size=20)
            map.drawmeridians(np.arange(0, 360, 30),
                              dashes=[1, 0],
                              color='#d9d6c3')
            map.drawparallels(np.arange(-90, 90, 15),
                              dashes=[1, 0],
                              labels=[1, 0, 0, 1],
                              color='#d9d6c3',
                              size=20)
            map.drawmapboundary(fill_color='#f6f5ec')
            n = n + 1
            return mplfig_to_npimage(fig)

        animation = VideoClip(make_frame, duration=duration)
        #animation.write_videofile(savevdir, fps=1/dt,codec = 'h264')
        animation.write_videofile(savevdir, fps=1 / dt)
        animation.close()
Beispiel #2
0
	def detector_video(self,dt,savevdir,radius = 10.0,source=None,points = None,good = False,
	                   lon_0 = 180.,ax = None,show_bodies = False,style = 'A'):
		
		name_list = self.detectors.name_list
		color_list = self.detectors.color_list
		
		if good and source :
			index_list,centor_list = self.get_good_detector_centers(source)
		else:
			index_list = self.get_detector_index()
			centor_list = self.get_detector_centers()
		if show_bodies and self.sc_pos is not None:
			sc_pos = self.sc_pos
			earth_points_list = self.get_earth_point()
		else:
			sc_pos = None
			earth_points_list = None
		if self.time is not None:
			time = self.time
		else:
			time = None
		#fig,ax = plt.subplots()
		fig = plt.figure(figsize = (20,10))
		ax = fig.add_subplot(1,1,1,projection=ccrs.Mollweide(central_longitude=lon_0),facecolor = '#f6f5ec')
		duration = dt * len(self.index)
		xticks = list(range(-180, 180, 30))
		yticks = list(range(-90, 90, 15))
		
		lons_x = np.arange(0,360,30)
		lons_y = np.zeros(lons_x.size)
		lats_y = np.arange(-75,76,15)
		lats_x = np.zeros(lats_y.size)
		
		def make_frame(t):
			n = int(t/dt)
			ax.clear()
			ax.set_title(str(n))
			#map = Basemap(projection=projection,lat_0=lat_0,lon_0 = lon_0,resolution = 'l',area_thresh=1000.0,celestial=True,ax = ax)
			index_,centor = index_list[n],centor_list[n]
			
			if source:
				#ra, dec = map(source.ra.value, source.dec.value)
				ax.plot(source.ra.value, source.dec.value, '*', color='#f36c21', markersize=20., transform=ccrs.Geodetic())
			if points:
				ax.plot(points.ra.value, points.dec.value, '*', color='#c7a252', markersize=20., transform=ccrs.Geodetic())
			if show_bodies and sc_pos is not None:
				
				postion, r, lon, lat = earth_points_list[n]
				lat[lat > 88.0] = 88.0
				lat[lat < -88.0] = -88.0
				#lon, lat = map(lon, lat)
				earth = Polygon(list(zip(lon, lat))[::-1], facecolor='#90d7ec', edgecolor='#90d7ec',
					         linewidth=2, alpha=1, transform=ccrs.Geodetic())
				ax.add_patch(earth)
				#ax.add_patch(mpatches.Circle(xy=[postion.ra.value, postion.dec.value], radius=r,
				#                             color='#90d7ec', alpha=0.3, transform=ccrs.Geodetic(),
				#                             zorder=0))
				if time is not None:
					earth_r = get_body_barycentric('earth',time[n])
					moon_r = get_body_barycentric('moon',time[n])
					r_e_m = moon_r - earth_r
					r = sc_pos[n] - np.array([r_e_m.x.value,r_e_m.y.value,r_e_m.z.value])*u.km
					moon_point_d = cartesian_to_spherical(-r[0],-r[1],-r[2])
					moon_ra,moon_dec = moon_point_d[2].deg,moon_point_d[1].deg
					moon_point = SkyCoord(moon_ra,moon_dec,frame='icrs', unit='deg')
					ax.plot(moon_point.ra.deg,moon_point.dec.deg,'o',color = '#72777b',markersize = 20,transform=ccrs.Geodetic())
					ax.text(moon_point.ra.deg,moon_point.dec.deg,'moon',size = 20,transform=ccrs.Geodetic(),va = 'center',ha='center')
				if show_bodies and time is not None:
					tmp_sun = get_sun(time[n])
					sun_position = SkyCoord(tmp_sun.ra.deg,tmp_sun.dec.deg,unit='deg', frame='icrs')
					ax.plot(sun_position.ra.value,sun_position.dec.value ,'o',color = '#ffd400', markersize=40,transform=ccrs.Geodetic())
					ax.text(sun_position.ra.value,sun_position.dec.value,'sun',size = 20,transform=ccrs.Geodetic(),va = 'center',ha='center')
					
			fovs = get_fov(centor,radius)
			
			
			for i,v in enumerate(index_):
				r,ra,dec = fovs[i]
				dec[dec > 88.0] = 88.0
				dec[dec < -88.0] = -88.0
				detec = Polygon(list(zip(ra,dec))[::-1],facecolor=color_list[v],edgecolor=color_list[v],linewidth=2, alpha=0.5,transform=ccrs.Geodetic())
				ax.add_patch(detec)
				#ra_x,dec_y = map(centor[i].ra.value+2.5,centor[i].dec.value-1)
				ax.text(centor[i].ra.value, centor[i].dec.value,str(name_list[v]), color=color_list[v], size=22,transform=ccrs.Geodetic(),va = 'center',ha='center')
			
			ax.gridlines(xlocs=xticks, ylocs=yticks)
			lats_y_ticke = ax.projection.transform_points(ccrs.Geodetic(),lats_x+lon_0+180.0, lats_y*1.1)
			lats_y_x = lats_y_ticke[:,0]*0.86
			lats_y_y = lats_y_ticke[:,1]
			
			proj_xyz = ax.projection.transform_points(ccrs.Geodetic(),np.array([0,30]), np.array([0,0]))
			dx_ = np.abs(proj_xyz[0][0]-proj_xyz[1][0])
			for indexi,i in enumerate(lons_x):
				ax.text(i,lons_y[indexi],r'$%d^{\circ}$'%i,transform = ccrs.Geodetic(),size = 20)
			for indexi,i in enumerate(lats_y):
				ax.text(lats_y_x[indexi]+dx_,lats_y_y[indexi],r'$%d^{\circ}$'%i,size = 20,ha = 'right',va = 'center')
			ax.set_global()
			ax.invert_xaxis()
			
			#n = n + 1
			return mplfig_to_npimage(fig)
		animation = VideoClip(make_frame, duration=duration)
		#animation.write_videofile(savevdir, fps=1/dt,codec = 'h264')
		animation.write_videofile(savevdir, fps=1/dt)
		animation.close()