示例#1
0
def spectrum_view(library, spec_id):
    lambda_min = 3600
    lambda_max = 9600
    try:
        lambda_min = float(request.form.get("lambda_min"))
    except (TypeError, ValueError):
        pass
    try:
        lambda_max = float(request.form.get("lambda_max"))
    except (TypeError, ValueError):
        pass

    parent_url = url_for("library_search", library=library)
    self_url = url_for("spectrum_view", library=library, spec_id=spec_id)
    txt_url = url_for("spectrum_txt", library=library, spec_id=spec_id)
    data_url = url_for("spectrum_json", library=library, spec_id=spec_id)
    png_url = url_for("spectrum_png", library=library, spec_id=spec_id, lambda_min=lambda_min, lambda_max=lambda_max)

    path = os_path.join(args.path, library)
    x = SpectrumLibrarySqlite(path=path)

    metadata_keys = x.list_metadata_fields()
    metadata_keys.sort()
    metadata = x.get_metadata(ids=int(spec_id))[0]
    metadata["spectrum_id"] = spec_id

    return render_template('spectrum.html', path=args.path, library=library, metadata_keys=metadata_keys,
                           parent_url=parent_url, metadata=metadata,
                           txt_url=txt_url, data_url=data_url, png_url=png_url,
                           self_url=self_url, lambda_min=lambda_min, lambda_max=lambda_max)
        time_start = time.time()
        stellar_labels = rv_code.fit_rv(test_spectrum_with_rv)
        time_end = time.time()

        # If this is the first object, write column headers
        if not column_headings_written:
            line1 = "# {:5s} {:7s} {:11s} ".format("Steps", "Time", "RV_in")
            line2 = "# {:5d} {:7d} {:11d} ".format(1, 2, 3)
            column_counter = 3
            stellar_label_names = list(stellar_labels.keys())
            stellar_label_names.sort()
            for key in stellar_label_names:
                column_counter += 1
                line2 = "%-*s %s" % (len(line1), line2, column_counter)
                line1 += "{}_out ".format(key)
            for key in test_library.list_metadata_fields():
                column_counter += 1
                line2 = "%-*s %s" % (len(line1), line2, column_counter)
                line1 += "{} ".format(key)
            output.write("{}\n{}\n".format(line1, line2))
            column_headings_written = True

        # Write a line to the output data file
        line = "{:5d} {:9.1f} {:11.3f} ".format(rv_code.n_steps,
                                                time_end - time_start,
                                                radial_velocity)
        for key in stellar_label_names:
            line += "{} ".format(stellar_labels.get(key, "-"))
        for key in test_library.list_metadata_fields():
            line += "{} ".format(test_spectrum.metadata.get(key, "-"))
def tabulate_labels(library_list, label_list, output_file, workspace=None):
    """
    Take a SpectrumLibrary and tabulate a list of the stellar parameters of the stars within it.

    :param workspace:
        Path to the workspace where we expect to find SpectrumLibraries stored
    :param library_list:
        A list of the SpectrumLibraries we are to tabulate the contents of
    :param label_list:
        A list of the labels whose values we are to tabulate
    :param output_file:
        The filename of the ASCII output file we are to produce
    :return:
        None
    """

    # Set path to workspace where we expect to find libraries of spectra
    if workspace is None:
        our_path = os_path.split(os_path.abspath(__file__))[0]
        workspace = os_path.join(our_path, "../../../../workspace")

    # Open output data file
    with open(output_file, "w") as output:
        # Loop over each spectrum library in turn
        for library in library_list:

            # Extract name of spectrum library we are to open. Filter off any constraints which follow the name in []
            test = re.match("([^\[]*)\[(.*)\]$", library)
            if test is None:
                library_name = library
            else:
                library_name = test.group(1)

            # Open spectrum library and extract list of metadata fields which are defined on this library
            library_path = os_path.join(workspace, library_name)
            library_object = SpectrumLibrarySqlite(path=library_path,
                                                   create=False)
            metadata_fields = library_object.list_metadata_fields()

            # Now search library for spectra matching any input constraints, with additional constraint on only
            # returning continuum normalised spectra, if that field is defined for this library
            constraints = {}
            if "continuum_normalised" in metadata_fields:
                constraints["continuum_normalised"] = 1

            library_spectra = SpectrumLibrarySqlite.open_and_search(
                library_spec=library,
                workspace=workspace,
                extra_constraints=constraints)

            # Write column headers at the top of the output
            columns = label_list if label_list is not None else library_object.list_metadata_fields(
            )
            output.write("# ")
            for label in columns:
                output.write("{} ".format(label))
            output.write("\n")

            # Loop over objects in each spectrum library
            for item in library_spectra["items"]:
                metadata = library_object.get_metadata(ids=item['specId'])[0]

                for label in columns:
                    output.write("{} ".format(metadata.get(label, "-")))
                output.write("\n")