# Path: pyfusion/data/utils.py
# def peak_freq(signal,timebase,minfreq=0,maxfreq=1.e18):
#     """
#     TODO: old code: needs review
#     this function only has a basic unittest to make sure it returns
#     the correct freq in a simple case.
#     """
#     timebase = array(timebase)
#     sig_fft = fft.fft(signal)
#     sample_time = float(mean(timebase[1:]-timebase[:-1]))
# 
#     #SRH modification, frequencies seemed a little bit off because of the -1 in the denominator
#     #Here we are trusting numpy....
#     #fft_freqs = (1./sample_time)*arange(len(sig_fft)).astype(float)/(len(sig_fft)-1)
#     fft_freqs = fft.fftfreq(len(sig_fft),d=sample_time)
#     # only show up to nyquist freq
#     new_len = len(sig_fft)/2
#     sig_fft = sig_fft[:new_len]
#     fft_freqs = fft_freqs[:new_len]
#     [minfreq_elmt,maxfreq_elmt] = searchsorted(fft_freqs,[minfreq,maxfreq])
#     sig_fft = sig_fft[minfreq_elmt:maxfreq_elmt]
#     fft_freqs = fft_freqs[minfreq_elmt:maxfreq_elmt]
#     
#     peak_elmt = (argsort(abs(sig_fft)))[-1]
#     return [fft_freqs[peak_elmt], peak_elmt]
# 
# def split_names(names, pad=' '):
#     """ Given an array of strings, return an array of the part of the string
#     (e.g. channel name) that varies, and optionally the prefix and suffix.
#     The array of varying parts is first in the tuple in case others are not
#     wanted.  This is used to make the x labels of phase plots simpler and smaller.
#     e.g.
#     >>> split_names(['MP01','MP10'])
#     (['01','10'], 'MP', '')
#     """
#     # make a new array with elements padded to the same length with <pad>
#     nms = []
#     maxlen = max([len(nm) for nm in names])
#     for nm in names:
#         nmarr = [c for c in nm]
#         while len(nmarr)< maxlen: nmarr.append(pad)
#         nms.append(nmarr)
#     
#     # the following numpy array comparisons look simple, but require the name string
#     # to be exploded into chars.  Although a single string can be interchangeably 
#     # referred to as a string or array of chars, these arrays they have to be 
#     # re-constituted before return.
#     #
#     #    for nm in nms:     # for each nm
#     #find the first mismatch - first will be the first char of the extracted arr
#     nms_arr=array(nms)
#     first=0
#     while (first < maxlen and
#            (nms_arr[:,first] == nms_arr[0,first]).all()):
#         first += 1
#     # and the last        
#     last = maxlen-1
#     while ((last >= 0) and
#            (nms_arr[:,last] == nms_arr[0,last]).all()):
#         last -= 1
# 
# 
#     # check for no mismatch        
#     if first==maxlen: return(['' for nm in names], ''.join(nms[0]),'')
#     # otherwise return, (no need for special code for the case of no match at all)
#     return(([''.join(s) for s in nms_arr[:,first:last+1]],
#             ''.join(nms_arr[0,0:first]),
#             ''.join(nms_arr[0,last+1:maxlen+1])))


from matplotlib.widgets import CheckButtons
from pyfusion.data.utils import peak_freq, split_names
import pylab as pl
import numpy as np
import pyfusion
    import pylab as pl
    import pylab as pl
    yZero = m1*xZero + b1
    return (xZero, yZero)

@register("FlucStruc")
def fsplot_phase(input_data, closed=True, hold=0):
    """ plot the phase of a flucstruc, optionally replicating the last point
    at the beginning (if closed=True).
    This version does not yet attempt to take into account angles, or check 
    that adjacent channels are adjacent (i.e. ch2-ch1, ch2-c2 etc).
    Channel names are taken from the fs and plotted abbreviated

    1/1/2011: TODO This appears to work only for database=None config
    1/17/2011:  bdb: May be fixed - I had used channel instead of channel.name
    """
    # extract by channels
    ch1n,ch2n,ch12n,dp = [],[],[],[]
    # bdb this line should be replaced by a call to a routine names something
    #like <plotted_width> to help in deciding if the label will fit on the 
    #current graph.
    if (2*len(input_data.dphase)*len(input_data.dphase[0].item.channel_1.name))> 50:
        sep = '\n-'
    else: sep = '-'
    #sep = '-'
    for dpn in input_data.dphase:
        ch1n.append(dpn.item.channel_1.name)
        ch2n.append(dpn.item.channel_2.name)
        ch12n.append(dpn.item.channel_1.name+sep+dpn.item.channel_2.name)
        dp.append(dpn.item.delta)

    short_names_1,p,s = split_names(ch1n)  # need to break up loops to do this

# Path: pyfusion/data/utils.py
# def peak_freq(signal,timebase,minfreq=0,maxfreq=1.e18):
#     """
#     TODO: old code: needs review
#     this function only has a basic unittest to make sure it returns
#     the correct freq in a simple case.
#     """
#     timebase = array(timebase)
#     sig_fft = fft.fft(signal)
#     sample_time = float(mean(timebase[1:]-timebase[:-1]))
# 
#     #SRH modification, frequencies seemed a little bit off because of the -1 in the denominator
#     #Here we are trusting numpy....
#     #fft_freqs = (1./sample_time)*arange(len(sig_fft)).astype(float)/(len(sig_fft)-1)
#     fft_freqs = fft.fftfreq(len(sig_fft),d=sample_time)
#     # only show up to nyquist freq
#     new_len = len(sig_fft)/2
#     sig_fft = sig_fft[:new_len]
#     fft_freqs = fft_freqs[:new_len]
#     [minfreq_elmt,maxfreq_elmt] = searchsorted(fft_freqs,[minfreq,maxfreq])
#     sig_fft = sig_fft[minfreq_elmt:maxfreq_elmt]
#     fft_freqs = fft_freqs[minfreq_elmt:maxfreq_elmt]
#     
#     peak_elmt = (argsort(abs(sig_fft)))[-1]
#     return [fft_freqs[peak_elmt], peak_elmt]
# 
# def split_names(names, pad=' '):
#     """ Given an array of strings, return an array of the part of the string
#     (e.g. channel name) that varies, and optionally the prefix and suffix.
#     The array of varying parts is first in the tuple in case others are not
#     wanted.  This is used to make the x labels of phase plots simpler and smaller.
#     e.g.
#     >>> split_names(['MP01','MP10'])
#     (['01','10'], 'MP', '')
#     """
#     # make a new array with elements padded to the same length with <pad>
#     nms = []
#     maxlen = max([len(nm) for nm in names])
#     for nm in names:
#         nmarr = [c for c in nm]
#         while len(nmarr)< maxlen: nmarr.append(pad)
#         nms.append(nmarr)
#     
#     # the following numpy array comparisons look simple, but require the name string
#     # to be exploded into chars.  Although a single string can be interchangeably 
#     # referred to as a string or array of chars, these arrays they have to be 
#     # re-constituted before return.
#     #
#     #    for nm in nms:     # for each nm
#     #find the first mismatch - first will be the first char of the extracted arr
#     nms_arr=array(nms)
#     first=0
#     while (first < maxlen and
#            (nms_arr[:,first] == nms_arr[0,first]).all()):
#         first += 1
#     # and the last        
#     last = maxlen-1
#     while ((last >= 0) and
#            (nms_arr[:,last] == nms_arr[0,last]).all()):
#         last -= 1
# 
# 
#     # check for no mismatch        
#     if first==maxlen: return(['' for nm in names], ''.join(nms[0]),'')
#     # otherwise return, (no need for special code for the case of no match at all)
#     return(([''.join(s) for s in nms_arr[:,first:last+1]],
#             ''.join(nms_arr[0,0:first]),
#             ''.join(nms_arr[0,last+1:maxlen+1])))


# Path: pyfusion/data/plots.py
from matplotlib.widgets import CheckButtons
from pyfusion.data.utils import peak_freq, split_names
import pylab as pl
import numpy as np
import pyfusion
    import pylab as pl
    import pylab as pl

    yZero = m1*xZero + b1
    return (xZero, yZero)

@register("FlucStruc")
def fsplot_phase(input_data, closed=True, hold=0):
    """ plot the phase of a flucstruc, optionally replicating the last point
    at the beginning (if closed=True).
    This version does not yet attempt to take into account angles, or check 
    that adjacent channels are adjacent (i.e. ch2-ch1, ch2-c2 etc).
    Channel names are taken from the fs and plotted abbreviated

    1/1/2011: TODO This appears to work only for database=None config
    1/17/2011:  bdb: May be fixed - I had used channel instead of channel.name
    """
    # extract by channels
    ch1n,ch2n,ch12n,dp = [],[],[],[]
    # bdb this line should be replaced by a call to a routine names something
    #like <plotted_width> to help in deciding if the label will fit on the 
    #current graph.
    if (2*len(input_data.dphase)*len(input_data.dphase[0].item.channel_1.name))> 50:
        sep = '\n-'
    else: sep = '-'
    #sep = '-'
    for dpn in input_data.dphase:
        ch1n.append(dpn.item.channel_1.name)
        ch2n.append(dpn.item.channel_2.name)
        ch12n.append(dpn.item.channel_1.name+sep+dpn.item.channel_2.name)
        dp.append(dpn.item.delta)

    short_names_1,p,s = split_names(ch1n)  # need to break up loops to do this

    short_names_2,p,s = split_names(ch2n)  # 