1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
|
# -*- coding: utf-8 -*-
#cython: embedsignature=True, language_level=3
#cython: boundscheck=False, wraparound=False, cdivision=True, initializedcheck=False,
## This is for developping
## cython: profile=True, warn.undeclared=True, warn.unused=True, warn.unused_result=False, warn.unused_arg=True
#
# Project: Fast Azimuthal integration
# https://github.com/silx-kit/pyFAI
#
# Copyright (C) 2012-2018 European Synchrotron Radiation Facility, Grenoble, France
#
# Principal author: Jérôme Kieffer (Jerome.Kieffer@ESRF.eu)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""
This extension is a fast-implementation for calculating the geometry, i.e. where
every pixel of an array stays in space (x,y,z) or its (r, :math:`\\chi`)
coordinates.
"""
__author__ = "Jerome Kieffer"
__license__ = "MIT"
__date__ = "11/01/2021"
__copyright__ = "2011-2020, ESRF"
__contact__ = "jerome.kieffer@esrf.fr"
cimport cython
import numpy
from cython.parallel cimport prange
from libc.math cimport sin, cos, atan2, sqrt, M_PI
cdef double twopi = 2.0 * M_PI
# We declare a second cython.floating so that it behaves like an actual template
ctypedef fused float_or_double:
cython.double
cython.float
cdef inline double f_t1(double p1, double p2, double p3, double sinRot1, double cosRot1, double sinRot2, double cosRot2, double sinRot3, double cosRot3) nogil:
"""Calculate t2 (aka y) for 1 pixel
:param p1:distances in meter along dim1 from PONI
:param p2: distances in meter along dim2 from PONI
:param p3: distance sample - PONI
:param sinRot1,sinRot2,sinRot3: sine of the angles
:param cosRot1,cosRot2,cosRot3: cosine of the angles
"""
return (p1 * cosRot2 * cosRot3 +
p2 * (cosRot3 * sinRot1 * sinRot2 - cosRot1 * sinRot3) -
p3 * (cosRot1 * cosRot3 * sinRot2 + sinRot1 * sinRot3))
cdef inline double f_t2(double p1, double p2, double p3, double sinRot1, double cosRot1, double sinRot2, double cosRot2, double sinRot3, double cosRot3) nogil:
"""Calculate t2 (aka y) for 1 pixel
:param p1:distances in meter along dim1 from PONI
:param p2: distances in meter along dim2 from PONI
:param p3: distance sample - PONI
:param sinRot1,sinRot2,sinRot3: sine of the angles
:param cosRot1,cosRot2,cosRot3: cosine of the angles
"""
return (p1 * cosRot2 * sinRot3 +
p2 * (cosRot1 * cosRot3 + sinRot1 * sinRot2 * sinRot3) -
p3 * (-(cosRot3 * sinRot1) + cosRot1 * sinRot2 * sinRot3))
cdef inline double f_t3(double p1, double p2, double p3, double sinRot1, double cosRot1, double sinRot2, double cosRot2, double sinRot3, double cosRot3) nogil:
"""Calculate t3 (aka -z) for 1 pixel
:param p1:distances in meter along dim1 from PONI
:param p2: distances in meter along dim2 from PONI
:param p3: distance sample - PONI
:param sinRot1,sinRot2,sinRot3: sine of the angles
:param cosRot1,cosRot2,cosRot3: cosine of the angles
"""
return p1 * sinRot2 - p2 * cosRot2 * sinRot1 + p3 * cosRot1 * cosRot2
cdef inline double f_tth(double p1, double p2, double L, double sinRot1, double cosRot1, double sinRot2, double cosRot2, double sinRot3, double cosRot3) nogil:
"""Calculate 2 theta for 1 pixel
:param p1:distances in meter along dim1 from PONI
:param p2: distances in meter along dim2 from PONI
:param L: distance sample - PONI
:param sinRot1,sinRot2,sinRot3: sine of the angles
:param cosRot1,cosRot2,cosRot3: cosine of the angles
:return: 2 theta
"""
cdef:
double t1 = f_t1(p1, p2, L, sinRot1, cosRot1, sinRot2, cosRot2, sinRot3, cosRot3)
double t2 = f_t2(p1, p2, L, sinRot1, cosRot1, sinRot2, cosRot2, sinRot3, cosRot3)
double t3 = f_t3(p1, p2, L, sinRot1, cosRot1, sinRot2, cosRot2, sinRot3, cosRot3)
return atan2(sqrt(t1 * t1 + t2 * t2), t3)
cdef inline double f_q(double p1, double p2, double L, double sinRot1, double cosRot1, double sinRot2, double cosRot2, double sinRot3, double cosRot3, double wavelength) nogil:
"""
Calculate the scattering vector q for 1 pixel
:param p1:distances in meter along dim1 from PONI
:param p2: distances in meter along dim2 from PONI
:param L: distance sample - PONI
:param sinRot1,sinRot2,sinRot3: sine of the angles
:param cosRot1,cosRot2,cosRot3: cosine of the angles
"""
return 4.0e-9 * M_PI / wavelength * sin(f_tth(p1, p2, L, sinRot1, cosRot1, sinRot2, cosRot2, sinRot3, cosRot3) / 2.0)
cdef inline double f_chi(double p1, double p2, double L, double sinRot1, double cosRot1, double sinRot2, double cosRot2, double sinRot3, double cosRot3) nogil:
"""
calculate chi for 1 pixel
:param p1:distances in meter along dim1 from PONI
:param p2: distances in meter along dim2 from PONI
:param L: distance sample - PONI
:param sinRot1,sinRot2,sinRot3: sine of the angles
:param cosRot1,cosRot2,cosRot3: cosine of the angles
"""
cdef:
double t1 = f_t1(p1, p2, L, sinRot1, cosRot1, sinRot2, cosRot2, sinRot3, cosRot3)
double t2 = f_t2(p1, p2, L, sinRot1, cosRot1, sinRot2, cosRot2, sinRot3, cosRot3)
return atan2(t1, t2)
cdef inline double f_r(double p1, double p2, double L, double sinRot1, double cosRot1, double sinRot2, double cosRot2, double sinRot3, double cosRot3) nogil:
"""
calculate r for 1 pixel, radius from beam center to current
:param p1:distances in meter along dim1 from PONI
:param p2: distances in meter along dim2 from PONI
:param L: distance sample - PONI
:param sinRot1,sinRot2,sinRot3: sine of the angles
:param cosRot1,cosRot2,cosRot3: cosine of the angles
"""
cdef:
double t1 = f_t1(p1, p2, L, sinRot1, cosRot1, sinRot2, cosRot2, sinRot3, cosRot3)
double t2 = f_t2(p1, p2, L, sinRot1, cosRot1, sinRot2, cosRot2, sinRot3, cosRot3)
# double t3 = f_t3(p1, p2, L, sinRot1, cosRot1, sinRot2, cosRot2, sinRot3, cosRot3)
return sqrt(t1 * t1 + t2 * t2)
# Changed 10/03/2016 ... the radius is in the pixel position.
# return L * sqrt(t1 * t1 + t2 * t2) / (t3 * cosRot1 * cosRot2)
cdef inline double f_cosa(double p1, double p2, double L) nogil:
"""
calculate cosine of the incidence angle for 1 pixel
:param p1:distances in meter along dim1 from PONI
:param p2: distances in meter along dim2 from PONI
:param L: distance sample - PONI
"""
return L / sqrt((L * L) + (p1 * p1) + (p2 * p2))
################################################################################
# End of pure cython function declaration
################################################################################
def calc_pos_zyx(double L, double poni1, double poni2,
double rot1, double rot2, double rot3,
pos1 not None,
pos2 not None,
pos3=None):
"""Calculate the 3D coordinates in the sample's referential
:param L: distance sample - PONI
:param poni1: PONI coordinate along y axis
:param poni2: PONI coordinate along x axis
:param rot1: angle1
:param rot2: angle2
:param rot3: angle3
:param pos1: numpy array with distances in meter along dim1 from PONI (Y)
:param pos2: numpy array with distances in meter along dim2 from PONI (X)
:param pos3: numpy array with distances in meter along Sample->PONI (Z), positive behind the detector
:return: 3-tuple of ndarray of double with same shape and size as pos1
"""
cdef:
double sinRot1 = sin(rot1)
double cosRot1 = cos(rot1)
double sinRot2 = sin(rot2)
double cosRot2 = cos(rot2)
double sinRot3 = sin(rot3)
double cosRot3 = cos(rot3)
ssize_t size = pos1.size, i = 0
double p1, p2, p3
assert pos2.size == size, "pos2.size == size"
cdef:
double[::1] c1 = numpy.ascontiguousarray(pos1.ravel(), dtype=numpy.float64)
double[::1] c2 = numpy.ascontiguousarray(pos2.ravel(), dtype=numpy.float64)
double[::1] c3
double[::1] t1 = numpy.empty(size, dtype=numpy.float64)
double[::1] t2 = numpy.empty(size, dtype=numpy.float64)
double[::1] t3 = numpy.empty(size, dtype=numpy.float64)
if pos3 is None:
for i in prange(size, nogil=True, schedule="static"):
p1 = c1[i] - poni1
p2 = c2[i] - poni2
t1[i] = f_t1(p1, p2, L, sinRot1, cosRot1, sinRot2, cosRot2, sinRot3, cosRot3)
t2[i] = f_t2(p1, p2, L, sinRot1, cosRot1, sinRot2, cosRot2, sinRot3, cosRot3)
t3[i] = f_t3(p1, p2, L, sinRot1, cosRot1, sinRot2, cosRot2, sinRot3, cosRot3)
else:
assert pos3.size == size, "pos3.size == size"
c3 = numpy.ascontiguousarray(pos3.ravel(), dtype=numpy.float64)
for i in prange(size, nogil=True, schedule="static"):
p1 = c1[i] - poni1
p2 = c2[i] - poni2
p3 = c3[i] + L
t1[i] = f_t1(p1, p2, p3, sinRot1, cosRot1, sinRot2, cosRot2, sinRot3, cosRot3)
t2[i] = f_t2(p1, p2, p3, sinRot1, cosRot1, sinRot2, cosRot2, sinRot3, cosRot3)
t3[i] = f_t3(p1, p2, p3, sinRot1, cosRot1, sinRot2, cosRot2, sinRot3, cosRot3)
r1 = numpy.asarray(t1)
r2 = numpy.asarray(t2)
r3 = numpy.asarray(t3)
if pos1.ndim == 3:
return (r3.reshape(pos1.shape[0], pos1.shape[1], pos1.shape[2]),
r1.reshape(pos1.shape[0], pos1.shape[1], pos1.shape[2]),
r2.reshape(pos1.shape[0], pos1.shape[1], pos1.shape[2]))
if pos1.ndim == 2:
return (r3.reshape(pos1.shape[0], pos1.shape[1]),
r1.reshape(pos1.shape[0], pos1.shape[1]),
r2.reshape(pos1.shape[0], pos1.shape[1]))
else:
return r3, r1, r2
def calc_tth(double L, double rot1, double rot2, double rot3,
pos1 not None,
pos2 not None,
pos3=None):
"""
Calculate the 2theta array (radial angle) in parallel
:param L: distance sample - PONI
:param rot1: angle1
:param rot2: angle2
:param rot3: angle3
:param pos1: numpy array with distances in meter along dim1 from PONI (Y)
:param pos2: numpy array with distances in meter along dim2 from PONI (X)
:param pos3: numpy array with distances in meter along Sample->PONI (Z), positive behind the detector
:return: ndarray of double with same shape and size as pos1
"""
cdef:
double sinRot1 = sin(rot1)
double cosRot1 = cos(rot1)
double sinRot2 = sin(rot2)
double cosRot2 = cos(rot2)
double sinRot3 = sin(rot3)
double cosRot3 = cos(rot3)
ssize_t size = pos1.size, i = 0
assert pos2.size == size, "pos2.size == size"
cdef:
double[::1] c1 = numpy.ascontiguousarray(pos1.ravel(), dtype=numpy.float64)
double[::1] c2 = numpy.ascontiguousarray(pos2.ravel(), dtype=numpy.float64)
double[::1] c3
double[::1] out = numpy.empty(size, dtype=numpy.float64)
if pos3 is None:
for i in prange(size, nogil=True, schedule="static"):
out[i] = f_tth(c1[i], c2[i], L, sinRot1, cosRot1, sinRot2, cosRot2, sinRot3, cosRot3)
else:
assert pos3.size == size, "pos3.size == size"
c3 = numpy.ascontiguousarray(pos3.ravel(), dtype=numpy.float64)
for i in prange(size, nogil=True, schedule="static"):
out[i] = f_tth(c1[i], c2[i], L + c3[i], sinRot1, cosRot1, sinRot2, cosRot2, sinRot3, cosRot3)
if pos1.ndim == 2:
return numpy.asarray(out).reshape(pos1.shape[0], pos1.shape[1])
else:
return numpy.asarray(out)
def calc_chi(double L, double rot1, double rot2, double rot3,
pos1 not None,
pos2 not None,
pos3=None,
bint chi_discontinuity_at_pi=True):
"""Calculate the chi array (azimuthal angles) using OpenMP
X1 = p1*cos(rot2)*cos(rot3) + p2*(cos(rot3)*sin(rot1)*sin(rot2) - cos(rot1)*sin(rot3)) - L*(cos(rot1)*cos(rot3)*sin(rot2) + sin(rot1)*sin(rot3))
X2 = p1*cos(rot2)*sin(rot3) - L*(-(cos(rot3)*sin(rot1)) + cos(rot1)*sin(rot2)*sin(rot3)) + p2*(cos(rot1)*cos(rot3) + sin(rot1)*sin(rot2)*sin(rot3))
X3 = -(L*cos(rot1)*cos(rot2)) + p2*cos(rot2)*sin(rot1) - p1*sin(rot2)
tan(Chi) = X2 / X1
:param L: distance sample - PONI
:param rot1: angle1
:param rot2: angle2
:param rot3: angle3
:param pos1: numpy array with distances in meter along dim1 from PONI (Y)
:param pos2: numpy array with distances in meter along dim2 from PONI (X)
:param pos3: numpy array with distances in meter along Sample->PONI (Z), positive behind the detector
:param chi_discontinuity_at_pi: set to False to obtain chi in the range [0, 2pi[ instead of [-pi, pi[
:return: ndarray of double with same shape and size as pos1
"""
cdef:
double sinRot1 = sin(rot1)
double cosRot1 = cos(rot1)
double sinRot2 = sin(rot2)
double cosRot2 = cos(rot2)
double sinRot3 = sin(rot3)
double cosRot3 = cos(rot3)
double chi
cdef ssize_t size = pos1.size, i = 0
assert pos2.size == size, "pos2.size == size"
cdef:
double[::1] c1 = numpy.ascontiguousarray(pos1.ravel(), dtype=numpy.float64)
double[::1] c2 = numpy.ascontiguousarray(pos2.ravel(), dtype=numpy.float64)
double[::1] c3
double[::1] out = numpy.empty(size, dtype=numpy.float64)
if pos3 is None:
for i in prange(size, nogil=True, schedule="static"):
out[i] = f_chi(c1[i], c2[i], L, sinRot1, cosRot1, sinRot2, cosRot2, sinRot3, cosRot3)
else:
assert pos3.size == size, "pos3.size == size"
c3 = numpy.ascontiguousarray(pos3.ravel(), dtype=numpy.float64)
for i in prange(size, nogil=True, schedule="static"):
chi = f_chi(c1[i], c2[i], L + c3[i], sinRot1, cosRot1, sinRot2, cosRot2, sinRot3, cosRot3)
if chi_discontinuity_at_pi:
out[i] = chi
else:
out[i] = (chi + twopi) % twopi
if pos1.ndim == 2:
return numpy.asarray(out).reshape(pos1.shape[0], pos1.shape[1])
else:
return numpy.asarray(out)
def calc_q(double L, double rot1, double rot2, double rot3,
pos1 not None,
pos2 not None,
double wavelength, pos3=None):
"""
Calculate the q (scattering vector) array using OpenMP
X1 = p1*cos(rot2)*cos(rot3) + p2*(cos(rot3)*sin(rot1)*sin(rot2) - cos(rot1)*sin(rot3)) - L*(cos(rot1)*cos(rot3)*sin(rot2) + sin(rot1)*sin(rot3))
X2 = p1*cos(rot2)*sin(rot3) - L*(-(cos(rot3)*sin(rot1)) + cos(rot1)*sin(rot2)*sin(rot3)) + p2*(cos(rot1)*cos(rot3) + sin(rot1)*sin(rot2)*sin(rot3))
X3 = -(L*cos(rot1)*cos(rot2)) + p2*cos(rot2)*sin(rot1) - p1*sin(rot2)
tan(Chi) = X2 / X1
:param L: distance sample - PONI
:param rot1: angle1
:param rot2: angle2
:param rot3: angle3
:param pos1: numpy array with distances in meter along dim1 from PONI (Y)
:param pos2: numpy array with distances in meter along dim2 from PONI (X)
:param pos3: numpy array with distances in meter along Sample->PONI (Z), positive behind the detector
:param wavelength: in meter to get q in nm-1
:return: ndarray of double with same shape and size as pos1
"""
cdef:
double sinRot1 = sin(rot1)
double cosRot1 = cos(rot1)
double sinRot2 = sin(rot2)
double cosRot2 = cos(rot2)
double sinRot3 = sin(rot3)
double cosRot3 = cos(rot3)
ssize_t size = pos1.size, i = 0
assert pos2.size == size, "pos2.size == size"
cdef:
double[::1] c1 = numpy.ascontiguousarray(pos1.ravel(), dtype=numpy.float64)
double[::1] c2 = numpy.ascontiguousarray(pos2.ravel(), dtype=numpy.float64)
double[::1] c3
double[::1] out = numpy.empty(size, dtype=numpy.float64)
if pos3 is None:
for i in prange(size, nogil=True, schedule="static"):
out[i] = f_q(c1[i], c2[i], L, sinRot1, cosRot1, sinRot2, cosRot2, sinRot3, cosRot3, wavelength)
else:
assert pos3.size == size, "pos3.size == size"
c3 = numpy.ascontiguousarray(pos3.ravel(), dtype=numpy.float64)
for i in prange(size, nogil=True, schedule="static"):
out[i] = f_q(c1[i], c2[i], L + c3[i], sinRot1, cosRot1, sinRot2, cosRot2, sinRot3, cosRot3, wavelength)
if pos1.ndim == 2:
return numpy.asarray(out).reshape(pos1.shape[0], pos1.shape[1])
else:
return numpy.asarray(out)
def calc_r(double L, double rot1, double rot2, double rot3,
pos1 not None, pos2 not None,
pos3=None):
"""
Calculate the radius array (radial direction) in parallel
:param L: distance sample - PONI
:param rot1: angle1
:param rot2: angle2
:param rot3: angle3
:param pos1: numpy array with distances in meter along dim1 from PONI (Y)
:param pos2: numpy array with distances in meter along dim2 from PONI (X)
:param pos3: numpy array with distances in meter along Sample->PONI (Z), positive behind the detector
:return: ndarray of double with same shape and size as pos1
"""
cdef:
double sinRot1 = sin(rot1)
double cosRot1 = cos(rot1)
double sinRot2 = sin(rot2)
double cosRot2 = cos(rot2)
double sinRot3 = sin(rot3)
double cosRot3 = cos(rot3)
ssize_t size = pos1.size, i = 0
assert pos2.size == size, "pos2.size == size"
cdef:
double[::1] c1 = numpy.ascontiguousarray(pos1.ravel(), dtype=numpy.float64)
double[::1] c2 = numpy.ascontiguousarray(pos2.ravel(), dtype=numpy.float64)
double[::1] c3
double[::1] out = numpy.empty(size, dtype=numpy.float64)
if pos3 is None:
for i in prange(size, nogil=True, schedule="static"):
out[i] = f_r(c1[i], c2[i], L, sinRot1, cosRot1, sinRot2, cosRot2, sinRot3, cosRot3)
else:
assert pos3.size == size, "pos3.size == size"
c3 = numpy.ascontiguousarray(pos3.ravel(), dtype=numpy.float64)
for i in prange(size, nogil=True, schedule="static"):
out[i] = f_r(c1[i], c2[i], L + c3[i], sinRot1, cosRot1, sinRot2, cosRot2, sinRot3, cosRot3)
if pos1.ndim == 2:
return numpy.asarray(out).reshape(pos1.shape[0], pos1.shape[1])
else:
return numpy.asarray(out)
def calc_cosa(double L,
pos1 not None,
pos2 not None,
pos3=None):
"""Calculate the cosine of the incidence angle using OpenMP.
Used for sensors thickness effect corrections
:param L: distance sample - PONI
:param pos1: numpy array with distances in meter along dim1 from PONI (Y)
:param pos2: numpy array with distances in meter along dim2 from PONI (X)
:param pos3: numpy array with distances in meter along Sample->PONI (Z), positive behind the detector
:return: ndarray of double with same shape and size as pos1
"""
cdef ssize_t size = pos1.size, i = 0
assert pos2.size == size, "pos2.size == size"
cdef:
double[::1] c1 = numpy.ascontiguousarray(pos1.ravel(), dtype=numpy.float64)
double[::1] c2 = numpy.ascontiguousarray(pos2.ravel(), dtype=numpy.float64)
double[::1] c3
double[::1] out = numpy.empty(size, dtype=numpy.float64)
if pos3 is None:
for i in prange(size, nogil=True, schedule="static"):
out[i] = f_cosa(c1[i], c2[i], L)
else:
assert pos3.size == size, "pos3.size == size"
c3 = numpy.ascontiguousarray(pos3.ravel(), dtype=numpy.float64)
for i in prange(size, nogil=True, schedule="static"):
out[i] = f_cosa(c1[i], c2[i], L + c3[i])
if pos1.ndim == 2:
return numpy.asarray(out).reshape(pos1.shape[0], pos1.shape[1])
else:
return numpy.asarray(out)
def calc_rad_azim(double L,
double poni1,
double poni2,
double rot1,
double rot2,
double rot3,
pos1 not None,
pos2 not None,
pos3=None,
space="2th",
wavelength=None,
bint chi_discontinuity_at_pi=True
):
"""Calculate the radial & azimutal position for each pixel from pos1, pos2, pos3.
:param L: distance sample - PONI
:param poni1: PONI coordinate along y axis
:param poni2: PONI coordinate along x axis
:param rot1: angle1
:param rot2: angle2
:param rot3: angle3
:param pos1: numpy array with distances in meter along dim1 from PONI (Y)
:param pos2: numpy array with distances in meter along dim2 from PONI (X)
:param pos3: numpy array with distances in meter along Sample->PONI (Z), positive behind the detector
:param space: can be "2th", "q" or "r" for radial units. Azimuthal units are radians
:param chi_discontinuity_at_pi: set to False to obtain chi in the range [0, 2pi[ instead of [-pi, pi[
:return: ndarray of double with same shape and size as pos1 + (2,),
:raise: KeyError when space is bad !
ValueError when wavelength is missing
"""
cdef ssize_t size = pos1.size, i = 0
assert pos2.size == size, "pos2.size == size"
cdef:
double sinRot1 = sin(rot1)
double cosRot1 = cos(rot1)
double sinRot2 = sin(rot2)
double cosRot2 = cos(rot2)
double sinRot3 = sin(rot3)
double cosRot3 = cos(rot3)
int cspace = 0
double[::1] c1 = numpy.ascontiguousarray(pos1.ravel(), dtype=numpy.float64)
double[::1] c2 = numpy.ascontiguousarray(pos2.ravel(), dtype=numpy.float64)
double[::1] c3
float[:, ::1] out = numpy.empty((size, 2), dtype=numpy.float32)
double t1, t2, t3, fwavelength, chi
if space == "2th":
cspace = 1
elif space == "q":
cspace = 2
if not wavelength:
raise ValueError("wavelength is needed for q calculation")
else:
fwavelength = float(wavelength)
elif space == "r":
cspace = 3
else:
raise KeyError("Not implemented space %s in cython" % space)
if pos3 is None:
for i in prange(size, nogil=True, schedule="static"):
t1 = f_t1(c1[i] - poni1, c2[i] - poni2, L, sinRot1, cosRot1, sinRot2, cosRot2, sinRot3, cosRot3)
t2 = f_t2(c1[i] - poni1, c2[i] - poni2, L, sinRot1, cosRot1, sinRot2, cosRot2, sinRot3, cosRot3)
t3 = f_t3(c1[i] - poni1, c2[i] - poni2, L, sinRot1, cosRot1, sinRot2, cosRot2, sinRot3, cosRot3)
if cspace == 1:
out[i, 0] = atan2(sqrt(t1 * t1 + t2 * t2), t3)
elif cspace == 2:
out[i, 0] = 4.0e-9 * M_PI / fwavelength * sin(atan2(sqrt(t1 * t1 + t2 * t2), t3) / 2.0)
elif cspace == 3:
out[i, 0] = sqrt(t1 * t1 + t2 * t2)
chi = atan2(t1, t2)
if chi_discontinuity_at_pi:
out[i, 1] = chi
else:
out[i, 1] = (chi + twopi) % twopi
else:
assert pos3.size == size, "pos3.size == size"
c3 = numpy.ascontiguousarray(pos3.ravel(), dtype=numpy.float64)
for i in prange(size, nogil=True, schedule="static"):
t1 = f_t1(c1[i] - poni1, c2[i] - poni2, L + c3[i], sinRot1, cosRot1, sinRot2, cosRot2, sinRot3, cosRot3)
t2 = f_t2(c1[i] - poni1, c2[i] - poni2, L + c3[i], sinRot1, cosRot1, sinRot2, cosRot2, sinRot3, cosRot3)
t3 = f_t3(c1[i] - poni1, c2[i] - poni2, L + c3[i], sinRot1, cosRot1, sinRot2, cosRot2, sinRot3, cosRot3)
if cspace == 1:
out[i, 0] = atan2(sqrt(t1 * t1 + t2 * t2), t3)
elif cspace == 2:
out[i, 0] = 4.0e-9 * M_PI / fwavelength * sin(atan2(sqrt(t1 * t1 + t2 * t2), t3) / 2.0)
elif cspace == 3:
out[i, 0] = sqrt(t1 * t1 + t2 * t2)
chi = atan2(t1, t2)
if chi_discontinuity_at_pi:
out[i, 1] = chi
else:
out[i, 1] = (chi + twopi) % twopi
nout = numpy.asarray(out)
if pos1.ndim == 3:
return nout.reshape(pos1.shape[0], pos1.shape[1], pos1.shape[2], 2)
if pos1.ndim == 2:
return nout.reshape(pos1.shape[0], pos1.shape[1], 2)
else:
return nout
def calc_delta_chi(cython.floating[:, ::1] centers,
float_or_double[:, :, :, ::1] corners):
"""Calculate the delta chi array (azimuthal angles) using OpenMP
:param centers: numpy array with chi angles of the center of the pixels
:param corners: numpy array with chi angles of the corners of the pixels
:return: ndarray of double with same shape and size as centers woth the delta chi per pixel
"""
cdef:
int width, height, row, col, corn, nbcorn
double co, ce, delta0, delta1, delta2, delta
double[:, ::1] res
height = centers.shape[0]
width = centers.shape[1]
assert corners.shape[0] == height, "height match"
assert corners.shape[1] == width, "width match"
nbcorn = corners.shape[2]
res = numpy.empty((height, width), dtype=numpy.float64)
with nogil:
for row in prange(height):
for col in range(width):
ce = centers[row, col]
delta = 0.0
for corn in range(nbcorn):
co = corners[row, col, corn, 1]
delta1 = (co - ce + twopi) % twopi
delta2 = (ce - co + twopi) % twopi
delta0 = min(delta1, delta2)
if delta0 > delta:
delta = delta0
res[row, col] = delta
return numpy.asarray(res)
|