Esempio n. 1
0
def source_to_unknowns(source, write=False, verbose=True, min_ampl=0.):
    """
    Parameters
    ----------
    source
    write
    min_ampl : float (optional)
      Minimum amplitude for a new UNKNOWN

    Returns
    -------

    """

    # Load
    src_dict = load_source.load(source)
    U_lines = src_dict['U_lines']

    # Cut on amplitude
    print("Cutting UNKNOWNs on min_ampl")
    U_lines = U_lines[U_lines['amplitude'] > min_ampl]
    src_dict['U_lines'] = U_lines

    # UNKNWN lines
    if U_lines is None:
        return

    # Unique ions
    uions = arcl_utils.unique_ions(source, src_dict=src_dict)

    # Check against 'complete' NIST and our line lists
    mask, _ = arcl_utils.vette_unkwn_against_lists(U_lines,
                                                   uions,
                                                   verbose=verbose)
    if np.sum(mask) == 0:
        return

    if not os.path.isfile(unk_file):  # Generate?
        if write:
            print("Generating line list:\n   {:s}".format(unk_file))
            unknwn_list = create_line_list(U_lines[mask > 0],
                                           source['File'],
                                           source['Instr'],
                                           unknown=True,
                                           ions=uions)
            arcl_io.write_line_list(unknwn_list, unk_file)
    else:  # Update
        unknwn_list, updated = update_uline_list(U_lines[mask > 0],
                                                 source['File'],
                                                 source['Instr'], uions)
        if write and updated:
            arcl_io.write_line_list(unknwn_list, unk_file)
    # Return
    return unknwn_list
Esempio n. 2
0
def source_to_line_lists(source, write=False, llist_dict=None):
    """
    Parameters
    ----------
    source
    write
    scratch : bool, optional
      Build from scratch -- EXPERTS ONLY

    Returns
    -------

    """
    if llist_dict is None:  # For building without writing
        llist_dict = {}
    # Parse
    src_dict = load_source.load(source)
    # Check
    if src_dict['ID_lines'] is None:
        print("No IDs in source: {:s}".format(source['File']))
        return llist_dict

    # Unique ions
    uions = arcl_utils.unique_ions(source, src_dict=src_dict)

    # Loop on ID ions
    for ion in uions:
        ion_file = llist_path + '{:s}_lines.dat'.format(ion)
        # Parse
        idx = src_dict['ID_lines']['ion'] == ion
        sub_tbl = src_dict['ID_lines'][idx]
        # Generate?
        if (not os.path.isfile(ion_file)) and (ion not in llist_dict.keys()):
            # New
            llist_dict[ion] = create_line_list(sub_tbl, source['File'],
                                               source['Instr'])
            if not write:
                print("Would generate line list:\n   {:s}".format(ion_file))
            else:
                print("Generating line list:\n   {:s}".format(ion_file))
                arcl_io.write_line_list(llist_dict[ion], ion_file)
        else:
            if ion not in llist_dict.keys():
                raise KeyError(
                    "You are trying to build from scratch but didn't remove {:s}"
                    .format(ion_file))
            llist_dict[ion], updated = update_line_list(
                llist_dict[ion], sub_tbl, source['File'], source['Instr'])
            # Write
            if write and updated:
                arcl_io.write_line_list(llist_dict[ion], ion_file)
    # Return
    return llist_dict
Esempio n. 3
0
def master_build(write=False, nsources=None, plots=True, verbose=True):
    """ Master loop to build the line lists

    Parameters
    ----------
    check_only : bool, optional
      Only check to see what would be created, i.e. do not execute
    nsources : int, optional
      Mainly for step-by-step build
    plots : bool, optional
      Generate plots, if write=True too
    """
    # Load sources
    sources = arcl_io.load_source_table()
    if nsources is not None:
        sources = sources[0:nsources]

    # Loop on sources
    for source in sources:
        print("=============================================================")
        print("Working on source {:s}".format(source['File']))
        print("=============================================================")
        # Load line table
        ID_lines, U_lines = load_source.load(source['File'], source['Format'],
                                             source['Lines'].split(','),
                                             plot=plots, wvmnx=[source['wvmin'], source['wvmax']])
        # Lines (Double check)
        src_lines = source['Lines'].split(',')
        if ID_lines is None:
            uions = src_lines
        else:
            # Check
            uions = np.unique(ID_lines['ion'].data)
            for src_line in src_lines:
                if src_line not in uions.tolist():
                    raise ValueError("Line {:s} not found in ID_lines".format(src_line))
            # Loop on ID ions
            for ion in uions:
                # Parse
                idx = ID_lines['ion'] == ion
                sub_tbl = ID_lines[idx]
                # Generate?
                ion_file = llist_path+'{:s}_lines.dat'.format(ion)
                if not os.path.isfile(ion_file):
                    if not write:
                        print("Would generate line list:\n   {:s}".format(ion_file))
                    else:
                        print("Generating line list:\n   {:s}".format(ion_file))
                        create_line_list(sub_tbl, source['File'],
                                     source['Instr'], ion_file)
                else:
                    update_line_list(sub_tbl, source['File'],
                                     source['Instr'], ion_file, write=write)
Esempio n. 4
0
def main(args=None):
    """ Run
    Parameters
    ----------
    args

    Returns
    -------

    """
    # Grab arguments
    pargs = parser(options=args)

    import numpy as np
    from astropy.table import vstack
    from arclines import build_lists
    from arclines import io as arcl_io
    from arclines import load_source
    from arclines import plots as arcl_plots
    from arclines import utils as arcl_utils


    print("=============================================================")
    print("This script is for EXPERTS ONLY")
    print("Continue only if you know what you are doing")
    print("This script adds only the *last* source listed")
    print("Otherwise exit")
    print("=============================================================")
    if not pargs.skip_stop:
        pdb.set_trace()

    # Load sources
    sources = arcl_io.load_source_table()
    source = sources[-1]

    # Load line lists
    uions = arcl_utils.unique_ions(source)
    llist_dict = {}
    for ion in uions:
        try:
            llist_dict[ion] = arcl_io.load_line_list(ion, use_ion=True)
        except IOError:
            print("No linelist found for ion={:s}.  Will create one".format(ion))


    # IDs
    print("=============================================================")
    print("Working on adding IDs from source {:s}".format(source['File']))
    print("=============================================================")
    llist_dict = build_lists.source_to_line_lists(source, write=pargs.write,
                                     llist_dict=llist_dict)
    # Create line list
    ll = []
    for key in llist_dict.keys():
        ll.append(llist_dict[key])
    line_lists = vstack(ll)

    # Purge unknowns
    print("=============================================================")
    print("Purging UNKNOWNs")
    print("=============================================================")
    build_lists.purge_unknowns(line_lists, write=pargs.write)

    print("=============================================================")
    print("Working on adding Unknowns from source {:s}".format(source['File']))
    print("=============================================================")
    if not pargs.no_unknowns:
        unknwns = build_lists.source_to_unknowns(source, write=pargs.write)
        unknwns.remove_column('line_flag')
    else:
        unknwns = None

    if unknwns is not None:
        ll.append(unknwns)
    # Loop to my loop
    print("=============================================================")
    print("Working on plot for {:s}".format(source['File']))
    print("=============================================================")
    # Load
    src_dict = load_source.load(source)
    uions = arcl_utils.unique_ions(source, src_dict=src_dict)
    src_dict['uions'] = uions
    # Outfile
    iext = source['File'].rfind('.')
    outfile = source['File'][:iext]+'.pdf'
    title = source['File'][:iext]
    #
    arcl_plots.show_source(src_dict, line_lists, outfile, title=title, clobber=True)