Beispiel #1
0
def _format_segment_brief(segment):
    cname = _names.get(segment.center)
    tname = _names.get(segment.target)
    return '{}{}{} -> {}{}{}'.format(
        segment.center,
        ' ' if cname else '',
        cname,
        segment.target,
        ' ' if tname else '',
        tname,
    )
Beispiel #2
0
def _format_segment_brief(segment):
    cname = _names.get(segment.center)
    tname = _names.get(segment.target)
    return '{0}{1}{2} -> {3}{4}{5}'.format(
        segment.center,
        ' ' if cname else '',
        cname,
        segment.target,
        ' ' if tname else '',
        tname,
    )
Beispiel #3
0
 def describe(self, verbose=True):
     """Return a textual description of the segment.
     """
     center = titlecase(target_names.get(self.center, 'Unknown center'))
     target = titlecase(target_names.get(self.target, 'Unknown target'))
     text = ('{0.start_jd:.2f}..{0.end_jd:.2f}  {1} ({0.center})'
             ' -> {2} ({0.target})'
             ' data_type={0.data_type}'.format(self, center, target))
     if verbose:
         text += ('\n  frame={0.frame} data_type={0.data_type} source={1}'
                  .format(self, self.source.decode('ascii')))
     return text
Beispiel #4
0
    def open(self):
        """Open the files
        """
        segments = []

        files = config.get("env", "jpl", fallback=[])

        if not files:
            raise JplConfigError("No JPL file defined")

        # Extraction of segments from each .bsp file
        for filepath in files:

            filepath = Path(filepath)

            if filepath.suffix.lower() != ".bsp":
                continue

            segments.extend(SPK.open(str(filepath)).segments)

        if not segments:
            raise JplError("No segment loaded")

        # list of available segments
        self.segments = dict(((s.center, s.target), s) for s in segments)

        # This variable will contain the Target of reference from which
        # all relations between frames are linked
        targets = {}

        for center_id, target_id in self.segments.keys():

            center_name = target_names.get(center_id, "Unknown")
            target_name = target_names.get(target_id, "Unknown")

            # Retrieval of the Target object representing the center if it exists
            # or creation of said object if it doesn't.
            center = targets.setdefault(center_id,
                                        Target(center_name, center_id))
            target = targets.setdefault(target_id,
                                        Target(target_name, target_id))

            # Link between the Target objects (see Node2)
            center + target

        # We take the Earth target and make it the top of the structure.
        # That way, it is easy to link it to the already declared earth-centered reference frames
        # from the `frames.frame` module.
        self.top = targets[399]
def _jpl_name(target):
    if not isinstance(target, int):
        return type(target).__name__
    name = _jpl_code_name_dict.get(target)
    if name is None:
        return str(target)
    return '{0} {1}'.format(target, name)
Beispiel #6
0
    def open(self):
        """Open the files
        """
        segments = []

        files = config.get('env', 'jpl', fallback=[])

        if not files:
            raise JplConfigError("No JPL file defined")

        # Extraction of segments from each .bsp file
        for filepath in files:

            filepath = Path(filepath)

            if filepath.suffix.lower() != ".bsp":
                continue

            segments.extend(SPK.open(str(filepath)).segments)

        if not segments:
            raise JplError("No segment loaded")

        # list of available segments
        self.segments = dict(((s.center, s.target), s) for s in segments)

        # This variable will contain the Target of reference from which
        # all relations between frames are linked
        targets = {}

        for center_id, target_id in self.segments.keys():

            center_name = target_names.get(center_id, 'Unknown')
            target_name = target_names.get(target_id, 'Unknown')

            # Retrieval of the Target object representing the center if it exists
            # or creation of said object if it doesn't.
            center = targets.setdefault(center_id, Target(center_name, center_id))
            target = targets.setdefault(target_id, Target(target_name, target_id))

            # Link between the Target objects (see Node2)
            center + target

        # We take the Earth target and make it the top of the structure.
        # That way, it is easy to link it to the already declared earth-centered reference frames
        # from the `frames.frame` module.
        self.top = targets[399]
Beispiel #7
0
def create_frames():
    """Create all frames available from the .bsp files"""

    # This variable will contain the Target of reference from which
    # all relations between frames are linked
    centers = {}

    for center_id, target_id in Bsp().pairs:

        center_name = target_names.get(center_id, "Unknown")
        target_name = target_names.get(target_id, "Unknown")

        if center_id not in centers:
            center_body = Pck()[center_name]
            center = centers[center_id] = JplCenter(
                center_name, center_id, body=center_body
            )
        else:
            center = centers[center_id]

        if target_id not in centers:
            target_body = Pck()[target_name]
            target = centers[target_id] = JplCenter(
                target_name, target_id, body=target_body
            )
        else:
            target = centers[target_id]

        if center.name not in _frame_cache:
            _frame_cache[center.name] = JplFrame(center)
        if target.name not in _frame_cache:
            _frame_cache[target.name] = JplFrame(target)

        if target.name not in _propagator_cache:
            _propagator_cache[target.name] = JplPropagator(
                target, _frame_cache[center.name]
            )

        # Link between the JplCenter objects (see beyond.frames.center)
        target.add_link(center, _propagator_cache[target.name])

    # We take the Earth JplCenter and attach it to the existing Earth center
    # (defined in beyond.frames.center)
    first_frame = _frame_cache["Earth"]
    BASE_FRAME.center.add_link(first_frame.center, BASE_FRAME.orientation, np.zeros(6))
Beispiel #8
0
 def __str__(self):
     segments = self.spk.segments
     lines = ['SPICE kernel file {0!r} has {1} segments'
              .format(self.filename, len(segments))]
     format_date = '{0}-{1:02}-{2:02}'.format
     start = end = None
     for s in segments:
         if start != s.start_jd or end != s.end_jd:
             start, end = s.start_jd, s.end_jd
             starts = format_date(*calendar_date(int(start)))
             ends = format_date(*calendar_date(int(end)))
             lines.append('    JD {0} - JD {1}  ({2} through {3})'
                          .format(start, end, starts, ends))
         cname = _names.get(s.center, 'unknown')
         tname = _names.get(s.target, 'unknown')
         lines.append('        {0:3} -> {1:<3}  {2} -> {3}'
                      .format(s.center, s.target, cname, tname))
     return '\n'.join(lines)
Beispiel #9
0
def numbered_name_of(code):
    """Given a code, return a string giving both the code and name.

    >>> numbered_name_of(301)
    '301 Moon'

    """
    name = code_names.get(code, '(Unnamed)')
    return '{0} {1}'.format(code, name)
Beispiel #10
0
def _target_name(target):
    """Return `target` annotated for display to the user.

    * A string target is quoted to make clear that it's a string, like 'comet'.
    * A numeric target has its NAIF name appended, like 399 EARTH.

    """
    if isinstance(target, str):
        return repr(target)
    name = code_names.get(target, 'UNKNOWN')
    return '{0} {1}'.format(target, name)
Beispiel #11
0
 def __str__(self):
     segments = self.spk.segments
     lines = [
         'SPICE kernel file {0!r} has {1} segments'.format(
             self.filename, len(segments))
     ]
     format_date = '{0}-{1:02}-{2:02}'.format
     start = end = None
     for s in segments:
         if start != s.start_jd or end != s.end_jd:
             start, end = s.start_jd, s.end_jd
             starts = format_date(*calendar_date(int(start)))
             ends = format_date(*calendar_date(int(end)))
             lines.append(
                 '  JD {0:.2f} - JD {1:.2f}  ({2} through {3})'.format(
                     start, end, starts, ends))
         cname = _names.get(s.center, 'unknown')
         tname = _names.get(s.target, 'unknown')
         lines.append('    {0:3} -> {1:<3}  {2} -> {3}'.format(
             s.center, s.target, cname, tname))
     return '\n'.join(lines)
Beispiel #12
0
 def __init__(self, file):
     if isinstance(file, str):
         file = open(file, 'rb')
     self.spk = SPK(file)
     segments = [Segment(s.center, s.target, _build_compute(s))
                 for s in self.spk.segments]
     codes = set(s.center for s in segments).union(
                 s.target for s in segments)
     for code in codes:
         body = Body(code, segments)
         self[code] = body
         raw_name = target_names.get(code, None)
         if raw_name is None:
             continue
         name = raw_name.lower().replace(' ', '_')
         setattr(self, name, body)
Beispiel #13
0
 def __init__(self, file):
     if isinstance(file, str):
         file = open(file, 'rb')
     self.spk = SPK(file)
     segments = [Segment(s.center, s.target, _build_compute(s))
                 for s in self.spk.segments]
     codes = set(s.center for s in segments).union(
                 s.target for s in segments)
     for code in codes:
         body = Body(code, segments)
         self[code] = body
         raw_name = target_names.get(code, None)
         if raw_name is None:
             continue
         name = raw_name.lower().replace(' ', '_')
         setattr(self, name, body)
Beispiel #14
0
def _code_and_name(code):
    name = _names.get(code, None)
    if name is None:
        return str(code)
    return '{0} {1!r}'.format(code, name)
Beispiel #15
0
def _format_segment(segment):
    cname = _names.get(segment.center, 'unknown')
    tname = _names.get(segment.target, 'unknown')
    return '    {0:3} -> {1:<3}  {2} -> {3}'.format(segment.center,
                                                    segment.target, cname,
                                                    tname)
Beispiel #16
0
def _format_code_and_name(code):
    name = _names.get(code, None)
    if name is None:
        return str(code)
    return '{0} {1}'.format(code, name)
Beispiel #17
0
def list_frames():
    """List frames available through .bsp files"""
    return list(_frame_cache.values())


def list_bodies():
    """List bodies available through .tpc files"""
    return [Pck()[k] for k in Pck().keys()]


if __name__ == "__main__":  # pragma: no cover

    import sys
    from beyond.dates import Date

    config.update({"eop": {"missing_policy": "pass"}})

    for file in sys.argv[1:]:
        print(file)
        print("*" * len(file))
        for segment in SPK.open(file).segments:

            start = Date(segment.start_jd - Date.JD_MJD)
            end = Date(segment.end_jd - Date.JD_MJD)

            center = target_names.get(segment.center, "Unknown")
            target = target_names.get(segment.target, "Unknown")
            fmt = "%Y-%m-%d"
            print(f"from {start:{fmt}} to {end:{fmt}} : {center} -> {target}")
        print()
Beispiel #18
0
    def parse(self):

        self.clear()

        # Parsing of multiple files provided in the configuration variable
        for filepath in config['env']['jpl']:

            filepath = Path(filepath)

            if filepath.suffix.lower() != ".tpc":
                continue

            with filepath.open() as fp:
                lines = fp.read().splitlines()

            datablock = False

            # Checking for header
            if lines[0].strip() != "KPL/PCK":
                raise JplError("Unknown file format")

            try:
                for i, line in enumerate(lines):

                    # Seek the beginning of a data block
                    if line.strip() == "\\begindata":
                        datablock = True
                        continue

                    # End of a datablock
                    if line.strip() == "\\begintext":
                        datablock = False
                        continue

                    # Variable extraction
                    if datablock and line.strip().lower().startswith('body'):

                        # retrieval of body ID, parameter name and value
                        line = line.strip().lower().lstrip('body')
                        body_id, _, param = line.partition('_')
                        key, _, value = param.partition("=")

                        # If possible, retrieval of the name of the body
                        # if not, use the ID as name
                        name = target_names.get(int(body_id), body_id).title().strip()

                        # If already existing, check out the dictionary describing the body
                        # characteristics
                        body_dict = self.setdefault(name, {})

                        # Extraction of interesting data
                        value = value.strip()

                        # List of value scattered on multiple lines
                        if not value.endswith(")"):
                            for next_line in lines[i + 1:]:
                                value += " " + next_line.strip()
                                if next_line.strip().endswith(")"):
                                    break

                        value = [self.parse_float(v) for v in value[1:-2].split()]

                        body_dict[key.upper().strip()] = value
            except Exception as e:
                raise JplError("Parsing error on file '{}'".format(filepath)) from e
Beispiel #19
0
    def parse(self):

        self.clear()

        files = config.get("env", "jpl", "files", fallback=[])

        if not files:
            raise JplConfigError("No JPL file defined")

        # Parsing of multiple files provided in the configuration variable
        for filepath in files:

            filepath = Path(filepath)

            if filepath.suffix.lower() != ".tpc":
                continue

            if not filepath.exists():
                log.warning(f"File not found : {filepath}")
                continue

            with filepath.open(encoding="ascii") as fp:
                lines = fp.read().splitlines()

            datablock = False

            # Checking for header
            if lines[0].strip() != "KPL/PCK":
                raise JplError("Unknown file format")

            try:
                for i, line in enumerate(lines):

                    # Seek the beginning of a data block
                    if line.strip() == "\\begindata":
                        datablock = True
                        continue

                    # End of a datablock
                    if line.strip() == "\\begintext":
                        datablock = False
                        continue

                    # Variable extraction
                    if datablock and line.strip().lower().startswith("body"):

                        # retrieval of body ID, parameter name and value
                        line = line.strip().lower().lstrip("body")
                        body_id, _, param = line.partition("_")
                        key, _, value = param.partition("=")

                        # If possible, retrieval of the name of the body
                        # if not, use the ID as name
                        name = target_names.get(int(body_id), body_id).title().strip()

                        # If already existing, check out the dictionary describing the body
                        # characteristics
                        body_dict = self.setdefault(name, {})

                        # Extraction of interesting data
                        value = value.strip()

                        # List of value scattered on multiple lines
                        if not value.endswith(")"):
                            for next_line in lines[i + 1 :]:
                                value += " " + next_line.strip()
                                if next_line.strip().endswith(")"):
                                    break

                        value = [self.parse_float(v) for v in value[1:-2].split()]

                        body_dict[key.upper().strip()] = value
            except Exception as e:
                raise JplError(f"Parsing error on file '{filepath}'") from e
Beispiel #20
0
def _segment_line(segment):
    cname = _names.get(segment.center, 'unknown')
    tname = _names.get(segment.target, 'unknown')
    return '    {0:3} -> {1:<3}  {2} -> {3}'.format(
        segment.center, segment.target, cname, tname)
Beispiel #21
0
def _jpl_name(code_or_string):
    if isinstance(code_or_string, int):
        name = _jpl_code_name_dict.get(code_or_string)
        if name is not None:
            return '{0} {1}'.format(code_or_string, name)
    return str(code_or_string)