Esempio n. 1
0
def create_graph_iot(sensor, time):
    client = MongoClient(os.environ["MONGODB_CLIENT"])
    start, now = helpers.get_time_range(time)
    db = client.iot
    df = pd.DataFrame(
        list(
            db.raw.find(
                {
                    "entity_id": {"$in": sensor},
                    "timestamp_": {"$gt": start, "$lte": now},
                }
            ).sort([("timestamp_", -1)])
        )
    )
    if len(df) == 0:
        df = pd.DataFrame(
            list(
                db.raw.find({"entity_id": {"$in": sensor}})
                .limit(2)
                .sort([("timestamp_", -1)])
            )
        )

    data = []
    for s in sensor:
        try:
            df_s = df[df["entity_id"] == s]
            data.append(
                go.Scatter(
                    x=df_s["timestamp_"],
                    y=df_s["state"],
                    name=s,
                    line=dict(shape="spline", smoothing=0.7, width=3),
                    mode="lines",
                )
            )
        except Exception:
            pass

    layout = go.Layout(
        autosize=True,
        colorway=config.colorway,
        font=dict(family="Roboto Mono"),
        showlegend=True,
        legend=dict(orientation="h"),
        xaxis=dict(range=[start, now]),
        hovermode="closest",
        hoverlabel=dict(font=dict(family="Roboto Mono")),
        uirevision=True,
        margin=dict(r=50, t=30, b=30, l=60, pad=0),
    )
    try:
        graphJSON = json.dumps(
            dict(data=data, layout=layout), cls=plotly.utils.PlotlyJSONEncoder
        )
    except Exception:
        graphJSON = None
    client.close()
    return graphJSON
Esempio n. 2
0
def create_spectrogram_iot(sensor, time):
    client = MongoClient(os.environ["MONGODB_CLIENT"])
    start, now = helpers.get_time_range(time)
    db = client.iot
    df = pd.DataFrame(
        list(
            db.raw.find(
                {
                    "entity_id": sensor,
                    "timestamp_": {"$gt": start, "$lte": now},
                }
            ).sort([("timestamp_", -1)])
        )
    )
    if len(df) == 0:
        df = pd.DataFrame(
            list(
                db.raw.find({"entity_id": sensor})
                .limit(2)
                .sort([("timestamp_", -1)])
            )
        )

    data = []
    data_spectro = []

    df_s = df
    df_s.index = pd.to_datetime(df_s["timestamp_"])
    df_s["state"] = df_s["state"].astype(float)
    df_s = df_s[["state"]].resample("1T").mean()
    df_s = df_s.interpolate()
    data.append(
        go.Scatter(
            x=df_s.index,
            y=df_s["state"],
            name=sensor,
            line=dict(shape="spline", smoothing=0.7, width=3),
            mode="lines",
        )
    )

    fs = 1 / 60
    nperseg = np.minimum(len(df_s["state"]), 128)
    noverlap = int(nperseg - 1)

    f, t, Sxx = signal.stft(
        # detrend='constant'
        df_s["state"],
        window="blackmanharris",
        nperseg=nperseg,
        noverlap=noverlap,
        fs=fs,
        #     df_s['state'], window='hanning', mode='magnitude',
        #     nperseg=nperseg, noverlap=noverlap, fs=fs
    )

    Sxx = np.abs(Sxx)

    data_spectro.append(
        go.Heatmap(
            x=t,
            y=f,
            z=Sxx,
            name=sensor,
            colorscale="Portland",
            showscale=False,
        )
    )

    layout = go.Layout(
        autosize=True,
        colorway=config.colorway,
        font=dict(family="Roboto Mono"),
        showlegend=True,
        legend=dict(orientation="h"),
        xaxis=dict(range=[start, now]),
        hovermode="closest",
        hoverlabel=dict(font=dict(family="Roboto Mono")),
        uirevision=True,
        margin=dict(r=50, t=30, b=30, l=60, pad=0),
    )
    try:
        graphJSON = json.dumps(
            dict(data=data, layout=layout), cls=plotly.utils.PlotlyJSONEncoder
        )
    except Exception:
        graphJSON = None

    layout_spectro = go.Layout(
        autosize=True,
        font=dict(family="Roboto Mono"),
        showlegend=False,
        # legend=dict(orientation='h'),
        xaxis=dict(range=[start, now]),
        hovermode="closest",
        hoverlabel=dict(font=dict(family="Roboto Mono")),
        uirevision=True,
        margin=dict(r=50, t=30, b=30, l=60, pad=0),
    )

    graphJSON_spectro = json.dumps(
        dict(data=data_spectro, layout=layout_spectro),
        cls=plotly.utils.PlotlyJSONEncoder,
    )

    client.close()
    return graphJSON, graphJSON_spectro
Esempio n. 3
0
def create_anomaly_iot(sensor, time):
    client = MongoClient(os.environ["MONGODB_CLIENT"])
    start, now = helpers.get_time_range(time)
    db = client.iot
    df = pd.DataFrame(
        list(
            db.raw.find(
                {
                    "entity_id": sensor,
                    "timestamp_": {"$gt": start, "$lte": now},
                }
            ).sort([("timestamp_", -1)])
        )
    )
    if len(df) == 0:
        df = pd.DataFrame(
            list(
                db.raw.find({"entity_id": sensor})
                .limit(2)
                .sort([("timestamp_", -1)])
            )
        )

    df_s = df
    df_s.index = pd.to_datetime(df_s["timestamp_"])
    df_s["state"] = df_s["state"].astype(float)
    df_s = df_s[["state"]].resample("1T").mean()
    df_s = df_s.interpolate()

    # fs = 1 / 60
    # nperseg = np.minimum(len(df_s["state"]), 128)
    # noverlap = int(nperseg - 1)

    # f, t, Sxx = signal.stft(
    #     # detrend='constant'
    #     df_s['state'], window='blackmanharris', nperseg=nperseg,
    #     noverlap=noverlap, fs=fs,
    #     #     df_s['state'], window='hanning', mode='magnitude',
    #     nperseg=nperseg, noverlap=noverlap, fs=fs
    # )

    f = np.arange(1, 21)
    Sxx = signal.cwt(df_s["state"], signal.ricker, f)
    t = df_s.index

    Sxx = np.abs(Sxx)
    # Sxx = np.log(Sxx)

    scaler = preprocessing.MinMaxScaler()

    ntrain = int(len(Sxx.T) / 2)

    X = pd.DataFrame(Sxx.T, index=t)

    X_train = scaler.fit_transform(X.iloc[:ntrain, :])

    X_test = scaler.transform(X.iloc[ntrain:, :])

    pca = PCA(n_components=2, svd_solver="full")

    X_train_pca = pca.fit_transform(X_train)
    X_train_pca = pd.DataFrame(X_train_pca)
    X_train_pca.index = X.iloc[:ntrain, :].index

    X_test_pca = pca.transform(X_test)
    X_test_pca = pd.DataFrame(X_test_pca)
    X_test_pca.index = X.iloc[ntrain:, :].index

    data_train = np.array(X_train_pca.values)
    data_test = np.array(X_test_pca.values)

    cov_mx, inv_cov_mx = cov_matrix(data_train)

    mean_distr = data_train.mean(axis=0)

    dist_test = mahalanobis_dist(
        inv_cov_mx, mean_distr, data_test, verbose=False
    )
    dist_train = mahalanobis_dist(
        inv_cov_mx, mean_distr, data_train, verbose=False
    )

    dist_all = np.concatenate([dist_train, dist_test], axis=0)
    thresh = md_threshold(dist_all)

    df_s["dist"] = dist_all[: len(df_s)]
    df_s["thresh"] = thresh

    data = []
    data_anom = []
    data_spectro = []

    data.append(
        go.Scatter(
            x=df_s.index,
            y=df_s["state"],
            name=sensor,
            line=dict(
                # color='#e3a622',
                shape="spline",
                smoothing=0.7,
                width=3,
            ),
            mode="lines",
        )
    )
    data.append(
        go.Scatter(
            x=df_s[df_s["dist"] > thresh].index,
            y=df_s[df_s["dist"] > thresh]["state"],
            name="anomalies",
            marker=dict(color="#eb2369"),
            mode="markers",
        )
    )
    data_anom.append(
        go.Scatter(
            x=df_s.index,
            y=df_s["dist"],
            name="distance",
            line=dict(color="#4823eb", shape="spline", smoothing=0.7, width=3),
            mode="lines",
        )
    )
    data_anom.append(
        go.Scatter(
            x=df_s.index,
            y=df_s["thresh"],
            name="threshold",
            line=dict(color="#de1b4f", shape="spline", smoothing=0.7, width=3),
            mode="lines",
        )
    )
    data_anom.append(
        go.Scatter(
            x=df_s[df_s["dist"] > thresh].index,
            y=df_s[df_s["dist"] > thresh]["dist"],
            name="anomalies",
            line=dict(color="#de1b4f", shape="spline", smoothing=0.7, width=3),
            mode="markers",
        )
    )
    data_spectro.append(
        go.Heatmap(
            x=t,
            y=f,
            z=np.sqrt(Sxx),
            name=sensor,
            colorscale="Jet",
            showscale=False,
        )
    )

    layout = go.Layout(
        autosize=True,
        colorway=config.colorway,
        font=dict(family="Roboto Mono"),
        showlegend=True,
        legend=dict(orientation="h"),
        xaxis=dict(range=[start, now]),
        hovermode="closest",
        hoverlabel=dict(font=dict(family="Roboto Mono")),
        uirevision=True,
        margin=dict(r=50, t=30, b=30, l=60, pad=0),
    )

    layout_spectro = go.Layout(
        autosize=True,
        font=dict(family="Roboto Mono"),
        showlegend=False,
        # legend=dict(orientation='h'),
        xaxis=dict(range=[start, now]),
        hovermode="closest",
        hoverlabel=dict(font=dict(family="Roboto Mono")),
        uirevision=True,
        margin=dict(r=50, t=30, b=30, l=60, pad=0),
    )

    try:
        graphJSON = json.dumps(
            dict(data=data, layout=layout), cls=plotly.utils.PlotlyJSONEncoder
        )
        graphJSON_anom = json.dumps(
            dict(data=data_anom, layout=layout),
            cls=plotly.utils.PlotlyJSONEncoder,
        )
        graphJSON_spectro = json.dumps(
            dict(data=data_spectro, layout=layout_spectro),
            cls=plotly.utils.PlotlyJSONEncoder,
        )
    except Exception:
        graphJSON = None
        graphJSON_anom = None
        graphJSON_spectro = None

    client.close()
    return graphJSON, graphJSON_anom, graphJSON_spectro
Esempio n. 4
0
def create_map_aprs(script, prop, time):
    params = {
        "none": [0, 0, 0, 0, ""],
        "altitude": [0, 1000, 3.2808, 0, "ft"],
        "speed": [0, 100, 0.621371, 0, "mph"],
        "course": [0, 359, 1, 0, "degrees"],
    }
    client = MongoClient(os.environ["MONGODB_CLIENT"])
    db = client.aprs
    start, now = helpers.get_time_range(time)
    if script == "prefix":
        df = pd.DataFrame(
            list(
                db.raw.find({
                    "script": script,
                    "from": "KK6GPV",
                    "latitude": {
                        "$exists": True,
                        "$ne": None
                    },
                    "timestamp_": {
                        "$gt": start,
                        "$lte": now
                    },
                }).sort([("timestamp_", -1)])))
    else:
        df = pd.DataFrame(
            list(
                db.raw.find({
                    "script": script,
                    "latitude": {
                        "$exists": True,
                        "$ne": None
                    },
                    "timestamp_": {
                        "$gt": start,
                        "$lte": now
                    },
                }).sort([("timestamp_", -1)])))

    if prop == "none":
        data_map = [
            go.Scattermapbox(
                lat=df["latitude"],
                lon=df["longitude"],
                text=df["raw"],
                mode="markers",
                marker=dict(size=10),
            )
        ]
    else:
        cs = config.cs_normal
        if prop == "course":
            cmin = 0
            cmax = 359
            cs = config.cs_circle
        else:
            cmin = df[prop].quantile(0.01)
            cmax = df[prop].quantile(0.99)
        data_map = [
            go.Scattermapbox(
                lat=df["latitude"],
                lon=df["longitude"],
                text=df["raw"],
                mode="markers",
                marker=dict(
                    size=10,
                    color=params[prop][2] * df[prop] + params[prop][3],
                    colorbar=dict(title=params[prop][4]),
                    colorscale=cs,
                    cmin=params[prop][2] * cmin + params[prop][3],
                    cmax=params[prop][2] * cmax + params[prop][3],
                ),
            )
        ]
    layout_map = go.Layout(
        autosize=True,
        font=dict(family="Roboto Mono"),
        showlegend=False,
        hovermode="closest",
        hoverlabel=dict(font=dict(family="Roboto Mono")),
        uirevision=True,
        margin=dict(r=0, t=0, b=0, l=0, pad=0),
        mapbox=dict(
            bearing=0,
            center=dict(lat=30, lon=-95),
            accesstoken=os.environ["MAPBOX_TOKEN"],
            style="mapbox://styles/areed145/ck3j3ab8d0bx31dsp37rshufu",
            pitch=0,
            zoom=6,
        ),
    )

    data_speed = [
        go.Scatter(
            x=df["timestamp_"],
            y=params["speed"][2] * df["speed"] + params["speed"][3],
            name="Speed (mph)",
            line=dict(color="#96F42E", width=3, shape="spline", smoothing=0.3),
            mode="lines",
        ),
    ]

    layout_speed = go.Layout(
        autosize=True,
        height=200,
        hoverlabel=dict(font=dict(family="Roboto Mono")),
        yaxis=dict(
            domain=[0.02, 0.98],
            title="Speed (mph)",
            fixedrange=True,
            titlefont=dict(color="#96F42E"),
        ),
        xaxis=dict(type="date", fixedrange=False, range=[start, now]),
        margin=dict(r=50, t=30, b=30, l=60, pad=0),
        showlegend=False,
    )

    data_alt = [
        go.Scatter(
            x=df["timestamp_"],
            y=params["altitude"][2] * df["altitude"] + params["altitude"][3],
            name="Altitude (ft)",
            line=dict(color="#2ED9F4", width=3, shape="spline", smoothing=0.3),
            mode="lines",
        ),
    ]

    layout_alt = go.Layout(
        autosize=True,
        font=dict(family="Roboto Mono"),
        hoverlabel=dict(font=dict(family="Roboto Mono")),
        height=200,
        yaxis=dict(
            domain=[0.02, 0.98],
            title="Altitude (ft)",
            fixedrange=True,
            titlefont=dict(color="#2ED9F4"),
        ),
        xaxis=dict(type="date", fixedrange=False, range=[start, now]),
        margin=dict(r=50, t=30, b=30, l=60, pad=0),
        showlegend=False,
    )

    data_course = [
        go.Scatter(
            x=df["timestamp_"],
            y=params["course"][2] * df["course"] + params["course"][3],
            name="Course (degrees)",
            line=dict(color="#B02EF4", width=3, shape="spline", smoothing=0.3),
            mode="lines",
        ),
    ]

    layout_course = go.Layout(
        autosize=True,
        font=dict(family="Roboto Mono"),
        hoverlabel=dict(font=dict(family="Roboto Mono")),
        height=200,
        yaxis=dict(
            domain=[0.02, 0.98],
            title="Course (degrees)",
            fixedrange=True,
            titlefont=dict(color="#B02EF4"),
        ),
        xaxis=dict(type="date", fixedrange=False, range=[start, now]),
        margin=dict(r=50, t=30, b=30, l=60, pad=0),
        showlegend=False,
    )

    graphJSON_map = json.dumps(
        dict(data=data_map, layout=layout_map),
        cls=plotly.utils.PlotlyJSONEncoder,
    )

    graphJSON_speed = json.dumps(
        dict(data=data_speed, layout=layout_speed),
        cls=plotly.utils.PlotlyJSONEncoder,
    )

    graphJSON_alt = json.dumps(
        dict(data=data_alt, layout=layout_alt),
        cls=plotly.utils.PlotlyJSONEncoder,
    )

    graphJSON_course = json.dumps(
        dict(data=data_course, layout=layout_course),
        cls=plotly.utils.PlotlyJSONEncoder,
    )

    df["timestamp_"] = df["timestamp_"].apply(
        lambda x: x.strftime("%Y-%m-%d %H:%M:%S"))
    df["latitude"] = np.round(df["latitude"], 3)
    df["longitude"] = np.round(df["longitude"], 3)
    df["speed"] = np.round(df["speed"], 2)
    df["altitude"] = np.round(df["altitude"], 1)
    df["course"] = np.round(df["course"], 1)
    df = df.fillna("")
    rows = []
    for _, row in df.iterrows():
        r = {}
        r["timestamp_"] = row["timestamp_"]
        r["from"] = row["from"]
        r["to"] = row["to"]
        r["via"] = row["via"]
        # r['latitude'] = row['latitude']
        # r['longitude'] = row['longitude']
        r["speed"] = row["speed"]
        r["altitude"] = row["altitude"]
        r["course"] = row["course"]
        r["comment"] = row["comment"]
        rows.append(r)
    client.close()
    return (
        graphJSON_map,
        graphJSON_speed,
        graphJSON_alt,
        graphJSON_course,
        rows,
    )
Esempio n. 5
0
def create_range_aprs(time):
    client = MongoClient(os.environ["MONGODB_CLIENT"])
    lat = 29.780880
    lon = -95.420410

    start, now = helpers.get_time_range(time)
    db = client.aprs
    df = pd.DataFrame(
        list(
            db.raw.find({
                "script": "entry",
                "latitude": {
                    "$exists": True,
                    "$ne": None
                },
                "timestamp_": {
                    "$gt": start,
                    "$lte": now
                },
            }).sort([("timestamp_", -1)])))
    df["dist"] = helpers.haversine_np(lon, lat, df["longitude"],
                                      df["latitude"])
    df["month"] = df["timestamp_"].apply(
        lambda row: str(row.year) + "-" + str(row.month).zfill(2))
    df["dist_"] = np.round(df["dist"] * 1) / 1
    df = df[df["dist"] <= 250]

    c5 = np.array([245 / 256, 200 / 256, 66 / 256, 1])
    c4 = np.array([245 / 256, 218 / 256, 66 / 256, 1])
    c3 = np.array([188 / 256, 245 / 256, 66 / 256, 1])
    c2 = np.array([108 / 256, 201 / 256, 46 / 256, 1])
    c1 = np.array([82 / 256, 138 / 256, 45 / 256, 1])
    c0 = np.array([24 / 256, 110 / 256, 45 / 256, 1])
    total = len(df["month"].unique())
    cm = LinearSegmentedColormap.from_list("custom", [c0, c1, c2, c3, c4, c5],
                                           N=total)

    data = []
    for idx, month in enumerate(df["month"].unique()):
        color = rgb2hex(cm(idx / total))
        df2 = df[df["month"] == month]
        df2 = df2.groupby(by="dist_").count()
        data.append(
            go.Scatter(
                x=df2.index,
                y=df2["_id"],
                name=month,
                line=dict(color=color, width=3, shape="spline", smoothing=0.3),
                mode="lines",
            ), )

    layout = go.Layout(
        autosize=True,
        hoverlabel=dict(font=dict(family="Roboto Mono")),
        yaxis=dict(
            domain=[0.02, 0.98],
            type="log",
            title="Frequency",
            fixedrange=False,
        ),
        xaxis=dict(
            type="log",
            title="Distance (mi)",
            fixedrange=False,
        ),
        margin=dict(r=50, t=30, b=30, l=60, pad=0),
        #     showlegend=False,
    )

    graphJSON = json.dumps(dict(data=data, layout=layout),
                           cls=plotly.utils.PlotlyJSONEncoder)
    client.close()
    return graphJSON
Esempio n. 6
0
def create_wx_figs(time: str, sid: str):
    start, now = helpers.get_time_range(time)
    client = MongoClient(os.environ["MONGODB_CLIENT"])
    db = client.wx
    df_wx_raw = pd.DataFrame(
        list(
            db.raw.find({
                "station_id": sid,
                "obs_time_utc": {
                    "$gt": start,
                    "$lte": now
                },
            }).sort([("obs_time_utc", -1)])))
    client.close()
    df_wx_raw.index = df_wx_raw["obs_time_local"]
    # df_wx_raw = df_wx_raw.tz_localize('UTC').tz_convert('US/Central')

    for col in df_wx_raw.columns:
        try:
            df_wx_raw.loc[df_wx_raw[col] < -50, col] = pd.np.nan
        except Exception:
            pass

    df_wx_raw["cloudbase"] = (
        (df_wx_raw["temp_f"] - df_wx_raw["dewpt_f"]) / 4.4) * 1000 + 50
    df_wx_raw.loc[df_wx_raw["pressure_in"] < 0, "pressure_in"] = pd.np.nan

    # df_wx_raw2 = df_wx_raw.resample('5T').mean().interpolate()
    # df_wx_raw2['dat'] = df_wx_raw2.index
    # df_wx_raw2['temp_delta'] = df_wx_raw2.temp_f.diff()
    # df_wx_raw2['precip_today_delta'] = df_wx_raw2.precip_total.diff()
    # df_wx_raw2.loc[df_wx_raw2['precip_today_delta'] < 0, 'precip_today_delta'] = 0
    # df_wx_raw2['precip_cum_in'] = df_wx_raw2.precip_today_delta.cumsum()
    # df_wx_raw2['pres_delta'] = df_wx_raw2.pressure_in.diff()
    # df_wx_raw2['dat_delta'] = df_wx_raw2.dat.diff().dt.seconds / 360
    # df_wx_raw2['dTdt'] = df_wx_raw2['temp_delta'] / df_wx_raw2['dat_delta']
    # df_wx_raw2['dPdt'] = df_wx_raw2['pres_delta'] / df_wx_raw2['dat_delta']
    # df_wx_raw3 = df_wx_raw2.drop(columns=['dat'])
    # df_wx_raw3 = df_wx_raw3.rolling(20*3).mean().add_suffix('_roll')
    # df_wx_raw = df_wx_raw2.join(df_wx_raw3)

    df_wx_raw["dat"] = df_wx_raw.index
    df_wx_raw.sort_values(by="dat", inplace=True)
    df_wx_raw["temp_delta"] = df_wx_raw.temp_f.diff()
    df_wx_raw["precip_today_delta"] = df_wx_raw.precip_total.diff()
    df_wx_raw.loc[df_wx_raw["precip_today_delta"] < 0,
                  "precip_today_delta"] = 0
    df_wx_raw["precip_cum_in"] = df_wx_raw.precip_today_delta.cumsum()
    df_wx_raw["pres_delta"] = df_wx_raw.pressure_in.diff()
    df_wx_raw["dat_delta"] = df_wx_raw.dat.diff().dt.seconds / 360
    df_wx_raw["dTdt"] = df_wx_raw["temp_delta"] / df_wx_raw["dat_delta"]
    df_wx_raw["dPdt"] = df_wx_raw["pres_delta"] / df_wx_raw["dat_delta"]

    df_wx_raw["date"] = df_wx_raw.index.date
    df_wx_raw["hour"] = df_wx_raw.index.hour

    df_wx_raw.loc[df_wx_raw["wind_speed_mph"] == 0, "wind_cat"] = "calm"
    df_wx_raw.loc[df_wx_raw["wind_speed_mph"] > 0, "wind_cat"] = "0-1"
    df_wx_raw.loc[df_wx_raw["wind_speed_mph"] > 1, "wind_cat"] = "1-2"
    df_wx_raw.loc[df_wx_raw["wind_speed_mph"] > 2, "wind_cat"] = "2-5"
    df_wx_raw.loc[df_wx_raw["wind_speed_mph"] > 5, "wind_cat"] = "5-10"
    df_wx_raw.loc[df_wx_raw["wind_speed_mph"] > 10, "wind_cat"] = ">10"

    df_wx_raw["wind_deg_cat"] = np.floor(df_wx_raw["wind_deg"] / 15) * 15
    df_wx_raw.loc[df_wx_raw["wind_deg_cat"] == 360, "wind_deg_cat"] = 0
    df_wx_raw["wind_deg_cat"] = (
        df_wx_raw["wind_deg_cat"].fillna(0).astype(int).astype(str))

    df_wx_raw.loc[df_wx_raw["wind_speed_mph"] == 0, "wind_deg"] = pd.np.nan

    wind = df_wx_raw[["wind_cat", "wind_deg_cat"]]
    wind.loc[:, "count"] = 1
    # wind['count'] = 1
    ct = len(wind)
    wind = pd.pivot_table(
        wind,
        values="count",
        index=["wind_deg_cat"],
        columns=["wind_cat"],
        aggfunc=np.sum,
    )
    ix = np.arange(0, 360, 5)
    col = ["calm", "0-1", "1-2", "2-5", "5-10", ">10"]
    wind_temp = pd.DataFrame(data=0, index=ix, columns=col)
    for i in ix:
        for j in col:
            try:
                wind_temp.loc[i, j] = wind.loc[str(i), j]
            except Exception:
                pass
    wind_temp = wind_temp.fillna(0)
    wind_temp["calm"] = wind_temp["calm"].mean()
    for col in range(len(wind_temp.columns)):
        try:
            wind_temp.iloc[:, col] = (wind_temp.iloc[:, col] +
                                      wind_temp.iloc[:, col - 1])
        except Exception:
            pass
    wind_temp = np.round(wind_temp / ct * 100, 2)
    wind_temp["wind_cat"] = wind_temp.index

    dt_min = df_wx_raw.index.min()
    dt_max = df_wx_raw.index.max()

    td_max = (max(
        df_wx_raw["temp_f"].max(),
        df_wx_raw["dewpt_f"].max(),
        df_wx_raw["heat_index_f"].max(),
        df_wx_raw["windchill_f"].max(),
    ) + 1)
    td_min = (min(
        df_wx_raw["temp_f"].min(),
        df_wx_raw["dewpt_f"].min(),
        df_wx_raw["heat_index_f"].min(),
        df_wx_raw["windchill_f"].min(),
    ) - 1)

    df_wx_raw.loc[df_wx_raw["heat_index_f"] == df_wx_raw["temp_f"],
                  "heat_index_f"] = pd.np.nan
    df_wx_raw.loc[df_wx_raw["windchill_f"] == df_wx_raw["temp_f"],
                  "windchill_f"] = pd.np.nan

    data_td = [
        go.Scatter(
            x=df_wx_raw.index,
            y=df_wx_raw["temp_f"],
            name="Temperature (F)",
            line=dict(
                color="rgb(255, 95, 63)",
                width=3,
                shape="spline",
                smoothing=0.3,
            ),
            xaxis="x",
            yaxis="y",
            mode="lines",
        ),
        go.Scatter(
            x=df_wx_raw.index,
            y=df_wx_raw["heat_index_f"],
            name="Heat Index (F)",
            line=dict(color="#F42ED0", width=3, shape="spline", smoothing=0.3),
            xaxis="x",
            yaxis="y",
            mode="lines",
        ),
        go.Scatter(
            x=df_wx_raw.index,
            y=df_wx_raw["windchill_f"],
            name="Windchill (F)",
            line=dict(color="#2EE8F4", width=3, shape="spline", smoothing=0.3),
            xaxis="x",
            yaxis="y",
            mode="lines",
        ),
        go.Scatter(
            x=df_wx_raw.index,
            y=df_wx_raw["dewpt_f"],
            name="Dewpoint (F)",
            line=dict(
                color="rgb(63, 127, 255)",
                width=3,
                shape="spline",
                smoothing=0.3,
            ),
            xaxis="x",
            yaxis="y2",
            mode="lines",
        ),
    ]

    layout_td = go.Layout(
        autosize=True,
        font=dict(family="Roboto Mono"),
        hoverlabel=dict(font=dict(family="Roboto Mono")),
        height=200,
        yaxis=dict(
            domain=[0.02, 0.98],
            title="Temperature (F)",
            range=[td_min, td_max],
            fixedrange=True,
            titlefont=dict(family="Roboto Mono", color="rgb(255, 95, 63)"),
        ),
        yaxis2=dict(
            domain=[0.02, 0.98],
            title="Dewpoint (F)",
            overlaying="y",
            side="right",
            range=[td_min, td_max],
            fixedrange=True,
            titlefont=dict(family="Roboto Mono", color="rgb(63, 127, 255)"),
        ),
        xaxis=dict(
            type="date",
            # fixedrange=True,
            range=[dt_min, dt_max],
        ),
        margin=dict(r=50, t=30, b=30, l=60, pad=0),
        showlegend=False,
    )

    data_pr = [
        go.Scatter(
            x=df_wx_raw.index,
            y=df_wx_raw["pressure_in"],
            name="Pressure (inHg)",
            line=dict(
                color="rgb(255, 127, 63)",
                width=3,
                shape="spline",
                smoothing=0.3,
            ),
            xaxis="x",
            yaxis="y",
            mode="lines",
        ),
        go.Scatter(
            x=df_wx_raw.index,
            y=df_wx_raw["humidity"],
            name="Humidity (%)",
            line=dict(
                color="rgb(127, 255, 63)",
                width=3,
                shape="spline",
                smoothing=0.3,
            ),
            xaxis="x",
            yaxis="y2",
            mode="lines",
        ),
    ]

    layout_pr = go.Layout(
        autosize=True,
        height=200,
        font=dict(family="Roboto Mono"),
        hoverlabel=dict(font=dict(family="Roboto Mono")),
        yaxis=dict(
            domain=[0.02, 0.98],
            title="Pressure (inHg)",
            # range=[0,120],
            fixedrange=True,
            titlefont=dict(family="Roboto Mono", color="rgb(255, 127, 63)"),
        ),
        yaxis2=dict(
            domain=[0.02, 0.98],
            title="Humidity (%)",
            overlaying="y",
            side="right",
            # range=[0,120],
            fixedrange=True,
            titlefont=dict(family="Roboto Mono", color="rgb(127, 255, 63)"),
        ),
        xaxis=dict(
            type="date",
            # fixedrange=True,
            range=[dt_min, dt_max],
        ),
        margin=dict(r=50, t=30, b=30, l=60, pad=0),
        showlegend=False,
    )

    data_pc = [
        go.Scatter(
            x=df_wx_raw.index,
            y=df_wx_raw["precip_rate"],
            name="Precip (in/hr)",
            line=dict(
                color="rgb(31, 190, 255)",
                width=3,
                shape="spline",
                smoothing=0.3,
            ),
            xaxis="x",
            yaxis="y",
            mode="lines",
        ),
        go.Scatter(
            x=df_wx_raw.index,
            y=df_wx_raw["precip_cum_in"],
            name="Precip Cumulative (in)",
            line=dict(
                color="rgb(63, 255, 255)",
                width=3,
                shape="spline",
                smoothing=0.3,
            ),
            xaxis="x",
            yaxis="y2",
            mode="lines",
        ),
    ]

    layout_pc = go.Layout(
        autosize=True,
        height=200,
        font=dict(family="Roboto Mono"),
        hoverlabel=dict(font=dict(family="Roboto Mono")),
        yaxis=dict(
            domain=[0.02, 0.98],
            title="Precip (in/hr)",
            # range=[0,120],
            fixedrange=True,
            titlefont=dict(family="Roboto Mono", color="rgb(31, 190, 255)"),
        ),
        yaxis2=dict(
            domain=[0.02, 0.98],
            title="Precip Cumulative (in)",
            overlaying="y",
            side="right",
            # range=[0,120],
            fixedrange=True,
            titlefont=dict(family="Roboto Mono", color="rgb(63, 255, 255)"),
        ),
        xaxis=dict(
            type="date",
            # fixedrange=True,
            range=[dt_min, dt_max],
        ),
        margin=dict(r=50, t=30, b=30, l=60, pad=0),
        showlegend=False,
    )

    data_cb = [
        go.Scatter(
            x=df_wx_raw.index,
            y=df_wx_raw["cloudbase"],
            name="Minimum Cloudbase (ft)",
            line=dict(
                color="rgb(90, 66, 245)",
                width=3,
                shape="spline",
                smoothing=0.3,
            ),
            xaxis="x",
            yaxis="y",
            mode="lines",
        ),
    ]

    layout_cb = go.Layout(
        autosize=True,
        height=200,
        font=dict(family="Roboto Mono"),
        hoverlabel=dict(font=dict(family="Roboto Mono")),
        yaxis=dict(
            domain=[0.02, 0.98],
            title="Minimum Cloudbase (ft)",
            # range=[0,120],
            fixedrange=True,
            titlefont=dict(family="Roboto Mono", color="rgb(90, 66, 245)"),
        ),
        xaxis=dict(
            type="date",
            # fixedrange=True,
            range=[dt_min, dt_max],
        ),
        margin=dict(r=50, t=30, b=30, l=60, pad=0),
        showlegend=False,
    )

    data_wd = [
        go.Scatter(
            x=df_wx_raw.index,
            y=df_wx_raw["wind_deg"],
            name="Wind Direction (degrees)",
            marker=dict(color="rgb(190, 63, 255)", size=8, symbol="x"),
            xaxis="x",
            yaxis="y",
            mode="markers",
        ),
        go.Scatter(
            x=df_wx_raw.index,
            y=df_wx_raw["wind_gust_mph"] * 0.869,
            name="Wind Gust (kts)",
            line=dict(
                color="rgb(31, 190, 15)",
                width=3,
                shape="spline",
                smoothing=0.3,
            ),
            xaxis="x",
            yaxis="y2",
            mode="lines",
        ),
        go.Scatter(
            x=df_wx_raw.index,
            y=df_wx_raw["wind_speed_mph"] * 0.869,
            name="Wind Speed (kts)",
            line=dict(
                color="rgb(127, 255, 31)",
                width=3,
                shape="spline",
                smoothing=0.3,
            ),
            xaxis="x",
            yaxis="y2",
            mode="lines",
        ),
    ]

    layout_wd = go.Layout(
        autosize=True,
        height=200,
        font=dict(family="Roboto Mono"),
        hoverlabel=dict(font=dict(family="Roboto Mono")),
        yaxis=dict(
            domain=[0.02, 0.98],
            title="Wind Direction (degrees)",
            range=[0, 360],
            fixedrange=True,
            titlefont=dict(family="Roboto Mono", color="rgb(190, 63, 255)"),
        ),
        yaxis2=dict(
            domain=[0.02, 0.98],
            title="Wind Speed / Gust (kts)",
            overlaying="y",
            side="right",
            range=[0, df_wx_raw["wind_gust_mph"].max() * 0.869],
            fixedrange=True,
            titlefont=dict(family="Roboto Mono", color="rgb(127, 255, 31)"),
        ),
        xaxis=dict(
            type="date",
            # fixedrange=True,
            range=[dt_min, dt_max],
        ),
        margin=dict(r=50, t=30, b=30, l=60, pad=0),
        showlegend=False,
    )

    data_su = [
        go.Scatter(
            x=df_wx_raw.index,
            y=df_wx_raw["solar"],
            name="Solar Radiation (W/m<sup>2</sup>)",
            line=dict(
                color="rgb(255, 63, 127)",
                width=3,
                shape="spline",
                smoothing=0.3,
            ),
            xaxis="x",
            yaxis="y",
            mode="lines",
        ),
        go.Scatter(
            x=df_wx_raw.index,
            y=df_wx_raw["uv"],
            name="UV",
            line=dict(
                color="rgb(255, 190, 63)",
                width=3,
                shape="spline",
                smoothing=0.3,
            ),
            xaxis="x",
            yaxis="y2",
            mode="lines",
        ),
    ]

    layout_su = go.Layout(
        autosize=True,
        height=200,
        font=dict(family="Roboto Mono"),
        hoverlabel=dict(font=dict(family="Roboto Mono")),
        yaxis=dict(
            domain=[0.02, 0.98],
            title="Solar Radiation (W/m<sup>2</sup>)",
            # range=[0,120],
            fixedrange=True,
            titlefont=dict(family="Roboto Mono", color="rgb(255, 63, 127)"),
        ),
        yaxis2=dict(
            domain=[0.02, 0.98],
            title="UV",
            overlaying="y",
            side="right",
            # range=[0,120],
            fixedrange=True,
            titlefont=dict(family="Roboto Mono", color="rgb(255, 190, 63)"),
        ),
        xaxis=dict(
            type="date",
            # fixedrange=True,
            range=[dt_min, dt_max],
        ),
        margin=dict(r=50, t=30, b=30, l=60, pad=0),
        showlegend=False,
    )

    t1 = go.Barpolar(
        r=wind_temp[">10"],
        theta=wind_temp["wind_cat"],
        name=">10 mph",
        width=10,
        base=0,
        marker=dict(color="#ffff00", line=dict(color="#ffff00")),
    )
    t2 = go.Barpolar(
        r=wind_temp["5-10"],
        theta=wind_temp["wind_cat"],
        name="5-10 mph",
        width=10,
        base=0,
        marker=dict(color="#ffcc00", line=dict(color="#ffcc00")),
    )
    t3 = go.Barpolar(
        r=wind_temp["2-5"],
        theta=wind_temp["wind_cat"],
        name="2-5 mph",
        width=10,
        base=0,
        marker=dict(color="#bfff00", line=dict(color="#bfff00")),
    )
    t4 = go.Barpolar(
        r=wind_temp["1-2"],
        theta=wind_temp["wind_cat"],
        name="1-2 mph",
        width=10,
        base=0,
        marker=dict(color="#00cc00", line=dict(color="#00cc00")),
    )
    t5 = go.Barpolar(
        r=wind_temp["0-1"],
        theta=wind_temp["wind_cat"],
        name="0-1 mph",
        width=10,
        base=0,
        marker=dict(color="#009999", line=dict(color="#009999")),
    )
    t6 = go.Barpolar(
        r=wind_temp["calm"],
        theta=wind_temp["wind_cat"],
        name="calm",
        width=10,
        base=0,
        marker=dict(color="#3366ff", line=dict(color="#3366ff")),
    )

    data_wr = [t1, t2, t3, t4, t5, t6]

    layout_wr = go.Layout(
        font=dict(family="Roboto Mono"),
        hoverlabel=dict(font=dict(family="Roboto Mono")),
        polar=dict(
            radialaxis=dict(
                # visible = False,
                showline=False,
                showticklabels=False,
                ticks="",
                range=[0, wind_temp[">10"].max()],
            ),
            angularaxis=dict(
                rotation=90,
                direction="clockwise",
            ),
        ),
    )

    graphJSON_td = json.dumps(
        dict(data=data_td, layout=layout_td),
        cls=plotly.utils.PlotlyJSONEncoder,
    )

    graphJSON_pr = json.dumps(
        dict(data=data_pr, layout=layout_pr),
        cls=plotly.utils.PlotlyJSONEncoder,
    )

    graphJSON_pc = json.dumps(
        dict(data=data_pc, layout=layout_pc),
        cls=plotly.utils.PlotlyJSONEncoder,
    )

    graphJSON_cb = json.dumps(
        dict(data=data_cb, layout=layout_cb),
        cls=plotly.utils.PlotlyJSONEncoder,
    )

    graphJSON_wd = json.dumps(
        dict(data=data_wd, layout=layout_wd),
        cls=plotly.utils.PlotlyJSONEncoder,
    )

    graphJSON_su = json.dumps(
        dict(data=data_su, layout=layout_su),
        cls=plotly.utils.PlotlyJSONEncoder,
    )

    graphJSON_wr = json.dumps(
        dict(data=data_wr, layout=layout_wr),
        cls=plotly.utils.PlotlyJSONEncoder,
    )

    graphJSON_thp = helpers.create_3d_plot(
        df_wx_raw,
        "temp_f",
        "dewpt_f",
        "humidity",
        config.cs_normal,
        "Temperature (F)",
        "Dewpoint (F)",
        "Humidity (%)",
        "rgb(255, 95, 63)",
        "rgb(255, 127, 63)",
        "rgb(63, 127, 255)",
    )

    freq = "1T"
    mult = 2
    try:
        df_wx_lt = pd.DataFrame(
            list(
                db.lightning.find({
                    "timestamp": {
                        "$gt": start,
                        "$lte": now
                    },
                }).sort([("timestamp", -1)])))
        client.close()
        # df_wx_lt = df_wx_lt[df_wx_lt["energy"] > 0]
        df_wx_lt["distance"] = 0.621 * df_wx_lt["distance"]
        df_wx_lt["distance"] = np.round(df_wx_lt["distance"] / mult, 0) * mult
        df_wx_lt = df_wx_lt.drop(columns=["_id", "type", "energy"])
        df_pivot = df_wx_lt.pivot_table(index="timestamp",
                                        columns="distance",
                                        aggfunc=len).fillna(0)
        df_pivot = df_pivot.resample(freq, base=dt_min.minute).sum()
        df_pivotT = df_pivot.T
        ymax = df_pivot.columns.max()
        df_pivotT_reindexed = df_pivotT.reindex(
            index=np.linspace(0, int(ymax), int(((1 / mult) * int(ymax)) + 1)))
        df_pivot = df_pivotT_reindexed.T.fillna(0)
        df_pivot = df_pivot.tz_localize("UTC")
        df_pivot = df_pivot.tz_convert("US/Central")
        df_pivot = df_pivot.tz_localize(None)
        idx = pd.date_range(
            dt_min.replace(second=0, microsecond=0),
            dt_max.replace(second=0, microsecond=0),
            freq=freq,
        )
        df_fill = pd.DataFrame(index=idx, columns=df_pivot.columns).fillna(0)
        df_fill = df_fill.tz_localize(None)
        df_pivot.index.name = None
        df_pivot = df_fill.add(df_pivot, fill_value=0)
        df_pivot = df_pivot.replace(0, pd.np.nan)
    except Exception:
        idx = pd.date_range(
            dt_min.replace(second=0, microsecond=0),
            dt_max.replace(second=0, microsecond=0),
            freq=freq,
        )
        df_fill = pd.DataFrame(
            index=idx,
            columns=np.linspace(0, int(50), int(((1 / mult) * int(50)) + 1)),
        ).fillna(0)
        df_pivot = df_fill.tz_localize(None)
        df_pivot = df_pivot.replace(0, pd.np.nan)

    data_lt = [
        go.Heatmap(
            x=df_pivot.index,
            y=df_pivot.columns,
            z=df_pivot.T.values,
            colorscale=config.scl_lightning,
            zmax=np.nanquantile(df_pivot, 0.95),
            zmin=1,
            zauto=False,
            showscale=False,
            # connectgaps=True,
        )
    ]

    layout_lt = go.Layout(
        autosize=True,
        font=dict(family="Roboto Mono"),
        hoverlabel=dict(font=dict(family="Roboto Mono")),
        height=300,
        yaxis=dict(
            range=[0, 30],
            domain=[0.02, 0.98],
            title="Distance (mi)",
            ticks="",
            fixedrange=True,
            titlefont=dict(family="Roboto Mono", color="rgb(255, 210, 63)"),
        ),
        xaxis=dict(
            type="date",
            range=[dt_min, dt_max],
            ticks="",
        ),
        margin=dict(r=50, t=30, b=35, l=60, pad=0),
        showlegend=False,
    )

    graphJSON_lt = json.dumps(
        dict(data=data_lt, layout=layout_lt),
        cls=plotly.utils.PlotlyJSONEncoder,
    )

    return (
        graphJSON_td,
        graphJSON_pr,
        graphJSON_cb,
        graphJSON_pc,
        graphJSON_wd,
        graphJSON_su,
        graphJSON_wr,
        graphJSON_thp,
        graphJSON_lt,
    )