!wget http://blpd0.ssl.berkeley.edu/Voyager_data/Voyager1.single_coarse.fine_res.h5

"""
Explanation: Breakthrough Listen: Voyager 1 Observations
Voyager 1 is the most distant man-made object from Earth. Launched by NASA in 1977, it has travelled at fantastic speed (roughly 17,000 m/s), past the outer boundaries of our Solar System and into interstellar space (>12.5 billion miles from the Sun).
Remarkably, 39 years on, Voyager 1 is still sending telemetry data from the depths of interstellar space. This makes it a great systems test for the Breakthrough Listen signal processing pipeline.
In this tutorial, we load, read, and plot some Breakthrough Listen (BL) observations of Voyager 1. The data were taken using the Robert C. Byrd Greenbank Telescope in West Virginia.
About this tutorial
This tutorial introduces you to BL filterbank data. It is intended for intermediate to advanced users, who have experience with Python, Numpy and basic astronomy. You'll need to have Jupyter installed, along with a scientific Python installation (numpy, scipy, matplotlib, and astropy). You'll also need blimpy to read and manipulate the data.
About the data
We used the Greenbank X-band receiver (8.0-11.6 GHz) on December 30, 2015, to observe the known position of Voyager 1. The BL digital signal processing system saves digitized data in a 'raw' format, which we have converted into 'filterbank' format using our gpuspec code (see guppi2spectra.c in https://github.com/UCBerkeleySETI/gbt_seti/tree/master/src ). For advanced users who want to start from scratch, the specific command is:
time /gbt_seti/bin/gpuspec -i ./blc3_2bit_guppi_57386_VOYAGER1_0004.0000.raw \
    -B 2 -f 1032192 -t 15 -V -o /datax2/scratch/dprice/
For the purposes of this tutorial, we suggest that you download the 50 MB Voyager1.single_coarse.fine_res.h5 consisting of a subset of frequencies (corresponding to a single coarse channel) in the blimpy github distribution.
Filterbank format
The voyager data is stored in filterbank format, a simple binary file format that is detailed in the SIGPROC user guide. For this tutorial, we've provided a simple Python class to load and interpret the filterbank file into a numpy array. 
Let's get started!
If you haven't downloaded the data yet, please do so with this link, or this wget command:
End of explanation
"""


%matplotlib inline

import pylab as plt
from blimpy import Waterfall

"""
Explanation: Firstly, let's setup the notebook and import the Waterfall() class to read the data.
End of explanation
"""


file_path = 'Voyager1.single_coarse.fine_res.h5'
obs = Waterfall(file_path)

"""
Explanation: Now, let's read the observation data using Waterfall():
End of explanation
"""


obs.info()

"""
Explanation: We can get basic info about the file by reading the filterbank header. The info() method will do this:
End of explanation
"""


print(obs.header)
print(obs.data.shape)

"""
Explanation: The specifics are discussed in the detailed in the SIGPROC user guide. Briefly, astronomers use a Celestial coordinate system to specify the location of objects in outer space. The src_raj and src_dej specify the J2000 coordinates, in terms of Right Ascension and Declination (RA & DEC), toward which the telescope is pointing. tstart specifies the Julian Date of the observation, and fch1 and foff specify the frequency of the first and frequency increment of each data channel respectively, in MHz. 
We can also look directly at the header and data, which are stored as objects in the Waterfall() class:
End of explanation
"""


obs.plot_spectrum(logged=True)

"""
Explanation: The data has the shape (16, 1, 1048576), meaning that is has 16 time integrations, and 1048576 channels, over a bandwidth of 2.9 MHz. For the record, that's a lot of channels! The corresponding channel bandwidth is far narrower (Hz) than that required for most astrophysical observations (kHz).
Plotting the data
The Waterfall() class provides a plot_spectrum() function to plot the data:
End of explanation
"""


obs.plot_spectrum(f_start=8419.26, f_stop=8419.34)

"""
Explanation: What we see here is the power spectral density (or simply "spectrum") of the data contained in the filterbank file.  The logged=True tells the plotting script to display the signal on a decibel scale, which makes it easier to see high dynamic range. 
Apparent in the plot are the edges of the digital filters, and a big spike in power in the centre. This is known as the 'DC bin', and is an artifact caused by the use of the Fast Fourier Transform (FFT) to create spectra. 
Voyager is a very narrowband signal, but we can still see it to the left of this plot. If we zoom in on where Voyager's telemetry is known to be, we see some very narrowband signals:
End of explanation
"""


plt.figure(figsize=(8, 6))
plt.subplot(3,1,1)
obs.plot_spectrum(f_start=8419.274, f_stop=8419.275) # left sideband
plt.subplot(3,1,2)
obs.plot_spectrum(f_start=8419.296, f_stop=8419.298) # carrier
plt.subplot(3,1,3)
obs.plot_spectrum(f_start=8419.319, f_stop=8419.320) # right sideband
plt.tight_layout()

"""
Explanation: This is it: the telemetry signal from Voyager 1! What you see is the carrier (center) and two sidebands that carry the data. We can zoom in on these to see more features:
End of explanation
"""


obs.plot_waterfall(f_start=8419.296, f_stop=8419.298)

"""
Explanation: Because Voyager is not fixed on the surface of Earth, it has a relative motion with respect to the telescope, which causes its apparent frequency to drift. We can see this by plotting what's known as a 'Waterfall', or dynamic spectrum, which shows the spectrum as a function of time.
End of explanation
"""


