Ejemplo n.º 1
0
def init_source_wl(spec: Spectrum) -> float:
    """
    When binning a source spectrum with the intention of shifting it back to rest frame and avoid re-binning,
    this method what the desired first wavelength should be in the source frame.

    Operates under the assumption that the desired rest frame bins are integer wavelength values.

    ie. if first wl in spectrum is 3805.43 at a redshift of z = 0.55, the first rest frame wavelength is 2454.4709...
    Thus, the first (integer wavelength) bin begins at 2454.

    Returning to the source frame, this becomes 3805.12.
    This method would, in this case, return 3805.12.  Pass this to the source binning process and it will begin the
    binning process there.


    :param spec: Source frame spectrum
    :type spec: Spectrum
    :return: initial source bin wavelength
    :rtype: float
    """
    z = shenCat.subkey(spec.getNS(), 'z')
    first_wl = spec.getWavelengths()[0] / (1 + z)
    first_wl = int(first_wl)
    first_wl *= (1 + z)
    return first_wl
Ejemplo n.º 2
0
def text_write(spec: Spectrum, path: str, filename: str) -> None:
    """
    Writes an ASCII formatted spec file with appropriate header information.

    Format can be read in by spec_load_write.text_load() method


    :param spec: spectrum to be written
    :param path: /path/to/write/
    :param filename: filename to write in path
    :type spec: Spectrum
    :type path: str
    :type filename: str
    :return: None
    :rtype: None
    """
    dirCheck(path)

    with open(join(path, filename), 'w') as outfile:
        header = "namestring=%s,z=%f,gmag=%f%s" % (spec.getNS(), spec.getRS(),
                                                   spec.getGmag(), os.linesep)
        outfile.writelines(header)

        fieldnames = ["wavelength", "flux density", "error"]
        writer = DictWriter(outfile, fieldnames=fieldnames)
        writer.writeheader()
        writer.writerows(spec.lineDictList())
Ejemplo n.º 3
0
def make_spectrum_plotitem( spec: Spectrum, color: str = None ) -> Gnuplot.Data:
    """
    Takes in a Spectrum class and returns Gnuplot.Data format for plotting using the data within the Spectrum object
     
    :param spec:
    :type spec: Spectrum
    :param color: 
    :type color: str
    :return: 
    :rtype: Gnuplot.Data
    """
    if color is not None:
        _with = f' lc "{color}"'
    return make_line_plotitem( spec.getWavelengths(), spec.getFluxlist(), title=spec.getNS(), with_=_with )
Ejemplo n.º 4
0
def spectrum_plot( path: str, filename: str, spec: Spectrum, color: str = "royalblue", debug: bool = False ) -> None:
    """
    Simple spectrum plotter.  Passes values to Gnuplot.py.  Plots in PDF format.
    
    If debug = True, no values will be written to the disk and Gnuplot will be initialized with persist = True and
    the path/filename values should be passed as empty strings "" to avoid an error.
    
    :param spec: Spectrum to be plot
    :type spec: Spectrum
    :param path: /path/to/output/file
    :type path: str
    :param filename: output file name
    :type filename: str
    :param color: Color to make the spectrum line.  Defaults to "royalblue."  Must be a color acceptable to Gnuplot
    :type color: str
    :param debug: Use the debug process, print outputs on the screen
    :type debug: bool
    :rtype: None
    """
    from common.messaging import ANGSTROM, FLUX_UNITS
    from fileio.utils import dirCheck

    if not debug: dirCheck( path )
    g = Gnuplot.Gnuplot( persist=debug )
    g.title( spec.getNS() )
    g.xlabel( f"Wavelength ({ANGSTROM})" )
    g.ylabel( f"Flux Density ({FLUX_UNITS})" )
    g( 'set key top right' )
    g( 'set grid' )
    if not debug:
        if not filename.lower().endswith( ".pdf" ): filename += ".pdf"
        g( 'set terminal pdf color enhanced size 9,6' )
        g( f'set output {__fix_outpath( path, "%s" % filename )}' )

    g.plot( make_spectrum_plotitem( spec, color=color ) )

    if not debug:
        g( 'set output' )
        g.close()