# -*- coding: utf-8 -*-

#####################
## Command Sequence of Data Reduction
## 2010.11.10 ver 0.2 use GetHistogramOfEnergyTransfer
## 2010.05.18 ver 0.1
#####################

# special reserved word for commands history
HISTORY = None
# reserved words for return value in this.
DAT = None

import Manyo as mm
import ana.Reduction.BaseCommands as BaseCom
from ana.Reduction.DefaultInstParams import *

import os

########################################
def CopyData(first_run=10,last_run=-1):
    """
    Copy EventData file from DAQ0 or DAQ1 into /data/XXX
    @param runno_from (int) First Run No of a measurement to be copied
    @param runno_to (int) Last Run No of a measurement to be copied
    @retval None
    """
    BaseCom.CopyData(first_run,last_run)

########################################
def GetDataOfWhite(runNo=197,startTOF=-1,endTOF=-1,binTOF=-1,MaskFile="mask.txt",NormFactor=1000000.0):
    """
    Load EventData and convert to histogram using corrections as below:
    - GetHistogram(runNo,startTOF=-1,endTOF=-1,binTOF=-1,startPSD=-1,endPSD=-1,pixelNo=100)
    - NormByBeamCurrent
    - DoMask
    - SolidAngleCorrection
    @param runNo      (int)    Run Number
    @param MaskFile   (string) file name for masking
    @param NormFactor (double) normalize factor data*factor/beamCurrent. if 0, do nothing.
    @retval DAT     (ElementContainerMatrix)
    """
    startPSD=-1
    endPSD=-1
    pixelNo=Def_NumOfPixel
    dat = BaseCom.GetHistogram(runNo,0,startTOF,endTOF,binTOF,startPSD,endPSD,pixelNo)
    if NormFactor!=0:
        BaseCom.NormByBeamCurrent(dat, NormFactor)
    BaseCom.DoMask(dat,MaskFile)
    BaseCom.SolidAngleCorrection(dat)

    return dat

#########################################
def GetDataOfMonochroEi(runNo=-1,Ei=0.0,Resolution=0.5,deltaE=0.0,L1=18.03,MaskFile="mask.txt",NormFactor=1000000.0):
    """
    Load EventData and convert to histogram using corrections as below:
    - GetHistogramBydE(runNo,Ei,Resolution=round(deltaE/Ei)/10,t0shift=0.0,L1,L2=2.5,NumOfPixel=100)
    - NormByBeamCurrent(DAT,NormFactor)
    - DoMask(DAT)
    - TofToEnergyTransfer(DAT,Ei,L1,t0shift=0.0,deltaE,EminRate=0.9,EmaxRate=0.9)
    - SolidAngleCorrection(DAT)
    - kikfCorrection(DAT)
    @param runNo    (int) Run Number
    @param Ei       (double) Incident Energy
    @param Resolution (double) Energy Resolution for tof binnings at Elastic Peak [%]
    @param deltaE   (double) Delta Energy Transfer [meV]
    @param L1       (double)  Distance between moderator to sample [m]
    @param MaskFile (string) file name for masking
    @param NormFactor (double) normalize factor data*factor/beamCurrent. if 0, do nothing.
    @retval DAT     ElementCotainerMatix as result
    """
    if (Ei<=0.0) or (deltaE<=0.0):
        raise UserWarning, "Ei and deltaE must be positive."

    t0shift = 0.0
    L2 = Def_L2
    numOfPixel = Def_NumOfPixel
    EminRate = 0.9
    EmaxRate = 0.9
    
    dat = BaseCom.GetHistogramBydE(runNo,Ei,Resolution,t0shift,L1,L2,numOfPixel)
    if NormFactor!=0:
        BaseCom.NormByBeamCurrent(dat, NormFactor)
    BaseCom.DoMask(dat,MaskFile)
    BaseCom.SolidAngleCorrection(dat)
    BaseCom.TofToEnergyTransfer(dat,Ei,L1,t0shift,deltaE,EminRate,EmaxRate)
    BaseCom.KiKfCorrection(dat)

    return dat

#########################################
def GetDataOfMonochroEi2(runNo=-1,Ei=0.0,deltaE=0.0,Emin=0.0,Emax=0.0,MaskFile="mask.txt",NormFactor=1000000.0):
    """
    Load EventData and convert to histogram using corrections as below:
    - GetHistogramOfEnergyTransfer(runNo,Ei,dHW,hw_min,hw_max,startTime,endTime)
    - NormByBeamCurrent(DAT,NormFactor)
    - DoMask(DAT)
    - SolidAngleCorrection(DAT)
    - kikfCorrection(DAT)
    @param runNo    (int) Run Number
    @param Ei       (double) Incident Energy [meV]
    @param deltaE   (double) Delta Energy Transfer [meV]
    @param Emin     (double) minimum of Energy Transfer [meV]
    @param Emax     (double) maximum of Energy Transfer [meV]
    @param MaskFile (string) file name for masking
    @param NormFactor (double) normalize factor data*factor/beamCurrent. if 0, do nothing.
    @retval DAT     ElementCotainerMatix as result
    """
    
    dat = BaseCom.GetHistogramOfEnergyTransfer(runNo,Ei,deltaE,Emin,Emax)
    if runNo!=0: ## [inamura 101115]
        if NormFactor!=0:
            BaseCom.NormByBeamCurrent(dat, NormFactor)
        BaseCom.DoMask(dat,MaskFile)
        BaseCom.SolidAngleCorrection(dat)
        BaseCom.KiKfCorrection(dat)

    return dat

##########################################
def ToPowder(dat=DAT,startQ=-1.0,endQ=-1.0,deltaQ=0.1):
    """
    Convert data from crystal to powder

    @param 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)
    """

    return BaseCom.ToPowder(dat,startQ,endQ,deltaQ)

#########################################
def MakeRunList( start_run=-1, end_run=-1, output="~/run_list.txt",flag_proton=1 ):
    """
    Make run list 
    @param start_run (int) start run number to be listed.
    @param end_run (int) start run number to be listed.
    @param output (string) output file name
    @param flag_proton (int) 0: no protons, 1:with protons
    @return None
    """
    BaseCom.MakeRunList( start_run, end_run, output, flag_proton )


# Dictionary for entry the name of functions
_functions = {
               "CopyData":CopyData,
               "GetDataOfWhite":GetDataOfWhite,
	       "GetDataOfMonochroEi":GetDataOfMonochroEi,
	       "GetDataOfMonochroEi2":GetDataOfMonochroEi2,
               "ToPowder":ToPowder,
               "MakeRunList":MakeRunList
             }
         
