#!/usr/bin/python3
# -*- coding: utf-8 -*-

import utsusemi.SAS.ana.Reduction.HistBaseCommands as HBC
import utsusemi.SAS.ana.Reduction.TransBaseCommands as TBC

# reserved words for return value in this.
DAT = None
ecs = None
EC = None
ret = None
PyObj = None
Iq = None
Tr = None


########################################
def GetHistogramSAS( runNo="0", Xrange="0.7 7.6 0.1 dL", Bank="SM", TimeSlice="-1 -1", frameBoundary="0.0", isTimeFoc=False, SolAngCor=True, paramFiles="- -" ):
    """
    Load EventData to produce histogram
    GetHistogramSAS( runNo=0, AxType="lambda", Xrange="0.7 7.7 0.05", Bank="SM", TimeSlice="-1 -1", frameBoundary="0.0", isTimeFoc=False )

    @param runNo     (strng)     Run Number
    @param Xrange    (string)  "startX endX widthX type" separated with space (not comma) : type=[dL, dL/L, tof, d, rd, ene]
    @param Bank      (string)  "SM"...  Monitors by "MON1" or "MON2"
    @param TimeSlice (string)  "startTime endTime" separated with space: "-1 -1" means no-setting.
    @param frameBoundary (string) "frameBoundary" or "frameBoundary TofMask_min TofMask_max"
    @param isTimeFoc (bool)    True or False
    @param SolAngCor  (bool)    True or False for Solid Angle Correction
    @param paramFiles (string) "WiringInfo DetectorInfo"
    @retval DAT (ElementContainerMatrix)
    """
    return HBC.GetHistogramSAS( runNo, Xrange, Bank, TimeSlice, frameBoundary, isTimeFoc, SolAngCor, paramFiles )

########################################
def GetMonHistSAS( runNo="0", MonNo=1, AxType="lambda", Xrange="0.7 7.6 0.1", MonIndex=0, TimeSlice="-1 -1", frameBoundary="0.0", CalibEffi=True, RPMTarea="150 150 152 152", RPMTeffiParam="0.10516 0.04838",paramFiles="- -" ):
    """
    Load EventData to produce histogram of monitors
    GetMonHistSAS( runNo="0", MonNo=1, AxType="lambda", Xrange="0.7 7.7 0.05", MonIndex=0, TimeSlice="-1 -1", frameBoundary="0.0", CalibEffi=True, RPMTarea="150 150 152 152", RPMTeffiParam="0.10516 0.04838" )

    @param runNo     (string)     Run Number
    @param MonNo     (int)     Monitor Number 1 (up-stream), 2 (down-stream), 3 or 10
    @param AxType    (string)  Type must be "tof', "lambda", "lamda2", "d" or "rd"
    @param Xrange    (string)  "startX endX widthX" separated with space (not comma)
    @param MonIndex  (int)
    @param TimeSlice (string)  "startTime endTime" separated with space: "-1 -1" means no-setting.
    @param frameBoundary (string) "frameBoundary" or "frameBoundary TofMask_min TofMask_max"
    @param CalibEffi (bool)    Monitor-detector efficiency calibration
    @param RPMTarea  (string)  Summation area of RPMT
    @param RPMTeffiParam (string) Efficiency parameters alpha and beta for RPMT
    @param paramFiles (string) "<WirignInfo.xml> <DetectorInfo>"
    @retval EC (ElementContainer)
    """
    return HBC.GetMonHistSAS( runNo, MonNo, AxType, Xrange, MonIndex, TimeSlice, frameBoundary, CalibEffi, RPMTarea, RPMTeffiParam, paramFiles )

########################################
def GetPulseHeightSAS( runNo="0", Xrange="0 40000 100", Bank="SM", TimeSlice="-1 -1", frameBoundary="0.0", paramFiles="- -",PHbin=5 ):
    """
    Make Pulse Height data of EventData
    GetPulseHeightSAS( runNo=0, Xrange="0 10000 10", Bank="SM", TimeSlice="-1 -1", paramFiles="- -" ):

    @param runNo     (string)     Run Number
    @param Xrange    (string)  "startX endX widthX type" separated with space (not comma) : type=[dL, dL/L, tof, d]
    @param Bank      (string)  "SM"...  Monitors by "MON1" or "MON2"
    @param TimeSlice (string)  "startTime endTime" separated with space: "-1 -1" means no-setting.
    @param frameBoundary (string) "frameBoundary" or "frameBoundary TofMask_min TofMask_max"
    @param paramFiles (string) "WiringInfo DetectorInfo"
    @param PHbin      (int)    Pulse Height bin
    @retval ECM (ElementContainerMatrix)
    """
    return HBC.GetPulseHeightSAS( runNo, Xrange, Bank,TimeSlice, frameBoundary, paramFiles, PHbin)

########################################
def GetMonHistByPhSAS( runNo="0", MonNo=1, Xrange="0.0 40000.0 10.0 tof", phRange="0 10000", MonIndex=0, TimeSlice="-1 -1", frameBoundary=0.0, CalibEffi=True ):
    """
    Load EventData to produce histogram of monitors limited by pulse height
    GetMonHistByPhSAS( runNo=0, MonNo=1, Xrange="0.0 40000.0 10.0 tof", phRange="0 10000", MonIndex=0, TimeSlice="-1 -1", frameBoundary=0.0, CalibEffi=True )

    @param runNo     (string)     Run Number
    @param MonNo     (int)     Monitor Number 1 (up-stream) or 2 (down-stream)
    @param Xrange    (string)  "startX endX widthX type" separated with space (not comma) : type=[dL, dL/L, tof, d]
    @param phRange   (string)  "lld hld"
    @param MonIndex  (int)     0.. digital  1.. analog
    @param TimeSlice (string)  "startTime endTime" separated with space: "-1 -1" means no-setting.
    @param frameBoundary (float)
    @param CalibEffi (bool)    Monitor-detector efficiency calibration
    @retval EC (ElementContainer)
    """
    return HBC.GetMonHistByPhSAS( runNo, MonNo, Xrange, phRange, MonIndex, TimeSlice, frameBoundary, CalibEffi )

########################################
def MergeSMSC4DetMap(dat=DAT):
    """
    Merge Data SM and SC bank into one ElementContainerMatrix
    to plot using DetectMap
    @param dat  [Def:DAT] (ElementContainerMatrix)
    @retval ECM (ElementContainerMatrix)
    """
    return HBC.MergeSMSC4DetMap(dat)

########################################
def GetHistogramRPMT( runNo="0", isLambda=True ):
    """
    Load EventData to produce histogram
    GetHistogramSAS( runNo=0, AxType="lambda", Xrange="0.7 7.7 0.05", Bank="SM", TimeSlice="-1 -1", frameBoundary=0.0, isTimeFoc=False )

    @param runNo     (string)     Run Number
    @retval DAT (ElementContainerMatrix)
    """
    return HBC.GetHistogramRPMT( runNo, isLambda )

########################################
def RPMTRemoveCenterLine( dat=DAT, PsdRange="200 201", PixelRange="198 199" ):
    """
    Merge Data SM and SC bank into one ElementContainerMatrix
    to plot using DetectMap
    @param dat  [Def:DAT] (ElementContainerMatrix)
    @param PsdRange
    @param PixelRange
    @retval None
    """
    HBC.RPMTRemoveCenterLine( dat, PsdRange, PixelRange )

#########################################
def GetTransNormByMonCounts(SmplRunNo="104000", DrctRunNo="104000", AxType="lambda", XRange="0.7 7.6 0.1", frameBoundary=0.0, CalibEffi=True ):
    """
    Monitor histogram of sample and direct followed by a transmittance calculation.
    @param SmplRunNo     (string)    Sample measurement run number
    @param DrctRunNo     (string)    Direct beam measurement run number
    @param AxType        (string)  Type must be "tof', "lambda" or "lamda2"
    @param XRange        (string) "startX endX widthX" separated with space (not comma)
    @param frameBoundary (float)
    @param CalibEffi     (bool)    Monitor-detector efficiency calibration
    @retval Tr
    """
    return TBC.GetTransNormByMonCounts(SmplRunNo, DrctRunNo, AxType, XRange, frameBoundary, CalibEffi)

#########################################
def GetTransNormByMonCountsTS(SmplRunNo="104000", DrctRunNo="104000", AxType="lambda", XRange="0.7 7.6 0.1", frameBoundary=0.0, CalibEffi=True, SmplTSlice="-1 -1", DrctTSlice="-1 -1"):
    """
    Monitor histogram of sample and direct followed by a transmittance calculation.
    @param SmplRunNo     (string)    Sample measurement run number
    @param DrctRunNo     (strint)    Direct beam measurement run number
    @param AxType        (string)  Type must be "tof', "lambda" or "lamda2"
    @param XRange        (string) "startX endX widthX" separated with space (not comma)
    @param frameBoundary (float)
    @param CalibEffi     (bool)    Monitor-detector efficiency calibration
    @param SmplTSlice    (string) Time Slice parameters for Sample run
    @param DrctTSlice    (string) Time Slice parameters for Directrun
    @retval Tr
    """
    return TBC.GetTransNormByMonCountsTS(SmplRunNo, DrctRunNo, AxType, XRange, frameBoundary, CalibEffi, SmplTSlice, DrctTSlice )


########################################
def GetHistInelaSAS( runNo="0", Bank="SM", Ei=10.0, Erange="-5 5", dHW=0.1):
    """
    Histogram Creation for inelastic scattering on SAS

    @param runNo     (string)  Run Number "10,11,12"
    @param Bank      (string)  "SM"...  Monitors by "MON1" or "MON2"
    @param Ei        (float)   Incident energy
    @param Erange    (string)  <hw_min>, <hw_max>
    @param dHW       (float)   bin width of hw
    @retval DAT (ElementContainerMatrix)
    """
    return HBC.GetHistInelaSAS( runNo, Bank, Ei, Erange, dHW)

########################################
import Manyo.Utsusemi as mu
try:
    import utsusemi.ana.Reduction.BaseCommandsInEla as BaseCom
except:
    import ana.Reduction.BaseCommandsInEla as BaseCom
def ToPowder(dat=DAT,startQ=-1.0,endQ=-1.0,deltaQ=0.1):
    """
    Convert data from crystal to powder

    @param dat [Def:DAT] (ElementContaienrMatrix)
    @param startQ (double) Q value of range; if minus, auto calculated
    @param endQ   (double) Q value of range; if minus, auto calculated
    @param deltaQ (double) binning of Q
    @retval ECM   (ElementContainerMatrix)
    """
    mu.UtsusemiMessage("DR.ToPowder({},{},{},{})".format("<ElementContainerMatrix>",startQ,endQ,deltaQ),True)
    return BaseCom.ToPowder(dat,startQ,endQ,deltaQ)

########################################
# Dictionary for entry the name of functions
_functions = {
            "GetHistogramSAS":GetHistogramSAS,
            "GetMonHistSAS":GetMonHistSAS,
            "GetPulseHeightSAS":GetPulseHeightSAS,
            "GetMonHistByPhSAS":GetMonHistByPhSAS,
            "MergeSMSC4DetMap":MergeSMSC4DetMap,
            "GetHistogramRPMT":GetHistogramRPMT,
            "RPMTRemoveCenterLine":RPMTRemoveCenterLine,
            "GetTransNormByMonCounts":GetTransNormByMonCounts,
            "GetTransNormByMonCountsTS":GetTransNormByMonCountsTS,
            "GetHistInelaSAS":GetHistInelaSAS,
            "ToPowder":ToPowder,
            }
_functionsOrder = [
    "GetHistogramSAS",
    "GetMonHistSAS",
    "GetPulseHeightSAS",
    "GetMonHistByPhSAS",
    "MergeSMSC4DetMap",
    "GetHistogramRPMT",
    "RPMTRemoveCenterLine",
    "GetTransNormByMonCounts",
    "GetTransNormByMonCountsTS",
    "GetHistInelaSAS",
    "ToPowder",
    ]

