Coder Social home page Coder Social logo

newage1512 / winds-mobi-providers Goto Github PK

View Code? Open in Web Editor NEW

This project forked from winds-mobi/winds-mobi-providers

0.0 0.0 0.0 982 KB

Real-time weather observations: weather data providers

Home Page: https://winds.mobi

License: GNU Affero General Public License v3.0

Python 99.52% Dockerfile 0.41% Shell 0.07%

winds-mobi-providers's Introduction

winds-mobi-providers

Python scripts that get the weather data from different providers and save it in a common format into mongodb. This project use Google Cloud APIs to compute any missing station details (altitude, name, timezone, ...). Google Cloud API results are cached with redis.

Run the project with docker compose (simple way)

Dependencies

  • Docker
  • Google Cloud API key
  • Providers secrets (optional)

Create an .env file from .env.template read by docker compose:

  • cp .env.template .env

In .env:

  • fill GOOGLE_API_KEY with you own Google Cloud API key
  • optionally fill the missing secrets for each provider you want to test

Start the databases

  • docker compose -f compose.services.yaml up

Run the providers

  • docker compose up

Or, run only a specific provider:

  • PROVIDER=ffvl docker compose up

Some providers need winds-mobi-admin running to get stations metadata.

Run the project locally on macOS

Dependencies

  • Homebrew
  • Python 3.10
  • Poetry
  • Google Cloud API key
  • Providers secrets (optional)

Create an .env.localhost file from .env.localhost.template read by dotenv for our local commands:

  • cp .env.localhost.template .env.localhost

In env.localhost:

  • fill GOOGLE_API_KEY with you own Google Cloud API key
  • optionally fill the missing secrets for each provider you want to test

Install libraries with homebrew:

  • brew install libpq
  • brew install mysql-client

Create virtual environment and install dependencies

  • export PATH=/usr/local/opt/libpq/bin:$PATH

  • export PATH=/usr/local/opt/mysql-client/bin:$PATH

  • poetry install

Activate and deactivate the virtual environment

  • poetry shell
  • exit

Start the databases

You must already have the .env file created in the previous section.

  • docker compose -f compose.services.yaml up

Run the providers

  • dotenv -f .env.localhost run python run_scheduler.py

Or, run only a specific provider:

  • dotenv -f .env.localhost run python -m providers.ffvl

Some providers need winds-mobi-admin running to get stations metadata.

Checking the code style

Format python code:

  • black .

Run the linter tools:

  • flake8 .
  • isort .

Contributing

Add a new provider to winds.mobi

You know good quality weather stations that would be useful for many paraglider pilots or kitesurfers?

Awesome! Fork this repository and open a pull request with your new provider code. It's easy, look at the following example:

providers/my_provider.py

import arrow
import requests

from winds_mobi_provider import Provider, ProviderException, StationStatus, ureg, Q_, Pressure


class MyProvider(Provider):
    provider_code = "my-provider"
    provider_name = "my-provider.com"

    def process_data(self):
        self.log.info("Processing MyProvider data...")
        try:
            # data = requests.get(
            #     "https://api.my-provider.com/stations.json", timeout=(self.connect_timeout, self.read_timeout)
            # ).json()
            data = [
                {
                    "id": "station-1",
                    "name": "Station 1",
                    "latitude": 46.713,
                    "longitude": 6.503,
                    "status": "ok",
                    "measures": [
                        {
                            "time": arrow.now().format("YYYY-MM-DD HH:mm:ssZZ"),
                            "windDirection": 180,
                            "windAverage": 10.5,
                            "windMaximum": 20.1,
                            "temperature": 25.7,
                            "pressure": 1013,
                        }
                    ],
                }
            ]
            for station in data:
                try:
                    winds_station = self.save_station(
                        provider_id=station["id"],
                        short_name=station["name"],
                        name=None,  # Lets winds.mobi provide the full name with the help of Google Geocoding API
                        latitude=station["latitude"],
                        longitude=station["longitude"],
                        status=StationStatus.GREEN if station["status"] == "ok" else StationStatus.RED,
                        url=f"https://my-provider.com/stations/{station['id']}",
                    )

                    measure_key = arrow.get(station["measures"][0]["time"], "YYYY-MM-DD HH:mm:ssZZ").int_timestamp
                    measures_collection = self.measures_collection(winds_station["_id"])

                    if not self.has_measure(measures_collection, measure_key):
                        new_measure = self.create_measure(
                            for_station=winds_station,
                            _id=measure_key,
                            wind_direction=station["measures"][0]["windDirection"],
                            wind_average=Q_(station["measures"][0]["windAverage"], ureg.meter / ureg.second),
                            wind_maximum=Q_(station["measures"][0]["windMaximum"], ureg.meter / ureg.second),
                            temperature=Q_(station["measures"][0]["temperature"], ureg.degC),
                            pressure=Pressure(station["measures"][0]["pressure"], qnh=None, qff=None),
                        )
                        self.insert_new_measures(measures_collection, winds_station, [new_measure])

                except ProviderException as e:
                    self.log.warning(f"Error while processing station '{station['id']}': {e}")
                except Exception as e:
                    self.log.exception(f"Error while processing station '{station['id']}': {e}")

        except Exception as e:
            self.log.exception(f"Error while processing MyProvider: {e}")

        self.log.info("...Done !")


def my_provider():
    MyProvider().process_data()


if __name__ == "__main__":
    my_provider()

Licensing

winds.mobi is licensed under the AGPL License, Version 3.0. See LICENSE.txt

winds-mobi-providers's People

Contributors

ysavary avatar pararnold avatar fimbo avatar skyrider31 avatar burnair avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.