#-*- coding: utf-8 -*-
"""
simple plot example where a user defined
glsl kernel maps x domain to y domain.

:author: Nicolas 'keksnicoh' Heimann
"""
from gllib.plot.domain import NumpyDomain
from gllib.plot.graph import Line2d
from gllib.plot.app import plot2d

import numpy as np

def plot_main(plotter):
    xf = np.array([0.01*x for x in np.arange(1000)], dtype=np.float32)
    plotter.graphs['test1'] = Line2d(NumpyDomain(xf), 'y=sin(x)*cos(2*x)')

plot2d(plot_main, axis=[10, 2], origin=[0,-1])
from gllib.plot.field import Field
import numpy as np 

#
# numpy array describes the texture size and format. 
# third component in shape is the length of a vertex
# 1 => GL_RED
# 2 => GL_RG
# 3 => GL_RGB
# 4 => GL_RGBA
#
# if third component not defined then 
# it will set it automatically to 1 so
# only GL_RED channel is used.
#
w = 1000
h = 1000

# lets define 3 color channels, so GL_RGB is used here.
color_channels = 3
data = np.random.random_sample(w*h*color_channels).reshape(w, h, color_channels)

def plot_main(plotter): 
    domain = FieldDomain.from_numpy(data)
    plotter.graphs['my_awesome_random_field'] = Field(domain)

plot2d(plot_main, axis=data.shape, origin=[-.5, .5], 
    title  = 'random numpy array of float{} vectors'.format(color_channels),
    xlabel = 'pixel space y',
    ylabel = 'pixel space x',
)
"""
simple field plot with custom color kernel
and custom data kernel.

:author: Nicolas 'keksnicoh' Heimann 
"""
from gllib.plot.app import plot2d
from gllib.plot.domain import FieldDomain
from gllib.plot.field import Field
from gllib.plot.color.schemes import ColorMap
import numpy as np

color_scheme = ColorMap("IDL_Hardcandy", colorrange=[-1, 1])


def plot_main(plotter):
    plotter.graphs["my_awesome_random_field"] = Field(
        color_scheme=color_scheme, data_kernel="fragment_color=vec4(sin(10*sqrt(x.x*x.x+x.y+x.y)), 0, 0, 1)"
    )


plot2d(
    plot_main,
    axis=[1, 1],
    origin=[0, 0],
    title="who is your colors?",
    xlabel="gravitiy divided by fuel price",
    ylabel="sin($pi$) times new york times",
    colorlegend=color_scheme,
)
        # which area is shown which allows to generate a transformation
        # on the domain so the domain will fill (screen_left, screen_right).
        #
        # This allows to fly around the plot and automatically rerender
        # the glsl function
        domains=RealAxis(),

        # in simplest way the kernel is a string passed 
        # to the glsl vertex shader. one can define  
        # wrapper methods which e.g. converts sympy
        # equations to a string.
        kernel='y=-0.005*x*x+sin(x)*cos(2*x)+exp(0.05*x)-1;',

        color_scheme=color_scheme,

        # lets make the line thick yo
        width=2
    )

plot2d(plot_main, 
    axis=[100, 7], 
    origin=[25,-3], 
    title='glsl plot with color scheme and dynamic domain',
    # XXX
    # - since we use a custom source in color_scheme
    #   one gets trouble here. Define a more stable concept here
    #   and fix this ... 

    # this will render a color legend on the right side of the plot
    #colorlegend=color_scheme  
)
#-*- coding: utf-8 -*-
"""
scipy fft example based on 
http://docs.scipy.org/doc/scipy/reference/tutorial/fftpack.html
:author: Nicolas 'keksnicoh' Heimann 
"""
from gllib.plot.domain import NumpyDomain
from gllib.plot.graph import Line2d
from gllib.plot.app import plot2d

import numpy as np 
from scipy.fftpack import fft

N = 1200
T = 1.0 / 800.0

def plot_main(plotter):
    x    = np.linspace(0.0, N*T, N)
    y    = np.sin(50.0 * 2.0*np.pi*x) + 0.2*np.sin(80.0 * 2.0*np.pi*x)
    yf   = 2.0/N * np.abs(fft(y)[0:0.5*N][0:N/2])
    xf   = np.linspace(0.0, 1.0/(2.0*T), N/2)
    data = np.column_stack((xf,yf))
    plotter.graphs['test1'] = Line2d([NumpyDomain(xf.astype(np.float32)), NumpyDomain(yf.astype(np.float32))], width=1)

plot2d(plot_main, axis=[200, 1])
import os,sys
import numpy as np
BASE = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(BASE, 'opengl_plot_prototype'))

from gllib.plot.app import plot2d 
from gllib.plot.domain import FieldDomain 
from gllib.plot.field import Field
from gllib.plot.color.schemes import ColorMap

COLOR_SCHEME = ColorMap('IDL_Blue-Pastel-Red', colorrange=[-.2,0.9])



def plot(plotter):
    plotter.graphs['foo'] = Field(FieldDomain.from_numpy(result),
        top_left=(s_range[0]+0.225, w_range[-1]),
        bottom_right=(s_range[-1]+0.225, w_range[0]),
        #data_kernel="fragment_color = texture(tex[0], vec2(x.y, x.x));",
        color_scheme=COLOR_SCHEME)

plot2d(plot, axis=[s_range[-1]-s_range[0], w_range[-1]-w_range[0]], origin=[-s_range[0]-0.225, w_range[0]],
    xlabel='sqrt(s)',
    colorlegend=COLOR_SCHEME,
    ylabel='sin^2(phi)',
    title='Forward Backward Asymmetry',
    height=300,
    width=500)