Commit cc614c1c authored by Zhang, Yuanpeng's avatar Zhang, Yuanpeng
Browse files

make live reduction consistent with autoreduction in terms of config

parent 30841145
Loading
Loading
Loading
Loading

live_config.json

0 → 100644
+16 −0
Original line number Diff line number Diff line
{
    "Cycle": "2023-1_11A_CAL",
    "SampleEnvironment": "PAC",
    "ConfigFile": {
        "PAC": "PAC_default.json",
        "HTJANIS": "HTJANIS_default.json",
        "LTJANIS": "LTJANIS_default.json",
        "OC": "OC_default.json",
        "MICAS": "MICAS_default.json",
        "AGES": "AGES_default.json",
        "JANIS_GAS_noscale":"JANIS_GAS_noscale.json",
        "GASJANIS":"GASJANIS_Default.json",
        "JANIS_GAS_intensifier_no_scale":"LTJANIS_intensifier.json",
        "LTJANIS_GAS_Dec":"LTJANIS_GAS_Dec_2022.json"
    }
}
+21 −0
Original line number Diff line number Diff line
{
    "CalibDirectory": "/SNS/PG3/shared/CALIBRATION/2022-2_11A_CAL/",
    "InputFiles": {
        "CalibFile": "PG3_PAC_d54007_2022_12_01.h5",
        "CharBackgrounds": "PG3_char_2022_12_03-HighRes_PAC_1.4 MW.txt",
        "CharInPlane": "PG3_char_2022_12_03_PAC_limit_1.4MW.txt"
    },
    "Params": {
        "Proc": {
            "TOFBinWidthInit": 100.0,
            "TOFBinWidthProc": -0.0008,
            "RemovePromptPulseWidth": 0
        },
        "PostProc": {
            "TOFBinWidthPProc": -0.0008,
            "RemovePromptPulseWidth": 0,
            "StripVPeaksPosTol": 0.05,
            "StripVPeaksBkgType": "Quadratic"
        }
    }
}
+21 −0
Original line number Diff line number Diff line
{
    "CalibDirectory": "/SNS/PG3/shared/CALIBRATION/2022-2_11A_CAL/",
    "InputFiles": {
        "CalibFile": "PG3_PAC_d54007_2022_12_01.h5",
        "CharBackgrounds": "PG3_char_2022_12_03-HighRes_PAC_1.4 MW.txt",
        "CharInPlane": "PG3_char_2022_12_03_PAC_limit_1.4MW.txt"
    },
    "Params": {
        "Proc": {
            "TOFBinWidthInit": 100.0,
            "TOFBinWidthProc": -0.0008,
            "RemovePromptPulseWidth": 0
        },
        "PostProc": {
            "TOFBinWidthPProc": -0.0008,
            "RemovePromptPulseWidth": 0,
            "StripVPeaksPosTol": 0.05,
            "StripVPeaksBkgType": "Quadratic"
        }
    }
}
+21 −0
Original line number Diff line number Diff line
{
    "CalibDirectory": "/SNS/PG3/shared/CALIBRATION/2022-2_11A_CAL/",
    "InputFiles": {
        "CalibFile": "PG3_PAC_d54007_2022_12_01.h5",
        "CharBackgrounds": "PG3_char_2022_12_03-HighRes_PAC_1.4 MW.txt",
        "CharInPlane": "PG3_char_2022_12_03_PAC_limit_1.4MW.txt"
    },
    "Params": {
        "Proc": {
            "TOFBinWidthInit": 100.0,
            "TOFBinWidthProc": -0.0008,
            "RemovePromptPulseWidth": 0
        },
        "PostProc": {
            "TOFBinWidthPProc": -0.0008,
            "RemovePromptPulseWidth": 0,
            "StripVPeaksPosTol": 0.05,
            "StripVPeaksBkgType": "Quadratic"
        }
    }
}
+158 −0
Original line number Diff line number Diff line
import mantid
from mantid import simpleapi
import json
import os

# -----> Config: Conda environment to use for the reduction
CONDA_ENV = 'mantid-qa'
central_config = "/SNS/PG3/shared/livereduce/live_config.json"
# <----- Config: Conda environment to use for the reduction

# -----> Config: Read in the config file
with open(central_config, "r") as f:
    input_c_config = json.load(f)

cycle = input_c_config["Cycle"]
sam_env = input_c_config["SampleEnvironment"]
config_file = input_c_config["ConfigFile"][sam_env]
config_file_f = os.path.join("/SNS/PG3/shared/livereduce/live_config_files",
                             cycle,
                             config_file)

with open(config_file_f, "r") as f:
    input_config = json.load(config_file_f)
# <----- Config: Read in the config file

# -----> Config: Live post processing parameters
tof_bin_width_pproc = input_config["Params"]["PostProc"]["TOFBinWidthPProc"]
rppw_pproc = input_config["Params"]["PostProc"]["RemovePromptPulseWidth"]
strip_v_pp_tol = input_config["Params"]["PostProc"]["StripVPeaksPosTol"]
strip_v_bt = input_config["Params"]["PostProc"]["StripVPeaksBkgType"]
# <----- Config: Live post processing parameters

simpleapi.CompressEvents(InputWorkspace=input, OutputWorkspace=output)
if simpleapi.mtd[str(input)].run().getProtonCharge() > 0.:
    simpleapi.NormaliseByCurrent(InputWorkspace=input, OutputWorkspace=output)

simpleapi.PDDetermineCharacterizations(InputWorkspace=output,
                                       Characterizations='characterizations',
                                       ReductionProperties='__pd_reduction_properties')
manager = mantid.PropertyManagerDataService.retrieve('__pd_reduction_properties')


def getRunId(manager, key):
    value = manager[key].value[0]
    if value == 0:
        return None
    else:
        return 'PG3_'+str(value)


def smooth(wksp):
    simpleapi.ConvertUnits(InputWorkspace=wksp, OutputWorkspace=wksp,
                           Target='TOF', EMode='Elastic')
    simpleapi.FFTSmooth(InputWorkspace=wksp,
                        OutputWorkspace=wksp,
                        Filter="Butterworth",
                        Params='20,2',
                        IgnoreXBins=True,
                        AllSpectra=True)
    simpleapi.ConvertUnits(InputWorkspace=wksp, OutputWorkspace=wksp,
                           Target='dSpacing', EMode='Elastic')


processingParams = {'CalibrationWorkspace': 'PG3_cal',
                    'GroupingWorkspace': 'PG3_group',
                    'MaskWorkspace': 'PG3_mask',
                    'Params': [tof_bin_width_pproc],
                    'RemovePromptPulseWidth': rppw_pproc,
                    'ReductionProperties': '__pd_reduction_properties'}

can = getRunId(manager, 'container')
if can is not None and not simpleapi.mtd.doesExist(can):
    mantid.logger.information("processing container '%s'" % can)
    simpleapi.AlignAndFocusPowderFromFiles(Filename=can,
                                           OutputWorkspace=can,
                                           CacheDir='/tmp',
                                           **processingParams)
    simpleapi.ConvertUnits(InputWorkspace=can,
                           OutputWorkspace=can,
                           Target='dSpacing',
                           EMode='Elastic')
    simpleapi.NormaliseByCurrent(InputWorkspace=can,
                                 OutputWorkspace=can)

if can is not None:
    simpleapi.Minus(LHSWorkspace=output,
                    RHSWorkspace=can,
                    OutputWorkspace=output)

van = getRunId(manager, 'vanadium')
if van is not None and not simpleapi.mtd.doesExist(van):
    mantid.logger.information("processing vanadium '%s'" % van)
    simpleapi.LoadEventNexus(Filename=van,
                             OutputWorkspace=van)
    simpleapi.AlignAndFocusPowderFromFiles(Filename=van,
                                           OutputWorkspace=van,
                                           CacheDir='/tmp',
                                           **processingParams)
    simpleapi.NormaliseByCurrent(InputWorkspace=van,
                                 OutputWorkspace=van)

    vanback = getRunId(manager, 'vanadium_background')
    if vanback is not None:
        mantid.logger.information("processing vanadium background '%s'" % vanback)
        simpleapi.AlignAndFocusPowderFromFiles(Filename=vanback,
                                               OutputWorkspace='__vanback',
                                               CacheDir='/tmp',
                                               **processingParams)
        vanback = '__vanback'
        simpleapi.NormaliseByCurrent(InputWorkspace=vanback,
                                     OutputWorkspace=vanback)

        mantid.logger.information("subtracting vanadium background")
        simpleapi.Minus(LHSWorkspace=van,
                        RHSWorkspace=vanback,
                        OutputWorkspace=van,
                        ClearRHSWorkspace=True)

        simpleapi.DeleteWorkspace(Workspace=vanback)
        simpleapi.CompressEvents(InputWorkspace=van,
                                 OutputWorkspace=van)

    simpleapi.ConvertUnits(InputWorkspace=van,
                           OutputWorkspace=van,
                           Target='dSpacing',
                           EMode='Elastic')

    simpleapi.StripVanadiumPeaks(InputWorkspace=van,
                                 OutputWorkspace=van,
                                 BackgroundType=strip_v_bt,
                                 PeakPositionTolerance=strip_v_pp_tol)
    smooth(van)

if van is not None:
    simpleapi.Divide(LHSWorkspace=output,
                     RHSWorkspace=van,
                     OutputWorkspace=output)

div = simpleapi.SavePlot1D(InputWorkspace=output,
                           OutputType='plotly',
                           XLabel='d-spacing (A)')
runNumber = simpleapi.mtd[output].getRunNumber()
if runNumber > 0:
    mantid.logger.information('Posting plot of PG3_%s' % runNumber)
    try:
        from postprocessing.publish_plot import publish_plot
    except ImportError:
        from finddata import publish_plot

    request = publish_plot('PG3', runNumber, files={'file': div})

    mantid.logger.information("post returned %d" % request.status_code)
    mantid.logger.information("resulting document:")
    mantid.logger.information(str(request.text))
else:
    s_part1 = "Not posting becuase run number is "
    s_part2 = "not greater than zero: %s" % runNumber
    mantid.logger.information(s_part1 + s_part2)
Loading