Building Time Based Bins
Hello, I'm new to python and this group and am trying to build some bins and was wondering if any of you could kindly help me out. I'm a bit lost on how to begin. I have some text files that have a time filed along with 2 other fields formatted like this >> 1231 23 56 1232 25 79 1234 26 88 1235 22 34 1237 31 85 1239 35 94 This goes on throughout a 12hr. period. I'd like to be able to place the low and high values of the additional fields in a single line divided into 5min intervals. So it would look something like this >> 1235 22 88 1240 31 94 I hope that makes sense. Should I be using a module like numarray for this, or is it possible to just use the native functions? Any ideas would help me very much. Thank you - Marcus -- http://mail.python.org/mailman/listinfo/python-list
Re: Building Time Based Bins
John Machin wrote: > Are you (extremely) new to computer programming? Is this school > homework? Lol, yes, I am relatively new to programming... and very new to python. I have experience working with loops, if thens, and boolean operations, but I haven't worked with lists or array's as of yet... so this is my first forray. This isn't homework, I'm long out of school. I've been wanting to extend my programming abilities and I chose python as the means to acheiving that goal... so far I really like it :-) Thank you both for the code. I ended up working with John's because it's a bit easier for me to get through. I very much appreciate the code... it taught me quite a few things about how python converts string's to integers and vice versa. I didn't expect to get thorugh it, but after looking at it a bit, I did, and was able to modify it so that I could work with my own files. Yeah! The only question I have is in regards to being able to sum a field in a bin. Using sum(hi) returns only the last value... I'm uncertain how to cumulatively add up the values as the script runs through each line. Any pointers? Thank you again for all your help. Marcus -- http://mail.python.org/mailman/listinfo/python-list
Re: Building Time Based Bins
Never mind about the summing... I learned that you can do this: sumhi = 0 sumhi += hi Cool! Thanks again. -- http://mail.python.org/mailman/listinfo/python-list
Re: Building Time Based Bins
Thanks Alessandro... I'll have to try that as well. I have a modified working version of John's code (thanks John!). I'm able to output the bins by 5min intervals, sum one of the fields, and get the high and low of each field. So far I'm really happy with how it works. Thank you to everybody. The only thing that I'd like to do, which I've been racking my brain on how to do in python... is how to keep track of the bins, so that I can refer back to them. For instance, if I wanted to get "binlo" from two bins back... in the scripting language I was working with (pascal based) you could create a counting series: for binlo = binlo - 1 do begin 2binlosBack = (binlo - 2) # if it was 12:00, I'd be looking back to 11:50 I would really appreciat if anyone could explain to me how this could be accomplished using python grammar... or perhaps some other method "look back" which I'm unable to conceive of. Many thanks, Marcus -- http://mail.python.org/mailman/listinfo/python-list
Re: Building Time Based Bins
Hi Michael, thanks for responding. I actually don't use a method to get each bin... the bin outputs are nested in the loop. Here's my code: data_file = open('G:\file.txt') DUMMY = bintm = DUMMY for line in data_file: fields = line.strip().split() if not line: continue ilist = [int(time), int(a)] #print "ilist:", ilist klock, a = ilist newbintm = ((klock + 4) // 5 * 5 ) % 2400 print "bintm = %d, newbintm = %d, a = %d" % (bintm, newbintm, a) # the above is the raw data and now the bin loop if bintm == : bintm = newbintm binlo = a elif bintm == newbintm: binlo = min(binl, t) else: print " ==>> %04d %2d" % (bintm, binl) ## this is the bin bintm = newbintm binl = a #--- the input file is in my first post in this thread, the output looks like: bintm = , newbintm = 1235, a = 23 bintm = 1235, newbintm = 1235, a = 25 bintm = 1235, newbintm = 1235, a = 26 bintm = 1235, newbintm = 1240, a = 22 ==>> 1235 23 bintm = 1240, newbintm = 1240, a = 31 bintm = 1240, newbintm = 1240, a = 35 #- I'm not sure where I could create the new list without it getting overwritten in the bin loop. Confused as to how to add the append method in a for loop without a defined method for the current bin. Anyway, I'll keep at it, but I'm not sure how to execute it. Thank you very much for your suggestion. Marcus -- http://mail.python.org/mailman/listinfo/python-list
Re: Building Time Based Bins
Ok, thanks Michael, I got it sorted out now. It was just a question of placing the append statement and the new list in the right place. I also added a delete command so the list doesn't become too huge, especially when there's no need to keep it. Here's the corrected code: if bintm == : bintm = newbintm binlo = a lastbinlo = [binlo]## new bin creation elif bintm == newbintm: binlo = min(binl, t) else: if len(lastbinlo) > 1: ## check for append data del lastbinlo(0) ## delete extras lastbinlo.append(binlo) ## append new data here print lastbinlo[-2] print " ==>> %04d %2d" % (bintm, binl) ## this is the bin bintm = newbintm binlo = a Anyway, many thanks to everyone who helped with this code. Best regards, Marcus -- http://mail.python.org/mailman/listinfo/python-list
Re: Building Time Based Bins
Michael Spencer wrote: > (BTW, there must be more to your code than you have shared for the above line to > execute without raising an exception - where are 'time' and 'a' initially bound? > BTW2, 'time' is the name of a stdlib module, so it's bad practice to use it as > an identifier) Yes there is more, I was copy/pasting a bit haphazardly as I see now. You're right about the identifier, I changed it in my current code to "t". > > print " ==>> %04d %2d" % (bintm, binl) ## this is the bin > This is where you've declared that you have a bin, so add it to the bins cache: > bins.append((bintm, binl)) > > bintm = newbintm > > binl = a > > > Michael Thanks Michael, I haven't been able to read my mail so I ended up placing the append a bit differently than the way you described, and somehow got it working... your way looks much easier :-). I'm going to try that right now. I've mostly been racking my brain with this bit of code: newtm = ((klock + 4) // 5 * 5 ) % 2400 It works ok until you get to the last five minutes of the hour. For instance, 956 will return 960... oops, that's not gonna work :). I don't completely understand how this code is doing what it's doing... I've played around with different values, but it's still a bit of a mystery in coming up with a solution. My only work around that I've been able to come up with is to add 40 to newtm when the last 2 digits are at 60, but I'm still working on how to do that. Anyway, thanks for your help, mentioning the append function... that really opened up a lot of solutions/possibilities for me. Take care, Marcus -- http://mail.python.org/mailman/listinfo/python-list