diff --git a/csv-file.go b/csv-file.go new file mode 100644 index 0000000..d4b5c77 --- /dev/null +++ b/csv-file.go @@ -0,0 +1,180 @@ +// Copyright 2019 Gytis Repečka (gytis@repecka.com). All rights reserved. +// Use of this source code is governed by a GNU GPL license that can be +// found in the LICENSE file. + +package main + +import ( + "fmt" + "time" + "os" + "encoding/csv" + "io" + // "log" + // "bufio" + // "strings" + "strconv" + "errors" +) + +// Todo: push loaded data to this struct +type carDiagData struct { + dataTimestamp string + engineRpm float64 + fuelFlow float64 + intakeAirTemp uint + speedObd uint + massAirFlow float64 + throttlePos float64 + voltageObd float64 + engineCoolTemp uint +} + +// Format date +func ConvertStrToDate (inStr string) (string, error) { + // Timestamp layout of data in CSV file + timestampLayoutFile := "Mon Jan 02 15:04:05 MST 2006" + // Timestamp layout to output + timestampLayoutOut := "2006-01-02 15:04:05" + var timestampOut string + + timestamp, err := time.Parse(timestampLayoutFile, inStr) + if err != nil { + return "", errors.New("Can't convert string to date!") + } else { + timestampOut = timestamp.Format(timestampLayoutOut) + return timestampOut, nil + } +} + +// String to float +func ConvertStrToFloat (inStr string) (float64, error) { + flt, err := strconv.ParseFloat(inStr, 64) + if err != nil { + return 0, errors.New("Can't convert string to float!") + } else { + return flt, nil + } +} + +// Convert string to integer +func ConvertStrToInt(inStr string) (int, error) { + intg, err := strconv.Atoi(inStr) + if err != nil { + return 0, errors.New("Can't convert string to int!") + } else { + return intg, nil + } +} + + +func main() { + currentTime := time.Now() + inputFileName := "trackLog-sample.csv" + + fmt.Printf("File to process: %s\n", inputFileName) + fmt.Printf("Started: %s.\n", currentTime.Format("2006-01-02 15:04:05.000 (MST Z07:00)")) + fmt.Println("--------------------") + + csvFile, err := os.Open(inputFileName) + // Close file in the end of main + defer csvFile.Close() + if err != nil { + fmt.Printf("There was an error opening file: %s.\n", err) + return + } + + // Read CSV file + fileReader := csv.NewReader(csvFile) + // Reader options + // - Field delimiter + fileReader.Comma = ',' + // - Each record field count must match header (0) record field count + fileReader.FieldsPerRecord = 0 + // / Reader options + + recordCount := 0 + + for { + // Read one line at one iteration + record, err := fileReader.Read() + + // If end of file, stop reading file + if err == io.EOF { + break + } + + // If error in record, stop reading file + if err != nil { + // log.Fatal(err) + fmt.Printf("Error in record: %s.\n", err) + break + } + + // Perform actions with record + // Record is an array of fields + + // If it is first record, treat it as header + if recordCount == 0 { + fmt.Printf("Fields: %d\n\n", len(record)) + fmt.Printf("\n---------------------------\n") + + // Iterate through record elements (fields) + fmt.Printf("|") + // for i := 0; i < len(record); i++ { + // For experimenting only output 4 fiels + for i := 0; i < 4; i++ { + fmt.Printf(" %s |", record[i]) + } + + fmt.Printf("\n---------------------------\n") + } else { + // Process data record (not header) + + + field0, err := ConvertStrToDate(record[0]) + if err != nil { + // Instead of returned error output question mark + fmt.Printf("(?)") + } else { + fmt.Printf("(%s) ", field0) + } + + field1, err := ConvertStrToFloat(record[1]) + if err != nil { + fmt.Printf("?.?? | ") + } else { + fmt.Printf("%.2f | ", field1) + } + + field2, err := ConvertStrToFloat(record[2]) + if err != nil { + fmt.Printf("?.?? | ") + } else { + fmt.Printf("%.2f | ", field2) + } + + // Convert string (inputString) to integer (inputInt) + field3, err := ConvertStrToInt(record[3]) + if err != nil { + // fmt.Printf("%s", err) + // Instead of ConvertStrToInt returned error output question mark + fmt.Printf("? |") + } else { + fmt.Printf("%d |", field3) + } + + + fmt.Printf("\n") + // fmt.Println(record) + } + // / Perform actions with record + + // Increase counter + recordCount++ + } + + fmt.Println("--------------------") + fmt.Printf("Total records processed: %d.\n", recordCount) + +} diff --git a/trackLog-sample.csv b/trackLog-sample.csv new file mode 100644 index 0000000..72c42d0 --- /dev/null +++ b/trackLog-sample.csv @@ -0,0 +1,30 @@ +GPS Time,Engine RPM(rpm),Fuel flow rate/hour(l/hr),Intake Air Temperature(°C),Speed (OBD)(km/h),Mass Air Flow Rate(g/s),Throttle Position(Manifold)(%),Voltage (OBD Adapter)(V),Engine Coolant Temperature(°C) +Fri Aug 01 08:45:07 EEST 2014,934.25,1.33029199,23,0,3.6400001,12.15686321,13.80000019,38 +Fri Aug 01 08:45:08 EEST 2014,1126.5,1.51302421,23,0,4.13999987,13.33333397,13.89999962,39 +Fri Aug 01 08:45:09 EEST 2014,721,1.37049305,23,0,3.75,12.54901981,13,39 +Fri Aug 01 08:45:10 EEST 2014,734.25,1.21334314,23,4,3.31999993,12.15686321,13.89999962,39 +Fri Aug 01 08:45:11 EEST 2014,997.5,1.18776059,23,7,3.25,12.15686321,13.89999962,39 +Fri Aug 01 08:45:12 EEST 2014,1153,1.27181745,23,6,3.48000002,12.15686321,13.89999962,39 +Fri Aug 01 08:45:13 EEST 2014,825,1.19872451,23,5,3.27999997,12.15686321,12.60000038,40 +Fri Aug 01 08:45:14 EEST 2014,664.75,1.14755952,23,7,3.1400001,12.15686321,13.89999962,40 +Fri Aug 01 08:45:15 EEST 2014,724,1.31201851,23,9,3.58999991,12.54901981,13.80000019,40 +Fri Aug 01 08:45:16 EEST 2014,763.25,1.53495216,23,9,4.19999981,14.50980377,13.80000019,40 +Fri Aug 01 08:45:17 EEST 2014,911.5,1.76154041,23,10,4.82000017,13.72549057,13.89999962,40 +Fri Aug 01 08:45:18 EEST 2014,1064,2.27684569,23,12,6.23000002,17.25490189,13.80000019,40 +Fri Aug 01 08:45:19 EEST 2014,1222.5,2.92737317,23,14,8.01000023,18.4313736,13.89999962,41 +Fri Aug 01 08:45:20 EEST 2014,1434.25,1.30105472,23,18,3.55999994,12.15686321,13.89999962,41 +Fri Aug 01 08:45:21 EEST 2014,1148.25,1.20237923,23,18,3.28999996,12.15686321,13.30000019,41 +Fri Aug 01 08:45:22 EEST 2014,1053.75,1.15852344,23,18,3.17000008,12.15686321,13.80000019,41 +Fri Aug 01 08:45:23 EEST 2014,1066.25,1.19141519,23,13,3.25999999,12.15686321,13.89999962,41 +Fri Aug 01 08:45:24 EEST 2014,806.25,1.96985531,23,12,5.38999987,12.54901981,13.89999962,42 +Fri Aug 01 08:45:25 EEST 2014,1028.75,2.2439537,23,11,6.13999987,16.47058868,13.89999962,42 +Fri Aug 01 08:45:26 EEST 2014,1245.25,2.5948,23,15,7.0999999,16.86274529,13.80000019,42 +Fri Aug 01 08:45:27 EEST 2014,1362.5,3.75697803,23,17,10.27999973,22.35294151,13.89999962,42 +Fri Aug 01 08:45:28 EEST 2014,1687.5,4.11147881,23,19,11.25,24.31372643,14,43 +Fri Aug 01 08:45:29 EEST 2014,1427.25,1.21334302,23,23,3.31999993,12.15686321,13.89999962,43 +Fri Aug 01 08:45:30 EEST 2014,1294.5,3.55597234,23,23,9.72999954,25.49019623,13.89999962,43 +Fri Aug 01 08:45:31 EEST 2014,1445.25,3.38420391,23,25,9.26000023,19.6078434,13.89999962,43 +Fri Aug 01 08:45:32 EEST 2014,1566.25,2.43034101,23,29,6.6500001,17.25490189,13.89999962,44 +Fri Aug 01 08:45:33 EEST 2014,1584.25,2.60210919,23,29,7.11999989,16.86274529,13.89999962,44 +Fri Aug 01 08:45:34 EEST 2014,1653,3.59617376,23,29,9.84000015,20.7843132,14,44 +Fri Aug 01 08:45:35 EEST 2014,1755.25,2.66058373,23,29,7.28000021,18.8235302,14,45