OptrisOptrisDocumentation

Documentation/OTC SDK/optris_sdk — Rust API referencev12.0

temperatures — functions and constants

The SDK's temperature unit, and converting, rounding and labelling one temperature yourself, the same way in every Optris application and every SDK language.

One switch for the whole SDK

set_temperature_unit chooses the unit of the whole SDK for this process. Celsius is the default, so a program that never calls it sees nothing change. With TemperatureUnit::Fahrenheit, every temperature the SDK takes or returns is in degrees Fahrenheit and every temperature difference in Fahrenheit degrees: frames, region statistics, alarm limits and events, the colour range, camera settings, process-interface channels, values read back from a recording or a snapshot. A name that says _c or celsius (crate::RegionStats::mean_c, crate::Frame::temperatures) follows the unit as well; the name is older than the switch.

The SDK keeps storing and computing in Celsius and converts where a value crosses the API, so a setting made in one unit keeps its meaning after a switch and reads back in the new unit, and every file the SDK writes stays Celsius, byte for byte the same in either unit. A CSV snapshot keeps its own unit (crate::CsvOptions::with_temperature_unit). crate::internal_to_celsius decodes the raw samples, which are Celsius by definition. Set the unit once, at start, before opening a camera:

# fn main() -> optris_sdk::Result<()> {
optris_sdk::set_temperature_unit(optris_sdk::TemperatureUnit::Fahrenheit)?;
# Ok(())
# }

The functions after those two convert one number for a program that does it itself.

A temperature and a difference convert differently

A temperature is a point on the scale: F = C × 9/5 + 32. A difference is a distance on it: F = K × 9/5, with no offset. An alarm's hysteresis, the limits of a rule with a reference (which are offsets), a standard deviation and a colour-scale span are differences. Converting 2 K like a temperature shows 35.6 °F; the right answer is 3.6 °F. So there are two pairs of functions, and the name says which one a number needs.

Converting never rounds; text is rounded once

The four conversions are plain arithmetic, so a value typed in one unit survives the trip into Celsius and back: 70.1 °F is stored as 21.1666… °C and shown as 70.1 °F again. format() and format_difference convert first and then round once, in the unit shown, half away from zero: 21.25 becomes "21.3" and -21.25 becomes "-21.3" at one decimal. "Half" is judged on the decimal the f64 stands for, the shortest decimal that reads back as the same f64, which is what {} prints for it: 2.675 rounds to "2.68" at two decimals although no f64 holds 2.675 exactly (format!("{:.2}", 2.675) gives "2.67"). A result that rounds to zero has no sign.

Every function runs the SDK's own code, so Rust, C, C++, C# and Python show the same last digit for the same reading.

Examples

# fn main() -> optris_sdk::Result<()> {
use optris_sdk::{temperatures, TemperatureUnit};

let unit = TemperatureUnit::Fahrenheit;

// A reading: "70.3 °F".
let shown = format!(
    "{} {}",
    temperatures::format(21.3, unit, 1, '.')?,
    temperatures::symbol(unit)?
);

// A hysteresis of 2 K: "3.6", not "35.6".
let band = temperatures::format_difference(2.0, unit, 1, '.')?;

// What the user typed, stored in Celsius without rounding.
let limit_c = temperatures::to_celsius(70.1, unit)?;
# let _ = (shown, band, limit_c);
# Ok(())
# }

Errors

Every function returns Status::InvalidArgument for TemperatureUnit::Other; the two format functions also for decimals above 15 and a separator other than '.' or ','. NaN and the infinities are not refused: the conversions carry them, and the format functions return an empty string for them.

Status::InvalidArgument: crate::Status::InvalidArgument

pub fn format(

A Celsius temperature as text in unit, without the symbol: converted, then rounded once, half away from zero, to decimals decimals, with decimal_separator ('.' or ',') before them. No digit grouping, and the process locale never reaches the text.

Returns an empty string for NaN and the infinities, so the application decides how it shows a missing value.

Errors

crate::Status::InvalidArgument for TemperatureUnit::Other, decimals above 15, or another separator.

pub fn format_difference(

A temperature difference in kelvin as text in degrees of unit: converted with no offset, then rounded exactly as format() rounds. 2 K in Fahrenheit is "3.6", not "35.6".

Errors

As format().

pub fn from_celsius(celsius: f64, unit: TemperatureUnit) -> Result<f64>

A temperature (a point on the scale) from degrees Celsius into unit. Does not round.

Errors

crate::Status::InvalidArgument for TemperatureUnit::Other.

pub fn from_kelvin_difference(kelvin: f64, unit: TemperatureUnit) -> Result<f64>

A temperature difference (a distance on the scale) from kelvin into degrees of unit: K × 9/5 for Fahrenheit, with no offset. Does not round.

Errors

crate::Status::InvalidArgument for TemperatureUnit::Other.

pub fn set_temperature_unit(unit: TemperatureUnit) -> Result<()>

Choose the temperature unit of the whole SDK, for this process.

Every temperature and temperature difference that crosses the API afterwards is in unit (see the top of this module). Call it once, at start, before opening a camera.

Errors

crate::Status::InvalidArgument for TemperatureUnit::Other; the unit then stays what it was.

pub fn symbol(unit: TemperatureUnit) -> Result<String>

The unit's symbol: the degree sign (U+00B0) followed by C or F.

Errors

crate::Status::InvalidArgument for TemperatureUnit::Other.

pub fn temperature_unit() -> Result<TemperatureUnit>

The SDK's temperature unit: what set_temperature_unit last chose, TemperatureUnit::Celsius until then.

Errors

Only if the library refuses the call, which it does not.

pub fn to_celsius(value: f64, unit: TemperatureUnit) -> Result<f64>

A temperature in unit back into degrees Celsius, for storing what a person typed. Does not round.

Errors

crate::Status::InvalidArgument for TemperatureUnit::Other.

pub fn to_kelvin_difference(value: f64, unit: TemperatureUnit) -> Result<f64>

A temperature difference in degrees of unit back into kelvin. Does not round.

Errors

crate::Status::InvalidArgument for TemperatureUnit::Other.