0

I would like to be able to convert between frequency values and period values. These are not directly convertible since they are not exactly the same unit, but I read that there are predefined relationships between units called Contexts. However, I can't find which Contexts define which relations and I cannot convert MHz to ns in the context 'sp' given in the example:

>>> import pint
>>> ureg = pint.UnitRegistry()
>>> freq = 25 * ureg.MHz
>>> ureg.enable_contexts('sp')
>>> freq.to('us')

gives the error

pint.errors.DimensionalityError: Cannot convert from 'megahertz' (1 / [time]) to 'microsecond' ([time])
mlg556
  • 419
  • 3
  • 13
  • Spectroscopy defines a relationship between *wavelength* and frequency. You can see its definition in the file example in the docs: https://pint.readthedocs.io/en/latest/contexts.html#defining-contexts-in-a-file – jonrsharpe Dec 02 '18 at 18:55
  • My trouble was about finding which Contexts are already predefined and which of those Contexts define which relationships and do we have a predefined context with the relationship `T = 1 / f`. The docs elaborate on the 'sp' Context but don't even hint to others. – mlg556 Dec 02 '18 at 19:13

1 Answers1

1

The relation between frequency and period is T = 1 / f:

>>> import pint
>>> ureg = pint.UnitRegistry()
>>> freq = 25 * ureg.MHz
>>> (1 / freq).to('ns')
39.99999999999999 nanosecond
caverac
  • 1,505
  • 2
  • 12
  • 17
  • I was rather hoping that you could point me to one Context which includes this relationship, I guess I am going to have to define my own and import it as explained in the docs. Isn't it weird that the simple relationship `T = 1 / f` is not included in any standard Context ? – mlg556 Dec 02 '18 at 19:10