Skip to content

Instantly share code, notes, and snippets.

@imrehg
Created September 13, 2012 11:54
Show Gist options
  • Save imrehg/3713847 to your computer and use it in GitHub Desktop.
Save imrehg/3713847 to your computer and use it in GitHub Desktop.
Data converter for temperature logger
# -*- coding: utf-8 -*-
import numpy as np
import pylab as pl
import datetime
import pytz
import time
from time import mktime
from matplotlib.dates import strpdate2num, epoch2num, num2date
tz = pytz.timezone('Asia/Taipei')
logfile = "2012_0828_15_26pm.csv"
# logfile = "test3.csv"
def dateconv(s):
date, time = s.split(' ')
month, day, year = [int(x) for x in date.split('/')]
time = time.decode('utf-8')
hour = int(time[2:4])
if time[0] != u'上':
if hour < 12:
hour += 12
elif hour == 12:
hour = 0
minute = int(time[5:7])
second = int(time[8:9])
thisdate = datetime.datetime(2000+year, month, day, hour, minute, second, 0)
return float(mktime(thisdate.timetuple()))
dt=np.dtype({'names':['date','temperature','humidity'],'formats':[np.float, np.float, np.float]})
logdate, temperature, humidity = np.loadtxt(logfile,
delimiter=',',
comments="\"",
usecols=(1, 2, 3),
converters = {1: lambda x: dateconv(x), 2: lambda x: np.nan if x == "" else float(x), 3: lambda x: np.nan if x == "" else float(x)},
dtype=dt,
unpack=True)
newdate = epoch2num(logdate)
# print date
# newdate = date[:]
print newdate
fig = pl.figure(figsize=(11.27, 8.69))
ax1 = fig.add_subplot(211)
ax1.plot_date(newdate, humidity, 'k-')
ax1.set_ylabel("Humidity (%)")
ax2 = fig.add_subplot(212)
ax2.plot_date(newdate, temperature, 'k.')
ax2.set_ylabel("Temperature (C)")
fig.autofmt_xdate()
pl.show()
1 09/10/12 下午03時14分16秒 25.805 51.39
2 09/10/12 下午03時14分26秒 25.805 51.65
3 09/10/12 下午03時14分36秒 25.902 51.96
4 09/10/12 下午03時14分46秒 25.902 52.35
5 09/10/12 下午03時14分56秒 26.097 53.02
6 09/10/12 下午03時15分06秒 26.195 53.31
7 09/10/12 下午03時15分16秒 26.292 53.38
8 09/10/12 下午03時15分26秒 26.292 53.67
9 09/10/12 下午03時15分36秒 26.390 53.79
10 09/10/12 下午03時15分46秒 26.390 53.55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment