diff --git a/bin/isisnet2socet b/bin/isisnet2socet new file mode 100644 index 0000000000000000000000000000000000000000..62436781ec718bf632a5e9e35cadcdd142349517 --- /dev/null +++ b/bin/isisnet2socet @@ -0,0 +1,102 @@ +#!/usr/bin/env python +import argparse +import os + +import pandas as pd + +from plio.io.io_bae import save_gpf, save_ipf +from plio.spatial.transformations import apply_isis_transformations +import plio.io.io_controlnetwork as cn +import plio.io.isis_serial_number as sn + +def parse_args(): + parser = argparse.ArgumentParser() + + # Add args here + parser.add_argument('cnet_file', help='Path to an isis control network.') + parser.add_argument('e_radius', type=float, help='The semimajor radius of a given target.') + parser.add_argument('p_radius', type=float, help='The semiminor radius of a given target.') + parser.add_argument('cub_path', help='Path to the cub files associated with a control network.') + parser.add_argument('cub_extension', help='Extension for all cubes.') + parser.add_argument('cub_list', help='Path to a list file of all cubes being used') + parser.add_argument('out_gpf', help='Path to save location of gpf file and new ipf files.') + parser.add_argument('--adjusted', help='Flag for saving apriori values or adjusted values', + default=False, required = False) + + return parser.parse_args() + + +def main(args): + # Create cub dict to map ipf to cub + df = cn.from_isis(args.cnet_file) + + e_radius = args.e_radius + p_radius = e_radius * (1 - args.p_radius) + + cub_path = args.cub_path + extension = args.cub_extension + + with open(args.cub_list, 'r') as f: + lines = f.readlines() + cub_list = [cub.replace('\n', '') for cub in lines] + + out_gpf = args.out_gpf + + adjusted_flag = args.adjusted + + # Create cub dict to map ipf to cub + cub_dict = {i: i + extension for i in cub_list} + + # Create serial dict to match serial to ipf + serial_dict = {sn.generate_serial_number(os.path.join(cub_path, i + extension)): i for i in cub_list} + + # Remove duplicate columns + # There are better ways to do this but pandas was not having it + columns = [] + column_index = [] + + for i, column in enumerate(list(df.columns)): + if column not in columns: + column_index.append(i) + columns.append(column) + + df = df.iloc[:, column_index] + + # Begin translation + # Remap the ISIS columns to socet column names + column_map = {'id': 'pt_id', 'line': 'l.', 'sample': 's.', + 'lineResidual': 'res_l', 'sampleResidual': 'res_s', 'type': 'known', + 'aprioriLatitudeSigma': 'sig0', 'aprioriLongitudeSigma': 'sig1', 'aprioriRadiusSigma': 'sig2', + 'linesigma': 'sig_l', 'samplesigma': 'sig_s', 'ignore': 'stat'} + + # Depending on the adjusted flag, set the renames for columns appropriately + if adjusted_flag: + column_map['adjustedY'] = 'lat_Y_North' + column_map['adjustedX'] = 'long_X_East' + column_map['adjustedZ'] = 'ht' + else: + column_map['aprioriY'] = 'lat_Y_North' + column_map['aprioriX'] = 'long_X_East' + column_map['aprioriZ'] = 'ht' + + df.rename(columns = column_map, inplace=True) + + apply_isis_transformations(df, e_radius, p_radius, serial_dict, extension, cub_path) + + # Save the ipf(s) + save_ipf(df, os.path.split(out_gpf)[0]) + + # Get the first record from each group as there all the same, put them + # into a list, and sort it + points = [int(i[1].index[0]) for i in df.groupby('pt_id')] + points.sort() + + # Set the gpf_df to only the values we need and do a small rename + gpf_df = df.iloc[points].copy() + gpf_df.rename(columns = {'pt_id': 'point_id'}, inplace=True) + + # Save the gpf + save_gpf(gpf_df, out_gpf) + +if __name__ == '__main__': + main(parse_args()) diff --git a/bin/socet2isis b/bin/socetnet2isis similarity index 64% rename from bin/socet2isis rename to bin/socetnet2isis index 2fe05808f4f518ef9f7586cf31bd3cfb57c5cb22..f549df8c8328dbfd9c8b3862a894d2a4d72017e7 100644 --- a/bin/socet2isis +++ b/bin/socetnet2isis @@ -1,14 +1,11 @@ #!/usr/bin/env python -import argparse import os import sys +import argparse import warnings -import csv -import numpy as np -from plio.examples import get_path from plio.io.io_bae import read_atf, read_gpf, read_ipf -from plio.spatial.transformations import * +from plio.spatial.transformations import apply_socet_transformations, serial_numbers import plio.io.io_controlnetwork as cn import pandas as pd @@ -19,9 +16,9 @@ def parse_args(): # Add args here parser.add_argument('at_file', help='Path to the .atf file for a project.') parser.add_argument('cub_file_path', help='Path to cube files related to ipf files.') - parser.add_argument('cub_ipf_map', help='Path to map file for all ipfs and cubes.') - parser.add_argument('--outpath', help='Directory for the control network to be output to.', - required = False) + parser.add_argument('extension', help='Extension for all cubes being used.') + parser.add_argument('target_name', help='Name of the target body used in the control net') + parser.add_argument('--outpath', help='Directory for the control network to be output to.') return parser.parse_args() @@ -37,10 +34,6 @@ def main(args): else: outpath = os.path.split(at_file)[0] - with open(args.cub_ipf_map) as cub_ipf_map: - reader = csv.reader(cub_ipf_map, delimiter = ',') - image_dict = dict([(row[0], row[1]) for row in reader]) - # Read in and setup the atf dict of information atf_dict = read_atf(at_file) @@ -60,33 +53,31 @@ def main(args): point_diff = ipf_pt_idx.difference(gpf_pt_idx) if len(point_diff) != 0: - warnings.warn("The following points found in ipf files missing from gpf file: " + - "\n\n{}\n\n".format("\n".join(point_diff)) + - "Continuing, but these points will be missing from the control " + - "network.", stacklevel=3) + warnings.warn("The following points found in ipf files missing from gpf file: \n\n{}. \ + \n\nContinuing, but these points will be missing from the control network".format(list(point_diff))) # Merge the two dataframes on their point id columns socet_df = ipf_df.merge(gpf_df, left_on='pt_id', right_on='point_id') # Apply the transformations - apply_transformations(atf_dict, socet_df) + apply_socet_transformations(atf_dict, socet_df) # Define column remap for socet dataframe - column_remap = {'l.': 'y', 's.': 'x', - 'res_l': 'LineResidual', 'res_s': 'SampleResidual', 'known': 'Type', - 'lat_Y_North': 'AprioriY', 'long_X_East': 'AprioriX', 'ht': 'AprioriZ', - 'sig0': 'AprioriLatitudeSigma', 'sig1': 'AprioriLongitudeSigma', - 'sig2': 'AprioriRadiusSigma'} + column_map = {'pt_id': 'id', 'l.': 'y', 's.': 'x', + 'res_l': 'lineResidual', 'res_s': 'sampleResidual', 'known': 'Type', + 'lat_Y_North': 'aprioriY', 'long_X_East': 'aprioriX', 'ht': 'aprioriZ', + 'sig0': 'aprioriLatitudeSigma', 'sig1': 'aprioriLongitudeSigma', 'sig2': 'aprioriRadiusSigma', + 'sig_l': 'linesigma', 'sig_s': 'samplesigma'} # Rename the columns using the column remap above - socet_df.rename(columns = column_remap, inplace=True) - - images = pd.unique(socet_df['ipf_file']) + socet_df.rename(columns = column_map, inplace=True) + # Build an image and serial dict assuming the cubes will be named as the IPFs are + image_dict = {i: i + args.extension for i in pd.unique(socet_df['ipf_file'])} serial_dict = serial_numbers(image_dict, cub_path) # creates the control network - cn.to_isis(os.path.join(outpath, cnet_out + '.net'), socet_df, serial_dict) + cn.to_isis(os.path.join(outpath, cnet_out + '.net'), socet_df, serial_dict, targetname = args.target_name) if __name__ == '__main__': main(parse_args()) diff --git a/notebooks/Socet2ISIS.ipynb b/notebooks/Socet2ISIS.ipynb index a0e578889f8f89a93fed0d8d1f25978b9cc08d4c..af97cd92fb1c9fe8bcc5c1b299ef190f4fe2a767 100644 --- a/notebooks/Socet2ISIS.ipynb +++ b/notebooks/Socet2ISIS.ipynb @@ -8,17 +8,13 @@ "source": [ "import os\n", "import sys\n", - "from functools import singledispatch\n", - "import warnings\n", "\n", "import pandas as pd\n", - "import numpy as np\n", "import math\n", - "import pyproj\n", "\n", - "from plio.examples import get_path\n", - "from plio.io.io_bae import read_gpf, read_ipf\n", - "from collections import defaultdict\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 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" ] @@ -29,267 +25,19 @@ "metadata": {}, "outputs": [], "source": [ - "# Reads a .atf file and outputs all of the \n", - "# .ipf, .gpf, .sup, .prj, and path to locate the \n", - "# .apf file (should be the same as all others) \n", - "def read_atf(atf_file):\n", - " with open(atf_file) as f:\n", - " \n", - " # Extensions of files we want\n", - " files_ext = ['.prj', '.sup', '.ipf', '.gpf']\n", - " files_dict = []\n", - " files = defaultdict(list)\n", - "\n", - " for line in f:\n", - " ext = os.path.splitext(line)[-1].strip()\n", - " \n", - " # Check is needed for split as all do not have a space\n", - " if ext in files_ext:\n", - " \n", - " # If it is the .prj file, it strips the directory away and grabs file name\n", - " if ext == '.prj':\n", - " files[ext].append(line.strip().split(' ')[1].split('\\\\')[-1])\n", - " \n", - " # If the ext is in the list of files we care about, it addes to the dict\n", - " files[ext].append(line.strip().split(' ')[-1])\n", - " \n", - " else:\n", - " \n", - " # Adds to the dict even if not in files we care about\n", - " files[ext.strip()].append(line)\n", - " \n", - " # Gets the base filepath\n", - " files['basepath'] = os.path.dirname(os.path.abspath(atf_file))\n", - " \n", - " # Creates a dict out of file lists for GPF, PRJ, IPF, and ATF\n", - " files_dict = (dict(files_dict))\n", - " \n", - " # Sets the value of IMAGE_IPF to all IPF images\n", - " files_dict['IMAGE_IPF'] = files['.ipf']\n", - " \n", - " # Sets the value of IMAGE_SUP to all SUP images\n", - " files_dict['IMAGE_SUP'] = files['.sup']\n", - " \n", - " # Sets value for GPF file\n", - " files_dict['GP_FILE'] = files['.gpf'][0]\n", - " \n", - " # Sets value for PRJ file\n", - " files_dict['PROJECT'] = files['.prj'][0]\n", - " \n", - " # Sets the value of PATH to the path of the ATF file\n", - " files_dict['PATH'] = files['basepath']\n", - " \n", - " return files_dict\n", - "\n", - "# converts columns l. and s. to isis\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", - " \n", - " if i == 3:\n", - " line_size = line.split(' ')\n", - " line_size = line_size[-1].strip()\n", - " assert int(line_size) > 0, \"Line number {} from {} is a negative number: Invalid Data\".format(line_size, record['ipf_file'])\n", - " \n", - " if i == 4:\n", - " sample_size = line.split(' ')\n", - " sample_size = sample_size[-1].strip()\n", - " assert int(sample_size) > 0, \"Sample number {} from {} is a negative number: Invalid Data\".format(sample_size, record['ipf_file'])\n", - " break\n", - " \n", - " \n", - " line_size = int(line_size)/2.0 + record['l.'] + 1\n", - " sample_size = int(sample_size)/2.0 + record['s.'] + 1\n", - " return sample_size, line_size, img_index\n", - " \n", - "# converts known to ISIS keywords\n", - "def known(record):\n", - " if record['known'] == 0:\n", - " return 'Free'\n", - " \n", - " elif record['known'] == 1 or record['known'] == 2 or record['known'] == 3:\n", - " return 'Constrained'\n", - " \n", - "# converts +/- 180 system to 0 - 360 system\n", - "def to_360(num):\n", - " return num % 360\n", - "\n", - "# ocentric to ographic latitudes\n", - "def oc2og(dlat, dMajorRadius, dMinorRadius):\n", - " try: \n", - " dlat = math.radians(dlat)\n", - " dlat = math.atan(((dMajorRadius / dMinorRadius)**2) * (math.tan(dlat)))\n", - " dlat = math.degrees(dlat)\n", - " except:\n", - " print (\"Error in oc2og conversion\")\n", - " return dlat\n", - "\n", - "# ographic to ocentric latitudes\n", - "def og2oc(dlat, dMajorRadius, dMinorRadius):\n", - " try:\n", - " dlat = math.radians(dlat)\n", - " dlat = math.atan((math.tan(dlat) / ((dMajorRadius / dMinorRadius)**2)))\n", - " dlat = math.degrees(dlat)\n", - " except:\n", - " print (\"Error in og2oc conversion\")\n", - " return dlat\n", - "\n", - "# gets eRadius and pRadius from a .prj file\n", - "def get_axis(file):\n", - " with open(file) as f:\n", - " from collections import defaultdict\n", - "\n", - " files = defaultdict(list)\n", - " \n", - " for line in f:\n", - " \n", - " ext = line.strip().split(' ')\n", - " files[ext[0]].append(ext[-1])\n", - " \n", - " eRadius = float(files['A_EARTH'][0])\n", - " pRadius = eRadius * (1 - float(files['E_EARTH'][0]))\n", - " \n", - " return eRadius, pRadius\n", - " \n", - "# function to convert lat_Y_North to ISIS_lat\n", - "def lat_ISIS_coord(record, semi_major, semi_minor):\n", - " ocentric_coord = og2oc(record['lat_Y_North'], semi_major, semi_minor)\n", - " coord_360 = to_360(ocentric_coord)\n", - " return coord_360\n", - "\n", - "# function to convert long_X_East to ISIS_lon\n", - "def lon_ISIS_coord(record, semi_major, semi_minor):\n", - " ocentric_coord = og2oc(record['long_X_East'], semi_major, semi_minor)\n", - " coord_360 = to_360(ocentric_coord)\n", - " return coord_360\n", - "\n", - "def body_fix(record, semi_major, semi_minor, inverse=False):\n", - " \"\"\"\n", - " Parameters\n", - " ----------\n", - " record : ndarray\n", - " (n,3) where columns are x, y, height or lon, lat, alt\n", - " \"\"\"\n", - " \n", - " ecef = pyproj.Proj(proj='geocent', a=semi_major, b=semi_minor)\n", - " lla = pyproj.Proj(proj='latlon', a=semi_major, b=semi_minor)\n", - " \n", - " if inverse:\n", - " lon, lat, height = pyproj.transform(ecef, lla, record[0], record[1], record[2])\n", - " return lon, lat, height\n", - " else:\n", - " y, x, z = pyproj.transform(lla, ecef, record[0], record[1], record[2])\n", - " return y, x, z\n", - "\n", - "def ignore_toggle(record):\n", - " if record['stat'] == 0:\n", - " return True\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", + " cub_path = cub_file_path\n", + "\n", + " if(outpath):\n", + " outpath = outpath\n", " else:\n", - " return False\n", - "\n", - "# TODO: Does isis cnet need a convariance matrix for sigmas? Even with a static matrix of 1,1,1,1 \n", - "def compute_sigma_covariance_matrix(lat, lon, rad, latsigma, lonsigma, radsigma, semimajor_axis):\n", - " \n", - " \"\"\"\n", - " Given geospatial coordinates, desired accuracy sigmas, and an equitorial radius, compute a 2x3\n", - " sigma covariange matrix.\n", - " Parameters\n", - " ----------\n", - " lat : float\n", - " A point's latitude in degrees\n", - " lon : float\n", - " A point's longitude in degrees\n", - " rad : float\n", - " The radius (z-value) of the point in meters\n", - " latsigma : float\n", - " The desired latitude accuracy in meters (Default 10.0)\n", - " lonsigma : float\n", - " The desired longitude accuracy in meters (Default 10.0)\n", - " radsigma : float\n", - " The desired radius accuracy in meters (Defualt: 15.0)\n", - " semimajor_axis : float\n", - " The semi-major or equitorial radius in meters (Default: 1737400.0 - Moon)\n", - " Returns\n", - " -------\n", - " rectcov : ndarray\n", - " (2,3) covariance matrix\n", - " \"\"\"\n", - " \n", - " lat = math.radians(lat)\n", - " lon = math.radians(lon)\n", - " \n", - " # SetSphericalSigmasDistance\n", - " scaled_lat_sigma = latsigma / semimajor_axis\n", - "\n", - " # This is specific to each lon.\n", - " scaled_lon_sigma = lonsigma * math.cos(lat) / semimajor_axis\n", - " \n", - " # SetSphericalSigmas\n", - " cov = np.eye(3,3)\n", - " cov[0,0] = scaled_lat_sigma ** 2\n", - " cov[1,1] = scaled_lon_sigma ** 2\n", - " cov[2,2] = radsigma ** 2\n", - " \n", - " # Approximate the Jacobian\n", - " j = np.zeros((3,3))\n", - " cosphi = math.cos(lat)\n", - " sinphi = math.sin(lat)\n", - " coslambda = math.cos(lon)\n", - " sinlambda = math.sin(lon)\n", - " rcosphi = rad * cosphi\n", - " rsinphi = rad * sinphi\n", - " j[0,0] = -rsinphi * coslambda\n", - " j[0,1] = -rcosphi * sinlambda\n", - " j[0,2] = cosphi * coslambda\n", - " j[1,0] = -rsinphi * sinlambda\n", - " j[1,1] = rcosphi * coslambda\n", - " j[1,2] = cosphi * sinlambda\n", - " j[2,0] = rcosphi\n", - " j[2,1] = 0.\n", - " j[2,2] = sinphi\n", - " mat = j.dot(cov)\n", - " mat = mat.dot(j.T)\n", - " rectcov = np.zeros((2,3))\n", - " rectcov[0,0] = mat[0,0]\n", - " rectcov[0,1] = mat[0,1]\n", - " rectcov[0,2] = mat[0,2]\n", - " rectcov[1,0] = mat[1,1]\n", - " rectcov[1,1] = mat[1,2]\n", - " rectcov[1,2] = mat[2,2]\n", - " \n", - " return rectcov\n", - "# return np.array([[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]])\n", - "\n", - "\n", - "def compute_cov_matrix(record, semimajor_axis):\n", - " cov_matrix = compute_sigma_covariance_matrix(record['lat_Y_North'], record['long_X_East'], record['ht'], record['sig0'], record['sig1'], record['sig2'], semimajor_axis)\n", - " return cov_matrix.ravel().tolist()\n", - "\n", - "# applys transformations to columns\n", - "def apply_transformations(atf_dict, df):\n", - " prj_file = os.path.join(atf_dict['PATH'], atf_dict['PROJECT'])\n", - " \n", - " eRadius, pRadius = get_axis(prj_file)\n", - " \n", - " lla = np.array([[df['long_X_East']], [df['lat_Y_North']], [df['ht']]])\n", - " \n", - " ecef = body_fix(lla, semi_major = eRadius, semi_minor = pRadius, inverse=False)\n", - " \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['long_X_East'] = ecef[0][0]\n", - " df['lat_Y_North'] = ecef[1][0]\n", - " df['ht'] = ecef[2][0] \n", - " df['aprioriCovar'] = df.apply(compute_cov_matrix, semimajor_axis = eRadius, axis=1)\n", - " df['ignore'] = df.apply(ignore_toggle, axis=1)\n", + " outpath = os.path.split(at_file)[0]\n", " \n", - "def socet2isis(prj_file):\n", " # Read in and setup the atf dict of information\n", - " atf_dict = read_atf(prj_file)\n", + " atf_dict = read_atf(at_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", @@ -314,58 +62,137 @@ " 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_remap = {'l.': 'y', 's.': 'x',\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", - " 'sig_l': 'linesigma', 'sig_s': 'samplesigma'}\n", + " column_map = {'pt_id': 'id', 'l.': 'y', 's.': 'x',\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", + " 'sig_l': 'linesigma', 'sig_s': 'samplesigma'}\n", " \n", " # Rename the columns using the column remap above\n", - " socet_df.rename(columns = column_remap, inplace=True)\n", + " socet_df.rename(columns = column_map, inplace=True)\n", " \n", - " # Return the socet dataframe to be converted to a control net\n", - " return socet_df\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 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", - " snum = sn.generate_serial_number(os.path.join(path, image + extension))\n", - " snum = snum.replace('Mars_Reconnaissance_Orbiter', 'MRO')\n", - " serial_dict[image] = snum\n", - " return serial_dict\n", - "\n" + " # creates the control network\n", + " 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": [ + "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", + " 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", + " # 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)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "# Setup stuffs for the cub information namely the path and extension\n", - "path = '/path/where/cub/files/are/'\n", + "cub_path = '/Path/to/cubs'\n", "\n", - "# Extension of your cub files\n", - "extension = '.something.cub'\n", + "# Name of the target body\n", + "targetname = 'Mars'\n", + "extension = 'cub.-->extension<--'\n", "\n", "# Path to atf file\n", - "atf_file = ('/path/to/atf/file')\n", + "atf_file = 'Path/to/socket/set/at_file.atf'\n", + "\n", + "socet2isis(atf_file, cub_path, extension, targetname)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 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", + "eccentricity = 1.08339143554195e-001\n", + "\n", + "cub_path = 'Path/to/cubs'\n", + "extension = 'cub.-->extension<--'\n", "\n", - "socet_df = socet2isis(atf_file)\n", + "# List of cubes to use\n", + "cub_list = ['D06_029601_1846_XN_04N224W', \n", + " 'F05_037684_1857_XN_05N224W']\n", "\n", - "images = pd.unique(socet_df['ipf_file'])\n", + "out_gpf = \"/Users/adampaquette/Desktop/InSightE09_XW.gpf\"\n", "\n", - "serial_dict = serial_numbers(images, path, extension)\n", + "adjusted_flag = False\n", "\n", - "# creates the control network\n", - "cn.to_isis('/path/you/want/the/cnet/to/be/in/cn.net', socet_df, serial_dict)" + "isis2socet(cnet, eRadius, eccentricity, cub_path, extension, cub_list, out_gpf, adjusted_flag)" ] }, { @@ -392,7 +219,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.4" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/plio/examples/SocetSet/cub_map.csv b/plio/examples/SocetSet/cub_map1.csv similarity index 100% rename from plio/examples/SocetSet/cub_map.csv rename to plio/examples/SocetSet/cub_map1.csv diff --git a/plio/examples/SocetSet/cub_map2.csv b/plio/examples/SocetSet/cub_map2.csv new file mode 100644 index 0000000000000000000000000000000000000000..a9401a26740e13f6e657c74be5d60bab898ec5c4 --- /dev/null +++ b/plio/examples/SocetSet/cub_map2.csv @@ -0,0 +1,2 @@ +D06_029601_1846_XN_04N224W,D06_029601_1846_XN_04N224W.8bit.cub +F05_037684_1857_XN_05N224W,F05_037684_1857_XN_05N224W.8bit.cub diff --git a/plio/io/io_bae.py b/plio/io/io_bae.py index 2810a8e19e91a8c9ad2472b2adb3d92783ff1faf..16b2a68186a00ea1b387833f356f7bd546d707db 100644 --- a/plio/io/io_bae.py +++ b/plio/io/io_bae.py @@ -1,6 +1,7 @@ -import json import re import os +import json +from collections import defaultdict from functools import singledispatch import numpy as np @@ -290,44 +291,48 @@ def read_atf(atf_file): project """ with open(atf_file) as f: - - files = [] - ipf = [] - sup = [] + # Extensions of files we want + files_ext = ['.prj', '.sup', '.ipf', '.gpf'] files_dict = [] + files = defaultdict(list) - # Grabs every PRJ, GPF, SUP, and IPF image from the ATF file for line in f: - if line[-4:-1] == 'prj' or line[-4:-1] == 'gpf' or line[-4:-1] == 'sup' or line[-4:-1] == 'ipf' or line[-4:-1] == 'atf': - files.append(line) + ext = os.path.splitext(line)[-1].strip() - files = np.array(files) + # Check is needed for split as all do not have a space + if ext in files_ext: - # Creates appropriate arrays for certain files in the right format - for file in files: - file = file.strip() - file = file.split(' ') + # If it is the .prj file, it strips the directory away and grabs file name + if ext == '.prj': + files[ext].append(line.strip().split(' ')[1].split('\\')[-1]) - # Grabs all the IPF files - if file[1].endswith('.ipf'): - ipf.append(file[1]) + # If the ext is in the list of files we care about, it addes to the dict + files[ext].append(line.strip().split(' ')[-1]) - # Grabs all the SUP files - if file[1].endswith('.sup'): - sup.append(file[1]) + else: - files_dict.append(file) + # Adds to the dict even if not in files we care about + files[ext.strip()].append(line) + + # Gets the base filepath + files['basepath'] = os.path.dirname(os.path.abspath(atf_file)) # Creates a dict out of file lists for GPF, PRJ, IPF, and ATF files_dict = (dict(files_dict)) # Sets the value of IMAGE_IPF to all IPF images - files_dict['IMAGE_IPF'] = ipf + files_dict['IMAGE_IPF'] = files['.ipf'] # Sets the value of IMAGE_SUP to all SUP images - files_dict['IMAGE_SUP'] = sup + files_dict['IMAGE_SUP'] = files['.sup'] + + # Sets value for GPF file + files_dict['GP_FILE'] = files['.gpf'][0] + + # Sets value for PRJ file + files_dict['PROJECT'] = files['.prj'][0] # Sets the value of PATH to the path of the ATF file - files_dict['PATH'] = os.path.dirname(os.path.abspath(atf_file)) + files_dict['PATH'] = files['basepath'] return files_dict diff --git a/plio/io/io_controlnetwork.py b/plio/io/io_controlnetwork.py index a52d58097723627958bf7bbc54c847e57c8f60e0..b9c6fefe72b453bdb151d22397d23ba14ccf73d3 100644 --- a/plio/io/io_controlnetwork.py +++ b/plio/io/io_controlnetwork.py @@ -187,11 +187,12 @@ class IsisStore(object): header_bytes = find_in_dict(pvl_header, 'HeaderBytes') point_start_byte = find_in_dict(pvl_header, 'PointsStartByte') version = find_in_dict(pvl_header, 'Version') + if version == 2: - point_attrs = [i for i in cnf._CONTROLPOINTFILEENTRYV0002.fields_by_name if i != 'measures'] - measure_attrs = [i for i in cnf._CONTROLPOINTFILEENTRYV0002_MEASURE.fields_by_name] + self.point_attrs = [i for i in cnf._CONTROLPOINTFILEENTRYV0002.fields_by_name if i != 'measures'] + self.measure_attrs = [i for i in cnf._CONTROLPOINTFILEENTRYV0002_MEASURE.fields_by_name] - cols = point_attrs + measure_attrs + cols = self.point_attrs + self.measure_attrs cp = cnf.ControlPointFileEntryV0002() self._handle.seek(header_start_byte) @@ -203,10 +204,10 @@ class IsisStore(object): pts = [] for s in pbuf_header.pointMessageSizes: cp.ParseFromString(self._handle.read(s)) - pt = [getattr(cp, i) for i in point_attrs if i != 'measures'] + pt = [getattr(cp, i) for i in self.point_attrs if i != 'measures'] for measure in cp.measures: - meas = pt + [getattr(measure, j) for j in measure_attrs] + meas = pt + [getattr(measure, j) for j in self.measure_attrs] pts.append(meas) df = IsisControlNetwork(pts, columns=cols) df.header = pvl_header diff --git a/plio/spatial/transformations.py b/plio/spatial/transformations.py index efc4ad609424a1c88e4ca778370b647249655e4c..237f403aee680b52d0461737275f0637502ef7a6 100644 --- a/plio/spatial/transformations.py +++ b/plio/spatial/transformations.py @@ -1,8 +1,12 @@ import os +import pvl import math import pyproj +import numpy as np + import plio.io.isis_serial_number as sn +from plio.utils.utils import find_in_dict def line_sample_size(record, path): """ @@ -163,7 +167,7 @@ def get_axis(file): files[ext[0]].append(ext[-1]) eRadius = float(files['A_EARTH'][0]) - pRadius = eRadius * (1 - float(files['E_EARTH'][0])) + pRadius = eRadius * math.sqrt(1 - (float(files['E_EARTH'][0]) ** 2)) return eRadius, pRadius @@ -217,7 +221,57 @@ def lon_ISIS_coord(record, semi_major, semi_minor): coord_360 = to_360(ocentric_coord) return coord_360 -def body_fix(record, semi_major, semi_minor): +def lat_socet_coord(record, semi_major, semi_minor): + """ + Function to convert lat_Y_North to ISIS_lat + + Parameters + ---------- + record : object + Pandas series object + + semi_major : float + Radius from the center of the body to the equater + + semi_minor : float + Radius from the pole to the center of mass + + Returns + ------- + coord_360 : float + Converted latitude into ocentric space, and mapped + into 0 to 360 + """ + ographic_coord = oc2og(record['lat_Y_North'], semi_major, semi_minor) + coord_180 = ((ographic_coord + 180) % 360) - 180 + return coord_180 + +def lon_socet_coord(record, semi_major, semi_minor): + """ + Function to convert long_X_East to ISIS_lon + + Parameters + ---------- + record : object + Pandas series object + + semi_major : float + Radius from the center of the body to the equater + + semi_minor : float + Radius from the pole to the center of mass + + Returns + ------- + coord_360 : float + Converted longitude into ocentric space, and mapped + into 0 to 360 + """ + ographic_coord = oc2og(record['long_X_East'], semi_major, semi_minor) + coord_180 = ((ographic_coord + 180) % 360) - 180 + return coord_180 + +def body_fix(record, semi_major, semi_minor, inverse = False, **kwargs): """ Transforms latitude, longitude, and height of a socet point into a body fixed point @@ -241,10 +295,85 @@ def body_fix(record, semi_major, semi_minor): """ ecef = pyproj.Proj(proj='geocent', a=semi_major, b=semi_minor) lla = pyproj.Proj(proj='latlon', a=semi_major, b=semi_minor) - lon, lat, height = pyproj.transform(lla, ecef, record['long_X_East'], record['lat_Y_North'], record['ht']) - return lon, lat, height -def apply_transformations(atf_dict, df): + if inverse: + lon, lat, height = pyproj.transform(ecef, lla, record[0], record[1], record[2], **kwargs) + return lon, lat, height + else: + y, x, z = pyproj.transform(lla, ecef, record[0], record[1], record[2], **kwargs) + return y, x, z + +def stat_toggle(record): + if record['stat'] == 0: + return True + else: + return False + +def apply_isis_transformations(df, eRadius, pRadius, serial_dict, extension, cub_path): + """ + Takes a atf dictionary and a socet dataframe and applies the necessary + transformations to convert that dataframe into a isis compatible + dataframe + + Parameters + ---------- + df : object + Pandas dataframe object + + eRadius : float + Equitorial radius + + pRadius : float + Polar radius + + serial_dict : dict + Dictionary mapping serials as keys to images as the values + + extension : str + String extension of all cubes being used + + cub_path : str + Path to all cubes being used + + """ + # Convert from geocentered coords (x, y, z), to lat lon coords (latitude, longitude, alltitude) + ecef = np.array([[df['long_X_East']], [df['lat_Y_North']], [df['ht']]]) + lla = body_fix(ecef, semi_major = eRadius, semi_minor = pRadius, inverse=True) + df['long_X_East'], df['lat_Y_North'], df['ht'] = lla[0][0], lla[1][0], lla[2][0] + + # df['lat_Y_North'] = df.apply(lat_socet_coord, semi_major = eRadius, semi_minor = pRadius, axis=1) + # df['long_X_East'] = df.apply(lon_socet_coord, semi_major = eRadius, semi_minor = pRadius, axis=1) + + # Update the stat fields and add the val field as it is just a clone of stat + df['stat'] = df.apply(ignore_toggle, axis = 1) + df['val'] = df['stat'] + + # Update the known field, add the ipf_file field for saving, and + # update the line, sample using data from the cubes + df['known'] = df.apply(reverse_known, axis = 1) + df['ipf_file'] = df['serialnumber'].apply(lambda serial_number: serial_dict[serial_number]) + df['l.'], df['s.'] = zip(*df.apply(fix_sample_line, serial_dict = serial_dict, + extension = extension, + cub_path = cub_path, axis = 1)) + + # Add dummy for generic value setting + x_dummy = lambda x: np.full(len(df), x) + + df['sig0'] = x_dummy(1) + df['sig1'] = x_dummy(1) + df['sig2'] = x_dummy(1) + + df['res0'] = x_dummy(0) + df['res1'] = x_dummy(0) + df['res2'] = x_dummy(0) + + df['fid_x'] = x_dummy(0) + df['fid_y'] = x_dummy(0) + + df['no_obs'] = x_dummy(1) + df['fid_val'] = x_dummy(0) + +def apply_socet_transformations(atf_dict, df): """ Takes a atf dictionary and a socet dataframe and applies the necessary transformations to convert that dataframe into a isis compatible @@ -259,15 +388,24 @@ def apply_transformations(atf_dict, df): Pandas dataframe object """ - prj_file = os.path.join(atf_dict['PATH'], atf_dict['PROJECT'].split('\\')[-1]) + prj_file = os.path.join(atf_dict['PATH'], atf_dict['PROJECT']) eRadius, pRadius = get_axis(prj_file) + # df['lat_Y_North'] = df.apply(lat_ISIS_coord, semi_major = eRadius, semi_minor = pRadius, axis=1) + # df['long_X_East'] = df.apply(lon_ISIS_coord, semi_major = eRadius, semi_minor = pRadius, axis=1) + + lla = np.array([[df['long_X_East']], [df['lat_Y_North']], [df['ht']]]) + + ecef = body_fix(lla, semi_major = eRadius, semi_minor = pRadius, inverse=False) + df['s.'], df['l.'], df['image_index'] = (zip(*df.apply(line_sample_size, path = atf_dict['PATH'], axis=1))) df['known'] = df.apply(known, axis=1) - df['lat_Y_North'] = df.apply(lat_ISIS_coord, semi_major = eRadius, semi_minor = pRadius, axis=1) - df['long_X_East'] = df.apply(lon_ISIS_coord, semi_major = eRadius, semi_minor = pRadius, axis=1) - df['long_X_East'], df['lat_Y_North'], df['ht'] = zip(*df.apply(body_fix, semi_major = eRadius, semi_minor = pRadius, axis = 1)) + df['long_X_East'] = ecef[0][0] + df['lat_Y_North'] = ecef[1][0] + df['ht'] = ecef[2][0] + df['aprioriCovar'] = df.apply(compute_cov_matrix, semimajor_axis = eRadius, axis=1) + df['stat'] = df.apply(stat_toggle, axis=1) def serial_numbers(image_dict, path): """ @@ -290,3 +428,158 @@ def serial_numbers(image_dict, path): for key in image_dict: serial_dict[key] = sn.generate_serial_number(os.path.join(path, image_dict[key])) return serial_dict + +# TODO: Does isis cnet need a convariance matrix for sigmas? Even with a static matrix of 1,1,1,1 +def compute_sigma_covariance_matrix(lat, lon, rad, latsigma, lonsigma, radsigma, semimajor_axis): + + """ + Given geospatial coordinates, desired accuracy sigmas, and an equitorial radius, compute a 2x3 + sigma covariange matrix. + Parameters + ---------- + lat : float + A point's latitude in degrees + + lon : float + A point's longitude in degrees + + rad : float + The radius (z-value) of the point in meters + + latsigma : float + The desired latitude accuracy in meters (Default 10.0) + + lonsigma : float + The desired longitude accuracy in meters (Default 10.0) + + radsigma : float + The desired radius accuracy in meters (Defualt: 15.0) + + semimajor_axis : float + The semi-major or equitorial radius in meters (Default: 1737400.0 - Moon) + Returns + ------- + rectcov : ndarray + (2,3) covariance matrix + """ + lat = math.radians(lat) + lon = math.radians(lon) + + # SetSphericalSigmasDistance + scaled_lat_sigma = latsigma / semimajor_axis + + # This is specific to each lon. + scaled_lon_sigma = lonsigma * math.cos(lat) / semimajor_axis + + # SetSphericalSigmas + cov = np.eye(3,3) + cov[0,0] = math.radians(scaled_lat_sigma) ** 2 + cov[1,1] = math.radians(scaled_lon_sigma) ** 2 + cov[2,2] = radsigma ** 2 + + # Approximate the Jacobian + j = np.zeros((3,3)) + cosphi = math.cos(lat) + sinphi = math.sin(lat) + cos_lmbda = math.cos(lon) + sin_lmbda = math.sin(lon) + rcosphi = rad * cosphi + rsinphi = rad * sinphi + j[0,0] = -rsinphi * cos_lmbda + j[0,1] = -rcosphi * sin_lmbda + j[0,2] = cosphi * cos_lmbda + j[1,0] = -rsinphi * sin_lmbda + j[1,1] = rcosphi * cos_lmbda + j[1,2] = cosphi * sin_lmbda + j[2,0] = rcosphi + j[2,1] = 0. + j[2,2] = sinphi + mat = j.dot(cov) + mat = mat.dot(j.T) + rectcov = np.zeros((2,3)) + rectcov[0,0] = mat[0,0] + rectcov[0,1] = mat[0,1] + rectcov[0,2] = mat[0,2] + rectcov[1,0] = mat[1,1] + rectcov[1,1] = mat[1,2] + rectcov[1,2] = mat[2,2] + + return rectcov + +def compute_cov_matrix(record, semimajor_axis): + cov_matrix = compute_sigma_covariance_matrix(record['lat_Y_North'], record['long_X_East'], record['ht'], record['sig0'], record['sig1'], record['sig2'], semimajor_axis) + return cov_matrix.ravel().tolist() + +def reverse_known(record): + """ + Converts the known field from an isis dataframe into the + socet known column + + Parameters + ---------- + record : object + Pandas series object + + Returns + ------- + : str + String representation of a known field + """ + record_type = record['known'] + if record_type == 0 or record_type == 2: + return 0 + + elif record_type == 1 or record_type == 3 or record_type == 4: + return 3 + +def fix_sample_line(record, serial_dict, extension, cub_path): + """ + Extracts the sample, line data from a cube and computes deviation from the + center of the image + + Parameters + ---------- + record : dict + Dict containing the key serialnumber, l., and s. + + serial_dict : dict + Maps serial numbers to images + + extension : str + Extension for cube being looked at + + cub_path : str + Path to a given cube being looked at + + Returns + ------- + new_line : int + new line deviation from the center + + new_sample : int + new sample deviation from the center + + """ + # Cube location to load + cube = pvl.load(os.path.join(cub_path, serial_dict[record['serialnumber']] + extension)) + line_size = find_in_dict(cube, 'Lines') + sample_size = find_in_dict(cube, 'Samples') + + new_line = record['l.'] - (int(line_size/2.0)) - 1 + new_sample = record['s.'] - (int(sample_size/2.0)) - 1 + + return new_line, new_sample + +def ignore_toggle(record): + """ + Maps the stat column in a record to 0 or 1 based on True or False + + Parameters + ---------- + record : dict + Dict containing the key stat + """ + if record['stat'] == True: + return 0 + else: + return 1 diff --git a/setup.py b/setup.py index 6932e2f2e29d75c78259d5c72149c418fd6c87fe..13b60810dfa75128d68ba8040f0ac70c9048da85 100644 --- a/setup.py +++ b/setup.py @@ -36,7 +36,7 @@ def setup_package(): package_data={'plio' : list(examples) + ['data/*.db', 'data/*.py'] +\ ['sqlalchemy_json/*.py', 'sqlalchemy_json/LICENSE']}, zip_safe=False, - scripts=['bin/socet2isis'], + scripts=['bin/socet2isis', 'bin/isis2socet'], install_requires=[ 'gdal', 'numpy', @@ -46,7 +46,7 @@ def setup_package(): 'pandas', 'sqlalchemy', 'pyyaml', - 'networkx', + 'networkx', 'affine', 'scipy'], classifiers=[