-
Notifications
You must be signed in to change notification settings - Fork 17
/
eco_scenario.go
171 lines (153 loc) · 4.1 KB
/
eco_scenario.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
164
165
166
167
168
169
170
171
package endpoints
import (
"errors"
"fmt"
"github.com/OpenTreeMap/otm-ecoservice/eco"
"github.com/OpenTreeMap/otm-ecoservice/ecorest/cache"
"strconv"
"time"
)
type ScenarioPostData struct {
Region string
Instance_id string
Years int
Scenario_trees []ScenarioTree
}
type ScenarioTree struct {
Otmcode string
Species_id int
Region string
Diameters []float64
}
type Scenario struct {
Total map[string]float64
Years []map[string]float64
}
// Take an array of prospective trees where each tree contains
// an array of diamaters, one for each year the tree is alive,
// and return an array of eco calulations, one for each year
//
// Trees will die of as part of the scenario, so the
// `diameters` arrays for the trees may have different
// lengths. Trees that die may be replaced with other trees,
// so there will be trees that appear in the scenario at t >
// 0, so the `diameters` array may have initial elements set
// to 0.
//
// Specifying a "region" for an individual tree will override the
// scenario-level "region" value.
//
// The "years" parameter must be >= the length of the longest
// "diameters" array under "scenario_trees".
//
// Request (with bogus example parameters):
//
// POST /eco_scenario.json
//
// {
// "region": "NoEastXXX",
// "instance_id": 1,
// "years": 3
// "scenario_trees": [
// {
// "otmcode": "CACO",
// "species_id": 1,
// "region": "NoEastXXX",
// "diameters": [1, 1.3, 1.7]
// }
// ]
// }
//
// Response (with bogus example values):
//
// {
// "Years": [
// {
// "aq_nox_avoided": 0.01548490,
// "aq_nox_dep": 0.00771784,
// "aq_pm10_avoided": 0.00546863
// },
// {
// "aq_nox_avoided": 0.02548420,
// "aq_nox_dep": 0.01973722,
// "aq_pm10_avoided": 0.00676823
// },
// {
// "aq_nox_avoided": 0.05484902,
// "aq_nox_dep": 0.04774471,
// "aq_pm10_avoided": 0.00946822
// }
// ],
// "Total": {
// "aq_nox_avoided": ... ,
// "aq_nox_dep": ... ,
// "aq_pm10_avoided": ...
// }
// }
func EcoScenarioPOST(cache *cache.Cache) func(*ScenarioPostData) (*Scenario, error) {
return func(data *ScenarioPostData) (*Scenario, error) {
t := time.Now()
scenarioTrees := data.Scenario_trees
scenarioRegion := data.Region
instanceId, err := strconv.Atoi(data.Instance_id)
if err != nil {
return nil, err
}
if len(scenarioRegion) == 0 {
var regions []eco.Region
regions, err = cache.Db.GetRegionsForInstance(
cache.RegionGeometry, instanceId)
if err != nil {
return nil, err
}
if len(regions) == 1 {
scenarioRegion = regions[0].Code
}
}
yearTotals := make([][]float64, data.Years)
grandTotals := make([]float64, len(eco.Factors))
for i := range yearTotals {
yearTotals[i] = make([]float64, len(eco.Factors))
}
for _, tree := range scenarioTrees {
effectiveRegion := scenarioRegion
if len(tree.Region) != 0 {
effectiveRegion = tree.Region
}
factorDataForRegion, found := cache.RegionData[effectiveRegion]
if !found {
return nil, errors.New("No data is available for the iTree region with code " + effectiveRegion)
}
itreecode, err := cache.GetITreeCode(tree.Otmcode,
tree.Species_id, effectiveRegion, instanceId)
if err != nil {
return nil, err
}
for i, diameter := range tree.Diameters {
factorSum := make([]float64, len(eco.Factors))
eco.CalcOneTree(
factorDataForRegion,
itreecode,
diameter,
factorSum)
for j, value := range factorSum {
yearTotals[i][j] += value
grandTotals[j] += value
}
}
}
// The requests are written to stdout like this:
// 2014/07/15 14:06:10 POST /eco_scenario.json
// Indenting the timing report aligns it with the http
// verb on the previous line.
fmt.Println(" ",
int64(time.Since(t)/time.Millisecond), "ms (total)")
years := make([]map[string]float64, data.Years)
for i, a := range yearTotals {
years[i] = eco.FactorArrayToMap(a)
}
return &Scenario{
Total: eco.FactorArrayToMap(grandTotals),
Years: years}, nil
}
}