Can you change workspace labels in Python?

Name: Arthur Wilcke

I have been using your precious advice and getting everything I wanted to done, however I have another question regarding Workspace creation. I have been wanting to create workspaces with units, and the only way I have found of doing this is using CreateWorkspace to create an empty workspace with the correct units, and using this as first argument of WorkspaceFactory.create. This works, but is however quite inelegant… Is there a way to set the units in the workspace directly?

Thanks in advance,

Arthur.

Yes you can,

This should help – let me know if you have any questions

def printAxes(ws):
    '''print out the current axes'''
    for i in range(ws.axes()):
        ax = ws.getAxis(i)
        axUnit = ax.getUnit()
        print i, axUnit.caption(), axUnit.symbol()
    yUnit = ws.YUnit()
    print "Y axis:", ws.YUnit(), ws.YUnitLabel()

dataX = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
dataY = [1,2,3,4,5,6,7,8,9,10,11,12]
dataE = [1,2,3,4,5,6,7,8,9,10,11,12]

# The workspace will be named "dataWS"
dataWS = CreateWorkspace(DataX=dataX, DataY=dataY, DataE=dataE, NSpec=4,UnitX="Wavelength")
# or get an existing workspace from Mantid
#dataWS = mtd["Name of workspace"]

print "Before"
printAxes(dataWS)

#Setting the y unit to counts
dataWS.setYUnit("neutron")
dataWS.setYUnitLabel("counts")

# changing the X unit from wavelength to Time of Flight
# Note this just changes the labels, to change the values as well use the ConvertUnits Algorithm
dataWS.getAxis(0).setUnit("Label").setLabel("Time", "ns")

#Also change the "spectrum Axis" 
dataWS.getAxis(1).setUnit("Label").setLabel("Temperature", "K")

print
print "After"
printAxes(dataWS)