#!/usr/bin/python
# Code to move remote station data from buffer to main log every minute and send to chords if turned on
# Joseph E. Rener
# UCAR
# Boulder, CO USA
# Email: jrener@ucar.edu
# Copyright (c) 2018 UCAR
# Developed at COMET at University Corporation for Atmospheric Research and the Research Applications Laboratory at the National Center for Atmospheric Research (NCAR)
# Updated: 30 May 2019

import helper_functions, datetime
from helper_functions import root

# Get script arguments
arguments = helper_functions.getArguments()
interval = arguments[0]
chords_toggle = arguments[1]
chords_id = arguments[2]

# Get data
file = open(root + 'data/remote_stations/remote_stations_buffer.log', 'r+')
data = file.readlines()
file.truncate(0) #clear file
file.close() 

# Format and send most recent data
rain_gauges = {}
statuses = {}
stations = {}
if len(data) > 0:
    for line in data:
        #lines will either look something like  Match Len[32] #3 => #1 -48dB : Msg[RG1,3,0,1,4.19] CS[728]  or  Match Len[96] #2 => #1 -48dB : Msg[3DP1,2,0,25.02,83818.67,1609.79,24.17,45.44,24.06,262.00,266.00,0.03,10574,0,10576,0,18,5.12] CS[4574]
        if line[:5] == "Match":
            target = line.split(" => #")[1]
            target = int(target.split(" ")[0])
            if target == chords_id:
                line = line.split("Msg[")[1]
                payload = line.split("] CS")[0].split(",")
                msg_type = payload[0]
                station_id = payload[1]
                #remote rain gauge data
                if msg_type == "RG1": #RG1, id, count, tips, battery
                    tips = payload[3]
                    if station_id in rain_gauges:
                        rain_gauges[station_id] = rain_gauges[station_id] + float(tips)*.2
                    else:
                        rain_gauges[station_id] = float(tips)*.2
                #remote rain gauge battery
                elif msg_type == "RG2": #RG2, id, count, "POWERON", battery  or  RG2, id, sequence, "HEARTBEAT", battery
                    status = float(payload[4])
                    statuses[station_id] = status
                #remote arduino data
                elif msg_type == "3DP1": #3DP1, id, count, bmp-temp, bmp-pres, bmp-alt, htu-temp, htu-hum, mcp-temp, sil-vis, sil-ir, sil-uv, windspd-time, windspd-rev, windspd, rain-time, rain, winddir, battery
                    bmp_tmp = float(payload[3])
                    bmp_pre = float(payload[4])
                    bmp_alt = float(payload[5])
                    htu_tmp = float(payload[6])
                    htu_hum = float(payload[7])
                    mcp_tmp = float(payload[8])
                    sil_vis = float(payload[9])
                    sil_ir = float(payload[10])
                    sil_uv = float(payload[11])
                    windspd = float(payload[14])
                    rain = float(payload[16])
                    winddir = float(payload[17])
                    battery = float(payload[18])
                    if station_id in stations:
                        stations[station_id].append([bmp_tmp, bmp_pre, bmp_alt, htu_tmp, htu_hum, mcp_tmp, sil_vis, sil_ir, sil_uv, windspd, rain, winddir, battery])
                    else:
                        stations[station_id] = [[bmp_tmp, bmp_pre, bmp_alt, htu_tmp, htu_hum, mcp_tmp, sil_vis, sil_ir, sil_uv, windspd, rain, winddir, battery]]
    now = datetime.datetime.now()
    minute = now.minute - 1
    for rain_gauge in rain_gauges:
        line = "station %s:   %02d %02d %4d %02d %02d %6.2f" % (rain_gauge, now.month, now.day, now.year, now.hour, minute, rain_gauges[rain_gauge])
        filename = root + 'data/remote_stations/remote_rain_%4d%02d%02d.dat' % (now.year, now.month, now.day)
        helper_functions.output(False, line, filename)
        url = "http://3d.chordsrt.com/measurements/url_create?instrument_id=%d&r_rain%s=%04.2f&key=21DE6A8A" % (chords_id, rain_gauge, rain_gauges[rain_gauge])
        helper_functions.reportCHORDS(chords_toggle, minute, interval, url)	
    for rain_gauge in statuses:
        url = "http://3d.chordsrt.com/measurements/url_create?instrument_id=%d&r_status%s=%04.2f&key=21DE6A8A" % (chords_id, rain_gauge, statuses[rain_gauge])
        helper_functions.reportCHORDS(chords_toggle, minute, 1, url)	
        #if there is a heartbeat but no recorded rain, send in 0
        if rain_gauge not in rain_gauges:
            url = "http://3d.chordsrt.com/measurements/url_create?instrument_id=%d&r_rain%s=%04.2f&key=21DE6A8A" % (chords_id, rain_gauge, 0)
            helper_functions.reportCHORDS(chords_toggle, minute, 1, url)
    for station_id in stations:
        #find averages of the data recieved over the minute, except for rain, which is cumulative
        totals = [0,0,0,0,0,0,0,0,0,0,0,0,0]
        station = stations[station_id]
        data_points = len(station)
        for data_set in station:
            for i in range(0, len(data_set)-1):
                totals[i] += data_set[i]
            totals[12] = data_set[12] #we don't need the average battery; only the last recorded value
        averages = [0,0,0,0,0,0,0,0,0,0,0,0]
        for j in range(0,12):
            if j != 10:
                averages[j] = float(totals[j])/data_points
            else:
                averages[10] = totals[10]
        #record data locally
        line = "station %s:    %02d %02d %4d %02d %02d %8.2f %10.2f %8.2f %7.1f %5.1f %6.1f %9.3f %8.3f %6.3f %7.2f %8.2f %8.1f %7.2f " % (station_id, now.month, now.day, now.year, now.hour, now.minute-1, averages[0], averages[1], averages[2], averages[3], averages[4], averages[5], averages[6], averages[7], averages[8], averages[9], averages[10], averages[11], totals[12])
        filename = root + 'data/remote_stations/remote_stations_%4d%02d%02d.dat' % (now.year, now.month, now.day)
        helper_functions.output(False, line, filename)
        #Report to chords as a seperate entity
        station_id = 38
        bmp_url = "http://3d.chordsrt.com/measurements/url_create?instrument_id=%d&t2=%05.1f&msl1=%07.2f&sp1=%07.2f&key=21DE6A8A" % (station_id, averages[0], averages[1], averages[2])
        htu_url = "http://3d.chordsrt.com/measurements/url_create?instrument_id=%d&t1=%05.1f&rh1=%04.1f&key=21DE6A8A" % (station_id, averages[3], averages[4])
        mcp_url = "http://3d.chordsrt.com/measurements/url_create?instrument_id=%d&t3=%05.1f&key=21DE6A8A" % (station_id, averages[5])
        sil_url = "http://3d.chordsrt.com/measurements/url_create?instrument_id=%d&vis1=%010.1f&ir1=%010.1f&uv1=%010.1f&key=21DE6A8A" % (station_id, averages[6], averages[7], averages[8])
        speed_url = "http://3d.chordsrt.com/measurements/url_create?instrument_id=%d&ws=%04.2f&key=21DE6A8A" % (station_id, averages[9])
        rain_url = "http://3d.chordsrt.com/measurements/url_create?instrument_id=%d&rain=%04.2f&key=21DE6A8A" % (station_id, averages[10])
        direction_url = "http://3d.chordsrt.com/measurements/url_create?instrument_id=%d&wd=%05.1f&key=21DE6A8A" % (station_id, averages[11])
        battery_url = "http://3d.chordsrt.com/measurements/url_create?instrument_id=%d&bv=%06.2f&key=21DE6A8A" % (station_id, totals[12])
        helper_functions.reportCHORDS(chords_toggle, minute, interval, bmp_url)	
        helper_functions.reportCHORDS(chords_toggle, minute, interval, htu_url)	
        helper_functions.reportCHORDS(chords_toggle, minute, interval, mcp_url)	
        helper_functions.reportCHORDS(chords_toggle, minute, interval, sil_url)	
        helper_functions.reportCHORDS(chords_toggle, minute, interval, speed_url)
        helper_functions.reportCHORDS(chords_toggle, minute, interval, rain_url)	
        helper_functions.reportCHORDS(chords_toggle, minute, interval, direction_url)
        helper_functions.reportCHORDS(chords_toggle, minute, interval, battery_url)	