diff --git a/notebooks/KernelSlice.ipynb b/notebooks/KernelSlice.ipynb index e16f8112b42715c62c55177086797753d1fc57c1..f51500b717ae2ae29b3fe28dc26b3bd23bf5162c 100644 --- a/notebooks/KernelSlice.ipynb +++ b/notebooks/KernelSlice.ipynb @@ -15,27 +15,13 @@ "from itertools import chain\n", "import io\n", "import networkx as nx\n", - "import tempfile\n", "\n", "\n", "# These should be provided when running this script. \n", "cube = \"/work/users/sgstapleton/kernel_split/EN1072174528M.cub\"\n", - "output_dir = \"/work/users/sgstapleton/kernel_split/\" # Output dir for created kernel files\n", + "output_dir = \"/Users/jmapel/ale/nb_test\" # Output dir for created kernel files\n", "data_dir = \"/usgs/cpkgs/isis3/data/\" # Dir of where to pull original kernels from\n", - "ckslicer_loc = \"/work/users/sgstapleton/kernel_split/ckslicer\"\n", - "\n", - " \n", - "def get_reference_id(bc_filename):\n", - " brief = subprocess.check_output([\"ckbrief\", bc_filename])\n", - " reference = None\n", - "\n", - " for line in brief.splitlines():\n", - " line = str(line)\n", - " if('Object:' in line):\n", - " reference = int(line.strip(\"''\").split()[1])\n", - " \n", - " return reference\n", - "\n", + "ckslicer_loc = \"/Users/jmapel/ale/ckslicer\"\n", "\n", "def add_light_time_correction(cube_info):\n", " \n", @@ -60,65 +46,7 @@ " cube_info.update(PaddedStartTimeSCLK = padded_start_sclk)\n", " cube_info.update(PaddedEndTimeSCLK = padded_end_sclk)\n", " cube_info.update(PaddedStartTimeUTC = padded_start_utc)\n", - " cube_info.update(PaddedEndTimeUTC = padded_end_utc)\n", - " \n", - "\n", - "def get_body_ids(spk_file, body_id):\n", - " brief = subprocess.check_output([\"brief\", \"-c {}\".format(spk_file)])\n", - " \n", - " # Convert from bytes\n", - " brief = brief.decode(\"utf-8\")\n", - " brief_io = io.StringIO(brief)\n", - " line = brief_io.readline()\n", - " edge_list = []\n", - " \n", - " while(line):\n", - " \n", - " if(\"w.r.t\" in line):\n", - " bodies_results = re.findall('\\((.*?)\\)', line)\n", - " edge_list.append(bodies_results)\n", - " \n", - " line = brief_io.readline()\n", - " \n", - " G = nx.DiGraph()\n", - " G.add_edges_from(edge_list)\n", - " \n", - " body_ids = []\n", - " for path in nx.all_simple_paths(G, body_id, '0'):\n", - " body_ids = body_ids + path\n", - " \n", - " body_ids = set(body_ids)\n", - "\n", - " return body_ids\n", - "\n", - "\n", - "def output_spkmerge_config(config_file, cube_info, output_dir):\n", - " \n", - " with open(config_file, \"w+\") as config:\n", - " config.write('''LEAPSECONDS_KERNEL = {}\\n'''.format(cube_info['LeapSecond'][0]))\n", - " \n", - " for kern in cube_info['InstrumentPosition']:\n", - " \n", - " basename = os.path.basename(kern).split('.')[0]\n", - " filename, file_extension = os.path.splitext(kern)\n", - " new_kern = output_dir + basename + \"_merged\" + file_extension\n", - " \n", - " config.write(''' SPK_KERNEL = {}\\n'''.format(new_kern))\n", - "\n", - " body_ids = get_body_ids(kern, str(cube_info['SpacecraftID']))\n", - "\n", - " for body in body_ids:\n", - "\n", - " if body != '0':\n", - "\n", - " config.write(''' SOURCE_SPK_KERNEL = {}\\n'''.format(kern))\n", - " config.write(''' INCLUDE_COMMENTS = no\\n''')\n", - " config.write(''' BODIES = {}\\n'''.format(body))\n", - " config.write(''' BEGIN_TIME = {}\\n'''.format(cube_info['PaddedStartTimeUTC']))\n", - " config.write(''' END_TIME = {}\\n'''.format(cube_info['PaddedEndTimeUTC']))\n", - " \n", - " config.close()\n", - " " + " cube_info.update(PaddedEndTimeUTC = padded_end_utc)" ] }, { @@ -156,53 +84,61 @@ "add_light_time_correction(cube_info)\n", "\n", "# For each binary ck kernel specified in cube, run the ckslicer, comment and to-transfer commands\n", - "for ck in cube_info['InstrumentPointing']:\n", - " if ck.endswith('.bc'):\n", - " \n", - " # Get reference id associated with ck\n", - " reference_id = get_reference_id(ck)\n", - " \n", - " ck_filename = os.path.basename(ck).split('.')[0]\n", - " ck_path, ck_file_extension = os.path.splitext(ck)\n", - " output_kern = output_dir + ck_filename + '_sliced' + ck_file_extension\n", - " \n", + "for ck in [k for k in kernels if k.lower().endswith('.bc')]:\n", + " ck_path, ck_file_extension = os.path.splitext(ck)\n", + " ck_basename = os.path.basename(ck_path)\n", + " for frame in util.get_ck_frames(ck):\n", + " output_basename = os.path.join(output_dir, ck_basename + '_sliced' + str(frame))\n", + " output_kern = output_basename + ck_file_extension\n", + " output_comments = output_basename + '.cmt'\n", + "\n", " # Create new sliced ck kernel\n", " ckslicer_command = [ckslicer_loc, \n", " '-LSK {}'.format(cube_info['LeapSecond'][0]), \n", " '-SCLK {}'.format(cube_info['SpacecraftClock'][0]), \n", " '-INPUTCK {}'.format(ck), \n", " '-OUTPUTCK {}'.format(output_kern),\n", - " '-ID {}'.format(reference_id),\n", + " '-ID {}'.format(frame),\n", " '-TIMETYPE {}'.format('SCLK'),\n", " '-START {}'.format(cube_info['PaddedStartTimeSCLK']),\n", " '-STOP {}'.format(cube_info['PaddedEndTimeSCLK'])]\n", - " subprocess.call(ckslicer_command)\n", - " \n", + " subprocess.run(ckslicer_command, check=True)\n", + "\n", " # Remove old comments from new ck kernel\n", " commnt_command = ['commnt', '-d {}'.format(output_kern)]\n", - " subprocess.call(commnt_command)\n", - " \n", - " # Makes a new txt file for the only comments that should be stored in the new ck file\n", - " with open(output_dir + \"temp_commnts.txt\",\"w+\") as f:\n", - " f.write(\"This CK is for testing with the image: {}\\n\".format(cube))\n", - " f.write(\"\\nThis CK was generated using the following command: {}\\n\".format(\" \".join(ckslicer_command)))\n", - " \n", + " subprocess.run(commnt_command, check=True)\n", + "\n", + " with open(output_comments, 'w+') as comment_file:\n", + " comment_file.write(\"This CK is for testing with the image: {}\\n\".format(cube))\n", + " comment_file.write(\"\\nThis CK was generated using the following command: {}\\n\")\n", + " comment_file.write(\" \".join(ckslicer_command))\n", + "\n", " # Add new comments to new ck kernel\n", - " new_commnts_command = [\"commnt\", \"-a {}\".format(output_kern), \"temp_commnts.txt\"]\n", - " subprocess.call(new_commnts_command)\n", - " \n", - " # Clean up temp comment file\n", - " os.remove(output_dir + \"temp_commnts.txt\")\n", - " \n", + " new_commnts_command = [\"commnt\", \"-a {}\".format(output_kern), output_comments]\n", + " subprocess.run(new_commnts_command, check=True)\n", + "\n", " # Create the transfer file of the new ck kernel\n", - " subprocess.call([\"toxfr\", output_kern])\n", + " subprocess.run([\"toxfr\", output_kern], check=True)\n", "\n", "# Create the config file for the spkmerge command\n", - "output_spkmerge_config(output_dir + \"spk.config\", cube_info, output_dir)\n", + "output_spk_basename = os.path.join(output_dir, os.path.basename(os.path.splitext(cube)[0]))\n", + "output_spk = output_spk_basename + '.bsp'\n", + "spk_dep_tree = util.create_spk_dependency_tree([k for k in kernels if k.lower().endswith('.bsp')])\n", + "config_string = util.spkmerge_config_string(spk_dep_tree,\n", + " output_spk,\n", + " [cube_info['TargetID'], cube_info['SpacecraftID'], 10],\n", + " cube_info['LeapSecond'][0],\n", + " cube_info['PaddedStartTimeUTC'],\n", + " cube_info['PaddedEndTimeUTC'])\n", + "with open(output_spk_basename + '.conf', 'w+') as spk_config:\n", + " spk_config.write(config_string)\n", + " \n", + "# Create the new SPK\n", + "spkmerge_command = [\"spkmerge\", spk_config.name]\n", + "subprocess.run(spkmerge_command, check=True)\n", "\n", - "# Run the spkmerge command\n", - "spkmerge_command = [\"spkmerge\", output_dir + \"spk.config\"]\n", - "subprocess.call(spkmerge_command)\n" + "# Create the transfer file of the new SPK kernel\n", + "subprocess.run([\"toxfr\", output_spk], check=True)" ] }, { @@ -233,5 +169,5 @@ } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 }