# Path: plenario/api/common.py
# RESPONSE_LIMIT = 1000
# CACHE_TIMEOUT = 60 * 60 * 6
# def unknown_object_json_handler(obj):
# def date_json_handler(obj):
# def crossdomain(origin=None, methods=None, headers=None,
#                 max_age=21600, attach_to_all=True,
#                 automatic_options=True):  # pragma: no cover
#     def get_methods():
#     def decorator(f):
#         def wrapped_function(*args, **kwargs):
# def make_cache_key(*args, **kwargs):
# def make_csv(data):
# def extract_first_geometry_fragment(geojson):
# def make_fragment_str(geojson_fragment, buffer=100):
# 
# Path: plenario/api/response.py
# def make_error(msg, status_code, arguments=None):
# 
#     if not arguments:
#         arguments = request.args
# 
#     resp = {
#         'meta': {
#             'status': 'error',
#             'message': msg,
#             'query': arguments
#         },
#         'objects': [],
#     }
# 
#     response = jsonify(resp)
#     response.status_code = status_code
#     return response
# 
# Path: plenario/database.py
# def create_database(bind: Engine, database: str) -> None:
# def drop_database(bind: Engine, database: str) -> None:
# def create_extension(bind: Engine, extension: str) -> None:
# def psql(path: str) -> None:
# def postgres_session_context():
# def redshift_session_context():
# 
# Path: plenario/utils/helpers.py
# def get_size_in_degrees(meters, latitude):
#     earth_circumference = 40041000.0  # meters, average circumference
#     degrees_per_meter = 360.0 / earth_circumference
# 
#     degrees_at_equator = meters * degrees_per_meter
# 
#     latitude_correction = 1.0 / math.cos(latitude * (math.pi / 180.0))
# 
#     degrees_x = degrees_at_equator * latitude_correction
#     degrees_y = degrees_at_equator
# 
#     return degrees_x, degrees_y


import json
import shapely.geometry
import shapely.wkb
import sqlalchemy as sa
from flask import jsonify, make_response, request
from sqlalchemy import Table, func
from sqlalchemy.exc import SQLAlchemyError
from plenario.api.common import CACHE_TIMEOUT, RESPONSE_LIMIT, cache, crossdomain, date_json_handler, make_cache_key
from plenario.api.response import make_error
from plenario.database import postgres_base, postgres_engine as engine, postgres_session
from plenario.utils.helpers import get_size_in_degrees
    from plenario.tasks import update_weather
        result = postgres_session.execute(q)
    except SQLAlchemyError:
        postgres_session.rollback()
        return False

    matched_wban = result.first()
    if not bool(matched_wban):
        return False

    return True


def wban_list_if_valid(wban_list_str):
    if not wban_list_str:
        return False
    wban_candidate_list = wban_list_str.split(',')

    # If user submits a _lot_ of WBANs, the inefficiency of
    # making one DB call per WBAN will be noticeable.
    # But assuming a handful (< 10) at a time, this is fine for a feature
    # that is designed as a kludge that we do not officially support.
    return [w for w in wban_candidate_list if wban_is_valid(w)]


@crossdomain(origin='*')
def weather_fill():

    args = request.args.copy()
    year = year_if_valid(args.get('year'))
    if not year:

# Path: plenario/api/common.py
# RESPONSE_LIMIT = 1000
# CACHE_TIMEOUT = 60 * 60 * 6
# def unknown_object_json_handler(obj):
# def date_json_handler(obj):
# def crossdomain(origin=None, methods=None, headers=None,
#                 max_age=21600, attach_to_all=True,
#                 automatic_options=True):  # pragma: no cover
#     def get_methods():
#     def decorator(f):
#         def wrapped_function(*args, **kwargs):
# def make_cache_key(*args, **kwargs):
# def make_csv(data):
# def extract_first_geometry_fragment(geojson):
# def make_fragment_str(geojson_fragment, buffer=100):
# 
# Path: plenario/api/response.py
# def make_error(msg, status_code, arguments=None):
# 
#     if not arguments:
#         arguments = request.args
# 
#     resp = {
#         'meta': {
#             'status': 'error',
#             'message': msg,
#             'query': arguments
#         },
#         'objects': [],
#     }
# 
#     response = jsonify(resp)
#     response.status_code = status_code
#     return response
# 
# Path: plenario/database.py
# def create_database(bind: Engine, database: str) -> None:
# def drop_database(bind: Engine, database: str) -> None:
# def create_extension(bind: Engine, extension: str) -> None:
# def psql(path: str) -> None:
# def postgres_session_context():
# def redshift_session_context():
# 
# Path: plenario/utils/helpers.py
# def get_size_in_degrees(meters, latitude):
#     earth_circumference = 40041000.0  # meters, average circumference
#     degrees_per_meter = 360.0 / earth_circumference
# 
#     degrees_at_equator = meters * degrees_per_meter
# 
#     latitude_correction = 1.0 / math.cos(latitude * (math.pi / 180.0))
# 
#     degrees_x = degrees_at_equator * latitude_correction
#     degrees_y = degrees_at_equator
# 
#     return degrees_x, degrees_y


# Path: plenario/api/sensor.py
import json
import shapely.geometry
import shapely.wkb
import sqlalchemy as sa
from flask import jsonify, make_response, request
from sqlalchemy import Table, func
from sqlalchemy.exc import SQLAlchemyError
from plenario.api.common import CACHE_TIMEOUT, RESPONSE_LIMIT, cache, crossdomain, date_json_handler, make_cache_key
from plenario.api.response import make_error
from plenario.database import postgres_base, postgres_engine as engine, postgres_session
from plenario.utils.helpers import get_size_in_degrees
    from plenario.tasks import update_weather

        result = postgres_session.execute(q)
    except SQLAlchemyError:
        postgres_session.rollback()
        return False

    matched_wban = result.first()
    if not bool(matched_wban):
        return False

    return True


def wban_list_if_valid(wban_list_str):
    if not wban_list_str:
        return False
    wban_candidate_list = wban_list_str.split(',')

    # If user submits a _lot_ of WBANs, the inefficiency of
    # making one DB call per WBAN will be noticeable.
    # But assuming a handful (< 10) at a time, this is fine for a feature
    # that is designed as a kludge that we do not officially support.
    return [w for w in wban_candidate_list if wban_is_valid(w)]


@crossdomain(origin='*')
def weather_fill():

    args = request.args.copy()
    year = year_if_valid(args.get('year'))
    if not year:

        return make_error('Must supply a year between 2000 and 2019', 400)