Code Monkey home page Code Monkey logo

Comments (2)

prjemian avatar prjemian commented on July 16, 2024

In #29 (comment), it was shown how to get photon energy from beam line controls. The same steps could be used to get wavelength instead.

Internally, the calc module stores only the wavelength (angstroms). It converts energy to wavelength when energy is set. Here's the code:

hklpy/hkl/calc.py

Lines 136 to 152 in 14463c4

@property
def wavelength(self):
'''The wavelength associated with the geometry, in angstrom'''
return self._geometry.wavelength_get(self._units)
@wavelength.setter
def wavelength(self, wavelength):
self._geometry.wavelength_set(wavelength, self._units)
@property
def energy(self):
'''The energy associated with the geometry, in keV'''
return A_KEV / self.wavelength
@energy.setter
def energy(self, energy):
self.wavelength = A_KEV / energy

example

This user operation sets the energy: fourc.calc.energy = 8.04 (the units are keV). The setter part sets the internal wavelength fourc.calc._wavelength by calculating A_KEV/energy. When the users asks for the current energy, it is recalculated from the internal wavelength (A_KEV/wavelength).

from hklpy.

prjemian avatar prjemian commented on July 16, 2024

It is recommended to get either the energy or wavelength from the beam line controls, but not both. This is always a local customization to the specific instrument so it is not appropriate to add into the code here. (This example could be part of the documentation, however.)

There is already code in place to use an energy ophyd.Signal (and a default Diffractometer._energy_changed() method) to set energy and wavelength in the calc attribute. Override the energy Signal with an ophyd.EpicsSignal and connect to the desired energy PV. An example is provided (from #29 (comment) & #29 (comment)). The example also shows unit conversion (using thepint package) and a Signal for energy offset (as requested in #25). (If the offset is an EPICS PV, then use EpicsSignal and the PV instead).

import gi
gi.require_version('Hkl', '5.0')
import hkl.diffract
from ophyd import Component, EpicsSignal, Signal
import pint

class LocalDiffractometer(hkl.diffract.Diffractometer):
    # ...
    energy = Component(EpicsSignal, "EPICS:diffractometer:energy")
    energy_EGU = Component(EpicsSignal, "EPICS:diffractometer:energy.EGU")
    energy_update_calc = Component(EpicsSignal, "EPICS:diffractometer:energy_update_hkl")
    energy_offset = Component(Signal, value=0)
    # ...

    def _energy_changed(self, value=None, **kwargs):
        '''
        Callback indicating that the energy signal was updated
        '''
        if energy_update_calc.get() in (1, "Yes", "locked", "OK"):
            local_energy = value + self.energy_offset.get()
            units = self.energy_EGU.get()   # get units from EPICS
            # units = "eV"    # alternative: from control system always gives "eV"
            logger.debug(
                '{0.name} energy changed: {1} {2}'.format(
                    self, value, units))
            keV = pint.Quantity(local_energy, units).to("keV")
            self._calc.energy = keV
            self._update_position()

from hklpy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.