diff --git a/notebooks/Untitled.ipynb b/notebooks/Untitled.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..425b499b51682423f7314d8619da3a8b174397e5 --- /dev/null +++ b/notebooks/Untitled.ipynb @@ -0,0 +1,195 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import ale\n", + "from ale import load" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "label = \"\"\"\n", + " PDS_VERSION_ID = PDS3\n", + "\n", + " /*FILE CHARACTERISTICS*/\n", + " RECORD_TYPE = FIXED_LENGTH\n", + " RECORD_BYTES = 5064\n", + " FILE_RECORDS = 13313\n", + " LABEL_RECORDS = 1\n", + " ^IMAGE = 2\n", + "\n", + " /*DATA IDENTIFICATION*/\n", + " DATA_SET_ID = \"LRO-L-LROC-2-EDR-V1.0\"\n", + " ORIGINAL_PRODUCT_ID = nacl0002fc60\n", + " PRODUCT_ID = M128963531LE\n", + " MISSION_NAME = \"LUNAR RECONNAISSANCE ORBITER\"\n", + " MISSION_PHASE_NAME = \"NOMINAL MISSION\"\n", + " INSTRUMENT_HOST_NAME = \"LUNAR RECONNAISSANCE ORBITER\"\n", + " INSTRUMENT_HOST_ID = LRO\n", + " INSTRUMENT_NAME = \"LUNAR RECONNAISSANCE ORBITER CAMERA\"\n", + " INSTRUMENT_ID = LROC\n", + " LRO:PREROLL_TIME = 2010-05-20T02:57:44.373\n", + " START_TIME = 2010-05-20T02:57:44.720\n", + " STOP_TIME = 2010-05-20T02:57:49.235\n", + " LRO:SPACECRAFT_CLOCK_PREROLL_COUNT = \"1/296017064:22937\"\n", + " SPACECRAFT_CLOCK_START_COUNT = \"1/296017064:45694\"\n", + " SPACECRAFT_CLOCK_STOP_COUNT = \"1/296017069:13866\"\n", + " ORBIT_NUMBER = 4138\n", + " PRODUCER_ID = LRO_LROC_TEAM\n", + " PRODUCT_CREATION_TIME = 2013-09-16T19:57:12\n", + " PRODUCER_INSTITUTION_NAME = \"ARIZONA STATE UNIVERSITY\"\n", + " PRODUCT_TYPE = EDR\n", + " PRODUCT_VERSION_ID = \"v1.8\"\n", + " UPLOAD_ID = \"SC_2010140_0000_A_V01.txt\"\n", + "\n", + " /*DATA DESCRIPTION*/\n", + " TARGET_NAME = \"MOON\"\n", + " RATIONALE_DESC = \"TARGET OF OPPORTUNITY\"\n", + " FRAME_ID = LEFT\n", + " DATA_QUALITY_ID = \"0\"\n", + " DATA_QUALITY_DESC = \"The DATA_QUALITY_ID is set to an 8-bit\n", + " value that encodes the following data quality information for the\n", + " observation. For each bit a value of 0 means FALSE and a value of 1 means\n", + " TRUE. More information about the data quality ID can be found in the LROC\n", + " EDR/CDR SIS, section 3.3 'Label and Header Descriptions'.\n", + " Bit 1: Temperature of focal plane array is out of bounds.\n", + " Bit 2: Threshold for saturated pixels is reached.\n", + " Bit 3: Threshold for under-saturated pixels is reached.\n", + " Bit 4: Observation is missing telemetry packets.\n", + " Bit 5: SPICE information is bad or missing.\n", + " Bit 6: Observation or housekeeping information is bad or missing.\n", + " Bit 7: Spare.\n", + " Bit 8: Spare.\"\n", + "\n", + " /*ENVIRONMENT*/\n", + " LRO:TEMPERATURE_SCS = 4.51 \n", + " LRO:TEMPERATURE_FPA = 17.88 \n", + " LRO:TEMPERATURE_FPGA = -12.33 \n", + " LRO:TEMPERATURE_TELESCOPE = 5.91 \n", + " LRO:TEMPERATURE_SCS_RAW = 2740\n", + " LRO:TEMPERATURE_FPA_RAW = 2107\n", + " LRO:TEMPERATURE_FPGA_RAW = 3418\n", + " LRO:TEMPERATURE_TELESCOPE_RAW = 2675\n", + "\n", + " /*IMAGING PARAMETERS*/\n", + " CROSSTRACK_SUMMING = 1\n", + " BANDWIDTH = 300 \n", + " CENTER_FILTER_WAVELENGTH = 600 \n", + " LINE_EXPOSURE_DURATION = 0.337600 \n", + " LRO:LINE_EXPOSURE_CODE = 0\n", + " LRO:DAC_RESET_LEVEL = 198\n", + " LRO:CHANNEL_A_OFFSET = 60\n", + " LRO:CHANNEL_B_OFFSET = 123\n", + " LRO:COMPAND_CODE = 3\n", + " LRO:LINE_CODE = 13\n", + " LRO:BTERM = (0,16,69,103,128)\n", + " LRO:MTERM = (0.5,0.25,0.125,0.0625,0.03125)\n", + " LRO:XTERM = (0,64,424,536,800)\n", + " LRO:COMPRESSION_FLAG = 1\n", + " LRO:MODE = 7\n", + "\n", + " /*DATA OBJECT*/\n", + " OBJECT = IMAGE\n", + " LINES = 13312\n", + " LINE_SAMPLES = 5064\n", + " SAMPLE_BITS = 8\n", + " SAMPLE_TYPE = LSB_INTEGER\n", + " UNIT = \"RAW_INSTRUMENT_COUNT\"\n", + " MD5_CHECKSUM = \"0fe91f4b2e93083ee0093e7c8d05f3bc\"\n", + " END_OBJECT = IMAGE\n", + " END\n", + " \"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "TRYING: \n", + "TRYING: \n", + "TRYING: \n", + "TRYING: \n", + "IN FURNISH\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Traceback (most recent call last):\n", + " File \"/Users/krodriguez-pr/repos/ale/ale/drivers/__init__.py\", line 25, in load\n", + " with res as r:\n", + " File \"/Users/krodriguez-pr/repos/ale/ale/drivers/base.py\", line 30, in __enter__\n", + " if self.metakernel:\n", + " File \"/Users/krodriguez-pr/repos/ale/ale/drivers/lro_driver.py\", line 17, in metakernel\n", + " self._metakernel = metakernels['data'][0]['path']\n", + "IndexError: list index out of range\n" + ] + }, + { + "ename": "Exception", + "evalue": "No Such Driver for Label", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mException\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mload\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlabel\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m~/repos/ale/ale/drivers/__init__.py\u001b[0m in \u001b[0;36mload\u001b[0;34m(label)\u001b[0m\n\u001b[1;32m 29\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mtraceback\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 30\u001b[0m \u001b[0mtraceback\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mprint_exc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 31\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'No Such Driver for Label'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mException\u001b[0m: No Such Driver for Label" + ] + } + ], + "source": [ + "load(label)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/notebooks/cpp_load.ipynb b/notebooks/cpp_load.ipynb index 40c8874edd2f819c95727b6c1591288e1eb4eb19..66040ad0b57ec920e48eeaf83713cb0e038845ec 100644 --- a/notebooks/cpp_load.ipynb +++ b/notebooks/cpp_load.ipynb @@ -2,134 +2,246 @@ "cells": [ { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ - "#pragma cling add_include_path(\"/Users/krodriguez-pr/repos/ale/include/\")\n", - "#pragma cling add_include_path(\"/Users/krodriguez-pr/repos/ale/include/json\")\n", - "#pragma cling add_include_path(\"/Users/krodriguez-pr/anaconda3/envs/ale/include/python3.7m\")\n", + "#pragma cling add_include_path(\"/home/krodriguez/repos/ale/include/\")\n", + "#pragma cling add_include_path(\"/home/krodriguez/repos/ale/include/json\")\n", + "#pragma cling add_include_path(\"/data/big/anaconda3/envs/ale/include/python3.7m\")\n", "\n", - "#pragma cling load(\"/Users/krodriguez-pr/repos/ale/build/libale.dylib\")" + "#pragma cling load(\"/home/krodriguez/repos/ale/build/libale.so\")" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "#include \"Python.h\"\n", "#include \"ale.h\"\n", "\n", + "#include \n", + "#include \n", + "\n", "std::string test_lro_label =\n", - " \"PDS_VERSION_ID = PDS3\"\n", - " \"\"\n", - " \"/*FILE CHARACTERISTICS*/\"\n", - " \"RECORD_TYPE = FIXED_LENGTH\"\n", - " \"RECORD_BYTES = 5064\"\n", - " \"FILE_RECORDS = 13313\"\n", - " \"LABEL_RECORDS = 1\"\n", - " \"^IMAGE = 2\"\n", - " \"\"\n", - " \"/*DATA IDENTIFICATION*/\"\n", - " \"DATA_SET_ID = \\\"LRO-L-LROC-2-EDR-V1.0\\\"\"\n", - " \"ORIGINAL_PRODUCT_ID = nacl0002fc60\"\n", - " \"PRODUCT_ID = M128963531LE\"\n", - " \"MISSION_NAME = \\\"LUNAR RECONNAISSANCE ORBITER\\\"\"\n", - " \"MISSION_PHASE_NAME = \\\"NOMINAL MISSION\\\"\"\n", - " \"INSTRUMENT_HOST_NAME = \\\"LUNAR RECONNAISSANCE ORBITER\\\"\"\n", - " \"INSTRUMENT_HOST_ID = LRO\"\n", - " \"INSTRUMENT_NAME = \\\"LUNAR RECONNAISSANCE ORBITER CAMERA\\\"\"\n", - " \"INSTRUMENT_ID = LROC\"\n", - " \"LRO:PREROLL_TIME = 2010-05-20T02:57:44.373\"\n", - " \"START_TIME = 2010-05-20T02:57:44.720\"\n", - " \"STOP_TIME = 2010-05-20T02:57:49.235\"\n", - " \"LRO:SPACECRAFT_CLOCK_PREROLL_COUNT = \\\"1/296017064:22937\\\"\"\n", - " \"SPACECRAFT_CLOCK_START_COUNT = \\\"1/296017064:45694\\\"\"\n", - " \"SPACECRAFT_CLOCK_STOP_COUNT = \\\"1/296017069:13866\\\"\"\n", - " \"ORBIT_NUMBER = 4138\"\n", - " \"PRODUCER_ID = LRO_LROC_TEAM\"\n", - " \"PRODUCT_CREATION_TIME = 2013-09-16T19:57:12\"\n", - " \"PRODUCER_INSTITUTION_NAME = \\\"ARIZONA STATE UNIVERSITY\\\"\"\n", - " \"PRODUCT_TYPE = EDR\"\n", - " \"PRODUCT_VERSION_ID = \\\"v1.8\\\"\"\n", - " \"UPLOAD_ID = \\\"SC_2010140_0000_A_V01.txt\\\"\"\n", - " \"\"\n", - " \"/*DATA DESCRIPTION*/\"\n", - " \"TARGET_NAME = \\\"MOON\\\"\"\n", - " \"RATIONALE_DESC = \\\"TARGET OF OPPORTUNITY\\\"\"\n", - " \"FRAME_ID = LEFT\"\n", - " \"DATA_QUALITY_ID = \\\"0\\\"\"\n", - " \"DATA_QUALITY_DESC = \\\"The DATA_QUALITY_ID is set to an 8-bit\"\n", - " \" value that encodes the following data quality information for the\"\n", - " \" observation. For each bit a value of 0 means FALSE and a value of 1 means\"\n", - " \" TRUE. More information about the data quality ID can be found in the LROC\"\n", - " \" EDR/CDR SIS, section 3.3 'Label and Header Descriptions'.\"\n", - " \" Bit 1: Temperature of focal plane array is out of bounds.\"\n", - " \" Bit 2: Threshold for saturated pixels is reached.\"\n", - " \" Bit 3: Threshold for under-saturated pixels is reached.\"\n", - " \" Bit 4: Observation is missing telemetry packets.\"\n", - " \" Bit 5: SPICE information is bad or missing.\"\n", - " \" Bit 6: Observation or housekeeping information is bad or missing.\"\n", - " \" Bit 7: Spare.\"\n", - " \" Bit 8: Spare.\\\"\"\n", - " \"\"\n", - " \"/*ENVIRONMENT*/\"\n", - " \"LRO:TEMPERATURE_SCS = 4.51 \"\n", - " \"LRO:TEMPERATURE_FPA = 17.88 \"\n", - " \"LRO:TEMPERATURE_FPGA = -12.33 \"\n", - " \"LRO:TEMPERATURE_TELESCOPE = 5.91 \"\n", - " \"LRO:TEMPERATURE_SCS_RAW = 2740\"\n", - " \"LRO:TEMPERATURE_FPA_RAW = 2107\"\n", - " \"LRO:TEMPERATURE_FPGA_RAW = 3418\"\n", - " \"LRO:TEMPERATURE_TELESCOPE_RAW = 2675\"\n", - " \"\"\n", - " \"/*IMAGING PARAMETERS*/\"\n", - " \"CROSSTRACK_SUMMING = 1\"\n", - " \"BANDWIDTH = 300 \"\n", - " \"CENTER_FILTER_WAVELENGTH = 600 \"\n", - " \"LINE_EXPOSURE_DURATION = 0.337600 \"\n", - " \"LRO:LINE_EXPOSURE_CODE = 0\"\n", - " \"LRO:DAC_RESET_LEVEL = 198\"\n", - " \"LRO:CHANNEL_A_OFFSET = 60\"\n", - " \"LRO:CHANNEL_B_OFFSET = 123\"\n", - " \"LRO:COMPAND_CODE = 3\"\n", - " \"LRO:LINE_CODE = 13\"\n", - " \"LRO:BTERM = (0,16,69,103,128)\"\n", - " \"LRO:MTERM = (0.5,0.25,0.125,0.0625,0.03125)\"\n", - " \"LRO:XTERM = (0,64,424,536,800)\"\n", - " \"LRO:COMPRESSION_FLAG = 1\"\n", - " \"LRO:MODE = 7\"\n", - " \"\"\n", - " \"/*DATA OBJECT*/\"\n", - " \"OBJECT = IMAGE\"\n", - " \" LINES = 13312\"\n", - " \" LINE_SAMPLES = 5064\"\n", - " \" SAMPLE_BITS = 8\"\n", - " \" SAMPLE_TYPE = LSB_INTEGER\"\n", - " \" UNIT = \\\"RAW_INSTRUMENT_COUNT\\\"\"\n", - " \" MD5_CHECKSUM = \\\"0fe91f4b2e93083ee0093e7c8d05f3bc\\\"\"\n", - " \"END_OBJECT = IMAGE\"\n", - " \"END\";" + " \"PDS_VERSION_ID = PDS3\\n\"\n", + " \"\\n\"\n", + " \"/*FILE CHARACTERISTICS*/\\n\"\n", + " \"RECORD_TYPE = FIXED_LENGTH\\n\"\n", + " \"RECORD_BYTES = 5064\\n\"\n", + " \"FILE_RECORDS = 13313\\n\"\n", + " \"LABEL_RECORDS = 1\\n\"\n", + " \"^IMAGE = 2\\n\"\n", + " \"\\n\"\n", + " \"/*DATA IDENTIFICATION*/\\n\"\n", + " \"DATA_SET_ID = \\\"LRO-L-LROC-2-EDR-V1.0\\\"\\n\"\n", + " \"ORIGINAL_PRODUCT_ID = nacl0002fc60\\n\"\n", + " \"PRODUCT_ID = M128963531LE\\n\"\n", + " \"MISSION_NAME = \\\"LUNAR RECONNAISSANCE ORBITER\\\"\\n\"\n", + " \"MISSION_PHASE_NAME = \\\"NOMINAL MISSION\\\"\\n\"\n", + " \"INSTRUMENT_HOST_NAME = \\\"LUNAR RECONNAISSANCE ORBITER\\\"\\n\"\n", + " \"INSTRUMENT_HOST_ID = LRO\\n\"\n", + " \"INSTRUMENT_NAME = \\\"LUNAR RECONNAISSANCE ORBITER CAMERA\\\"\\n\"\n", + " \"INSTRUMENT_ID = LROC\\n\"\n", + " \"LRO:PREROLL_TIME = 2010-05-20T02:57:44.373\\n\"\n", + " \"START_TIME = 2010-05-20T02:57:44.720\\n\"\n", + " \"STOP_TIME = 2010-05-20T02:57:49.235\\n\"\n", + " \"LRO:SPACECRAFT_CLOCK_PREROLL_COUNT = \\\"1/296017064:22937\\\"\\n\"\n", + " \"SPACECRAFT_CLOCK_START_COUNT = \\\"1/296017064:45694\\\"\\n\"\n", + " \"SPACECRAFT_CLOCK_STOP_COUNT = \\\"1/296017069:13866\\\"\\n\"\n", + " \"ORBIT_NUMBER = 4138\\n\"\n", + " \"PRODUCER_ID = LRO_LROC_TEAM\\n\"\n", + " \"PRODUCT_CREATION_TIME = 2013-09-16T19:57:12\\n\"\n", + " \"PRODUCER_INSTITUTION_NAME = \\\"ARIZONA STATE UNIVERSITY\\\"\\n\"\n", + " \"PRODUCT_TYPE = EDR\\n\"\n", + " \"PRODUCT_VERSION_ID = \\\"v1.8\\\"\\n\"\n", + " \"UPLOAD_ID = \\\"SC_2010140_0000_A_V01.txt\\\"\\n\"\n", + " \"\\n\"\n", + " \"/*DATA DESCRIPTION*/\\n\"\n", + " \"TARGET_NAME = \\\"MOON\\\"\\n\"\n", + " \"RATIONALE_DESC = \\\"TARGET OF OPPORTUNITY\\\"\\n\"\n", + " \"FRAME_ID = LEFT\\n\"\n", + " \"DATA_QUALITY_ID = \\\"0\\\"\\n\"\n", + " \"DATA_QUALITY_DESC = \\\"The DATA_QUALITY_ID is set to an 8-bit\\n\"\n", + " \" value that encodes the following data quality information for the\\n\"\n", + " \" observation. For each bit a value of 0 means FALSE and a value of 1 means\\n\"\n", + " \" TRUE. More information about the data quality ID can be found in the LROC\\n\"\n", + " \" EDR/CDR SIS, section 3.3 'Label and Header Descriptions'.\\n\"\n", + " \" Bit 1: Temperature of focal plane array is out of bounds.\\n\"\n", + " \" Bit 2: Threshold for saturated pixels is reached.\\n\"\n", + " \" Bit 3: Threshold for under-saturated pixels is reached.\\n\"\n", + " \" Bit 4: Observation is missing telemetry packets.\\n\"\n", + " \" Bit 5: SPICE information is bad or missing.\\n\"\n", + " \" Bit 6: Observation or housekeeping information is bad or missing.\\n\"\n", + " \" Bit 7: Spare.\\n\"\n", + " \" Bit 8: Spare.\\\"\\n\"\n", + " \"\\n\"\n", + " \"/*ENVIRONMENT*/\\n\"\n", + " \"LRO:TEMPERATURE_SCS = 4.51 \\n\"\n", + " \"LRO:TEMPERATURE_FPA = 17.88 \\n\"\n", + " \"LRO:TEMPERATURE_FPGA = -12.33 \\n\"\n", + " \"LRO:TEMPERATURE_TELESCOPE = 5.91 \\n\"\n", + " \"LRO:TEMPERATURE_SCS_RAW = 2740\\n\"\n", + " \"LRO:TEMPERATURE_FPA_RAW = 2107\\n\"\n", + " \"LRO:TEMPERATURE_FPGA_RAW = 3418\\n\"\n", + " \"LRO:TEMPERATURE_TELESCOPE_RAW = 2675\\n\"\n", + " \"\\n\"\n", + " \"/*IMAGING PARAMETERS*/\\n\"\n", + " \"CROSSTRACK_SUMMING = 1\\n\"\n", + " \"BANDWIDTH = 300 \\n\"\n", + " \"CENTER_FILTER_WAVELENGTH = 600 \\n\"\n", + " \"LINE_EXPOSURE_DURATION = 0.337600 \\n\"\n", + " \"LRO:LINE_EXPOSURE_CODE = 0\\n\"\n", + " \"LRO:DAC_RESET_LEVEL = 198\\n\"\n", + " \"LRO:CHANNEL_A_OFFSET = 60\\n\"\n", + " \"LRO:CHANNEL_B_OFFSET = 123\\n\"\n", + " \"LRO:COMPAND_CODE = 3\\n\"\n", + " \"LRO:LINE_CODE = 13\\n\"\n", + " \"LRO:BTERM = (0,16,69,103,128)\\n\"\n", + " \"LRO:MTERM = (0.5,0.25,0.125,0.0625,0.03125)\\n\"\n", + " \"LRO:XTERM = (0,64,424,536,800)\\n\"\n", + " \"LRO:COMPRESSION_FLAG = 1\\n\"\n", + " \"LRO:MODE = 7\\n\"\n", + " \"\\n\"\n", + " \"/*DATA OBJECT*/\\n\"\n", + " \"OBJECT = IMAGE\\n\"\n", + " \" LINES = 13312\\n\"\n", + " \" LINE_SAMPLES = 5064\\n\"\n", + " \" SAMPLE_BITS = 8\\n\"\n", + " \" SAMPLE_TYPE = LSB_INTEGER\\n\"\n", + " \" UNIT = \\\"RAW_INSTRUMENT_COUNT\\\"\\n\"\n", + " \" MD5_CHECKSUM = \\\"0fe91f4b2e93083ee0093e7c8d05f3bc\\\"\\n\"\n", + " \"END_OBJECT = IMAGE\\n\"\n", + " \"END\\n\";\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "std::string load(std::string filename) {\n", + " static bool first_run = true;\n", + " if(first_run) {\n", + " // Initialize the Python interpreter but only once.\n", + " first_run = !first_run;\n", + " Py_Initialize();\n", + " atexit(Py_Finalize);\n", + " }\n", + " \n", + " // Import the file as a Python module.\n", + " PyObject *pModule = PyImport_Import(PyUnicode_FromString(\"ale\"));\n", + "\n", + " // Create a dictionary for the contents of the module.\n", + " PyObject *pDict = PyModule_GetDict(pModule);\n", + "\n", + " // Get the add method from the dictionary.\n", + " PyObject *pFunc = PyDict_GetItemString(pDict, \"loads\");\n", + "\n", + " // Create a Python tuple to hold the arguments to the method.\n", + " PyObject *pArgs = PyTuple_New(1);\n", + "\n", + " // Set the Python int as the first and second arguments to the method.\n", + " PyObject *pString = PyUnicode_FromString(filename.c_str());\n", + " PyTuple_SetItem(pArgs, 0, pString);\n", + "\n", + " // Call the function with the arguments.\n", + " PyObject* pResult = PyObject_CallObject(pFunc, pArgs);\n", + "\n", + " // Print a message if calling the method failed.\n", + " if(pResult == NULL) {\n", + " throw \"pResult\";\n", + " }\n", + "\n", + " std::string cResult;\n", + "\n", + " PyObject *temp_bytes = PyUnicode_AsUTF8String(pResult); // Owned reference\n", + " if (temp_bytes != NULL) {\n", + " char *temp_str = PyBytes_AS_STRING(temp_bytes); // Borrowed pointer\n", + " cResult = temp_str; // copy into std::string\n", + " Py_DECREF(temp_str); \n", + " } else {\n", + " throw \"pybytes\";\n", + " }\n", + " \n", + " Py_DECREF(temp_bytes);\n", + " Py_DECREF(pArgs);\n", + " Py_DECREF(pModule);\n", + " Py_DECREF(pString);\n", + " \n", + " return cResult;\n", + "}" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, + "outputs": [], + "source": [ + "std::string label;" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "label = load(test_lro_label);" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "// label" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "using json = nlohmann::json;\n", + "json jlabel = json::parse(label)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "std::vector orientations = jlabel.at(\"sensor_orientation\").at(0).get>()" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, "outputs": [ { - "ename": "Standard Exception", - "evalue": "Calling the load method failed.", - "output_type": "error", - "traceback": [ - "Standard Exception: Calling the load method failed." - ] + "data": { + "text/plain": [ + "{ 0.622216, 0.274174, -0.668159, 0.30206 }" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "ale::load(test_lro_label);" + "orientations" ] }, { diff --git a/src/ale.cpp b/src/ale.cpp index 27e4e4127a507da2bf5988c622ffa9f722651cc3..5d5c7caf6c2e03f40daf138523ca4e0c7e8c2b36 100644 --- a/src/ale.cpp +++ b/src/ale.cpp @@ -263,50 +263,55 @@ namespace ale { 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); -} + std::string load(std::string filename) { + static bool first_run = true; + if(first_run) { + // Initialize the Python interpreter but only once. + first_run = !first_run; + Py_Initialize(); + atexit(Py_Finalize); + } + + // 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, "loads"); + + // 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. + PyObject *pString = PyUnicode_FromString(filename.c_str()); + PyTuple_SetItem(pArgs, 0, pString); + + // Call the function with the arguments. + PyObject* pResult = PyObject_CallObject(pFunc, pArgs); + + // Print a message if calling the method failed. + if(pResult == NULL) { + throw "pResult"; + } + + std::string cResult; + + PyObject *temp_bytes = PyUnicode_AsUTF8String(pResult); // Owned reference + if (temp_bytes != NULL) { + char *temp_str = PyBytes_AS_STRING(temp_bytes); // Borrowed pointer + cResult = temp_str; // copy into std::string + Py_DECREF(temp_str); + } else { + throw "pybytes"; + } + + Py_DECREF(temp_bytes); + Py_DECREF(pArgs); + Py_DECREF(pModule); + Py_DECREF(pString); + + return cResult; + } }