#!/usr/bin/env python3
"""
An example of defining a reflectometry instrument
for a time-of-flight experiment with relative Q resolution.

The scan uses a purely q_z-defined beam (no explicit angle or wavelength).
Relative resolution means each Q point is smeared by sigma = 0.03 * q_z.
Compare TOFRWithResolution.py for the pointwise-resolution equivalent.
Note that these approaches work with SLD-based materials only.
"""
import numpy as np
import bornagain as ba
from bornagain import ba_plot as bp, std_samples, nm


def get_sample():
    return std_samples.alternating_layers()


def get_simulation(sample):
    "Specular simulation with a qz-defined beam and relative Q resolution"
    n = 500

    qzs = np.linspace(0.01, 1, n)  # qz-values
    distr = ba.DistributionGaussian(0., 1., 25, 2.)

    scan = ba.QzScan(qzs)
    scan.setRelativeQResolution(distr, 0.03)

    return ba.SpecularSimulation(scan, sample)


if __name__ == '__main__':
    sample = get_sample()
    simulation = get_simulation(sample)
    result = simulation.simulate()
    plotargs = bp.parse_commandline()
    bp.plot_datafield(result, **plotargs)
    bp.export(**plotargs)
