Exemplo n.º 1
0
def process_file(root, name, index, files, selection="S"):

    # Get the volume ID
    temp = root.upper()
    volume_id = '"' + temp[temp.index("COVIMS_00"):][:11] + '"'

    # Create the file specification name
    temp = os.path.join(root, name)
    temp = temp[temp.index("/data/"):][1:]
    filespec = '"%-57s"' % temp

    # Create the prefix for the ring_observation_id
    roid_prefix = ring_observation_id(name)
    logstr = "%4d/%4d  %s" % (index, files, roid_prefix)

    # Don't abort if cspice throws a runtime error
    try:

        # Create observations (vis,ir)
        observations = vims.from_file(os.path.join(root, name))

        # For each detector...
        for obs in observations:

            # Ignore empty VIMS channels
            if obs is None: continue

            # Ignore certain mission phases
            if obs.dict["MISSION_PHASE_NAME"] in IGNORED_PHASE_NAMES:
                return

            # Write lines to the output files if necessary
            roid = "%-33s" % (roid_prefix + obs.detector)

            # Get the target
            target = target_name(obs.dict)
            if target in TRANSLATIONS.keys():
                target = TRANSLATIONS[target]

            # Create the backplane
            # (meshgrid, times) = meshgrid_and_times(obs)
            # backplane = oops.Backplane(obs, meshgrid, times)
            backplane = oops.Backplane(obs)

            # Print a log of progress. This records where errors occurred
            logstr = "%s  %4d/%4d  %s %s" % (volume_id[1:-1], index, files,
                                             roid, target)
            print logstr

            # Inventory the bodies in the FOV (including targeted irregulars)
            if (target not in SYSTEM_NAMES and oops.Body.exists(target) and \
                target != 'SUN'):
                body_names = SYSTEM_NAMES + [target]
            else:
                body_names = SYSTEM_NAMES

            inventory_names = obs.inventory(body_names, expand=0.)

            # Write a record into the inventory file
            prefixes = [volume_id, filespec, '"' + roid + '"']
            inventory_file.write(",".join(prefixes))
            for name in inventory_names:
                inventory_file.write(',"' + name + '"')

            inventory_file.write("\r\n")  # Use <CR><LF> line termination

            # Convert the inventory into a list of moon names
            if len(inventory_names) > 0 and inventory_names[0] == PLANET:
                moon_names = inventory_names[1:]
            else:
                moon_names = inventory_names

            # Define a blocker moon, if any
            if target in moon_names:
                blocker = target
            else:
                blocker = None

            # Add an irregular moon to the dictionaries if necessary
            if target in moon_names and target not in MOON_SUMMARY_DICT:
                MOON_SUMMARY_DICT[target] = meta.replace(
                    MOON_SUMMARY_COLUMNS, MOONX, target)
                MOON_DETAILED_DICT[target] = meta.replace(
                    MOON_DETAILED_COLUMNS, MOONX, target)
                MOON_TILE_DICT[target] = meta.replace(MOON_TILES, MOONX,
                                                      target)

            # Write the summary files for the rings and for the planet
            if "S" in selection:
                meta.write_record(prefixes, backplane, blocker, ring_summary,
                                  RING_SUMMARY_COLUMNS, PLANET)

                meta.write_record(prefixes,
                                  backplane,
                                  blocker,
                                  planet_summary,
                                  PLANET_SUMMARY_COLUMNS,
                                  PLANET,
                                  moon=PLANET,
                                  moon_length=NAME_LENGTH)

            # Write the detailed files for the rings and for the planet
            if "D" in selection:
                meta.write_record(prefixes,
                                  backplane,
                                  blocker,
                                  ring_detailed,
                                  RING_DETAILED_COLUMNS,
                                  PLANET,
                                  tiles=RING_TILES,
                                  tiling_min=10)

                meta.write_record(prefixes,
                                  backplane,
                                  blocker,
                                  planet_detailed,
                                  PLANET_DETAILED_COLUMNS,
                                  PLANET,
                                  moon=PLANET,
                                  moon_length=NAME_LENGTH,
                                  tiles=PLANET_TILES,
                                  tiling_min=10)

            # Write the moon files
            for name in moon_names:

                # ...but skip tiny moons with no intercepts
                if name != target:
                    mask = backplane.where_intercepted(name)
                    if not mask.any(): continue

                if "S" in selection:
                    meta.write_record(prefixes,
                                      backplane,
                                      blocker,
                                      moon_summary,
                                      MOON_SUMMARY_DICT[name],
                                      PLANET,
                                      moon=name,
                                      moon_length=NAME_LENGTH)

                if "D" in selection:
                    meta.write_record(prefixes,
                                      backplane,
                                      blocker,
                                      moon_detailed,
                                      MOON_DETAILED_DICT[name],
                                      PLANET,
                                      moon=name,
                                      moon_length=NAME_LENGTH,
                                      tiles=MOON_TILE_DICT[name],
                                      tiling_min=10)

    # A RuntimeError is probably caused by missing spice data. There is
    # probably nothing we can do.
    except RuntimeError as e:

        if logstr.endswith("/"): print logstr
        print e
        log_file.write(40 * "*" + "\n" + logstr + "\n")
        log_file.write(str(e))
        log_file.write("\n\n")

    # Other kinds of errors are genuine bugs. For now, we just log the
    # problem, and jump over the image; we can deal with it later.
    except (AssertionError, AttributeError, IndexError, KeyError, LookupError,
            TypeError, ValueError, ZeroDivisionError,
            pyparsing.ParseException):

        if logstr.endswith("/"): print logstr
        traceback.print_exc()
        log_file.write(40 * "*" + "\n" + logstr + "\n")
        log_file.write(traceback.format_exc())
        log_file.write("\n\n")
Exemplo n.º 2
0
def process_file(root, name, index, files, selection="S"):

    # Don't abort if cspice throws a runtime error
    try:

        (volume_id, file_spec) = volume_and_filespec(root, name)

        roid = ring_observation_id(name)
        logstr = "%s  %4d/%4d  %s" % (volume_id, index, files, roid)
        print logstr,
        terminated_print = False

        i20 = name.index("20")
        year = int(name[i20:i20 + 4])
        if year < 2004:
            print "skipped"
            return

        prefixes = [
            '"' + volume_id + '"', '"' + file_spec + '"', '"' + roid + '"'
        ]

        # Create the observation object
        obs = uvis.from_file(os.path.join(root, name), enclose=True)

        # Get the target
        target = obs.dict["TARGET_NAME"].upper()
        if target in TRANSLATIONS.keys():
            target = TRANSLATIONS[target]

        print " " + target
        terminated_print = True

        # Create the backplane
        cad = obs.cadence

        if cad.steps <= MAX_TIMESTEPS:
            steps = cad.steps
            tstride = cad.tstride
        else:
            steps = MAX_TIMESTEPS
            tstride = (cad.time[1] - cad.time[0]) / steps

        time_vals = cad.tstart + tstride * np.arange(0.5, steps)

        if len(obs.shape) > 1:
            time = oops.Scalar(time_vals[:, np.newaxis])
        else:
            time = oops.Scalar(time_vals)

        backplane = oops.Backplane(obs, time=time)

        # Catch SPICE NOFRAMECONNECT exceptions before going any further
        ignore = backplane.right_ascension()

        # Define a list of moon names and the blocker moon
        if target in SYSTEM_NAMES and target != PLANET:
            blocker = target
            moon_names = [target]
        else:
            blocker = None
            moon_names = []

        # Write the summary files for the rings and for the planet
        if "S" in selection:
            meta.write_record(prefixes,
                              backplane,
                              blocker,
                              ring_summary,
                              RING_SUMMARY_COLUMNS,
                              PLANET,
                              ignore_shadows=True)

            meta.write_record(prefixes,
                              backplane,
                              blocker,
                              planet_summary,
                              PLANET_SUMMARY_COLUMNS,
                              PLANET,
                              moon=PLANET,
                              moon_length=NAME_LENGTH,
                              ignore_shadows=True)

        # Write the detailed files for the rings and for the planet
        if "D" in selection:
            meta.write_record(prefixes,
                              backplane,
                              blocker,
                              ring_detailed,
                              RING_DETAILED_COLUMNS,
                              PLANET,
                              tiles=RING_TILES,
                              ignore_shadows=True)

            meta.write_record(prefixes,
                              backplane,
                              blocker,
                              planet_detailed,
                              PLANET_DETAILED_COLUMNS,
                              PLANET,
                              moon=PLANET,
                              moon_length=NAME_LENGTH,
                              tiles=PLANET_TILES,
                              ignore_shadows=True)

        # Write the moon files
        for name in moon_names:
            if "S" in selection:
                meta.write_record(prefixes,
                                  backplane,
                                  blocker,
                                  moon_summary,
                                  MOON_SUMMARY_DICT[name],
                                  PLANET,
                                  moon=name,
                                  moon_length=NAME_LENGTH,
                                  ignore_shadows=True)

            if "D" in selection:
                meta.write_record(prefixes,
                                  backplane,
                                  blocker,
                                  moon_detailed,
                                  MOON_DETAILED_DICT[name],
                                  PLANET,
                                  moon=name,
                                  moon_length=NAME_LENGTH,
                                  tiles=MOON_TILE_DICT[name],
                                  ignore_shadows=True)

    # A RuntimeError is probably caused by missing spice data. There is
    # probably nothing we can do.
    except RuntimeError as e:

        if "NOFRAMECONNECT" in str(e):
            print "**** SPICE(NOFRAMECONNECT)"
            noframe_file.write(PREFIX + "\n")
        else:
            if not terminated_print: print
            print e

            error_file.write(40 * "*" + "\n" + logstr + "\n")
            error_file.write(str(e))
            error_file.write("\n\n")

    # Other kinds of errors are genuine bugs. For now, we just log the
    # problem, and jump over the image; we can deal with it later.
    except (AssertionError, AttributeError, IndexError, KeyError, LookupError,
            TypeError, ValueError, ZeroDivisionError,
            pyparsing.ParseException):

        if not terminated_print: print

        traceback.print_exc()
        error_file.write(40 * "*" + "\n" + logstr + "\n")
        error_file.write(traceback.format_exc())
        error_file.write("\n\n")
Exemplo n.º 3
0
def process_index(input_filename, selection="S"):
    """Process one index file and write a selection of metadata files.

    Input:
        input_filename  the name of the label for an ISS index file.
        selection       a string containing...
                            "S" to generate summary files;
                            "D" to generate detailed files.
    """

    snapshots = iss.from_index(input_filename)
    volume_id = snapshots[0].dict["VOLUME_ID"]

    records = len(snapshots)

    # Open the output files
    (path, filename) = os.path.split(input_filename)
    path = path.replace("/index", "")
    path = path.replace("/INDEX", "")

    prefix = path + "/" + volume_id
    log_file = open(prefix + "_log.txt", "w")
    inventory_file = open(prefix + "_inventory.tab", "w")

    if "S" in selection:
        ring_summary = open(prefix + "_ring_summary.tab", "w")
        planet_summary = open(prefix + "_%s_summary.tab" % PLANET.lower(), "w")
        moon_summary = open(prefix + "_moon_summary.tab", "w")

    if "D" in selection:
        ring_detailed = open(prefix + "_ring_detailed.tab", "w")
        planet_detailed = open(prefix + "_%s_detailed.tab" % PLANET.lower(),
                               "w")
        moon_detailed = open(prefix + "_moon_detailed.tab", "w")

    # Loop through the snapshots...

    for i in range(records):
        snapshot = snapshots[i]

        target = target_name(snapshot.dict)
        if target in TRANSLATIONS.keys():
            target = TRANSLATIONS[target]

        # Don't abort if cspice throws a runtime error
        try:

            # Create the record prefix
            volume_id = snapshot.dict["VOLUME_ID"]
            filespec = snapshot.dict["FILE_SPECIFICATION_NAME"]
            roid = ring_observation_id(snapshot.dict)
            prefixes = [
                '"' + volume_id + '"',
                '"%-45s"' % filespec.replace(".IMG", ".LBL"), '"' + roid + '"'
            ]

            # Create the backplane
            meshgrid = MESHGRIDS[(snapshot.detector, snapshot.sampling)]
            backplane = oops.Backplane(snapshot, meshgrid)

            # Print a log of progress. This records where errors occurred
            logstr = "%s  %4d/%4d  %s  %s" % (volume_id, i + 1, records, roid,
                                              target)
            print logstr

            # Inventory the bodies in the FOV (including targeted irregulars)
            if (target not in SYSTEM_NAMES and oops.Body.exists(target)
                    and target != 'SUN'):
                body_names = SYSTEM_NAMES + [target]
            else:
                body_names = SYSTEM_NAMES

            inventory_names = snapshot.inventory(body_names, expand=EXPAND)

            # Write a record into the inventory file
            inventory_file.write(",".join(prefixes))
            for name in inventory_names:
                inventory_file.write(',"' + name + '"')

            inventory_file.write("\r\n")  # Use <CR><LF> line termination

            # Convert the inventory into a list of moon names
            if len(inventory_names) > 0 and inventory_names[0] == PLANET:
                moon_names = inventory_names[1:]
            else:
                moon_names = inventory_names

            # Define a blocker moon, if any
            if target in moon_names:
                blocker = target
            else:
                blocker = None

            # Add an irregular moon to the dictionaries if necessary
            if target in moon_names and target not in MOON_SUMMARY_DICT.keys():
                MOON_SUMMARY_DICT[target] = meta.replace(
                    MOON_SUMMARY_COLUMNS, MOONX, target)
                MOON_DETAILED_DICT[target] = meta.replace(
                    MOON_DETAILED_COLUMNS, MOONX, target)
                MOON_TILE_DICT[target] = meta.replace(MOON_TILES, MOONX,
                                                      target)

            # Write the summary files
            if "S" in selection:
                meta.write_record(prefixes,
                                  backplane,
                                  blocker,
                                  ring_summary,
                                  RING_SUMMARY_COLUMNS,
                                  PLANET,
                                  count_length=COUNT_LENGTH)

                meta.write_record(prefixes,
                                  backplane,
                                  blocker,
                                  planet_summary,
                                  PLANET_SUMMARY_COLUMNS,
                                  PLANET,
                                  moon=PLANET,
                                  moon_length=NAME_LENGTH,
                                  count_length=COUNT_LENGTH)

                for name in moon_names:
                    meta.write_record(prefixes,
                                      backplane,
                                      blocker,
                                      moon_summary,
                                      MOON_SUMMARY_DICT[name],
                                      PLANET,
                                      moon=name,
                                      moon_length=NAME_LENGTH,
                                      count_length=COUNT_LENGTH)

            # Write the detailed files
            if "D" in selection:
                meta.write_record(prefixes,
                                  backplane,
                                  blocker,
                                  ring_detailed,
                                  RING_DETAILED_COLUMNS,
                                  PLANET,
                                  count_length=COUNT_LENGTH,
                                  tiles=RING_TILES)

                meta.write_record(prefixes,
                                  backplane,
                                  blocker,
                                  planet_detailed,
                                  PLANET_DETAILED_COLUMNS,
                                  PLANET,
                                  moon=PLANET,
                                  moon_length=NAME_LENGTH,
                                  count_length=COUNT_LENGTH,
                                  tiles=PLANET_TILES)

                for name in moon_names:
                    meta.write_record(prefixes,
                                      backplane,
                                      blocker,
                                      moon_detailed,
                                      MOON_DETAILED_DICT[name],
                                      PLANET,
                                      moon=name,
                                      moon_length=NAME_LENGTH,
                                      count_length=COUNT_LENGTH,
                                      tiles=MOON_TILE_DICT[name])

        # A RuntimeError is probably caused by missing spice data. There is
        # probably nothing we can do.
        except RuntimeError as e:

            print e
            log_file.write(40 * "*" + "\n" + logstr + "\n")
            log_file.write(str(e))
            log_file.write("\n\n")

        # Other kinds of errors are genuine bugs. For now, we just log the
        # problem, and jump over the image; we can deal with it later.
        except (AssertionError, AttributeError, IndexError, KeyError,
                LookupError, TypeError, ValueError):

            traceback.print_exc()
            log_file.write(40 * "*" + "\n" + logstr + "\n")
            log_file.write(traceback.format_exc())
            log_file.write("\n\n")

    # Close all files
    log_file.close()
    inventory_file.close()

    if "S" in selection:
        ring_summary.close()
        planet_summary.close()
        moon_summary.close()

    if "D" in selection:
        ring_detailed.close()
        planet_detailed.close()
        moon_detailed.close()
Exemplo n.º 4
0
def process_index(input_filename, selection='S'):
    """Process one index file and write a selection of metadata files.

    Input:
        input_filename  the name of the label for a LORRI supplemental index
                        file.
        selection       a string containing...
                            'S' to generate summary files;
                            'D' to generate detailed files;
    """

    snapshots = lorri.from_index(input_filename)
    volume_id = snapshots[0].dict['VOLUME_ID']

    records = len(snapshots)

    # Open the output files
    (path, filename) = os.path.split(input_filename)

    prefix = path + '/' + volume_id
    log_file = open(prefix + '_log.txt', 'w')
    inventory_file = open(prefix + '_inventory.tab', 'w')

    if 'S' in selection:
        ring_summary = open(prefix + '_ring_summary.tab', 'w')
        planet_summary = open(prefix + '_pluto_summary.tab', 'w')
        charon_summary = open(prefix + '_charon_summary.tab', 'w')
        moon_summary = open(prefix + '_moon_summary.tab', 'w')

    if 'D' in selection:
        ring_detailed = open(prefix + '_ring_detailed.tab', 'w')
        planet_detailed = open(prefix + '_pluto_detailed.tab', 'w')
        charon_detailed = open(prefix + '_charon_detailed.tab', 'w')
        moon_detailed = open(prefix + '_moon_detailed.tab', 'w')

    # Loop through the snapshots...

    for i in range(records):
        snapshot = snapshots[i]

        target = target_name(snapshot.dict)

        # Don't abort if cspice throws a runtime error
        try:

            # Create the record prefix
            volume_id = snapshot.dict['VOLUME_ID']
            filespec = snapshot.dict['FILE_SPECIFICATION_NAME'].lower()
            roid = ring_observation_id(snapshot.dict)
            prefixes = [
                '"' + volume_id + '"',
                '"%-52s"' % filespec, '"' + roid + '"'
            ]

            # Create the backplane
            meshgrid = MESHGRIDS[snapshot.dict['BINNING_MODE']]
            backplane = oops.Backplane(snapshot, meshgrid)

            # Print a log of progress. This records where errors occurred
            logstr = '%s  %4d/%4d  %s  %s' % (volume_id, i + 1, records, roid,
                                              target)
            print logstr

            # Inventory the bodies in the FOV (including targeted irregulars)
            if target not in SYSTEM_NAMES and oops.Body.exists(target):
                body_names = SYSTEM_NAMES + [target]
            else:
                body_names = SYSTEM_NAMES

            inventory_names = snapshot.inventory(body_names, expand=EXPAND)

            # Write a record into the inventory file
            inventory_file.write(','.join(prefixes))
            for name in inventory_names:
                inventory_file.write(',"' + name + '"')

            inventory_file.write('\r\n')  # Use <CR><LF> line termination

            # Convert the inventory into a list of moon names
            if len(inventory_names) > 0 and inventory_names[0] == PLANET:
                moon_names = inventory_names[1:]
            else:
                moon_names = inventory_names

            if len(inventory_names) > 0 and inventory_names[0] == BIGMOON:
                moon_names = inventory_names[1:]
            else:
                moon_names = inventory_names

            # Define a blocker moon, if any
            if target in moon_names:
                blocker = target
            else:
                blocker = None

            # Add an irregular moon to the dictionaries if necessary
            if target in moon_names and target not in MOON_SUMMARY_DICT.keys():
                MOON_SUMMARY_DICT[target] = meta.replace(
                    MOON_SUMMARY_COLUMNS, MOONX, target)
                MOON_DETAILED_DICT[target] = meta.replace(
                    MOON_DETAILED_COLUMNS, MOONX, target)
                MOON_TILE_DICT[target] = meta.replace(MOON_TILES, MOONX,
                                                      target)

            # Write the summary files
            if 'S' in selection:
                meta.write_record(prefixes, backplane, blocker, ring_summary,
                                  RING_SUMMARY_COLUMNS, PLANET)

                meta.write_record(prefixes,
                                  backplane,
                                  blocker,
                                  planet_summary,
                                  PLANET_SUMMARY_COLUMNS,
                                  PLANET,
                                  moon=PLANET,
                                  moon_length=NAME_LENGTH)

                meta.write_record(prefixes,
                                  backplane,
                                  blocker,
                                  charon_summary,
                                  CHARON_SUMMARY_COLUMNS,
                                  PLANET,
                                  moon=BIGMOON,
                                  moon_length=NAME_LENGTH)

                for name in moon_names:
                    if name == 'CHARON' or name == 'PLUTO': continue
                    meta.write_record(prefixes,
                                      backplane,
                                      blocker,
                                      moon_summary,
                                      MOON_SUMMARY_DICT[name],
                                      PLANET,
                                      moon=name,
                                      moon_length=NAME_LENGTH)

            # Write the detailed files
            if 'D' in selection:
                meta.write_record(prefixes,
                                  backplane,
                                  blocker,
                                  ring_detailed,
                                  RING_DETAILED_COLUMNS,
                                  PLANET,
                                  tiles=(RING_TILES, OUTER_RING_TILES))

                meta.write_record(prefixes,
                                  backplane,
                                  blocker,
                                  planet_detailed,
                                  PLANET_DETAILED_COLUMNS,
                                  PLANET,
                                  moon=PLANET,
                                  moon_length=NAME_LENGTH,
                                  tiles=PLANET_TILES)

                meta.write_record(prefixes,
                                  backplane,
                                  blocker,
                                  charon_detailed,
                                  CHARON_DETAILED_COLUMNS,
                                  PLANET,
                                  moon=BIGMOON,
                                  moon_length=NAME_LENGTH,
                                  tiles=CHARON_TILES)

                for name in moon_names:
                    if name == 'CHARON': continue
                    meta.write_record(prefixes,
                                      backplane,
                                      blocker,
                                      moon_detailed,
                                      MOON_DETAILED_DICT[name],
                                      PLANET,
                                      moon=name,
                                      moon_length=NAME_LENGTH,
                                      tiles=MOON_TILE_DICT[name])

        # A RuntimeError is probably caused by missing spice data. There is
        # probably nothing we can do.
        except RuntimeError as e:

            print e
            log_file.write(40 * '*' + '\n' + logstr + '\n')
            log_file.write(str(e))
            log_file.write('\n\n')

        # Other kinds of errors are genuine bugs. For now, we just log the
        # problem, and jump over the image; we can deal with it later.
        except (AssertionError, AttributeError, IndexError, KeyError,
                LookupError, TypeError, ValueError):

            traceback.print_exc()
            log_file.write(40 * '*' + '\n' + logstr + '\n')
            log_file.write(traceback.format_exc())
            log_file.write('\n\n')

    # Close all files
    log_file.close()
    inventory_file.close()

    if 'S' in selection:
        ring_summary.close()
        planet_summary.close()
        charon_summary.close()
        moon_summary.close()

    if 'D' in selection:
        ring_detailed.close()
        planet_detailed.close()
        charon_detailed.close()
        moon_detailed.close()