-
Notifications
You must be signed in to change notification settings - Fork 1
/
listener.go
106 lines (93 loc) · 2.59 KB
/
listener.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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
func activateCondaEnv(envName string) *exec.Cmd {
var cmd *exec.Cmd
switch runtime.GOOS {
case "windows":
cmd = exec.Command("cmd", "/c", "conda", "activate", envName)
default:
cmd = exec.Command("bash", "-c", "conda activate "+envName)
}
return cmd
}
func main() {
condaEnvName := "nefis2nc"
folderPath := "D:/Sea4cast/Sea4cast/Delft3D-sample/For Hengkek/Sample setup and simulation/"
outputPath := filepath.Join(".", "tests/testdata")
// Activate Conda environment
activateCmd := activateCondaEnv(condaEnvName)
activateCmd.Stdout = os.Stdout
activateCmd.Stderr = os.Stderr
err := activateCmd.Run()
if err != nil {
fmt.Println("Error activating Conda environment:", err)
return
}
var (
trihDatFile string
trihDefFile string
trimDatFile string
trimDefFile string
outputTrihFile = filepath.Join(outputPath, "test-trih.nc")
outputTrimFile = filepath.Join(outputPath, "test-trim.nc")
)
// Iterate through the files in the folder
filepath.Walk(folderPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Println(err)
return err
}
if info.IsDir() {
return nil
}
filename := info.Name()
if (strings.HasPrefix(filename, "trih") || strings.HasPrefix(filename, "trim")) &&
(strings.HasSuffix(filename, ".dat") || strings.HasSuffix(filename, ".def")) {
switch {
case strings.HasSuffix(filename, ".dat"):
if strings.HasPrefix(filename, "trih") {
trihDatFile = path
} else if strings.HasPrefix(filename, "trim") {
trimDatFile = path
}
case strings.HasSuffix(filename, ".def"):
if strings.HasPrefix(filename, "trih") {
trihDefFile = path
} else if strings.HasPrefix(filename, "trim") {
trimDefFile = path
}
}
}
return nil
})
fmt.Println("trihDatFile:", trihDatFile)
fmt.Println("trihDefFile:", trihDefFile)
fmt.Println("trimDatFile:", trimDatFile)
fmt.Println("trimDefFile:", trimDefFile)
// Check if trihDatFile and trihDefFile are not empty
if trihDatFile != "" && trihDefFile != "" {
cmd := exec.Command("python", "trih2nc.py", trihDatFile, trihDefFile, outputTrihFile)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
fmt.Println("Error running trih2nc.py:", err)
}
}
if trimDatFile != "" && trimDefFile != "" {
cmd := exec.Command("python", "trim2nc.py", trimDatFile, trimDefFile, outputTrimFile)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
fmt.Println("Error running trim2nc.py:", err)
}
}
}