In [1]:
import os
import sys
import json
import pylab
import numpy as np
import matplotlib.ticker
from dateutil import parser
from scipy import interpolate
from matplotlib.dates import date2num
from datetime import timedelta, datetime
from pysurvey.plot import (setup, dateticks, minmax, hcolorbar, line, embiggen, icolorbar)

In [11]:
%%bash

#rsync -ravpP aether:.temperature.xenon.log ~/
#rsync -ravpP xenon:.temperature.xenon.log ~/
rsync -ravpP gxenon:.temperature.xenon.log ~/
rsync -ravpP gxenon:.uptime.xenon.log ~/


receiving file list ... 
1 file to consider
.temperature.xenon.log
    11502860 100%    2.49MB/s    0:00:04 (xfer#1, to-check=0/1)

sent 18024 bytes  received 2551257 bytes  302268.35 bytes/sec
total size is 11502860  speedup is 4.48
receiving file list ... 
1 file to consider
.uptime.xenon.log
     2135049 100%    2.68MB/s    0:00:00 (xfer#1, to-check=0/1)

sent 7938 bytes  received 425729 bytes  78848.55 bytes/sec
total size is 2135049  speedup is 4.92

In [12]:
FILENAME = os.path.expanduser('~/.temperature.xenon.log')
SEP = ' | '
YR = [5,60]
TNORM = 15

In [13]:
def read_temps(filename=FILENAME):
    out = []
    with open(filename, 'r') as f:
        lines = f.readlines()
        for line in lines:
            if ('Killed' in line) or ('Segmentation' in line):
                continue
            items = line.split(SEP)
            try:
                out.append(dict(date=parser.parse(items[1]),
                                temperature=float(items[2])/1000.0))
            except Exception as e:
                print e
                print items
                raise
    return out
data = read_temps()
print len(data)


179729

In [14]:
def get_continuum(dates, x, y, delta=2):
    out = []
    t = timedelta(hours=delta)
    for d in dates:
        ii = np.where( (date2num(x) >  date2num(d-t) ) &
                       (date2num(x) <= date2num(d+t) ) )[0]
        if len(ii) <= 20:
            out.append(-1)
        else:
            out.append( np.mean(y[ii]) )
    
    
    t = date2num(dates)
    n = np.min(t)
    tmp = np.array(out)
    ii = np.where(tmp > 0)
    f = interpolate.UnivariateSpline(date2num(dates[ii])-n,tmp[ii], s=4)
    return f(t-n)

def get_continuum(dates, x, y, delta=5):
    out = []
#     t = timedelta(hours=delta)
    tmp = date2num(x)
    nd = date2num(dates)
    for d in nd:
        t = np.abs(tmp-d)
        ii = np.where( t < delta/24.0)[0]
        if len(ii) <= 20:
            out.append(-1)
        else:
            out.append( np.mean(y[ii]) )
#             out.append(np.min(y[ii]))
#     return np.array(out)
    
    tmp = np.array(out)
    n = np.min(nd)
    ii = np.where(tmp > 0)
    f = interpolate.UnivariateSpline(nd[ii]-n,tmp[ii], s=4)
    return f(nd-n)

    



dates, values = map(np.array, zip(*[(d['date'], d['temperature'])
                                    for d in data]))
ndates = date2num(dates)
tmp = (ndates % 1.0)*24.0
ii = np.where((tmp > 0) & (tmp < 8))[0]
continuum = get_continuum(dates, dates[ii], values[ii])

In [15]:
def setupplot(secondax=False, **kwargs):
    ytickv = np.linspace(YR[0],YR[1],6)
    yticknames = map('{:0.0f}'.format, ytickv)
    tmp = dict(
        ylabel='Temperature [c]',
        yr=minmax(ytickv), ytickv=ytickv,
        yticknames=yticknames,
    )
    tmp.update(kwargs)
    ax = setup(**tmp)
    
    if secondax:
        subplt = kwargs.get('subplt',None)
        f = lambda x: '{:0.0f}'.format(1.8*x + 32.0)
        yticknames = map(f, ytickv)
        ax2 = ax.twinx()
        ax2.set_ylabel(r"Temperature [F]")
        ax2.set_ylim(minmax(ytickv))
        ax2.yaxis.set_major_locator(matplotlib.ticker.FixedLocator(ytickv))
        ax2.yaxis.set_major_formatter(matplotlib.ticker.FixedFormatter(yticknames))
        pylab.sca(ax)
    return ax

In [16]:
def plot_temp():
    setup(figsize=(12,6))

    setupplot(subplt=(1,2,1), autoticks=True, xlabel='Date',)
    pylab.plot(dates, values)
    pylab.plot(dates[ii], values[ii], '.r')
    pylab.plot(dates, continuum, '.k')
    dateticks('%Y.%m.%d')
    
    
    setupplot(subplt=(2,2,2), autoticks=False, xlabel='Hour of Day')
    pylab.plot(tmp, values, '.')
    setupplot(subplt=(2,2,2), ylabel='', secondax=True)
    
    setupplot(subplt=(2,2,4), autoticks=False, xlabel='Hour of Day')
    sc = pylab.scatter(tmp, values-continuum+TNORM, 
                       c=date2num(dates)-np.min(date2num(dates)), s=15,
                       marker='.', edgecolor='none',
                       label='Days since Start')
    
    setupplot(subplt=(2,2,4), ylabel='', secondax=True)
    hcolorbar(sc, axes=[0.6, 0.4, 0.1, 0.01])
    
    pylab.tight_layout()
    pylab.show()
plot_temp()



In [17]:
now = datetime.now()
start = date2num(now - timedelta(hours=48))
jj = np.where(ndates > start)[0]
print len(jj)

setup(figsize=(6,6))
# setupplot(secondax=True, title='Now:{}'.format(now))

pylab.plot(ndates[jj], values[jj])  
kk = np.where((continuum[jj] < 60) &
              (continuum[jj] > 30) )[0]
pylab.plot(ndates[jj[kk]], continuum[jj[kk]], '.k')
# dateticks('%Y.%m.%d %H')
# line(now+timedelta(hours=5)) # to gmt


1987
-c:10: RuntimeWarning: invalid value encountered in less
-c:11: RuntimeWarning: invalid value encountered in greater
Out[17]:
[<matplotlib.lines.Line2D at 0x10c0f7990>]

In [18]:
def plot_recent(delta=48):
    '''plot the last delta [48] hrs'''
    now = datetime.now()
    start = date2num(now - timedelta(hours=delta))
    jj = np.where(ndates > start)[0]
    print len(jj)
    
    setup(figsize=(6,6), xr=minmax(ndates[jj]))
    setupplot(secondax=True, title='Now:{}'.format(now))
    
    pylab.plot(dates, values)
    pylab.plot(dates[ii], values[ii], '.r')
    pylab.plot(dates, continuum, 'k')
    dateticks('%Y.%m.%d %H')
    line(now+timedelta(hours=5)) # to gmt

plot_recent(256)


13594

In [19]:
def plot_recent(delta=48):
    '''plot the last delta [48] hrs'''
    now = datetime.now()
    start = date2num(now - timedelta(hours=delta))
    ii = np.where(ndates > start)[0]
    print len(ii)
    
    setup(figsize=(6,6))
#     setupplot(secondax=True, title='Now:{}'.format(now))
    
    
    
#     pylab.plot(dates[ii], values[ii])  
    
    nsmooth = 10
    nend = nsmooth*2
    svalues = np.convolve(np.ones(nsmooth)/nsmooth, values[ii], mode='same')
    pylab.plot(dates[ii][nend:-nend], svalues[nend:-nend])  
    
#     kk = np.where( (continuum[ii] > 30) & (continuum[ii] < 100) )
#     pylab.plot(dates[ii][kk], continuum[ii][kk], '.k')
    dateticks('%Y.%m.%d')
    line(now+timedelta(hours=5)) # to gmt

plot_recent(256)


13594

In [20]:
tmp = np.genfromtxt('/Users/ajmendez/.uptime.xenon.log', delimiter='|',
                    dtype=[np.int64,'S32', 'S96'])


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-20-5b9f815d0083> in <module>()
      1 tmp = np.genfromtxt('/Users/ajmendez/.uptime.xenon.log', delimiter='|',
----> 2                     dtype=[np.int64,'S32', 'S96'])

/usr/local/lib/python2.7/site-packages/numpy/lib/npyio.pyc in genfromtxt(fname, dtype, comments, delimiter, skiprows, skip_header, skip_footer, converters, missing, missing_values, filling_values, usecols, names, excludelist, deletechars, replace_space, autostrip, case_sensitive, defaultfmt, unpack, usemask, loose, invalid_raise)
   1690             # Raise an exception ?
   1691             if invalid_raise:
-> 1692                 raise ValueError(errmsg)
   1693             # Issue a warning ?
   1694             else:

ValueError: Some errors were detected !
    Line #16959 (got 1 columns instead of 3)
    Line #18999 (got 1 columns instead of 3)

In [ ]:
def get_5min(x):
    try:
        return float(x[2].split(',')[-3].split(' ')[-1])
    except Exception as e:
        print x[2]
        raise
        return None
def get_date(x):
    return datetime.fromtimestamp(x[0])
# get_date(tmp[0])
udate = map(get_date, tmp)
uptime = map(get_5min, tmp)

In [ ]:
setup(figsize=(12,6), xr=minmax(udate), ylog=True )
pylab.plot(udate, uptime)
pylab.plot(dates, values)
dateticks('%Y-%m-%d', rotation=90)

In [ ]:
print len(dates), len(udate)

In [ ]:
utemp = np.interp(date2num(udate), date2num(dates), values)

In [ ]:
setup(figsize=(12,12),
      xlabel='Uptime', ylabel='Temperature[c]')
c = date2num(udate)
c -= np.min(c)

# pylab.plot(uptime, utemp, '.')
sc = pylab.scatter(uptime, utemp, c=c, edgecolor='none')
_ = icolorbar(sc, loc=1)

In [ ]: