|
| 1 | +# SPDX-License-Identifier: BSD-3-Clause |
| 2 | +# Copyright (c) 2024 Scipp contributors (https://github.com/scipp) |
| 3 | +# flake8: noqa: F403, F405 |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import pytest |
| 7 | +import sciline |
| 8 | +import scipp as sc |
| 9 | + |
| 10 | +from ess.estia import EstiaWorkflow |
| 11 | +from ess.estia.data import estia_mcstas_reference_run, estia_mcstas_sample_run |
| 12 | +from ess.estia.load import load_mcstas_events |
| 13 | +from ess.reflectometry.types import ( |
| 14 | + BeamDivergenceLimits, |
| 15 | + Filename, |
| 16 | + ProtonCurrent, |
| 17 | + QBins, |
| 18 | + ReducibleData, |
| 19 | + ReferenceRun, |
| 20 | + ReflectivityOverQ, |
| 21 | + SampleRun, |
| 22 | + WavelengthBins, |
| 23 | + YIndexLimits, |
| 24 | + ZIndexLimits, |
| 25 | +) |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture |
| 29 | +def estia_mcstas_pipeline() -> sciline.Pipeline: |
| 30 | + wf = EstiaWorkflow() |
| 31 | + wf.insert(load_mcstas_events) |
| 32 | + wf[Filename[ReferenceRun]] = estia_mcstas_reference_run() |
| 33 | + |
| 34 | + wf[YIndexLimits] = sc.scalar(35), sc.scalar(64) |
| 35 | + wf[ZIndexLimits] = sc.scalar(0), sc.scalar(14 * 32) |
| 36 | + wf[BeamDivergenceLimits] = sc.scalar(-1.0, unit='deg'), sc.scalar(1.0, unit='deg') |
| 37 | + wf[WavelengthBins] = sc.geomspace('wavelength', 3.5, 12, 2001, unit='angstrom') |
| 38 | + wf[ProtonCurrent[SampleRun]] = sc.DataArray( |
| 39 | + sc.array(dims=('time',), values=[]), |
| 40 | + coords={'time': sc.array(dims=('time',), values=[], unit='s')}, |
| 41 | + ) |
| 42 | + wf[ProtonCurrent[ReferenceRun]] = sc.DataArray( |
| 43 | + sc.array(dims=('time',), values=[]), |
| 44 | + coords={'time': sc.array(dims=('time',), values=[], unit='s')}, |
| 45 | + ) |
| 46 | + |
| 47 | + return wf |
| 48 | + |
| 49 | + |
| 50 | +def test_mcstas_loading(estia_mcstas_pipeline: sciline.Pipeline): |
| 51 | + estia_mcstas_pipeline[Filename[SampleRun]] = estia_mcstas_sample_run(11) |
| 52 | + da = estia_mcstas_pipeline.compute(ReducibleData[SampleRun]) |
| 53 | + assert da.dims == ('blade', 'wire', 'stripe') |
| 54 | + assert da.shape == (14, 32, 64) |
| 55 | + assert 'position' in da.coords |
| 56 | + assert 'sample_rotation' in da.coords |
| 57 | + assert 'detector_rotation' in da.coords |
| 58 | + assert 'theta' in da.coords |
| 59 | + assert 'wavelength' in da.bins.coords |
| 60 | + assert 'Q' in da.bins.coords |
| 61 | + |
| 62 | + |
| 63 | +def test_can_compute_reflectivity_curve(estia_mcstas_pipeline: sciline.Pipeline): |
| 64 | + estia_mcstas_pipeline[Filename[SampleRun]] = estia_mcstas_sample_run(11) |
| 65 | + estia_mcstas_pipeline[QBins] = sc.geomspace('Q', 0.005, 0.1, 200, unit='1/angstrom') |
| 66 | + r = estia_mcstas_pipeline.compute(ReflectivityOverQ) |
| 67 | + assert "Q" in r.coords |
| 68 | + assert "Q_resolution" in r.coords |
| 69 | + r = r.hist() |
| 70 | + min_q = sc.where( |
| 71 | + r.data > sc.scalar(0), |
| 72 | + sc.midpoints(r.coords['Q']), |
| 73 | + sc.scalar(np.nan, unit='1/angstrom'), |
| 74 | + ).nanmin() |
| 75 | + max_q = sc.where( |
| 76 | + r.data > sc.scalar(0), |
| 77 | + sc.midpoints(r.coords['Q']), |
| 78 | + sc.scalar(np.nan, unit='1/angstrom'), |
| 79 | + ).nanmax() |
| 80 | + |
| 81 | + assert max_q > sc.scalar(0.075, unit='1/angstrom') |
| 82 | + assert min_q < sc.scalar(0.007, unit='1/angstrom') |
0 commit comments