11#include " virtual_temperature.hpp"
22#include < pybind11/pybind11.h>
3+ #include < pybind11/numpy.h>
34#include < pybind11/stl.h>
45
56namespace py = pybind11;
67
7- // Flexible dispatcher that supports scalar/list combinations
8- std::vector<double > virtual_temperature_py (py::object temperature, py::object mixing_ratio, double epsilon) {
9- std::vector<double > temperature_vec;
10- std::vector<double > mixing_ratio_vec;
11-
12- // Case 1: both are lists
13- if (py::isinstance<py::list>(temperature) && py::isinstance<py::list>(mixing_ratio)) {
14- temperature_vec = temperature.cast <std::vector<double >>();
15- mixing_ratio_vec = mixing_ratio.cast <std::vector<double >>();
16- if (temperature_vec.size () != mixing_ratio_vec.size ()) {
17- throw std::invalid_argument (" Temperature and mixing ratio lists must be the same length." );
18- }
19- }
20- // Case 2: temperature is float, mixing_ratio is list
21- else if (py::isinstance<py::float_>(temperature) && py::isinstance<py::list>(mixing_ratio)) {
22- mixing_ratio_vec = mixing_ratio.cast <std::vector<double >>();
23- temperature_vec = std::vector<double >(mixing_ratio_vec.size (), temperature.cast <double >());
24- }
25- // Case 3: temperature is list, mixing_ratio is float
26- else if (py::isinstance<py::list>(temperature) && py::isinstance<py::float_>(mixing_ratio)) {
27- temperature_vec = temperature.cast <std::vector<double >>();
28- mixing_ratio_vec = std::vector<double >(temperature_vec.size (), mixing_ratio.cast <double >());
29- }
30- // Case 4: both are floats
31- else if (py::isinstance<py::float_>(temperature) && py::isinstance<py::float_>(mixing_ratio)) {
32- temperature_vec = {temperature.cast <double >()};
33- mixing_ratio_vec = {mixing_ratio.cast <double >()};
34- }
35- else {
36- throw std::invalid_argument (" Inputs must be float or list." );
37- }
38-
39- return VirtualTemperature (temperature_vec, mixing_ratio_vec, epsilon);
40- }
41-
428int add (int i, int j) {
43- return i - j;
9+ return i + j;
4410}
4511
4612PYBIND11_MODULE (_calc_mod, m) {
@@ -49,11 +15,11 @@ PYBIND11_MODULE(_calc_mod, m) {
4915 m.def (" add" , &add, " Add two numbers" );
5016
5117 // Unified binding with default epsilon
52- m.def (" virtual_temperature " , &virtual_temperature_py ,
53- py::arg ( " temperature " ), py::arg ( " mixing_ratio " ), py::arg ( " epsilon " ) = 0.622 ,
54- " Compute virtual temperature. \n "
55- " Accepts: \n "
56- " - two lists of equal length \n "
57- " - one scalar and one list \n "
58- " Defaults to epsilon = 0.622" );
18+ m.def (" dewpoint " , py::vectorize (DewPoint) ,
19+ " Calculate dew point from water vapor partial pressure. " ,
20+ py::arg ( " vapor_pressure " ));
21+
22+ m. def ( " virtual_temperature " , py::vectorize (VirtualTemperature),
23+ " Calculate virtual temperature from temperature and mixing ratio. " ,
24+ py::arg ( " temperature " ), py::arg ( " mixing_ratio " ), py::arg ( " epsilon" ) = 0.622 );
5925}
0 commit comments