#!/usr/bin/env python3
"""
In this example we demonstrate how to plot a simulation result with
axes in different units (nbins, mm, degs and QyQz).
"""
import bornagain as ba
from bornagain import angstrom, ba_plot as bp, deg, nm
from matplotlib import rcParams


def get_sample():
    # Materials
    air_color = (0.90, 0.93, 0.97)
    air_mat = ba.RefractiveMaterial("Air", air_color, 0, 0)
    particle_color = (0.86, 0.24, 0.18)
    particle_mat = ba.RefractiveMaterial("Particle", particle_color, 6e-5, 2e-08)
    substrate_color = (0.28, 0.57, 0.82)
    substrate_mat = ba.RefractiveMaterial("Substrate", substrate_color, 6e-06, 2e-08)

    # Particles
    R = 2.5*nm
    ff = ba.Spheroid(R, R)
    particle = ba.Particle(particle_mat, ff)

    # Interference function
    lattice = ba.SquareLattice2D(10*nm, 2*deg)
    layout = ba.Crystal2D(particle, lattice)
    pdf = ba.Profile2DCauchy(50*nm, 50*nm, 0)
    layout.setDecayFunction(pdf)

    # Layers
    l_air = ba.Layer(air_mat)
    l_air.deposit2D(layout)
    l_substrate = ba.Layer(substrate_mat)

    # Sample
    sample = ba.Sample()
    sample.addLayer(l_air)
    sample.addLayer(l_substrate)
    return sample


def get_simulation(sample, wavelength, alpha_i):
    beam = ba.Beam(1e9, wavelength, alpha_i)
    n = 200
    detector = ba.SphericalDetector(n, -1*deg, 1*deg, n, 0, 1*deg)
    simulation = ba.ScatteringSimulation(beam, sample, detector)
    return simulation

if __name__ == '__main__':
    sample = get_sample()
    wavelength = 0.04*nm
    alpha_i = 0.2*deg
    simulation = get_simulation(sample, wavelength, alpha_i)
    result = simulation.simulate()

    trafo = ba.FrameTrafo.ScatteringToQ(wavelength, alpha_i)
    res2 = trafo.transformedDatafield(result)

    ba.showSample3D(sample, sample_size=100*nm, seed=0)
    bp.plot_datafield(res2, unit_aspect=1)
    bp.plt.show()
