#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 15 22:57:58 2019

@author: RachelCampo
"""

import numpy as np
from astropy.io import fits
import matplotlib.pyplot as plt


platelist = fits.open('/Users/RachelCampo/Desktop/Research/Data/Other Spectra/platelist.fits')
specdatalist = fits.open('/Users/RachelCampo/Desktop/Research/CIV-Variability/Programs/DR14Q_v4_4.fits')


wavelength = specdatalist[1].data['loglam']
redshift = specdatalist[1].data['Z']
SN_Ratio = platelist.data['PLATESN2']
BAL_Indicator = specdatalist[1].data['BI_CIV'] 
plate_quality = platelist.data['PLATEQUALITY']
plate_number = specdatalist[1].data['PLATE']

Plate_RA = platelist.data['RACEN']
Plate_Dec = platelist.data['DECCEN']
RA = specdatalist[1].data['RA']
Dec = specdatalist[1].data['DEC']


success_list = np.array([])
data_list = np.array([])
null_list = np.array([])

dec_list = np.array([])
ra_list = np.array([])


for x, y in enumerate(specdatalist):
    RAdiff = abs(Plate_RA[x] - RA[x])
    ra_list = np.concatenate([ra_list, [RAdiff]])
    
   
for x, y in enumerate(specdatalist):
    DECdiff = abs(Plate_Dec[x] - Dec[x])
    dec_list = np.concatenate([dec_list, [DECdiff]])

  
for i, x in enumerate(dec_list):
    condition = x
    if x > 1:
        failure = i
        null_list = np.concatenate([null_list , [failure]])
    else:
        success = i
        success_list = np.concatenate([success_list, [success]])
        
for i, x in enumerate(ra_list):
    condition = x
    if x > 1:
        failure = i
        null_list = np.concatenate([null_list, [failure]])
    else:
        success = i
        success_list = np.concatenate([success_list, [success]])


for i, x in enumerate(BAL_Indicator):
    condition = x
    if x > 0:
        failure = i
        null_list = np.concatenate([null_list, [failure]])
    else:
        success = i
        success_list = np.concatenate([success_list, [success]])


for i, x in enumerate(SN_Ratio):
    condition = x
    if x >= 2:
        success = i
        data_list = np.concatenate([data_list, [success]])
    else:
        failure = i
        success_list = np.concatenate([success_list, [failure]])


for i, x in enumerate(plate_number):
    condition = plate_quality
    if plate_quality == 'good':
        success = i
        data_list = np.concatenate([data_list, [success]])
    else:
        failure = i
        success_list = np.concatenate([success_list, [failure]])


for i, x in enumerate(redshift):
    condition = x
    if x > 1.7 and x < 2.6:
        success = i
        data_list = np.concatenate([data_list, [success]])
    else:
        failure = i
        success_list = np.concatenate([success_list, [failure]])
        

for x in success_list:
    if (dec_list[x] <= 1) and (ra_list[x] <= 1) and (BAL_Indicator[x] == 0) and (SN_Ratio[x] >= 2) and (plate_quality[x] == 'good') and (redshift[x] > 1.7) and (redshift[x] < 2.6):
        success = x
        data_list = np.concatenate([data_list, [success]])
    else:
        failure = x
        null_list = np.concatenate([null_list, [failure]])


#this searches through the data and sorts out what spectra will be considered
#when graphing the data.
        
for i in data_list:
    flux = np.log10(1e17 * data_list[1].data['flux'][i])
    rest_frame_Z = 10**wavelength / (1+i)
    
    plt.plot(rest_frame_Z, flux, 'g')
    plt.ylabel('log(flux [erg/s/cm^2/Angstrom])')
    plt.xlabel('Wavelength [Angstrom]')
    plt.show()
    
#here, I created a for loop for graphing each redshift in the data_list
#array. I adjusted the redshift to the rest frame and then it takes the rest
#frame redshift and creates a graph for each piece of data with respect to 
#flux.