Commit e3536ccd authored by Zhou, Wenduo's avatar Zhou, Wenduo
Browse files

Merge branch 'sans834_integrate_2d_case_into_workflow' into 'next'

Sans834 integrate 2d case into workflow

See merge request sns-hfir-scse/sans/sans-backend!970
parents 7d6a0ef6 09b2978e
Loading
Loading
Loading
Loading
Loading
+58 −1
Original line number Diff line number Diff line
# Main method in this module implement step 2 of
# wavelength dependent inelastic incoherent scattering correction
# https://code.ornl.gov/sns-hfir-scse/sans/sans-backend/-/issues/689
from drtsans.dataobjects import verify_same_q_bins, IQmod
from drtsans.dataobjects import verify_same_q_bins, IQmod, IQazimuthal
import numpy as np


@@ -216,6 +216,63 @@ def normalize_by_elastic_reference(i_of_q, ref_i_of_q):
    return normalized_i_of_q, k_vec, k_error_vec


def normalize_by_elastic_reference2D(i_of_q, ref_i_of_q):
    """Normalize I(Q2D) by elastic reference run

    Parameters
    ----------
    i_of_q: ~drtsans.dataobjects.IQmod
        Input I(Q, wavelength) to normalize
    ref_i_of_q: ~drtsans.dataobjects.IQmod
        Input I(Q, wavelength) as elastic reference run

    Returns
    -------
    tuple
        normalized Q(2D), K vector and delta K vector

    """
    # check i_of_q and ref_i_of_q shall have same binning
    if not verify_same_q_bins(i_of_q, ref_i_of_q):
        raise RuntimeError(
            "Input I(Q) and elastic reference I(Q) have different Q and wavelength binning"
        )

    # skip reshape step of the 1D case?

    k_vec, k_error2_vec, p_vec, s_vec = calculate_K_2d(i_of_q)

    # common grouping mask
    mask = determine_common_mod_q2d_range_mesh(i_of_q.qx, i_of_q.qy, i_of_q.wavelength, i_of_q.intensity)

    ref_wavelength_vec = determine_reference_wavelength_q2d(i_of_q, mask)

    normalized_intensity_array, normalized_error2_array = normalize_intensity_q2d(
        i_of_q.wavelength,
        i_of_q.qx,
        i_of_q.qy,
        i_of_q.intensity,
        i_of_q.error,
        ref_wavelength_vec,
        k_vec,
        p_vec,
        s_vec,
        mask
    )

    normalized_i_of_q = IQazimuthal(
        intensity=normalized_intensity_array,
        error=normalized_error2_array,
        qx=i_of_q.qx,
        qy=i_of_q.qy,
        wavelength=i_of_q.wavelength,
        delta_qx=i_of_q.delta_qx,
        delta_qy=i_of_q.delta_qy
    )

    return normalized_i_of_q, k_vec, k_error2_vec


def build_i_of_q1d(wl_vector, q_vector, intensity_array, error_array, delta_q_array):
    """From wavelength, Q, intensity, error and delta Q to build I(Q1D)

+35 −20
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ from drtsans.tof.eqsans.correction_api import (
)
from drtsans.tof.eqsans.elastic_reference_normalization import (
    normalize_by_elastic_reference,
    normalize_by_elastic_reference2D,
)
import os
from collections import namedtuple
@@ -287,7 +288,7 @@ def bin_i_with_correction(
            )

        # Bin elastic reference run
        if iq1d_elastic_ref_fr:
        if iq1d_elastic_ref_fr or iq2d_elastic_ref_fr:
            # bin the reference elastic runs of the current frame
            iq2d_elastic_wl, iq1d_elastic_wl = bin_all(
                iq2d_elastic_ref_fr[wl_frame],
@@ -309,6 +310,7 @@ def bin_i_with_correction(
                error_weighted=weighted_errors,
                n_wavelength_bin=None,
            )
            if (iq1d_elastic_ref_fr):
                if len(iq1d_elastic_wl) != 1:
                    raise NotImplementedError(
                        "Not expected that there are more than 1 IQmod of "
@@ -329,6 +331,19 @@ def bin_i_with_correction(
                    k_error_vec,
                    path=os.path.join(output_dir, f"k_{run_number}.dat"),
                )
            else:
                # 2D normalization
                iq2d_wl, k_vec, k_error_vec = normalize_by_elastic_reference2D(
                    iq2d_main_wl[0], iq2d_elastic_wl[0]
                )
                iq2d_main_wl[0] = iq2d_wl

                save_k_vector(
                    iq2d_wl.wavelength,
                    k_vec,
                    k_error_vec,
                    path=os.path.join(output_dir, f"k_{run_number}.dat"),
                )

        # 1D correction
        b_file_prefix = f"{raw_name}_frame_{wl_frame}"