-
Notifications
You must be signed in to change notification settings - Fork 2
Scripting and programming tips
Manuel Sanchez del Rio edited this page Jan 23, 2025
·
4 revisions
This contains some tips to help users writing scripts.
This simulates the data in the angle.xx files in shadow3
Add a Script widget attached at your element and write:
import numpy
#
# extract input data
#
x = in_object_1.footprint.get_columns([1,2,3])
v = in_object_1.footprint.get_columns([4,5,6])
beamline_elements_list = in_object_1.beamline.get_beamline_elements()
normal = beamline_elements_list[-1].get_optical_element().get_optical_surface_instance().get_normal(x)
normal *= -1.0 # conic surfaces return downwards normal
print(x.shape, v.shape, normal.shape)
#
# calculate angle between normal and output direction
#
# note vector_dot accepts array(npoints,3) and x, v and normal are array(3,npoints).
from shadow4.tools.arrayofvectors import vector_dot
cos_angles = vector_dot(v.T, normal.T)
mrad = 1e3 * (numpy.pi / 2 - numpy.arccos(cos_angles) )
#
# make the plot
#
from srxraylib.plot.gol import plot_scatter
plot_scatter(mrad, in_object_1.footprint.get_column(23), plot_histograms=0, xtitle="Grazing angle [mrad]", ytitle="Intensity" )
For publication-quality plots you can export SHADOW data to an script and use directly matplotlib. For example:
import matplotlib.pylab as plt
def plot_ticket_plotxy(tkt, col_h=1, col_v=3, title="", xtitle="", ytitle="", filename="", hfactor=1e4,vfactor=1e4):
cmap = plt.cm.jet # Greys #cm.coolwarm
fx = 9
fy = 9
figure = plt.figure(figsize=(fx,fy))
left, width = 0.13, 0.61
bottom, height = 0.09, 0.61
bottom_h = left_h = left + width + 0.02
rect_scatter = [left, bottom, width, height]
rect_histx = [left, bottom_h, width, 0.2]
rect_histy = [left_h, bottom, 0.2, height]
#
#main plot
#
axScatter = figure.add_axes(rect_scatter)
axScatter.set_xlabel(xtitle,fontsize=20)
axScatter.set_ylabel(ytitle,fontsize=20)
axScatter.axis(xmin=hfactor*tkt["xrange"][0],xmax=hfactor*tkt["xrange"][1])
axScatter.axis(ymin=vfactor*tkt["yrange"][0],ymax=vfactor*tkt["yrange"][1])
axScatter.set_aspect(1.0)
axScatter.pcolormesh(hfactor*tkt["bin_h_edges"], vfactor*tkt["bin_v_edges"], tkt["histogram"].T,cmap=cmap)
axScatter.tick_params(labelsize=24)
#
#histograms
#
axHistx = figure.add_axes(rect_histx, sharex=axScatter)
axHisty = figure.add_axes(rect_histy, sharey=axScatter)
tmp_h_b = []
tmp_h_h = []
for s,t,v in zip(hfactor*tkt["bin_h_left"],hfactor*tkt["bin_h_right"],tkt["histogram_h"]):
tmp_h_b.append(s)
tmp_h_h.append(v)
tmp_h_b.append(t)
tmp_h_h.append(v)
tmp_v_b = []
tmp_v_h = []
for s,t,v in zip(vfactor*tkt["bin_v_left"],vfactor*tkt["bin_v_right"],tkt["histogram_v"]):
tmp_v_b.append(s)
tmp_v_h.append(v)
tmp_v_b.append(t)
tmp_v_h.append(v)
axHistx.plot(tmp_h_b,tmp_h_h)
axHisty.plot(tmp_v_h,tmp_v_b)
# supress ordinates labels ans ticks
axHistx.get_yaxis().set_visible(False)
axHisty.get_xaxis().set_visible(False)
# supress abscissas labels (keep ticks)
for tl in axHistx.get_xticklabels(): tl.set_visible(False)
for tl in axHisty.get_yticklabels(): tl.set_visible(False)
if title != None:
axHistx.set_title(title,fontsize=20)
if filename != "":
plt.savefig(filename)
print("File written to disk: %s"%filename)
plt.show()
return tkt
beam = in_object_1.beam
print(beam)
tkt = beam.histo2(1, 3, ref=23, nolost=1, nbins=301, xrange=[-0.04e-6, 0.04e-6], yrange=[-0.04e-6, 0.04e-6], calculate_widths=1)
print("Transmission: %6.3f percent" % (100 * beam.intensity(nolost=1) / beam.N))
print((tkt["fwhm_h"], tkt["fwhm_v"]))
plot_ticket_plotxy(tkt, hfactor=1e9, vfactor=1e9, xtitle="H [nm]", ytitle="V [nm]", filename="",
title="S4 %3.1f x %3.1f nm$^2$" % (tkt["fwhm_h"] * 1e9, tkt["fwhm_v"] * 1e9))