def __init__(self): """Takes in the file name of a file created by the NI temperature logger program and generates a Temp_log object.""" self.units = None self.temp_collection = SortedCollection([],lambda x:x[0])
def addFile(self,fname): """Adds the data from a file to this log """ (tmp,units) = self._parseFile(fname) if (self.units is not None) and (units != self.units): print units print self.units raise Exception('units must match') tmp.extend(self.temp_collection._items) self.temp_collection = SortedCollection(tmp,self.temp_collection._getkey())
class Temp_log: def __init__(self): """Takes in the file name of a file created by the NI temperature logger program and generates a Temp_log object.""" self.units = None self.temp_collection = SortedCollection([],lambda x:x[0]) def parse_line(self,strin): """Parse a single line of the log file """ (tm,temp) = strin.split('\t') tm = datetime.strptime(tm.strip(),'%I:%M:%S %p').time() if tm < self.start_time: self.start_time = tm self.cur_date = self.cur_date + timedelta(days=1) temp = float(temp.strip()) return (datetime.combine(self.cur_date,tm),temp) def _parseFile(self,fname): # open file to deal with it f = open(fname,'r') # get the start time and date tmp_ln = f.readline() init_time = datetime.strptime((":".join(tmp_ln.split(":")[1:])).strip(),'%m/%d/%Y %I:%M:%S %p') self.cur_date = init_time.date() self.start_time = init_time.time() # get units of temperature units = f.readline().split(":")[-1].strip() # jump over the other header stuff for j in range(0,6): f.readline() tmp = [self.parse_line(ln) for ln in f] # clean up file f.close() return tmp,units def addFile(self,fname): """Adds the data from a file to this log """ (tmp,units) = self._parseFile(fname) if (self.units is not None) and (units != self.units): print units print self.units raise Exception('units must match') tmp.extend(self.temp_collection._items) self.temp_collection = SortedCollection(tmp,self.temp_collection._getkey()) def get_temp(self,in_time): """returns the temperature at the time asked""" try: temp = self.temp_collection.find(in_time.replace(microsecond=0))[1] except ValueError,v: temp_m1 = self.temp_collection.find(in_time.replace(microsecond=0) - timedelta(seconds=1))[1] temp_p1 = self.temp_collection.find(in_time.replace(microsecond=0) + timedelta(seconds=1))[1] temp = (temp_p1 + temp_m1)/2 return temp