Skip to content
Snippets Groups Projects
Commit 1eb0f89c authored by Kelvin Rodriguez's avatar Kelvin Rodriguez
Browse files

first draft of cpp python interface

parent 78270248
Branches
Tags
No related merge requests found
...@@ -20,6 +20,7 @@ set(CMAKE_CXX_STANDARD 11) ...@@ -20,6 +20,7 @@ set(CMAKE_CXX_STANDARD 11)
# Third Party Dependencies # Third Party Dependencies
find_package(GSL REQUIRED) find_package(GSL REQUIRED)
find_package(Eigen3 3.3 REQUIRED NO_MODULE) find_package(Eigen3 3.3 REQUIRED NO_MODULE)
find_package(PythonLibs REQUIRED COMPONENTS Development Interpreter Compiler)
# Library setup # Library setup
add_library(ale SHARED add_library(ale SHARED
...@@ -34,6 +35,7 @@ target_include_directories(ale ...@@ -34,6 +35,7 @@ target_include_directories(ale
PRIVATE PRIVATE
${GSL_INCLUDE_DIRS} ${GSL_INCLUDE_DIRS}
${EIGEN3_INCLUDE_DIR} ${EIGEN3_INCLUDE_DIR}
${PYTHON_INCLUDE_DIR}
PUBLIC PUBLIC
${ALE_INCLUDE_DIRS}) ${ALE_INCLUDE_DIRS})
...@@ -42,6 +44,7 @@ find_package (Threads) ...@@ -42,6 +44,7 @@ find_package (Threads)
target_link_libraries(ale target_link_libraries(ale
PRIVATE PRIVATE
${GSL_LIBRARIES} ${GSL_LIBRARIES}
${PYTHON_LIBRARY}
PUBLIC PUBLIC
gtest ${CMAKE_THREAD_LIBS_INIT}) gtest ${CMAKE_THREAD_LIBS_INIT})
......
from . import drivers from . import drivers
from .drivers import load
...@@ -40,6 +40,7 @@ namespace ale { ...@@ -40,6 +40,7 @@ namespace ale {
std::vector<double> coefficients, double time); std::vector<double> coefficients, double time);
double evaluatePolynomial(std::vector<double> coeffs, double time, int d); double evaluatePolynomial(std::vector<double> coeffs, double time, int d);
double interpolate(std::vector<double> points, std::vector<double> times, double time, interpolation interp, int d); double interpolate(std::vector<double> points, std::vector<double> times, double time, interpolation interp, int d);
std::string load(std::string filename);
} }
#endif // ALE_H #endif // ALE_H
%% Cell type:code id: tags:
``` C++11
#pragma cling add_include_path("/Users/krodriguez-pr/repos/ale/include/")
#pragma cling add_include_path("/Users/krodriguez-pr/repos/ale/include/json")
#pragma cling add_include_path("/Users/krodriguez-pr/anaconda3/envs/ale/include/python3.7m")
#pragma cling load("/Users/krodriguez-pr/repos/ale/build/libale.dylib")
```
%% Cell type:code id: tags:
``` C++11
#include "Python.h"
#include "ale.h"
std::string test_lro_label =
"PDS_VERSION_ID = PDS3"
""
"/*FILE CHARACTERISTICS*/"
"RECORD_TYPE = FIXED_LENGTH"
"RECORD_BYTES = 5064"
"FILE_RECORDS = 13313"
"LABEL_RECORDS = 1"
"^IMAGE = 2"
""
"/*DATA IDENTIFICATION*/"
"DATA_SET_ID = \"LRO-L-LROC-2-EDR-V1.0\""
"ORIGINAL_PRODUCT_ID = nacl0002fc60"
"PRODUCT_ID = M128963531LE"
"MISSION_NAME = \"LUNAR RECONNAISSANCE ORBITER\""
"MISSION_PHASE_NAME = \"NOMINAL MISSION\""
"INSTRUMENT_HOST_NAME = \"LUNAR RECONNAISSANCE ORBITER\""
"INSTRUMENT_HOST_ID = LRO"
"INSTRUMENT_NAME = \"LUNAR RECONNAISSANCE ORBITER CAMERA\""
"INSTRUMENT_ID = LROC"
"LRO:PREROLL_TIME = 2010-05-20T02:57:44.373"
"START_TIME = 2010-05-20T02:57:44.720"
"STOP_TIME = 2010-05-20T02:57:49.235"
"LRO:SPACECRAFT_CLOCK_PREROLL_COUNT = \"1/296017064:22937\""
"SPACECRAFT_CLOCK_START_COUNT = \"1/296017064:45694\""
"SPACECRAFT_CLOCK_STOP_COUNT = \"1/296017069:13866\""
"ORBIT_NUMBER = 4138"
"PRODUCER_ID = LRO_LROC_TEAM"
"PRODUCT_CREATION_TIME = 2013-09-16T19:57:12"
"PRODUCER_INSTITUTION_NAME = \"ARIZONA STATE UNIVERSITY\""
"PRODUCT_TYPE = EDR"
"PRODUCT_VERSION_ID = \"v1.8\""
"UPLOAD_ID = \"SC_2010140_0000_A_V01.txt\""
""
"/*DATA DESCRIPTION*/"
"TARGET_NAME = \"MOON\""
"RATIONALE_DESC = \"TARGET OF OPPORTUNITY\""
"FRAME_ID = LEFT"
"DATA_QUALITY_ID = \"0\""
"DATA_QUALITY_DESC = \"The DATA_QUALITY_ID is set to an 8-bit"
" value that encodes the following data quality information for the"
" observation. For each bit a value of 0 means FALSE and a value of 1 means"
" TRUE. More information about the data quality ID can be found in the LROC"
" EDR/CDR SIS, section 3.3 'Label and Header Descriptions'."
" Bit 1: Temperature of focal plane array is out of bounds."
" Bit 2: Threshold for saturated pixels is reached."
" Bit 3: Threshold for under-saturated pixels is reached."
" Bit 4: Observation is missing telemetry packets."
" Bit 5: SPICE information is bad or missing."
" Bit 6: Observation or housekeeping information is bad or missing."
" Bit 7: Spare."
" Bit 8: Spare.\""
""
"/*ENVIRONMENT*/"
"LRO:TEMPERATURE_SCS = 4.51 <degC>"
"LRO:TEMPERATURE_FPA = 17.88 <degC>"
"LRO:TEMPERATURE_FPGA = -12.33 <degC>"
"LRO:TEMPERATURE_TELESCOPE = 5.91 <degC>"
"LRO:TEMPERATURE_SCS_RAW = 2740"
"LRO:TEMPERATURE_FPA_RAW = 2107"
"LRO:TEMPERATURE_FPGA_RAW = 3418"
"LRO:TEMPERATURE_TELESCOPE_RAW = 2675"
""
"/*IMAGING PARAMETERS*/"
"CROSSTRACK_SUMMING = 1"
"BANDWIDTH = 300 <nm>"
"CENTER_FILTER_WAVELENGTH = 600 <nm>"
"LINE_EXPOSURE_DURATION = 0.337600 <ms>"
"LRO:LINE_EXPOSURE_CODE = 0"
"LRO:DAC_RESET_LEVEL = 198"
"LRO:CHANNEL_A_OFFSET = 60"
"LRO:CHANNEL_B_OFFSET = 123"
"LRO:COMPAND_CODE = 3"
"LRO:LINE_CODE = 13"
"LRO:BTERM = (0,16,69,103,128)"
"LRO:MTERM = (0.5,0.25,0.125,0.0625,0.03125)"
"LRO:XTERM = (0,64,424,536,800)"
"LRO:COMPRESSION_FLAG = 1"
"LRO:MODE = 7"
""
"/*DATA OBJECT*/"
"OBJECT = IMAGE"
" LINES = 13312"
" LINE_SAMPLES = 5064"
" SAMPLE_BITS = 8"
" SAMPLE_TYPE = LSB_INTEGER"
" UNIT = \"RAW_INSTRUMENT_COUNT\""
" MD5_CHECKSUM = \"0fe91f4b2e93083ee0093e7c8d05f3bc\""
"END_OBJECT = IMAGE"
"END";
```
%% Cell type:code id: tags:
``` C++11
ale::load(test_lro_label);
```
%% Output
Standard Exception: Calling the load method failed.
%% Cell type:code id: tags:
``` C++11
```
...@@ -9,6 +9,9 @@ ...@@ -9,6 +9,9 @@
#include <Eigen/Core> #include <Eigen/Core>
#include <Eigen/Geometry> #include <Eigen/Geometry>
#include <iostream>
#include <Python.h>
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <stdexcept> #include <stdexcept>
...@@ -260,4 +263,50 @@ namespace ale { ...@@ -260,4 +263,50 @@ namespace ale {
return result; return result;
} }
std::string load(std::string filename) {
// Initialize the Python interpreter.
Py_Initialize();
// Import the file as a Python module.
PyObject *pModule = PyImport_Import(PyUnicode_FromString("ale"));
// Create a dictionary for the contents of the module.
PyObject *pDict = PyModule_GetDict(pModule);
// Get the add method from the dictionary.
PyObject *pFunc = PyDict_GetItemString(pDict, "load");
// Create a Python tuple to hold the arguments to the method.
PyObject *pArgs = PyTuple_New(1);
// Set the Python int as the first and second arguments to the method.
PyTuple_SetItem(pArgs, 0, PyUnicode_FromString(filename.c_str()));
// Call the function with the arguments.
PyObject* pResult = PyObject_CallObject(pFunc, pArgs);
// Print a message if calling the method failed.
if(pResult == NULL) {
throw invalid_argument("Calling the load method failed.");
}
char *cResult = 0;
if (PyUnicode_Check(pResult)) {
PyObject * temp_bytes = PyUnicode_AsEncodedString(pResult, "UTF-8", "strict"); // Owned reference
if (temp_bytes != NULL) {
cResult = PyBytes_AS_STRING(temp_bytes); // Borrowed pointer
cResult = strdup(cResult);
Py_DECREF(temp_bytes);
} else {
// some error handling
}
}
Py_Finalize();
return std::string(cResult);
}
} }
...@@ -253,3 +253,101 @@ TEST(LinearInterpTest, GetRotationDifferentCounts) { ...@@ -253,3 +253,101 @@ TEST(LinearInterpTest, GetRotationDifferentCounts) {
EXPECT_THROW(ale::getRotation(rots, times, 2, ale::linear), invalid_argument); EXPECT_THROW(ale::getRotation(rots, times, 2, ale::linear), invalid_argument);
} }
TEST(LinearInterpTest, FileLoad) {
std::string test_lro_label =
"PDS_VERSION_ID = PDS3"
""
"/*FILE CHARACTERISTICS*/"
"RECORD_TYPE = FIXED_LENGTH"
"RECORD_BYTES = 5064"
"FILE_RECORDS = 13313"
"LABEL_RECORDS = 1"
"^IMAGE = 2"
""
"/*DATA IDENTIFICATION*/"
"DATA_SET_ID = \"LRO-L-LROC-2-EDR-V1.0\""
"ORIGINAL_PRODUCT_ID = nacl0002fc60"
"PRODUCT_ID = M128963531LE"
"MISSION_NAME = \"LUNAR RECONNAISSANCE ORBITER\""
"MISSION_PHASE_NAME = \"NOMINAL MISSION\""
"INSTRUMENT_HOST_NAME = \"LUNAR RECONNAISSANCE ORBITER\""
"INSTRUMENT_HOST_ID = LRO"
"INSTRUMENT_NAME = \"LUNAR RECONNAISSANCE ORBITER CAMERA\""
"INSTRUMENT_ID = LROC"
"LRO:PREROLL_TIME = 2010-05-20T02:57:44.373"
"START_TIME = 2010-05-20T02:57:44.720"
"STOP_TIME = 2010-05-20T02:57:49.235"
"LRO:SPACECRAFT_CLOCK_PREROLL_COUNT = \"1/296017064:22937\""
"SPACECRAFT_CLOCK_START_COUNT = \"1/296017064:45694\""
"SPACECRAFT_CLOCK_STOP_COUNT = \"1/296017069:13866\""
"ORBIT_NUMBER = 4138"
"PRODUCER_ID = LRO_LROC_TEAM"
"PRODUCT_CREATION_TIME = 2013-09-16T19:57:12"
"PRODUCER_INSTITUTION_NAME = \"ARIZONA STATE UNIVERSITY\""
"PRODUCT_TYPE = EDR"
"PRODUCT_VERSION_ID = \"v1.8\""
"UPLOAD_ID = \"SC_2010140_0000_A_V01.txt\""
""
"/*DATA DESCRIPTION*/"
"TARGET_NAME = \"MOON\""
"RATIONALE_DESC = \"TARGET OF OPPORTUNITY\""
"FRAME_ID = LEFT"
"DATA_QUALITY_ID = \"0\""
"DATA_QUALITY_DESC = \"The DATA_QUALITY_ID is set to an 8-bit"
" value that encodes the following data quality information for the"
" observation. For each bit a value of 0 means FALSE and a value of 1 means"
" TRUE. More information about the data quality ID can be found in the LROC"
" EDR/CDR SIS, section 3.3 'Label and Header Descriptions'."
" Bit 1: Temperature of focal plane array is out of bounds."
" Bit 2: Threshold for saturated pixels is reached."
" Bit 3: Threshold for under-saturated pixels is reached."
" Bit 4: Observation is missing telemetry packets."
" Bit 5: SPICE information is bad or missing."
" Bit 6: Observation or housekeeping information is bad or missing."
" Bit 7: Spare."
" Bit 8: Spare.\""
""
"/*ENVIRONMENT*/"
"LRO:TEMPERATURE_SCS = 4.51 <degC>"
"LRO:TEMPERATURE_FPA = 17.88 <degC>"
"LRO:TEMPERATURE_FPGA = -12.33 <degC>"
"LRO:TEMPERATURE_TELESCOPE = 5.91 <degC>"
"LRO:TEMPERATURE_SCS_RAW = 2740"
"LRO:TEMPERATURE_FPA_RAW = 2107"
"LRO:TEMPERATURE_FPGA_RAW = 3418"
"LRO:TEMPERATURE_TELESCOPE_RAW = 2675"
""
"/*IMAGING PARAMETERS*/"
"CROSSTRACK_SUMMING = 1"
"BANDWIDTH = 300 <nm>"
"CENTER_FILTER_WAVELENGTH = 600 <nm>"
"LINE_EXPOSURE_DURATION = 0.337600 <ms>"
"LRO:LINE_EXPOSURE_CODE = 0"
"LRO:DAC_RESET_LEVEL = 198"
"LRO:CHANNEL_A_OFFSET = 60"
"LRO:CHANNEL_B_OFFSET = 123"
"LRO:COMPAND_CODE = 3"
"LRO:LINE_CODE = 13"
"LRO:BTERM = (0,16,69,103,128)"
"LRO:MTERM = (0.5,0.25,0.125,0.0625,0.03125)"
"LRO:XTERM = (0,64,424,536,800)"
"LRO:COMPRESSION_FLAG = 1"
"LRO:MODE = 7"
""
"/*DATA OBJECT*/"
"OBJECT = IMAGE"
" LINES = 13312"
" LINE_SAMPLES = 5064"
" SAMPLE_BITS = 8"
" SAMPLE_TYPE = LSB_INTEGER"
" UNIT = \"RAW_INSTRUMENT_COUNT\""
" MD5_CHECKSUM = \"0fe91f4b2e93083ee0093e7c8d05f3bc\""
"END_OBJECT = IMAGE"
"END";
cout << ale::load(test_lro_label) << endl;
}
...@@ -116,3 +116,5 @@ def test_lro_creation(lro_lroclabel): ...@@ -116,3 +116,5 @@ def test_lro_creation(lro_lroclabel):
with LRO_LROC(lro_lroclabel) as m: with LRO_LROC(lro_lroclabel) as m:
d = m.to_dict() d = m.to_dict()
assert isinstance(d, dict) assert isinstance(d, dict)
from ale import load
print(load(lro_lroclabel))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment