From 6b3bf45e77a096a4ab1f68bd1a1bd3e8d3dd7638 Mon Sep 17 00:00:00 2001
From: Egor Sobolev <egor.sobolev@xfel.eu>
Date: Thu, 15 Jun 2023 19:35:15 +0200
Subject: [PATCH] Add crystallographic coordinate transformation

---
 src/geomtools/sfx/lattice.py | 41 ++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

diff --git a/src/geomtools/sfx/lattice.py b/src/geomtools/sfx/lattice.py
index fdd9d63..92c8c26 100644
--- a/src/geomtools/sfx/lattice.py
+++ b/src/geomtools/sfx/lattice.py
@@ -2,6 +2,47 @@
 import numpy as np
 
 
+
+# Electron charge in C
+ELECTRON_CHARGE = 1.6021773e-19
+# Planck's constant (Js)
+PLANCK = 6.62606896e-34
+# Speed of light in vacuo (m/s)
+C_VACUO = 299792458
+
+
+def ph_en_to_lambda(a):
+    """Transforms photon energy (eV) to wavelength (m)"""
+    return PLANCK * C_VACUO / (a * ELECTRON_CHARGE)
+
+
+def get_q_from_xyz(x, y, z, lmd):
+    """Calculates q vector by detector coordinates"""
+    # r = sqrt(x*x + y*y)
+    # S = λ sqrt(z*z + x*x + y*y)
+    # cos φ = cos(arctan(y / x)) = x / r
+    # sin φ = sin(arctan(y / x)) = z / r
+    # sin 2θ / λ = sin(arctan(r / z)) = r / S
+    # cos 2θ / λ = cos(arctan(r / z)) = z / S
+    # q = sin 2θ / λ
+    # u = sin 2θ * cos φ / λ = x / S
+    # v = cos 2θ * sin φ / λ = y / S
+    # w = (cos 2θ - 1) / λ = z / S - 1 / λ
+    S = lmd * np.sqrt(x * x + y * y + z * z)
+    u = x / S
+    v = y / S
+    w = z / S - 1.0 / lmd
+    return np.array([u, v, w])
+
+
+def get_min_bragg_dist(r, clen, lmd, cell):
+    """Calculates minimal inter-bragg distance"""
+    # sin 2θ = sin(arctan(r / clen))
+    # q = sin 2θ / lmd
+    invq = lmd * np.sqrt(clen * clen + r * r) / r
+    return min(invq / a for a in cell)
+
+
 def spacing(h, k, l, a, b, c, alpha, beta, gamma):
     alpha *= np.pi / 180.
     beta *= np.pi / 180.
-- 
GitLab