|
| 1 | +# SPDX-License-Identifier: BSD-3-Clause |
| 2 | +# Copyright (c) 2023 Scipp contributors (https://github.com/scipp) |
| 3 | +import scipp as sc |
| 4 | +from scipp.constants import g |
| 5 | + |
| 6 | +from ..choppers import make_chopper |
| 7 | +from ..logging import log_call |
| 8 | + |
| 9 | + |
| 10 | +@log_call( |
| 11 | + instrument='amor', message='Constructing AMOR beamline from default parameters' |
| 12 | +) |
| 13 | +def make_beamline( |
| 14 | + sample_rotation: sc.Variable, |
| 15 | + beam_size: sc.Variable = None, |
| 16 | + sample_size: sc.Variable = None, |
| 17 | + detector_spatial_resolution: sc.Variable = None, |
| 18 | + gravity: sc.Variable = None, |
| 19 | + chopper_frequency: sc.Variable = None, |
| 20 | + chopper_phase: sc.Variable = None, |
| 21 | + chopper_1_position: sc.Variable = None, |
| 22 | + chopper_2_position: sc.Variable = None, |
| 23 | +) -> dict: |
| 24 | + """ |
| 25 | + Amor beamline components. |
| 26 | +
|
| 27 | + Parameters |
| 28 | + ---------- |
| 29 | + sample_rotation: |
| 30 | + Sample rotation (omega) angle. |
| 31 | + beam_size: |
| 32 | + Size of the beam perpendicular to the scattering surface. Default is `0.001 m`. |
| 33 | + sample_size: |
| 34 | + Size of the sample in direction of the beam. Default :code:`0.01 m`. |
| 35 | + detector_spatial_resolution: |
| 36 | + Spatial resolution of the detector. Default is `2.5 mm`. |
| 37 | + gravity: |
| 38 | + Vector representing the direction and magnitude of the Earth's gravitational |
| 39 | + field. Default is `[0, -g, 0]`. |
| 40 | + chopper_frequency: |
| 41 | + Rotational frequency of the chopper. Default is `6.6666... Hz`. |
| 42 | + chopper_phase: |
| 43 | + Phase offset between chopper pulse and ToF zero. Default is `-8. degrees of |
| 44 | + arc`. |
| 45 | + chopper_position: |
| 46 | + Position of the chopper. Default is `-15 m`. |
| 47 | +
|
| 48 | + Returns |
| 49 | + ------- |
| 50 | + : |
| 51 | + A dict. |
| 52 | + """ |
| 53 | + if beam_size is None: |
| 54 | + beam_size = 2.0 * sc.units.mm |
| 55 | + if sample_size is None: |
| 56 | + sample_size = 10.0 * sc.units.mm |
| 57 | + if detector_spatial_resolution is None: |
| 58 | + detector_spatial_resolution = 0.0025 * sc.units.m |
| 59 | + if gravity is None: |
| 60 | + gravity = sc.vector(value=[0, -1, 0]) * g |
| 61 | + if chopper_frequency is None: |
| 62 | + chopper_frequency = sc.scalar(20 / 3, unit='Hz') |
| 63 | + if chopper_phase is None: |
| 64 | + chopper_phase = sc.scalar(-8.0, unit='deg') |
| 65 | + if chopper_1_position is None: |
| 66 | + chopper_1_position = sc.vector(value=[0, 0, -15.5], unit='m') |
| 67 | + if chopper_2_position is None: |
| 68 | + chopper_2_position = sc.vector(value=[0, 0, -14.5], unit='m') |
| 69 | + beamline = { |
| 70 | + 'sample_rotation': sample_rotation, |
| 71 | + 'beam_size': beam_size, |
| 72 | + 'sample_size': sample_size, |
| 73 | + 'detector_spatial_resolution': detector_spatial_resolution, |
| 74 | + 'gravity': gravity, |
| 75 | + } |
| 76 | + # TODO: in scn.load_nexus, the chopper parameters are stored as coordinates |
| 77 | + # of a DataArray, and the data value is a string containing the name of the |
| 78 | + # chopper. This does not allow storing e.g. chopper cutout angles. |
| 79 | + # We should change this to be a Dataset, which is what we do here. |
| 80 | + beamline["source_chopper_2"] = sc.scalar( |
| 81 | + make_chopper( |
| 82 | + frequency=chopper_frequency, |
| 83 | + phase=chopper_phase, |
| 84 | + position=chopper_2_position, |
| 85 | + ) |
| 86 | + ) |
| 87 | + beamline["source_chopper_1"] = sc.scalar( |
| 88 | + make_chopper( |
| 89 | + frequency=chopper_frequency, |
| 90 | + phase=chopper_phase, |
| 91 | + position=chopper_1_position, |
| 92 | + ) |
| 93 | + ) |
| 94 | + return beamline |
| 95 | + |
| 96 | + |
| 97 | +@log_call(instrument='amor', level='DEBUG') |
| 98 | +def instrument_view_components(da: sc.DataArray) -> dict: |
| 99 | + """ |
| 100 | + Create a dict of instrument view components, containing: |
| 101 | + - the sample |
| 102 | + - the source chopper |
| 103 | +
|
| 104 | + Parameters |
| 105 | + ---------- |
| 106 | + da: |
| 107 | + The DataArray containing the sample and source chopper coordinates. |
| 108 | +
|
| 109 | + Returns |
| 110 | + ------- |
| 111 | + : |
| 112 | + Dict of instrument view definitions. |
| 113 | + """ |
| 114 | + return { |
| 115 | + "sample": { |
| 116 | + 'center': da.meta['sample_position'], |
| 117 | + 'color': 'red', |
| 118 | + 'size': sc.vector(value=[0.2, 0.01, 0.2], unit=sc.units.m), |
| 119 | + 'type': 'box', |
| 120 | + }, |
| 121 | + "source_chopper_2": { |
| 122 | + 'center': da.meta['source_chopper_2'].value["position"].data, |
| 123 | + 'color': 'blue', |
| 124 | + 'size': sc.vector(value=[0.5, 0, 0], unit=sc.units.m), |
| 125 | + 'type': 'disk', |
| 126 | + }, |
| 127 | + "source_chopper_1": { |
| 128 | + 'center': da.meta['source_chopper_1'].value["position"].data, |
| 129 | + 'color': 'blue', |
| 130 | + 'size': sc.vector(value=[0.5, 0, 0], unit=sc.units.m), |
| 131 | + 'type': 'disk', |
| 132 | + }, |
| 133 | + } |
0 commit comments