
import sys

sys.path.insert(0, r'/home/bb1131207/Condensed Function Libraries/')


from refl1d.names import *

import refl1d.dist as dist

Si = Material(formula="Si", density=2.33)
SiO2 = Material(formula="SiO2", density=2.2)
FeCu = Material(formula="FeCu", density=8.5) # Cu at 8.92 Fe at 7.874
Ti = Material(formula="Ti", density=4.506)
TiO2 = Material(formula="TiO2", density=4.23)
Al2O3 = Material(formula="Al2O3", density=3.99)
Bi2Se3 = Material(formula="Bi2Se3", density=7.51)
Pt = Material(formula="Pt", density=21.4)
Pt_x = Material(formula="Pt", density=21.4)
Pt_cap = Material(formula="Pt", density=21.4, name = 'Pt_cap')
CoB = Material(formula="CoB", density=6.788)
Ru = Material(formula="Ru", density=12.2)

CoB_x = Material(formula="CoB", density=6.788)
Ru_x = Material(formula="Ru", density=12.2)
Ta = Material(formula="Ta", density=16.65)

Ta_x = Material(formula="Ta", density=16.65)
Ru_cap = Material(formula="Ru", density=12.2, name = 'Ru_cap')
Mo = Material(formula = 'Mo', density = 10.2)
Ta_Layer = Slab(material=Ta, thickness=15, interface=2)

Bi2Se3_x = Material(formula="Bi2Se3", density=7.51)

from MML_Fit import *

probes = probe_loader("TI_Ta20_ML")
zed = 5
step = False

roughness_ranges = {'Al2O3' : (0.1,8), 'Ta' : (0.7,7), 'Bi2Se3' : (0.7,10), 'Ru_cap' : (0.7,12)}

#Mo10_sample = fitter.FitMultilayer([Al2O3, Bi2Se3, Mo], [Pt, CoB, Ru], [Ru_cap], [0,200, 10 ,8,6,5,5], roughnesses = 1, roughnesses_ranges = roughness_ranges)
#NB_sample = fitter.FitMultilayer([Al2O3, Bi2Se3], [Pt, CoB, Ru], [Pt_cap], [0,200 ,8,6,5,40], roughnesses = 1, roughnesses_ranges = roughness_ranges)
#Ta15_sample = fitter.FitMultilayer([Al2O3, Bi2Se3, Ta], [Pt, CoB, Ru], [Pt_cap], [0,200, 15 ,8,6,5,40], roughnesses = 1, roughnesses_ranges = roughness_ranges)
Ta20_sample = FitMultilayer([Al2O3, Bi2Se3, Ta], [Pt, CoB, Ru], [Ru_cap], [0,200, 20 ,8,6,5,5], 
	roughnesses = 1, roughnesses_ranges = roughness_ranges, gradient = True)


#rhoM_roughness = Parameter.default(1, name= 'RhoM Roughness')
#rhoM_roughness.range(0.1, 6)
#rhoM_roughness_gradient = Parameter.default(-0.2, name= 'RhoM Roughness Gradient')
#rhoM_roughness_gradient.range(-1, 0)



#samples = fitter.Assign_per_layer_per_field(Mo10_sample, [0.7,0.2,0.1, 0.05], rhoM_roughness = rhoM_roughness) 
samples = samples = Assign_uniform_magnetisation_function(Ta20_sample, [0.7,0.2,0.1,0.05,0.01], oxidation = True)#, rhoM_roughness = rhoM_roughness, rhoM_roughness_gradient = rhoM_roughness_gradient) 
#samples = fitter.Assign_magnetisation_convolving_function_per_layer(Mo10_sample, [0.7,0.2], rhoM_roughness = rhoM_roughness, rhoM_roughness_gradient = rhoM_roughness_gradient) 

experiments = []

for probe, sample in zip(probes, samples):

	experiment = Experiment(probe = probe, sample = sample.sample, dz = zed, step_interfaces=step)
	experiment.plot_profile()
	experiments += [experiment]



from scipy.interpolate import interp1d
import scipy.stats as stats
x_ray = r"/home/bb1131207/PNR May 2025 Files/X-Ray Data/MBE-MET20241206-02 XRR__extracted.dat"
#x_ray = r"/home/bb1131207/PNR May 2025 Files/X-Ray Data/MBE-MET20240725-06 XRR__extracted.dat"
x_ray = r"/mnt/ceph/home/bb1131207/PNR May 2025 Files/X-Ray Data/MET20241206-02 XRR 0.307789 deg __extracted.dat"
x_ray_off = r"/mnt/ceph/home/bb1131207/PNR May 2025 Files/X-Ray Data/MET20241206-02 XRR 0.280276 deg Of__extracted.dat"
probe = load_X_ray_probe(x_ray, name = 'Ta20 X-ray', offspec_file = x_ray_off, limit_T = [1.1/3, 6/2+1.1], theta_offset = -0, intensity = 2.4, 
						sample_broadening = -0.0)
dQoQ = 0.03
probe.intensity.range(1, 2.8)
probe.background.range(1e-9, 1e2)
probe.sample_broadening.range(-0.03, 0.03)
#probe.theta_offset.range(-0.1,0.1)
#probe.H.value = -1e9

sample_xray = Ta20_sample.duplicate_structure(magnetic_modelling = False)
den  = Parameter.default(9, name = 'Bi2Se3 density (x_ray)')
den.range(6,20)

BS_t = Parameter.default(200, name = 'Bi2Se3 thickness (x_ray)')
BS_t.range(160,240)
sample_xray.sample['Bi2Se3'].material = Bi2Se3_x
sample_xray.sample['Bi2Se3'].material.density = den
sample_xray.sample['Bi2Se3'].thickness = BS_t

experiment = Experiment(probe = probe, sample = sample_xray.sample, dz = zed, step_interfaces=step, name = 'Mo10 X-ray')

Terrace  = Parameter.default(0, name = 'Terrace')
#Terrace.range(0,2)
sample_xray.sample['Bi2Se3'].thickness += Terrace
terrace_int= 0
from scipy.interpolate import interp1d
import scipy.stats as stats
import refl1d.dist as dist


Terrace  = Parameter.default(0, name = 'Terrace')
#Terrace.range(0,2)
sample_xray.sample['Bi2Se3'].thickness += Terrace

heights = r"/home/bb1131207/Condensed Function Libraries/Height Dist"
z,r = np.loadtxt(heights, unpack = True)
x = 1*(z*10**10 + 4.93) ; y = r*10**-10
#x = np.linspace(-10,11,42)
#y = np.zeros(41)
#y[0] = 0.5 ;  y[-1] = 0.5 ; y[20] = 0.5
x = np.append(x,x[-1] + (x[-1]-x[-2]))
hist = stats.rv_histogram((y,x))
#cdf = stats.norm.cdf
cdf = hist.cdf

weights = dist.Weights(cdf = cdf, scale = 1, loc = 0, edges = 1*np.array([-10,-9,-5,-1,-1,5,9,10]))
#weights = dist.Weights(cdf= 'norm4')

#experiment = dist.DistributionExperiment(experiment, P = Terrace, distribution = weights, coherent = False)




experiment.plot_profile()
experiments +=[experiment]
problem = MultiFitProblem(experiments)
