""" 
Title: sample_detection.py

Date/Version: 17 April 2024 (v0.1)

Author: Matthew G. Finley (NASA GSFC/University of Maryland)

Contact: matthew.g.finley@nasa.gov

Description: Simple Python script to download sample MMS FGM data and analyze it for anomalous samples. This script accompanies the manuscript
    'Generalized Time-Series Analysis for In-Situ Spacecraft Observations: Anomaly Detection and Data Prioritization using Principal Components Analysis and Unsupervised Clustering' 
    by M.G. Finley, M. Martinez-Ledesma, W.R. Paterson, M.R. Argall, D.M. Miles, J.C. Dorelli, and E. Zesta.

Notes: The results in this example will differ slightly from the results shown in the above manuscript. The data, as mentioned in the manuscript, was previously downsampled to match the
    slow survey rate (8 Hz) that occurs in this CDF; this was omitted for simplicity in this example. This script also relies on the 'anomaly_detection_utilities.py' library file, which contains
    the functions needed to perform detrending and anomaly detection.
"""

import pyspedas
from pytplot import tplot, get_data, store_data
import numpy as np
import matplotlib.pyplot as plt
import datetime
from sample_detection_utilities import anomaly_detection, detrend

# Get data from Pyspedas
fgm_vars = pyspedas.mms.fgm(trange=['2015-12-14/13:00', '2015-12-14/14:00'], time_clip=True)
# Load individual elements of data
fgm_bvec_gsm_t = get_data('mms1_fgm_b_gsm_srvy_l2_bvec').times
fgm_bvec_gsm_dt = [datetime.datetime.utcfromtimestamp(fgm_bvec_gsm_t[i]) for i in range(len(fgm_bvec_gsm_t))]
fgm_bvec_gsm_vals = get_data('mms1_fgm_b_gsm_srvy_l2_bvec').y
fgm_bvec_gsm_bz = fgm_bvec_gsm_vals[:,2]

# Detrend
bz_detrended, bz_trend = detrend(fgm_bvec_gsm_bz, 16 * 20)

# Detect anomalies
nu = 0.3
fs = 16 # Fast survey rate -- although in the manuscript this was resampled down to 8 Hz, since this interval has slow survey data too.
window_length = 5 # 5 sec
bz_anomaly_flags = anomaly_detection(bz_detrended, fs, window_length, nu)

# Develop cmap for scatterplots
bz_colormap = ['red' if bz_anomaly_flags[i] > 0 else 'black' for i in range(len(bz_anomaly_flags))]

# Plot
plt.figure(figsize=(12,8))
plt.subplot(3,1,1)
plt.scatter(fgm_bvec_gsm_dt, bz_detrended, color='black', s=1, marker='.')
plt.ylabel('MMS FGM\nDetrended\n$B_{Z,GSM}$ (nT)')
plt.ylim([-20,25])
plt.grid()
plt.subplot(3,1,2)
plt.scatter(fgm_bvec_gsm_dt, bz_anomaly_flags, marker='.', color=bz_colormap, s=2)
plt.yticks([0, 1])
plt.grid()
plt.ylabel('Sample Labels\n(Binary)')
from matplotlib.lines import Line2D
custom_lines = [Line2D([0], [0], color='black', linewidth=2),
                Line2D([0], [0], color='red', linewidth=2)]
plt.legend(custom_lines, ['Nominal', 'Anomalous'])
plt.subplot(3,1,3)
plt.scatter(fgm_bvec_gsm_dt, bz_detrended, color=bz_colormap, s=1, marker='.')
plt.ylabel('MMS FGM\nDetrended\n$B_{Z,GSM}$ (nT)')
plt.ylim([-20,25])
plt.grid()
plt.legend(custom_lines, ['Nominal', 'Anomalous'])
plt.suptitle('MMS Observations (14 Dec 2015)\nN = 1 h; L = 5 s; $\\nu$ = 0.3')
plt.tight_layout()
plt.savefig('example_output.png')




