def generate_tweet(tpf_fn=None, movie_length=120): """Generate a status message and animated gif. Parameters ---------- tpf_fn : str (optional) Path or url to a TPF file. If `None`, a random file will be downloaded. move_length : int (optional) Number of frames in the animation. Returns ------- (status, gif, tpf) : (str, str, `TargetPixelFile`) """ # Open the Target Pixel File if tpf_fn is None: # Get a random url db = KeplerArchiveCrawlerDB('tpf-urls/latest-tpf-urls.txt') tpf_fn = db.random_url() log.info('Opening {0}'.format(tpf_fn)) tpf = TargetPixelFile(tpf_fn, cache=False) log.info('KEPMAG = {0}, DIM = {1}'.format(tpf.hdulist[0].header['KEPMAG'], tpf.hdulist[1].header['TDIM5'])) # Don't tweet tiny strips if (tpf.hdulist[2].header['NAXIS1'] < 3) or (tpf.hdulist[2].header['NAXIS2'] < 3): raise Exception('Tiny strip') # Files contain occasional bad frames, so we make multiple attempts # with random starting points attempt_no = 0 while attempt_no < 7: attempt_no += 1 try: start = random.randint(0, tpf.no_frames - movie_length) try: kepmag = '🔆 Kp {:.1f}\n'.format(float(tpf.hdulist[0].header['KEPMAG'])) except Exception: kepmag = '' timestr = tpf.timestamp(start).split(' ')[0] campaign = tpf.hdulist[0].header['CAMPAIGN'] url = "https://archive.stsci.edu/k2/data_search/search.php?ktc_k2_id={}&action=Search".format(tpf.target.split(' ')[1]) status = ('New Kepler/K2 data were recently released!\n' '🔎 {}\n' '🗓 {} (C{})\n' '{}' '🔗 {}'.format(tpf.target, timestr, campaign, kepmag, url)) log.info(status) # Create the animated gif gif_fn = '/tmp/keplerbot.gif' tpf.save_movie(gif_fn, start=start, stop=start + movie_length, step=1, fps=8, min_percent=0., max_percent=93., ignore_bad_frames=True) return status, gif_fn, tpf except Exception as e: log.warning(e) raise Exception('Tweet failed')
class TestCore(): def setup_class(self): url = ('http://archive.stsci.edu/missions/k2/target_pixel_files/c1/' '201500000/08000/ktwo201508413-c01_lpd-targ.fits.gz') self.tpf = TargetPixelFile(url) def test_time(self): assert self.tpf.timestamp(0)[0:19] == '2014-05-30 16:09:28' assert self.tpf.timestamp(900)[0:19] == '2014-06-18 01:31:25' def test_flux(self): assert self.tpf.flux(500).shape == (15, 16) def test_create_figure(self): tmp = self.tpf.create_figure(1000) def test_save_gif(self, tmpdir): moviepath = tmpdir.join('test.gif') self.tpf.save_movie(output_fn=str(moviepath), start=10, stop=20, step=1) assert moviepath.check() def test_save_mp4(self, tmpdir): imageio.plugins.ffmpeg.download() moviepath = tmpdir.join('test.mp4') self.tpf.save_movie(output_fn=str(moviepath), start=10, stop=20, step=1) assert moviepath.check()
class TestCore(): def setup_class(self): url = ('http://archive.stsci.edu/missions/k2/target_pixel_files/c1/' '201500000/08000/ktwo201508413-c01_lpd-targ.fits.gz') self.tpf = TargetPixelFile(url) def test_time(self): assert self.tpf.timestamp(0)[0:19] == '2014-05-30 16:16:51' assert self.tpf.timestamp(900)[0:19] == '2014-06-18 01:36:52' def test_flux(self): assert self.tpf.flux(500).shape == (15, 16) def test_annotated_image(self): tmp = self.tpf.annotated_image(1000) def test_save_gif(self, tmpdir): moviepath = tmpdir.join('test.gif') self.tpf.save_movie(output_fn=str(moviepath), start=10, stop=20, step=1) assert moviepath.check() def test_save_mp4(self, tmpdir): moviepath = tmpdir.join('test.mp4') self.tpf.save_movie(output_fn=str(moviepath), start=10, stop=20, step=1) assert moviepath.check()
def setup_class(self): url = ('http://archive.stsci.edu/missions/k2/target_pixel_files/c1/' '201500000/08000/ktwo201508413-c01_lpd-targ.fits.gz') self.tpf = TargetPixelFile(url)