def _get_InstrRun(self, ws_name):
        """
        Get the instrument name and run number from a workspace.

        @param ws_name - name of the workspace
        @return tuple of form (instrument, run number)
        """

        run_number = str(mtd[ws_name].getRunNumber())
        if run_number == '0':
            # Attempt to parse run number off of name
            match = re.match(r'([a-zA-Z]+)([0-9]+)', ws_name)
            if match:
                run_number = match.group(2)
            else:
                raise RuntimeError("Could not find run number associated with workspace.")

        instrument = mtd[ws_name].getInstrument().getName()
        if instrument != '':
            for facility in config.getFacilities():
                try:
                    instrument = facility.instrument(instrument).filePrefix(int(run_number))
                    instrument = instrument.lower()
                    break
                except RuntimeError:
                    continue

        return instrument, run_number
Esempio n. 2
0
    def _get_InstrRun(self, ws_name):
        """
        Get the instrument name and run number from a workspace.

        @param ws_name - name of the workspace
        @return tuple of form (instrument, run number)
        """

        run_number = str(mtd[ws_name].getRunNumber())
        if run_number == '0':
            # Attempt to parse run number off of name
            match = re.match(r'([a-zA-Z]+)([0-9]+)', ws_name)
            if match:
                run_number = match.group(2)
            else:
                raise RuntimeError(
                    "Could not find run number associated with workspace.")

        instrument = mtd[ws_name].getInstrument().getName()
        if instrument != '':
            for facility in config.getFacilities():
                try:
                    instrument = facility.instrument(instrument).filePrefix(
                        int(run_number))
                    instrument = instrument.lower()
                    break
                except RuntimeError:
                    continue

        return instrument, run_number
    def _analyser_reflection(self, workspace):
        if workspace == '':
            return ''
        ws = mtd[workspace]
        inst = ws.getInstrument().getName()

        short_name = ''
        try:
            short_name = config.getFacility().instrument(inst).shortName().lower()
        except RuntimeError:
            for facility in config.getFacilities():
                try:
                    short_name = facility.instrument(inst).shortName().lower()
                except RuntimeError:
                    pass

        if short_name == '':
            raise RuntimeError('Cannot find instrument "%s" in any facility' % str(inst))

        run = ws.getRun().getLogData('run_number').value
        if self._multi_run:
            run += '_multi'
        try:
            analyser = ws.getInstrument().getStringParameter('analyser')[0]
            reflection = ws.getInstrument().getStringParameter('reflection')[0]
        except IndexError:
            analyser = ''
            reflection = ''
        prefix = short_name + run + '_' + analyser + reflection + '_red'
        return prefix
Esempio n. 4
0
def check_instrument_name(old_name,new_name):
    """ function checks if new instrument name is acceptable instrument name"""


    if new_name is None:
        if not old_name is None:
            return (None,None,str(config.getFacility()))
        else:
            raise KeyError("No instrument name is defined")

    if old_name == new_name:
        return

    # Instrument name might be a prefix, query Mantid for the full name
    short_name=''
    full_name=''
    try :
        instrument = config.getFacility().instrument(new_name)
        short_name = instrument.shortName()
        full_name = instrument.name()
    except RuntimeError:
        # it is possible to have wrong facility:
        facilities = config.getFacilities()
        old_facility = str(config.getFacility())
        for facility in facilities:
            config.setString('default.facility',facility.name())
            try :
                instrument = facility.instrument(new_name)
                short_name = instrument.shortName()
                full_name = instrument.name()
                if len(short_name)>0 :
                    break
            except:
                pass
        if len(short_name)==0 :
            config.setString('default.facility',old_facility)
            raise KeyError(" Can not find/set-up the instrument: "+new_name+' in any supported facility')

    new_name = short_name
    facility = str(config.getFacility())

    config['default.instrument'] = full_name
    return (new_name,full_name,facility)
Esempio n. 5
0
def check_instrument_name(old_name, new_name):
    """ function checks if new instrument name is acceptable instrument name"""

    if new_name is None:
        if not (old_name is None):
            return (None, None, str(config.getFacility()))
        else:
            raise KeyError("No instrument name is defined")

    if old_name == new_name:
        return

    # Instrument name might be a prefix, query Mantid for the full name
    short_name = ''
    full_name = ''
    try:
        instrument = config.getFacility().instrument(new_name)
        short_name = instrument.shortName()
        full_name = instrument.name()
    except RuntimeError:
        # it is possible to have wrong facility:
        facilities = config.getFacilities()
        old_facility = str(config.getFacility())
        for facility in facilities:
            config.setString('default.facility', facility.name())
            try:
                instrument = facility.instrument(new_name)
                short_name = instrument.shortName()
                full_name = instrument.name()
                if len(short_name) > 0:
                    break
            except:
                pass
        if len(short_name) == 0:
            config.setString('default.facility', old_facility)
            raise KeyError(" Can not find/set-up the instrument: " + new_name +
                           ' in any supported facility')

    new_name = short_name
    facility = str(config.getFacility())

    config['default.instrument'] = full_name
    return (new_name, full_name, facility)
Esempio n. 6
0
def getInstrRun(ws_name):
    """
    Get the instrument name and run number from a workspace.

    @param ws_name - name of the workspace
    @return tuple of form (instrument, run number)
    """

    run_number = get_run_number(ws_name)

    instrument = s_api.mtd[ws_name].getInstrument().getName()
    if instrument != '':
        for facility in config.getFacilities():
            try:
                instrument = facility.instrument(instrument).filePrefix(int(run_number))
                instrument = instrument.lower()
                break
            except RuntimeError:
                continue

    return instrument, run_number
Esempio n. 7
0
def rename_reduction(workspace_name, multiple_files):
    """
    Renames a worksapce according to the naming policy in the Workflow.NamingConvention parameter.

    @param workspace_name Name of workspace
    @param multiple_files Insert the multiple file marker
    @return New name of workspace
    """
    from mantid.simpleapi import RenameWorkspace
    import string

    is_multi_frame = isinstance(mtd[workspace_name], WorkspaceGroup)

    # Get the instrument
    if is_multi_frame:
        instrument = mtd[workspace_name].getItem(0).getInstrument()
    else:
        instrument = mtd[workspace_name].getInstrument()

    # Get the naming convention parameter form the parameter file
    try:
        convention = instrument.getStringParameter('Workflow.NamingConvention')[0]
    except IndexError:
        # Defualt to run title if naming convention parameter not set
        convention = 'RunTitle'
    logger.information('Naming convention for workspace %s is %s' % (workspace_name, convention))

    # Get run number
    if is_multi_frame:
        run_number = mtd[workspace_name].getItem(0).getRun()['run_number'].value
    else:
        run_number = mtd[workspace_name].getRun()['run_number'].value
    logger.information('Run number for workspace %s is %s' % (workspace_name, run_number))

    inst_name = instrument.getName()
    for facility in config.getFacilities():
        try:
            short_inst_name = facility.instrument(inst_name).shortName()
            break
        except _:
            pass
    logger.information('Short name for instrument %s is %s' % (inst_name, short_inst_name))

    # Get run title
    if is_multi_frame:
        run_title = mtd[workspace_name].getItem(0).getRun()['run_title'].value.strip()
    else:
        run_title = mtd[workspace_name].getRun()['run_title'].value.strip()
    logger.information('Run title for workspace %s is %s' % (workspace_name, run_title))

    if multiple_files:
        multi_run_marker = '_multi'
    else:
        multi_run_marker = ''

    if convention == 'None':
        new_name = workspace_name

    elif convention == 'RunTitle':
        valid = "-_.() %s%s" % (string.ascii_letters, string.digits)
        formatted_title = ''.join([c for c in run_title if c in valid])
        new_name = '%s%s%s-%s' % (short_inst_name.lower(), run_number, multi_run_marker, formatted_title)

    elif convention == 'AnalyserReflection':
        analyser = instrument.getStringParameter('analyser')[0]
        reflection = instrument.getStringParameter('reflection')[0]
        new_name = '%s%s%s_%s%s_red' % (short_inst_name.upper(), run_number, multi_run_marker,
                                        analyser, reflection)

    else:
        raise RuntimeError('No valid naming convention for workspace %s' % workspace_name)

    logger.information('New name for %s workspace: %s' % (workspace_name, new_name))

    RenameWorkspace(InputWorkspace=workspace_name,
                    OutputWorkspace=new_name)

    return new_name