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)
def library_search(library): self_url = url_for("library_search", library=library) path = os_path.join(args.path, library) x = SpectrumLibrarySqlite(path=path) metadata_keys = [str(i) for i in x._metadata_fields] metadata_keys.sort() # Fetch search constraints from POST data search = {"minima": {}, "maxima": {}} # This structure contains strings which we put into the search form constraints = {} # This structure contains the float / string constraints which we pass to the SpectrumLibrary # Loop over each of the metadata items we can be constrained on for item in metadata_keys: lower_limit = str(request.form.get("min_{}".format(item), "")) upper_limit = str(request.form.get("max_{}".format(item), "")) search['minima'][item] = lower_limit search['maxima'][item] = upper_limit # We only have a constraint on this metadata item if we have POST data which is not blank have_constraint = (lower_limit != "") or (upper_limit != "") # If we do have a constraint, test whether it's a string constraint, or a numeric constraint if have_constraint: lower_limit_float = 0 upper_limit_float = 1e9 # Test whether we get an exception when we try converting constraint to floats try: if lower_limit != "": lower_limit_float = float(lower_limit) if upper_limit != "": upper_limit_float = float(upper_limit) string_constraint = False except ValueError: string_constraint = True # Create a new numeric metadata constraint if not string_constraint: constraints[item] = ((lower_limit_float, upper_limit_float) if lower_limit_float != upper_limit_float else lower_limit_float) # Create a new string metadata constraint else: if lower_limit is None: lower_limit = "" if upper_limit is None or upper_limit == "": upper_limit = "zzzzzzzzz" constraints[item] = (lower_limit, upper_limit) if lower_limit != upper_limit else lower_limit # Search the SpectrumLibrary for matching spectra spectrum_ids = [i['specId'] for i in x.search(**constraints)] result_count = len(spectrum_ids) # Show a maximum of 100 results if len(spectrum_ids) > 100: spectrum_ids = spectrum_ids[:100] results = x.get_metadata(ids=spectrum_ids) # Add spectrum_id into each spectrum's metadata -- the HTML template needs this so we can link to spectrum viewer for i in range(len(spectrum_ids)): results[i]["spectrum_id"] = spectrum_ids[i] return render_template('library.html', path=args.path, library=library, metadata_keys=metadata_keys, search=search, results=results, result_count=result_count, self_url=self_url)
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")