Signal Processing Notebook

This notebook demonstrates some DaViTPy tools for working with timeseries data. It includes filtering and visualization tools.

The DaViTPy signal is used by creating a VTSig object, which includes built-in methods for keeping track of all steps in a signal processing algorithm. As the signal is processed, all preceeding variants of the signal are saved within the object and a history is created. This makes it very easy to see what has been done with the data at any step along the way.

Written by N.A. Frissell, 9 July 2013.


In [1]:
#Import the modules we need.
import numpy as np
import scipy as sp
import matplotlib.pyplot as mp
import datetime
import copy

import pydarn
import utils
import pydarn.proc.signal as signal


/usr/local/lib/python2.7/dist-packages/mpl_toolkits/__init__.py:2: UserWarning: Module dap was already imported from None, but /usr/lib/python2.7/dist-packages is being added to sys.path
  __import__('pkg_resources').declare_namespace(__name__)

In [2]:
#This cell just sets some preferences to make the plots look nice.
font = {'family' : 'sans-serif',
        'weight' : 'bold',
        'size'   : 22}
matplotlib.rc('font', **font)

figure = {'figsize' : '15, 12',
          'dpi'     : 600}
matplotlib.rc('figure', **figure)

Signal Processing Example - GOES10 Data

We will start with some GOES10 magnetometer data since it is regularly sampled and easy to load from the provided sample data file.

Set Global Metadata


In [3]:
#Metadata is used to control aspects of signal processing and plotting throughout the routine, as well as a way
#of keeping track of different data properties and processing done to the data.

#Global metadata will control parameters pertaining to all signals being worked on.
#Here, we define the time period we want any processing we do to apply to.
signal.globalMetaData_add(validTimes = [datetime.datetime(2008,07,14,16,00), datetime.datetime(2008,07,15,00,00)])

Load Data


In [4]:
#Read data from file and sort out variables.
satfile = 'signal-sample_data-goes10.txt'
dataIn  = np.genfromtxt(satfile,comments='#',usecols=(0,1,2),dtype=[('date','S10'),('time','S5'),('btot','f8')])
timeStr = map(' '.join, zip(dataIn['date'],dataIn['time']))
datetimeObjList = [ datetime.datetime.strptime(x,'%Y-%m-%d %H:%M') for x in timeStr ]

Next we instantiate vt sig object. All you need is a list of Python datetime.datetime objects and the corresponding data.

  • The goes10 object will have a number of subobjects and methods.
  • goes10.raw is subobject that contains the raw data of datetimeObjList,data.
  • goes10.active should always point to the most processed version of the data, unless you manually set it to point to something else.
  • When you apply a processing algorthim to the goes10 object, it will use the "active" dataset, unless you specify otherwise.

In [5]:
#Instantiate vt sig object
goes10 = signal.sig(datetimeObjList,dataIn['btot'])

In [6]:
#Now lets look to see where the data is actually stored...
goes10.raw.dtv #Datetime vector (dtv)


Out[6]:
array([2008-07-14 00:00:00, 2008-07-14 00:01:00, 2008-07-14 00:02:00, ...,
       2008-07-14 23:57:00, 2008-07-14 23:58:00, 2008-07-14 23:59:00], dtype=object)

In [7]:
goes10.raw.data #Data vector


Out[7]:
array([  98.42,   98.92,  100.2 , ...,  103.  ,  103.07,  102.98])

Now we set the object metadata. This not only allows you to keep track of information about the date, but is also used by the plotting and signal processing routines.

goes10.metadata applies to all versions of the signal stored in the goes10 object. goes10.[dataSetName].metadata applies just to that data set. Anything set in goes10.[dataSetName].metadata will always override anything set in goes10.metadata. You can use the goes10.[dataSetName].getAllMetaData() method to see what metadata is actually being applied to a particular dataset.


In [8]:
goes10.metadata['title'] = 'GOES 10'
goes10.metadata['ylabel'] = 'B$_{tot}$ [nT]'
goes10.metadata['xlabel'] = 'Time [UT]\n14 July 2008'
#goes10.metadata['xmin'] = datetime.datetime(2008,07,14,18,00)
#goes10.metadata['xmax']  = datetime.datetime(2008,07,15,00,00)
#goes10.raw.metadata['ymin'] = 100.
#goes10.raw.metadata['ymax'] = 125.

In [9]:
#Now let's plot our raw data.  This plot shows all of the GOES10 data which has been loaded into the object.
#The white region is the region defined by the validTimes in the global metadata.
goes10.raw.plot()


Process Data

Let's run some processing now. Remember, each step does not discard the data from the previous steps.


In [10]:
#Apply a linear detrend through the data.
signal.detrend(goes10.raw)

In [11]:
#Here is the result of the detrending.  Note that only the validTimes period is shown now.  This is because
#the detrending was applied only to the validTimes period.
goes10.detrended.plot()



In [12]:
#Global metadata can also be used to define characteristics of a filter.
signal.globalMetaData_add(filter_numtaps = 101) #This is the length of the filter.  A larger number is a better filter, but slower computationally.
signal.globalMetaData_add(filter_cutoff_high  = 0.004) #Frequencies are in Hz.
signal.globalMetaData_add(filter_cutoff_low  = 0.001)

In [13]:
#Now filter the data with filter we defined in the global metadata.
goes10Filt = signal.filter(goes10)

In [14]:
#Let's look at the current history of our signal object.
goes10.active.history


Out[14]:
{datetime.datetime(2013, 7, 9, 10, 57, 32, 13890): 'Signal Object Created',
 datetime.datetime(2013, 7, 9, 10, 57, 39, 927455): 'Truncate: 2008JUL14 16:00 UT - 2008JUL15 00:00 UT',
 datetime.datetime(2013, 7, 9, 10, 57, 40, 207134): 'Linear detrend (scipy.signal.detrend)',
 datetime.datetime(2013, 7, 9, 10, 57, 42, 588337): 'Filter: blackman, Nyquist: 0.00833333333333 Hz, Cuttoff: [0.001, 0.004] Hz'}

In [15]:
#We can also see all of the metadata set for a particular dataObject.
goes10.active.getAllMetaData()


Out[15]:
{'fft_xlabel': 'Frequency [Hz]',
 'fft_ylabel': 'FFT Spectrum Magnitude',
 'filter_cutoff_high': 0.004,
 'filter_cutoff_low': 0.001,
 'filter_numtaps': 101,
 'title': 'Filtered Detrended  GOES 10',
 'validTimes': [datetime.datetime(2008, 7, 14, 16, 50),
  datetime.datetime(2008, 7, 14, 23, 10)],
 'xlabel': 'Time [UT]\n14 July 2008',
 'ylabel': 'B$_{tot}$ [nT]'}

In [16]:
#Let's see the transfer function of the filter we used.
goes10Filt.plotTransferFunction()



In [17]:
#And now the filtered result.  Because the filter causes a delay in the signal and doesn't get to the end of
#the signal, part of the filtered signal should be discard.  Appropriate new validTimes have been set, and the
#gray bars at the beginning and end mark the part of the signal to be discarded.

#Note that the validTimes here only apply to this filtered signal (goes10.filtered.metadata['validTimes']), and
#not the global metadata.  When multiple validTimes are in place for a particular object, the most restrictive set of
#validTimes is always used.
goes10.filtered.plot()



In [18]:
#We can also look at the spectrum of any signal that is regularly sampled in time.
goes10.filtered.plotfft()



In [19]:
#Once you plot the FFT (or invoke the fft() method), you now have access to the spectrum
goes10.filtered.freqVec #Here are the frequency bins in Hz.


Out[19]:
array([ -8.33333333e-03,  -8.28947368e-03,  -8.24561404e-03,
        -8.20175439e-03,  -8.15789474e-03,  -8.11403509e-03,
        -8.07017544e-03,  -8.02631579e-03,  -7.98245614e-03,
        -7.93859649e-03,  -7.89473684e-03,  -7.85087719e-03,
        -7.80701754e-03,  -7.76315789e-03,  -7.71929825e-03,
        -7.67543860e-03,  -7.63157895e-03,  -7.58771930e-03,
        -7.54385965e-03,  -7.50000000e-03,  -7.45614035e-03,
        -7.41228070e-03,  -7.36842105e-03,  -7.32456140e-03,
        -7.28070175e-03,  -7.23684211e-03,  -7.19298246e-03,
        -7.14912281e-03,  -7.10526316e-03,  -7.06140351e-03,
        -7.01754386e-03,  -6.97368421e-03,  -6.92982456e-03,
        -6.88596491e-03,  -6.84210526e-03,  -6.79824561e-03,
        -6.75438596e-03,  -6.71052632e-03,  -6.66666667e-03,
        -6.62280702e-03,  -6.57894737e-03,  -6.53508772e-03,
        -6.49122807e-03,  -6.44736842e-03,  -6.40350877e-03,
        -6.35964912e-03,  -6.31578947e-03,  -6.27192982e-03,
        -6.22807018e-03,  -6.18421053e-03,  -6.14035088e-03,
        -6.09649123e-03,  -6.05263158e-03,  -6.00877193e-03,
        -5.96491228e-03,  -5.92105263e-03,  -5.87719298e-03,
        -5.83333333e-03,  -5.78947368e-03,  -5.74561404e-03,
        -5.70175439e-03,  -5.65789474e-03,  -5.61403509e-03,
        -5.57017544e-03,  -5.52631579e-03,  -5.48245614e-03,
        -5.43859649e-03,  -5.39473684e-03,  -5.35087719e-03,
        -5.30701754e-03,  -5.26315789e-03,  -5.21929825e-03,
        -5.17543860e-03,  -5.13157895e-03,  -5.08771930e-03,
        -5.04385965e-03,  -5.00000000e-03,  -4.95614035e-03,
        -4.91228070e-03,  -4.86842105e-03,  -4.82456140e-03,
        -4.78070175e-03,  -4.73684211e-03,  -4.69298246e-03,
        -4.64912281e-03,  -4.60526316e-03,  -4.56140351e-03,
        -4.51754386e-03,  -4.47368421e-03,  -4.42982456e-03,
        -4.38596491e-03,  -4.34210526e-03,  -4.29824561e-03,
        -4.25438596e-03,  -4.21052632e-03,  -4.16666667e-03,
        -4.12280702e-03,  -4.07894737e-03,  -4.03508772e-03,
        -3.99122807e-03,  -3.94736842e-03,  -3.90350877e-03,
        -3.85964912e-03,  -3.81578947e-03,  -3.77192982e-03,
        -3.72807018e-03,  -3.68421053e-03,  -3.64035088e-03,
        -3.59649123e-03,  -3.55263158e-03,  -3.50877193e-03,
        -3.46491228e-03,  -3.42105263e-03,  -3.37719298e-03,
        -3.33333333e-03,  -3.28947368e-03,  -3.24561404e-03,
        -3.20175439e-03,  -3.15789474e-03,  -3.11403509e-03,
        -3.07017544e-03,  -3.02631579e-03,  -2.98245614e-03,
        -2.93859649e-03,  -2.89473684e-03,  -2.85087719e-03,
        -2.80701754e-03,  -2.76315789e-03,  -2.71929825e-03,
        -2.67543860e-03,  -2.63157895e-03,  -2.58771930e-03,
        -2.54385965e-03,  -2.50000000e-03,  -2.45614035e-03,
        -2.41228070e-03,  -2.36842105e-03,  -2.32456140e-03,
        -2.28070175e-03,  -2.23684211e-03,  -2.19298246e-03,
        -2.14912281e-03,  -2.10526316e-03,  -2.06140351e-03,
        -2.01754386e-03,  -1.97368421e-03,  -1.92982456e-03,
        -1.88596491e-03,  -1.84210526e-03,  -1.79824561e-03,
        -1.75438596e-03,  -1.71052632e-03,  -1.66666667e-03,
        -1.62280702e-03,  -1.57894737e-03,  -1.53508772e-03,
        -1.49122807e-03,  -1.44736842e-03,  -1.40350877e-03,
        -1.35964912e-03,  -1.31578947e-03,  -1.27192982e-03,
        -1.22807018e-03,  -1.18421053e-03,  -1.14035088e-03,
        -1.09649123e-03,  -1.05263158e-03,  -1.00877193e-03,
        -9.64912281e-04,  -9.21052632e-04,  -8.77192982e-04,
        -8.33333333e-04,  -7.89473684e-04,  -7.45614035e-04,
        -7.01754386e-04,  -6.57894737e-04,  -6.14035088e-04,
        -5.70175439e-04,  -5.26315789e-04,  -4.82456140e-04,
        -4.38596491e-04,  -3.94736842e-04,  -3.50877193e-04,
        -3.07017544e-04,  -2.63157895e-04,  -2.19298246e-04,
        -1.75438596e-04,  -1.31578947e-04,  -8.77192982e-05,
        -4.38596491e-05,   0.00000000e+00,   4.38596491e-05,
         8.77192982e-05,   1.31578947e-04,   1.75438596e-04,
         2.19298246e-04,   2.63157895e-04,   3.07017544e-04,
         3.50877193e-04,   3.94736842e-04,   4.38596491e-04,
         4.82456140e-04,   5.26315789e-04,   5.70175439e-04,
         6.14035088e-04,   6.57894737e-04,   7.01754386e-04,
         7.45614035e-04,   7.89473684e-04,   8.33333333e-04,
         8.77192982e-04,   9.21052632e-04,   9.64912281e-04,
         1.00877193e-03,   1.05263158e-03,   1.09649123e-03,
         1.14035088e-03,   1.18421053e-03,   1.22807018e-03,
         1.27192982e-03,   1.31578947e-03,   1.35964912e-03,
         1.40350877e-03,   1.44736842e-03,   1.49122807e-03,
         1.53508772e-03,   1.57894737e-03,   1.62280702e-03,
         1.66666667e-03,   1.71052632e-03,   1.75438596e-03,
         1.79824561e-03,   1.84210526e-03,   1.88596491e-03,
         1.92982456e-03,   1.97368421e-03,   2.01754386e-03,
         2.06140351e-03,   2.10526316e-03,   2.14912281e-03,
         2.19298246e-03,   2.23684211e-03,   2.28070175e-03,
         2.32456140e-03,   2.36842105e-03,   2.41228070e-03,
         2.45614035e-03,   2.50000000e-03,   2.54385965e-03,
         2.58771930e-03,   2.63157895e-03,   2.67543860e-03,
         2.71929825e-03,   2.76315789e-03,   2.80701754e-03,
         2.85087719e-03,   2.89473684e-03,   2.93859649e-03,
         2.98245614e-03,   3.02631579e-03,   3.07017544e-03,
         3.11403509e-03,   3.15789474e-03,   3.20175439e-03,
         3.24561404e-03,   3.28947368e-03,   3.33333333e-03,
         3.37719298e-03,   3.42105263e-03,   3.46491228e-03,
         3.50877193e-03,   3.55263158e-03,   3.59649123e-03,
         3.64035088e-03,   3.68421053e-03,   3.72807018e-03,
         3.77192982e-03,   3.81578947e-03,   3.85964912e-03,
         3.90350877e-03,   3.94736842e-03,   3.99122807e-03,
         4.03508772e-03,   4.07894737e-03,   4.12280702e-03,
         4.16666667e-03,   4.21052632e-03,   4.25438596e-03,
         4.29824561e-03,   4.34210526e-03,   4.38596491e-03,
         4.42982456e-03,   4.47368421e-03,   4.51754386e-03,
         4.56140351e-03,   4.60526316e-03,   4.64912281e-03,
         4.69298246e-03,   4.73684211e-03,   4.78070175e-03,
         4.82456140e-03,   4.86842105e-03,   4.91228070e-03,
         4.95614035e-03,   5.00000000e-03,   5.04385965e-03,
         5.08771930e-03,   5.13157895e-03,   5.17543860e-03,
         5.21929825e-03,   5.26315789e-03,   5.30701754e-03,
         5.35087719e-03,   5.39473684e-03,   5.43859649e-03,
         5.48245614e-03,   5.52631579e-03,   5.57017544e-03,
         5.61403509e-03,   5.65789474e-03,   5.70175439e-03,
         5.74561404e-03,   5.78947368e-03,   5.83333333e-03,
         5.87719298e-03,   5.92105263e-03,   5.96491228e-03,
         6.00877193e-03,   6.05263158e-03,   6.09649123e-03,
         6.14035088e-03,   6.18421053e-03,   6.22807018e-03,
         6.27192982e-03,   6.31578947e-03,   6.35964912e-03,
         6.40350877e-03,   6.44736842e-03,   6.49122807e-03,
         6.53508772e-03,   6.57894737e-03,   6.62280702e-03,
         6.66666667e-03,   6.71052632e-03,   6.75438596e-03,
         6.79824561e-03,   6.84210526e-03,   6.88596491e-03,
         6.92982456e-03,   6.97368421e-03,   7.01754386e-03,
         7.06140351e-03,   7.10526316e-03,   7.14912281e-03,
         7.19298246e-03,   7.23684211e-03,   7.28070175e-03,
         7.32456140e-03,   7.36842105e-03,   7.41228070e-03,
         7.45614035e-03,   7.50000000e-03,   7.54385965e-03,
         7.58771930e-03,   7.63157895e-03,   7.67543860e-03,
         7.71929825e-03,   7.76315789e-03,   7.80701754e-03,
         7.85087719e-03,   7.89473684e-03,   7.93859649e-03,
         7.98245614e-03,   8.02631579e-03,   8.07017544e-03,
         8.11403509e-03,   8.15789474e-03,   8.20175439e-03,
         8.24561404e-03,   8.28947368e-03,   8.33333333e-03])

In [20]:
goes10.filtered.spectrum #Here is the actual spectrum


Out[20]:
array([ -7.76702755e-06 +2.54847505e-06j,
        -5.90275347e-06 -2.81282607e-06j,
        -5.36560407e-06 +2.65659079e-06j,
        -5.19584919e-06 -1.97506808e-06j,
        -8.42210569e-06 -6.46565482e-07j,
        -5.27510107e-06 +1.83602431e-06j,
        -7.89133924e-06 -3.79907448e-06j,
        -8.08425183e-06 +3.93443713e-06j,
        -4.81798088e-06 -2.09931369e-06j,
        -4.86121340e-06 -9.54500879e-07j,
        -8.67840453e-06 -2.31863988e-07j,
        -5.73122042e-06 -2.67375314e-07j,
        -1.04302766e-05 +8.14176940e-07j,
        -5.59987738e-06 -2.16149663e-06j,
        -3.01864589e-06 +6.63031932e-07j,
        -1.21224738e-05 -2.08722704e-06j,
        -5.98831825e-06 +5.68979104e-08j,
        -7.99450758e-06 -1.38322925e-06j,
        -6.81068830e-06 +2.69142231e-06j,
        -8.57678859e-06 -5.74709467e-06j,
        -8.19289545e-06 +1.20282212e-06j,
        -6.67270743e-06 -4.24091201e-07j,
        -1.13936169e-05 +4.83365573e-07j,
        -6.73647092e-06 -1.31694120e-06j,
        -7.58844379e-06 -1.59116282e-06j,
        -9.59496292e-06 -3.82771106e-06j,
        -1.23309466e-05 +5.37049885e-07j,
        -1.04289003e-05 +1.92700755e-06j,
        -7.82157115e-06 -2.39249024e-06j,
        -1.10025939e-05 -3.53233535e-06j,
        -7.80477797e-06 +2.32915834e-06j,
        -1.11347241e-05 -3.18710309e-06j,
        -1.16658098e-05 -5.60606289e-06j,
        -1.48288561e-05 +9.09436957e-06j,
        -1.04196941e-05 -1.14899661e-05j,
        -1.40247814e-05 +2.55239548e-06j,
        -1.31567743e-05 -1.97939652e-06j,
        -1.21843013e-05 -3.07869793e-06j,
        -9.44982409e-06 +4.78220879e-06j,
        -2.23825326e-05 -8.71373871e-06j,
        -1.30694042e-05 +3.95017693e-06j,
        -7.13793845e-06 -6.25401460e-06j,
        -3.15584789e-05 -1.25204566e-06j,
        -1.01343270e-05 +4.37854456e-06j,
        -5.60815196e-06 -8.49306141e-06j,
        -4.24847161e-05 -1.10626724e-05j,
        -7.11835819e-06 +9.83881871e-06j,
        -2.63795508e-05 -4.79257962e-06j,
        -1.86447277e-05 -5.10439940e-06j,
        -1.36947555e-05 -1.26879614e-05j,
        -4.13003648e-05 +1.17840896e-05j,
        -7.22641801e-06 -7.72355301e-07j,
        -4.31391772e-05 -3.69095771e-07j,
        -1.45438528e-05 -2.02580430e-05j,
        -4.24542324e-05 -6.41217960e-06j,
        -3.24988411e-05 +1.76408111e-05j,
         8.23810634e-06 -2.26502538e-05j,
        -9.09149452e-05 +7.74958705e-06j,
        -1.81228066e-05 -1.99234533e-05j,
        -2.86321929e-05 +2.22628789e-05j,
        -4.29024563e-05 -4.92141885e-05j,
        -6.10991787e-05 +6.56421285e-06j,
        -4.44685977e-05 -1.95384479e-05j,
        -3.21755185e-05 +3.11369692e-05j,
        -1.00000255e-04 -1.38185708e-05j,
         4.92729679e-06 -1.53747788e-05j,
        -1.11517429e-04 -4.14232592e-05j,
        -4.51608747e-05 +8.65043326e-06j,
        -4.73975748e-05 +1.52691441e-05j,
        -8.80130073e-05 -6.66323553e-05j,
        -1.08666029e-04 -1.31060001e-05j,
        -2.07967394e-05 +3.37882189e-05j,
        -1.49426825e-04 -1.16467187e-04j,
        -1.39574424e-04 +3.90201607e-05j,
        -8.78580039e-05 -7.99136551e-05j,
        -1.04858532e-04 -1.62246349e-05j,
        -1.57631774e-04 +1.24083034e-04j,
        -1.59058437e-04 -2.05257673e-04j,
        -1.54753390e-04 -2.98971821e-04j,
        -5.71055708e-05 +3.72391615e-04j,
        -3.78523230e-04 -2.55495443e-04j,
        -1.11063161e-04 -1.85465871e-04j,
        -1.17981186e-04 +1.16219404e-04j,
        -8.93838917e-04 -2.48555446e-04j,
        -8.02778409e-06 -2.63625650e-04j,
        -2.74266928e-04 -1.69997549e-05j,
        -6.43710567e-04 -1.65985299e-04j,
        -1.65541648e-04 -1.16301874e-03j,
        -1.89493477e-03 -2.50305671e-03j,
         1.12635249e-02 -5.42512425e-03j,
         1.49538375e-02 +1.24837911e-02j,
        -3.79581605e-02 -2.88322382e-02j,
        -1.11486973e-01 +2.53668946e-02j,
         2.12666299e-01 -8.75151033e-02j,
        -4.98501809e-01 +3.02552359e-01j,
         6.44107649e-01 +2.25325679e-01j,
        -7.28525915e-01 -8.85213852e-01j,
         9.75106487e-01 +1.39898965e-01j,
         4.63760268e-01 +2.88487151e-01j,
        -1.67929667e-01 +2.66105952e-01j,
        -1.68859896e+00 -3.55450024e-01j,
        -5.08390008e-01 -6.81704213e-01j,
         2.88720560e+00 +1.06044852e+00j,
        -1.94868312e+00 +9.65123064e-01j,
        -8.73606429e-01 -7.83142251e-01j,
        -3.51227389e-01 -2.78034812e+00j,
         3.14364643e+00 +2.27792592e+00j,
         3.30284578e+00 +5.59112295e-01j,
        -6.97497928e+00 +1.64394599e+00j,
         6.16718840e-02 -3.28879510e+00j,
         3.64038131e-01 -3.39572622e-01j,
         7.00670034e+00 -3.01260262e-01j,
        -5.82148954e+00 +4.47028309e+00j,
         3.04142504e+00 +2.94016555e+00j,
        -1.04897655e+01 -9.26777456e+00j,
         9.03655320e+00 +1.77598090e-01j,
        -8.04345998e-01 +2.36627978e+00j,
         4.83386410e+00 +3.49999216e+00j,
        -8.08259553e+00 +1.98136365e+00j,
         5.45978265e+00 -5.65372134e+00j,
        -4.59614818e+00 +9.68901660e-01j,
         4.33314385e+00 -5.53716102e+00j,
        -4.24919340e+00 +7.45782777e+00j,
         6.90518593e-01 -2.19931791e+00j,
         3.62090954e+00 +3.70554226e+00j,
        -4.40276760e+00 -3.04757449e+00j,
         2.90453795e+00 +4.30623301e+00j,
         2.41064260e-01 -5.42741716e+00j,
         8.05255709e-01 -2.14580808e+00j,
         1.34513224e+00 +5.68419162e-01j,
        -2.52259730e+00 +5.12332037e+00j,
        -1.23936683e+00 -3.77121202e+00j,
        -4.86156074e+00 +3.51040811e-01j,
         5.40011070e+00 +1.54672348e+00j,
         1.78426411e+00 -3.74829813e+00j,
        -3.51162971e+00 +1.02910654e+01j,
         1.97137814e+00 -6.49592764e+00j,
        -9.75466559e-01 +2.11270159e+00j,
         1.36661157e+00 -5.74512402e+00j,
         7.49409614e+00 +1.22044928e+00j,
        -1.07942388e+01 +1.20650826e+00j,
         6.17603700e+00 +7.13059889e-01j,
        -1.23125587e+01 -1.32652248e+00j,
         9.82261920e+00 -1.71073048e+00j,
        -5.03855203e+00 +3.10975678e+00j,
         1.32638192e+01 -1.43349344e+00j,
        -1.06715537e+01 +3.39586337e+00j,
        -2.67821860e+00 +2.15920257e+00j,
         3.62880760e+00 +4.27433731e+00j,
         4.45167350e+00 -2.01776003e+01j,
         3.43079663e+00 +9.38241663e+00j,
        -7.15457436e+00 +4.15715663e+00j,
        -1.14933231e+00 +2.07014895e+00j,
        -7.97580899e+00 -8.70717373e+00j,
         8.68883337e+00 -3.33466122e+00j,
        -4.39661318e+00 +9.24849298e+00j,
         1.30591313e+01 +6.12545077e+00j,
        -1.00280314e+01 -9.02297923e-01j,
         5.38035310e+00 -7.97237836e+00j,
        -6.99411834e+00 -3.11869607e+00j,
         1.29067812e+01 -5.46324294e+00j,
        -7.48654831e+00 +1.97427577e+01j,
        -5.48011569e+00 -9.23434471e+00j,
        -5.88856702e+00 -1.91868480e+00j,
         5.92711819e+00 -1.27331210e+00j,
         7.18212525e+00 +7.80232484e+00j,
        -5.08317766e+00 -5.59253508e+00j,
         2.33159158e+00 +7.47975626e-01j,
         1.35111799e+00 -2.12405757e+00j,
        -5.28464768e-01 -5.74814661e-02j,
        -1.66749040e+00 +2.31937390e+00j,
        -5.59627736e-01 -3.20590186e-01j,
        -6.16408629e-01 +1.21676509e+00j,
         5.75812554e-01 -1.63883210e+00j,
         2.22873817e-01 +5.26988970e-01j,
         1.64198095e-01 -1.35279201e-01j,
        -6.07452349e-02 +1.24697877e-03j,
         6.23779880e-03 -1.23297146e-02j,
         7.32115107e-03 -3.32223155e-03j,
        -4.75588016e-03 -3.81838666e-03j,
         1.20964860e-02 -5.62089524e-03j,
         3.99169356e-04 +3.87113859e-04j,
         2.69638801e-03 -2.00592645e-03j,
         4.90786902e-03 +3.90088155e-04j,
         3.42785821e-03 -4.08407738e-03j,
         3.87643634e-03 +9.26392651e-03j,
        -8.78526185e-03 +7.98674245e-04j,
        -1.90074163e-03 -1.55843919e-02j,
         1.00735506e-02 +1.03666044e-02j,
         5.22900025e-03 -6.84998097e-03j,
        -5.05528860e-03 +0.00000000e+00j,
         5.22900025e-03 +6.84998097e-03j,
         1.00735506e-02 -1.03666044e-02j,
        -1.90074163e-03 +1.55843919e-02j,
        -8.78526185e-03 -7.98674245e-04j,
         3.87643634e-03 -9.26392651e-03j,
         3.42785821e-03 +4.08407738e-03j,
         4.90786902e-03 -3.90088155e-04j,
         2.69638801e-03 +2.00592645e-03j,
         3.99169356e-04 -3.87113859e-04j,
         1.20964860e-02 +5.62089524e-03j,
        -4.75588016e-03 +3.81838666e-03j,
         7.32115107e-03 +3.32223155e-03j,
         6.23779880e-03 +1.23297146e-02j,
        -6.07452349e-02 -1.24697877e-03j,
         1.64198095e-01 +1.35279201e-01j,
         2.22873817e-01 -5.26988970e-01j,
         5.75812554e-01 +1.63883210e+00j,
        -6.16408629e-01 -1.21676509e+00j,
        -5.59627736e-01 +3.20590186e-01j,
        -1.66749040e+00 -2.31937390e+00j,
        -5.28464768e-01 +5.74814661e-02j,
         1.35111799e+00 +2.12405757e+00j,
         2.33159158e+00 -7.47975626e-01j,
        -5.08317766e+00 +5.59253508e+00j,
         7.18212525e+00 -7.80232484e+00j,
         5.92711819e+00 +1.27331210e+00j,
        -5.88856702e+00 +1.91868480e+00j,
        -5.48011569e+00 +9.23434471e+00j,
        -7.48654831e+00 -1.97427577e+01j,
         1.29067812e+01 +5.46324294e+00j,
        -6.99411834e+00 +3.11869607e+00j,
         5.38035310e+00 +7.97237836e+00j,
        -1.00280314e+01 +9.02297923e-01j,
         1.30591313e+01 -6.12545077e+00j,
        -4.39661318e+00 -9.24849298e+00j,
         8.68883337e+00 +3.33466122e+00j,
        -7.97580899e+00 +8.70717373e+00j,
        -1.14933231e+00 -2.07014895e+00j,
        -7.15457436e+00 -4.15715663e+00j,
         3.43079663e+00 -9.38241663e+00j,
         4.45167350e+00 +2.01776003e+01j,
         3.62880760e+00 -4.27433731e+00j,
        -2.67821860e+00 -2.15920257e+00j,
        -1.06715537e+01 -3.39586337e+00j,
         1.32638192e+01 +1.43349344e+00j,
        -5.03855203e+00 -3.10975678e+00j,
         9.82261920e+00 +1.71073048e+00j,
        -1.23125587e+01 +1.32652248e+00j,
         6.17603700e+00 -7.13059889e-01j,
        -1.07942388e+01 -1.20650826e+00j,
         7.49409614e+00 -1.22044928e+00j,
         1.36661157e+00 +5.74512402e+00j,
        -9.75466559e-01 -2.11270159e+00j,
         1.97137814e+00 +6.49592764e+00j,
        -3.51162971e+00 -1.02910654e+01j,
         1.78426411e+00 +3.74829813e+00j,
         5.40011070e+00 -1.54672348e+00j,
        -4.86156074e+00 -3.51040811e-01j,
        -1.23936683e+00 +3.77121202e+00j,
        -2.52259730e+00 -5.12332037e+00j,
         1.34513224e+00 -5.68419162e-01j,
         8.05255709e-01 +2.14580808e+00j,
         2.41064260e-01 +5.42741716e+00j,
         2.90453795e+00 -4.30623301e+00j,
        -4.40276760e+00 +3.04757449e+00j,
         3.62090954e+00 -3.70554226e+00j,
         6.90518593e-01 +2.19931791e+00j,
        -4.24919340e+00 -7.45782777e+00j,
         4.33314385e+00 +5.53716102e+00j,
        -4.59614818e+00 -9.68901660e-01j,
         5.45978265e+00 +5.65372134e+00j,
        -8.08259553e+00 -1.98136365e+00j,
         4.83386410e+00 -3.49999216e+00j,
        -8.04345998e-01 -2.36627978e+00j,
         9.03655320e+00 -1.77598090e-01j,
        -1.04897655e+01 +9.26777456e+00j,
         3.04142504e+00 -2.94016555e+00j,
        -5.82148954e+00 -4.47028309e+00j,
         7.00670034e+00 +3.01260262e-01j,
         3.64038131e-01 +3.39572622e-01j,
         6.16718840e-02 +3.28879510e+00j,
        -6.97497928e+00 -1.64394599e+00j,
         3.30284578e+00 -5.59112295e-01j,
         3.14364643e+00 -2.27792592e+00j,
        -3.51227389e-01 +2.78034812e+00j,
        -8.73606429e-01 +7.83142251e-01j,
        -1.94868312e+00 -9.65123064e-01j,
         2.88720560e+00 -1.06044852e+00j,
        -5.08390008e-01 +6.81704213e-01j,
        -1.68859896e+00 +3.55450024e-01j,
        -1.67929667e-01 -2.66105952e-01j,
         4.63760268e-01 -2.88487151e-01j,
         9.75106487e-01 -1.39898965e-01j,
        -7.28525915e-01 +8.85213852e-01j,
         6.44107649e-01 -2.25325679e-01j,
        -4.98501809e-01 -3.02552359e-01j,
         2.12666299e-01 +8.75151033e-02j,
        -1.11486973e-01 -2.53668946e-02j,
        -3.79581605e-02 +2.88322382e-02j,
         1.49538375e-02 -1.24837911e-02j,
         1.12635249e-02 +5.42512425e-03j,
        -1.89493477e-03 +2.50305671e-03j,
        -1.65541648e-04 +1.16301874e-03j,
        -6.43710567e-04 +1.65985299e-04j,
        -2.74266928e-04 +1.69997549e-05j,
        -8.02778409e-06 +2.63625650e-04j,
        -8.93838917e-04 +2.48555446e-04j,
        -1.17981186e-04 -1.16219404e-04j,
        -1.11063161e-04 +1.85465871e-04j,
        -3.78523230e-04 +2.55495443e-04j,
        -5.71055708e-05 -3.72391615e-04j,
        -1.54753390e-04 +2.98971821e-04j,
        -1.59058437e-04 +2.05257673e-04j,
        -1.57631774e-04 -1.24083034e-04j,
        -1.04858532e-04 +1.62246349e-05j,
        -8.78580039e-05 +7.99136551e-05j,
        -1.39574424e-04 -3.90201607e-05j,
        -1.49426825e-04 +1.16467187e-04j,
        -2.07967394e-05 -3.37882189e-05j,
        -1.08666029e-04 +1.31060001e-05j,
        -8.80130073e-05 +6.66323553e-05j,
        -4.73975748e-05 -1.52691441e-05j,
        -4.51608747e-05 -8.65043326e-06j,
        -1.11517429e-04 +4.14232592e-05j,
         4.92729679e-06 +1.53747788e-05j,
        -1.00000255e-04 +1.38185708e-05j,
        -3.21755185e-05 -3.11369692e-05j,
        -4.44685977e-05 +1.95384479e-05j,
        -6.10991787e-05 -6.56421285e-06j,
        -4.29024563e-05 +4.92141885e-05j,
        -2.86321929e-05 -2.22628789e-05j,
        -1.81228066e-05 +1.99234533e-05j,
        -9.09149452e-05 -7.74958705e-06j,
         8.23810634e-06 +2.26502538e-05j,
        -3.24988411e-05 -1.76408111e-05j,
        -4.24542324e-05 +6.41217960e-06j,
        -1.45438528e-05 +2.02580430e-05j,
        -4.31391772e-05 +3.69095771e-07j,
        -7.22641801e-06 +7.72355301e-07j,
        -4.13003648e-05 -1.17840896e-05j,
        -1.36947555e-05 +1.26879614e-05j,
        -1.86447277e-05 +5.10439940e-06j,
        -2.63795508e-05 +4.79257962e-06j,
        -7.11835819e-06 -9.83881871e-06j,
        -4.24847161e-05 +1.10626724e-05j,
        -5.60815196e-06 +8.49306141e-06j,
        -1.01343270e-05 -4.37854456e-06j,
        -3.15584789e-05 +1.25204566e-06j,
        -7.13793845e-06 +6.25401460e-06j,
        -1.30694042e-05 -3.95017693e-06j,
        -2.23825326e-05 +8.71373871e-06j,
        -9.44982409e-06 -4.78220879e-06j,
        -1.21843013e-05 +3.07869793e-06j,
        -1.31567743e-05 +1.97939652e-06j,
        -1.40247814e-05 -2.55239548e-06j,
        -1.04196941e-05 +1.14899661e-05j,
        -1.48288561e-05 -9.09436957e-06j,
        -1.16658098e-05 +5.60606289e-06j,
        -1.11347241e-05 +3.18710309e-06j,
        -7.80477797e-06 -2.32915834e-06j,
        -1.10025939e-05 +3.53233535e-06j,
        -7.82157115e-06 +2.39249024e-06j,
        -1.04289003e-05 -1.92700755e-06j,
        -1.23309466e-05 -5.37049885e-07j,
        -9.59496292e-06 +3.82771106e-06j,
        -7.58844379e-06 +1.59116282e-06j,
        -6.73647092e-06 +1.31694120e-06j,
        -1.13936169e-05 -4.83365573e-07j,
        -6.67270743e-06 +4.24091201e-07j,
        -8.19289545e-06 -1.20282212e-06j,
        -8.57678859e-06 +5.74709467e-06j,
        -6.81068830e-06 -2.69142231e-06j,
        -7.99450758e-06 +1.38322925e-06j,
        -5.98831825e-06 -5.68979104e-08j,
        -1.21224738e-05 +2.08722704e-06j,
        -3.01864589e-06 -6.63031932e-07j,
        -5.59987738e-06 +2.16149663e-06j,
        -1.04302766e-05 -8.14176940e-07j,
        -5.73122042e-06 +2.67375314e-07j,
        -8.67840453e-06 +2.31863988e-07j,
        -4.86121340e-06 +9.54500879e-07j,
        -4.81798088e-06 +2.09931369e-06j,
        -8.08425183e-06 -3.93443713e-06j,
        -7.89133924e-06 +3.79907448e-06j,
        -5.27510107e-06 -1.83602431e-06j,
        -8.42210569e-06 +6.46565482e-07j,
        -5.19584919e-06 +1.97506808e-06j,
        -5.36560407e-06 -2.65659079e-06j,
        -5.90275347e-06 +2.81282607e-06j,  -7.76702755e-06 -2.54847505e-06j])

In [21]:
#We can also compare the spectra of two different signals.
a = signal.oplotfft([goes10.detrended,goes10.filtered])


Signal Processing Example - SuperDARN Rankin Inlet Data

Now lets try looking at some SuperDARN data from the same period. SuperDARN can be easily accessed through Virginia Tech's servers, as shown below. Working with this data is a little trickier because it is not necessarily regularly sampled.

Load Data


In [22]:
#Set some parameters regarding the event of interest.  Let's use a time series from beam 7, rangegate 0.
sTime = datetime.datetime(2008,7,14)
eTime = datetime.datetime(2008,7,14,23)
fileType = 'fitacf'
radar = 'rkn'
beam = 7
gate = 0

In [23]:
#Use this cell to load in data straight from DaViTPy.  See the radDataRead notebook for
#additional inspiration on reading SuperDARN data.
myPtr = pydarn.sdio.radDataOpen(sTime,radar,eTime=eTime,bmnum=beam,fileType=fileType)

datetimeObjList  = []
data = []
time_ii = copy.copy(sTime)
myBeam = 1
while time_ii < eTime:
    myBeam = pydarn.sdio.radDataReadRec(myPtr)
    if myBeam == None:
        break
    time_ii = myBeam.time
    if not gate in myBeam.fit.slist:
        continue
    
    datetimeObjList.append(time_ii)
    inx = int(np.where(np.array(myBeam.fit.slist) == gate)[0])
    data.append(myBeam.fit.v[inx])


Found cached file: /tmp/sd/20080713.220100.20080714.230000.rkn.fitacf

reached end of data

In [24]:
#Instantiate vt sig object.
rkn = signal.sig(datetimeObjList,data)

In [25]:
#Set Rankin Inlet Radar Metadata.
rkn.metadata['title'] = ' '.join([radar.upper(),'Beam:',str(beam),'Gate:',str(gate)])
rkn.metadata['ylabel'] = 'Velocity [m/s]'
rkn.metadata['xlabel'] = 'Time [UT]\n14 July 2008'
#rkn.metadata['lineStyle'] = '.-'
#rkn.active.metadata['xmin'] = datetime.datetime(2008,07,14,20,30)
#rkn.active.metadata['xmax'] = datetime.datetime(2008,07,14,23,30)
#rkn.metadata['validTimes'] = [datetime.datetime(2008,07,14,01,00), datetime.datetime(2008,07,15,01,00)]

In [26]:
#Here is a way to change the valid times for the raw dataset.  Just uncomment the lines below and give it a try!
#valid = [rkn.raw.dtv[0], rkn.raw.dtv[-1]]
#rkn.raw.metadata['validTimes'] = valid

In [27]:
rkn.raw.plot()



In [28]:
#This method will tell us the sampling period of a dataset.
#If it is not regularly sampled, it will give us a warning.
rkn.raw.samplePeriod()


WARNING FOR "RKN Beam: 7 Gate: 0":
   Date time vector is not regularly sampled!
   Maximum difference in sampling rates is 657.003 sec.
   Using average sampling period of 97.774103 sec.
Out[28]:
97.774103

In [29]:
#We can do a linear interpolation the signal to make it regularly sampled.
#This does need to be done with care... we will compare the raw and interpolated signal
#in the next cell.
signal.interpolate(rkn.raw,samplePeriod=3.)

In [30]:
#Now compare the two signals.  You can see that the interpolated version misses parts of the raw signal.
signal.oplot([rkn.raw,rkn.active],xmin=datetime.datetime(2008,7,14,17))


Out[30]:
<pydarn.proc.signal.compare.oplot at 0x4628990>

In [31]:
#Again, detrending and filtering.  We are overriding the global metadata set before by using the numtaps keyword.
signal.detrend(rkn.active)
rknFilt = signal.filter(rkn.detrended,numtaps=1001)

In [32]:
#Print out some information about the filter being used.
rknFilt.comment


Out[32]:
'Filter: blackman, Nyquist: 0.166666666667 Hz, Cuttoff: [0.001, 0.004] Hz'

In [33]:
#Plot the transfer function.  The worN keyword computes the transfer function at a higher resolution than the
#default 512/(2*Pi).  See plotTransferFunction docstring for details.
rknFilt.plotTransferFunction(xmax=0.009,worN=5000)



In [34]:
#Look at the filtered data.
rkn.filtered.plot(xmin=datetime.datetime(2008,7,14,17))



In [35]:
#Compare the filtered with the non-filetered spectrum.
a = signal.oplotfft([rkn.detrended,rkn.filtered],xmax=0.01)


Overplotting Data

We can now compare the Rankin Inlet data with the satellite data.


In [36]:
#The truncate method removes any part of the signal that is outside of the validTimes.
goes10.filtered.truncate()
rkn.filtered.truncate()


Out[36]:
<pydarn.proc.signal.signal.sigStruct at 0x4d329d0>

In [37]:
#Now we overplot the time series.  There is still some gray since the data sets do not overlap perfectly in time.
oplot = signal.oplot([goes10,rkn],normalize=True,legend_size=10)



In [38]:
#We can also compare the spectra of the two signals.
oplot = signal.oplotfft([goes10,rkn],normalize=True,legend_size=12,xmax=0.010)


Common Dtv Grid


In [39]:
#Finally, it is possible to place everything onto an identical time grid. The most restrictive range of validtimes and the highest time resolution is used.
signal.commonDtv([goes10.active,rkn.active])

In [40]:
#Overplot two signals.  The amplitude of goes10 is small compare to rkn, so it is hard to really make anything out here.
#Note in the command below, no dataset is specified, so the active one is used by default.
signal.oplot([goes10,rkn])


Out[40]:
<pydarn.proc.signal.compare.oplot at 0x3bf9c90>

In [41]:
#Don't forget we can look at the history of an object!
goes10.active.history


Out[41]:
{datetime.datetime(2013, 7, 9, 10, 57, 32, 13890): 'Signal Object Created',
 datetime.datetime(2013, 7, 9, 10, 57, 39, 927455): 'Truncate: 2008JUL14 16:00 UT - 2008JUL15 00:00 UT',
 datetime.datetime(2013, 7, 9, 10, 57, 40, 207134): 'Linear detrend (scipy.signal.detrend)',
 datetime.datetime(2013, 7, 9, 10, 57, 42, 588337): 'Filter: blackman, Nyquist: 0.00833333333333 Hz, Cuttoff: [0.001, 0.004] Hz',
 datetime.datetime(2013, 7, 9, 10, 58, 16, 313758): 'Truncate: 2008JUL14 16:50 UT - 2008JUL14 23:10 UT',
 datetime.datetime(2013, 7, 9, 10, 58, 19, 356851): 'Interpolate: [2008-07-14 16:50:00 to 2008-07-14 22:34:12.300000] dt=0.3 s, Nyq=1.66666666667 Hz'}

In [42]:
rkn.active.history


Out[42]:
{datetime.datetime(2013, 7, 9, 10, 58, 0, 910984): 'Signal Object Created',
 datetime.datetime(2013, 7, 9, 10, 58, 8, 288424): 'Truncate: 2008JUL14 16:00 UT - 2008JUL15 00:00 UT',
 datetime.datetime(2013, 7, 9, 10, 58, 8, 377004): 'Interpolate: [2008-07-14 16:00:24.512000 to 2008-07-14 22:59:09.512000] dt=3.0 s, Nyq=0.166666666667 Hz',
 datetime.datetime(2013, 7, 9, 10, 58, 9, 822152): 'Linear detrend (scipy.signal.detrend)',
 datetime.datetime(2013, 7, 9, 10, 58, 10, 32108): 'Filter: blackman, Nyquist: 0.166666666667 Hz, Cuttoff: [0.001, 0.004] Hz',
 datetime.datetime(2013, 7, 9, 10, 58, 16, 467798): 'Truncate: 2008JUL14 16:25 UT - 2008JUL14 22:34 UT',
 datetime.datetime(2013, 7, 9, 10, 58, 19, 812012): 'Interpolate: [2008-07-14 16:50:00 to 2008-07-14 22:34:12.300000] dt=0.3 s, Nyq=1.66666666667 Hz'}

In the future, we can have cross correlation tools, too. These commands are almost working, but not quite...


In [43]:
#xcor = signal.xcor(rkn,goes10)

In [44]:
#xcor.xcor.plot()