Skip to content

Commit

Permalink
Changed the way the program outputs data
Browse files Browse the repository at this point in the history
We now output to Output\$FILENAME.txt
  • Loading branch information
HelixSpiral committed Mar 2, 2018
1 parent e0cc17c commit df33849
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
31 changes: 31 additions & 0 deletions FileFunctions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bufio" // Needed to write to the output file
"fmt" // Used for printing error messages
"os" // Needed for os.FileInfo
"path/filepath" // Needed for filepath.Walk
Expand Down Expand Up @@ -39,3 +40,33 @@ func returnInputFiles(inputFolder string) []string {

return inputFiles
}

// Write to the file
func writeFile(file string, data PowerDataReturn) error {
// Create the file
createdFile, err := os.Create(file)
if err != nil {
fmt.Println("Error:", err)
}
defer createdFile.Close()

// Create a new writer for the file
write := bufio.NewWriter(createdFile)

// Write the data to the file
write.WriteString(fmt.Sprintf("Power data for file: %s\r\n", data.filePath))
write.WriteString(fmt.Sprintf("Total kWh usage: %.03f\r\n", data.totalkWh))
write.WriteString(fmt.Sprintf("Average daily kWh: %.03f\r\n", data.averageDailykWh))
write.WriteString(fmt.Sprintf("Average hourly kWh: %.03f\r\n", data.averageHourlykWh))
write.WriteString(fmt.Sprintf("Lowest daily kWh: %.03f on %s\r\n", data.lowestDailykWh, data.lowestDay))
write.WriteString(fmt.Sprintf("Highest daily kWh: %.03f on %s\r\n", data.highestDailykWh, data.highestDay))
for y, z := range data.hourlykWh {
write.WriteString(fmt.Sprintf("Hour: %v | Usage: %.03f, Highest: %.03f | Lowest: %.03f\r\n", y, z/data.totalDays, data.highestHour[y], data.lowestHour[y]))
}

// Flush
write.Flush()

// Return no error
return nil
}
1 change: 1 addition & 0 deletions Output/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.txt
27 changes: 13 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
package main

import (
"fmt" // Needed to print output
"sync" // Needed for WaitGroups
"time" // Needed for time.Time
"fmt" // Needed to print output
"regexp" // Needed for regexs
"sync" // Needed for WaitGroups
"time" // Needed for time.Time
)

// Populated from the csv files
Expand Down Expand Up @@ -61,18 +62,16 @@ func main() {
// Wait for the go routines to finish and then print
wg.Wait()

// Loop the slice of PowerDataReturns and provide output.
// Loop the slice of PowerDataReturns
for x := range outputInfo {
fmt.Println("Power data for file:", outputInfo[x].filePath)
fmt.Printf("Total kWh usage: %.03f\r\n", outputInfo[x].totalkWh)
fmt.Printf("Average daily kWh: %.03f\r\n", outputInfo[x].averageDailykWh)
fmt.Printf("Average hourly kWh: %.03f\r\n", outputInfo[x].averageHourlykWh)
fmt.Printf("Lowest daily kWh: %.03f on %s\r\n", outputInfo[x].lowestDailykWh, outputInfo[x].lowestDay)
fmt.Printf("Highest daily kWh: %.03f on %s\r\n", outputInfo[x].highestDailykWh, outputInfo[x].highestDay)

// Loop for each hour and print hour-specific data.
for y, z := range outputInfo[x].hourlykWh {
fmt.Printf("Hour: %v | Usage: %.03f, Highest: %.03f | Lowest: %.03f\r\n", y, z/outputInfo[x].totalDays, outputInfo[x].highestHour[y], outputInfo[x].lowestHour[y])
// Find the file name
re := regexp.MustCompile("(?i)Input\\\\(.+)\\.csv")
outputFile := re.FindStringSubmatch(outputInfo[x].filePath)

// Write to the file.
err := writeFile(fmt.Sprintf("Output\\\\%s.txt", outputFile[1]), outputInfo[x])
if err != nil {
fmt.Println("Error:", err)
}
}
}

0 comments on commit df33849

Please sign in to comment.