Commit 4169aef8 authored by Zhang, Yuanpeng's avatar Zhang, Yuanpeng
Browse files

upgrade the live scripts to be consistent with auto in terms of config

parent cc614c1c
Loading
Loading
Loading
Loading
+39 −0
Original line number Diff line number Diff line
CONDA_ENV = 'mantid-qa'

import mantid
from mantid import simpleapi
import os

# get information from autoreduction

cal_dir = '/SNS/PG3/shared/CALIBRATION/2022-2_11A_CAL/'
cal_file  = os.path.join(cal_dir,'PG3_PAC_d54007_2022_12_01.h5') # contains ALL grouping 
char_backgrounds = os.path.join(cal_dir, "PG3_char_2022_12_03-HighRes_PAC_1.4_MW.txt") 
char_inplane = os.path.join(cal_dir, "PG3_char_2022_12_03_PAC_limit_1.4MW.txt")


##### this will clear out the cache directory
#filenames = [os.path.join('/tmp', item) for item in os.listdir('/tmp') if 'PG3_' in item]
#for filename in filenames:
#    os.unlink(filename)

mantid.logger.information('Number events = %d' % input.getNumberEvents())

simpleapi.PDLoadCharacterizations(Filename=char_backgrounds+','+char_inplane,
                                  OutputWorkspace='characterizations')
simpleapi.PDDetermineCharacterizations(InputWorkspace=input,
                                       Characterizations='characterizations',
                                       ReductionProperties='__pd_reduction_properties')
manager = mantid.PropertyManagerDataService.retrieve('__pd_reduction_properties')

simpleapi.Rebin(InputWorkspace=input, OutputWorkspace=input,
                Params=(manager['tof_min'].value,100,manager['tof_max'].value))

if True: #input.getNumberEvents() > 0:
    simpleapi.AlignAndFocusPowder(InputWorkspace=input, OutputWorkspace=output,
                                  CalFilename=cal_file,
                                  Params=[-0.0008],
                                  RemovePromptPulseWidth=0, # should be 50
                                  ReductionProperties='__pd_reduction_properties')
    simpleapi.ConvertUnits(InputWorkspace=output, OutputWorkspace=output,
                           Target='dSpacing', EMode='Elastic')
+1 −1
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ config_file_f = os.path.join("/SNS/PG3/shared/livereduce/live_config_files",
                             config_file)

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

# -----> Config: Live post processing parameters
+3 −3
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ config_file_f = os.path.join("/SNS/PG3/shared/livereduce/live_config_files",
                             config_file)

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

# -----> Config: Calib and char files
@@ -30,7 +30,7 @@ for key in must_keys:
    if key not in input_config.keys():
        raise Exception(f"No {key} found in {config_file_f}.")
for key in infiles_must_keys:
    if key not in auto_params["InputFiles"].keys():
    if key not in input_config["InputFiles"].keys():
        raise Exception(f"No {key} found under 'InputFiles' in {config_file_f}.")

cal_dir = input_config["CalibDirectory"]
@@ -45,7 +45,7 @@ char_inplane = os.path.join(cal_dir,
# -----> Config: Live reduction parameters
tof_bin_width_init = input_config["Params"]["Proc"]["TOFBinWidthInit"]
tof_bin_width_proc = input_config["Params"]["Proc"]["TOFBinWidthProc"]
rppw = nput_config["Params"]["Proc"]["RemovePromptPulseWidth"]
rppw = input_config["Params"]["Proc"]["RemovePromptPulseWidth"]
# <----- Config: Live reduction parameters

mantid.logger.information('Number events = %d' % input.getNumberEvents())
+4 −4
Original line number Diff line number Diff line
{
    "CalibDirectory": "/SNS/PG3/shared/CALIBRATION/2022-2_11A_CAL/",
    "CalibDirectory": "/SNS/PG3/shared/CALIBRATION/2023-1_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"
        "CalibFile": "PG3_PAC_d55292_2023_06_14-HighRes_8mmCan.h5",
        "CharBackgrounds": "PG3_char_2023_6_22-HighRes_PAC_1.4MW.txt",
        "CharInPlane": "PG3_char_2023_6_22_PAC_limit_1.4MW.txt"
    },
    "Params": {
        "Proc": {
+88 −36
Original line number Diff line number Diff line
CONDA_ENV = 'mantid-qa'

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(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.:
@@ -12,6 +39,7 @@ simpleapi.PDDetermineCharacterizations(InputWorkspace=output,
                                       ReductionProperties='__pd_reduction_properties')
manager = mantid.PropertyManagerDataService.retrieve('__pd_reduction_properties')


def getRunId(manager, key):
    value = manager[key].value[0]
    if value == 0:
@@ -19,6 +47,7 @@ def getRunId(manager, key):
    else:
        return 'PG3_'+str(value)


def smooth(wksp):
    simpleapi.ConvertUnits(InputWorkspace=wksp, OutputWorkspace=wksp,
                           Target='TOF', EMode='Elastic')
@@ -35,72 +64,95 @@ def smooth(wksp):
processingParams = {'CalibrationWorkspace': 'PG3_cal',
                    'GroupingWorkspace': 'PG3_group',
                    'MaskWorkspace': 'PG3_mask',
                    'Params':[-0.0008],
                    'RemovePromptPulseWidth':0, # should be 50
                    '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,
    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)
    #smooth(can)
    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)
    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,
    simpleapi.LoadEventNexus(Filename=van,
                             OutputWorkspace=van)
    simpleapi.AlignAndFocusPowderFromFiles(Filename=van,
                                           OutputWorkspace=van,
                                           CacheDir='/tmp',
                                           **processingParams)
    simpleapi.NormaliseByCurrent(InputWorkspace=van, OutputWorkspace=van)
    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',
        simpleapi.AlignAndFocusPowderFromFiles(Filename=vanback,
                                               OutputWorkspace='__vanback',
                                               CacheDir='/tmp',
                                               **processingParams)
        vanback = '__vanback'
        simpleapi.NormaliseByCurrent(InputWorkspace=vanback, OutputWorkspace=vanback)
        simpleapi.NormaliseByCurrent(InputWorkspace=vanback,
                                     OutputWorkspace=vanback)

        mantid.logger.information("subtracting vanadium background")
        simpleapi.Minus(LHSWorkspace=van, RHSWorkspace=vanback, OutputWorkspace=van,
        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='Quadratic', PeakPositionTolerance=.05)
        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)
    simpleapi.Divide(LHSWorkspace=output,
                     RHSWorkspace=van,
                     OutputWorkspace=output)

##### generate plot and post
div = simpleapi.SavePlot1D(InputWorkspace=output, OutputType='plotly',
div = simpleapi.SavePlot1D(InputWorkspace=output,
                           OutputType='plotly',
                           XLabel='d-spacing (A)')
runNumber = simpleapi.mtd[output].getRunNumber()
if runNumber > 0:  # it is 0 between runs
if runNumber > 0:
    mantid.logger.information('Posting plot of PG3_%s' % runNumber)
    try: # version on autoreduce
    try:
        from postprocessing.publish_plot import publish_plot
    except ImportError: # version on instrument computers
    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:
    mantid.logger.information('Not posting becuase run number is not greater than zero: %s' % runNumber)
    s_part1 = "Not posting becuase run number is "
    s_part2 = "not greater than zero: %s" % runNumber
    mantid.logger.information(s_part1 + s_part2)
Loading