예제 #1
0
    def main(self):
        """
        main body of utiliy.

        Preconditions:
            utility setup
        Postconditions:
            utility is run
        """
        # set up
        data = DatFile(self.commands["--infile"],"4")
        latitude = float(self.commands["--lat"])
        longitude = float(self.commands["--long"])
        columns = [data.getColumn(0),
                   np.array(data.getColumn(1)).astype(float)]
        # test out put
        out_file = CsvFile(self.commands["--outfile"], opti = True)
        last_date = datetime(1000,1,1)
        if not out_file.exists():
            fd = open(self.commands["--infile"])
            first_line = fd.read().split(",")[0:2]
            first_line[1] += "\n"
            out_file.set_header([ first_line,
                                  ("timestamp", "Albedo\n"),
                                  ("","\n"),
                                  ("","Avg\n")])
        else:
            last_date = out_file[0][-1]
        alb_vals = []
        alb_dates = []
        idx = 0
        while idx < len(columns[0]):
            compVal = columns[0][idx]
            # I'm not sure what this does...
            if datetime.strptime(compVal,'"%Y-%m-%d %H:%M:%S"') <= last_date:
                idx = idx + 1
                continue
            # this point is where the magic happens.
            raw_index = float(columns[1][idx])
            isdark = self.calc_SunAngle(latitude,longitude,datetime.strptime(compVal,'"%Y-%m-%d %H:%M:%S"'))
            if isdark == 6999 :
                alb_vals.append(6999.0)
            else:
                alb_vals.append(raw_index)

            alb_dates.append(datetime.strptime(compVal,'"%Y-%m-%d %H:%M:%S"'))
            idx= idx+1
        # save
        out_file.add_dates(alb_dates)
        out_file.add_data(1,alb_vals)
        out_file.append()
예제 #2
0
class TramTransform (object):
    """
    this class is for breaking the tram data in to separate files
    """
    def __init__ (self, fName):
        """
        set up class
        
        Preconditions:
            file 'fname' should exist'
        Postconditions: 
            the other functions may be run
        """
        self.inData = DatFile(fName,'tram')
        self.outData = {}

    def transform (self):
        """
        transform the data
        
        Preconditions:
            inData should have data
        Postcondidions:
            outData is a library of sets of rows for each variable in the 
        inData
        """
        dateCol = self.inData.getColumn(0)
        countCol = self.inData.getColumn(2)
        colLib = {}
        idx = 3
        while True:
            try:
                colLib[self.inData.col_names[idx]]= self.inData.getColumn(idx)
                idx +=1
            except IndexError:
                break
        for key in self.inData.col_names[3:]:
            print key
            #~ print colLib[key][:10]
            rows = []
            start = 0
            last = 0
            for idx in range(len(countCol)):
                if int(countCol[idx]) < int(countCol[last]) and idx != start:
                    date = datetime.datetime.strptime(dateCol[start][:13],
                                            "%Y-%m-%d %H")
                    if start == 0:
                        row = colLib[key][start:idx]
                    else:
                        row = colLib[key][start-1:idx]
                    row.insert(0,date)
                    #~ print row
                    rows.append(row)
                    start = idx +1

                last = idx
                #~ if idx == 200:
                    #~ break
            end = colLib[key][start-1:]
            date = datetime.datetime.strptime(dateCol[start][:13],
                                            "%Y-%m-%d %H")
            end.insert(0,date)
            rows.append(end)
            self.outData[key] = rows

    def save (self, outDir):
        """
        Save the output files.
        
        Pre:
            self.transform sould have been called.
            outDir needs to exist
        Post:
            Files for each variable will be saved or updated
        """
        for key in self.inData.col_names[3:]:
            data = self.outData[key]
            outFile = CsvFile(outDir+key+'.csv', opti = True)
            if not outFile.exists():
                cols = range(1,len(data[0]))

                cols.insert(0,'TIMESTAMP')
                #~ print cols
                for i in range(len(cols)):
                    cols[i] = str(cols[i])
                cols[-1]+='\n'
                outFile.set_header([('TOA5','ngee-tram','CR3000','8379',
                                    'CR3000.Std.25','CPU:cart6runzt.CR3',
                                    '46105','Radiation\n'),
                                    cols])
            else:
                last_date = outFile[0][-1]

            for idx in range(len(data[0])):
                col = []
                for row in data:
                    try:
                        col.append(row[idx])
                    except IndexError:
                        col.append('')
                        continue
                outFile.add_data(idx,col)
            outFile.append()
예제 #3
0
    def main(self):
        """
        main body of utiliy. 
        
        Preconditions:
            utility setup
        Postconditions:
            utility is run
        """
        # set up 
        data = DatFile(self.commands["--inFile"],"4")
        timeC = int(self.commands["--timeCol"])
        tempC = int(self.commands["--tempCol"])
        powerC = self.commands["--powerCol"]
        columns = [data.getColumn(0), 
                   np.array(data.getColumn(timeC)).astype(float),
                   np.array(data.getColumn(tempC)).astype(float)]
        if powerC != "":
            powerC = int(powerC)
            columns.append(np.array(data.getColumn(powerC)).astype(float))
        
        # test out put
        out_file = CsvFile(self.commands["--outFile"], opti = True)
        last_date = datetime(1000,1,1) 
        if not out_file.exists():
            fd = open(self.commands["--inFile"])
            first_line = fd.read().split(",")[0:2]
            first_line[1] += "\n"
            out_file.set_header([ first_line,
                                  ("timestamp","k\n"), 
                                  ("","w/mk\n"),
                                  ("","smp\n")])
        else:
            last_date = out_file[0][-1]
    
        # process k 
        k_vals = []
        k_dates = []
        idx = 0 
        while idx < len(columns[0]):
            compVal = columns[0][idx]
            begin = idx
            end = idx
            try:
                while compVal == columns[0][idx]:
                    idx += 1
                    end = idx
            except:
                pass
            if datetime.strptime(compVal,'"%Y-%m-%d %H:%M:%S"') <= last_date: 
                continue
            if powerC == "":
                k_vals.append(self.calc_K(columns[1][begin:end],
                                          columns[2][begin:end]))
            else:
                k_vals.append(self.calc_K(columns[1][begin:end],
                                          columns[2][begin:end],
                                          columns[3][begin:end]))
            k_dates.append(datetime.strptime(compVal,'"%Y-%m-%d %H:%M:%S"'))                                          
        
        
        # save 
        out_file.add_dates(k_dates)
        out_file.add_data(1,k_vals)

        out_file.append()