timeseries_sample1ΒΆ

A sample script for timeseries.ScalarSeries

This is a sample file to use the timeseries.ScalarSeries. In addition, danger of extrapolation is also shown.

''' A sample script for timeseries.ScalarSeries

This is a sample file to use the :class:`timeseries.ScalarSeries`.
In addition, danger of extrapolation is also shown.

'''

import os
import sys
import logging
logging.basicConfig()
import datetime
from datetime import datetime as dt
import math

import matplotlib.pyplot as plt
import numpy as np
import scipy as sp

from irfpy.util.timeseries import ScalarSeries as SS

def main():

    fig = plt.figure()
    ax = fig.add_subplot(111)

    # Prepare the data

    delta = datetime.timedelta(hours=1)
    tlist = [dt(2011, 12, 1), ]
    for i in range(100):
        tlist.append(tlist[-1] + delta * i)
    # The last day is end of 2012.

    data = np.arange(101) * np.cos(np.arange(101) * np.pi / 18)

    ax.plot(tlist, data, 'o', label='raw data')

    # Time series instance
    ts = SS(tlist, data)

    # Interpolation
    delta2 = datetime.timedelta(days=1)
    tlist2 = []
    t = ts.t0()
    t1 = ts.t1()
    while t <= t1:
        tlist2.append(t)
        t = t + delta2
    ax.plot(tlist2, ts.spline_interpolate(tlist2), 'r-', label='Interpolation')

    # Extrapolation
    delta3 = datetime.timedelta(hours=6)
    tlist3 = []
    t = datetime.datetime(2011, 11, 30)
    t1 = datetime.datetime(2012, 10, 1)
    while t <= t1:
        tlist3.append(t)
        t = t + delta3
    ax.plot(tlist3, ts.spline_interpolate(tlist3, extrapolate=True), 'g:', label='Extrapolation')
    
    ax.set_xlim(datetime.datetime(2011, 11, 28), t1)

    ax.legend(loc='lower right')
    fig.autofmt_xdate()
    fig.savefig('timeseries_sample1.png')


if __name__ == '__main__':
    main()