#!/usr/bin/python
import os, re, sys
import matplotlib as matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np

# Generate scatterplots of the AMPS and LDAS SNR
# Using the data set of recovered iHope S6 hardware
# Injections generated by Ian Harry
# Description of methods:
# https://www.lsc-group.phys.uwm.edu/ligovirgo/cbcnote/S6Plan/130422155409DQandVetoesHWInjs_in_AMPS_data
# Location of files on the CIT cluster:
# /home/spxiwh/S6/AMPS-STRAIN/hardware_inj_test/dump_snr/[H,L]1-HWINJS_WITH_SNR.dat
# 02013-05-15 (JD 2456428)
# Grant David Meadors
# g m e a d o r s @  u m i c h . e d u

def scatterplotter(observatory):
    print 'Plotting SNR scatterplot for this observatory:'
    print observatory

    # Assume that the file is in the local directory:
    # Since this is S6 data, the detector will be number 1,
    # Either H1 or L1
    SNRfile = observatory + '1-HWINJS_WITH_SNR.dat'
    LDAS, AMPS = np.loadtxt(SNRfile, delimiter = ' ', usecols=(0, 1), unpack=True)
    plt.figure()
    p1 = plt.scatter(LDAS, AMPS)
    plt.grid(True)
    plt.xlabel('SNR before feedforward (LDAS)')
    plt.ylabel('SNR after feedforward (AMPS)')
    plt.title(observatory + '1 iHope SNR after vs before feedforward')
    plt.savefig(observatory + '1_AMPSplotSNR.png')
    plt.savefig(observatory + '1_AMPSplotSNR.pdf')
    plt.close()

    plt.figure()
    p2 = plt.scatter(LDAS, (AMPS-LDAS)/LDAS)
    plt.grid(True)
    plt.xlabel('SNR before feedforward (LDAS)')
    plt.ylabel('SNR ratio: (AMPS-LDAS)/LDAS')
    plt.title(observatory + '1 iHope SNR (after-before)/before feedforward')
    plt.savefig(observatory + '1_AMPSratioSNR.png')
    plt.savefig(observatory + '1_AMPSratioSNR.pdf')
    plt.close()

scatterplotter(sys.argv[1])
