<reponame>GCOOS/dbhydro<filename>station_tools/dbhydro_station_list_to_config.py
#!/usr/bin/env python3
"""dbhydro_station_list_to_config.

Takes an html table generated by DBHYDRO browser and parses into a JSON
compliant data structure that we can add to halo.cfg so we have a list
of station names, descriptions and coordinates. DBHYDRO does NOT return
coordinates for station data queries so we need to have our own list.
"""
import sys
import pprint
import pandas as pd


def build_station(row):
    """build_station.

    Takes row of df and creates JSON structure
    """
    station = row['Station']
    # Convert to standard format
    latitude = row['Latitude(ddmmss.sss)']*.0001
    longitude = -row['Longitude(ddmmss.sss)']*.0001
    latitude = "%0.4f" % latitude
    longitude = "%0.4f" % longitude
    station_dict = {"station": station, "latitude": latitude,
                    "longitude": longitude}
    return station_dict


def parse_html(file_name):
    """parse_html.

    Reads html file using pandas read_html and iterates to generate
    json compliant data structure.  JSON data is written out as
    stations.json
    """
    stations = []
    try:
        df = pd.read_html(file_name)
    except IOError as error:
            print("parse_html(): Error %s" % error)
            sys.exit()
    # Data is returned in df[2]
    df = df[2]
    for _, row in df.iterrows():
        station = build_station(row)
        stations.append(station)
    write_json(stations)


def write_json(data):
    """write_json.

    Created: 2020-06-05
    Modified: 2020-06-09
    Author: <EMAIL>
    Notes: writes out feature collection
    """
    pp = pprint.PrettyPrinter(indent=4)
    pp.pprint(data)
    #fname = ("./dbhydro_stations.json")
    #print("write_geojson(): Writing %s" % fname)


if __name__ == '__main__':
    if len(sys.argv) != 2:
        print("dbhydro_station_list_to_config(): Must supply html file name.")
        sys.exit()
    else:
        file_name = sys.argv[1]
    parse_html(file_name)

#!/usr/bin/env python3
"""dbhydro_station_list_to_config.

Takes an html table generated by DBHYDRO browser and parses into a JSON
compliant data structure that we can add to halo.cfg so we have a list
of station names, descriptions and coordinates. DBHYDRO does NOT return
coordinates for station data queries so we need to have our own list.
"""
import sys
import pprint
import pandas as pd


def build_station(row):
    """build_station.

    Takes row of df and creates JSON structure
    """
    station = row['Station']
    # Convert to standard format
    latitude = row['Latitude(ddmmss.sss)']*.0001
    longitude = -row['Longitude(ddmmss.sss)']*.0001
    latitude = "%0.4f" % latitude
    longitude = "%0.4f" % longitude
    station_dict = {"station": station, "latitude": latitude,
                    "longitude": longitude}
    return station_dict


def parse_html(file_name):
    """parse_html.

    Reads html file using pandas read_html and iterates to generate
    json compliant data structure.  JSON data is written out as
    stations.json
    """
    stations = []
    try:
        df = pd.read_html(file_name)
    except IOError as error:
            print("parse_html(): Error %s" % error)
            sys.exit()
    # Data is returned in df[2]
    df = df[2]
    for _, row in df.iterrows():
        station = build_station(row)
        stations.append(station)
    write_json(stations)


def write_json(data):
    """write_json.

    Created: 2020-06-05
    Modified: 2020-06-09
    Author: <EMAIL>
    Notes: writes out feature collection
    """
    pp = pprint.PrettyPrinter(indent=4)
    pp.pprint(data)
    #fname = ("./dbhydro_stations.json")
    #print("write_geojson(): Writing %s" % fname)


if __name__ == '__main__':
    if len(sys.argv) != 2:
        print("dbhydro_station_list_to_config(): Must supply html file name.")
        sys.exit()
    else:
        file_name = sys.argv[1]
    parse_html(file_name)
#!/usr/bin/env python3 dbhydro_station_list_to_config.  Takes an html table generated by DBHYDRO browser and parses into a JSON compliant data structure that we can add to halo.cfg so we have a list of station names, descriptions and coordinates. DBHYDRO does NOT return coordinates for station data queries so we need to have our own list. build_station.      Takes row of df and creates JSON structure # Convert to standard format parse_html.      Reads html file using pandas read_html and iterates to generate     json compliant data structure.  JSON data is written out as     stations.json # Data is returned in df[2] write_json.      Created: 2020-06-05     Modified: 2020-06-09     Author: <EMAIL>     Notes: writes out feature collection #fname = ("./dbhydro_stations.json") #print("write_geojson(): Writing %s" % fname)