예제 #1
0
def create_target_identification_nodes(
    bundle_db: BundleDB,
    target_identifications: List[TargetIdentification],
    reference_type: str,
) -> List[NodeBuilder]:
    """
    Take in a list of TargetIdentification records (from db), build a target
    dictionary for each record and return a list of Target_Identification nodes.
    It will be inserted in XML when passing into make_label.
    """
    reference_type_dict = {
        "data": "data_to_target",
        "collection": "collection_to_target",
        "bundle": "bundle_to_target",
    }
    target_identification_nodes: List[NodeBuilder] = []
    for target in target_identifications:
        bundle_db.create_context_product(
            get_target_lidvid([target.type, target.name]), "target")
        target_dict: Dict[str, Any] = {}
        target_dict["name"] = target.name
        target_dict["type"] = target.type
        target_dict["alternate_designations"] = target.alternate_designations
        target_dict["description"] = target.description
        target_dict["lid"] = target.lid_reference
        target_dict["reference_type"] = reference_type_dict[reference_type]
        target_identification_nodes.append(get_target(target_dict))
    return target_identification_nodes
예제 #2
0
def make_fits_product_label(
    working_dir: str,
    bundle_db: BundleDB,
    collection_lidvid: str,
    product_lidvid: str,
    bundle_lidvid: str,
    file_basename: str,
    verify: bool,
    use_mod_date_for_testing: bool = False,
) -> bytes:
    try:
        product = bundle_db.get_product(product_lidvid)
        collection = bundle_db.get_collection(collection_lidvid)
        if not isinstance(collection, OtherCollection):
            raise TypeError(f"{collection} is not OtherCollection.")
        instrument = collection.instrument
        suffix = collection.suffix

        # If a label is created for testing purpose to compare with pre-made XML
        # we will use MOD_DATE_FOR_TESTESING as the modification date.
        if not use_mod_date_for_testing:
            # Get the date when the label is created
            mod_date = get_current_date()
        else:
            mod_date = MOD_DATE_FOR_TESTESING

        card_dicts = bundle_db.get_card_dictionaries(product_lidvid,
                                                     file_basename)
        lookup = DictLookup(file_basename, card_dicts)
        siblings = _directory_siblings(working_dir, bundle_db, product_lidvid)
        hdu_lookups = _find_RAWish_lookups(bundle_db, product_lidvid,
                                           file_basename, siblings)
        shm_lookup = _find_SHMish_lookup(bundle_db, product_lidvid,
                                         file_basename, siblings)

        start_date_time, stop_date_time = get_start_stop_date_times(
            hdu_lookups, shm_lookup)
        exposure_duration = get_exposure_duration(hdu_lookups, shm_lookup)
        start_stop_times = {
            "start_date_time": start_date_time,
            "stop_date_time": stop_date_time,
            "exposure_duration": exposure_duration,
        }

        # Store start/stop time for each fits_product in fits_products table.
        # The min/max will be pulled out for roll-up in data collection/bundle.
        bundle_db.update_fits_product_time(product_lidvid, start_date_time,
                                           stop_date_time)

        hst_parameters = get_hst_parameters(hdu_lookups, shm_lookup)
        bundle = bundle_db.get_bundle(bundle_lidvid)
        proposal_id = bundle.proposal_id

        investigation_area_name = mk_Investigation_Area_name(proposal_id)
        investigation_area_lidvid = mk_Investigation_Area_lidvid(proposal_id)
        bundle_db.create_context_product(investigation_area_lidvid,
                                         "investigation")
        bundle_db.create_context_product(instrument_host_lidvid(),
                                         "instrument_host")
        bundle_db.create_context_product(observing_system_lidvid(instrument),
                                         "instrument")

        # Fetch target identifications from db
        target_id = shm_lookup["TARG_ID"]
        target_identifications = bundle_db.get_target_identifications_based_on_id(
            target_id)

        # At this stage, target identifications should be in the db
        if len(target_identifications) == 0:
            raise ValueError("Target identification is not stored in db.")

        target_identification_nodes: List[NodeBuilder] = []
        target_identification_nodes = create_target_identification_nodes(
            bundle_db, target_identifications, "data")

        # Get wavelength
        instrument_id = get_instrument_id(hdu_lookups, shm_lookup)
        detector_ids = get_detector_ids(hdu_lookups, shm_lookup)
        filter_name = get_filter_name(hdu_lookups, shm_lookup)
        wavelength_range = wavelength_ranges(instrument_id, detector_ids,
                                             filter_name)
        bundle_db.update_wavelength_range(product_lidvid, wavelength_range)

        # Get title
        channel_id = get_channel_id(hdu_lookups, shm_lookup)
        try:
            titles = get_titles_format(instrument_id, channel_id, suffix)
            product_title = titles[0] + "."
            product_title = product_title.format(I=instrument_id + "/" +
                                                 channel_id,
                                                 F=file_basename,
                                                 P=proposal_id)
            collection_title = titles[1] + "."
            collection_title = collection_title.format(I=instrument_id + "/" +
                                                       channel_id,
                                                       F=file_basename,
                                                       P=proposal_id)
            # save data/misc collection title to OtherCollection table
            bundle_db.update_fits_product_collection_title(
                collection_lidvid, collection_title)
        except KeyError:
            # If product_title doesn't exist in SUFFIX_TITLES, we use the
            # following text as the product_title.
            product_title = (
                f"{instrument_id} data file {file_basename} " +
                f"obtained by the HST Observing Program {proposal_id}.")

        # Dictionary used for primary result summary
        processing_level = get_processing_level(suffix, instrument_id,
                                                channel_id)
        primary_result_dict: Dict[str, Any] = {}
        primary_result_dict["processing_level"] = processing_level
        primary_result_dict["description"] = product_title
        primary_result_dict["wavelength_range"] = wavelength_range

        # Dictionary passed into templates. Use the same data dictionary for
        # either data label template or misc label template
        data_dict = {
            "lid":
            lidvid_to_lid(product_lidvid),
            "vid":
            lidvid_to_vid(product_lidvid),
            "title":
            product_title,
            "mod_date":
            mod_date,
            "file_name":
            file_basename,
            "file_contents":
            get_file_contents(bundle_db, card_dicts, instrument,
                              product_lidvid),
            "Investigation_Area":
            investigation_area(investigation_area_name,
                               investigation_area_lidvid, "data"),
            "Observing_System":
            observing_system(instrument),
            "Time_Coordinates":
            get_time_coordinates(start_stop_times),
            "Target_Identification":
            combine_nodes_into_fragment(target_identification_nodes),
            "HST":
            hst_parameters,
            "Primary_Result_Summary":
            primary_result_summary(primary_result_dict),
            "Reference_List":
            make_document_reference_list([instrument], "data"),
        }

        # Pass the data_dict to either data label or misc label based on
        # collection_type
        collection_type = get_collection_type(suffix, instrument_id,
                                              channel_id)
        if collection_type == "data":
            label = make_data_label(data_dict).toxml().encode()
        elif collection_type == "miscellaneous":
            label = make_misc_label(data_dict).toxml().encode()

    except AssertionError:
        raise AssertionError(
            f"{product_lidvid} has no target identifications stored in DB.")
    except Exception as e:
        print(str(e))
        raise LabelError(product_lidvid, file_basename,
                         (lookup, hdu_lookups[0], shm_lookup)) from e

    return pretty_and_verify(label, verify)
예제 #3
0
def make_context_target_label(
    bundle_db: BundleDB,
    target: str,
    verify: bool,
    use_mod_date_for_testing: bool = False,
) -> bytes:
    """
    Create the label text for the context target having this LIDVID using
    the bundle database.  If verify is True, verify the label against
    its XML and Schematron schemas.  Raise an exception if either
    fails.
    """
    target_lid = f"urn:nasa:pds:context:target:{target}"
    target_lidvid = f"{target_lid}::1.0"
    target_identification = bundle_db.get_target_identification_based_on_lid(
        target_lid)
    bundle_db.create_context_product(
        get_target_lidvid(
            [target_identification.type, target_identification.name]),
        "target",
    )

    alias = str(target_identification.alternate_designations)
    if len(alias) != 0:
        alias_list = alias.split("\n")
    alias_nodes: List[NodeBuilder] = [
        make_alias(alias) for alias in alias_list
    ]

    target_description = str(target_identification.description)
    if len(target_description) != 0:
        # properly align multi line textnodes with 8 spaces
        target_description = " " * 8 + target_description
        target_description = target_description.replace("\n", "\n" + " " * 8)
    else:
        target_description = " " * 8 + "None"
    description_nodes: List[NodeBuilder] = [
        make_description(target_description)
    ]

    if not use_mod_date_for_testing:
        # Get the date when the label is created
        mod_date = get_current_date()
    else:
        mod_date = MOD_DATE_FOR_TESTESING

    try:
        label = (make_label({
            "target_lid":
            target_lid,
            "target_vid":
            "1.0",
            "title":
            target_identification.name,
            "alias":
            combine_nodes_into_fragment(alias_nodes),
            "name":
            target_identification.name,
            "type":
            target_identification.type,
            "description":
            combine_nodes_into_fragment(description_nodes),
            "mod_date":
            mod_date,
        }).toxml().encode())
    except Exception as e:
        raise LabelError(target_lidvid) from e

    return pretty_and_verify(label, verify)