""" 
Lettura di un database in formato testo
uso du numpy.genfromtxt
display a tre variabili
gestione interattiva di eventi
"""

import numpy as np
import matplotlib.pyplot as plt
#import ppfeg

def onpick(event):
    ind = event.ind
    print ('#, npulse, Ip, betamax:', ind, npulse[ind], Ip[ind], np.take(bNm, ind))

wall = 'ILW'
case = '_y_'; skip_header=1
nmode = 1
filename = 'n' + str(nmode) + case + wall

a = np.genfromtxt(filename, names=True, skip_header=skip_header)


npulse = a['npulse']   # pulse number
Ip = np.abs(a['Ip'])   # plasma current (MA)
P = a['Pmax']          # NBI peak power (MW)
bNm = a['bNmax']       # maximum betaN

x = P; xlab ='Peak power'
y = bNm; ylab ='Peak betaN'
z = Ip; zlab ='Ip'

fig = plt.figure(facecolor='w')
#cmap = plt.get_cmap('viridis')
plt.scatter(x, y, 40, z, cmap='hot', picker=True)
plt.xlabel(xlab)
plt.ylabel(ylab)
plt.title('file: ' + filename + '  Color: ' + zlab)
plt.colorbar()
# connessione con gestione evento mouse click
fig.canvas.mpl_connect('pick_event', onpick)
plt.show()

plt.savefig('Esercizio2.png', dpi=300)

"""
npulse = a['npulse']   # pulse number
B0 = np.abs(a['B0'])   # vacuum toroidal field (T)
Ip = np.abs(a['Ip'])   # plasma current (MA)
Pt1 = a['Pt1']         # time of NBI on (4.5 MW threshold crossing)
Pt2 = a['Pt2']         # time of NBI off    "
t_rad = a['t_rad']     # time of smoothed Prad > 0.7*(smoothed PNBI+PICRH), no analysis after this
P = a['Pmax']          # NBI peak power (MW)
q95 = a['q95']         # q95 at mode onset or at max beta
tril = a['tril']       # lower triang. at mode onset or at max beta
triu = a['triu']       # upper triang. at mode onset or at max beta 
bNm = a['bNmax']       # maximum betaN
bNt = a['tbNmax']      # time of maximum betaN
bNon = a['bNon']       # betaN at mode onset time (max betaN if there is no onset)

Mmax = a['Mmax']       # maximum zero-to-peak mode amplitude (T)
Mdur = a['Mdur']       # mode duration above threshold
ONt = a['Mtime']       # mode onset time (threshold crossing), zero if there is no onset
ONtype = a['Mtype']    # rapidity of mode onset
M0 = a['Mamp0']        # mode amplitude just after onset (average on 100ms), zero if there is no onset
M1 = a['Mamp1']        # mode amplitude averaged from onset to 0.2 s later, zero if there is no onset
M2 = a['Mamp2']        # mode amplitude averaged from onset to 0.3 s later, zero if there is no onset

dbN1 = a['dbN1']       # beta variation from 0.3 s before onset to onset (to check trend)
dbN2 = a['dbN2']       # beta variation from mode onset to 0.3 s later

Hon = a['Hon']         # H98 at mode onset 
dH1 = a['dH1']         # H98 variation from 0.3 s before onset to onset (to check trend)
dH2 = a['dH2']         # H98 variation from mode onset to 0.3 s later

Pon = a['Pon']         # power at mode onset time (zero if there is no onset)
dP1 = a['dP1']         # max. power relative variation in an interval of 0.3s before mode onset
dP2 = a['dP2']         # max. power relative variation in an interval of 0.3s after mode onset

kg1l = a['kg1l']       # Line-average density from interferometer (KG1L/LAD3), at mode onset or at max beta
hrtx = a['hrtx']       # Line-average density from HRTX/NELA, at mode onset or at max beta

Pt1s = a['Pt1s']       # time of smoothed NBI on (threshold crossing)
Pt2s = a['Pt2s']       # time of smoothed NBI off    "
Ps = a['Pmaxs']        # smoothed NBI peak power (MW)

kg1v = a['kg1v']       # Line-integral density from interferometer (KG1V/LID3), at mode onset or at max beta
li3m = a['li3m']       # Internal inductance at mode onset or at max beta

BOLOon = a['BOLOon']     # Bolometer at mode onset or at max beta
dBOLO1 = a['dBOLO1']     # Bolometer variation before mode onset or at max beta
dBOLO2 = a['dBOLO2']     # Bolometer after mode onset or at max beta
xpeakon = a['xpeakon']   # Soft-x peaking at mode onset or at max beta
dxpeak1 = a['dxpeak1']   # Soft-x peaking variation before mode onset or at max beta
dxpeak2 = a['dxpeak2']   # Soft-x peaking variation after mode onset or at max beta
"""