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

change to pg32 machine

parent 9237f031
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -29,9 +29,9 @@ simpleapi.LoadDiffCal(InstrumentName="PG3",
                      Filename=cal_file,
                      WorkspaceName="PG3")

with open("/SNS/PG3/shared/livereduce/tmp/alignandfocuspowder.json", "r") as f:
with open("/SNS/PG3/shared/livereduce/tmp/alignandfocuspowder_zyp.json", "r") as f:
    af_config = json.load(f)
with open("/SNS/PG3/shared/livereduce/tmp/can.name", "r") as f:
with open("/SNS/PG3/shared/livereduce/tmp/can_zyp.name", "r") as f:
    can = f.readline().strip()

simpleapi.AlignAndFocusPowderFromFiles(Filename=can,
+2 −2
Original line number Diff line number Diff line
@@ -29,9 +29,9 @@ simpleapi.LoadDiffCal(InstrumentName="PG3",
                      Filename=cal_file,
                      WorkspaceName="PG3")

with open("/SNS/PG3/shared/livereduce/tmp/alignandfocuspowder.json", "r") as f:
with open("/SNS/PG3/shared/livereduce/tmp/alignandfocuspowder_zyp.json", "r") as f:
    af_config = json.load(f)
with open("/SNS/PG3/shared/livereduce/tmp/van.name", "r") as f:
with open("/SNS/PG3/shared/livereduce/tmp/van_zyp.name", "r") as f:
    van = f.readline().strip()

simpleapi.LoadEventNexus(Filename=van,
+2 −2
Original line number Diff line number Diff line
@@ -29,9 +29,9 @@ simpleapi.LoadDiffCal(InstrumentName="PG3",
                      Filename=cal_file,
                      WorkspaceName="PG3")

with open("/SNS/PG3/shared/livereduce/tmp/alignandfocuspowder.json", "r") as f:
with open("/SNS/PG3/shared/livereduce/tmp/alignandfocuspowder_zyp.json", "r") as f:
    af_config = json.load(f)
with open("/SNS/PG3/shared/livereduce/tmp/vanback.name", "r") as f:
with open("/SNS/PG3/shared/livereduce/tmp/vanback_zyp.name", "r") as f:
    vanback = f.readline().strip()

simpleapi.AlignAndFocusPowderFromFiles(Filename=vanback,
+9 −1
Original line number Diff line number Diff line
@@ -310,6 +310,14 @@ else:
        except:  # noqa
            raise Exception("Failed in vanadium normalization.")

    # live_data_d = os.path.join("/SNS/PG3", ipts, "shared/livereduce")
    # if not os.path.exists(live_data_d):
    #     os.makedirs(live_data_d)
    # simpleapi.SaveAscii(
    #     InputWorkspace=output,
    #     Filename=os.path.join(live_data_d, f"PG3_{run_num}.dat")
    # )

    div = simpleapi.SavePlot1D(
        InputWorkspace=output,
        OutputType='plotly',
+348 −0
Original line number Diff line number Diff line
import json
import mantid
from mantid import simpleapi
import os
import subprocess
import time

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

try:
    run = input.getRun()
    ipts = run.getLogData("experiment_identifier").value
    run_num = run.getLogData("run_number").value
except:  # noqa E722
    ipts = None
    run_num = None

if not all([run_num, ipts]):
    mantid.logger.information("Live post proc run number unavailable.")
    simpleapi.CreateSampleWorkspace(OutputWorkspace=output)
else:
    # -----> 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: Calib and char files
    must_keys = ["CalibDirectory", "InputFiles"]
    infiles_must_keys = ["CalibFile", "CharBackgrounds", "CharInPlane"]
    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 input_config["InputFiles"].keys():
            raise Exception(
                f"No {key} found under 'InputFiles' in {config_file_f}."
            )
    
    cal_dir = input_config["CalibDirectory"]
    cal_file = os.path.join(
        cal_dir,
        input_config["InputFiles"]["CalibFile"]
    )
    char_backgrounds = os.path.join(
        cal_dir,
        input_config["InputFiles"]["CharBackgrounds"]
    )
    char_inplane = os.path.join(
        cal_dir,
        input_config["InputFiles"]["CharInPlane"]
    )
    # <----- Config: Calib and char files
    
    # -----> 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

    try:
        accum_prtc = simpleapi.mtd[str(input)].run().getProtonCharge()
        if accum_prtc > 0.:
            mantid.logger.information(f"Accumulated proton charge: {accum_prtc}")
            simpleapi.NormaliseByCurrent(
                InputWorkspace=input,
                OutputWorkspace=output,
                RecalculatePCharge=True
            )
    except:
        raise Exception("Failed in proton charge normalization.")

    simpleapi.SaveNexus(
        InputWorkspace=input,
        Filename=f"/SNS/PG3/shared/livereduce/tmp/pproc_zyp_tmp.nxs")
    simpleapi.LoadNexus(
        Filename="/SNS/PG3/shared/livereduce/tmp/pproc_zyp_tmp.nxs",
        OutputWorkspace="pproc_in"
    )

    try:
        simpleapi.PDLoadCharacterizations(
            Filename=char_backgrounds + ',' + char_inplane,
            OutputWorkspace='characterizations'
        )
        simpleapi.PDDetermineCharacterizations(
            InputWorkspace="pproc_in",
            Characterizations='characterizations',
            ReductionProperties='__pd_reduction_properties'
        )
        manager = mantid.PropertyManagerDataService.retrieve(
            '__pd_reduction_properties'
        )
    except:
        raise Exception("Failed in characterization determination.")

    mantid.logger.information("In post processing ...")
    
    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',
                        'PreserveEvents': True,
                        'CompressTolerance': 0.}
    
    can = getRunId(manager, 'container')
    with open(
        "/SNS/PG3/shared/livereduce/tmp/alignandfocuspowder_zyp.json",
        "w"
    ) as f:
        json.dump(processingParams, f, indent=4)
    
    if can is not None and not simpleapi.mtd.doesExist(can):
        with open("/SNS/PG3/shared/livereduce/tmp/can_zyp.name", "w") as f:
            f.write(can)
        mantid.logger.information("processing container '%s'" % can)
        subprocess.run(
            [
                "mantidpython",
                "/SNS/PG3/shared/livereduce/align_focus_can.py"
            ]
        )
        while not os.path.exists(
            f"/SNS/PG3/shared/livereduce/tmp/can_{can}.nxs"
        ):
            time.sleep(1)
        simpleapi.LoadNexus(
            Filename=f"/SNS/PG3/shared/livereduce/tmp/can_{can}.nxs",
            OutputWorkspace=can
        )
        simpleapi.ConvertUnits(
            InputWorkspace=can,
            OutputWorkspace=can,
            Target='dSpacing',
            EMode='Elastic'
        )
        simpleapi.NormaliseByCurrent(
            InputWorkspace=can,
            OutputWorkspace=can
        )
    

    if can is not None:
        try:
            simpleapi.RebinToWorkspace(
                WorkspaceToRebin=can,
                WorkspaceToMatch=output,
                OutputWorkspace=can
            )
        except:  # noqa
            raise Exception("Failed in rebinning.")

        simpleapi.SaveNexus(
            InputWorkspace=can,
            Filename=f"/SNS/PG3/shared/livereduce/tmp/can_{can}_final.nxs")
        simpleapi.SaveNexus(
            InputWorkspace=output,
            Filename=f"/SNS/PG3/shared/livereduce/tmp/live_check.nxs")

        try:
            simpleapi.Minus(
                LHSWorkspace=output,
                RHSWorkspace=can,
                OutputWorkspace=output
            )
        except:  # noqa
            raise Exception("Failed in background subtraction.")
    
    van = getRunId(manager, 'vanadium')
    if van is not None and not simpleapi.mtd.doesExist(van):
        with open("/SNS/PG3/shared/livereduce/tmp/van_zyp.name", "w") as f:
            f.write(van)
        mantid.logger.information("processing vanadium '%s'" % van)
        subprocess.run(
            [
                "mantidpython",
                "/SNS/PG3/shared/livereduce/align_focus_van.py"
            ]
        )
        while not os.path.exists(
            f"/SNS/PG3/shared/livereduce/tmp/van_{van}.nxs"
        ):
            time.sleep(1)
        simpleapi.LoadNexus(
            Filename=f"/SNS/PG3/shared/livereduce/tmp/van_{van}.nxs",
            OutputWorkspace=van
        )
        simpleapi.NormaliseByCurrent(
            InputWorkspace=van,
            OutputWorkspace=van
        )
    
        vanback = getRunId(manager, 'vanadium_background')
        with open("/SNS/PG3/shared/livereduce/tmp/vanback_zyp.name", "w") as f:
            f.write(vanback)
        if vanback is not None:
            mantid.logger.information(
                "processing vanadium background '%s'" % vanback
            )
            subprocess.run(
                [
                    "mantidpython",
                    "/SNS/PG3/shared/livereduce/align_focus_vanback.py"
                ]
            )
            while not os.path.exists(
                f"/SNS/PG3/shared/livereduce/tmp/vanback_{vanback}.nxs"
            ):
                time.sleep(1)
            simpleapi.LoadNexus(
                Filename=f"/SNS/PG3/shared/livereduce/tmp/vanback_{vanback}.nxs",
                OutputWorkspace="__vanback"
            )
            vanback = '__vanback'
            simpleapi.NormaliseByCurrent(
                InputWorkspace=vanback,
                OutputWorkspace=vanback
            )

            mantid.logger.information("subtracting vanadium background")

            try:
                simpleapi.RebinToWorkspace(
                    WorkspaceToRebin=vanback,
                    WorkspaceToMatch=van,
                    OutputWorkspace=vanback
                )
                simpleapi.Minus(
                    LHSWorkspace=van,
                    RHSWorkspace=vanback,
                    OutputWorkspace=van,
                    ClearRHSWorkspace=True
                )
            except:  # noqa
                raise Exception("Failed in bkg subtraction for vanadium.")
    
            simpleapi.DeleteWorkspace(Workspace=vanback)

        try:
            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)
        except:  # noqa
            raise Exception("Failed in preparing vanadium normalization.")
    
    if van is not None:
        try:
            simpleapi.RebinToWorkspace(
                WorkspaceToRebin=van,
                WorkspaceToMatch=output,
                OutputWorkspace=van
            )
        except:  # noqa
            raise Exception("Failed in rebinning vanadium.")

        simpleapi.SaveNexus(
            InputWorkspace=van,
            Filename=f"/SNS/PG3/shared/livereduce/tmp/van_{van}_final.nxs")

        try:
            simpleapi.Divide(
                LHSWorkspace=output,
                RHSWorkspace=van,
                OutputWorkspace=output
            )
        except:  # noqa
            raise Exception("Failed in vanadium normalization.")

    # live_data_d = os.path.join("/SNS/PG3", ipts, "shared/livereduce")
    # if not os.path.exists(live_data_d):
    #     os.makedirs(live_data_d)
    # simpleapi.SaveAscii(
    #     InputWorkspace=output,
    #     Filename=os.path.join(live_data_d, f"PG3_{run_num}.dat")
    # )

    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

        try:
            request = publish_plot('PG3', runNumber, files={'file': div})
        except:  # noqa
            raise Exception("Failed in data plot publishing.")

        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