# 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():


import csv
import os
import requests
from lxml import etree, objectify
from lxml.etree import fromstring
from metar.metar import Metar
from plenario.database import postgres_engine as engine
    from .weather import degToCardinal
# Example METAR URL: 'https://aviationweather.gov/adds/dataserver_current/httpparam?datasource=metars&requesttype=retrieve&format=xml&hoursBeforeNow=1.25&stationString=KORD'

current_METAR_url = 'http://aviationweather.gov/adds/dataserver_current/current/'


def _make_call_sign_wban_map():
    try:
        with open('plenario/utils/wban_to_call_sign.csv') as fp:
            reader = csv.reader(fp)
            # Discard header
            next(reader)
            call_sign_to_wban_map = {row[1]: row[0] for row in reader}
        return call_sign_to_wban_map
    except IOError as err:
        raise IOError(str(err) + " -- running from {}".format(os.path.abspath(".")))


# An example code:
# - In this example, we have "few clouds at 1500 feet, broken clouds at 4,000 feet w/ cumulonimbus,
#   broken at 6,500 feet, overcast at 20,000 feet"
# - Visibility is 2 statute miles
# code = "METAR KEWR 111851Z VRB03G19KT 2SM R04R/3000VP6000FT TSRA BR FEW015 BKN040CB BKN065 OVC200 22/22 A2987 RMK AO2 PK WND 29028/1817 WSHFT 1812 TSB05RAB22 SLP114 FRQ LTGICCCCG TS OHD AND NW-N-E MOV NE P0013 T02270215"

def getMetar(metar_string):
    m = Metar(metar_string)
    return m


def all_callSigns():
    sql = "SELECT call_sign FROM weather_stations ORDER BY call_sign"

# 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/weather_metar.py
import csv
import os
import requests
from lxml import etree, objectify
from lxml.etree import fromstring
from metar.metar import Metar
from plenario.database import postgres_engine as engine
    from .weather import degToCardinal

# Example METAR URL: 'https://aviationweather.gov/adds/dataserver_current/httpparam?datasource=metars&requesttype=retrieve&format=xml&hoursBeforeNow=1.25&stationString=KORD'

current_METAR_url = 'http://aviationweather.gov/adds/dataserver_current/current/'


def _make_call_sign_wban_map():
    try:
        with open('plenario/utils/wban_to_call_sign.csv') as fp:
            reader = csv.reader(fp)
            # Discard header
            next(reader)
            call_sign_to_wban_map = {row[1]: row[0] for row in reader}
        return call_sign_to_wban_map
    except IOError as err:
        raise IOError(str(err) + " -- running from {}".format(os.path.abspath(".")))


# An example code:
# - In this example, we have "few clouds at 1500 feet, broken clouds at 4,000 feet w/ cumulonimbus,
#   broken at 6,500 feet, overcast at 20,000 feet"
# - Visibility is 2 statute miles
# code = "METAR KEWR 111851Z VRB03G19KT 2SM R04R/3000VP6000FT TSRA BR FEW015 BKN040CB BKN065 OVC200 22/22 A2987 RMK AO2 PK WND 29028/1817 WSHFT 1812 TSB05RAB22 SLP114 FRQ LTGICCCCG TS OHD AND NW-N-E MOV NE P0013 T02270215"

def getMetar(metar_string):
    m = Metar(metar_string)
    return m


def all_callSigns():
    sql = "SELECT call_sign FROM weather_stations ORDER BY call_sign"

    result = engine.execute(sql)