fix a segmentation fault with the HklSampleReflection in the binding.
[hkl.git] / Documentation / sphinx / source / pyplots / trajectory_simple.py
blobcb1bb91b008574b62e0ff2b8084c8140557de1a8
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 import math
5 import numpy
7 import matplotlib.pyplot as plt
9 from gi.repository import GLib
10 from gi.repository import Hkl
13 sample = Hkl.Sample.new("toto")
14 lattice = Hkl.Lattice.new(1.54, 1.54, 1.54,
15 math.radians(90),
16 math.radians(90),
17 math.radians(90))
18 sample.lattice_set(lattice)
20 detector = Hkl.Detector.factory_new(Hkl.DetectorType(0))
22 factory = Hkl.factories()['K6C']
23 geometry = factory.create_new_geometry()
24 axes_names = geometry.axes_names_get()
26 # set the initial position
27 geometry.axes_values_set([0, 120, 0, -90, 0, 60], Hkl.UnitEnum.USER)
29 # get all engines for a given configuration
30 engines = factory.create_new_engine_list()
32 # prepare the engines to work with the related geometry, detector and
33 # sample
34 engines.init(geometry, detector, sample)
36 #[0, 0, 1] -> [0, 1, 1]
37 n = 10
38 h = numpy.linspace(0, 0, n + 1)
39 k = numpy.linspace(0, 1, n + 1)
40 l = numpy.linspace(1, 1, n + 1)
42 # get the hkl engine
43 hkl = engines.engine_get_by_name("hkl")
44 pseudo_axes_names = ["h", "k", "l"]
46 # compute the trajectory
47 motors_positions = []
48 for idx, hh, kk, ll in zip(range(n), h, k, l):
49 try:
50 solutions = hkl.pseudo_axes_values_set([hh, kk, ll],
51 Hkl.UnitEnum.USER)
52 first_solution = solutions.items()[0]
53 # if no exception raised we have at least one solution
54 # move the diffractometer to the solution
55 engines.select_solution(first_solution)
56 motors_positions.append(geometry.axes_values_get(Hkl.UnitEnum.USER))
57 except GLib.GError, err:
58 pass
60 plt.subplot(1, 2, 1)
61 plt.title("motors trajectory (1st solution)")
62 # reorder the motors_positions for the plot
63 motors_positions = numpy.array(motors_positions).T
64 for y, name in zip(motors_positions, axes_names):
65 plt.plot(y, 'o-', label=name)
66 plt.legend(loc=2)
67 plt.ylim(-180, 180)
68 plt.xlabel("trajectory point index")
69 plt.ylabel("motor position (Degree)")
71 plt.subplot(1, 2, 2)
72 plt.title("hkl trajectory")
73 hkl_positions = numpy.array([h, k, l])
74 for y, name in zip(hkl_positions, pseudo_axes_names):
75 plt.plot(y, 'o-', label=name)
76 plt.legend(loc=2)
77 plt.ylim(-0.1, 1.1)
78 plt.xlabel("trajectory point index")
79 plt.ylabel("pseudo motor position")