diff --git a/notebooks/Socet2ISIS.ipynb b/notebooks/Socet2ISIS.ipynb index b060d18a23d1e74383bfa298c88bb5eccde2c375..af97cd92fb1c9fe8bcc5c1b299ef190f4fe2a767 100644 --- a/notebooks/Socet2ISIS.ipynb +++ b/notebooks/Socet2ISIS.ipynb @@ -8,21 +8,13 @@ "source": [ "import os\n", "import sys\n", - "import csv\n", - "import pvl\n", "\n", "import pandas as pd\n", - "import numpy as np\n", "import math\n", - "import pyproj\n", - "from functools import singledispatch\n", - "import warnings\n", "\n", - "from plio.examples import get_path\n", - "from plio.io.io_bae import read_gpf, read_ipf, read_atf\n", + "from plio.io.io_bae import read_gpf, read_ipf, read_atf, save_gpf, save_ipf\n", "from plio.utils.utils import find_in_dict\n", - "from plio.spatial.transformations import *\n", - "from collections import defaultdict\n", + "from plio.spatial.transformations import apply_isis_transformations, apply_socet_transformations, serial_numbers\n", "import plio.io.io_controlnetwork as cn\n", "import plio.io.isis_serial_number as sn" ] @@ -33,7 +25,7 @@ "metadata": {}, "outputs": [], "source": [ - "def socet2isis(at_file, cub_file_path, cub_ipf_map, target_name, outpath=None):\n", + "def socet2isis(at_file, cub_file_path, extension, target_name, outpath=None):\n", " # Setup the at_file, path to cubes, and control network out path\n", " at_file = at_file\n", " cnet_out = os.path.split(os.path.splitext(at_file)[0])[1]\n", @@ -43,10 +35,6 @@ " outpath = outpath\n", " else:\n", " outpath = os.path.split(at_file)[0]\n", - "\n", - " with open(cub_ipf_map) as cub_ipf_map:\n", - " reader = csv.reader(cub_ipf_map, delimiter = ',')\n", - " image_dict = dict([(row[0], row[1]) for row in reader])\n", " \n", " # Read in and setup the atf dict of information\n", " atf_dict = read_atf(at_file)\n", @@ -74,7 +62,7 @@ " socet_df = ipf_df.merge(gpf_df, left_on='pt_id', right_on='point_id')\n", " \n", " # Apply the transformations\n", - " apply_transformations(atf_dict, socet_df)\n", + " apply_socet_transformations(atf_dict, socet_df)\n", " \n", " # Define column remap for socet dataframe\n", " column_map = {'pt_id': 'id', 'l.': 'y', 's.': 'x',\n", @@ -86,30 +74,79 @@ " # Rename the columns using the column remap above\n", " socet_df.rename(columns = column_map, inplace=True)\n", " \n", + " # Build an image and serial dict assuming the cubes will be named as the IPFs are\n", + " image_dict = {i: i + extension for i in pd.unique(socet_df['ipf_file'])}\n", " serial_dict = serial_numbers(image_dict, cub_path)\n", "\n", " # creates the control network\n", - " cn.to_isis(os.path.join(outpath, cnet_out + '.net'), socet_df, serial_dict, targetname = targetname)\n", - " return socet_df" + " cn.to_isis(os.path.join(outpath, cnet_out + '.net'), socet_df, serial_dict, targetname = targetname)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { - "scrolled": true + "scrolled": false }, "outputs": [], "source": [ - "# Setup stuffs for the cub information namely the path and extension\n", - "cub_path = '/Volumes/Blueman/'\n", - "targetname = 'Mars'\n", - "cub_map = '/Users/adampaquette/repos/plio/plio/examples/SocetSet/cub_map2.csv'\n", + "def isis2socet(cnet_path, eRadius, eccentricity, cub_path, extension, cub_list, out_gpf, adjusted_flag = False):\n", + " pRadius = eRadius * math.sqrt(1 - (eccentricity ** 2))\n", + " \n", + " df = cn.from_isis(cnet_path)\n", + " # Create cub dict to map ipf to cub\n", + " cub_dict = {i: i + extension for i in cub_list}\n", + "\n", + " # Create serial dict to match serial to ipf\n", + " serial_dict = {sn.generate_serial_number(os.path.join(cub_path, i + extension)): i for i in cub_list}\n", + "\n", + " # Remove duplicate columns\n", + " # There are better ways to do this but pandas was not having it\n", + " columns = []\n", + " column_index = []\n", + "\n", + " for i, column in enumerate(list(df.columns)):\n", + " if column not in columns:\n", + " column_index.append(i)\n", + " columns.append(column)\n", + "\n", + " df = df.iloc[:, column_index]\n", + "\n", + " # Begin translation\n", + " # Remap the ISIS columns to socet column names\n", + " column_map = {'id': 'pt_id', 'line': 'l.', 'sample': 's.', \n", + " 'lineResidual': 'res_l', 'sampleResidual': 'res_s', 'type': 'known', \n", + " 'aprioriLatitudeSigma': 'sig0', 'aprioriLongitudeSigma': 'sig1', 'aprioriRadiusSigma': 'sig2', \n", + " 'linesigma': 'sig_l', 'samplesigma': 'sig_s', 'ignore': 'stat'}\n", + "\n", + " # Depending on the adjusted flag, set the renames for columns appropriately\n", + " if adjusted_flag:\n", + " column_map['adjustedY'] = 'lat_Y_North'\n", + " column_map['adjustedX'] = 'long_X_East'\n", + " column_map['adjustedZ'] = 'ht'\n", + " else:\n", + " column_map['aprioriY'] = 'lat_Y_North'\n", + " column_map['aprioriX'] = 'long_X_East'\n", + " column_map['aprioriZ'] = 'ht'\n", "\n", - "# Path to atf file\n", - "atf_file = ('/Users/adampaquette/repos/plio/plio/examples/SocetSet/Relative.atf')\n", + " df.rename(columns = column_map, inplace=True)\n", + " \n", + " apply_isis_transformations(df, eRadius, pRadius, serial_dict, extension, cub_path)\n", + "\n", + " # Save the ipf\n", + " save_ipf(df, os.path.split(out_gpf)[0])\n", "\n", - "socet_df = socet2isis(atf_file, cub_path, cub_map, targetname)" + " # Get the first record from each group as there all the same, put them\n", + " # into a list, and sort it\n", + " points = [int(i[1].index[0]) for i in df.groupby('pt_id')]\n", + " points.sort()\n", + "\n", + " # Set the gpf_df to only the values we need and do a small rename\n", + " gpf_df = df.iloc[points].copy()\n", + " gpf_df.rename(columns = {'pt_id': 'point_id'}, inplace=True)\n", + "\n", + " # Save the gpf\n", + " save_gpf(gpf_df, out_gpf)" ] }, { @@ -118,202 +155,46 @@ "metadata": {}, "outputs": [], "source": [ - "def reverse_known(record):\n", - " \"\"\"\n", - " Converts the known field from a socet dataframe into the\n", - " isis point_type column\n", - "\n", - " Parameters\n", - " ----------\n", - " record : object\n", - " Pandas series object\n", - "\n", - " Returns\n", - " -------\n", - " : str\n", - " String representation of a known field\n", - " \"\"\"\n", - " record_type = record['known']\n", - " if record_type == 0 or record_type == 2:\n", - " return 0\n", - "\n", - " elif record_type == 1 or record_type == 3 or record_type == 4:\n", - " return 3\n", - " \n", - "def lat_socet_coord(record, semi_major, semi_minor):\n", - " \"\"\"\n", - " Function to convert lat_Y_North to ISIS_lat\n", - "\n", - " Parameters\n", - " ----------\n", - " record : object\n", - " Pandas series object\n", - "\n", - " semi_major : float\n", - " Radius from the center of the body to the equater\n", - "\n", - " semi_minor : float\n", - " Radius from the pole to the center of mass\n", - "\n", - " Returns\n", - " -------\n", - " coord_360 : float\n", - " Converted latitude into ocentric space, and mapped\n", - " into 0 to 360\n", - " \"\"\"\n", - " ographic_coord = oc2og(record['lat_Y_North'], semi_major, semi_minor)\n", - " return ((ographic_coord + 180) % 360) - 180\n", - "\n", - "def lon_socet_coord(record, semi_major, semi_minor):\n", - " \"\"\"\n", - " Function to convert lat_Y_North to ISIS_lat\n", - "\n", - " Parameters\n", - " ----------\n", - " record : object\n", - " Pandas series object\n", - "\n", - " semi_major : float\n", - " Radius from the center of the body to the equater\n", - "\n", - " semi_minor : float\n", - " Radius from the pole to the center of mass\n", - "\n", - " Returns\n", - " -------\n", - " coord_360 : float\n", - " Converted latitude into ocentric space, and mapped\n", - " into 0 to 360\n", - " \"\"\"\n", - " ographic_coord = oc2og(record['long_X_East'], semi_major, semi_minor)\n", - " return ((ographic_coord + 180) % 360) - 180\n", + "# Setup stuffs for the cub information namely the path and extension\n", + "cub_path = '/Path/to/cubs'\n", "\n", - "def fix_sample_line(record, serial_dict, extension, cub_path):\n", - " # Cube location to load\n", - " cube = pvl.load(os.path.join(cub_path, serial_dict[record['serialnumber']] + extension))\n", - " line_size = find_in_dict(cube, 'Lines')\n", - " sample_size = find_in_dict(cube, 'Samples')\n", + "# Name of the target body\n", + "targetname = 'Mars'\n", + "extension = 'cub.-->extension<--'\n", "\n", - " new_line = record['l.'] - (int(line_size)/2.0) - 1\n", - " new_sample = record['s.'] - (int(sample_size)/2.0) - 1\n", - " return new_line, new_sample\n", + "# Path to atf file\n", + "atf_file = 'Path/to/socket/set/at_file.atf'\n", "\n", - "def ignore_toggle(record):\n", - " if record['stat'] == True:\n", - " return 0\n", - " else:\n", - " return 1" + "socet2isis(atf_file, cub_path, extension, targetname)" ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "scrolled": false - }, + "metadata": {}, "outputs": [], "source": [ - "return_df = cn.from_isis(\"/Users/adampaquette/repos/plio/plio/examples/SocetSet/Relative.net\")\n", + "# Setup stuffs for the cub information namely the path and extension\n", + "# along with eRadius and eccentricity\n", + "cnet = \"Path/to/control/network.net\"\n", "\n", "eRadius = 3.39619000000000e+006\n", - "pRadius = eRadius * (1 - 1.08339143554195e-001)\n", + "eccentricity = 1.08339143554195e-001\n", "\n", - "adjusted_flag = False\n", + "cub_path = 'Path/to/cubs'\n", + "extension = 'cub.-->extension<--'\n", "\n", - "cub_path = '/Volumes/Blueman/'\n", - "extension = '.8bit.cub'\n", + "# List of cubes to use\n", "cub_list = ['D06_029601_1846_XN_04N224W', \n", " 'F05_037684_1857_XN_05N224W']\n", "\n", - "# \n", - "cub_dict = {i: i + extension for i in cub_list}\n", - "serial_dict = {sn.generate_serial_number(os.path.join(cub_path, i + extension)): i for i in cub_list}\n", - "\n", - "columns = []\n", - "column_index = []\n", - "\n", - "for i, column in enumerate(list(return_df.columns)):\n", - " if column not in columns:\n", - " column_index.append(i)\n", - " columns.append(column)\n", + "out_gpf = \"/Users/adampaquette/Desktop/InSightE09_XW.gpf\"\n", "\n", - "return_df = return_df.iloc[:, column_index]\n", - "\n", - "column_map = {'id': 'pt_id', 'line': 'l.', 'sample': 's.', \n", - " 'lineResidual': 'res_l', 'sampleResidual': 'res_s', 'type': 'known', \n", - " 'aprioriLatitudeSigma': 'sig0', 'aprioriLongitudeSigma': 'sig1', 'aprioriRadiusSigma': 'sig2', \n", - " 'linesigma': 'sig_l', 'samplesigma': 'sig_s', 'ignore': 'stat'}\n", - "\n", - "if adjusted_flag:\n", - " column_map['adjustedY'] = 'lat_Y_North'\n", - " column_map['adjustedX'] = 'long_X_East'\n", - " column_map['adjustedZ'] = 'ht'\n", - "else:\n", - " column_map['aprioriY'] = 'lat_Y_North'\n", - " column_map['aprioriX'] = 'long_X_East'\n", - " column_map['aprioriZ'] = 'ht'\n", - "\n", - "return_df.rename(columns = column_map, inplace=True)\n", - "\n", - "return_df['known'] = return_df.apply(reverse_known, axis = 1)\n", - "return_df['ipf_file'] = return_df['serialnumber'].apply(lambda serial_number: serial_dict[serial_number])\n", - "return_df['l.'], return_df['s.'] = zip(*return_df.apply(fix_sample_line, serial_dict = serial_dict, \n", - " extension = extension, \n", - " cub_path = cub_path, axis = 1))\n", - "\n", - "ecef = np.array([[return_df['long_X_East']], [return_df['lat_Y_North']], [return_df['ht']]])\n", - "lla = body_fix(ecef, semi_major = eRadius, semi_minor = pRadius, inverse=True)\n", - "return_df['long_X_East'], return_df['lat_Y_North'], return_df['ht'] = lla[0][0], lla[1][0], lla[2][0]\n", - "\n", - "return_df['lat_Y_North'] = return_df.apply(lat_socet_coord, semi_major=eRadius, semi_minor=pRadius, axis = 1)\n", - "return_df['long_X_East'] = return_df.apply(lon_socet_coord, semi_major=eRadius, semi_minor=pRadius, axis = 1)\n", - "\n", - "return_df['stat'] = return_df.apply(ignore_toggle, axis = 1)\n", - "return_df['val'] = return_df['stat']\n", - "\n", - "# Add dumby\n", - "x_dummy = lambda x: np.full(len(return_df), x)\n", - "\n", - "return_df['sig0'] = x_dummy(0)\n", - "return_df['sig1'] = x_dummy(0)\n", - "return_df['sig2'] = x_dummy(0)\n", - "\n", - "return_df['res0'] = x_dummy(0)\n", - "return_df['res1'] = x_dummy(0)\n", - "return_df['res2'] = x_dummy(0)\n", - "\n", - "return_df['fid_x'] = x_dummy(0)\n", - "return_df['fid_y'] = x_dummy(0)\n", + "adjusted_flag = False\n", "\n", - "return_df['no_obs'] = x_dummy(1)\n", - "return_df['fid_val'] = x_dummy(0)" + "isis2socet(cnet, eRadius, eccentricity, cub_path, extension, cub_list, out_gpf, adjusted_flag)" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "return_df[['long_X_East', 'lat_Y_North', 'ht']].iloc[2]" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "return_df[['long_X_East', 'lat_Y_North', 'ht']].iloc[2]" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "code", "execution_count": null,