-
Notifications
You must be signed in to change notification settings - Fork 0
/
mesurades.go
437 lines (356 loc) · 14.1 KB
/
mesurades.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
package meteocat
import (
"encoding/json"
"fmt"
"net/http"
"strings"
)
var url string // helper variable to hold url values
var sFlag bool // helper variable to check where to handle JSON that varies between an array or a single item
// Coordenades struct holds georeference information of the stations
type Coordenades struct {
Latitud float64 `json:"latitud"` // Latitude expressed in decimal degrees. WSG84 reference system
Longitud float64 `json:"Longitud"` // Longitude expressed in decimal degrees. WSG84 reference system
}
// Municipi struct holds information on which municipi is located the station.
type Municipi struct {
Codi string `json:"codi"` // INE code of the municipi
Nom string `json:"nom"` // Name of the municipi
}
// Comarca struct holds information on which Comarca is located the station.
type Comarca struct {
Codi int `json:"codi"` // Comarca identification code
Nom string `json:"nom"` // Name of the comarca
}
// Provincia struct holds information on which Provincia is located the station.
type Provincia struct {
Codi int `json:"codi"` // Provincia identification code
Nom string `json:"nom"` // Name of the provincia
}
// Xarxa struct holds information on which Xarxa is located the station. The xarxa field
// refers to the stations of the Network of Automatic Meteorological Stations (XEMA) of Catalonia
type Xarxa struct {
Codi int `json:"codi"` // Xarxa identification code
Nom string `json:"nom"` // Name of the Network
}
// Aggregation of fields and structs to unmarshal the responses
type MetadadesEstacions struct {
Codi string `json:"codi"` // Identification code for each automatic weather station (EMA)
Nom string `json:"nom"` // Name of the EMA
Tipus string `json:"tipus"` // Type of station typically automatic
Coordenades Coordenades `json:"coordenades"` // Georeference data of the station
Emplacament string `json:"emplacament"` // Descriptive name of where the station is located e.g Planters Gusi, ctra. antiga de València, km 14
Altitud float64 `json:"altitud"` // Altitude in meters of the station above the sea level.
Municipi Municipi `json:"municipi"` // Municipi
Comarca Comarca `json:"comarca"` // Comarca
Provincia Provincia `json:"provincia"` // Provincia
Xarxa Xarxa `json:"xarxa"` // Network tipically XEMA
Estats Estats `json:"estats"`
}
// MetadadesVariable is an aggregation of fields to hold the metadata asociated with
// the variables of the Network of Automatic Meteorological Stations (XEMA), integrated into the Network of Meteorological
// Equipment of the Generalitat de Catalunya (Xemec), of the Meteorological Service of Catalonia. Each variable is identified by a code.
type MetadadesVariable struct {
Codi int `json:"codi"` // Identifier code of the variable
Nom string `json:"nom"` // Identifier name of the variable
Unitats string `json:"unitats"` // Unit of measurement of variables e.g T(ºC),...
Acronim string `json:"acronim"` // Acronym of the variable
Tipus string `json:"tipus"` // Type of variable
Decimals int `json:"decimals"` // Number of decimal numbers
}
// MetadadesVariables is a slice which holds the metadata of all variables
type MetadadesVariables []struct{ MetadadesVariable }
// Estats struct holds information of station code when initiated and or finalized.
type Estats []struct {
Codi int `json:"codi"`
DataInici string `json:"dataInici"`
DataFi interface{} `json:"dataFi"`
}
// BasesTemporals TODO
type BasesTemporals []struct {
Codi string `json:"codi"`
DataInici string `json:"dataInici"`
DataFi string `json:"dataFi"`
}
// MetadadesVariablesEstacio is a slice which holds the
// variables metadata of all the data registered bu a station
type MetadadesVariablesEstacio []struct{ MetadadesVariableEstacio }
// MetadadesVariableEstacio is an agreggate type to hold the metadata of the variable data registered in a particular station
type MetadadesVariableEstacio struct {
MetadadesVariable
Estats
BasesTemporals
}
// Lectura is an aggregate type which represents the data registered in the station. This value with a code represents
// a variable, e.g {"codi":5,"lectures":[{"data":"2021-01-06T10:00Z","dataExtrem":"2021-01-06T10:24Z","valor":8.7,"estat":" ","baseHoraria":"SH"}]}
type Lectura struct {
Data string `json:"data"`
Valor float64 `json:"valor"`
Estat string `json:"estat"`
BaseHoraria string `json:"baseHoraria"`
}
// Variable is an agreggate type which represents the variable data registered in a station.
type Variable struct {
Codi int `json:"codi"`
Lectures []Lectura `json:"lectures"`
}
// Measurements holds the measurements done in a station
type Measurements []struct {
Codi string `json:"codi"`
Variables []Variable `json:"variables"`
}
// Mesurades holds all the data representations to unmarshall the API responses
type Mesurades struct {
Variable
Measurements
MetadadesVariablesEstacio
MetadadesVariableEstacio
MetadadesVariables
Key string
CodiEstacio string // ?
CodiVariable string // ?
*Settings
}
// NewMesurades returns a new MesuradesData pointer with the supplied parameters
func NewMesurades(key string) (*Mesurades, error) {
c := &Mesurades{
Settings: NewSettings(),
}
c.Key, _ = setKey(key)
return c, nil
}
// Returns information of a variable for all stations for a given day, if the station code is reported,
// returns the data of the variable for the requested station.
// The API resource is /variables/mesurades/{codi_variable}/{any}/{mes}/{dia}?codiEstacio={codi_estacio} where the
// parameters `codi_variable`, `any`, `mes`, `dia` are mandatory and `codi_estacio` is optional.
// Request example: https://api.meteo.cat/xema/v1/variables/mesurades/32/2017/03/27?codiEstacio=UG
func (m *Mesurades) MeasurementByDay(p *Parameters) error {
if ValidCodiVariable(p.codiVariable) {
m.CodiVariable = p.codiVariable
} else {
return errVariableUnavailable
}
if p.codiEstacio != "" {
p.codiEstacio = strings.ToUpper(p.codiEstacio)
if ValidCodiEstacio(p.codiEstacio) {
m.CodiEstacio = p.codiEstacio
sFlag = true
url = fmt.Sprintf(baseURL, fmt.Sprintf("/variables/mesurades/%s/%s/%s/%s?codiEstacio=%s", p.codiVariable, p.Any, p.Mes, p.Dia, p.codiEstacio))
} else {
return errEstacioUnavailable
}
} else {
url = fmt.Sprintf(baseURL, fmt.Sprintf("/variables/mesurades/%s/%s/%s/%s", p.codiVariable, p.Any, p.Mes, p.Dia))
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
req.Header.Add("X-Api-Key", m.Key)
resp, err := m.client.Do(req)
if err != nil {
fmt.Println(err)
return err
}
defer resp.Body.Close()
if sFlag == true {
if err = json.NewDecoder(resp.Body).Decode(&m.Variable); err != nil {
return err
}
} else {
if err = json.NewDecoder(resp.Body).Decode(&m.Measurements); err != nil {
return err
}
}
// print status line and headers
return nil
}
// Returns information of all variables for a station for a given day.
// The API resource is /estacions/mesurades/{codiEstacio}/{any}/{mes}/{dia} where the parameters `codiEstacio` `any`
// `mes` and `dia` are all mandatory. Request example: https://api.meteo.cat/xema/v1/estacions/mesurades/CC/2020/06/16
func (m *Mesurades) MeasurementAllByStation(p *Parameters) error {
if ValidCodiEstacio(p.codiEstacio) {
m.CodiEstacio = strings.ToUpper(p.codiEstacio)
} else {
return errEstacioUnavailable
}
req, err := http.NewRequest("GET", fmt.Sprintf(baseURL, fmt.Sprintf("/estacions/mesurades/%s/%s/%s/%s", strings.ToUpper(p.codiEstacio), p.Any, p.Mes, p.Dia)), nil)
if err != nil {
return err
}
req.Header.Add("X-Api-Key", m.Key)
resp, err := m.client.Do(req)
if err != nil {
fmt.Println(err)
return err
}
defer resp.Body.Close()
if err = json.NewDecoder(resp.Body).Decode(&m.Measurements); err != nil {
return err
}
return nil
}
// Returns the last measurement of the last 4 hours for all stations of a variable, filtered by station if indicated
// The API resource is /variables/mesurades/{codi_variable}/ultimes?codiEstacio={codi_estacio} where `codi_variable` is
// mandatory and `codi_estacio` is optional. Request example: https://api.meteo.cat/xema/v1/variables/mesurades/5/ultimes?codiEstacio=UG
func (m *Mesurades) MeasurementLast(p *Parameters) error {
if ValidCodiVariable(p.codiVariable) {
m.CodiVariable = p.codiVariable
} else {
return errVariableUnavailable
}
if p.codiEstacio != "" {
p.codiEstacio = strings.ToUpper(p.codiEstacio)
if ValidCodiEstacio(p.codiEstacio) {
m.CodiEstacio = p.codiEstacio
url = fmt.Sprintf(baseURL, fmt.Sprintf("/variables/mesurades/%s/ultimes?codiEstacio=%s", p.codiVariable, p.codiEstacio))
sFlag = true
} else {
return errEstacioUnavailable
}
} else {
url = fmt.Sprintf(baseURL, fmt.Sprintf("/variables/mesurades/%s/ultimes", p.codiVariable))
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
req.Header.Add("X-Api-Key", m.Key)
resp, err := m.client.Do(req)
if err != nil {
fmt.Println(err)
return err
}
defer resp.Body.Close()
if sFlag == true {
if err = json.NewDecoder(resp.Body).Decode(&m.Variable); err != nil {
return err
}
} else {
if err = json.NewDecoder(resp.Body).Decode(&m.Measurements); err != nil {
return err
}
}
return nil
}
// Returns metadata of all variables measured by the station with code specified in the URL, filtered by status and date if specified
// The API resource is /estacions/{codiEstacio}/variables/mesurades/metadades?estat={estat}&data={data} where `estat` and `date` are optional.
// The `estat` parameter describes the state of the station and it can have one of the following values [ope, des, bte]. These values means
// "Operativa", "Baixa temporal" and "Desmantellada" respectively. See https://apidocs.meteocat.gencat.cat/documentacio/dades-de-la-xema/ for more information.
// Note that the 'data' and 'estat' parameters are required together in order to filter the metadata
// Request example https://api.meteo.cat/xema/v1/estacions/UG/variables/mesurades/metadades?estat=ope&data=2017-03-27Z
func (m *Mesurades) MeasurementMetadataAllByStation(p *Parameters) error {
if ValidCodiEstacio(p.codiEstacio) {
m.CodiEstacio = p.codiEstacio
} else {
return errVariableUnavailable
}
dataOk := ValidData(p.Data)
if p.codiEstat != "" && dataOk == true {
p.codiEstat = strings.ToLower(p.codiEstat)
if ValidCodiEstat(p.codiEstat) {
sFlag = true
url = fmt.Sprintf(baseURL, fmt.Sprintf("/estacions/%s/variables/mesurades/metadades?estat=%s&data=%s-%s-%sZ", p.codiEstacio, p.codiEstat, p.Any, p.Mes, p.Dia))
} else {
return errEstacioUnavailable
}
} else {
url = fmt.Sprintf(baseURL, fmt.Sprintf("/estacions/%s/variables/mesurades/metadades", p.codiEstacio))
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
req.Header.Add("X-Api-Key", m.Key)
resp, err := m.client.Do(req)
if err != nil {
fmt.Println(err)
return err
}
defer resp.Body.Close()
if sFlag == true {
if err = json.NewDecoder(resp.Body).Decode(&m.MetadadesVariableEstacio); err != nil {
return err
}
} else {
if err = json.NewDecoder(resp.Body).Decode(&m.MetadadesVariablesEstacio); err != nil {
return err
}
}
// print status line and headers
return nil
}
// MeasurementMetadataByStation Returns the metadata of the variable with the code specified in the URL that measures the station with the code indicated in the URL
// The API resource is /estacions/{codiEstacio}/variables/mesurades/{codiVariable}/metadades where the parameters 'codiEstacio' and 'codiVariable' are mandatory
// Request example https://api.meteo.cat/xema/v1/estacions/UG/variables/mesurades/3/metadades
func (m *Mesurades) MeasurementMetadataByStation(p *Parameters) error {
if ValidCodiVariable(p.codiVariable) {
m.CodiVariable = p.codiVariable
} else {
return errVariableUnavailable
}
if ValidCodiEstacio(p.codiEstacio) {
m.CodiEstacio = p.codiEstacio
} else {
return errEstacioUnavailable
}
req, err := http.NewRequest("GET", fmt.Sprintf(baseURL, fmt.Sprintf("/estacions/%s/variables/mesurades/%s/metadades", p.codiEstacio, p.codiVariable)), nil)
if err != nil {
return err
}
req.Header.Add("X-Api-Key", m.Key)
resp, err := m.client.Do(req)
if err != nil {
fmt.Println(err)
return err
}
defer resp.Body.Close()
if err = json.NewDecoder(resp.Body).Decode(&m.MetadadesVariableEstacio); err != nil {
return err
}
return nil
}
// Returns the metadata of all variables regardless of the stations at which they are measured. The API resource is /variables/mesurades/metadades and
// there are no parameters. Request example https://api.meteo.cat/xema/v1/variables/mesurades/metadades
func (m *Mesurades) MeasurementMetadataAll() error {
req, err := http.NewRequest("GET", fmt.Sprintf(baseURL, fmt.Sprintf("/variables/mesurades/metadades")), nil)
if err != nil {
return err
}
req.Header.Add("X-Api-Key", m.Key)
resp, err := m.client.Do(req)
if err != nil {
fmt.Println(err)
return err
}
defer resp.Body.Close()
if err = json.NewDecoder(resp.Body).Decode(&m.MetadadesVariables); err != nil {
return err
}
return nil
}
// Returns the metadata of the variable with code indicated in the URL, regardless of the stations in which they are measured.
// The API resource is /variables/mesurades/{codi_variable}/metadades where the parameter 'codi_variable' is mandatory.
// Request example: https://api.meteo.cat/xema/v1/variables/mesurades/1/metadades
func (m *Mesurades) MeasurementMetadataUnique(p *Parameters) error {
if ValidCodiVariable(p.codiVariable) {
m.CodiVariable = p.codiVariable
} else {
return errVariableUnavailable
}
req, err := http.NewRequest("GET", fmt.Sprintf(baseURL, fmt.Sprintf("/variables/mesurades/%s/metadades", p.codiVariable)), nil)
if err != nil {
return err
}
req.Header.Add("X-Api-Key", m.Key)
resp, err := m.client.Do(req)
if err != nil {
fmt.Println(err)
return err
}
defer resp.Body.Close()
if err = json.NewDecoder(resp.Body).Decode(&m.MetadadesVariable); err != nil {
return err
}
return nil
}