-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
115 lines (100 loc) · 2.64 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
package main
import (
"encoding/csv"
"flag"
"fmt"
"math"
"os"
"path/filepath"
"strconv"
"github.com/takatoh/jscale/intensity"
"github.com/takatoh/jscale/lpgm"
"github.com/takatoh/seismicwave"
)
const (
progVersion = "v1.6.1"
)
func main() {
progName := filepath.Base(os.Args[0])
flag.Usage = func() {
fmt.Fprintf(os.Stderr,
`Usage:
%s [-long-period] <wavefile.csv>
%s [-long-period] -jma <wavefile.txt>
%s [-long-period] -knet <wavefile>
%s [-long-period] -fixed <input.toml>
Options:
`, progName, progName, progName, progName)
flag.PrintDefaults()
}
opt_jma := flag.Bool("jma", false, "Load JMA waves.")
opt_knet := flag.Bool("knet", false, "Load KNET waves.")
opt_fixed := flag.Bool("fixed", false, "Load fixed format waves.")
opt_lpgm := flag.Bool("long-period", false, "Caclulate intensity scale on long-period ground motion.")
opt_spectrum := flag.Bool("spectrum", false, "Output spectrum to file.")
opt_version := flag.Bool("version", false, "Show version.")
flag.Parse()
if *opt_version {
fmt.Println(progVersion)
os.Exit(0)
}
filename := flag.Arg(0)
var waves []*seismicwave.Wave
var err error
if *opt_jma {
waves, err = seismicwave.LoadJMA(filename)
} else if *opt_knet {
waves, err = seismicwave.LoadKNETSet(filename)
} else if *opt_fixed {
waves, err = seismicwave.LoadFixedFormatWithTOML(filename)
} else {
waves, err = seismicwave.LoadCSV(filename)
}
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
}
if *opt_lpgm {
Sva := lpgm.Calc(waves[0], waves[1])
maxSva := maxValue(Sva)
if *opt_spectrum {
outputSpectra(lpgm.Periods(), Sva, "spec-lpgm.csv")
}
fmt.Printf("絶対速度応答スペクトルの最大値 %.1f cm/sec\n", maxSva)
fmt.Println(lpgm.Scale(maxSva))
} else {
I := intensity.Calc(waves[0], waves[1], waves[2])
fmt.Printf("計測震度 %.1f\n", I)
fmt.Println(intensity.Scale(I))
}
}
func outputSpectra(periods, responses []float64, filename string) {
f, err := os.Create(filename)
if err != nil {
panic("Error! Can not open the file")
}
defer f.Close()
w := csv.NewWriter(f)
defer w.Flush()
header := []string{"period", "Sva"}
err = w.Write(header)
if err != nil {
panic("Error! Can not write to the file")
}
for i := 0; i < len(periods); i++ {
period := strconv.FormatFloat(periods[i], 'f', 1, 64)
response := strconv.FormatFloat(responses[i], 'f', 3, 64)
record := []string{period, response}
err := w.Write(record)
if err != nil {
panic("Error! Can not write to the file")
}
}
}
func maxValue(values []float64) float64 {
max := 0.0
for i := 0; i < len(values); i++ {
max = math.Max(max, values[i])
}
return max
}