Ejemplo n.º 1
0
    def setup_method(self):
        names = 'alpha', 'delta', 'gamma', 'omega', 'chi', 'phi'
        self.hardware = DummyHardwareAdapter(names)
        settings.hardware = self.hardware
        self.conv = CoordinateConverter(transform=self.t_matrix)
        self._refineub_matrix = matrix('0.70711   0.70711   0.00000; -0.70711   0.70711   0.00000; 0.00000   0.00000   1.00000')

        from diffcalc.ub import ub
        reload(ub)
        self.ub = ub
        #self.ub.ubcalc = ub.ubcalc
        prepareRawInput([])
        diffcalc.util.RAISE_EXCEPTIONS_FOR_ALL_ERRORS = True
Ejemplo n.º 2
0
 def testVectorFail(self):
     failvec = matrix('0;0;1;0')
     failmarix = matrix('0 0 1; 0 0 1; 1 0 0; 0 0 1')
     with pytest.raises(TypeError):
         CoordinateConverter(transform=failmarix)
     with pytest.raises(TypeError):
         self.conv.transform(failvec)
     with pytest.raises(TypeError):
         self.conv.transform(failmarix)
Ejemplo n.º 3
0
    def __init__(self,
                 persister,
                 strategy,
                 include_sigtau=True,
                 include_reference=True):

        # The diffractometer geometry is required to map the internal angles
        # into those used by this diffractometer (for display only)

        self._persister = persister
        self._strategy = strategy
        self.include_sigtau = include_sigtau
        self.include_reference = include_reference
        try:
            self._tobj = CoordinateConverter(
                transform=settings.geometry.beamline_axes_transform)
        except AttributeError:
            self._tobj = CoordinateConverter(transform=None)
        self._clear()
Ejemplo n.º 4
0
 def setup_method(self):
     self.conv = CoordinateConverter(
         transform=matrix('0 0 1; 1 0 0; 0 1 0'))
Ejemplo n.º 5
0
class TestCoordinateConverter(object):
    def setup_method(self):
        self.conv = CoordinateConverter(
            transform=matrix('0 0 1; 1 0 0; 0 1 0'))

    def testVector(self):
        vec100 = matrix('1;0;0')
        vec010 = matrix('0;1;0')
        vec001 = matrix('0;0;1')

        conv100 = self.conv.transform(vec100)
        conv010 = self.conv.transform(vec010)
        conv001 = self.conv.transform(vec001)

        inv100 = self.conv.transform(conv100, True)
        inv010 = self.conv.transform(conv010, True)
        inv001 = self.conv.transform(conv001, True)

        eq_(conv100.tolist(), vec010.tolist())
        eq_(conv010.tolist(), vec001.tolist())
        eq_(conv001.tolist(), vec100.tolist())
        eq_(vec100.tolist(), inv100.tolist())
        eq_(vec010.tolist(), inv010.tolist())
        eq_(vec001.tolist(), inv001.tolist())

    def testVector1m10(self):
        vec110 = matrix(' 1; -1;  0')
        vec011 = matrix(' 0;  1; -1')
        vec101 = matrix('-1;  0;  1')

        conv110 = self.conv.transform(vec110)
        conv011 = self.conv.transform(vec011)
        conv101 = self.conv.transform(vec101)

        inv110 = self.conv.transform(conv110, True)
        inv011 = self.conv.transform(conv011, True)
        inv101 = self.conv.transform(conv101, True)

        eq_(conv110.tolist(), vec011.tolist())
        eq_(conv011.tolist(), vec101.tolist())
        eq_(conv101.tolist(), vec110.tolist())
        eq_(vec110.tolist(), inv110.tolist())
        eq_(vec011.tolist(), inv011.tolist())
        eq_(vec101.tolist(), inv101.tolist())

    def testMatrix(self):
        m = matrix('0 -1 0; 1 0 0; 0 0 -1')
        tm = self.conv.transform(m)
        im = self.conv.transform(tm, True)

        eq_(m.tolist(), im.tolist())

        vec110 = matrix(' 1; -1;  0')
        vec011 = matrix(' 0;  1; -1')
        vec101 = matrix('-1;  0;  1')
        conv110 = self.conv.transform(vec110)
        conv011 = self.conv.transform(vec011)
        conv101 = self.conv.transform(vec101)

        m110 = m * vec110
        m011 = m * vec011
        m101 = m * vec101
        cm110 = tm * conv110
        cm011 = tm * conv011
        cm101 = tm * conv101
        inv110 = self.conv.transform(cm110, True)
        inv011 = self.conv.transform(cm011, True)
        inv101 = self.conv.transform(cm101, True)

        eq_(cm110.tolist(), self.conv.transform(m110).tolist())
        eq_(cm011.tolist(), self.conv.transform(m011).tolist())
        eq_(cm101.tolist(), self.conv.transform(m101).tolist())
        eq_(m110.tolist(), inv110.tolist())
        eq_(m011.tolist(), inv011.tolist())
        eq_(m101.tolist(), inv101.tolist())

    def testVectorFail(self):
        failvec = matrix('0;0;1;0')
        failmarix = matrix('0 0 1; 0 0 1; 1 0 0; 0 0 1')
        with pytest.raises(TypeError):
            CoordinateConverter(transform=failmarix)
        with pytest.raises(TypeError):
            self.conv.transform(failvec)
        with pytest.raises(TypeError):
            self.conv.transform(failmarix)
Ejemplo n.º 6
0
class UBCalculation:
    """A UB matrix calculation for an experiment.

    Contains the parameters for the _crystal under test, a list of measured
    reflections and, if its been calculated, a UB matrix to be used by the rest
    of the code.
    """
    def __init__(self,
                 persister,
                 strategy,
                 include_sigtau=True,
                 include_reference=True):

        # The diffractometer geometry is required to map the internal angles
        # into those used by this diffractometer (for display only)

        self._persister = persister
        self._strategy = strategy
        self.include_sigtau = include_sigtau
        self.include_reference = include_reference
        try:
            self._tobj = CoordinateConverter(
                transform=settings.geometry.beamline_axes_transform)
        except AttributeError:
            self._tobj = CoordinateConverter(transform=None)
        self._clear()

    def _get_diffractometer_axes_names(self):
        return settings.hardware.get_axes_names()

    def _clear(self, name=None):
        # NOTE the Diffraction calculator is expecting this object to exist in
        # the long run. We can't remove this entire object, and recreate it.
        # It also contains a required link to the angle calculator.
        reflist = ReflectionList(
            settings.geometry,
            self._get_diffractometer_axes_names(),
            multiplier=settings.hardware.energyScannableMultiplierToGetKeV)
        orientlist = OrientationList(settings.geometry,
                                     self._get_diffractometer_axes_names())
        reference = YouReference(self._get_UB)
        reference._set_n_hkl_configured(settings.reference_vector)
        surface = YouReference(self._get_UB)
        surface._set_n_phi_configured(settings.surface_vector)
        self._state = UBCalcState(name=name,
                                  reflist=reflist,
                                  orientlist=orientlist,
                                  reference=reference,
                                  surface=surface)
        self._U = None
        self._UB = None
        self._state.configure_calc_type()

### State ###

    def start_new(self, name):
        """Start new UB matrix calculation.
        
        Creates a new blank UB matrix calculation.
        
        Parameters
        ----------
        name: str
            Name of a new UB matrix calculation
        """
        # Create storage object if name does not exist (TODO)
        self._clear(name)
        self.save()

    def load(self, name):
        state = self._persister.load(name)
        if isinstance(self._persister, UBCalculationJSONPersister):
            self._state = self._persister.encoder.decode_ubcalcstate(
                state, settings.geometry,
                self._get_diffractometer_axes_names(),
                settings.hardware.energyScannableMultiplierToGetKeV)
            self._state.reference.get_UB = self._get_UB
            self._state.surface.get_UB = self._get_UB
        elif isinstance(self._persister, UBCalculationPersister):
            self._state = state
        else:
            raise DiffcalcException('Unexpected persister type: ' +
                                    str(self._persister))
        if self._state.manual_U is not None:
            self._U = self._state.manual_U
            self._UB = self._U * self._state.crystal.B
            self.save()
        elif self._state.manual_UB is not None:
            self._UB = self._state.manual_UB
            self.save()
        elif self._state.is_okay_to_autocalculate_ub:
            try:
                self.calculate_UB(self._state.or0, self._state.or1)
            except Exception as e:
                print(e)
        else:
            print("Warning: No UB calculation loaded.")

    def save(self):
        """Save current UB matrix calculation."""
        if self._state.name:
            self.saveas(self._state.name)
        else:
            print("Warning: No UB calculation defined.")

    def saveas(self, name):
        """Save current UB matrix calculation.
        
        Parameters
        ----------
        name : str
            Name for the saved UB matrix calculation.
        """
        self._state.name = name
        self._persister.save(self._state, name)

    def listub(self):
        """List saved UB matrix calculations.
        
        Returns:
        :obj:`list` of :obj:`str`
            List of names of the saved UB matrix calculations.
        """
        return self._persister.list()

    def listub_metadata(self):
        return self._persister.list_metadata()

    def remove(self, name):
        """Delete UB matrix calculation.
        
        Parameters
        ----------
        name : str
            Name of the UB matrix calculation to be deleted.
        """
        self._persister.remove(name)
        if self._state.name == name:
            self._clear()

    def getState(self):
        return self._state.getState()

    def __str__(self):

        if self._state.name is None:
            return "<<< No UB calculation started >>>"
        lines = []
        lines.append(bold("UBCALC"))
        lines.append("")
        lines.append("   name:".ljust(WIDTH) + self._state.name.rjust(9))

        if self.include_sigtau:
            lines.append("")
            lines.append("   sigma:".ljust(WIDTH) +
                         ("% 9.5f" % self._state.sigma).rjust(9))
            lines.append("   tau:".ljust(WIDTH) +
                         ("% 9.5f" % self._state.tau).rjust(9))

        _ub_calculated = self._UB is not None
        if self.include_reference:
            lines.append("")
            lines.append(bold("REFERNCE"))
            lines.append("")
            lines.extend(
                self._state.reference.repr_lines(_ub_calculated, WIDTH,
                                                 self._tobj))
        lines.append("")
        lines.append(bold("SURFACE NORMAL"))
        lines.append("")
        lines.extend(
            self._state.surface.repr_lines(_ub_calculated, WIDTH, self._tobj))

        lines.append("")
        lines.append(bold("CRYSTAL"))
        lines.append("")

        if self._state.crystal is None:
            lines.append("   <<< none specified >>>")
        else:
            lines.extend(self._state.crystal.str_lines(self._tobj))

        lines.append("")
        lines.append(bold("UB MATRIX"))
        lines.append("")

        if self._UB is None:
            lines.append("   <<< none calculated >>>")
        else:
            lines.extend(self.str_lines_u(self.U))
            lines.append("")
            lines.extend(self.str_lines_ub_angle_and_axis(self.UB))
            lines.append("")
            lines.extend(self.str_lines_ub(self.UB))

        lines.extend(self.str_lines_refl())

        lines.extend(self.str_lines_orient())

        return '\n'.join(lines)

    def str_lines_u(self, umatrix):
        lines = []
        fmt = "% 9.5f % 9.5f % 9.5f"
        U = self._tobj.transform(umatrix, True)
        lines.append("   U matrix:".ljust(WIDTH) + fmt %
                     (z(U[0, 0]), z(U[0, 1]), z(U[0, 2])))
        lines.append(' ' * WIDTH + fmt % (z(U[1, 0]), z(U[1, 1]), z(U[1, 2])))
        lines.append(' ' * WIDTH + fmt % (z(U[2, 0]), z(U[2, 1]), z(U[2, 2])))
        return lines

    def str_lines_ub_angle_and_axis(self, ubmatrix):
        lines = []
        fmt = "% 9.5f % 9.5f % 9.5f"
        rotation_angle, rotation_axis = self.get_miscut_angle_axis(ubmatrix)
        if abs(norm(rotation_axis)) < SMALL:
            lines.append("   miscut angle:".ljust(WIDTH) + "  0")
        else:
            lines.append("   miscut:")
            lines.append("      angle:".ljust(WIDTH) +
                         "% 9.5f" % rotation_angle)
            lines.append("       axis:".ljust(WIDTH) +
                         fmt % tuple((rotation_axis.T).tolist()[0]))

        return lines

    def str_lines_ub(self, ubmatrix):
        lines = []
        fmt = "% 9.5f % 9.5f % 9.5f"
        if self._tobj.R is not None:
            UB = self._tobj.R.I * ubmatrix
        else:
            UB = self.UB
        lines.append("   UB matrix:".ljust(WIDTH) + fmt %
                     (z(UB[0, 0]), z(UB[0, 1]), z(UB[0, 2])))
        lines.append(' ' * WIDTH + fmt %
                     (z(UB[1, 0]), z(UB[1, 1]), z(UB[1, 2])))
        lines.append(' ' * WIDTH + fmt %
                     (z(UB[2, 0]), z(UB[2, 1]), z(UB[2, 2])))
        return lines

    def str_lines_refl(self):
        lines = ["", bold("REFLECTIONS"), ""]

        lines.extend(self._state.reflist.str_lines())
        return lines

    def str_lines_orient(self):
        lines = ["", bold("CRYSTAL ORIENTATIONS"), ""]

        lines.extend(self._state.orientlist.str_lines(self._tobj))
        return lines

    @property
    def name(self):
        """str: Name of the current UB matrix calculation."""
        return self._state.name
### Lattice ###

    def set_lattice(self, name, *shortform):
        """
        Converts a list shortform crystal parameter specification to a six-long
        tuple. See setlat() for a description of the shortforms supported.
        """
        if not shortform:
            raise TypeError("Please specify unit cell parameters.")
        elif allnum(shortform):
            sf = shortform
            if len(sf) == 1:
                system = "Cubic"
            elif len(sf) == 2:
                system = "Tetragonal"
            elif len(sf) == 3:
                system = "Orthorhombic"
            elif len(sf) == 4:
                if abs(sf[0] - sf[1]) < SMALL and sf[3] == 120:
                    system = "Hexagonal"
                else:
                    system = "Monoclinic"
            elif len(sf) == 6:
                system = "Triclinic"
            else:
                raise TypeError(
                    "Invalid number of input parameters to set unit lattice.")
            fullform = (system, ) + shortform
        else:
            if not isinstance(shortform[0], str):
                raise TypeError("Invalid unit cell parameters specified.")
            fullform = shortform
        if self._state.name is None:
            raise DiffcalcException(
                "Cannot set lattice until a UBCalcaluation has been started "
                "with newubcalc")
        self._state.crystal = CrystalUnderTest(name, *fullform)
        # Clear U and UB if these exist
        if self._U is not None:  # (UB will also exist)
            print("Warning: Setting new unit cell parameters.\n"
                  "         The old UB calculation has been cleared.")
        self.save()

### Surface normal stuff ###

    def _gettau(self):
        """
        Returns tau (in degrees): the (minus) ammount of phi axis rotation ,
        that together with some chi axis rotation (minus sigma) brings the
        optical surface normal parallel to the omega axis.
        """
        return self._state.tau

    def _settau(self, tau):
        self._state.tau = tau
        self.save()

    tau = property(_gettau, _settau)

    def _getsigma(self):
        """
        Returns sigma (in degrees): the (minus) ammount of phi axis rotation ,
        that together with some phi axis rotation (minus tau) brings the
        optical surface normal parallel to the omega axis.
        """
        return self._state.sigma

    def _setsigma(self, sigma):
        self.state._sigma = sigma
        self.save()

    sigma = property(_getsigma, _setsigma)

    ### Reference vector ###

    def _get_n_hkl(self):
        return self._state.reference.n_hkl

    def _get_n_phi(self):
        return self._state.reference.n_phi

    n_hkl = property(_get_n_hkl)
    n_phi = property(_get_n_phi)

    def set_n_phi_configured(self, n_phi):
        self._state.reference.n_phi_configured = self._tobj.transform(n_phi)
        self.save()

    def set_n_hkl_configured(self, n_hkl):
        self._state.reference.n_hkl_configured = n_hkl
        self.save()

    def print_reference(self):
        print('\n'.join(
            self._state.reference.repr_lines(self.is_ub_calculated(),
                                             WIDTH=9,
                                             conv=self._tobj)))

### Surface vector ###

    def _get_surf_nhkl(self):
        return self._state.surface.n_hkl

    def _get_surf_nphi(self):
        return self._state.surface.n_phi

    surf_nhkl = property(_get_surf_nhkl)
    surf_nphi = property(_get_surf_nphi)

    def set_surf_nphi_configured(self, n_phi):
        self._state.surface.n_phi_configured = self._tobj.transform(n_phi)
        self.save()

    def set_surf_nhkl_configured(self, n_hkl):
        self._state.surface.n_hkl_configured = n_hkl
        self.save()

    def print_surface(self):
        print('\n'.join(
            self._state.surface.repr_lines(self.is_ub_calculated(),
                                           WIDTH=9,
                                           conv=self._tobj)))

### Reflections ###

    def add_reflection(self, h, k, l, position, energy, tag, time):
        """Add a reference reflection.
        
        Adds a reflection position in degrees and in the
        systems internal representation.
        
        Parameters
        ----------
        h : number
            h index of the reflection
        k : number
            k index of the reflection
        l : number
            l index of the reflection
        position: :obj:`list` or :obj:`tuple` of numbers
            list of diffractometer angles in internal representation in degrees
        energy : float
            energy of the x-ray beam
        tag : str
            identifying tag for the reflection
        time : :obj:`datetime`
            datetime object
        """
        if self._state.reflist is None:
            raise DiffcalcException("No UBCalculation loaded")
        self._state.reflist.add_reflection(h, k, l, position, energy, tag,
                                           time)
        self.save()  # incase autocalculateUbAndReport fails

        # If second reflection has just been added then calculateUB
        if self.get_reference_count() == 2:
            self._autocalculateUbAndReport()
        self.save()

    def edit_reflection(self, idx, h, k, l, position, energy, tag, time):
        """Changes a reference reflection.
        
        Changes a reflection position in degrees and in the
        systems internal representation.
        
        Parameters
        ----------
        idx : str or int
            index or tag of the reflection to be changed
        h : number
            h index of the reflection
        k : number
            k index of the reflection
        l : number
            l index of the reflection
        position: :obj:`list` or :obj:`tuple` of numbers
            list of diffractometer angles in internal representation in degrees
        energy : float
            energy of the x-ray beam
        tag : str
            identifying tag for the reflection
        time : :obj:`datetime`
            datetime object
        """
        if self._state.reflist is None:
            raise DiffcalcException("No UBCalculation loaded")
        num = self.get_tag_refl_num(idx)
        self._state.reflist.edit_reflection(num, h, k, l, position, energy,
                                            tag, time)

        # If first or second reflection has been changed and there are at least
        # two reflections then recalculate  UB
        or12 = self.get_ub_references()
        if idx in or12 or num in or12:
            self._autocalculateUbAndReport()
        self.save()

    def get_reflection(self, idx):
        """Get a reference reflection.
        
        Get a reflection position in degrees and in the
        systems internal representation.
        
        Parameters
        ----------
        idx : str or int
            index or tag of the reflection
        """
        return self._state.reflist.getReflection(idx)

    def get_reflection_in_external_angles(self, idx):
        """Get a reference reflection.
        
        Get a reflection position in degrees and in the
        external diffractometer representation.
        
        Parameters
        ----------
        idx : str or int
            index or tag of the reflection
        """
        return self._state.reflist.get_reflection_in_external_angles(idx)

    def get_number_reflections(self):
        """Get a number of stored reference reflections.
        
        Returns
        -------
        int:
            Number of reference reflections.
        """
        try:
            return len(self._state.reflist)
        except TypeError:
            return 0

    def get_tag_refl_num(self, tag):
        """Get a reference reflection index.
        
        Get a reference reflection index for the 
        provided reflection tag.
        
        Parameters
        ----------
        tag : str
            identifying tag for the reflection
        Returns
        -------
        int:
            Reference reflection index
        """
        if tag is None:
            raise IndexError("Reflection tag is None")
        return self._state.reflist.get_tag_index(tag) + 1

    def del_reflection(self, idx):
        """Delete a reference reflection.
        
        Parameters
        ----------
        idx : str or int
            index or tag of the deleted reflection
        """
        num = self.get_tag_refl_num(idx)
        self._state.reflist.removeReflection(num)
        or12 = self.get_ub_references()
        if ((idx in or12 or num in or12) and (self._U is not None)):
            self._autocalculateUbAndReport()
        self.save()

    def swap_reflections(self, idx1, idx2):
        """Swap indices of two reference reflections.
        
        Parameters
        ----------
        idx1 : str or int
            index or tag of the first reflection to be swapped
        idx2 : str or int
            index or tag of the second reflection to be swapped
        """
        num1 = self.get_tag_refl_num(idx1)
        num2 = self.get_tag_refl_num(idx2)
        self._state.reflist.swap_reflections(num1, num2)
        or12 = self.get_ub_references()
        if ((idx1 in or12 or idx2 in or12 or num1 in or12 or num2 in or12)
                and (self._U is not None)):
            self._autocalculateUbAndReport()
        self.save()

    def _autocalculateUbAndReport(self):
        if self.get_reference_count() < 2:
            pass
        elif self._state.crystal is None:
            print("Not calculating UB matrix as no lattice parameters have "
                  "been specified.")
        elif not self._state.is_okay_to_autocalculate_ub:
            print("Not calculating UB matrix as it has been manually set. "
                  "Use 'calcub' to explicitly recalculate it.")
        else:  # okay to autocalculate
            if self._UB is None:
                print("Calculating UB matrix.")
            else:
                print("Recalculating UB matrix.")
            or12 = self.get_ub_references()
            self.calculate_UB(*or12)

### Orientations ###

    def add_orientation(self, h, k, l, x, y, z, position, tag, time):
        """Add a reference orientation.
        
        Adds a reference orientation in the external diffractometer
        coordinate system.
        
        Parameters
        ----------
        h : number
            h index of the reference orientation
        k : number
            k index of the reference orientation
        l : number
            l index of the reference orientation
        x : number
            x coordinate of the reference orientation
        y : number
            x coordinate of the reference orientation
        z : number
            x coordinate of the reference orientation
        position: :obj:`list` or :obj:`tuple` of numbers
            list of diffractometer angles in internal representation in degrees
        tag : str
            identifying tag for the reflection
        time : :obj:`datetime`
            datetime object
        """
        if self._state.orientlist is None:
            raise DiffcalcException("No UBCalculation loaded")
        xyz_tr = self._tobj.transform(matrix([[x], [y], [z]]))
        xr, yr, zr = xyz_tr.T.tolist()[0]
        self._state.orientlist.add_orientation(h, k, l, xr, yr, zr, position,
                                               tag, time)
        self.save()  # in case autocalculateUbAndReport fails

        # If second reflection has just been added then calculateUB
        if self.get_reference_count() == 2:
            self._autocalculateUbAndReport()
        self.save()

    def edit_orientation(self, idx, h, k, l, x, y, z, position, tag, time):
        """Change a reference orientation.
        
        Changes a reference orientation in the external diffractometer
        coordinate system.
        
        Parameters
        ----------
        idx : str or int
            index or tag of the orientation to be changed
        h : number
            h index of the reference orientation
        k : number
            k index of the reference orientation
        l : number
            l index of the reference orientation
        x : number
            x coordinate of the reference orientation
        y : number
            x coordinate of the reference orientation
        z : number
            x coordinate of the reference orientation
        position: :obj:`list` or :obj:`tuple` of numbers
            list of diffractometer angles in internal representation in degrees
        tag : str
            identifying tag for the reflection
        time : :obj:`datetime`
            datetime object
        """
        if self._state.orientlist is None:
            raise DiffcalcException("No UBCalculation loaded")
        num = self.get_tag_orient_num(idx)
        xyz_tr = self._tobj.transform(matrix([[x], [y], [z]]))
        xr, yr, zr = xyz_tr.T.tolist()[0]
        self._state.orientlist.edit_orientation(num, h, k, l, xr, yr, zr,
                                                position, tag, time)

        # If first or second orientation has been changed and there are
        # two orientations then recalculate  UB
        or12 = self.get_ub_references()
        if idx in or12 or num in or12:
            self._autocalculateUbAndReport()
        self.save()

    def get_orientation(self, idx, conv=False):
        """Get a reference orientation.
        
        Get a reference orientation in the external diffractometer
        coordinate system.
        
        Parameters
        ----------
        idx : str or int
            index or tag of the reference orientation
        """
        hkl, xyz, pos, tg, tm = self._state.orientlist.getOrientation(idx)
        if conv:
            xyz_rot = self._tobj.transform(
                matrix([[xyz[0]], [xyz[1]], [xyz[2]]]), True)
            xyz = xyz_rot.T.tolist()[0]
        return hkl, xyz, pos, tg, tm

    def get_orientation_in_external_angles(self, idx, conv=False):
        """Get a reference orientation.
        
        Get a reference orientation in the external diffractometer
        coordinate system with diffractometer position in external angles.
        
        Parameters
        ----------
        idx : str or int
            index or tag of the reference orientation
        """
        hkl, xyz, pos, tg, tm = self._state.orientlist.get_orientation_in_external_angles(
            idx)
        if conv:
            xyz_rot = self._tobj.transform(
                matrix([[xyz[0]], [xyz[1]], [xyz[2]]]), True)
            xyz = xyz_rot.T.tolist()[0]
        return hkl, xyz, pos, tg, tm

    def get_number_orientations(self):
        """Get a number of stored reference orientations.
        
        Returns
        -------
        int:
            Number of reference orientations.
        """
        try:
            return len(self._state.orientlist)
        except TypeError:
            return 0

    def get_tag_orient_num(self, tag):
        """Get a reference orientation index.
        
        Get a reference orientation index for the 
        provided orientation tag.
        
        Parameters
        ----------
        tag : str
            identifying tag for the orientation
        Returns
        -------
        int:
            Reference orientation index
        """
        if tag is None:
            raise IndexError("Orientations tag is None")
        return self._state.orientlist.get_tag_index(tag) + 1

    def del_orientation(self, idx):
        """Delete a reference orientation.
        
        Parameters
        ----------
        idx : str or int
            index or tag of the deleted orientation
        """
        orientationNumber = self.get_tag_orient_num(idx)
        self._state.orientlist.removeOrientation(orientationNumber)
        if ((orientationNumber == 2) and (self._U is not None)):
            self._autocalculateUbAndReport()
        self.save()

    def swap_orientations(self, idx1, idx2):
        """Swap indices of two reference orientations.
        
        Parameters
        ----------
        idx1 : str or int
            index or tag of the first orientation to be swapped
        idx2 : str or int
            index or tag of the second orientation to be swapped
        """
        num1 = self.get_tag_orient_num(idx1)
        num2 = self.get_tag_orient_num(idx2)
        self._state.orientlist.swap_orientations(num1, num2)
        if ((num1 == 1 or num1 == 2 or num2 == 1 or num2 == 2)
                and (self._U is not None)):
            self._autocalculateUbAndReport()
        self.save()

    def get_ub_references(self):
        return (self._state.or0, self._state.or1)

    def get_reference_count(self):
        n_ref = self.get_number_reflections()
        n_orient = self.get_number_orientations()
        return n_ref + n_orient
#    @property
#    def reflist(self):
#        return self._state.reflist
### Calculations ###

    def set_U_manually(self, m, conv=True):
        """Manually sets U. matrix must be 3*3 Jama or python matrix.
        Turns off aution UB calcualtion."""

        # Check matrix is a 3*3 Jama matrix
        if m.__class__ != matrix:
            m = matrix(m)  # assume its a python matrix
        if m.shape[0] != 3 or m.shape[1] != 3:
            raise ValueError("Expects 3*3 matrix")

        if self._UB is None:
            print("Calculating UB matrix.")
        else:
            print("Recalculating UB matrix.")

        if conv:
            self._U = self._tobj.transform(m)
        else:
            self._U = m
        self._state.configure_calc_type(manual_U=self._U)
        if self._state.crystal is None:
            raise DiffcalcException(
                "A crystal must be specified before manually setting U")
        self._UB = self._U * self._state.crystal.B
        self.save()

    def set_UB_manually(self, m):
        """Manually sets UB. matrix must be 3*3 Jama or python matrix.
        Turns off aution UB calcualtion."""

        # Check matrix is a 3*3 Jama matrix
        if m.__class__ != matrix:
            m = matrix(m)  # assume its a python matrix
        if m.shape[0] != 3 or m.shape[1] != 3:
            raise ValueError("Expects 3*3 matrix")

        if self._tobj.R is not None:
            self._UB = self._tobj.R * m
        else:
            self._UB = m
        self._state.configure_calc_type(manual_UB=self._UB)
        self.save()

    @property
    def U(self):
        """matrix: Returns U matrix."""
        if self._U is None:
            raise DiffcalcException(
                "No U matrix has been calculated during this ub calculation")
        return self._U

    @property
    def UB(self):
        """matrix: Returns UB matrix."""
        return self._get_UB()

    def is_ub_calculated(self):
        return self._UB is not None

    def _get_UB(self):
        if not self.is_ub_calculated():
            raise DiffcalcException(
                "No UB matrix has been calculated during this ub calculation")
        else:
            return self._UB

    def _calc_UB(self, h1, h2, u1p, u2p):
        B = self._state.crystal.B
        h1c = B * h1
        h2c = B * h2

        # Create modified unit vectors t1, t2 and t3 in crystal and phi systems
        t1c = h1c
        t3c = cross3(h1c, h2c)
        t2c = cross3(t3c, t1c)

        t1p = u1p  # FIXED from h1c 9July08
        t3p = cross3(u1p, u2p)
        t2p = cross3(t3p, t1p)

        # ...and nornmalise and check that the reflections used are appropriate
        SMALL = 1e-4  # Taken from Vlieg's code

        def normalise(m):
            d = norm(m)
            if d < SMALL:
                raise DiffcalcException(
                    "Invalid UB reference data. Please check that the specified "
                    "reference reflections/orientations are not parallel.")
            return m / d

        t1c = normalise(t1c)
        t2c = normalise(t2c)
        t3c = normalise(t3c)

        t1p = normalise(t1p)
        t2p = normalise(t2p)
        t3p = normalise(t3p)

        Tc = hstack([t1c, t2c, t3c])
        Tp = hstack([t1p, t2p, t3p])
        self._U = Tp * Tc.I
        self._UB = self._U * B

    def calculate_UB(self, idx1=None, idx2=None):
        """Calculate UB matrix.
        
        Calculate UB matrix using two reference reflections and/or
        reference orientations.
        
        By default use the first two reference reflections when provided.
        If one or both reflections are not available use one or two reference
        orientations to complement mission reflection data.
        
        Parameters
        ----------
        idx1: int or str, optional
            The index or the tag of the first reflection or orientation.
        idx2: int or str, optional
            The index or the tag of the second reflection or orientation.
        """

        # Major variables:
        # h1, h2: user input reciprocal lattice vectors of the two reflections
        # h1c, h2c: user input vectors in cartesian crystal plane
        # pos1, pos2: measured diffractometer positions of the two reflections
        # u1a, u2a: measured reflection vectors in alpha frame
        # u1p, u2p: measured reflection vectors in phi frame

        # Get hkl and angle values for the first two reflections
        if self._state.reflist is None and self._state.orientlist is None:
            raise DiffcalcException("Cannot calculate a U matrix until a "
                                    "UBCalculation has been started with "
                                    "'newub'")
        if idx1 is not None and idx2 is None:
            self.calculate_UB_from_primary_only(idx1)
            return
        elif idx1 is None and idx2 is None:
            ref_data = []
            for func, idx in product(
                (self.get_reflection, self.get_orientation), (1, 2)):
                try:
                    ref_data.append(func(idx))
                except Exception:
                    pass
            try:
                ref1, ref2 = ref_data[:2]
            except ValueError:
                raise DiffcalcException(
                    "Cannot find calculate a U matrix. Please add "
                    "reference reflection and/or orientation data.")
        else:
            try:
                ref1 = self.get_reflection(idx1)
            except Exception:
                try:
                    ref1 = self.get_orientation(idx1)
                except Exception:
                    raise DiffcalcException(
                        "Cannot find first reflection or orientation with index %s"
                        % str(idx1))
            try:
                ref2 = self.get_reflection(idx2)
            except Exception:
                try:
                    ref2 = self.get_orientation(idx2)
                except Exception:
                    raise DiffcalcException(
                        "Cannot find second reflection or orientation with index %s"
                        % str(idx2))
        h1 = matrix([ref1[0]]).T  # row->column
        h2 = matrix([ref2[0]]).T

        # Compute the two reflections' reciprocal lattice vectors in the
        # cartesian crystal frame
        try:
            _, pos1, _ = ref1[:3]
            pos1.changeToRadians()
            u1p = self._strategy.calculate_q_phi(pos1)
        except AttributeError:
            _, r1, pos1 = ref1[:3]
            pos1.changeToRadians()
            [MU, _, _, ETA, CHI, PHI] = create_you_matrices(*pos1.totuple())
            Z = PHI.I * CHI.I * ETA.I * MU.I
            u1p = Z * matrix([r1]).T
        try:
            _, pos2, _ = ref2[:3]
            pos2.changeToRadians()
            u2p = self._strategy.calculate_q_phi(pos2)
        except AttributeError:
            _, r2, pos2 = ref2[:3]
            pos2.changeToRadians()
            [MU, _, _, ETA, CHI, PHI] = create_you_matrices(*pos2.totuple())
            Z = PHI.I * CHI.I * ETA.I * MU.I
            u2p = Z * matrix([r2]).T

        self._calc_UB(h1, h2, u1p, u2p)

        self._state.configure_calc_type(or0=idx1, or1=idx2)
        self.save()

    def calculate_UB_from_primary_only(self, idx=1):
        """
        Calculate orientation matrix with the shortest absolute angle change.
        Default: use first reflection.
        """

        # Algorithm from http://www.j3d.org/matrix_faq/matrfaq_latest.html

        # Get hkl and angle values for the first two reflections
        if self._state.reflist is None:
            raise DiffcalcException(
                "Cannot calculate a u matrix until a UBCalcaluation has been "
                "started with newub")
        try:
            (h, pos, _, _, _) = self.get_reflection(idx)
        except IndexError:
            raise DiffcalcException(
                "One reflection is required to calculate a u matrix")

        h = matrix([h]).T  # row->column
        pos.changeToRadians()
        B = self._state.crystal.B
        h_crystal = B * h
        h_crystal = h_crystal * (1 / norm(h_crystal))

        q_measured_phi = self._strategy.calculate_q_phi(pos)
        q_measured_phi = q_measured_phi * (1 / norm(q_measured_phi))

        rotation_axis = cross3(h_crystal, q_measured_phi)
        rotation_axis = rotation_axis * (1 / norm(rotation_axis))

        cos_rotation_angle = dot3(h_crystal, q_measured_phi)
        rotation_angle = acos(cos_rotation_angle)

        uvw = rotation_axis.T.tolist()[0]  # TODO: cleanup
        print("resulting U angle: %.5f deg" % (rotation_angle * TODEG))
        u_repr = (', '.join(['% .5f' % el for el in uvw]))
        print("resulting U axis direction: [%s]" % u_repr)

        u, v, w = uvw
        rcos = cos(rotation_angle)
        rsin = sin(rotation_angle)
        m = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]  # TODO: tidy
        m[0][0] = rcos + u * u * (1 - rcos)
        m[1][0] = w * rsin + v * u * (1 - rcos)
        m[2][0] = -v * rsin + w * u * (1 - rcos)
        m[0][1] = -w * rsin + u * v * (1 - rcos)
        m[1][1] = rcos + v * v * (1 - rcos)
        m[2][1] = u * rsin + w * v * (1 - rcos)
        m[0][2] = v * rsin + u * w * (1 - rcos)
        m[1][2] = -u * rsin + v * w * (1 - rcos)
        m[2][2] = rcos + w * w * (1 - rcos)

        if self._UB is None:
            print("Calculating UB matrix from the first reflection only.")
        else:
            print("Recalculating UB matrix from the first reflection only.")
        print("NOTE: A new UB matrix will not be automatically calculated "
              "when the orientation reflections are modified.")

        self._U = matrix(m)
        self._UB = self._U * B

        self._state.configure_calc_type(manual_U=self._U, or0=idx)
        self.save()

    def fit_ub_matrix(self, *args):
        if args is None:
            raise DiffcalcException(
                "Please specify list of reference reflection indices.")
        if len(args) < 3:
            raise DiffcalcException(
                "Need at least 3 reference reflections to fit UB matrix.")

        if len(self._state.crystal.get_lattice_params()[1]) == 6:
            new_u, uc_params = self._fit_ub_matrix_uncon(*args)
        else:
            refl_list = []
            for idx in args:
                try:
                    hkl_vals, pos, en, _, _ = self.get_reflection(idx)
                    pos.changeToRadians()
                    refl_list.append((
                        hkl_vals,
                        pos,
                        en,
                    ))
                except IndexError:
                    raise DiffcalcException(
                        "Cannot read reflection data for index %s" % str(idx))
            print("Fitting crystal lattice parameters...")
            new_lattice = fit_crystal(self._state.crystal, refl_list)
            print("Fitting orientation matrix...")
            new_u = fit_u_matrix(self._U, new_lattice, refl_list)
            uc_params = (self._state.crystal.getLattice()[0],
                         ) + new_lattice.getLattice()[1:]
        return new_u, uc_params

    def _fit_ub_matrix_uncon(self, *args):
        if args is None:
            raise DiffcalcException(
                "Please specify list of reference reflection indices.")
        if len(args) < 3:
            raise DiffcalcException(
                "Need at least 3 reference reflections to fit UB matrix.")

        x = []
        y = []
        for idx in args:
            try:
                hkl_vals, pos, en, _, _ = self.get_reflection(idx)
            except IndexError:
                raise DiffcalcException(
                    "Cannot read reflection data for index %s" % str(idx))
            pos.changeToRadians()
            x.append(hkl_vals)
            wl = 12.3984 / en
            y_tmp = self._strategy.calculate_q_phi(pos) * 2. * pi / wl
            y.append(y_tmp.T.tolist()[0])

        xm = matrix(x)
        ym = matrix(y)
        b = (xm.T * xm).I * xm.T * ym

        b1, b2, b3 = matrix(b.tolist()[0]), matrix(b.tolist()[1]), matrix(
            b.tolist()[2])
        e1 = b1 / norm(b1)
        e2 = b2 - e1 * dot3(b2.T, e1.T)
        e2 = e2 / norm(e2)
        e3 = b3 - e1 * dot3(b3.T, e1.T) - e2 * dot3(b3.T, e2.T)
        e3 = e3 / norm(e3)

        new_umatrix = matrix(e1.tolist() + e2.tolist() + e3.tolist()).T

        V = dot3(cross3(b1.T, b2.T), b3.T)
        a1 = cross3(b2.T, b3.T) * 2 * pi / V
        a2 = cross3(b3.T, b1.T) * 2 * pi / V
        a3 = cross3(b1.T, b2.T) * 2 * pi / V
        ax, bx, cx = norm(a1), norm(a2), norm(a3)
        alpha = acos(dot3(a2, a3) / (bx * cx)) * TODEG
        beta = acos(dot3(a1, a3) / (ax * cx)) * TODEG
        gamma = acos(dot3(a1, a2) / (ax * bx)) * TODEG

        lattice_name = self._state.crystal.getLattice()[0]
        return new_umatrix, (lattice_name, ax, bx, cx, alpha, beta, gamma)

    def set_miscut(self, xyz, angle, add_miscut=False):
        """Calculate U matrix using a miscut axis and an angle"""
        if xyz is None:
            rot_matrix = xyz_rotation([0, 1, 0], angle)
        else:
            rot_matrix = self._tobj.transform(xyz_rotation(xyz, angle))
        if self.is_ub_calculated() and add_miscut:
            new_U = rot_matrix * self._U
        else:
            new_U = rot_matrix
        self.set_U_manually(new_U, False)
        self.print_surface()
        self.save()

    def get_hkl_plane_distance(self, hkl):
        """Calculates and returns the distance between planes"""
        return self._state.crystal.get_hkl_plane_distance(hkl)

    def get_hkl_plane_angle(self, hkl1, hkl2):
        """Calculates and returns the angle between planes"""
        return self._state.crystal.get_hkl_plane_angle(hkl1, hkl2)

    def rescale_unit_cell(self, h, k, l, pos):
        """
        Calculate unit cell scaling parameter that matches
        given hkl position and diffractometer angles
        """
        q_vec = self._strategy.calculate_q_phi(pos)
        q_hkl = norm(q_vec) / settings.hardware.get_wavelength()
        d_hkl = self._state.crystal.get_hkl_plane_distance([h, k, l])
        sc = 1 / (q_hkl * d_hkl)
        name, a1, a2, a3, alpha1, alpha2, alpha3 = self._state.crystal.getLattice(
        )
        if abs(sc - 1.) < SMALL:
            return None, None
        ref_a1 = sc * a1 if abs(h) > SMALL else a1
        ref_a2 = sc * a2 if abs(k) > SMALL else a2
        ref_a3 = sc * a3 if abs(l) > SMALL else a3
        return sc, (name, ref_a1, ref_a2, ref_a3, alpha1, alpha2, alpha3)

    def calc_miscut(self, h, k, l, pos):
        """
        Calculate miscut angle and axis that matches
        given hkl position and diffractometer angles
        """
        q_vec = self._strategy.calculate_q_phi(pos)
        hkl_nphi = self._UB * matrix([[h], [k], [l]])
        axis = self._tobj.transform(cross3(q_vec, hkl_nphi), True)
        norm_axis = norm(axis)
        if norm_axis < SMALL:
            return None, None
        axis = axis / norm(axis)
        try:
            miscut = acos(
                bound(dot3(q_vec, hkl_nphi) /
                      (norm(q_vec) * norm(hkl_nphi)))) * TODEG
        except AssertionError:
            return None, None
        return miscut, axis.T.tolist()[0]

    def get_miscut_angle_axis(self, ubmatrix):
        y = self._get_surf_nphi()
        l = ubmatrix * y
        rotation_axis = self._tobj.transform(cross3(y, l), True)
        if abs(norm(rotation_axis)) < SMALL:
            rotation_axis = matrix('0; 0; 0')
            rotation_angle = 0
        else:
            rotation_axis = rotation_axis * (1 / norm(rotation_axis))
            cos_rotation_angle = bound(dot3(y, l) / norm(l))
            rotation_angle = acos(cos_rotation_angle) * TODEG
        return rotation_angle, rotation_axis

    def calc_hkl_offset(self, h, k, l, pol, az):
        """
        Calculate hkl orientation values that are related to
        the input hkl values by rotation with a given polar
        and azimuthal angles
        """
        hkl_nphi = self._UB * matrix([[h], [k], [l]])
        y_axis = cross3(hkl_nphi, matrix('0; 1; 0'))
        if norm3(y_axis) < SMALL:
            y_axis = cross3(hkl_nphi, matrix('0; 0; 1'))
        rot_polar = xyz_rotation(y_axis.T.tolist()[0], pol)
        rot_azimuthal = xyz_rotation(hkl_nphi.T.tolist()[0], az)
        hklrot_nphi = rot_azimuthal * rot_polar * hkl_nphi
        hklrot = self._UB.I * hklrot_nphi
        hkl_list = hklrot.T.tolist()[0]
        return hkl_list

    def calc_offset_for_hkl(self, hkl_offset, hkl_ref):
        """
        Calculate polar and azimuthal angles and scaling factor
        relating offset and reference hkl values
        """
        d_ref = self._state.crystal.get_hkl_plane_distance(hkl_ref)
        hklref_nphi = self._UB * matrix([[hkl_ref[0]], [hkl_ref[1]],
                                         [hkl_ref[2]]])
        d_offset = self._state.crystal.get_hkl_plane_distance(hkl_offset)
        hkloff_nphi = self._UB * matrix([[hkl_offset[0]], [hkl_offset[1]],
                                         [hkl_offset[2]]])
        sc = d_ref / d_offset
        par_ref_off = cross3(hklref_nphi, hkloff_nphi)
        if norm3(par_ref_off) < SMALL:
            return 0, float('nan'), sc
        y_axis = cross3(hklref_nphi, matrix('0; 1; 0'))
        if norm3(y_axis) < SMALL:
            y_axis = cross3(hklref_nphi, matrix('0; 0; 1'))
        x_axis = cross3(y_axis, hklref_nphi)
        x_coord = cos(angle_between_vectors(hkloff_nphi, x_axis))
        y_coord = cos(angle_between_vectors(hkloff_nphi, y_axis))
        pol = angle_between_vectors(hklref_nphi, hkloff_nphi)
        if abs(y_coord) < SMALL and abs(x_coord) < SMALL:
            # Set azimuthal rotation matrix to identity
            az = 0
        else:
            az = atan2(y_coord, x_coord)
        return pol, az, sc
Ejemplo n.º 7
0
class _UBCommandsBase():

    def setup_method(self):
        names = 'alpha', 'delta', 'gamma', 'omega', 'chi', 'phi'
        self.hardware = DummyHardwareAdapter(names)
        settings.hardware = self.hardware
        self.conv = CoordinateConverter(transform=self.t_matrix)
        self._refineub_matrix = matrix('0.70711   0.70711   0.00000; -0.70711   0.70711   0.00000; 0.00000   0.00000   1.00000')

        from diffcalc.ub import ub
        reload(ub)
        self.ub = ub
        #self.ub.ubcalc = ub.ubcalc
        prepareRawInput([])
        diffcalc.util.RAISE_EXCEPTIONS_FOR_ALL_ERRORS = True

    def testNewUb(self):
        self.ub.newub('test1')
        eq_(self.ub.ubcalc._state.name, 'test1')
        with pytest.raises(TypeError):
            self.ub.newub(1)

    def testNewUbInteractively(self):
        prepareRawInput(['ubcalcname', 'xtal', '1', '1', '2', '3', '91', '92',
                         '93'])
        self.ub.newub()

    def testLoadub(self):
        with pytest.raises(TypeError):
            self.ub.loadub((1, 2))

    def testSaveubcalcas(self):
        with pytest.raises(TypeError):
            self.ub.saveubas(1)
        with pytest.raises(TypeError):
            self.ub.saveubas((1, 2))
        self.ub.saveubas('blarghh')

    def testUb(self):
        with pytest.raises(TypeError):
            self.ub.showref((1))
        self.ub.ub()
        self.ub.newub('testubcalc')
        self.ub.ub()

    def testSetlat(self):
        # "Exception should result if no UBCalculation started")
        with pytest.raises(DiffcalcException):
            self.ub.setlat('HCl', 2)

        self.ub.newub('testing_setlat')
        with pytest.raises(TypeError):
            self.ub.setlat(1)
        with pytest.raises(TypeError):
            self.ub.setlat(1, 2)
        with pytest.raises(TypeError):
            self.ub.setlat('HCl')
        self.ub.setlat('NaCl', 1.1)
        ubcalc = self.ub.ubcalc
        eq_(('NaCl', 1.1, 1.1, 1.1, 90, 90, 90), ubcalc._state.crystal.getLattice())
        self.ub.setlat('NaCl', 1.1, 2.2)
        eq_(('NaCl', 1.1, 1.1, 2.2, 90, 90, 90), ubcalc._state.crystal.getLattice())
        self.ub.setlat('NaCl', 1.1, 2.2, 3.3)
        eq_(('NaCl', 1.1, 2.2, 3.3, 90, 90, 90), ubcalc._state.crystal.getLattice())
        self.ub.setlat('NaCl', 1.1, 2.2, 3.3, 91)
        eq_(('NaCl', 1.1, 2.2, 3.3, 90, 91, 90), ubcalc._state.crystal.getLattice())
        with pytest.raises(TypeError):
            self.ub.setlat(('NaCl', 1.1, 2.2, 3.3, 91, 92))
        self.ub.setlat('NaCl', 1.1, 2.2, 3.3, 91, 92, 93)
        assert_iterable_almost_equal(
            ('NaCl', 1.1, 2.2, 3.3, 91, 92, 92.99999999999999),
             ubcalc._state.crystal.getLattice())

    def testSetlatInteractive(self):
        self.ub.newub('testing_setlatinteractive')
        prepareRawInput(['xtal', '1', '1', '2', '3', '91', '92', '93'])
        self.ub.setlat()
        getLattice = self.ub.ubcalc._state.crystal.getLattice
        assert_iterable_almost_equal(getLattice(),
                         ('xtal', 1., 2., 3., 91, 92, 92.999999999999986))

        #Defaults:
        prepareRawInput(['xtal', '', '', '', '', '', '', ''])
        self.ub.setlat()
        getLattice = self.ub.ubcalc._state.crystal.getLattice
        eq_(getLattice(), ('xtal', 1., 1., 1., 90, 90, 90))

    def testShowref(self):
        with pytest.raises(TypeError):
            self.ub.showref((1))
        eq_(self.ub.showref(), None)  # No UBCalculation loaded
        # will be tested, for exceptions at least, implicitly below
        self.ub.newub('testing_showref')
        eq_(self.ub.showref(), None)  # No UBCalculation loaded"

    def testAddref(self):
        with pytest.raises(TypeError):
            self.ub.addref(1)
        with pytest.raises(TypeError):
            self.ub.addref(1, 2)
        with pytest.raises(TypeError):
            self.ub.addref(1, 2, 'blarghh')
        # start new ubcalc
        self.ub.newub('testing_addref')
        reflist = self.ub.ubcalc._state.reflist  # for convenience

        pos1 = (1.1, 1.2, 1.3, 1.4, 1.5, 1.6)
        pos2 = (2.1, 2.2, 2.3, 2.4, 2.5, 2.6)
        pos3 = (3.1, 3.2, 3.3, 3.4, 3.5, 3.6)
        pos4 = (4.1, 4.2, 4.3, 4.4, 4.5, 4.6)
        #
        self.hardware.energy = 1.10
        self.hardware.position = pos1
        self.ub.addref([1.1, 1.2, 1.3])
        result = reflist.get_reflection_in_external_angles(1)
        eq_(result[:-1], ([1.1, 1.2, 1.3], pos1, 1.10, None))

        self.hardware.energy = 2.10
        self.hardware.position = pos2
        self.ub.addref([2.1, 2.2, 2.3], 'atag')
        result = reflist.get_reflection_in_external_angles(2)
        eq_(result[:-1], ([2.1, 2.2, 2.3], pos2, 2.10, 'atag'))

        self.ub.addref([3.1, 3.2, 3.3], pos3, 3.10)
        result = reflist.get_reflection_in_external_angles(3)
        eq_(result[:-1], ([3.1, 3.2, 3.3], pos3, 3.10, None))

        self.ub.addref([4.1, 4.2, 4.3], pos4, 4.10, 'tag2')
        result = reflist.get_reflection_in_external_angles(4)
        eq_(result[:-1], ([4.1, 4.2, 4.3], pos4, 4.10, 'tag2'))

    def testAddrefInteractively(self):
        prepareRawInput([])
        # start new ubcalc
        self.ub.newub('testing_addref')
        reflist = self.ub.ubcalc._state.reflist  # for convenience

        pos1 = (1.1, 1.2, 1.3, 1.4, 1.5, 1.6)
        pos2 = (2.1, 2.2, 2.3, 2.4, 2.5, 2.6)
        pos3 = (3.1, 3.2, 3.3, 3.4, 3.5, 3.6)
        pos3s = ['3.1', '3.2', '3.3', '3.4', '3.5', '3.6']
        pos4 = (4.1, 4.2, 4.3, 4.4, 4.5, 4.6)
        pos4s = ['4.1', '4.2', '4.3', '4.4', '4.5', '4.6']
        #
        self.hardware.energy = 1.10
        self.hardware.position = pos1
        prepareRawInput(['1.1', '1.2', '1.3', '', ''])
        self.ub.addref()
        result = reflist.get_reflection_in_external_angles(1)
        eq_(result[:-1], ([1.1, 1.2, 1.3], pos1, 1.10, None))

        self.hardware.energy = 2.10
        self.hardware.position = pos2
        prepareRawInput(['2.1', '2.2', '2.3', '', 'atag'])
        self.ub.addref()
        result = reflist.get_reflection_in_external_angles(2)
        eq_(result[:-1], ([2.1, 2.2, 2.3], pos2, 2.10, 'atag'))

        prepareRawInput(['3.1', '3.2', '3.3', 'n'] + pos3s + ['3.10', ''])
        self.ub.addref()
        result = reflist.get_reflection_in_external_angles(3)
        eq_(result[:-1], ([3.1, 3.2, 3.3], pos3, 3.10, None))

        prepareRawInput(['4.1', '4.2', '4.3', 'n'] + pos4s + ['4.10', 'tag2'])
        self.ub.addref()
        result = reflist.get_reflection_in_external_angles(4)
        eq_(result[:-1], ([4.1, 4.2, 4.3], pos4, 4.10, 'tag2'))

    def testEditRefInteractivelyWithCurrentPosition(self):
        pos1 = (1.1, 1.2, 1.3, 1.4, 1.5, 1.6)
        pos2 = (2.1, 2.2, 2.3, 2.4, 2.5, 2.6)
        self.ub.newub('testing_editref')
        self.ub.addref([1, 2, 3], pos1, 10, 'tag1')
        self.hardware.energy = 11
        self.hardware.position = pos2
        prepareRawInput(['1.1', '', '3.1', 'y', ''])
        self.ub.editref(1)

        reflist = self.ub.ubcalc._state.reflist  # for convenience
        result = reflist.get_reflection_in_external_angles(1)
        eq_(result[:-1], ([1.1, 2, 3.1], pos2, 11, 'tag1'))

    def testEditRefInteractivelyWithEditedPosition(self):
        pos1 = (1.1, 1.2, 1.3, 1.4, 1.5, 1.6)
        pos2 = (2.1, 2.2, 2.3, 2.4, 2.5, 2.6)
        pos2s = ['2.1', '2.2', '2.3', '2.4', '2.5', '2.6']
        self.ub.newub('testing_editref')
        self.ub.addref([1, 2, 3], pos1, 10, 'tag1')
        prepareRawInput(['1.1', '', '3.1', 'n'] + pos2s + ['12', 'newtag'])
        self.ub.editref(1)

        reflist = self.ub.ubcalc._state.reflist
        result = reflist.get_reflection_in_external_angles(1)
        eq_(result[:-1], ([1.1, 2, 3.1], pos2, 12, 'newtag'))

    def testSwapref(self):
        with pytest.raises(TypeError):
            self.ub.swapref(1)
        with pytest.raises(TypeError):
            self.ub.swapref(1, 2, 3)
        with pytest.raises(IndexError):
            self.ub.swapref(1, 1.1)
        self.ub.newub('testing_swapref')
        pos = (1.1, 1.2, 1.3, 1.4, 1.5, 1.6)
        self.ub.addref([1, 2, 3], pos, 10, 'tag1')
        self.ub.addref([1, 2, 3], pos, 10, 'tag2')
        self.ub.addref([1, 2, 3], pos, 10, 'tag3')
        self.ub.swapref(1, 3)
        self.ub.swapref(1, 3)
        self.ub.swapref(3, 1)  # end flipped
        reflist = self.ub.ubcalc._state.reflist
        tag1 = reflist.get_reflection_in_external_angles(1)[3]
        tag2 = reflist.get_reflection_in_external_angles(2)[3]
        tag3 = reflist.get_reflection_in_external_angles(3)[3]
        eq_(tag1, 'tag3')
        eq_(tag2, 'tag2')
        eq_(tag3, 'tag1')
        self.ub.swapref()
        tag1 = reflist.get_reflection_in_external_angles(1)[3]
        tag2 = reflist.get_reflection_in_external_angles(2)[3]
        eq_(tag1, 'tag2')
        eq_(tag2, 'tag3')

    def testDelref(self):
        self.ub.newub('testing_swapref')
        pos = (1.1, 1.2, 1.3, 1.4, 1.5, 1.6)
        self.ub.addref([1, 2, 3], pos, 10, 'tag1')
        reflist = self.ub.ubcalc._state.reflist
        reflist.get_reflection_in_external_angles(1)
        self.ub.delref(1)
        with pytest.raises(IndexError):
            reflist.get_reflection_in_external_angles(1)

    def testShoworient(self):
        with pytest.raises(TypeError):
            self.ub.showorient((1))
        eq_(self.ub.showorient(), None)  # No UBCalculation loaded
        # will be tested, for exceptions at least, implicitly below
        self.ub.newub('testing_showorient')
        eq_(self.ub.showorient(), None)  # No UBCalculation loaded"

    def testAddorient(self):
        with pytest.raises(TypeError):
            self.ub.addorient(1)
        with pytest.raises(TypeError):
            self.ub.addorient(1, 2)
        with pytest.raises(TypeError):
            self.ub.addorient(1, 2, 'blarghh')
        # start new ubcalc
        self.ub.newub('testing_addorient')
        orientlist = self.ub.ubcalc._state.orientlist  # for convenience

        hkl1 = [1.1, 1.2, 1.3]
        hkl2 = [2.1, 2.2, 2.3]
        orient1 = [1.4, 1.5, 1.6]
        orient2 = [2.4, 2.5, 2.6]
        #
        self.ub.addorient(hkl1, orient1)
        result = orientlist.getOrientation(1)
        trans_orient1 = self.conv.transform(matrix([orient1]).T)
        eq_(result[0], hkl1)
        mneq_(matrix([result[1]]), trans_orient1.T)
        eq_(result[-2], None)

        self.ub.addorient(hkl2, orient2, 'atag')
        result = orientlist.getOrientation(2)
        trans_orient2 = self.conv.transform(matrix([orient2]).T)
        eq_(result[0], hkl2)
        mneq_(matrix([result[1]]), trans_orient2.T)
        eq_(result[-2], 'atag')

    def testAddorientInteractively(self):
        prepareRawInput([])
        # start new ubcalc
        self.ub.newub('testing_addorient')
        orientlist = self.ub.ubcalc._state.orientlist  # for convenience

        hkl1 = [1.1, 1.2, 1.3]
        hkl2 = [2.1, 2.2, 2.3]
        orient1 = [1.4, 1.5, 1.6]
        orient2 = [2.4, 2.5, 2.6]
        #
        prepareRawInput(['1.1', '1.2', '1.3', '1.4', '1.5', '1.6', 'y', ''])
        self.ub.addorient()
        result = orientlist.getOrientation(1)
        trans_orient1 = self.conv.transform(matrix([orient1]).T)
        eq_(result[0], hkl1)
        mneq_(matrix([result[1]]), trans_orient1.T)
        eq_(result[-2], None)

        prepareRawInput(['2.1', '2.2', '2.3', '2.4', '2.5', '2.6', 'y', 'atag'])
        self.ub.addorient()
        result = orientlist.getOrientation(2)
        trans_orient2 = self.conv.transform(matrix([orient2]).T)
        eq_(result[0], hkl2)
        mneq_(matrix([result[1]]), trans_orient2.T)
        eq_(result[-2], 'atag')

    def testEditOrientInteractively(self):
        hkl1 = [1.1, 1.2, 1.3]
        hkl2 = [1.1, 1.2, 3.1]
        orient1 = [1.4, 1.5, 1.6]
        orient2 = [2.4, 1.5, 2.6]
        orient2s = ['2.4', '', '2.6']
        self.ub.newub('testing_editorient')
        self.ub.addorient(hkl1, orient1, 'tag1')
        prepareRawInput(['1.1', '', '3.1'] + orient2s + ['y', 'newtag',])
        self.ub.editorient(1)

        orientlist = self.ub.ubcalc._state.orientlist
        result = orientlist.getOrientation(1)
        trans_orient2 = self.conv.transform(matrix([orient2]).T)
        eq_(result[0], hkl2)
        mneq_(matrix([result[1]]), trans_orient2.T)
        eq_(result[-2], 'newtag')

    def testSwaporient(self):
        with pytest.raises(TypeError):
            self.ub.swaporient(1)
        with pytest.raises(TypeError):
            self.ub.swaporient(1, 2, 3)
        with pytest.raises(IndexError):
            self.ub.swaporient(1, 1.1)
        self.ub.newub('testing_swaporient')
        hkl = [1.1, 1.2, 1.3]
        orient = [1.4, 1.5, 1.6]
        self.ub.addorient(hkl, orient, 'tag1')
        self.ub.addorient(hkl, orient, 'tag2')
        self.ub.addorient(hkl, orient, 'tag3')
        self.ub.swaporient(1, 3)
        self.ub.swaporient(1, 3)
        self.ub.swaporient(3, 1)  # end flipped
        orientlist = self.ub.ubcalc._state.orientlist
        tag1 = orientlist.getOrientation(1)[-2]
        tag2 = orientlist.getOrientation(2)[-2]
        tag3 = orientlist.getOrientation(3)[-2]
        eq_(tag1, 'tag3')
        eq_(tag2, 'tag2')
        eq_(tag3, 'tag1')
        self.ub.swaporient()
        tag1 = orientlist.getOrientation(1)[-2]
        tag2 = orientlist.getOrientation(2)[-2]
        eq_(tag1, 'tag2')
        eq_(tag2, 'tag3')

    def testDelorient(self):
        self.ub.newub('testing_delorient')
        hkl = [1.1, 1.2, 1.3]
        pos = [1.4, 1.5, 1.6]
        self.ub.addorient(hkl, pos, 'tag1')
        orientlist = self.ub.ubcalc._state.orientlist
        orientlist.getOrientation(1)
        self.ub.delorient(1)
        with pytest.raises(IndexError):
            orientlist.getOrientation(1)

    def testSetu(self):
        # just test calling this method
        #self.ub.setu([[1,2,3],[1,2,3],[1,2,3]])
        self.ub.newub('testsetu')
        setu = self.ub.setu
        with pytest.raises(TypeError):
            setu(1, 2)
        with pytest.raises(TypeError):
            setu(1)
        with pytest.raises(TypeError):
            setu('a')
        with pytest.raises(TypeError):
            setu([1, 2, 3])
        with pytest.raises(TypeError):
            setu([[1, 2, 3], [1, 2, 3], [1, 2]])
        # diffCalcException expected if no lattice set yet
        with pytest.raises(DiffcalcException):
            setu([[1, 2, 3], [1, 2, 3], [1, 2, 3]])
        self.ub.setlat('NaCl', 1.1)
        setu([[1, 2, 3], [1, 2, 3], [1, 2, 3]])  # check no exceptions only
        setu(((1, 2, 3), (1, 2, 3), (1, 2, 3)))  # check no exceptions only

    def testSetuInteractive(self):
        self.ub.newub('testsetu')
        self.ub.setlat('NaCl', 1.1)

        prepareRawInput(['1 2 3', '4 5 6', '7 8 9'])
        self.ub.setu()
        a = self.ub.ubcalc.U.tolist()
        eq_([list(a[0]), list(a[1]), list(a[2])],
            [[1, 2, 3], [4, 5, 6], [7, 8, 9]])

        prepareRawInput(['', ' 9 9.9 99', ''])
        self.ub.setu()

        a = self.ub.ubcalc.U.tolist()
        eq_([list(a[0]), list(a[1]), list(a[2])],
            [[1, 0, 0], [9, 9.9, 99], [0, 0, 1]])

    def testSetub(self):
        # just test calling this method
        self.ub.newub('testsetub')
        setub = self.ub.setub
        with pytest.raises(TypeError):
            setub(1, 2)
        with pytest.raises(TypeError):
            setub(1)
        with pytest.raises(TypeError):
            setub('a')
        with pytest.raises(TypeError):
            setub([1, 2, 3])
        with pytest.raises(TypeError):
            setub([[1, 2, 3], [1, 2, 3], [1, 2]])
        setub([[1, 2, 3], [1, 2, 3], [1, 2, 3]])  # check no exceptions only
        setub(((1, 2, 3), (1, 2, 3), (1, 2, 3)))  # check no exceptions only

    def testSetUbInteractive(self):
        self.ub.newub('testsetu')
        self.ub.setlat('NaCl', 1.1)

        prepareRawInput(['1 2 3', '4 5 6', '7 8 9'])
        self.ub.setub()
        a = self.ub.ubcalc.UB.tolist()
        eq_([list(a[0]), list(a[1]), list(a[2])],
            [[1, 2, 3], [4, 5, 6], [7, 8, 9]])

        prepareRawInput(['', ' 9 9.9 99', ''])
        self.ub.setub()
        a = self.ub.ubcalc.UB.tolist()
        eq_([list(a[0]), list(a[1]), list(a[2])],
            [[1, 0, 0], [9, 9.9, 99], [0, 0, 1]])

    def testCalcub(self):
        with pytest.raises(DiffcalcException):
            self.ub.calcub(1)  # wrong input
        # no ubcalc started:
        with pytest.raises(DiffcalcException):
            self.ub.calcub()
        self.ub.newub('testcalcub')
        # not enough reflections:
        with pytest.raises(DiffcalcException):
            self.ub.calcub()

        for s in scenarios.sessions(settings.Pos):
            self.ub.setlat(s.name, *s.lattice)
            self.ub.clearref()
            r = s.ref1
            self.ub.addref(
                [r.h, r.k, r.l], r.pos.totuple(), r.energy, r.tag)
            r = s.ref2
            self.ub.addref(
                [r.h, r.k, r.l], r.pos.totuple(), r.energy, r.tag)
            self.ub.calcub(s.ref1.tag, s.ref2.tag)
            mneq_(self.ub.ubcalc.UB, matrix(s.umatrix) * matrix(s.bmatrix),
                  4, note="wrong UB matrix after calculating U")

    def testOrientub(self):
        with pytest.raises(DiffcalcException):
            self.ub.orientub(1)  # wrong input
        # no ubcalc started:
        with pytest.raises(DiffcalcException):
            self.ub.orientub()
        self.ub.newub('testorientub')
        # not enough orientations:
        with pytest.raises(DiffcalcException):
            self.ub.orientub()

        s = scenarios.sessions(settings.Pos)[1]
        self.ub.setlat(s.name, *s.lattice)
        r1 = s.ref1
        orient1 = self.conv.transform(matrix('1; 0; 0'), True)
        tag1 = 'or'+r1.tag
        self.ub.addorient(
            (r1.h, r1.k, r1.l), orient1.T.tolist()[0], tag1)
        r2 = s.ref2
        orient2 = self.conv.transform(matrix('0; -1; 0'), True)
        tag2 = 'or'+r2.tag
        self.ub.addorient(
            (r2.h, r2.k, r2.l), orient2.T.tolist()[0], tag2)
        self.ub.orientub()
        mneq_(self.ub.ubcalc.UB, matrix(s.umatrix) * matrix(s.bmatrix),
              4, note="wrong UB matrix after calculating U")
        self.ub.orientub(tag1, tag2)
        mneq_(self.ub.ubcalc.UB, matrix(s.umatrix) * matrix(s.bmatrix),
              4, note="wrong UB matrix after calculating U")
        self.ub.addref(
            [r1.h, r1.k, r1.l], r1.pos.totuple(), r1.energy, r1.tag)
        self.ub.orientub(r1.tag, tag2)
        mneq_(self.ub.ubcalc.UB, matrix(s.umatrix) * matrix(s.bmatrix),
              4, note="wrong UB matrix after calculating U")
        self.ub.addref(
            [r2.h, r2.k, r2.l], r2.pos.totuple(), r2.energy, r2.tag)
        self.ub.orientub(tag1, r2.tag)
        mneq_(self.ub.ubcalc.UB, matrix(s.umatrix) * matrix(s.bmatrix),
              4, note="wrong UB matrix after calculating U")

    def testRefineubInteractively(self):
        self.ub.newub('testing_refineubinteractive')
        self.ub.setlat('xtal', 1, 1, 1, 90, 90, 90)
        self.ub.setmiscut(0)
        prepareRawInput(['1', '1', '', 'n', '0', '60', '0', '30', '0', '0', 'y', 'y'])
        self.ub.refineub()
        getLattice = self.ub.ubcalc._state.crystal.getLattice
        eq_(('xtal', sqrt(2.), sqrt(2.), 1, 90, 90, 90), getLattice())
        mneq_(self.ub.ubcalc.U, self._refineub_matrix,
              4, note="wrong U matrix after refinement")

    def testRefineubInteractivelyWithPosition(self):
        self.ub.newub('testing_refineubinteractivepos')
        self.ub.setlat('xtal', 1, 1, 1, 90, 90, 90)
        self.ub.setmiscut(0)
        self.hardware.position = [0, 60, 0, 30, 0, 0]
        prepareRawInput(['1', '1', '', 'y', 'y', 'y'])
        self.ub.refineub()
        getLattice = self.ub.ubcalc._state.crystal.getLattice
        eq_(('xtal', sqrt(2.), sqrt(2.), 1, 90, 90, 90), getLattice())
        mneq_(self.ub.ubcalc.U, self._refineub_matrix,
              4, note="wrong U matrix after refinement")

    def testRefineubInteractivelyWithHKL(self):
        self.ub.newub('testing_refineubinteractivehkl')
        self.ub.setlat('xtal', 1, 1, 1, 90, 90, 90)
        self.ub.setmiscut(0)
        self.hardware.position = [0, 60, 0, 30, 0, 0]
        prepareRawInput(['y', 'y', 'y'])
        self.ub.refineub([1, 1, 0])
        getLattice = self.ub.ubcalc._state.crystal.getLattice
        eq_(('xtal', sqrt(2.), sqrt(2.), 1, 90, 90, 90), getLattice())
        mneq_(self.ub.ubcalc.U, self._refineub_matrix,
              4, note="wrong U matrix after refinement")

    def testRefineub(self):
        self.ub.newub('testing_refineub')
        self.ub.setlat('xtal', 1, 1, 1, 90, 90, 90)
        self.ub.setmiscut(0)
        prepareRawInput(['y', 'y'])
        self.ub.refineub([1, 1, 0], [0, 60, 0, 30, 0, 0])
        getLattice = self.ub.ubcalc._state.crystal.getLattice
        eq_(('xtal', sqrt(2.), sqrt(2.), 1, 90, 90, 90), getLattice())
        mneq_(self.ub.ubcalc.U, self._refineub_matrix,
              4, note="wrong U matrix after refinement")

    def testFitub(self):
        self.ub.newub('testfitub')
        for s in scenarios.sessions(settings.Pos)[-3:]:
            a, b, c, alpha, beta, gamma = s.lattice
            self.ub.clearref()
            for r in s.reflist:
                self.ub.addref(
                    [r.h, r.k, r.l], r.pos.totuple(), r.energy, r.tag)
            self.ub.setlat(s.name, s.system, *s.lattice)
            self.ub.calcub(s.ref1.tag, s.ref2.tag)

            init_latt = (1.06 * a, 1.07 * b, 0.94 * c,
                         1.05 * alpha, 1.06 * beta, 0.95 * gamma)
            self.ub.setlat(s.name, s.system, *init_latt)
            self.ub.addmiscut(3., [0.2, 0.8, 0.1])

            prepareRawInput(['y', 'y'])
            self.ub.fitub(*tuple(r.tag for r in s.reflist))
            assert self.ub.ubcalc._state.crystal._system == s.system
            mneq_(matrix((self.ub.ubcalc._state.crystal.getLattice()[1:])), matrix(s.lattice),
                  2, note="wrong lattice after fitting UB")
            mneq_(self.ub.ubcalc.U, matrix(s.umatrix),
                  3, note="wrong U matrix after fitting UB")

    def testC2th(self):
        self.ub.newub('testc2th')
        self.ub.setlat('cube', 1, 1, 1, 90, 90, 90)
        assert self.ub.c2th((0, 0, 1)) == pytest.approx(60)

    def testHKLangle(self):
        self.ub.newub('testhklangle')
        self.ub.setlat('cube', 1, 1, 1, 90, 90, 90)
        assert self.ub.hklangle((0, 0, 1), (0, 0, 2)) == pytest.approx(0)
        assert self.ub.hklangle((0, 1, 0), (0, 0, 2)) == pytest.approx(90)
        assert self.ub.hklangle((1, 0, 0), (0, 0, 2)) == pytest.approx(90)
        assert self.ub.hklangle((1, 1, 0), (0, 0, 2)) == pytest.approx(90)
        assert self.ub.hklangle((0, 1, 1), (0, 0, 2)) == pytest.approx(45)
        assert self.ub.hklangle((1, 0, 1), (0, 0, 2)) == pytest.approx(45)
        assert self.ub.hklangle((1, 1, 1), (0, 0, 2)) == pytest.approx(atan(sqrt(2))*TODEG)

    def testSigtau(self):
        # sigtau [sig tau]
        with pytest.raises(TypeError):
            self.ub.sigtau(1)
        with pytest.raises(ValueError):
            self.ub.sigtau(1, 'a')
        self.ub.sigtau(1, 2)
        self.ub.sigtau(1, 2.0)
        eq_(self.ub.ubcalc.sigma, 1)
        eq_(self.ub.ubcalc.tau, 2.0)

    def testSigtauInteractive(self):
        prepareRawInput(['1', '2.'])
        self.ub.sigtau()
        eq_(self.ub.ubcalc.sigma, 1)
        eq_(self.ub.ubcalc.tau, 2.0)
        #Defaults:
        prepareRawInput(['', ''])
        self.hardware.position = [None, None, None, None, 3, 4.]
        self.ub.sigtau()
        eq_(self.ub.ubcalc.sigma, -3.)
        eq_(self.ub.ubcalc.tau, -4.)

    def testSetWithString(self):
        with pytest.raises(TypeError):
            self.ub.setlat('alpha')
        with pytest.raises(TypeError):
            self.ub.setlat('alpha', 1, 'a')

    def test_setnphihkl_at_various_phases(self):
        self.ub.setnphi([1, 0, 1])
        self.ub.setnhkl([1, 0, 1])
        self.ub.newub('test')
        self.ub.setnphi([1, 0, 1])
        self.ub.setnhkl([1, 0, 1]) 
        self.ub.setlat('cube', 1, 1, 1, 90, 90, 90)
        self.ub.setnphi([1, 0, 1])
        self.ub.setnhkl([1, 0, 1])
        self.ub.setu([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
        self.ub.setnphi([1, 0, 1])
        self.ub.setnhkl([1, 0, 1])

    def test_surfnphihkl_at_various_phases(self):
        self.ub.surfnphi([1, 0, 1])
        self.ub.surfnhkl([1, 0, 1])
        self.ub.newub('test')
        self.ub.surfnphi([1, 0, 1])
        self.ub.surfnhkl([1, 0, 1]) 
        self.ub.setlat('cube', 1, 1, 1, 90, 90, 90)
        self.ub.surfnphi([1, 0, 1])
        self.ub.surfnhkl([1, 0, 1])
        self.ub.setu([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
        self.ub.surfnphi([1, 0, 1])
        self.ub.surfnhkl([1, 0, 1])

    def testMiscut(self):
        self.ub.newub('testsetmiscut')
        self.ub.ubcalc._state.reference._set_n_phi_configured(matrix('0; 0; 1'))
        self.ub.setlat('cube', 1, 1, 1, 90, 90, 90)
        beam_axis = self.conv.transform(matrix('0; 1; 0'), True).T.tolist()[0]
        beam_maxis = self.conv.transform(matrix('0; -1; 0'), True).T.tolist()[0]
        self.ub.setmiscut(self.t_hand * 30, beam_axis)
        mneq_(self.ub.ubcalc._state.reference.n_hkl, matrix('-0.5000000; 0.00000; 0.8660254'))
        self.ub.addmiscut(self.t_hand * 15, beam_axis)
        mneq_(self.ub.ubcalc._state.reference.n_hkl, matrix('-0.7071068; 0.00000; 0.7071068'))
        self.ub.addmiscut(self.t_hand * 45, beam_maxis)
        mneq_(self.ub.ubcalc._state.reference.n_hkl, matrix('0.0; 0.0; 1.0'))