{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "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",
    "\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"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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",
    "        files = []\n",
    "        ipf = []\n",
    "        sup = []\n",
    "        files_dict = []\n",
    "        \n",
    "        # Grabs every PRJ, GPF, SUP, and IPF image from the ATF file\n",
    "        for line in f:\n",
    "            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':\n",
    "                files.append(line)\n",
    "        \n",
    "        files = np.array(files)\n",
    "        \n",
    "        # Creates appropriate arrays for certain files in the right format\n",
    "        for file in files:\n",
    "            file = file.strip()\n",
    "            file = file.split(' ')\n",
    "\n",
    "            # Grabs all the IPF files\n",
    "            if file[1].endswith('.ipf'):\n",
    "                ipf.append(file[1])\n",
    "\n",
    "            # Grabs all the SUP files\n",
    "            if file[1].endswith('.sup'):\n",
    "                sup.append(file[1])\n",
    "\n",
    "            files_dict.append(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'] = ipf\n",
    "        \n",
    "        # Sets the value of IMAGE_SUP to all SUP images\n",
    "        files_dict['IMAGE_SUP'] = sup\n",
    "        \n",
    "        # 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",
    "print(atf_dict)\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).set_index('point_id')\n",
    "ipf_df = read_ipf(ipf_list).set_index('pt_id')\n",
    "\n",
    "point_diff = ipf_df.index.difference(gpf_df.index)\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_index=True, right_index=True)\n",
    "list(new_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sample_size(record):\n",
    "    \n",
    "    with open(atf_dict['PATH'] + '/' + record['ipf_file'] + '.sup') as f:\n",
    "        sample_num = 0\n",
    "        for i, line in enumerate(f):\n",
    "            if i == 4:\n",
    "                sample_num = line.split(' ')\n",
    "                sample_num = sample_num[-1].strip()\n",
    "                break\n",
    "        assert int(sample_num) > 0, \"Sample number {} from {} is a negative number: Invalid Data\".format(sample_num, record['ipf_file'])\n",
    "            \n",
    "        sample_num = int(sample_num)/2.0 + record['s.'] + 1\n",
    "        return sample_num\n",
    "\n",
    "def line_size(record):\n",
    "    \n",
    "    with open(atf_dict['PATH'] + '/' + record['ipf_file'] + '.sup') as f:\n",
    "        sample_num = 0\n",
    "        for i, line in enumerate(f):\n",
    "            if i == 3:\n",
    "                sample_num = line.split(' ')\n",
    "                sample_num = sample_num[-1].strip()\n",
    "                break\n",
    "        assert int(sample_num) > 0, \"Sample number {} from {} is a negative number: Invalid Data\".format(sample_num, record['ipf_file'])\n",
    "            \n",
    "        sample_num = int(sample_num)/2.0 + record['l.'] + 1\n",
    "        return sample_num\n",
    "\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",
    "# new_df['s.'] = new_df.apply(sample_size, axis=1)\n",
    "# new_df['l.'] = new_df.apply(line_size, axis=1)\n",
    "# new_df['known'] = new_df.apply(known, axis=1)\n",
    "display(new_df)\n",
    "# cmeas->SetCoordinate(img_samples/2.0+sample+1,img_lines/2.0+line+1);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "import pyproj\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",
    "def get_axis():\n",
    "    with open(atf_dict['PATH'] + '/' + 'CTX_Athabasca_Middle.prj') 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):\n",
    "    with open(atf_dict['PATH'] + '/' + 'CTX_Athabasca_Middle.prj') 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",
    "        ocentric_coord = og2oc(record['lat_Y_North'], eRadius, pRadius)\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):\n",
    "    with open(atf_dict['PATH'] + '/' + 'CTX_Athabasca_Middle.prj') 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",
    "        ocentric_coord = og2oc(record['long_X_East'], eRadius, pRadius)\n",
    "        coord_360 = to_360(ocentric_coord)\n",
    "        return coord_360\n",
    "            \n",
    "def body_fixed_lat(record):\n",
    "    semi_major, semi_minor = get_axis() \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",
    "    lon, lat, height = pyproj.transform(lla, ecef, record['long_X_East'], record['lat_Y_North'], record['ht'])\n",
    "    return lat\n",
    "\n",
    "def body_fixed_lon(record):\n",
    "    semi_major, semi_minor = get_axis() \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",
    "    lon, lat, height = pyproj.transform(lla, ecef, record['long_X_East'], record['lat_Y_North'], record['ht'])\n",
    "    return lon\n",
    "\n",
    "def body_fixed_height(record):\n",
    "    semi_major, semi_minor = get_axis() \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",
    "    lon, lat, height = pyproj.transform(lla, ecef, record['long_X_East'], record['lat_Y_North'], record['ht'])\n",
    "    return height\n",
    "\n",
    "\n",
    "new_df['lat_Y_North'] = new_df.apply(lat_ISIS_coord, axis=1)\n",
    "new_df['long_X_East'] = new_df.apply(lon_ISIS_coord, axis=1)\n",
    "\n",
    "new_df['lat_Y_North'] = new_df.apply(body_fixed_lat, axis=1)\n",
    "new_df['long_X_East'] = new_df.apply(body_fixed_lon, axis=1)\n",
    "\n",
    "new_df['ht'] = new_df.apply(body_fixed_height, axis=1)\n",
    "\n",
    "\n",
    "display(new_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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",
    "        files = []\n",
    "        ipf = []\n",
    "        sup = []\n",
    "        files_dict = []\n",
    "        \n",
    "        # Grabs every PRJ, GPF, SUP, and IPF image from the ATF file\n",
    "        for line in f:\n",
    "            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':\n",
    "                files.append(line)\n",
    "        \n",
    "        files = np.array(files)\n",
    "        \n",
    "        # Creates appropriate arrays for certain files in the right format\n",
    "        for file in files:\n",
    "            file = file.strip()\n",
    "            file = file.split(' ')\n",
    "\n",
    "            # Grabs all the IPF files\n",
    "            if file[1].endswith('.ipf'):\n",
    "                ipf.append(file[1])\n",
    "\n",
    "            # Grabs all the SUP files\n",
    "            if file[1].endswith('.sup'):\n",
    "                sup.append(file[1])\n",
    "\n",
    "            files_dict.append(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'] = ipf\n",
    "        \n",
    "        # Sets the value of IMAGE_SUP to all SUP images\n",
    "        files_dict['IMAGE_SUP'] = sup\n",
    "        \n",
    "        # 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\n",
    "    \n",
    "@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",
    "    \"\"\"\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:\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"
   ]
  }
 ],
 "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.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}