Esempio n. 1
0
def station_form_auto():
    """Crea ula estaciones meterológicas automaticamente"""
    data = pd.read_csv("data/ListadoEStaciones_CSV_v04.csv", sep=";")

    stations = WeatherStation()
    stations_ids = [r.station_id for r in stations.query.distinct()]

    for i, row in data.iterrows():
        if row["ID"] in stations_ids:
            logger.info(f'{row["ID"]}:{row["MUNICIPIO"]} ya existe en la DB')
        else:
            weather_station = WeatherStation(
                station_id=row["ID"],
                municipio=row["MUNICIPIO"],
                provincia=row["PROVINCIA"],
                altura=row["ALTURA"],
                latitud=row["LATITUD"],
                latitud_gd=row["LATITUD GD"],
                longitud=row["LONGITUD"],
                longitud_gd=row["LONGITUD GD"],
            )
            weather_station.save()
            logger.info(
                f'Guardada la nueva estación metereológica {row["ID"]}:{row["MUNICIPIO"]}'
            )

    return redirect(url_for('meteo.list_weather_stations'))
Esempio n. 2
0
def station_form():
    """Crea una nueva estación meterológica"""
    form = StationWeatherForm()
    if form.validate_on_submit():
        station_id = form.station_id.data
        municipio = form.municipio.data
        provincia = form.provincia.data
        altura = form.altura.data
        latitud = form.latitud.data
        latitud_gd = form.latitud_gd.data
        longitud = form.longitud.data
        longitud_gd = form.longitud_gd.data

        weather_station = WeatherStation(
            station_id=station_id,
            municipio=municipio,
            provincia=provincia,
            altura=altura,
            latitud=latitud,
            latitud_gd=latitud_gd,
            longitud=longitud,
            longitud_gd=longitud_gd,
        )
        weather_station.save()
        logger.info(
            f'Guardada la nueva estación metereológica {station_id}:{municipio}'
        )
        return redirect(url_for('meteo.list_weather_stations'))
    return render_template("meteo/weatherstation_form.html", form=form)
Esempio n. 3
0
def get_or_create_weather_station(weather):
    weather_station = WeatherStation.query.filter_by(weatherstation_uid=weather['name']).first()
    if not weather_station:
        weather_station = WeatherStation()
        weather_station.weatherstation_uid = weather['name']
        weather_station.longitude = weather['coord']['lon']
        weather_station.latitude = weather['coord']['lat']
        db.session.add(weather_station)
        db.session.commit()
    return weather_station.weatherstation_id
Esempio n. 4
0
def update_station_form(station_id):
    """Actualiza una estación existente"""
    station = WeatherStation.get_by_station(station_id)
    if station is None:
        logger.info(f'La estación {station_id} no existe')
        abort(404)
    # Crea un formulario inicializando los campos con
    # los valores del post.
    form = StationWeatherForm(obj=station)
    if form.validate_on_submit():
        # Actualiza los campos del post existente
        station.station_id = form.station_id.data
        station.municipio = form.municipio.data
        station.provincia = form.provincia.data
        station.altura = form.altura.data
        station.latitud = form.latitud.data
        station.latitud_gd = form.latitud_gd.data
        station.longitud = form.longitud.data
        station.longitud_gd = form.longitud_gd.data

        station.save()
        logger.info(
            f'Guardanda la estación metereológica {station_id}:{station.municipio}'
        )
        return redirect(url_for('meteo.list_weather_stations'))
    return render_template("meteo/weatherstation_form.html",
                           form=form,
                           station=station)
Esempio n. 5
0
def database_station(station_id):

    stations = WeatherStation()
    _station = stations.get_by_station(station_id)
    if _station is None:
        logger.info(f'La estación {station_id} no existe')
        abort(404)
    _municipio = _station.municipio
    try:
        obs = owm.weather_at_place(f'{_municipio}, ES')
    except:
        logger.info(f'La estación {_municipio} no existe')
        abort(404)
    logger.info(_municipio)

    return render_template("meteo/weatherstation.html", station_id=station_id, data=get_data(obs))
Esempio n. 6
0
def show_meteo_station(slug):
    logger.info('Mostrando un post')
    logger.debug(f'Slug: {slug}')
    post = WeatherStation.get_by_slug(slug)
    if not post:
        logger.info(f'La estación {slug} no existe')
        abort(404)
    return render_template("meteo/weatherstations.html", post=post)
Esempio n. 7
0
def list_weather_stations():
    page = int(request.args.get('page', 1))
    per_page = current_app.config['ITEMS_PER_PAGE']
    meteo_pagination = WeatherStation.all_paginated(page, per_page)
    logger.info(meteo_pagination)
    logger.info(page)
    logger.info(per_page)
    return render_template("meteo/weatherstations.html",
                           meteo_pagination=meteo_pagination)
Esempio n. 8
0
def delete_station(station_id):
    logger.info(f'Se va a eliminar la estación metereológica {station_id}')
    station = WeatherStation.get_by_station(station_id)
    if station is None:
        logger.info(f'La estación metereológica {station_id} no existe')
        abort(404)
    station.delete()
    logger.info(f'L aestación meterológica {station_id} ha sido eliminada')
    return redirect(url_for('meteo.list_weather_stations'))
Esempio n. 9
0
def scheduler_db():
    stations = WeatherStation()
    logger.info("---------")
    logger.info(stations.query.distinct())
    stations_ids = [r.station_id for r in stations.query.distinct()]
    logger.info(stations_ids)

    if stations_ids is None:
        logger.info('No existen estaciones dadas de alta')
        abort(404)

    for station_id in stations_ids:
        station = stations.get_by_station(station_id)
        _municipio = station.municipio
        try:
            obs = owm.weather_at_place(f'{_municipio}, ES')
            logger.info(_municipio)
            data_to_db(get_data(obs), station_id)
        except:
            logger.info(f'Pyowm no envuentra el municipio {_municipio}')