# derived from database_write.py 
# writes all the 4 database files suite in one run.
# first used to add radiation peaking data

# V02 with treatment of m2 cases
# writes a datafile for given:
# nmode, NBIthreshold, radiation threshold, MODE threshold
# one line for each pulse in list0 with NBI threshold exceeded.
# The radiation threshold condition is: 
# Prad > 0.7*Ptot & t > NBIstart + 0.3s (to avoid initial NBI transients)
# it is stupidly hidden on the powers function of databasefunctions.py
# Mode recognized if at least 12 points (100 ms) are above threshold
#npulse   # pulse
#B0       # B0 T
#Ip       # Ip MA  mean on Pt1-Pt2 interval
#Pt1 Pt2  # NBI on an off times (threshold)
#t_rad    # time of radiation crossing a thershold fraction of total power
#P        # max NBI power MW                                                           <<< Pmax
#...      # relative power variation in shorter interval after mode on (def. 0.2 s)   <<<<<<<<  ADD <<< dP1
#...      #      "                   in longer interval after onset (def. 0.4 s)      <<<<<<<<  ADD <<< dP2

#q95      # q95, 
#tril     # lower triangularity
#triu     # upper triang
#bNm      # max betaN                                                                 <<< bNmax
#bNt      # time of max betaN                                                         <<< tbNmax
#bNon     # betaN at mode onset
#...      # beta var. at the end of shorter interval after mode on (def. 0.2 s)      <<<<<<<<  ADD <<< dbN1
#...      # beta var. at the end of longer interval after onset (def. 0.4 s)         <<<<<<<<  ADD <<< dbN2

#Mmax     # Mode maximum amplitude before t_rad  (0 if no mode)
           #Also available max amplitude after t_rad (mode in tail)
#Mdur     # Mode duration to last time above threshold   (0 if no mode)          
#ONt      # Mode onset time   (0 if no mode)                                          <<< Mtime
#ONtype   # rapidity of intial growh  (10 if no mode)         NOT WELL DEFINED        <<< Mtype
#ONa      # onset amplitude: mean on 12 pts after threshold crossing  (0 if no mode)  <<< Mamp0
           #the condition for mode existence is at least 12 pts above threshold. POSSIBLE FALSE ALARM
#...      # average amplitude in shorter interval after onset (def. 0.2 s)      <<<<<<<<  ADD <<< Mamp1
#...      # average amplitude in longer interval after onset (def. 0.4 s)       <<<<<<<<  ADD <<< Mamp2
 
#kg1l     # line average density from interferomery
#hrtx     # line average density from HRTS

import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
import ftu
#import databasefunctions as df



# IMPROVE USE AND DESCRIPTION OF   dt1 and dt2
# WRITE INPUTS IN A HEADER
# TRY FIXED FORMATS

def producefile(pulselist, outfile, param):
    # file header with options
    headformats = ', parameter={0}'
    firstline = pulselist + headformats.format(param)

    names = 'npulse B0    Ip   '                          # pulse numner, B0 (T), Ip (MA)
    formats = '{0} {1:5.2f} {2:5.2f} '
    #names = names + ' Pt1     Pt2     t_rad  Pmax '            # NBI on and off t, radiation time, max NBI (MW)
    #formats = formats + '{3:7.3f} {4:7.3f} {5:7.3f} {6:6.3f} '
    formats = formats + '\n'
    
    with open(pulselist) as flist, open(outfile, 'w') as fo:
        fo.write(firstline + '\n')
        fo.write(names + '\n')
        for line in flist:
            npulse = int(line)
            print npulse
            
            Ip = ftu.ftudata(npulse, 'ZZZZED.IPL').get(t=param)[1]
            
            B0 = ftu.ftudata(npulse, '%E.BTOR').get(t=param)[1]
            
            
            fo.write(formats.format(npulse, B0, Ip))

    fo.close()
    flist.close()

# General options
time = 0.1    # tempo per la lettura

#pulselist = 'list_test' 
pulselist = 'Esercizio4_list.txt'
outfile = 'Esercizio4_outfile.txt'

producefile(pulselist, outfile, time)


