Example #1
0
 def runTest(self):
     ws = Load(Filename="INTER00013469.nxs")
     ws = ConvertUnits(InputWorkspace=ws,Target="Wavelength",AlignBins=1)
     #Io=CropWorkspace(InputWorkspace=ws,XMin=0.8,XMax=14.5,StartWorkspaceIndex=2,EndWorkspaceIndex=2)
     #D=CropWorkspace(InputWorkspace=ws,XMin=0.8,XMax=14.5,StartWorkspaceIndex=3)
     #I= Divide(LHSWorkspace=D,RHSWorkspace=Io,AllowDifferentNumberSpectra=True)
     detector_component_name =  'linear-detector'
     sample_component_name = 'some-surface-holder'
     theta = 0.7
     l2q(ws, detector_component_name, theta, sample_component_name) # This generates an output workspace called IvsQ
Example #2
0
 def runTest(self):
     ws = Load(Filename="INTER00013469.nxs")
     ws = ConvertUnits(InputWorkspace=ws, Target="Wavelength", AlignBins=1)
     #Io=CropWorkspace(InputWorkspace=ws,XMin=0.8,XMax=14.5,StartWorkspaceIndex=2,EndWorkspaceIndex=2)
     #D=CropWorkspace(InputWorkspace=ws,XMin=0.8,XMax=14.5,StartWorkspaceIndex=3)
     #I= Divide(LHSWorkspace=D,RHSWorkspace=Io,AllowDifferentNumberSpectra=True)
     detector_component_name = 'linear-detector'
     sample_component_name = 'some-surface-holder'
     theta = 0.7
     l2q(ws, detector_component_name, theta, sample_component_name
         )  # This generates an output workspace called IvsQ
Example #3
0
def quick_explicit(run, i0_monitor_index, lambda_min, lambda_max,  background_min, background_max, int_min, int_max,
                   point_detector_start=0, point_detector_stop=0, multi_detector_start=0, theta=None,
                   pointdet=True,roi=[0,0], db=[0,0], trans='', debug=False, correction_strategy=NullCorrectionStrategy(),
                   stitch_start_overlap=None, stitch_end_overlap=None, stitch_params=None,
                   polcorr=False, crho=None, calpha=None, cAp=None, cPp=None, detector_component_name='point-detector',
                   sample_component_name='some-surface-holder', correct_positions=True ):

    '''
    Version of quick where all parameters are explicitly provided.
    '''

    _sample_ws = ConvertToWavelength.to_single_workspace(run)
    nHist =  _sample_ws.getNumberHistograms()
    to_lam = ConvertToWavelength(run)

    if pointdet:
        detector_index_ranges = (point_detector_start, point_detector_stop)
    else:
        detector_index_ranges = (multi_detector_start, nHist-1)


    _monitor_ws, _detector_ws = to_lam.convert(wavelength_min=lambda_min, wavelength_max=lambda_max,
                                               detector_workspace_indexes=detector_index_ranges,
                                               monitor_workspace_index=i0_monitor_index, correct_monitor=True,
                                               bg_min=background_min, bg_max=background_max )

    inst = _sample_ws.getInstrument()
    # Some beamline constants from IDF

    print i0_monitor_index
    print nHist

    if run=='0':
        RunNumber = '0'
    else:
        RunNumber = groupGet(_sample_ws.getName(),'samp','run_number')

    if not pointdet:
        # Proccess Multi-Detector; assume MD goes to the end:
        # if roi or db are given in the function then sum over the apropriate channels
        print "This is a multidetector run."

        _I0M = RebinToWorkspace(WorkspaceToRebin=_monitor_ws,WorkspaceToMatch=_detector_ws)
        IvsLam = _detector_ws / _I0M
        if (roi != [0,0]) :
            ReflectedBeam = SumSpectra(InputWorkspace=IvsLam, StartWorkspaceIndex=roi[0], EndWorkspaceIndex=roi[1])
        if (db != [0,0]) :
            DirectBeam = SumSpectra(InputWorkspace=_detector_ws, StartWorkspaceIndex=db[0], EndWorkspaceIndex=db[1])
            ReflectedBeam = ReflectedBeam / DirectBeam
        polCorr(polcorr, IvsLam, crho, calpha, cAp, cPp)
        if theta and correct_positions:
            IvsQ = l2q(ReflectedBeam, detector_component_name, theta, sample_component_name)
        else:
            IvsQ = ConvertUnits(InputWorkspace=ReflectedBeam, Target="MomentumTransfer")


    # Single Detector processing-------------------------------------------------------------
    else:
        print "This is a Point-Detector run."
        # handle transmission runs
        # process the point detector reflectivity
        _I0P = RebinToWorkspace(WorkspaceToRebin=_monitor_ws,WorkspaceToMatch=_detector_ws)
        IvsLam = Scale(InputWorkspace=_detector_ws,Factor=1)

        if not trans:
            print "No transmission file. Trying default exponential/polynomial correction..."
            IvsLam = correction_strategy.apply(_detector_ws)
            IvsLam = Divide(LHSWorkspace=IvsLam, RHSWorkspace=_I0P)
        else: # we have a transmission run
            _monInt = Integration(InputWorkspace=_I0P,RangeLower=int_min,RangeUpper=int_max)
            IvsLam = Divide(LHSWorkspace=_detector_ws,RHSWorkspace=_monInt)
            names = mtd.getObjectNames()

            IvsLam = transCorr(trans, IvsLam, lambda_min, lambda_max, background_min, background_max,
                               int_min, int_max, detector_index_ranges, i0_monitor_index, stitch_start_overlap,
                               stitch_end_overlap, stitch_params )


        IvsLam = polCorr(polcorr, IvsLam, crho, calpha, cAp, cPp)



        # Convert to I vs Q
        # check if detector in direct beam
        if theta == None or theta == 0 or theta == '':
            inst = groupGet('IvsLam','inst')
            detLocation=inst.getComponentByName(detector_component_name).getPos()
            sampleLocation=inst.getComponentByName(sample_component_name).getPos()
            detLocation=inst.getComponentByName(detector_component_name).getPos()
            source=inst.getSource()
            beamPos = sampleLocation - source.getPos()
            theta = groupGet(str(_sample_ws),'samp','theta')
            if not theta:
                theta = inst.getComponentByName(detector_component_name).getTwoTheta(sampleLocation, beamPos)*180.0/math.pi/2.0
            print "Det location: ", detLocation, "Calculated theta = ",theta
            if correct_positions:  # detector is not in correct place
                # Get detector angle theta from NeXuS
                logger.information('The detectorlocation is not at Y=0')
                print 'Nexus file theta =', theta
                IvsQ = l2q(IvsLam, detector_component_name, theta, sample_component_name)
            else:
                IvsQ = ConvertUnits(InputWorkspace=IvsLam,OutputWorkspace="IvsQ",Target="MomentumTransfer")

        else:
            if correct_positions:
                theta = float(theta)
                try:
                    IvsQ = l2q(IvsLam, detector_component_name, theta, sample_component_name)
                except AttributeError:
                    logger.warning("detector_component_name " + detector_component_name + " is unknown")
                    IvsQ = ConvertUnits(InputWorkspace=IvsLam,OutputWorkspace="IvsQ",Target="MomentumTransfer")
            else:
                IvsQ = ConvertUnits(InputWorkspace=IvsLam,OutputWorkspace="IvsQ",Target="MomentumTransfer")

    RenameWorkspace(InputWorkspace=IvsLam,OutputWorkspace=RunNumber+'_IvsLam')
    if isinstance(IvsLam, WorkspaceGroup):
        counter = 0
        for ws in IvsLam:
            RenameWorkspace(ws, OutputWorkspace=RunNumber+'_IvsLam_'+str(counter))
            counter += 1
    RenameWorkspace(InputWorkspace=IvsQ,OutputWorkspace=RunNumber+'_IvsQ')

    # delete all temporary workspaces unless in debug mode (debug=1)

    if not debug:
        cleanup()
        if mtd.doesExist('IvsLam'):
            DeleteWorkspace('IvsLam')
    return  mtd[RunNumber+'_IvsLam'], mtd[RunNumber+'_IvsQ'], theta
Example #4
0
def quick_explicit(run,
                   i0_monitor_index,
                   lambda_min,
                   lambda_max,
                   background_min,
                   background_max,
                   int_min,
                   int_max,
                   point_detector_start=0,
                   point_detector_stop=0,
                   multi_detector_start=0,
                   theta=None,
                   pointdet=True,
                   roi=[0, 0],
                   db=[0, 0],
                   trans='',
                   debug=False,
                   correction_strategy=NullCorrectionStrategy(),
                   stitch_start_overlap=None,
                   stitch_end_overlap=None,
                   stitch_params=None,
                   polcorr=False,
                   crho=None,
                   calpha=None,
                   cAp=None,
                   cPp=None,
                   detector_component_name='point-detector',
                   sample_component_name='some-surface-holder',
                   correct_positions=True):
    '''
    Version of quick where all parameters are explicitly provided.
    '''

    _sample_ws = ConvertToWavelength.to_single_workspace(run)
    nHist = _sample_ws.getNumberHistograms()
    to_lam = ConvertToWavelength(run)

    if pointdet:
        detector_index_ranges = (point_detector_start, point_detector_stop)
    else:
        detector_index_ranges = (multi_detector_start, nHist - 1)

    _monitor_ws, _detector_ws = to_lam.convert(
        wavelength_min=lambda_min,
        wavelength_max=lambda_max,
        detector_workspace_indexes=detector_index_ranges,
        monitor_workspace_index=i0_monitor_index,
        correct_monitor=True,
        bg_min=background_min,
        bg_max=background_max)

    inst = _sample_ws.getInstrument()
    # Some beamline constants from IDF

    print(i0_monitor_index)
    print(nHist)

    if run == '0':
        RunNumber = '0'
    else:
        RunNumber = groupGet(_sample_ws.name(), 'samp', 'run_number')

    if not pointdet:
        # Proccess Multi-Detector; assume MD goes to the end:
        # if roi or db are given in the function then sum over the apropriate channels
        print("This is a multidetector run.")

        _I0M = RebinToWorkspace(WorkspaceToRebin=_monitor_ws,
                                WorkspaceToMatch=_detector_ws)
        IvsLam = _detector_ws / _I0M
        if (roi != [0, 0]):
            ReflectedBeam = SumSpectra(InputWorkspace=IvsLam,
                                       StartWorkspaceIndex=roi[0],
                                       EndWorkspaceIndex=roi[1])
        if (db != [0, 0]):
            DirectBeam = SumSpectra(InputWorkspace=_detector_ws,
                                    StartWorkspaceIndex=db[0],
                                    EndWorkspaceIndex=db[1])
            ReflectedBeam = ReflectedBeam / DirectBeam
        polCorr(polcorr, IvsLam, crho, calpha, cAp, cPp)
        if theta and correct_positions:
            IvsQ = l2q(ReflectedBeam, detector_component_name, theta,
                       sample_component_name)
        else:
            IvsQ = ConvertUnits(InputWorkspace=ReflectedBeam,
                                Target="MomentumTransfer")

    # Single Detector processing-------------------------------------------------------------
    else:
        print("This is a Point-Detector run.")
        # handle transmission runs
        # process the point detector reflectivity
        _I0P = RebinToWorkspace(WorkspaceToRebin=_monitor_ws,
                                WorkspaceToMatch=_detector_ws)
        IvsLam = Scale(InputWorkspace=_detector_ws, Factor=1)

        if not trans:
            print(
                "No transmission file. Trying default exponential/polynomial correction..."
            )
            IvsLam = correction_strategy.apply(_detector_ws)
            IvsLam = Divide(LHSWorkspace=IvsLam, RHSWorkspace=_I0P)
        else:  # we have a transmission run
            _monInt = Integration(InputWorkspace=_I0P,
                                  RangeLower=int_min,
                                  RangeUpper=int_max)
            IvsLam = Divide(LHSWorkspace=_detector_ws, RHSWorkspace=_monInt)

            IvsLam = transCorr(trans, IvsLam, lambda_min, lambda_max,
                               background_min, background_max, int_min,
                               int_max, detector_index_ranges,
                               i0_monitor_index, stitch_start_overlap,
                               stitch_end_overlap, stitch_params)

        IvsLam = polCorr(polcorr, IvsLam, crho, calpha, cAp, cPp)

        # Convert to I vs Q
        # check if detector in direct beam
        if theta is None or theta == 0 or theta == '':
            inst = groupGet('IvsLam', 'inst')
            detLocation = inst.getComponentByName(
                detector_component_name).getPos()
            sampleLocation = inst.getComponentByName(
                sample_component_name).getPos()
            detLocation = inst.getComponentByName(
                detector_component_name).getPos()
            source = inst.getSource()
            beamPos = sampleLocation - source.getPos()
            theta = groupGet(str(_sample_ws), 'samp', 'theta')
            if not theta:
                theta = inst.getComponentByName(
                    detector_component_name).getTwoTheta(
                        sampleLocation, beamPos) * 180.0 / math.pi / 2.0
            print("Det location: ", detLocation, "Calculated theta = ", theta)
            if correct_positions:  # detector is not in correct place
                # Get detector angle theta from NeXuS
                logger.information('The detectorlocation is not at Y=0')
                print('Nexus file theta =', theta)
                IvsQ = l2q(IvsLam, detector_component_name, theta,
                           sample_component_name)
            else:
                IvsQ = ConvertUnits(InputWorkspace=IvsLam,
                                    OutputWorkspace="IvsQ",
                                    Target="MomentumTransfer")

        else:
            if correct_positions:
                theta = float(theta)
                try:
                    IvsQ = l2q(IvsLam, detector_component_name, theta,
                               sample_component_name)
                except AttributeError:
                    logger.warning("detector_component_name " +
                                   detector_component_name + " is unknown")
                    IvsQ = ConvertUnits(InputWorkspace=IvsLam,
                                        OutputWorkspace="IvsQ",
                                        Target="MomentumTransfer")
            else:
                IvsQ = ConvertUnits(InputWorkspace=IvsLam,
                                    OutputWorkspace="IvsQ",
                                    Target="MomentumTransfer")

    RenameWorkspace(InputWorkspace=IvsLam,
                    OutputWorkspace=RunNumber + '_IvsLam')
    if isinstance(IvsLam, WorkspaceGroup):
        counter = 0
        for ws in IvsLam:
            RenameWorkspace(ws,
                            OutputWorkspace=RunNumber + '_IvsLam_' +
                            str(counter))
            counter += 1
    RenameWorkspace(InputWorkspace=IvsQ, OutputWorkspace=RunNumber + '_IvsQ')

    # delete all temporary workspaces unless in debug mode (debug=1)

    if not debug:
        cleanup()
        if mtd.doesExist('IvsLam'):
            DeleteWorkspace('IvsLam')
    return mtd[RunNumber + '_IvsLam'], mtd[RunNumber + '_IvsQ'], theta