diff --git a/notebooks/Socet2ISIS.ipynb b/notebooks/Socet2ISIS.ipynb index e4c81011976bee371c9e092fe6d7906fb01d1f13..bf0637ec03df83e13dcaa2eaa15e31b2eb6347ac 100644 --- a/notebooks/Socet2ISIS.ipynb +++ b/notebooks/Socet2ISIS.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -13,16 +13,20 @@ "\n", "import pandas as pd\n", "import numpy as np\n", + "import math\n", + "import pyproj\n", "\n", "sys.path.insert(0, \"/home/tthatcher/Desktop/Projects/Plio/plio\")\n", "\n", "from plio.examples import get_path\n", - "from plio.io.io_bae import read_gpf, read_ipf" + "from plio.io.io_bae import read_gpf, read_ipf\n", + "import plio.io.io_controlnetwork as cn\n", + "import plio.io.isis_serial_number as sn" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -71,62 +75,16 @@ " # Sets the value of PATH to the path of the ATF file\n", " files_dict['PATH'] = os.path.dirname(os.path.abspath(atf_file))\n", " \n", - " return files_dict" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "scrolled": false - }, - "outputs": [], - "source": [ - "atf_dict = read_atf(get_path('CTX_Athabasca_Middle_step0.atf'))\n", - "\n", - "gpf_file = os.path.join(atf_dict['PATH'], atf_dict['GP_FILE']);\n", - "ipf_list = [os.path.join(atf_dict['PATH'], i) for i in atf_dict['IMAGE_IPF']]\n", - "\n", - "gpf_df = read_gpf(gpf_file)\n", - "ipf_df = read_ipf(ipf_list)\n", - "\n", - "point_diff = ipf_df.index.difference(gpf_df.index)\n", - "\n", - "\n", - "\n", - "if len(point_diff) != 0:\n", - " warnings.warn(\"The following points found in ipf files missing from gpf file: \\n\\n{}. \\\n", - " \\n\\nContinuing, but these points will be missing from the control network\".format(list(point_diff)))\n", - "\n", - "new_df = ipf_df.merge(gpf_df, left_on='pt_id', right_on='point_id')" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import math\n", - "import pyproj\n", - "\n", - "image_dict = {'P01_001540_1889_XI_08N204W' : 'P01_001540_1889_XI_08N204W.lev1.cub',\n", - " 'P01_001606_1897_XI_09N203W' : 'P01_001606_1897_XI_09N203W.lev1.cub',\n", - " 'P02_001804_1889_XI_08N204W' : 'P02_001804_1889_XI_08N204W.lev1.cub',\n", - " 'P03_002226_1895_XI_09N203W' : 'P03_002226_1895_XI_09N203W.lev1.cub',\n", - " 'P03_002371_1888_XI_08N204W' : 'P03_002371_1888_XI_08N204W.lev1.cub',\n", - " 'P19_008344_1894_XN_09N203W' : 'P19_008344_1894_XN_09N203W.lev1.cub',\n", - " 'P20_008845_1894_XN_09N203W' : 'P20_008845_1894_XN_09N203W.lev1.cub'}\n", + " return files_dict\n", "\n", "# converts columns l. and s. to isis\n", - "def line_sample_size(record):\n", - " with open(atf_dict['PATH'] + '/' + record['ipf_file'] + '.sup') as f:\n", + "def line_sample_size(record, path):\n", + " with open(os.path.join(path, record['ipf_file'] + '.sup')) as f:\n", " for i, line in enumerate(f):\n", " if i == 2:\n", " img_index = line.split('\\\\')\n", " img_index = img_index[-1].strip()\n", " img_index = img_index.split('.')[0]\n", - " img_index = image_dict[img_index]\n", " \n", " if i == 3:\n", " line_size = line.split(' ')\n", @@ -178,7 +136,7 @@ "\n", "# gets eRadius and pRadius from a .prj file\n", "def get_axis(file):\n", - " with open(atf_dict['PATH'] + '/' + file) as f:\n", + " with open(file) as f:\n", " from collections import defaultdict\n", "\n", " files = defaultdict(list)\n", @@ -210,76 +168,104 @@ " lla = pyproj.Proj(proj='latlon', a=semi_major, b=semi_minor)\n", " lon, lat, height = pyproj.transform(lla, ecef, record['long_X_East'], record['lat_Y_North'], record['ht'])\n", " return lon, lat, height\n", + " \n", "\n", "# applys transformations to columns\n", - "def socet2isis(prj_file):\n", + "def apply_transformations(atf_dict, df):\n", + " prj_file = os.path.join(atf_dict['PATH'], atf_dict['PROJECT'].split('\\\\')[-1])\n", + " \n", " eRadius, pRadius = get_axis(prj_file)\n", - " new_df['s.'], new_df['l.'], new_df['image_index'] = (zip(*new_df.apply(line_sample_size, axis=1)))\n", - " new_df['known'] = new_df.apply(known, axis=1)\n", - " new_df['lat_Y_North'] = new_df.apply(lat_ISIS_coord, semi_major = eRadius, semi_minor = pRadius, axis=1)\n", - " new_df['long_X_East'] = new_df.apply(lon_ISIS_coord, semi_major = eRadius, semi_minor = pRadius, axis=1)\n", - " new_df['long_X_East'], new_df['lat_Y_North'], new_df['ht'] = zip(*new_df.apply(body_fix, semi_major = eRadius, semi_minor = pRadius, axis = 1))\n", - "socet2isis('CTX_Athabasca_Middle.prj')" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "new_df['image_index']" + " \n", + " df['s.'], df['l.'], df['image_index'] = (zip(*df.apply(line_sample_size, path = atf_dict['PATH'], axis=1)))\n", + " df['known'] = df.apply(known, axis=1)\n", + " df['lat_Y_North'] = df.apply(lat_ISIS_coord, semi_major = eRadius, semi_minor = pRadius, axis=1)\n", + " df['long_X_East'] = df.apply(lon_ISIS_coord, semi_major = eRadius, semi_minor = pRadius, axis=1)\n", + " df['long_X_East'], df['lat_Y_North'], df['ht'] = zip(*df.apply(body_fix, semi_major = eRadius, semi_minor = pRadius, axis = 1))\n", + " \n", + "def socet2isis(prj_file):\n", + " # Read in and setup the atf dict of information\n", + " atf_dict = read_atf(prj_file)\n", + " \n", + " # Get the gpf and ipf files using atf dict\n", + " gpf_file = os.path.join(atf_dict['PATH'], atf_dict['GP_FILE']);\n", + " ipf_list = [os.path.join(atf_dict['PATH'], i) for i in atf_dict['IMAGE_IPF']]\n", + " \n", + " # Read in the gpf file and ipf file(s) into seperate dataframes\n", + " gpf_df = read_gpf(gpf_file)\n", + " ipf_df = read_ipf(ipf_list)\n", + "\n", + " # Check for differences between point ids using each dataframes\n", + " # point ids as a reference\n", + " gpf_pt_idx = pd.Index(pd.unique(gpf_df['point_id']))\n", + " ipf_pt_idx = pd.Index(pd.unique(ipf_df['pt_id']))\n", + "\n", + " point_diff = ipf_pt_idx.difference(gpf_pt_idx)\n", + "\n", + " if len(point_diff) != 0:\n", + " warnings.warn(\"The following points found in ipf files missing from gpf file: \\n\\n{}. \\\n", + " \\n\\nContinuing, but these points will be missing from the control network\".format(list(point_diff)))\n", + " \n", + " # Merge the two dataframes on their point id columns\n", + " 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", + " \n", + " # Define column remap for socet dataframe\n", + " column_remap = {'l.': 'x', 's.': 'y',\n", + " 'res_l': 'LineResidual', 'res_s': 'SampleResidual', 'known': 'Type',\n", + " 'lat_Y_North': 'AprioriY', 'long_X_East': 'AprioriX', 'ht': 'AprioriZ',\n", + " 'sig0': 'AprioriLatitudeSigma', 'sig1': 'AprioriLongitudeSigma', 'sig2': 'AprioriRadiusSigma'}\n", + " \n", + " # Rename the columns using the column remap above\n", + " socet_df.rename(columns = column_remap, inplace=True)\n", + " \n", + " # Return the socet dataframe to be converted to a control net\n", + " return socet_df\n", + "\n", + "# creates a dict of serial numbers with the cub being the key\n", + "def serial_numbers(images, path, extension):\n", + " serial_dict = dict()\n", + " \n", + " for image in images:\n", + " serial_dict[image] = sn.generate_serial_number(os.path.join(path, image + extension))\n", + " return serial_dict" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": { - "scrolled": true + "scrolled": false }, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/adampaquette/anaconda/envs/pysat/lib/python3.6/site-packages/ipykernel_launcher.py:173: UserWarning: The following points found in ipf files missing from gpf file: \n", + "\n", + "['P03_002226_1895_XI_09N203W_15', 'P03_002226_1895_XI_09N203W_16', 'P03_002226_1895_XI_09N203W_17', 'P03_002226_1895_XI_09N203W_18', 'P03_002226_1895_XI_09N203W_19', 'P03_002226_1895_XI_09N203W_20', 'P03_002226_1895_XI_09N203W_21', 'P03_002226_1895_XI_09N203W_22', 'P03_002226_1895_XI_09N203W_24', 'P03_002226_1895_XI_09N203W_26', 'P03_002226_1895_XI_09N203W_30', 'P03_002226_1895_XI_09N203W_31', 'P03_002226_1895_XI_09N203W_32', 'P03_002226_1895_XI_09N203W_34', 'P03_002226_1895_XI_09N203W_36', 'P03_002226_1895_XI_09N203W_37', 'P03_002226_1895_XI_09N203W_44', 'P03_002226_1895_XI_09N203W_48', 'P03_002226_1895_XI_09N203W_49', 'P03_002226_1895_XI_09N203W_56', 'P03_002226_1895_XI_09N203W_57', 'P03_002226_1895_XI_09N203W_61', 'P03_002226_1895_XI_09N203W_62', 'P03_002226_1895_XI_09N203W_63', 'P03_002226_1895_XI_09N203W_65', 'P19_008344_1894_XN_09N203W_4', 'P20_008845_1894_XN_09N203W_15']. \n", + "\n", + "Continuing, but these points will be missing from the control network\n" + ] + } + ], "source": [ - "column_remap = {'l.': 'x', 's.': 'y',\n", - "'res_l': 'LineResidual', 'res_s': 'SampleResidual', 'known': 'Type',\n", - "'lat_Y_North': 'AprioriY', 'long_X_East': 'AprioriX', 'ht': 'AprioriZ',\n", - "'sig0': 'AprioriLatitudeSigma', 'sig1': 'AprioriLongitudeSigma', 'sig2': 'AprioriRadiusSigma'}\n", + "# Setup stuffs for the cub information namely the path and extension\n", + "path = '/Volumes/Blueman/'\n", + "extension = '.lev1.cub'\n", "\n", - "new_df.rename(columns=column_remap, inplace=True)\n", - "\n", - "new_df" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import plio.io.io_controlnetwork as cn\n", - "import plio.io.isis_serial_number as sn\n", + "prj_file = get_path('CTX_Athabasca_Middle_step0.atf')\n", "\n", - "# creates a dict of serial numbers with the cub being the key\n", - "def serial_numbers():\n", - " serial_dict = {}\n", - " image_dict = {'P01_001540_1889_XI_08N204W' : 'P01_001540_1889_XI_08N204W.lev1.cub',\n", - " 'P01_001606_1897_XI_09N203W' : 'P01_001606_1897_XI_09N203W.lev1.cub',\n", - " 'P02_001804_1889_XI_08N204W' : 'P02_001804_1889_XI_08N204W.lev1.cub',\n", - " 'P03_002226_1895_XI_09N203W' : 'P03_002226_1895_XI_09N203W.lev1.cub',\n", - " 'P03_002371_1888_XI_08N204W' : 'P03_002371_1888_XI_08N204W.lev1.cub',\n", - " 'P19_008344_1894_XN_09N203W' : 'P19_008344_1894_XN_09N203W.lev1.cub',\n", - " 'P20_008845_1894_XN_09N203W' : 'P20_008845_1894_XN_09N203W.lev1.cub'}\n", - " \n", - " for key in image_dict:\n", - " serial_dict[image_dict[key]] = sn.generate_serial_number('/home/tthatcher/Desktop/Projects/Plio/' + image_dict[key])\n", - " return serial_dict\n", + "socet_df = socet2isis(prj_file)\n", "\n", - "# serial number dictionary\n", - "serial_dict = serial_numbers()\n", + "images = pd.unique(socet_df['ipf_file'])\n", "\n", - "print(serial_dict)\n", + "serial_dict = serial_numbers(images, path, extension)\n", "\n", "# creates the control network\n", - "cnet = cn.to_isis('/home/tthatcher/Desktop/Projects/Plio/cn.csv', new_df, serial_dict)" + "cnet = cn.to_isis('/Volumes/Blueman/cn.csv', socet_df, serial_dict)" ] }, { @@ -287,82 +273,7 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "@singledispatch\n", - "def read_ipf(arg):\n", - " return str(arg)\n", - "# new_df['known'] = new_df.apply(known, axis=1)\n", - "\n", - "@read_ipf.register(str)\n", - "def read_ipf_str(input_data):\n", - " \"\"\"AttributeError: 'Series' object has no attribute 'image_index'\n", - "\n", - " Read a socet ipf file into a pandas data frame\n", - "\n", - " Parameters\n", - " ----------\n", - " input_data : str\n", - " path to the an input data file\n", - "\n", - " Returns\n", - " -------\n", - " df : pd.DataFrame\n", - " containing the ipf data with appropriate column names and indices\n", - " \"\"\"\n", - "\n", - " # Check that the number of rows is matching the expected number\n", - " with open(input_data, 'r') as f:\n", - " for i, l in enumerate(f):\n", - " if i == 1:/home/tthatcher/Desktop/Projects/Plio/plio\n", - " cnt = int(l)\n", - " elif i == 2:\n", - " col = l\n", - " break\n", - " \n", - " columns = np.genfromtxt(input_data, skip_header=2, dtype='unicode',\n", - " max_rows = 1, delimiter = ',')\n", - "\n", - " # TODO: Add unicode conversion\n", - " d = [line.split() for line in open(input_data, 'r')]\n", - " d = np.hstack(np.array(d[3:]))\n", - " \n", - " d = d.reshape(-1, 12)\n", - " \n", - " df = pd.DataFrame(d, columns=columns)\n", - " file = os.path.split(os.path.splitext(input_data)[0])[1]\n", - " df['ipf_file'] = pd.Series(np.full((len(df['pt_id'])), file), index = df.index)\n", - "\n", - " assert int(cnt) == len(df), 'Dataframe length {} does not match point length {}.'.format(int(cnt), len(df))\n", - " \n", - " # Soft conversion of numeric types to numerics, allows str in first col for point_id\n", - " df = df.apply(pd.to_numeric, errors='ignore')\n", - "\n", - " return df\n", - "\n", - "@read_ipf.register(list)\n", - "def read_ipf_list(input_data_list):\n", - " \"\"\"\n", - " Read a socet ipf file into a pandas data frame\n", - "\n", - " Parameters\n", - " ----------\n", - " input_data_list : list\n", - " list of paths to the a set of input data files\n", - "\n", - " Returns\n", - " -------\n", - " df : pd.DataFrame\n", - " containing the ipf data with appropriate column names and indices\n", - " \"\"\"\n", - " frames = []\n", - "\n", - " for input_file in input_data_list:\n", - " frames.append(read_ipf(input_file))\n", - "\n", - " df = pd.concat(frames)\n", - "\n", - " return df" - ] + "source": [] } ], "metadata": { @@ -381,7 +292,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.4" + "version": "3.6.3" } }, "nbformat": 4,