-
Notifications
You must be signed in to change notification settings - Fork 41
/
utilities.go
189 lines (173 loc) · 4.37 KB
/
utilities.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
Copyright (C) 2012 the InMAP authors.
This file is part of InMAP.
InMAP is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
InMAP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with InMAP. If not, see <http://www.gnu.org/licenses/>.
*/
package aep
import (
"fmt"
"math"
"strings"
"github.com/ctessum/unit"
)
// MatchCode finds code in matchmap.
// For cases where a specific code needs to be matched with a more
// general code. For instance, if code is "10101" and matchmap is
// {"10102":"xxx","10100":"yyyy"}, "10100" will be returned as the
// closest match to the input code. "10102" will never be returned,
// even if the "10100" item didn't exist. Returns an error if there
// is no match.
func MatchCode(code string, matchmap map[string]interface{}) (matchedCode string, matchVal interface{}, err error) {
var ok bool
l := len(code)
for i := l - 1; i >= -1; i-- {
matchedCode = code[0:i+1] + strings.Repeat("0", l-i-1)
matchVal, ok = matchmap[matchedCode]
if ok {
return
}
}
err = fmt.Errorf("No matching code for %v; available codes are %v", code, matchmap)
return
}
// MatchCodeDouble finds code1 and code2 in matchmap.
func MatchCodeDouble(code1, code2 string, matchmap map[string]map[string]interface{}) (matchedCode1, matchedCode2 string, matchVal interface{}, err error) {
l1 := len(code1)
l2 := len(code2)
for i := l1 - 1; i >= -1; i-- {
matchedCode1 = code1[0:i+1] + strings.Repeat("0", l1-i-1)
match1, ok := matchmap[matchedCode1]
if ok {
for i := l2 - 1; i >= -1; i-- {
matchedCode2 = code2[0:i+1] + strings.Repeat("0", l2-i-1)
matchVal, ok = match1[matchedCode2]
if ok {
return
}
}
}
}
err = fmt.Errorf("No matching codes for %v, %v", code1, code2)
return
}
// IsStringInArray returns whether s is a member of a.
func IsStringInArray(a []string, s string) bool {
for _, val := range a {
if val == s {
return true
}
}
return false
}
func absBias(a, b float64) (o float64) {
o = math.Abs(a-b) / b
return
}
// Country represents a country where emissions occur.
type Country int
// These are the currently supported countries.
const (
USA Country = 0
Canada = 1
Mexico = 2
Cuba = 3
Bahamas = 4
Haiti = 5
DominicanRepublic = 6
Global = 7
Unknown = -1
)
func (c Country) String() string {
switch c {
case USA:
return "USA"
case Canada:
return "CA"
case Mexico:
return "MEXICO"
case Cuba:
return "CUBA"
case Bahamas:
return "BAHAMAS"
case Haiti:
return "HAITI"
case DominicanRepublic:
return "DOMINICANREPUBLIC"
case Global:
return "GLOBAL"
default:
panic(fmt.Errorf("Unknown country %d", c))
}
}
func getCountryCode(country Country) string {
return fmt.Sprintf("%d", country)
}
func getCountryFromID(code string) Country {
switch code {
case "0":
return USA
case "1":
return Canada
case "2":
return Mexico
case "3":
return Cuba
case "4":
return Bahamas
case "5":
return Haiti
case "6":
return DominicanRepublic
case "7":
return Global
default:
err := fmt.Errorf("Unknown country code %v.", code)
panic(err)
}
}
func countryFromName(name string) (Country, error) {
switch name {
case "USA", "US":
return USA, nil
case "CANADA", "CA", "CAN":
return Canada, nil
case "MEXICO", "MEX", "MX":
return Mexico, nil
case "CUBA":
return Cuba, nil
case "BAHAMAS":
return Bahamas, nil
case "HAITI":
return Haiti, nil
case "DOMINICANREPUBLIC":
return DominicanRepublic, nil
case "GLOBAL":
return Global, nil
default:
return Unknown, fmt.Errorf("Unkown country '%s'", name)
}
}
// EmissionsTotal returns the total combined emissions in recs.
func EmissionsTotal(recs []Record) map[Pollutant]*unit.Unit {
o := make(map[Pollutant]*unit.Unit)
for _, r := range recs {
t := r.Totals()
for p, e := range t {
if o[p] == nil {
o[p] = e.Clone()
} else {
o[p].Add(e)
}
}
}
return o
}