w_412 = 0.56
w_443 = 0.73
w_490 = 0.71
w_510 = 0.36
w_560 = 0.01

"""
Explanation: E-CEO Challenge #3 Evaluation
Weights
Define the weight of each wavelength
End of explanation
"""


run_id = '0000000-150701000046181-oozie-oozi-W'
run_meta = 'http://sb-10-16-10-55.dev.terradue.int:50075/streamFile/ciop/run/participant-d/0000000-150701000046181-oozie-oozi-W/results.metalink?'
participant = 'participant-d'

"""
Explanation: Run
Provide the run information:
* run id
* run metalink containing the 3 by 3 kernel extractions
* participant
End of explanation
"""


import glob
import pandas as pd
from scipy.stats.stats import pearsonr
import numpy
import math

"""
Explanation: Define all imports in a single cell
End of explanation
"""


!curl $run_meta | aria2c -d $participant -M -

path = participant # use your path

allFiles = glob.glob(path + "/*.txt")
frame = pd.DataFrame()
list_ = []
for file_ in allFiles:
    df = pd.read_csv(file_,index_col=None, header=0)
    list_.append(df)
    frame = pd.concat(list_)

"""
Explanation: Manage run results
Download the results and aggregate them in a single Pandas dataframe
End of explanation
"""


len(frame.index)

"""
Explanation: Number of points extracted from MERIS level 2 products
End of explanation
"""


insitu_path = './insitu/AAOT.csv'
insitu = pd.read_csv(insitu_path)
frame_full = pd.DataFrame.merge(frame.query('Name == "AAOT"'), insitu, how='inner', on = ['Date', 'ORBIT'])

frame_xxx= frame_full[['Rrs_413_mean', 'rho_wn_IS_412']].dropna()
r_aaot_412 = pearsonr(frame_xxx.ix[:,0], frame_xxx.ix[:,1])[0] 

print(str(len(frame_xxx.index)) + " observations for band @412")

frame_xxx= frame_full[['Rrs_443_mean', 'rho_wn_IS_443']].dropna()
r_aaot_443 = pearsonr(frame_xxx.ix[:,0], frame_xxx.ix[:,1])[0] 

print(str(len(frame_xxx.index)) + " observations for band @443")

frame_xxx= frame_full[['Rrs_490_mean', 'rho_wn_IS_490']].dropna()
r_aaot_490 = pearsonr(frame_xxx.ix[:,0], frame_xxx.ix[:,1])[0] 

print(str(len(frame_xxx.index)) + " observations for band @490")

r_aaot_510 = 0
print("0 observations for band @510")

frame_xxx= frame_full[['Rrs_510_mean', 'rho_wn_IS_560']].dropna()
r_aaot_560 = pearsonr(frame_xxx.ix[:,0], frame_xxx.ix[:,1])[0] 

print(str(len(frame_xxx.index)) + " observations for band @560")

insitu_path = './insitu/BOUSS.csv'
insitu = pd.read_csv(insitu_path)
frame_full = pd.DataFrame.merge(frame.query('Name == "BOUS"'), insitu, how='inner', on = ['Date', 'ORBIT'])

frame_xxx= frame_full[['Rrs_413_mean', 'rho_wn_IS_412']].dropna()
r_bous_412 = pearsonr(frame_xxx.ix[:,0], frame_xxx.ix[:,1])[0] 

print(str(len(frame_xxx.index)) + " observations for band @412")

frame_xxx= frame_full[['Rrs_443_mean', 'rho_wn_IS_443']].dropna()
r_bous_443 = pearsonr(frame_xxx.ix[:,0], frame_xxx.ix[:,1])[0] 

print(str(len(frame_xxx.index)) + " observations for band @443")

frame_xxx= frame_full[['Rrs_490_mean', 'rho_wn_IS_490']].dropna()
r_bous_490 = pearsonr(frame_xxx.ix[:,0], frame_xxx.ix[:,1])[0] 

print(str(len(frame_xxx.index)) + " observations for band @490")

frame_xxx= frame_full[['Rrs_510_mean', 'rho_wn_IS_510']].dropna()
r_bous_510 = pearsonr(frame_xxx.ix[:,0], frame_xxx.ix[:,1])[0]

print(str(len(frame_xxx.index)) + " observations for band @510")

frame_xxx= frame_full[['Rrs_560_mean', 'rho_wn_IS_560']].dropna()
r_bous_560 = pearsonr(frame_xxx.ix[:,0], frame_xxx.ix[:,1])[0]

print(str(len(frame_xxx.index)) + " observations for band @560")

insitu_path = './insitu/MOBY.csv'
insitu = pd.read_csv(insitu_path)
frame_full = pd.DataFrame.merge(frame.query('Name == "MOBY"'), insitu, how='inner', on = ['Date', 'ORBIT'])

frame_xxx= frame_full[['Rrs_413_mean', 'rho_wn_IS_412']].dropna()
r_moby_412 = pearsonr(frame_xxx.ix[:,0], frame_xxx.ix[:,1])[0] 

print(str(len(frame_xxx.index)) + " observations for band @12")

frame_xxx= frame_full[['Rrs_443_mean', 'rho_wn_IS_443']].dropna()
r_moby_443 = pearsonr(frame_xxx.ix[:,0], frame_xxx.ix[:,1])[0] 

print(str(len(frame_xxx.index)) + " observations for band @443")

frame_xxx= frame_full[['Rrs_490_mean', 'rho_wn_IS_490']].dropna()
r_moby_490 = pearsonr(frame_xxx.ix[:,0], frame_xxx.ix[:,1])[0]

print(str(len(frame_xxx.index)) + " observations for band @490")

frame_xxx= frame_full[['Rrs_510_mean', 'rho_wn_IS_510']].dropna()
r_moby_510 = pearsonr(frame_xxx.ix[:,0], frame_xxx.ix[:,1])[0] 

print(str(len(frame_xxx.index)) + " observations for band @510")

frame_xxx= frame_full[['Rrs_560_mean', 'rho_wn_IS_560']].dropna()
r_moby_560 = pearsonr(frame_xxx.ix[:,0], frame_xxx.ix[:,1])[0] 

print(str(len(frame_xxx.index)) + " observations for band @560")

[r_aaot_412, r_aaot_443, r_aaot_490, r_aaot_510, r_aaot_560]

[r_bous_412, r_bous_443, r_bous_490, r_bous_510, r_bous_560]


[r_moby_412, r_moby_443, r_moby_490, r_moby_510, r_moby_560]

r_final = (numpy.mean([r_bous_412, r_moby_412, r_aaot_412]) * w_412 \
    + numpy.mean([r_bous_443, r_moby_443, r_aaot_443]) * w_443 \
    + numpy.mean([r_bous_490, r_moby_490, r_aaot_490]) * w_490 \
    + numpy.mean([r_bous_510, r_moby_510, r_aaot_510]) * w_510 \
    + numpy.mean([r_bous_560, r_moby_560, r_aaot_560]) * w_560) \
    / (w_412 + w_443 + w_490 + w_510 + w_560)

r_final

"""
Explanation: Calculate Pearson
For all three sites, AAOT, BOUSSOLE and MOBY, calculate the Pearson factor for each band.

Note AAOT does not have measurements for band @510

AAOT site
End of explanation
"""


