Add a class and windchill example.

This commit is contained in:
Dorian 2019-11-06 17:42:41 -05:00
parent 57bfde36c0
commit 5c53942b62
2 changed files with 31 additions and 0 deletions

View File

@ -25,3 +25,7 @@ Insure the following dependencies are in place:
## Documentation ## Documentation
* [PyO3 Python - Rust bindings](https://pyo3.rs/master/get_started.html) * [PyO3 Python - Rust bindings](https://pyo3.rs/master/get_started.html)
* Windchill calculations:
* [PDF from US Gov](https://www.weather.gov/media/epz/wxcalc/windChill.pdf)
* [Calculation in Celsius and kph](https://www.calcunation.com/calculator/wind-chill-celsius.php)
* [Windchill Calculator](https://www.weather.gov/epz/wxcalc_windchill)

View File

@ -5,6 +5,32 @@ extern crate pyo3;
use pyo3::prelude::*; use pyo3::prelude::*;
#[pyclass(module = "unit_converter")]
struct Temperature {
celsius: f32,
}
#[pymethods]
impl Temperature {
#[new]
fn new(obj: &PyRawObject, temperature: f32) {
obj.init(Temperature {
celsius: temperature,
});
}
fn to_fahrenheit(&self) -> f32 {
self.celsius * 1.8 + 32.0
}
fn windchill(&self, wind_speed_kph: f32) -> f32 {
13.12 + (0.6215 * self.celsius) - (11.37 * wind_speed_kph.powf(0.16))
+ (0.3965 * self.celsius * wind_speed_kph.powf(0.16))
}
}
// TODO: Add a test that -20 C and 32 kph feels like -32.9
#[pyfunction] #[pyfunction]
fn convert_to_fahrenheit(celsius: f32) -> f32 { fn convert_to_fahrenheit(celsius: f32) -> f32 {
celsius * 1.8 + 32.0 celsius * 1.8 + 32.0
@ -22,6 +48,7 @@ fn batch_convert_to_fahrenheit(celsius: Vec<f32>) -> Vec<f32> {
fn unit_converter(_py: Python, m: &PyModule) -> PyResult<()> { fn unit_converter(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_wrapped(wrap_pyfunction!(convert_to_fahrenheit))?; m.add_wrapped(wrap_pyfunction!(convert_to_fahrenheit))?;
m.add_wrapped(wrap_pyfunction!(batch_convert_to_fahrenheit))?; m.add_wrapped(wrap_pyfunction!(batch_convert_to_fahrenheit))?;
m.add_class::<Temperature>()?;
Ok(()) Ok(())
} }