-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
163 lines (137 loc) · 3.42 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"bufio"
"encoding/csv"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strconv"
"strings"
)
func processCSVFiles(directory string) {
files, err := os.ReadDir(directory)
if err != nil {
log.Fatal(err)
}
for _, file := range files {
if strings.HasSuffix(file.Name(), ".a00") {
processFile(filepath.Join(directory, file.Name()))
}
}
}
func processFile(filepath string) {
matrix := [][]float64{}
// insideBad := false
// zero := []float64{}
// storage := [][]float64{}
file, err := os.Open(filepath)
if err != nil {
log.Fatal(err)
}
defer file.Close()
reader := csv.NewReader(file)
reader.Comma = '\t'
reader.FieldsPerRecord = -1 // Allow variable number of fields
data, err := reader.ReadAll()
data_copy := data
if err != nil {
log.Fatal(err)
}
//interpolation
for i, row := range data {
// Here, row is a slice of strings (a row in the CSV file)
if i >= 7 {
dummy, _ := convertToFloats(row)
matrix = append(matrix, dummy)
}
}
// go through all the columns
for i := 0; i < len(matrix[0]); i++ {
// read a column
column := make([]float64, len(matrix))
for j := 0; j < len(matrix); j++ {
column[j] = matrix[j][i]
}
column = interpolate(column)
// copy interpolated column back to matrix
for j := 0; j < len(matrix); j++ {
matrix[j][i] = column[j]
}
}
// copy interpolated matrix to str data
for i, dummy := range matrix {
dummyStr := make([]string, len(dummy))
for i, v := range dummy {
dummyStr[i] = strconv.FormatFloat(v, 'f', -1, 64)
}
if (i + 7) < len(data) {
data_copy[i+7] = dummyStr
}
}
newFilepath := "processed_" + filepath
outputFile, err := os.Create(newFilepath)
if err != nil {
log.Fatal(err)
}
defer outputFile.Close()
writer := csv.NewWriter(outputFile)
writer.Comma = '\t'
err = writer.WriteAll(data_copy)
if err != nil {
log.Fatal(err)
}
writer.Flush()
}
func addNewLineAfterLine(directory string, lineNumber int) {
files, err := ioutil.ReadDir(directory)
if err != nil {
fmt.Printf("Error reading directory %s: %v\n", directory, err)
return
}
for _, file := range files {
// Skip if not a file or doesn't have the right extension
if file.IsDir() || !strings.HasPrefix(file.Name(), "processed_") {
continue
}
// Open the original file
originalPath := filepath.Join(directory, file.Name())
original, err := os.Open(originalPath)
if err != nil {
fmt.Printf("Error opening file %s: %v\n", originalPath, err)
continue
}
// Create a buffer to hold the file contents
var buffer strings.Builder
// Create a scanner to read the file line by line
scanner := bufio.NewScanner(original)
currentLine := 1
for scanner.Scan() {
// Write the line to the buffer
buffer.WriteString(scanner.Text() + "\n")
// If it's the desired line, write an additional newline
if currentLine == lineNumber {
buffer.WriteString("\n")
}
currentLine++
}
if err := scanner.Err(); err != nil {
fmt.Printf("Error reading file %s: %v\n", originalPath, err)
}
original.Close()
// Open the original file again, this time for writing
original, err = os.OpenFile(originalPath, os.O_WRONLY|os.O_TRUNC, 0666)
if err != nil {
fmt.Printf("Error opening file %s for writing: %v\n", originalPath, err)
continue
}
// Write the buffer contents to the file
original.WriteString(buffer.String())
original.Close()
}
}
func main() {
processCSVFiles(".")
addNewLineAfterLine(".", 3)
}