Click here to Skip to main content
14,325,129 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi,

This is a snippet of my txt file (data.txt)

TimeStamp,Irradiance,Ambient_Temperature
21/7/2014 0:00,0.66,29.16
21/7/2014 0:00,0.71,29.16
21/7/2014 0:00,0.65,29.17
21/7/2014 0:00,0.67,29.17
21/7/2014 0:01,0.58,29.17
.
.
.
22/7/2014 23:58,0.69,28.54
22/7/2014 23:58,0.61,28.54
22/7/2014 23:58,0.65,28.51
22/7/2014 23:58,0.59,28.54
22/7/2014 23:59,0.63,28.55
22/7/2014 23:59,0.67,28.54
22/7/2014 23:59,0.68,28.56
22/7/2014 23:59,0.58,28.55



My Program will ask the user to enter from what time to what and will plot both the irradiance & ambient temperature

For now, i am able to display max/min/mean for over the days
but what i trying to achieve is:

1. Display the max/min per day for irradiance & ambient temperature(within each 24 hours period)
2. Display mean per day between 8:00a.m to 5:00p.m for irradiance & ambient temperature

This is a snippet of my code (for displaying max/min/mean):

from datetime import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np # support large, multi-dimen arrays n matrices
from scipy.stats import mode # stats like finding mode
import re # regular expression (process text)
import itertools

linematchregex = re.compile('(\d+/\d+/\d+ \d+:\d+),(\d+\.\d+)') # find multiple pairs on the same line

startTime = datetime.strptime(raw_input('please enter start time in format like 21/7/2014 0:00 :'), '%d/%m/%Y %H:%M')
endTime   = datetime.strptime(raw_input('please enter end time in format like 22/7/2014 23:57 :') , '%d/%m/%Y %H:%M')

with open(r'temp.txt', 'r') as f:
f.next() #skip first line
t,temp = zip(*[p for line in f for p in linematchregex.findall(line)]) 
groups = itertools.groupby(f, lambda row: row[0].split(',')[0])
for date, group in groups:     
group = [(datetime.datetime.strptime(dt), value)                  
    for dt, value in group]      
during_8to5 = [(dt, value) for dt, value in group                          
  if datetime.time(8) <= dt.time() < datetime.time(17)]          


t = [datetime.strptime(x, '%d/%m/%Y %H:%M') for x in t ]
temp = [float(x) for i,x in enumerate(temp) if startTime<=t[i]<=endTime]

t = [x for x in t if startTime<=x<=endTime]
fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1, axisbg='white')
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
ax1.set_ylim(15,40)
ax1.plot(t, temp, 'c', linewidth=3.3)


short_title = ('Ambient Temperature vs. Time Graph \n From {} To {} \n ')
long_title = ('\n Max AMB_TEMP Value: {:.2f} at {} , Min AMB_TEMP Value: {:.2f} at {}, Mean AMB_TEMP Value: {:.2f} at {}\n')
fig.suptitle(short_title.format(startTime, endTime), fontsize=20, color='blue')
ax1.set_title(long_title.format(max(group),min(group),sum(during_8to5)/len(during_8to5),fontsize=16, color='green', ha='bottom'))
plt.ylabel(u'Ambient Temperature(\u2103)', fontsize=16, color='blue')
plt.xlabel('Time ($H:M$)', fontsize=16, color='blue')
fig.tight_layout()
plt.show()


Problem i have when i run it is: a error will say the for date, group in groups: line's there's an error in your program: unexpected indent
Posted
Updated 5-Aug-14 22:24pm
v4

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

The error is telling you what the problem is, i.e. there is an indentation error. As you know working with python, you need to ensure that the code structure has the correct level of indentation at each point otherwise it is not synatically correct.

Start by building up the code and execute in small chunks until the error is thrown that will tell you which part is causing the problem, you can then look at that section and add/remove spaces with etc. with the indents until the problem is fixed.

for date, group in groups:
group = [(datetime.datetime.strptime(dt), value)
    for dt, value in group]
during_8to5 = [(dt, value) for dt, value in group
  if datetime.time(8) <= dt.time() < datetime.time(17)]


A quick glance, suggests this doesn't look right.
   
Comments
fleetfox 6-Aug-14 5:27am
   
hi DaveAuld,
when i add ax1.set_title line below the if datetime.time line then the error is gone
but no max/min/mean is displayed
could you possibly help me solve this error? i have been facing this problem for days

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100