From 88ebea70bdf6085a5f1a0548ef9b228a7cc15f6b Mon Sep 17 00:00:00 2001 From: Jesse Mapel Date: Tue, 11 Dec 2018 17:04:41 -0700 Subject: [PATCH] Added first steps at cube reading notebook (#58) --- .gitignore | 3 + notebooks/read_cube.ipynb | 433 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 436 insertions(+) create mode 100644 notebooks/read_cube.ipynb diff --git a/.gitignore b/.gitignore index 189b0c2..0d943eb 100644 --- a/.gitignore +++ b/.gitignore @@ -72,3 +72,6 @@ build */build install */install + +# Jupyter ignores +.ipynb_checkpoints diff --git a/notebooks/read_cube.ipynb b/notebooks/read_cube.ipynb new file mode 100644 index 0000000..f1f54b3 --- /dev/null +++ b/notebooks/read_cube.ipynb @@ -0,0 +1,433 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "import pvl, struct, quaternion\n", + "import numpy as np" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "def read_position_table(label, file):\n", + " with open(file, 'rb') as f:\n", + " f.seek(int(label['StartByte']))\n", + " data = f.read(int(label['Bytes']))\n", + " field_count = len(label.getlist('Field'))\n", + " record_count = int(label['Records'])\n", + " format = 'd' * field_count\n", + " positions = []\n", + " velocities = []\n", + " times = []\n", + " for index in range(record_count):\n", + " doubles = struct.unpack_from(format, data, index * field_count * 8)\n", + " positions.append(doubles[:3])\n", + " if field_count > 3:\n", + " times.append(doubles[-1])\n", + " if field_count == 7:\n", + " velocities.append(doubles[3:6])\n", + " return positions, velocities, times" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [], + "source": [ + "def read_rotation_table(label, file):\n", + " with open(file, 'rb') as f:\n", + " f.seek(int(label['StartByte']))\n", + " data = f.read(int(label['Bytes']))\n", + " field_count = len(label.getlist('Field'))\n", + " record_count = int(label['Records'])\n", + " format = 'd' * field_count\n", + " rotations = []\n", + " velocities = []\n", + " times = []\n", + " for index in range(record_count):\n", + " doubles = struct.unpack_from(format, data, index * field_count * 8)\n", + " # Euler angle functions\n", + " if field_count == 3:\n", + " rotations.append(doubles[:3])\n", + " else:\n", + " rotations.append(np.quaternion(doubles[0], doubles[1], doubles[2], doubles[3]))\n", + " times.append(doubles[-1])\n", + " if field_count == 8:\n", + " velocities.append(doubles[4:7])\n", + " \n", + " const_rotation_mat = np.array(label['ConstantRotation'])\n", + " const_rotation = quaternion.from_rotation_matrix(np.reshape(const_rotation_mat, (3, 3)))\n", + " return rotations, velocities, times, const_rotation" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "cube_file = '/Users/jmapel/eal/cubes/CAS-MCO-2016-11-22T16.38.39.354-NIR-02036-00.cub'" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[PVLObject([\n", + " ('Name', 'InstrumentPointing')\n", + " ('StartByte', 336115)\n", + " ('Bytes', 64)\n", + " ('Records', 1)\n", + " ('ByteOrder', 'Lsb')\n", + " ('TimeDependentFrames', [-143410, -143400, -143000, 1])\n", + " ('ConstantFrames', [-143420, -143410])\n", + " ('ConstantRotation',\n", + " [6.12323399573677e-17,\n", + " 0.0,\n", + " 1.0,\n", + " 0.98513931843935,\n", + " 0.17175716364932,\n", + " -6.03223856520476e-17,\n", + " -0.17175716364932,\n", + " 0.98513931843935,\n", + " 1.05170930346882e-17])\n", + " ('CkTableStartTime', 533104787.53776)\n", + " ('CkTableEndTime', 533104787.53776)\n", + " ('CkTableOriginalSize', 1)\n", + " ('FrameTypeCode', 3)\n", + " ('Description', 'Created by spiceinit')\n", + " ('Kernels',\n", + " ['$tgo/kernels/ck/em16_tgo_sc_fpp_d161102_pointing_20161028_20171129_s20160719_v01.bc',\n", + " '$tgo/kernels/ck/em16_tgo_cassis_ipp_tel_20160407_20170309_s20170116_v02.bc',\n", + " '$tgo/kernels/fk/em16_tgo_v12.tf',\n", + " '$tgo/kernels/fk/rssd0002.tf'])\n", + " ('Field',\n", + " PVLGroup([\n", + " ('Name', 'J2000Q0')\n", + " ('Type', 'Double')\n", + " ('Size', 1)\n", + " ]))\n", + " ('Field',\n", + " PVLGroup([\n", + " ('Name', 'J2000Q1')\n", + " ('Type', 'Double')\n", + " ('Size', 1)\n", + " ]))\n", + " ('Field',\n", + " PVLGroup([\n", + " ('Name', 'J2000Q2')\n", + " ('Type', 'Double')\n", + " ('Size', 1)\n", + " ]))\n", + " ('Field',\n", + " PVLGroup([\n", + " ('Name', 'J2000Q3')\n", + " ('Type', 'Double')\n", + " ('Size', 1)\n", + " ]))\n", + " ('Field', PVLGroup([\n", + " ('Name', 'AV1')\n", + " ('Type', 'Double')\n", + " ('Size', 1)\n", + " ]))\n", + " ('Field', PVLGroup([\n", + " ('Name', 'AV2')\n", + " ('Type', 'Double')\n", + " ('Size', 1)\n", + " ]))\n", + " ('Field', PVLGroup([\n", + " ('Name', 'AV3')\n", + " ('Type', 'Double')\n", + " ('Size', 1)\n", + " ]))\n", + " ('Field', PVLGroup([\n", + " ('Name', 'ET')\n", + " ('Type', 'Double')\n", + " ('Size', 1)\n", + " ]))\n", + "]), PVLObject([\n", + " ('Name', 'InstrumentPosition')\n", + " ('StartByte', 336179)\n", + " ('Bytes', 56)\n", + " ('Records', 1)\n", + " ('ByteOrder', 'Lsb')\n", + " ('CacheType', 'Linear')\n", + " ('SpkTableStartTime', 533104787.53776)\n", + " ('SpkTableEndTime', 533104787.53776)\n", + " ('SpkTableOriginalSize', 1.0)\n", + " ('Description', 'Created by spiceinit')\n", + " ('Kernels', '$tgo/kernels/spk/em16_tgo_fpp_015_01_20160314_20170315_v01.bsp')\n", + " ('Field', PVLGroup([\n", + " ('Name', 'J2000X')\n", + " ('Type', 'Double')\n", + " ('Size', 1)\n", + " ]))\n", + " ('Field', PVLGroup([\n", + " ('Name', 'J2000Y')\n", + " ('Type', 'Double')\n", + " ('Size', 1)\n", + " ]))\n", + " ('Field', PVLGroup([\n", + " ('Name', 'J2000Z')\n", + " ('Type', 'Double')\n", + " ('Size', 1)\n", + " ]))\n", + " ('Field',\n", + " PVLGroup([\n", + " ('Name', 'J2000XV')\n", + " ('Type', 'Double')\n", + " ('Size', 1)\n", + " ]))\n", + " ('Field',\n", + " PVLGroup([\n", + " ('Name', 'J2000YV')\n", + " ('Type', 'Double')\n", + " ('Size', 1)\n", + " ]))\n", + " ('Field',\n", + " PVLGroup([\n", + " ('Name', 'J2000ZV')\n", + " ('Type', 'Double')\n", + " ('Size', 1)\n", + " ]))\n", + " ('Field', PVLGroup([\n", + " ('Name', 'ET')\n", + " ('Type', 'Double')\n", + " ('Size', 1)\n", + " ]))\n", + "]), PVLObject([\n", + " ('Name', 'BodyRotation')\n", + " ('StartByte', 336235)\n", + " ('Bytes', 64)\n", + " ('Records', 1)\n", + " ('ByteOrder', 'Lsb')\n", + " ('TimeDependentFrames', [10014, 1])\n", + " ('CkTableStartTime', 533104787.53776)\n", + " ('CkTableEndTime', 533104787.53776)\n", + " ('CkTableOriginalSize', 1)\n", + " ('FrameTypeCode', 2)\n", + " ('PoleRa', [317.68143, -0.1061, 0.0])\n", + " ('PoleDec', [52.8865, -0.0609, 0.0])\n", + " ('PrimeMeridian', [176.63, 350.89198226, 0.0])\n", + " ('Description', 'Created by spiceinit')\n", + " ('Kernels',\n", + " ['$tgo/kernels/tspk/de430.bsp',\n", + " '$tgo/kernels/tspk/mar097.bsp',\n", + " '$tgo/kernels/pck/pck00010.tpc',\n", + " '$tgo/kernels/pck/de-403-masses.tpc'])\n", + " ('SolarLongitude', 266.45237441729)\n", + " ('Field',\n", + " PVLGroup([\n", + " ('Name', 'J2000Q0')\n", + " ('Type', 'Double')\n", + " ('Size', 1)\n", + " ]))\n", + " ('Field',\n", + " PVLGroup([\n", + " ('Name', 'J2000Q1')\n", + " ('Type', 'Double')\n", + " ('Size', 1)\n", + " ]))\n", + " ('Field',\n", + " PVLGroup([\n", + " ('Name', 'J2000Q2')\n", + " ('Type', 'Double')\n", + " ('Size', 1)\n", + " ]))\n", + " ('Field',\n", + " PVLGroup([\n", + " ('Name', 'J2000Q3')\n", + " ('Type', 'Double')\n", + " ('Size', 1)\n", + " ]))\n", + " ('Field', PVLGroup([\n", + " ('Name', 'AV1')\n", + " ('Type', 'Double')\n", + " ('Size', 1)\n", + " ]))\n", + " ('Field', PVLGroup([\n", + " ('Name', 'AV2')\n", + " ('Type', 'Double')\n", + " ('Size', 1)\n", + " ]))\n", + " ('Field', PVLGroup([\n", + " ('Name', 'AV3')\n", + " ('Type', 'Double')\n", + " ('Size', 1)\n", + " ]))\n", + " ('Field', PVLGroup([\n", + " ('Name', 'ET')\n", + " ('Type', 'Double')\n", + " ('Size', 1)\n", + " ]))\n", + "]), PVLObject([\n", + " ('Name', 'SunPosition')\n", + " ('StartByte', 336299)\n", + " ('Bytes', 56)\n", + " ('Records', 1)\n", + " ('ByteOrder', 'Lsb')\n", + " ('CacheType', 'Linear')\n", + " ('SpkTableStartTime', 533104787.53776)\n", + " ('SpkTableEndTime', 533104787.53776)\n", + " ('SpkTableOriginalSize', 1.0)\n", + " ('Description', 'Created by spiceinit')\n", + " ('Kernels', ['$tgo/kernels/tspk/de430.bsp', '$tgo/kernels/tspk/mar097.bsp'])\n", + " ('Field', PVLGroup([\n", + " ('Name', 'J2000X')\n", + " ('Type', 'Double')\n", + " ('Size', 1)\n", + " ]))\n", + " ('Field', PVLGroup([\n", + " ('Name', 'J2000Y')\n", + " ('Type', 'Double')\n", + " ('Size', 1)\n", + " ]))\n", + " ('Field', PVLGroup([\n", + " ('Name', 'J2000Z')\n", + " ('Type', 'Double')\n", + " ('Size', 1)\n", + " ]))\n", + " ('Field',\n", + " PVLGroup([\n", + " ('Name', 'J2000XV')\n", + " ('Type', 'Double')\n", + " ('Size', 1)\n", + " ]))\n", + " ('Field',\n", + " PVLGroup([\n", + " ('Name', 'J2000YV')\n", + " ('Type', 'Double')\n", + " ('Size', 1)\n", + " ]))\n", + " ('Field',\n", + " PVLGroup([\n", + " ('Name', 'J2000ZV')\n", + " ('Type', 'Double')\n", + " ('Size', 1)\n", + " ]))\n", + " ('Field', PVLGroup([\n", + " ('Name', 'ET')\n", + " ('Type', 'Double')\n", + " ('Size', 1)\n", + " ]))\n", + "])]\n" + ] + } + ], + "source": [ + "label = pvl.load(cube_file)\n", + "tables = label.getlist('Table')\n", + "print(tables)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "([(4.743092590963316e-305, 4.739987214924918e-305, 9.462663060940373e+251)],\n", + " [(-1.6878690421469897e+271, 5.29682553422139e-228, -5.8500298023031387e-98)],\n", + " [-1.1371270199850502e-143])" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "read_position_table(tables[1], cube_file)" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "([quaternion(-1.34337767237385e-225, -5.29807286672666e-110, 8.1011017870871e-115, -1.44413722000277e+194)],\n", + " [(-8089.420679908819, -3.5340714299727612e+249, -2.2489288021413767e-100)],\n", + " [1.9746554944903616e-307],\n", + " quaternion(0.54123866354163, 0.4550392390637, 0.54123866354163, 0.4550392390637))" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "read_rotation_table(tables[0], cube_file)" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "ename": "KeyError", + "evalue": "'ConstantRotation'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mKeyError\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[0mread_rotation_table\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtables\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcube_file\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36mread_rotation_table\u001b[0;34m(label, file)\u001b[0m\n\u001b[1;32m 20\u001b[0m \u001b[0mvelocities\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdoubles\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;36m7\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 21\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 22\u001b[0;31m \u001b[0mconst_rotation_mat\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlabel\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'ConstantRotation'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 23\u001b[0m \u001b[0mconst_rotation\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mquaternion\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfrom_rotation_matrix\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreshape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mconst_rotation_mat\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\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[1;32m 24\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mrotations\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvelocities\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtimes\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mconst_rotation\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m~/miniconda3/envs/pandas/lib/python3.7/site-packages/pvl/_collections.py\u001b[0m in \u001b[0;36m__getitem__\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m 121\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mINDEX_TYPES\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 122\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__items\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 123\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mdict_getitem\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 124\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 125\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__delitem__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mKeyError\u001b[0m: 'ConstantRotation'" + ] + } + ], + "source": [ + "read_rotation_table(tables[3], cube_file)" + ] + }, + { + "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.0" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} -- GitLab