Last active
July 5, 2022 13:24
-
-
Save kidpixo/d1aaba6e954e704a6b2e590365b9654b to your computer and use it in GitHub Desktop.
Write metakernel to custom location and update KERNELS. Optionally, create symlink to Kernels location.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
"""This module contains code related to NAIF/SPICE. | |
""" | |
import pathlib | |
def update_metakernel(metakernel_path=pathlib.Path, | |
metakernel_version=None, | |
KPATH=None, | |
outdir=None, | |
symlink=None, | |
loglevel=None): | |
"""Extract SKD_VERSION from input metakernel, | |
update given metakernel relative path to absolute. | |
Write the update metakernel to output with SKD_VERSION in | |
filename. | |
Return both SKD_VERSION and updated metakernel pathlib.Path | |
Parameters | |
---------- | |
metakernel_path: pathlib.Path or str | |
input metakernel path. | |
metakernel_version: str, default None. | |
Version string for the output metakernel. | |
If not defined and SKD_VERSION not found in metakernel, | |
strftime("%Y-%M-%dT%H:%M:%S") is used. | |
KPATH: pathlib.Path or str, default None | |
SPICE kernels location path. Default | |
points to metakernel_path.parent.parent. | |
outdir: pathlib.Path or str, default None | |
output directory to write the metakernel to. | |
Default points to metakernel_path.parent. | |
symlink: pathlib.Path or str, default None | |
If defined, symlink KPATH to it. Useful to create | |
a shorter KPATH i.e. /tmp/symlink . | |
loglevel: str, default None | |
if defined, set logging level. | |
Returns | |
------- | |
meta_mdf : pathlib.Path | |
updated metakernel pathlib.Path | |
SKD_VERSION: str | |
version of the kernel data set used. | |
""" | |
import logging | |
if loglevel is not None : | |
logging.getLogger().setLevel(loglevel.upper()) | |
SKD_VERSION=None | |
# check if inputs are defined (!= None) and are pathlib.Path class | |
metakernel_path=pathlib.Path(metakernel_path) | |
if KPATH is not None and not isinstance(KPATH,pathlib.Path): | |
KPATH = pathlib.Path(KPATH) | |
if outdir is not None and not isinstance(outdir,pathlib.Path): | |
outdir = pathlib.Path(outdir) | |
if symlink is not None and not isinstance(symlink,pathlib.Path): | |
symlink = pathlib.Path(symlink) | |
if KPATH is None : | |
KPATH = metakernel_path.parent.parent | |
if outdir is None : | |
outdir = metakernel_path.parent | |
# try to read metakernel | |
mylines = [] | |
try: | |
with open(metakernel_path, 'r') as f: | |
for lines in f: | |
mylines.append(lines) | |
if not metakernel_version: | |
if lines.find('SKD_VERSION') != -1: | |
SKD_VERSION = (str.split(lines, "'"))[1] | |
logging.info(f'Reading SPICE Metakernel {metakernel_path}') | |
except FileNotFoundError: | |
logging.error(f'SPICE Metakernel {metakernel_path} could not be opened.') | |
sys.exit(1) | |
if metakernel_version: | |
logging.info('SKD_VERSION : using user input') | |
SKD_VERSION = metakernel_version | |
if not SKD_VERSION: | |
logging.warning('NOT FOUND: SPICE Kernel Dataset Version variable SKD_VERSION - use current time') | |
from time import strftime,localtime | |
SKD_VERSION = strftime("%Y-%M-%dT%H:%M:%S", localtime()) | |
# create fake spice kernels directory | |
if symlink: | |
try: | |
symlink.symlink_to(KPATH, target_is_directory=True) | |
logging.info(f'Create symlink {symlink} to {KPATH}') | |
except FileExistsError as e: | |
logging.info(e) | |
# write temporary metakernel with modified paths | |
meta_mdf = outdir / f"{metakernel_path.stem}_updated-{SKD_VERSION}.tm" | |
try: | |
with open(meta_mdf, 'w') as f: | |
for lines in mylines: | |
if lines.find("PATH_VALUES") != -1: | |
splitted = lines.split("'") | |
if symlink: | |
lines = f"{splitted[0]}'{symlink}'{splitted[-1]}" | |
else: | |
lines = f"{splitted[0]}'{KPATH}'{splitted[-1]}" | |
# #TODO adapt Replacing path separator and convert line terminators to non *NIX systems | |
# if lines.find("$KERNELS") != 0: | |
# lines = lines.replace("/", os.sep) | |
f.write(lines) | |
logging.info(f'Metakernel with updated paths created and moved to {outdir}') | |
except FileNotFoundError: | |
logging.error(f'SPICE Metakernel {metakernel_path} could not be write.') | |
logging.debug(f'{metakernel_path=}') | |
logging.debug(f'{metakernel_version=}') | |
logging.debug(f'{KPATH=}') | |
logging.debug(f'{meta_mdf=}') | |
logging.debug(f'{SKD_VERSION=}') | |
logging.debug(f'{outdir=}') | |
logging.debug(f'{symlink=}') | |
return {'kernels_updated_version':SKD_VERSION,'metakernel_path':meta_mdf} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment