import re


def axes_from_cmd(cmd, alt_names=None):
    """
    Get axes name from command string
    :param cmd: str
    :param alt_names: dict {name_in_cmd: name_in_file}
    :return: str
    """
    alt_names = {} if alt_names is None else alt_names
    cmd = cmd.split()
    axes = cmd[1]
    if axes in alt_names:
        axes = alt_names[axes]
    # These are specific to I16...
    if axes == 'hkl':
        if cmd[0] == 'scan':
            hstep, kstep, lstep = cmd[8:11]
        elif cmd[0] == 'scancn':
            hstep, kstep, lstep = cmd[2:5]
        else:
            raise Warning('Warning unknown type of hkl scan')

        if float(re.sub("[^0-9.]", "", hstep)) > 0.0:
            axes = 'h'
        elif float(re.sub("[^0-9.]", "", kstep)) > 0.0:
            axes = 'k'
        else:
            axes = 'l'
    elif axes == 'energy':
        axes = 'energy2'
    elif axes == 'sr2':
        axes = 'azimuthal'  # 'phi' in pre-DiffCalc scans
    elif axes == 'th2th':
        axes = 'delta'
    elif axes == 'ppp_energy':
        axes = 'ppp_offset'
    return axes