-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
136 lines (128 loc) · 3.01 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
package main
import (
"encoding/json"
"errors"
"fmt"
"log"
"github.com/usace/cc-go-sdk"
"github.com/usace/cc-go-sdk/plugin"
"github.com/usace/fragility-curves/fragilitycurve"
)
func main() {
fmt.Println("Fragility Curves!")
pm, err := cc.InitPluginManager()
if err != nil {
log.Fatalf("Unable to initialize the plugin manager: %s\n", err)
}
payload := pm.GetPayload()
err = computePayload(payload, pm)
if err != nil {
pm.LogError(cc.Error{
ErrorLevel: cc.ERROR,
Error: err.Error(),
})
}
}
func computePayload(payload cc.Payload, pm *cc.PluginManager) error {
if len(payload.Outputs) != 1 {
err := errors.New("more than one output was defined")
pm.LogError(cc.Error{
ErrorLevel: cc.ERROR,
Error: err.Error(),
})
return err
}
eventConfigurationResourceInfo, err := pm.GetInputDataSource("seeds")
if err != nil {
pm.LogError(cc.Error{
ErrorLevel: cc.ERROR,
Error: err.Error(),
})
return err
}
//read the fragility curve datasource
modelResourceInfo, err := pm.GetInputDataSource("fragilitycurve")
if err != nil {
pm.LogError(cc.Error{
ErrorLevel: cc.ERROR,
Error: err.Error(),
})
return err
}
var fcm fragilitycurve.Model
modelReader, err := pm.FileReader(modelResourceInfo, 0)
if err != nil {
pm.LogError(cc.Error{
ErrorLevel: cc.ERROR,
Error: err.Error(),
})
return err
}
defer modelReader.Close()
err = json.NewDecoder(modelReader).Decode(&fcm)
if err != nil {
pm.LogError(cc.Error{
ErrorLevel: cc.ERROR,
Error: err.Error(),
})
return err
}
var seedSet plugin.SeedSet
var ec plugin.EventConfiguration
eventConfigurationReader, err := pm.FileReader(eventConfigurationResourceInfo, 0)
if err != nil {
pm.LogError(cc.Error{
ErrorLevel: cc.ERROR,
Error: err.Error(),
})
return err
}
defer eventConfigurationReader.Close()
err = json.NewDecoder(eventConfigurationReader).Decode(&ec)
if err != nil {
pm.LogError(cc.Error{
ErrorLevel: cc.ERROR,
Error: err.Error(),
})
return err
}
seedSetName := "fragilitycurveplugin"
seedSet, seedsFound := ec.Seeds[seedSetName]
if !seedsFound {
pm.LogError(cc.Error{
ErrorLevel: cc.ERROR,
Error: fmt.Errorf("no seeds found by name of %v", seedSetName).Error(),
})
return err
}
modelResult, err := fcm.Compute(seedSet.EventSeed, seedSet.RealizationSeed)
if err != nil {
pm.LogError(cc.Error{
ErrorLevel: cc.ERROR,
Error: err.Error(),
})
return err
}
bytes, err := json.Marshal(modelResult)
if err != nil {
pm.LogError(cc.Error{
ErrorLevel: cc.ERROR,
Error: err.Error(),
})
return err
}
fmt.Println(string(bytes))
err = pm.PutFile(bytes, payload.Outputs[0], 0)
if err != nil {
pm.LogError(cc.Error{
ErrorLevel: cc.ERROR,
Error: err.Error(),
})
return err
}
pm.ReportProgress(cc.StatusReport{
Status: cc.SUCCEEDED,
Progress: 100,
})
return nil
}