meteocat

package module
v0.0.0-...-ab06343 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 13, 2023 License: MIT Imports: 5 Imported by: 0

README

Meteocat API Client

Go Report Card MIT License pkg.go.dev

API Rest client implented in Go to send requests and inspect responses of the Meteocat Rest API

This package allows you to use the API provided by Meteocat to retrieve weather data from its weather stations networks. The package is still in development and does not include support for all the API operations. See the TODO section for more information.

What kind of data can I get with Meteocat Go Library ?

Access to forecasts, real-time and historical data from the Meteorological Service of Catalonia

Get started

API key

As Meteocat APIs need a valid API key to allow responses, this library won't work if you don't provide one. This stands for both free and paid (pro) subscription plans. You can signup for a free API key on the Meteocat website. Please notice that both subscriptions plan are subject to requests throttling.

Installation
Build from source

go get github.com/oscaromeu/meteocat

Examples
Get value of Minimum subsoil temperature at 5 cm at the Viladecans station
package main

import (
	"fmt"
	"github.com/oscaromeu/meteocat"
	"log"
	"os"
)

func main() {

	// execute export METEOCAT_API_KEY=<API_KEY_VALUE> on a shell first
	d, err := meteocat.NewMesurades(os.Getenv("METEOCAT_API_KEY"))
	if err != nil {
		log.Fatalln(err)
	}

	if meteocat.CheckAPIKeyExists(d.Key) == false {
		fmt.Println("ApiKey is not set. ")
	}

	data := meteocat.Data{
		Any: "2021",
		Mes: "01",
		Dia: "06",
	}
	params, _ := meteocat.NewParameters(
		meteocat.OptionCodiEstacio("UG"),
		meteocat.OptionCodiVariable("5"),
		meteocat.OptionData(data),
	)

	// Call MeasurementByDay Method
	d.MeasurementByDay(params)
	fmt.Println(d.Measurements)
}

Documentation

Documentation of the API can be found at https://apidocs.meteocat.gencat.cat/documentacio/.

TODO

  • Add support for the following API operations:
    • Mesurades
    • Predicció
  • Add more tests

Bug Tracker

Report bug at Issues・oscaromeu/meteocat - GitHub.

Author

oscaromeu (https://github.com/oscaromeu)

License

This software is released under the MIT License, see LICENSE.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CodisEstacions = map[string]string{}/* 189 elements not displayed */

CodisEstacions holds all stations in ope state to be used

View Source
var CodisEstat = map[string]string{
	"ope": "Operativa",
	"des": "Desmantellada",
	"bte": "Baixa temporal",
}

CodisEstat holds all the status that a station can have

View Source
var CodisVariables = map[string]string{
	"1":  "Pressió atmosfèrica màxima",
	"2":  "Pressió atmosfèrica mínima",
	"3":  "Humitat relativa màxima",
	"X4": "Temperatura màxima de subsòl a 5 cm",
	"5":  "Temperatura mínima de subsòl a 5 cm",
	"6":  "TDR màxima a 10 cm",
	"7":  "TDR mínima a 10 cm",
	"8":  "Desviació estàndard de la irradiància neta",
	"9":  "Irradiància reflectida",
	"10": "Irradiància fotosintèticament activa (PAR)",
	"11": "Temperatura de supefície",
	"12": "Temperatura màxima de superfície",
	"13": "Temperatura mínima de superfície",
	"14": "Temperatura de subsòl a 40 cm",
	"16": "Nivell evaporímetre",
	"20": "Velocitat del vent a 10 m (vec.)",
	"21": "Direcció del vent a 10 m (m. u)",
	"22": "Desviació est. de la direcció del vent a 10 m",
	"23": "Velocitat del vent a 6 m (vec.)",
	"24": "Direcció del vent a 6 m (m. u)",
	"25": "Desviació est. de la direcció de vent a 6 m",
	"26": "Velocitat del vent a 2 m (vec.)",
	"27": "Direcció del vent a 2 m (m. u)",
	"28": "Desviació est. de la direcció del vent a 2 m",
	"30": "Velocitat del vent a 10 m (esc.)",
	"31": "Direcció de vent 10 m (m. 1)",
	"32": "Temperatura",
	"33": "Humitat relativa",
	"34": "Pressió atmosfèrica",
	"35": "Precipitació",
	"36": "Irradiància solar global",
	"37": "Desviació est. de la irradiància solar global",
	"38": "Gruix de neu a terra",
	"39": "Radiació UV",
	"40": "Temperatura màxima",
	"42": "Temperatura mínima",
	"44": "Humitat relativa mínima",
	"46": "Velocitat del vent a 2 m (esc.)",
	"47": "Direcció del vent a 2 m (m. 1)",
	"48": "Velocitat del vent a 6 m (esc.)",
	"49": "Direcció del vent a 6 m (m. 1)",
	"50": "Ratxa màxima del vent a 10 m",
	"51": "Direcció de la ratxa màxima del vent a 10 m",
	"53": "Ratxa màxima del vent a 6 m",
	"54": "Direcció de la ratxa màxima del vent a 6 m",
	"56": "Ratxa màxima del vent a 2 m",
	"57": "Direcció de la ratxa màxima del vent a 2 m",
	"59": "Irradiància neta",
	"60": "Temperatura de subsòl a 5 cm",
	"61": "Temperatura de subsòl a 50 cm",
	"62": "TDR a 10 cm",
	"63": "TDR a 35 cm",
	"64": "Humectació moll",
	"65": "Humectació sec",
	"66": "Humectació res",
	"67": "Humectació moll 2",
	"68": "Humectació sec 2",
	"69": "Humectació res 2",
	"70": "Precipitació acumulada",
	"71": "Bateria",
	"72": "Precipitació màxima en 1 minut",
	"74": "Humitat del combustible forestal 1",
	"75": "Temperatura del combustible forestal 1",
	"76": "Humitat del combustible forestal 2",
	"77": "Temperatura del combustible forestal 2",
	"78": "Humitat del combustible forestal 3",
	"79": "Temperatura del combustible forestal 3",
	"80": "Temperatura de la neu 1",
	"81": "Temperatura de la neu 2",
	"82": "Temperatura de la neu 3",
	"83": "Temperatura de la neu 4",
	"84": "Temperatura de la neu 5",
	"85": "Temperatura de la neu 6",
	"86": "Temperatura de la neu 7",
	"87": "Temperatura de la neu 8",
	"88": "Quality number",
	"89": "Temperatura del datalogger",
	"90": "Altura màxima",
	"91": "Període màxima",
	"92": "Altura significant",
	"93": "Període significant",
	"94": "Altura mitjana",
	"95": "Període mitjà",
	"96": "Direcció del pic",
	"97": "Temperatura superficial del mar",
}

CodisVariables holds all the measurements performed by the weather stations. Note that not all the stations measures all the variables. To see which measurements a station does check the method MeasurementMetadataAllByStation

Functions

func CheckAPIKeyExists

func CheckAPIKeyExists(apiKey string) bool

CheckAPIKeyExists will see if an API key has been set.

func OptionCodiEstacio

func OptionCodiEstacio(codiEstacio string) func(p *Parameters) error

OptionCodiEstacio is a helper function to set up the value of CodiEstacio to be passed in Parameters struct

func OptionCodiEstat

func OptionCodiEstat(codiEstat string) func(p *Parameters) error

OptionCodiEstat is a helper function to set up the value of CodiEstat to be passed in Parameters struct

func OptionCodiVariable

func OptionCodiVariable(codiVariable string) func(p *Parameters) error

OptionCodiVariable is a helper function to set up the value of CodiVariable to be passed in Parameters struct

func OptionData

func OptionData(d Data) func(p *Parameters) error

OptionData is a helper function to set up the value of Data to be passed in Parameters struct

func OptionTimeDate

func OptionTimeDate(d TimeDate) func(p *Parameters) error

Option TimeData is a helper function to set up the value of Timedate to be passed in Parameters struct

func ValidAPIKey

func ValidAPIKey(key string) error

ValidAPIKey makes sure that the key given is a valid one

func ValidCodiEstacio

func ValidCodiEstacio(c string) bool

ValidCodiEstacio makes sure the string passed in is an acceptable station code.

func ValidCodiEstat

func ValidCodiEstat(c string) bool

ValidCodiEstat makes sure the string passed in is an acceptable estat code.

func ValidCodiVariable

func ValidCodiVariable(c string) bool

ValidCodiVariable makes sure the string passed in is an acceptable variable code.

func ValidData

func ValidData(d Data) bool

ValidData validates that we set a correct data

Types

type APIError

type APIError struct {
	Message string `json:"message"`
	COD     string `json:"cod"`
}

APIError returned on failed API calls.

type BasesTemporals

type BasesTemporals []struct {
	Codi      string `json:"codi"`
	DataInici string `json:"dataInici"`
	DataFi    string `json:"dataFi"`
}

BasesTemporals TODO

type Comarca

type Comarca struct {
	Codi int    `json:"codi"` // Comarca identification code
	Nom  string `json:"nom"`  // Name of the comarca
}

Comarca struct holds information on which Comarca is located the station.

type Config

type Config struct {
	APIKey string // API Key for connecting to the OWM
}

Config will hold default settings

type Coordenades

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
}

Coordenades struct holds georeference information of the stations

type Data

type Data struct {
	Any string
	Mes string
	Dia string
}

Data struct holds the time settings in general the time format will be YYYY/MM/D or YYYY-MM-DZ but this is transparent for the final user.

type Estacions

type Estacions struct {
	MetadadesEstacions
	Key          string
	CodiEstacio  string // ?
	CodiVariable string // ?
	*Settings
}

Mesurades holds all the data representations to unmarshall the API responses

func NewEstacions

func NewEstacions(key string) (*Estacions, error)

NewMesurades returns a new MesuradesData pointer with the supplied parameters

func (*Estacions) StationsAll

func (e *Estacions) StationsAll(p *Parameters) error

Returns a list of metadata from all stations. If settings are specified, filters by specified status and date The API resource is /estacions/metadades?estat={estat}&data={data} where the parameters `estat` and data optional. Request example: https://api.meteo.cat/xema/v1/estacions/metadades?estat=ope&data=2017-03-27Z

type Estats

type Estats []struct {
	Codi      int         `json:"codi"`
	DataInici string      `json:"dataInici"`
	DataFi    interface{} `json:"dataFi"`
}

Estats struct holds information of station code when initiated and or finalized.

type Lectura

type Lectura struct {
	Data        string  `json:"data"`
	Valor       float64 `json:"valor"`
	Estat       string  `json:"estat"`
	BaseHoraria string  `json:"baseHoraria"`
}

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 Measurements

type Measurements []struct {
	Codi      string     `json:"codi"`
	Variables []Variable `json:"variables"`
}

Measurements holds the measurements done in a station

type Mesurades

type Mesurades struct {
	Variable
	Measurements
	MetadadesVariablesEstacio
	MetadadesVariableEstacio
	MetadadesVariables
	Key          string
	CodiEstacio  string // ?
	CodiVariable string // ?
	*Settings
}

Mesurades holds all the data representations to unmarshall the API responses

func NewMesurades

func NewMesurades(key string) (*Mesurades, error)

NewMesurades returns a new MesuradesData pointer with the supplied parameters

func (*Mesurades) MeasurementAllByStation

func (m *Mesurades) MeasurementAllByStation(p *Parameters) error

func (*Mesurades) MeasurementByDay

func (m *Mesurades) MeasurementByDay(p *Parameters) error

This function returns information about a weather variable for all stations on a specific day. If a station code is provided, it returns the data for that variable and station. The API endpoint for this function is /variables/mesurades/{codi_variable}/{any}/{mes}/{dia}?codiEstacio={codi_estacio}.

The following parameters are mandatory: - `codi_variable`: The code of the variable to retrieve data for. - `any`: The year of the date to retrieve data for. - `mes`: The month of the date to retrieve data for. - `dia`: The day of the date to retrieve data for. The `codi_estacio` parameter is optional, and can be used to filter the data by a specific station code. Request example: https://api.meteo.cat/xema/v1/variables/mesurades/32/2017/03/27?codiEstacio=UG

func (*Mesurades) MeasurementLast

func (m *Mesurades) MeasurementLast(p *Parameters) error

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 (*Mesurades) MeasurementMetadataAll

func (m *Mesurades) MeasurementMetadataAll() error

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 (*Mesurades) MeasurementMetadataAllByStation

func (m *Mesurades) MeasurementMetadataAllByStation(p *Parameters) error

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 (*Mesurades) MeasurementMetadataByStation

func (m *Mesurades) MeasurementMetadataByStation(p *Parameters) error

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 (*Mesurades) MeasurementMetadataUnique

func (m *Mesurades) MeasurementMetadataUnique(p *Parameters) error

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

type MetadadesEstacions

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"`
}

Aggregation of fields and structs to unmarshal the responses

type MetadadesVariable

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
}

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 MetadadesVariableEstacio

type MetadadesVariableEstacio struct {
	MetadadesVariable
	Estats
	BasesTemporals
}

MetadadesVariableEstacio is an agreggate type to hold the metadata of the variable data registered in a particular station

type MetadadesVariables

type MetadadesVariables []struct{ MetadadesVariable }

MetadadesVariables is a slice which holds the metadata of all variables

type MetadadesVariablesEstacio

type MetadadesVariablesEstacio []struct{ MetadadesVariableEstacio }

MetadadesVariablesEstacio is a slice which holds the variables metadata of all the data registered bu a station

type Municipi

type Municipi struct {
	Codi string `json:"codi"` // INE code of the municipi
	Nom  string `json:"nom"`  // Name of the municipi
}

Municipi struct holds information on which municipi is located the station.

type Option

type Option func(s *Settings) error

Optional client settings

func WithHttpClient

func WithHttpClient(c *http.Client) Option

// WithHttpClient sets custom http client when creating a new Client.

type Parameters

type Parameters struct {
	Data
	TimeDate
	// contains filtered or unexported fields
}

Parameters holds all the options to be passed in to the methods

func NewParameters

func NewParameters(options ...func(*Parameters) error) (*Parameters, error)

NewParameters generates a new Parameters config.

type Provincia

type Provincia struct {
	Codi int    `json:"codi"` // Provincia identification code
	Nom  string `json:"nom"`  // Name of the provincia
}

Provincia struct holds information on which Provincia is located the station.

type Settings

type Settings struct {
	// contains filtered or unexported fields
}

Settings holds the client settings

func NewSettings

func NewSettings() *Settings

NewSettings returns a new Setting pointer with default http client.

type TimeDate

type TimeDate struct {
	Hour         string
	Minute       string
	Seconds      string
	Milliseconds string
}

type Variable

type Variable struct {
	Codi     int       `json:"codi"`
	Lectures []Lectura `json:"lectures"`
}

Variable is an agreggate type which represents the variable data registered in a station.

type Xarxa

type Xarxa struct {
	Codi int    `json:"codi"` // Xarxa identification code
	Nom  string `json:"nom"`  // Name of the Network
}

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

Directories

Path Synopsis
_examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL