Esempio n. 1
0
def test_fits_mixins_qtable_to_table(tmpdir):
    """Test writing as QTable and reading as Table.  Ensure correct classes
    come out.
    """
    filename = str(tmpdir.join('test_simple.fits'))

    names = sorted(mixin_cols)

    t = QTable([mixin_cols[name] for name in names], names=names)
    t.write(filename, format='fits')
    t2 = Table.read(filename, format='fits', astropy_native=True)

    assert t.colnames == t2.colnames

    for name, col in t.columns.items():
        col2 = t2[name]

        # Special-case Time, which does not yet support round-tripping
        # the format.
        if isinstance(col2, Time):
            col2.format = col.format

        attrs = compare_attrs[name]
        compare_class = True

        if isinstance(col.info, QuantityInfo):
            # Downgrade Quantity to Column + unit
            assert type(col2) is Column
            # Class-specific attributes like `value` or `wrap_angle` are lost.
            attrs = ['unit']
            compare_class = False
            # Compare data values here (assert_objects_equal doesn't know how in this case)
            assert np.all(col.value == col2)

        assert_objects_equal(col, col2, attrs, compare_class)
Esempio n. 2
0
def test_possible_string_format_functions():
    """
    The QuantityInfo info class for Quantity implements a
    possible_string_format_functions() method that overrides the
    standard pprint._possible_string_format_functions() function.
    Test this.
    """
    t = QTable([[1, 2] * u.m])
    t['col0'].info.format = '%.3f'
    assert t.pformat() == [' col0',
                           '  m  ',
                           '-----',
                           '1.000',
                           '2.000']

    t['col0'].info.format = 'hi {:.3f}'
    assert t.pformat() == ['  col0  ',
                           '   m    ',
                           '--------',
                           'hi 1.000',
                           'hi 2.000']

    t['col0'].info.format = '.4f'
    assert t.pformat() == [' col0 ',
                           '  m   ',
                           '------',
                           '1.0000',
                           '2.0000']
Esempio n. 3
0
def test_ecsv_mixins_qtable_to_table():
    """Test writing as QTable and reading as Table.  Ensure correct classes
    come out.
    """
    names = sorted(mixin_cols)

    t = QTable([mixin_cols[name] for name in names], names=names)
    out = StringIO()
    t.write(out, format="ascii.ecsv")
    t2 = Table.read(out.getvalue(), format='ascii.ecsv')

    assert t.colnames == t2.colnames

    for name, col in t.columns.items():
        col2 = t2[name]
        attrs = compare_attrs[name]
        compare_class = True

        if isinstance(col.info, QuantityInfo):
            # Downgrade Quantity to Column + unit
            assert type(col2) is Column
            # Class-specific attributes like `value` or `wrap_angle` are lost.
            attrs = ['unit']
            compare_class = False
            # Compare data values here (assert_objects_equal doesn't know how in this case)
            assert np.allclose(col.value, col2, rtol=1e-10)

        assert_objects_equal(col, col2, attrs, compare_class)
Esempio n. 4
0
        def generic_export(spectrum, path):
            """
            Creates a temporary export format for use in writing out data.
            """
            from astropy.table import QTable
            import astropy.units as u

            data = {
                'spectral_axis': spectrum.spectral_axis,
                'flux': spectrum.flux,
                'mask': spectrum.mask if spectrum.mask is not None
                        else u.Quantity(np.ones(spectrum.spectral_axis.shape))
            }

            if spectrum.uncertainty is not None:
                data['uncertainty'] = spectrum.uncertainty.array * spectrum.uncertainty.unit

            meta = {}

            if spectrum.meta is not None and 'header' in spectrum.meta:
                meta.update({'header': {k: v for k, v in
                                        spectrum.meta['header'].items()}})

            tab = QTable(data, meta=meta)
            tab.write(path, format='ascii.ecsv')
Esempio n. 5
0
    def load_HST_WFC3(cls):
        """ Load the LLS survey using HST/WFC3

        by O'Meara et al. 2013, ApJ, 765, 137

        Parameters
        ----------

        Returns
        -------
        lls_survey : IGMSurvey
        """
        # LLS File
        lls_fil = pyigm_path+'/data/LLS/HST/lls_wfc3_stat_LLS.fits.gz'
        lls = QTable.read(lls_fil)

        # Rename some columns?
        lls.rename_column('QSO_RA', 'RA')
        lls.rename_column('QSO_DEC', 'DEC')

        # Read
        lls_survey = cls.from_sfits(lls)
        lls_survey.ref = 'HST-WFC3'

        # QSOs file
        qsos_fil = pyigm_path+'/data/LLS/HST/lls_wfc3_qsos_sn1020.fits.gz'
        qsos = QTable.read(qsos_fil)
        lls_survey.sightlines = qsos

        # Return
        print('HST-WFC3: Loaded')
        return lls_survey
Esempio n. 6
0
def test_insert_row_bad_unit():
    """
    Insert a row into a QTable with the wrong unit
    """
    t = QTable([[1] * u.m])
    with pytest.raises(ValueError) as exc:
        t.insert_row(0, (2 * u.m / u.s,))
    assert "'m / s' (speed) and 'm' (length) are not convertible" in str(exc.value)
Esempio n. 7
0
def test_auto_format_func():
    """Test for #5802 (fix for #5800 where format_func key is not unique)"""
    t = Table([[1, 2] * u.m])
    t['col0'].format = '%f'
    t.pformat()  # Force caching of format function

    qt = QTable(t)
    qt.pformat()  # Generates exception prior to #5802
Esempio n. 8
0
    def load_mage_z3(cls, sample='all'):
        """ Load the LLS table from the z~3 MagE survey

        (Fumagalli et al. 2013, ApJ, 775, 78)

        Parameters
        ----------
        sample : str
          Survey sample
            * all -- All
            * non-color -- Restricts to quasars that were *not* color-selected
            * color -- Restricts to quasars that were color-selected

        Returns
        -------
        lls_survey : IGMSurvey
          Includes all quasars observed in the survey
          And all the LLS

        """
        # LLS File
        survey_fil = pyigm_path+'/data/LLS/HD-LLS/fumagalli13_apj775_78_tab1+2.fits'
        tab = Table.read(survey_fil)

        # Rename some columns
        tab.rename_column('RAJ2000', 'RA')
        tab['RA'].unit = u.deg
        tab.rename_column('DEJ2000', 'DEC')
        tab['DEC'].unit = u.deg
        tab.rename_column('zqso', 'Z_QSO')
        tab.rename_column('zlls', 'Z_LLS')
        tab.rename_column('zend', 'Z_START')  # F13 was opposite of POW10
        tab.rename_column('zstart', 'Z_END')  # F13 was opposite of POW10

        # Cut table
        if sample == 'all':
            pass
        elif sample == 'non-color':
            NC = np.array([True if row['n_Name'][0] == 'N' else False for row in tab])
            tab = tab[NC]
        elif sample == 'color':
            Clr = [True if row['n_Name'][0] == 'C' else False for row in tab]
            tab = tab[Clr]

        # Good LLS
        lls = tab['Z_LLS'] >= tab['Z_START']
        lls_tab = QTable(tab[lls])
        nlls = np.sum(lls)
        # Set NHI to 17.8 (tau>=2)
        lls_tab.add_column(Column([17.8]*nlls, name='NHI'))
        lls_tab.add_column(Column([99.9]*nlls, name='SIGNHI'))

        # Generate survey
        lls_survey = cls.from_sfits(lls_tab)
        lls_survey.ref = 'z3_MagE'
        lls_survey.sightlines = tab

        return lls_survey
Esempio n. 9
0
    def load_SDSS_DR7(cls, sample='stat'):
        """ Load the LLS from the SDSS-DR7 survey (Prochaska+10, ApJ, 718, 391)

        Parameters
        ----------
        sample : str, optional
          LLS sample
            stat : Statistical sample
            all : All LLS
            nonstat : Non-statistical sample


        Returns
        -------
        lls_survey : IGMSurvey

        """
        # LLS File
        lls_fil = pyigm_path+'/data/LLS/SDSS/lls_dr7_stat_LLS.fits.gz'
        print('SDSS-DR7: Loading LLS file {:s}'.format(lls_fil))
        lls = QTable.read(lls_fil)

        # Rename some columns?
        lls.rename_column('QSO_RA', 'RA')
        lls.rename_column('QSO_DEC', 'DEC')

        # Read
        lls_survey = cls.from_sfits(lls)
        lls_survey.ref = 'SDSS-DR7'

        # QSOs file
        qsos_fil = pyigm_path+'/data/LLS/SDSS/lls_dr7_qsos_sn2050.fits.gz'
        print('SDSS-DR7: Loading QSOs file {:s}'.format(qsos_fil))
        qsos = QTable.read(qsos_fil)
        lls_survey.sightlines = qsos

        # All?
        if sample == 'all':
            return lls_survey


        # Stat
        # z_em cut
        zem_min = 3.6
        lowz_q = qsos['ZEM'] < zem_min
        qsos['ZT2'][lowz_q] = 99.99

        # Generate mask
        print('SDSS-DR7: Performing stats (~60s)')
        mask = lls_stat(lls_survey, qsos)
        if sample == 'stat':
            lls_survey.mask = mask
        else:
            lls_survey.mask = ~mask
        # Return
        print('SDSS-DR7: Loaded')
        return lls_survey
Esempio n. 10
0
def test_quantity_representation():
    """
    Test that table representation of quantities does not have unit
    """
    t = QTable([[1, 2] * u.m])
    assert t.pformat() == ['col0',
                           ' m  ',
                           '----',
                           ' 1.0',
                           ' 2.0']
Esempio n. 11
0
def test_convert_np_array(mixin_cols):
    """
    Test that converting to numpy array creates an object dtype and that
    each instance in the array has the expected type.
    """
    t = QTable(mixin_cols)
    ta = t.as_array()
    m = mixin_cols['m']
    dtype_kind = m.dtype.kind if hasattr(m, 'dtype') else 'O'
    assert ta['m'].dtype.kind == dtype_kind
Esempio n. 12
0
 def __getitem__(self, item):
     if self._is_list_or_tuple_of_str(item):
         if 'time_bin_start' not in item or 'time_bin_size' not in item:
             out = QTable([self[x] for x in item],
                          meta=deepcopy(self.meta),
                          copy_indices=self._copy_indices)
             out._groups = groups.TableGroups(out, indices=self.groups._indices,
                                              keys=self.groups._keys)
             return out
     return super().__getitem__(item)
def read_observation_table(pathin):
    if pathin[-4:]=='.csv':
        table = ascii.read(pathin)
        qtable = QTable(table)
        for key in qtable.keys():
            if key[-3:]=='[s]' or key[-5:]=='[sec]':
                qtable[key] = qtable[key] * u.s
            if key[-5:]=='[min]':
                qtable[key] = qtable[key] * u.min
            if key[-3:]=='[h]' or key[-4:]=='[hr]' or key[-6:]=='[hour]':
                qtable[key] = qtable[key] * u.s
            if key[-3:]=='[d]' or key[-5:]=='[day]':
                qtable[key] = qtable[key] * u.d
            if key[-5:]=='[mag]':
                qtable[key] = qtable[key] * u.Magnitude
            if key[-4:]=='[Jy]':
                qtable[key] = qtable[key] * u.Jy
            if key[-5:]=='[mJy]':
                qtable[key] = qtable[key] * u.mJy
            if key[-12:]=='[erg/cm^2/s]':
                qtable[key] = qtable[key] * u.erg / u.cm / u.cm / u.s
    elif pathin[-7:]=='.pickle':
        pdata = pickle_utilities.load(pathin)['results']
        time = []
        time_err_lo = []
        time_err_hi = []
        eflux = []
        eflux_err_lo = []
        eflux_err_hi = []
        for period in pdata:
            time.append(np.sqrt(period['time']['min']*period['time']['max']))
            time_err_lo.append(time[-1] - period['time']['min'])
            time_err_hi.append(period['time']['max'] - time[-1])
            
            dll_map = period['dloglike']['dloglike']
            eflux_map = period['dloglike']['eflux']

            args_bestlike = zip(np.where(dll_map==dll_map.min())[0], np.where(dll_map==dll_map.min())[1])[0]

            eflux_shown = eflux_map[2.*dll_map<=2.30]
            eflux_min = min(eflux_shown.flatten())
            eflux_max = max(eflux_shown.flatten())
            eflux.append(eflux_map[args_bestlike[0]][args_bestlike[1]])
            eflux_err_hi.append(eflux_max-eflux[-1]) 
            eflux_err_lo.append(eflux[-1]-eflux_min) 
        qtable = QTable()
        qtable['time'] = Column(time, unit=u.s)
        qtable['time_err_lo'] = Column(time_err_lo, unit=u.s)
        qtable['time_err_hi'] = Column(time_err_hi, unit=u.s)
        qtable['eflux'] = Column(eflux, unit=u.MeV/u.cm/u.cm/u.s)
        qtable['eflux_err_lo'] = Column(eflux_err_lo, unit=u.MeV/u.cm/u.cm/u.s)
        qtable['eflux_err_hi'] = Column(eflux_err_hi, unit=u.MeV/u.cm/u.cm/u.s)

    return qtable
Esempio n. 14
0
def get_coord(targ_file, radec=None):
    '''
    radec: int (None)
      None: Read from ASCII file
      1: List of [Name, RA, DEC] with RA/DEC as : separated strings
      2: List of [Name, RA, DEC] with RA/DEC as decimal degrees
    '''

    if not isinstance(targ_file,basestring):
        raise IOError('Bad input to finder.get_coord!')

    from astropy.io import ascii 
    # Import Tables
    if radec == None:
        # Read 
        ra_tab = ascii.read(targ_file) #, names=('Name','RA','DEC','Epoch'))
        # Rename the columns
        ra_tab.rename_column('col1','Name')
        if isinstance(ra_tab['col2'][0],basestring):
            ra_tab.rename_column('col2','RAS')
            ra_tab.rename_column('col3','DECS')
        else:
            ra_tab.rename_column('col2','RA')
            ra_tab.rename_column('col3','DEC')
    elif radec == 1: 
        # Error check
        if len(targ_file) != 3:
            return -1
        # Manipulate
        arr = np.array(targ_file).reshape(1,3)
        # Generate the Table
        ra_tab = QTable( arr, names=('Name','RAS','DECS') )
    elif radec == 2: 
        # Error check
        if len(targ_file) != 3:
            return -1
        # Manipulate
        ras, decs = x_radec.dtos1((targ_file[1], targ_file[2]))
        # Generate the Table
        ra_tab = QTable( [ [targ_file[0]], [ras], [decs] ], names=('Name','RA','DEC') )
    else:
        raise ValueError('get_coord: Bad flag')

    # Add dummy columns for decimal degrees and EPOCH
    nrow = len(ra_tab)
    col_RAD = Column(name='RAD', data=np.zeros(nrow), unit=u.degree)
    col_DECD = Column(name='DECD', data=np.zeros(nrow), unit=u.degree)
    col_EPOCH = Column(name='EPOCH', data=np.zeros(nrow))
    ra_tab.add_columns( [col_RAD, col_DECD, col_EPOCH] )
    # Assume 2000 for now
    ra_tab['EPOCH'] = 2000.
        
    return ra_tab
Esempio n. 15
0
def test_insert_row(mixin_cols):
    """
    Test inserting a row, which only works for BaseColumn and Quantity
    """
    t = QTable(mixin_cols)
    t['m'].info.description = 'd'
    if isinstance(t['m'], (u.Quantity, Column, time.Time)):
        t.insert_row(1, t[-1])
        assert t[1] == t[-1]
        assert t['m'].info.description == 'd'
    else:
        with pytest.raises(ValueError) as exc:
            t.insert_row(1, t[-1])
        assert "Unable to insert row" in str(exc.value)
Esempio n. 16
0
def test_io_ascii_write():
    """
    Test that table with mixin column can be written by io.ascii for
    every pure Python writer.  No validation of the output is done,
    this just confirms no exceptions.
    """
    from astropy.io.ascii.connect import _get_connectors_table
    t = QTable(MIXIN_COLS)
    for fmt in _get_connectors_table():
        if fmt['Format'] == 'ascii.ecsv' and not HAS_YAML:
            continue
        if fmt['Write'] and '.fast_' not in fmt['Format']:
            out = StringIO()
            t.write(out, format=fmt['Format'])
Esempio n. 17
0
 def build_table(self):
     """Generate an astropy QTable out of the component.
     Returns
     -------
     comp_tbl : QTable
     """
     if len(self._abslines) == 0:
         return
     comp_tbl = QTable()
     comp_tbl.add_column(Column([iline.wrest.to(u.AA).value for iline in self._abslines]*u.AA, name='wrest'))
     for attrib in ['z', 'flag_N', 'logN', 'sig_logN']:
         comp_tbl.add_column(Column([iline.attrib[attrib] for iline in self._abslines], name=attrib))
     # Return
     return comp_tbl
Esempio n. 18
0
    def write_to_ascii(self, outfil, format='ascii.ecsv'):
        ''' Write to a text file.

        Parameters
        ----------
        outfil: str
          Filename.
        '''
        # Convert to astropy Table
        table = QTable([self.wavelength, self.flux, self.sig],
                       names=('WAVE', 'FLUX', 'ERROR'))

        # Write
        table.write(outfil, format=format)
Esempio n. 19
0
def test_rename_mixin_columns(mixin_cols):
    """
    Rename a mixin column.
    """
    t = QTable(mixin_cols)
    tc = t.copy()
    t.rename_column('m', 'mm')
    assert t.colnames == ['i', 'a', 'b', 'mm']
    if isinstance(t['mm'], table_helpers.ArrayWrapper):
        assert np.all(t['mm'].data == tc['m'].data)
    elif isinstance(t['mm'], coordinates.SkyCoord):
        assert np.all(t['mm'].ra == tc['m'].ra)
        assert np.all(t['mm'].dec == tc['m'].dec)
    else:
        assert np.all(t['mm'] == tc['m'])
Esempio n. 20
0
def test_votable_quantity_write(tmpdir):
    """
    Test that table with Quantity mixin column can be round-tripped by
    io.votable.  Note that FITS and HDF5 mixin support are tested (much more
    thoroughly) in their respective subpackage tests
    (io/fits/tests/test_connect.py and io/misc/tests/test_hdf5.py).
    """
    t = QTable()
    t['a'] = u.Quantity([1, 2, 4], unit='Angstrom')

    filename = str(tmpdir.join('table-tmp'))
    t.write(filename, format='votable', overwrite=True)
    qt = QTable.read(filename, format='votable')
    assert isinstance(qt['a'], u.Quantity)
    assert qt['a'].unit == 'Angstrom'
Esempio n. 21
0
def read_verner94():
    """ Read Verner1994 Table
    """
    # Read
    verner94 = lt_path + '/data/lines/verner94_tab6.fits'
    print(
        'linetools.lists.parse: Reading linelist --- \n   {:s}'.format(
            verner94))
    tbl_6 = QTable.read(verner94)


    # My table
    ldict, data = line_data(nrows=len(tbl_6))

    # Fill
    data['wrest'] = tbl_6['lambda']
    data['f'] = tbl_6['Fik']
    data['gj'] = tbl_6['Gi']
    data['gk'] = tbl_6['Gk']
    data['Z'] = tbl_6['Z']
    data['ion'] = tbl_6['Z'] - tbl_6['N'] + 1
    for ii,row in enumerate(tbl_6):
        data[ii]['name'] = (
            row['Species'][0:2].strip() + row['Species'][2:].strip() + 
            ' {:d}'.format(int(row['lambda'].value)))
        #xdb.set_trace()

    #  Finish
    data['group'] = 1
    data['Ref'] = 'Verner1994'
    data['mol'] = ''

    # Return
    return data
Esempio n. 22
0
 def from_sfits(self):
     '''Generate the Survey from a summary FITS file
     '''
     # Read
     systems = QTable.read(self.summ_fits)
     self.nsys = len(systems)
     # Dict
     kdict = dict(NHI=['NHI','logNHI'],
         sigNHI=['sig(logNHI)'],
         name=['Name'], 
         vlim=['vlim'],
         zabs=['Z_LLS'],
         zem=['Z_QSO'],
         RA=['RA'],
         Dec=['DEC','Dec'])
     # Parse the Table
     inputs = {}
     for key in kdict.keys():
         vals, tag = lsio.get_table_column(kdict[key],[systems],idx=0)
         if vals is not None:
             inputs[key] = vals
     # Generate
     for kk,row in enumerate(systems):
         # Generate keywords
         kwargs = {}
         for key in inputs.keys():
             kwargs[key] = inputs[key][kk]
         # Instantiate
         self._abs_sys.append(set_absclass(self.abs_type)(**kwargs))
Esempio n. 23
0
    def from_sfits(cls, summ_fits, **kwargs):
        """Generate the Survey from a summary FITS file

        Handles SPEC_FILES too.

        Parameters
        ----------
        summ_fits : str or Table or QTable
          Summary FITS file
        **kwargs : dict
          passed to __init__
        """
        # Init
        slf = cls(**kwargs)
        # Read
        if isinstance(summ_fits, Table):
            systems = summ_fits
        else:
            systems = QTable.read(summ_fits)
        nsys = len(systems)
        # Dict
        kdict = dict(NHI=['NHI', 'logNHI'],
                     sig_NHI=['sig(logNHI)', 'SIGNHI'],
                     name=['Name'], vlim=['vlim'],
                     zabs=['Z_LLS', 'ZABS', 'zabs'],
                     zem=['Z_QSO', 'QSO_ZEM'],
                     RA=['RA'], Dec=['DEC', 'Dec'])
        # Parse the Table
        inputs = {}
        for key in kdict.keys():
            vals, tag = lsio.get_table_column(kdict[key], [systems],idx=0)
            if vals is not None:
                inputs[key] = vals
        # vlim
        if 'vlim' not in inputs.keys():
            default_vlim = [-1000, 1000.]* u.km / u.s
            inputs['vlim'] = [default_vlim]*nsys
        # Generate
        for kk in range(nsys):
            # Generate keywords
            kwargs = {}
            args = {}
            for key in inputs.keys():
                if key in ['vlim', 'zabs', 'RA', 'Dec']:
                    args[key] = inputs[key][kk]
                else:
                    kwargs[key] = inputs[key][kk]
            # Instantiate
            abssys = class_by_type(slf.abs_type)((args['RA'], args['Dec']), args['zabs'], args['vlim'], **kwargs)
            # spec_files
            try:
                abssys.spec_files += systems[kk]['SPEC_FILES'].tolist()
            except (KeyError, AttributeError):
                pass
            slf._abs_sys.append(abssys)
        # Mask
        slf.init_mask()
        # Return
        return slf
Esempio n. 24
0
def test_votable_mixin_write_fail(mixin_cols):
    """
    Test that table with mixin columns (excluding Quantity) cannot be written by
    io.votable.
    """
    t = QTable(mixin_cols)
    # Only do this test if there are unsupported column types (i.e. anything besides
    # BaseColumn and Quantity class instances).
    unsupported_cols = t.columns.not_isinstance((BaseColumn, u.Quantity))

    if not unsupported_cols:
        pytest.skip("no unsupported column types")

    out = StringIO()
    with pytest.raises(ValueError) as err:
        t.write(out, format='votable')
    assert 'cannot write table with mixin column(s)' in str(err.value)
Esempio n. 25
0
def test_group_mixins():
    """
    Test grouping a table with mixin columns
    """
    # Setup mixins
    idx = np.arange(4)
    x = np.array([3., 1., 2., 1.])
    q = x * u.m
    lon = coordinates.Longitude(x * u.deg)
    lat = coordinates.Latitude(x * u.deg)
    # For Time do J2000.0 + few * 0.1 ns (this requires > 64 bit precision)
    tm = time.Time(2000, format='jyear') + time.TimeDelta(x * 1e-10, format='sec')
    sc = coordinates.SkyCoord(ra=lon, dec=lat)
    aw = table_helpers.ArrayWrapper(x)
    nd = np.array([(3, 'c'), (1, 'a'), (2, 'b'), (1, 'a')],
                  dtype='<i4,|S1').view(NdarrayMixin)

    qt = QTable([idx, x, q, lon, lat, tm, sc, aw, nd],
                names=['idx', 'x', 'q', 'lon', 'lat', 'tm', 'sc', 'aw', 'nd'])

    # Test group_by with each supported mixin type
    mixin_keys = ['x', 'q', 'lon', 'lat', 'tm', 'sc', 'aw', 'nd']
    for key in mixin_keys:
        qtg = qt.group_by(key)

        # Test that it got the sort order correct
        assert np.all(qtg['idx'] == [1, 3, 2, 0])

        # Test that the groups are right
        # Note: skip testing SkyCoord column because that doesn't have equality
        for name in ['x', 'q', 'lon', 'lat', 'tm', 'aw', 'nd']:
            assert np.all(qt[name][[1, 3]] == qtg.groups[0][name])
            assert np.all(qt[name][[2]] == qtg.groups[1][name])
            assert np.all(qt[name][[0]] == qtg.groups[2][name])

    # Test that unique also works with mixins since most of the work is
    # done with group_by().  This is using *every* mixin as key.
    uqt = unique(qt, keys=mixin_keys)
    assert len(uqt) == 3
    assert np.all(uqt['idx'] == [1, 2, 0])
    assert np.all(uqt['x'] == [1., 2., 3.])

    # Column group_by() with mixins
    idxg = qt['idx'].group_by(qt[mixin_keys])
    assert np.all(idxg == [1, 3, 2, 0])
Esempio n. 26
0
    def write_to_ascii(self, outfil, format='ascii.ecsv'):
        ''' Write spectrum to an ASCII file

        Parameters
        ----------
        outfil: str
          Name of the FITS file

        Returns:
        --------
        Outputs an ASCII file
        '''
        # Convert to astropy Table
        table = QTable([self.dispersion, self.flux, self.sig],
            names=('WAVE','FLUX','ERROR'))

        # Write
        table.write(outfil,format=format)
Esempio n. 27
0
def test_ecsv_mixins_ascii_read_class():
    """Ensure that ascii.read(ecsv_file) returns the correct class
    (QTable if any Quantity subclasses, Table otherwise).
    """
    # Make a table with every mixin type except Quantities
    t = QTable({name: col for name, col in mixin_cols.items()
                if not isinstance(col.info, QuantityInfo)})
    out = StringIO()
    t.write(out, format="ascii.ecsv")
    t2 = ascii.read(out.getvalue(), format='ecsv')
    assert type(t2) is Table

    # Add a single quantity column
    t['lon'] = mixin_cols['lon']

    out = StringIO()
    t.write(out, format="ascii.ecsv")
    t2 = ascii.read(out.getvalue(), format='ecsv')
    assert type(t2) is QTable
Esempio n. 28
0
def test_assignment_and_copy():
    """
    Test that assignment of an int, slice, and fancy index works.
    Along the way test that copying table works.
    """
    for name in ('quantity', 'arraywrap'):
        m = MIXIN_COLS[name]
        t0 = QTable([m], names=['m'])
        for i0, i1 in ((1, 2),
                       (slice(0, 2), slice(1, 3)),
                       (np.array([1, 2]), np.array([2, 3]))):
            t = t0.copy()
            t['m'][i0] = m[i1]
            if name == 'arraywrap':
                assert np.all(t['m'].data[i0] == m.data[i1])
                assert np.all(t0['m'].data[i0] == m.data[i0])
                assert np.all(t0['m'].data[i0] != t['m'].data[i0])
            else:
                assert np.all(t['m'][i0] == m[i1])
                assert np.all(t0['m'][i0] == m[i0])
                assert np.all(t0['m'][i0] != t['m'][i0])
Esempio n. 29
0
def test_write_with_mixins():
    """Writing a table with mixins just drops them via to_pandas()

    This also tests passing a kwarg to pandas read and write.
    """
    sc = SkyCoord([1, 2], [3, 4], unit='deg')
    q = [5, 6] * u.m
    qt = QTable([[1, 2], q, sc], names=['i', 'q', 'sc'])

    buf = StringIO()
    qt.write(buf, format='pandas.csv', sep=' ')
    exp = ['i q sc.ra sc.dec',
           '1 5.0 1.0 3.0',
           '2 6.0 2.0 4.0']
    assert buf.getvalue().splitlines() == exp

    # Read it back
    buf.seek(0)
    qt2 = Table.read(buf, format='pandas.csv', sep=' ')
    exp_t = ascii.read(exp)
    assert qt2.colnames == exp_t.colnames
    assert np.all(qt2 == exp_t)
Esempio n. 30
0
def read_elvis_z0(fn):
    tab = QTable.read(fn, format='ascii.commented_header', data_start=0, header_start=1)

    col_name_re = re.compile(r'(.*?)(\(.*\))?$')
    for col in tab.columns.values():
        match = col_name_re.match(col.name)
        if match:
            nm, unit = match.groups()
            if nm != col.name:
                col.name = nm
            if unit is not None:
                col.unit = u.Unit(unit[1:-1])  # 1:-1 to get rid of the parenthesis

    return tab
Esempio n. 31
0
def test_masked_column_with_unit_in_qtable():
    """Test that adding a MaskedColumn with a unit to QTable issues warning"""
    t = QTable()
    with catch_warnings() as w:
        t['a'] = MaskedColumn([1, 2])
    assert len(w) == 0
    assert isinstance(t['a'], MaskedColumn)

    with catch_warnings() as w:
        t['b'] = MaskedColumn([1, 2], unit=u.m)
    assert len(w) == 0
    assert isinstance(t['b'], u.Quantity)

    with catch_warnings() as w:
        t['c'] = MaskedColumn([1, 2], unit=u.m, mask=[True, False])
    assert len(w) == 1
    assert "dropping mask in Quantity column 'c'"
    assert isinstance(t['b'], u.Quantity)
Esempio n. 32
0
def test_create_grism_objects():
    source_catalog = get_file_path('step_SourceCatalogStep_cat.ecsv')

    # create from test ascii file
    grism_objects = read_catalog(source_catalog)
    assert isinstance(grism_objects,
                      list), "return grism objects were not a list"

    required_fields = list(SkyObject()._fields)
    go_fields = grism_objects[0]._fields
    assert all([a == b for a, b in zip(required_fields, go_fields)
                ]), "Required fields mismatch for SkyObject and GrismObject"

    # create from QTable object
    tempcat = QTable.read(source_catalog, format='ascii.ecsv')
    grism_object_from_table = read_catalog(tempcat)
    assert isinstance(grism_object_from_table,
                      list), "return grism objects were not a list"
Esempio n. 33
0
def test_get_items(mixin_cols):
    """
    Test that slicing / indexing table gives right values and col attrs inherit
    """
    attrs = ('name', 'unit', 'dtype', 'format', 'description', 'meta')
    m = mixin_cols['m']
    m.info.name = 'm'
    m.info.format = '{0}'
    m.info.description = 'd'
    m.info.meta = {'a': 1}
    t = QTable([m])
    for item in ([1, 3], np.array([0, 2]), slice(1, 3)):
        t2 = t[item]
        m2 = m[item]
        assert_table_name_col_equal(t2, 'm', m[item])
        for attr in attrs:
            assert getattr(t2['m'].info, attr) == getattr(m.info, attr)
            assert getattr(m2.info, attr) == getattr(m.info, attr)
Esempio n. 34
0
    def catalog(self):
        """
        The final source catalog.
        """
        self.convert_to_jy()
        self.set_segment_properties()
        self.set_aperture_properties()
        self.set_ci_properties()

        catalog = QTable()
        for column in self.colnames:
            catalog[column] = getattr(self, column)
            catalog[column].info.description = self.column_desc[column]

        catalog = self.format_columns(catalog)
        catalog.meta.update(self.catalog_metadata)

        return catalog
Esempio n. 35
0
    def __init__(self, filename='$GAMMA_CAT/docs/data/gammacat.fits.gz'):
        filename = make_path(filename)

        if 'GAMMA_CAT' not in os.environ:
            msg = 'The gamma-cat repo is not available. '
            msg += 'You have to set the GAMMA_CAT environment variable '
            msg += 'to point to the location for it to be found.'
            raise GammaCatNotFoundError(msg)

        self.filename = str(filename)
        table = QTable.read(self.filename)
        source_name_key = 'common_name'
        source_name_alias = ('other_names', 'gamma_names')
        super(SourceCatalogGammaCat, self).__init__(
            table=table,
            source_name_key=source_name_key,
            source_name_alias=source_name_alias,
        )
def test_units():
    """ test units on multi-row tables """

    ground_truth = QTable(
        [[1, 2, 3] * u.Unit('m'), [4, 5, 6] * u.m / u.s, ['a', 'b', 'c']],
        names=('aa', 'bb', 'cc'))

    assert ((ground_truth['aa']**2).unit == 'm2')

    test_dict = DataClass.from_dict(
        OrderedDict((('aa', [1, 2, 3] * u.m), ('bb', [4, 5, 6] * u.m / u.s),
                     ('cc', ['a', 'b', 'c']))))
    assert all(test_dict.table == ground_truth)

    test_array = DataClass.from_columns(
        [[1, 2, 3] * u.m, [4, 5, 6] * u.m / u.s, ['a', 'b', 'c']],
        names=('aa', 'bb', 'cc'))
    assert all(test_array.table == ground_truth)
Esempio n. 37
0
def load_specific_scope_settings(path):
    
    """One scope settings file will be loaded into a table
    example usage:
    PATH = 'Scope_Settings/FAKEDATA_observation_2458180_7314443593_scope_settings.csv'
    scope_settings = load_specific_scope_settings(PATH)"""
    
    
    names = ['obs_JD', 'east_alt', 'east_az', 'west_alt', 'west_az']
    
    table = QTable(ascii.read(path), names = names)
    
    table['east_alt'].unit = u.deg
    table['east_az' ].unit = u.deg
    table['west_alt'].unit = u.deg
    table['west_az' ].unit = u.deg
    
    return table
Esempio n. 38
0
def read_elvis_z0(fn):
    tab = QTable.read(fn,
                      format='ascii.commented_header',
                      data_start=0,
                      header_start=1)

    col_name_re = re.compile(r'(.*?)(\(.*\))?$')
    for col in tab.columns.values():
        match = col_name_re.match(col.name)
        if match:
            nm, unit = match.groups()
            if nm != col.name:
                col.name = nm
            if unit is not None:
                col.unit = u.Unit(
                    unit[1:-1])  # 1:-1 to get rid of the parenthesis

    return tab
Esempio n. 39
0
def _properties_table(obj, columns=None, exclude_columns=None):

    if isinstance(obj, SourceCatalog) and len(obj) == 0:
        raise ValueError('SourceCatalog contains no sources.')

    # all scalar-valued properties
    columns_all = [
        'id', 'xcentroid', 'ycentroid', 'sky_centroid', 'sky_centroid_icrs',
        'source_sum', 'source_sum_err', 'background_sum', 'background_mean',
        'background_at_centroid', 'xmin', 'xmax', 'ymin', 'ymax', 'min_value',
        'max_value', 'minval_xpos', 'minval_ypos', 'maxval_xpos',
        'maxval_ypos', 'area', 'equivalent_radius', 'perimeter',
        'semimajor_axis_sigma', 'semiminor_axis_sigma', 'eccentricity',
        'orientation', 'ellipticity', 'elongation', 'covar_sigx2',
        'covar_sigxy', 'covar_sigy2', 'cxx', 'cxy', 'cyy'
    ]

    table_columns = None
    if exclude_columns is not None:
        table_columns = [s for s in columns_all if s not in exclude_columns]
    if columns is not None:
        table_columns = np.atleast_1d(columns)
    if table_columns is None:
        table_columns = columns_all

    tbl = QTable()
    for column in table_columns:
        values = getattr(obj, column)

        if isinstance(obj, SourceProperties):
            values = np.atleast_1d(values)
            if isinstance(values[0], u.Quantity):
                # turn list of Quantities into a Quantity array
                values = u.Quantity(values)
            if isinstance(values[0], SkyCoord):  # pragma: no cover
                # turn list of SkyCoord into a SkyCoord array
                values = SkyCoord(values)

        if isinstance(obj, SourceCatalog) and values is None:
            values = [None] * len(obj)

        tbl[column] = values

    return tbl
Esempio n. 40
0
def cenA_model(): 

    cwd = os.getenv('LUVOIR_SIMTOOLS_DIR')

    #### Grab Cen A information from SIMBAD eventually the galaxy will be a user choice 
    cenA = Simbad.query_object('ngc5128')
    cenAdistance = 3.8 * u.Mpc  # Harris et al. (2010)
    cenACoords = SkyCoord(cenA['RA'][0], cenA['DEC'][0], unit=(u.hourangle, u.deg))

    imCenA = plt.imread(cwd+'/data/cenA-Fermi-noLabel.jpg')
    imCenA = imCenA[:,0:556,:]
    
    # Work out angle for FoV on the sky. The "scale" is based on a measurement with
    # a different image, so need to get that scale right (all the bits at the end).
    scale = 300.*u.kpc / (595*786./1007) # kpc/pixel
    FoV = np.array(np.shape(imCenA)[0:-1])
    FoV = FoV[[1,0]]
    FoV_kpc = FoV * scale
    angle = np.rad2deg(np.arctan(FoV_kpc / cenAdistance.to('kpc'))  )
    area = angle[0]*angle[1]                  # x * y
    
    dr7qso = QTable.read(cwd+'/data/dr7_qso_galex.fits')
    zem = dr7qso['Z'][0]
    fuv = dr7qso['FUV_MAG'][0]
    nuv = dr7qso['NUV_MAG'][0]
    
    # these limiting mags correspond to the apertures Howk used 
    limitingmags = [18.0,19.2,20.1,21.0,21.8]
    numQSOs = np.zeros_like(limitingmags)
    for j in np.arange(np.size(limitingmags)):
        num = ((fuv > 10) & (fuv < limitingmags[j]))
        num = num.sum()
        numQSOs[j] += num
        
    numQSOs = np.array(numQSOs)/np.max(numQSOs) # Make this a relative number:
    qso_density_ref = 5./u.deg**2   # Rough QSO density = 5 for 15-m
    num_qsos = qso_density_ref * numQSOs[j] * area
    xqso=np.random.randint(0,FoV[0],size=num_qsos)
    yqso=np.random.randint(0,FoV[1],size=num_qsos)
    print 'numQSOs', numQSOs  
    # need to randomly generate mags too. . . . 
    mags = 3.8 * np.random.rand(num_qsos) + 18. 
  
    return xqso, yqso, mags 
Esempio n. 41
0
    def as_fits_table(self, line: QTable) -> QTable:
        """ 
        Writes out the results for an emission line within a multiple line fit 
        for which there is no spectral coverage
        Input:
            self: emission line fit parameters
            line: lab properties of the fitted emission line
        Return: 
            Fits table with all the lab and measured properties of a particular
            emission line
        """
        t = QTable()
        # Vacuum/laboratory properties of the line
        t = hstack([t, line])

        t["z"] = Column([self.z],
                        dtype="f",
                        description="Source redshift, from specpro")

        # Line fits
        # Continuum around line
        t["cont"] = Column(
            [self.continuum.value.value],
            dtype="f",
            unit=self.continuum.value.unit,
            description="Continuum around line, Gaussian fit",
        )
        t["cont_err"] = Column(
            [self.continuum.error.value],
            dtype="f",
            unit=self.continuum.error.unit,
            description="Error on continuum around line, Gaussian fit",
        )
        # Detection flag
        t["detected"] = Column([False],
                               dtype="bool",
                               description="Detected or nondetected line")

        # Coverage flag
        t["covered"] = Column([False],
                              dtype="bool",
                              description="Does the spectrum cover the line?")

        return t
Esempio n. 42
0
    def make_catalog_table(self):
        spatial_pixel_scale = self.get_spatial_pixel_scale(self.img_path)

        catalog = []
        with open(self.target_file_path) as f:
            for line in f:
                if '#' in line:
                    continue
                line = line.replace("\n", "")
                row = line.split(" ")
                if len(row) != 3 and len(row) != 4:
                    raise Exception("Incorrect target catalog file format.")
                for i, r in enumerate(row[1:]):
                    i += 1
                    row[i] = float(r)
                row.append(spatial_pixel_scale)
                row.append(self.cutout_x_size)  #cutout_x_size
                row.append(self.cutout_y_size)  #cutout_y_size
                catalog.append(row)

        if len(catalog[0]) == 7:
            colNames = [
                "id", "ra", "dec", "slit_pa", "spatial_pixel_scale",
                "cutout_x_size", "cutout_y_size"
            ]
        elif len(catalog[0]) == 6:
            colNames = [
                "id", "ra", "dec", "spatial_pixel_scale", "cutout_x_size",
                "cutout_y_size"
            ]
        else:
            raise Exception("Catalog generation unsuccessful.")

        t = QTable(rows=catalog, names=colNames)
        t["ra"].unit = u.deg
        t["dec"].unit = u.deg
        t["spatial_pixel_scale"].unit = (u.arcsec / u.pix)
        t["cutout_x_size"].unit = u.arcsec
        t["cutout_y_size"].unit = u.arcsec

        if len(row[0]) == 7:
            t["slit_pa"].unit = u.deg

        return t
Esempio n. 43
0
    def _parse_result(self, response, verbose=False):
        """
        internal wrapper to parse queries
        """

        if self._get_raw_response:
            return response.text

        # intercept error messages
        response_txt = response.text.split('\n')[2:-1]
        if len(response_txt) < 3 and len(response_txt[-1].split('|')) < 21:
            raise RuntimeError(response_txt[-1])

        names = response_txt[0].replace('# ', '').strip().split(' | ')
        results = ascii.read(response_txt[1:],
                             delimiter='|',
                             names=names,
                             fast_reader=False)
        results = QTable(results)

        # convert coordinates to degrees
        coo = SkyCoord(ra=results['RA(h)'],
                       dec=results['DE(deg)'],
                       unit=(u.hourangle, u.deg),
                       frame='icrs')
        results['RA(h)'] = coo.ra.deg
        results['DE(deg)'] = coo.dec.deg

        colnames = results.columns[:]
        for fieldname in colnames:
            # apply field name change
            results.rename_column(fieldname, conf.field_names[fieldname])
            # apply unit, if available
            if conf.field_names[fieldname] in conf.field_units:
                results[conf.field_names[fieldname]].unit = conf.field_units[
                    conf.field_names[fieldname]]

        # convert object numbers to int
        # unnumbered asteroids return as non numeric values ('-')
        # this is treated as defaulting to 0, and masking the entry
        unnumbered_mask = [not str(x).isdigit() for x in results['Number']]
        numbers = [
            int(x) if str(x).isdigit() else 0 for x in results['Number']
        ]
        asteroid_number_col = MaskedColumn(numbers,
                                           name='Number',
                                           mask=unnumbered_mask)

        results.replace_column('Number', asteroid_number_col)

        return results
Esempio n. 44
0
def build_table(filename):
    data_array = np.genfromtxt(filename)

    ## Read from columns of data arrays
    Lx = data_array[:,0]
    Ly = data_array[:,1]
    Lz = data_array[:,2]
    r  = data_array[:,3]

    ## Get more quantities
    L = np.sqrt(Lx*Lx + Ly*Ly + Lz*Lz)
    lx = Lx/L
    ly = Ly/L
    lz = Lz/L
    rho = L*r**(-1./2.)
    tilt = np.arcsin(np.sqrt(lx*lx + ly*ly))*180.0/np.pi
    prec = np.arctan2(ly,lx)*180.0/np.pi

    ## Derivative quantitiess need to know how grid is spaced (linear or log)
    if ((r[1] - r[0]) - (r[2] - r[1]) < 1e-14):
        dr = r[2:] - r[:-2]
        print "Assuming linearly spaced grid..."
    elif (((np.log10(r)[1] - np.log10(r)[0]) - (np.log10(r)[2] - np.log10(r)[1]))  < 1e-14):
        dr = r[1:-1]*np.log(10)*(np.log10(r)[2:] - np.log10(r)[:-2])
        print "Assuming log10 spaced grid..."
    else:
        print "Error: Something is wrong with the log check! Exiting!"
        return

    ## Need to fill guard cells for derivative quantities
    # Build warp amplitude Psi
    psi = np.zeros(len(L))
    psi_x = (0.5*r[1:-1]/dr)*(lx[2:]-lx[:-2])
    psi_y = (0.5*r[1:-1]/dr)*(ly[2:]-ly[:-2])
    psi_z = (0.5*r[1:-1]/dr)*(lz[2:]-lz[:-2])
    psi[1:-1] = np.sqrt(psi_x**2.0 + psi_y**2.0 + psi_z**2.0)
    psi[0] = psi[1]
    psi[-1] = psi[-2]

    ## Compile tables
    table_list   = [Lx,Ly,Lz,r,L,lx,ly,lz,rho,tilt,prec,psi]
    table_titles = ["Lx","Ly","Lz","r","L","lx","ly","lz","rho","tilt","prec","psi"]
    return QTable(data=table_list,names=table_titles)
Esempio n. 45
0
def loadScenarios(filename=None):
    '''
    Load various observing scenarios
    '''
    if filename is None:
        fname = path + '/Inputs/ETC/Scenarios.csv'
    else:
        fname = filename
    scens = pd.read_csv(fname, skip_blank_lines=True)
    scenarios = QTable.from_pandas(scens)

    # two indexes
    scenarios.add_index('Scenario')
    scenarios.add_index('Coronagraph')
    scenarios['Center lam'].unit = u.nm
    scenarios['t integ, hrs'].unit = u.h
    scenarios['Years at L2'].unit = u.year
    scenarios['Ref  Dmag'].unit = u.mag
    return scenarios
Esempio n. 46
0
 def build_table(self):
     """Generate an astropy QTable out of the abs lines
     Returns
     -------
     comp_tbl : QTable
     """
     if len(self._abslines) == 0:
         return
     comp_tbl = QTable()
     comp_tbl.add_column(
         Column([iline.wrest.to(u.AA).value
                 for iline in self._abslines] * u.AA,
                name='wrest'))
     comp_tbl.add_column(
         Column([iline.z for iline in self._abslines], name='z'))
     for attrib in ['flag_N', 'logN', 'sig_logN']:
         comp_tbl.add_column(
             Column([iline.attrib[attrib] for iline in self._abslines],
                    name=attrib))
     # Return
     return comp_tbl
Esempio n. 47
0
    def __init__(self, version=None, verbose=True):
        # Set version
        if version is None:
            self._version = 'DR7'
        else:
            self._version = version
        self.verbose = verbose
        # Paths
        if os.getenv('SDSSPATH') is None:
            print('SDSS_QUASAR: This code requires an SDSS database. Discuss with JXP')
            return
        self._path = os.getenv('SDSSPATH')+'/'+self._version+'_QSO/' 
        self._datdir = self._path+'spectro/1d_26/'

        # Grab Summary FITS file
        self._summf = self._path+self._version.lower()+'_qso.fits.gz'
        if self.verbose:
            print('SDSS_QUASAR: Using summary file {:s}'.format(self._summf))
        self._data = QTable.read(self._summf)
Esempio n. 48
0
def prepare_catalog(cat,
                    size,
                    band,
                    ra='ra',
                    dec='dec',
                    name=None,
                    unit='arcsec',
                    prefix=None,
                    output='./'):
    """Get the input catalog ready."""
    # RA, Dec: convert to standard column names
    ra_arr, dec_arr = cat[ra], cat[dec]

    # Name of the output file
    if prefix is None:
        prefix = DATA_ROOT.split('/')[-1]
    prefix = prefix.lower()

    if name is None:
        name_arr = ra_dec_prefix(ra_arr, dec_arr, band, prefix, output=output)
    else:
        name_arr = [
            os.path.join(
                output, "{:s}_{:s}_{:s}".format(prefix,
                                                str(name).strip(),
                                                band.lower()))
            for name in cat[name]
        ]

    # Radius of the cutout
    if is_number(size):
        # Using the same size for all objects
        size_arr = np.full(len(cat), float(size))
    else:
        size_arr = cat[size]

    # Add size unit if necessary
    if unit != 'pixel':
        size_arr = [s * u.Unit(unit) for s in size_arr]

    return QTable([name_arr, list(ra_arr),
                   list(dec_arr), size_arr],
                  names=('prefix', 'ra', 'dec', 'radius'))
Esempio n. 49
0
    def __init__(self,
                 n_levels=None,
                 energy_unit=None,
                 level_name='j',
                 *args,
                 **kwargs):
        """
        Constructor
        """
        super(EnergyLevelsOnDegreeOfFreedom, self).__init__(*args, **kwargs)

        table_type = {
            level_name: numpy.zeros(n_levels, 'i4'),
            'g': numpy.zeros(n_levels, 'i4'),
            'label': numpy.zeros(n_levels, 'i4'),
            'E': numpy.zeros(n_levels, 'f8') * energy_unit
        }
        self.data = QTable(dict(**table_type))
        """the astropy table that holds the levels data"""
Esempio n. 50
0
def test_easplomparser(tmpdir):
    f = tmpdir.mkdir('ion_sources').join('be_2.easplom')
    f.write(
        """  4  2  1  3  1 7.551e-01 8.649e+00 1.700e+00 9.785e-02 1.007e-01 1.147e-01 1.415e-01 1.749e-01
-1
%file:  be_2.easplom
%excitation autoionization cross section parameter file
 derived from fits to experimental and theoretical data
Dere, K. P., 2007, A&A, 466, 771
ADS ref:  http://adsabs.harvard.edu/abs/2007A%26A...466..771D
 created for CHIANTI database for astrophysical spectroscopy
  created by Ken Dere  (GMU)  Fri Jan 26 12:40:26 2007
-1""")
    table = QTable(
        data=[[1], [3], [1], [0.7551], [8.649], [1.7],
              [np.array([0.09785, 0.1007, 0.1147, 0.1415, 0.1749])]],
        names=EasplomParser.headings,
        meta={
            'element':
            'be',
            'ion':
            'be_2',
            'filename':
            'be_2.easplom',
            'footer':
            "file:  be_2.easplom\nexcitation autoionization cross section parameter file\nderived from fits to experimental and theoretical data\nDere, K. P., 2007, A&A, 466, 771\nADS ref:  http://adsabs.harvard.edu/abs/2007A%26A...466..771D\ncreated for CHIANTI database for astrophysical spectroscopy\ncreated by Ken Dere  (GMU)  Fri Jan 26 12:40:26 2007"
        })
    for h, unit in zip(EasplomParser.headings, EasplomParser.units):
        table[h].unit = unit
    p = EasplomParser('be_2.easplom',
                      full_path=os.path.join(f.dirname, f.basename))
    table_parsed = p.parse()
    for c in table.colnames:
        for row, row_parsed in zip(table[c], table_parsed[c]):
            test = row == row_parsed
            if isinstance(test, np.ndarray):
                assert np.all(test)
            else:
                assert test
    for k in ('footer', 'filename', 'element', 'ion'):
        assert table_parsed.meta[k] == table.meta[k]
    assert 'chianti_version' in table_parsed.meta
    assert 'descriptions' in table_parsed.meta
Esempio n. 51
0
def test_creation_multi():
    """ test the creation of DataClass objects from dicts or arrays;
    multiple rows"""

    ground_truth = QTable([[1, 2, 3], [4, 5, 6], ['a', 'b', 'c']],
                          names=('a', 'b', 'c'))

    test_dict = DataClass.from_dict(
        [OrderedDict((('a', 1), ('b', 4), ('c', 'a'))),
         OrderedDict((('a', 2), ('b', 5), ('c', 'b)'))),
         OrderedDict((('a', 3), ('b', 6), ('c', 'c')))])
    assert all(test_dict.table == ground_truth)

    test_array = DataClass.from_array([[1, 2, 3], [4, 5, 6],
                                       ['a', 'b', 'c']],
                                      names=('a', 'b', 'c'))
    assert all(test_array.table == ground_truth)

    test_table = DataClass.from_table(ground_truth)
    assert all(test_table.table == ground_truth)

    # test reading data from file
    ra = [10.223423, 10.233453, 10.243452]
    dec = [-12.42123, -12.41562, -12.40435]
    epoch = 2451523.5 + array([0.1234, 0.2345, 0.3525])
    file_ground_truth = DataClass.from_array(
        [ra, dec, epoch], names=['ra', 'dec', 't'])

    test_file = DataClass.from_file(data_path('test.txt'),
                                    format='ascii')

    assert all(file_ground_truth.table == test_file.table)

    # test failing if columns have different lengths
    with pytest.raises(ValueError):
        test_dict = DataClass.from_dict(
            OrderedDict([(('a', 1), ('b', 4), ('c', 'a')),
                         (('a', 2), ('b', 5), ('c', 'b)')),
                         (('a', 3), ('b', 6), ('c', 7))]))

    with pytest.raises(ValueError):
        test_array = DataClass.from_array([[1, 2, 3], [4, 5, 6], ['a', 'b']],
                                          names=('a', 'b', 'c'))
Esempio n. 52
0
def write_angular_resolutions(outfile, e_bins, res, overwrite=False, append=True):
    """

    Parameters
    ----------
    outfile: str
    e_bins: `numpy.ndarray` or `astropy.Quantity`
    res: `np.ndarray`
    overwrite
    append
    """
    e_bins_t = QTable(data=e_bins[..., np.newaxis], names=['energy_bins'])

    data = res
    names = ['angular_res', 'angular_res_err_lo', 'angular_res_err_hi']

    res_t = Table(data=data, names=names)
    write_table_hdf5(e_bins_t, outfile, path='bins', overwrite=overwrite, append=append, serialize_meta=True)
    write_table_hdf5(res_t, outfile, path='res', append=True)
Esempio n. 53
0
def test_insert_row(mixin_cols):
    """
    Test inserting a row, which only works for BaseColumn and Quantity
    """
    t = QTable(mixin_cols)
    t['m'].info.description = 'd'
    if isinstance(t['m'], (u.Quantity, Column, time.Time)):
        t.insert_row(1, t[-1])
        assert t[1] == t[-1]
        assert t['m'].info.description == 'd'
    else:
        with pytest.raises(ValueError) as exc:
            t.insert_row(1, t[-1])
        assert "Unable to insert row" in str(exc.value)
Esempio n. 54
0
def read_summary_table(file: str) -> QTable:
    """Read summary table to be in Astropy format.

    Parameters
    ----------
    file: str
        file to read with QTable

    Returns
    -------
    df : QTable

    """
    # read table, in Table format for better editing access
    df = Table.read(file, format="ascii.commented_header")
    df.add_index("Name")  # index by name

    # units dictionary
    units = {
        "ra": u.deg,
        "dec": u.deg,
        "dist": u.kpc,
        "vlos": u.km / u.s,
        "vloserr": u.km / u.s,
        "sigma": u.km / u.s,
        "rmax": u.arcmin,
        "pmra": u.mas / u.yr,
        "pmdec": u.mas / u.yr,
        "pmra_e": u.mas / u.yr,
        "pmdec_e": u.mas / u.yr,
        "rscale": u.arcmin,
        "pmdisp": u.mas / u.yr,
        "pmscale": u.mas / u.yr,
        "pmscale_e": u.mas / u.yr,
    }

    # assign units
    for name, unit in units.items():
        if name in df.columns:  # needed b/c creating columns
            df[name].unit = unit

    return QTable(df)
Esempio n. 55
0
def convert_qe_curve(filename):
    """Convert a single QE curve from its native FITS format to an
    an `astropy.table.QTable` representation.

    Parameters
    ----------
    filename : `str`
        Full path, including filename for the file to be converted.

    Returns
    -------
    table : `astropy.table.QTable`
        A QTable object containing the columns that describe this
        QE curve.

    Notes
    -----
    This function is specific to how the ts8 data are formatted
    with a curve per amp.  If ther are other formats, a different
    converter will be necessary.
    """
    with fits.open(filename) as hdu_list:
        # qe data is in first extension
        data = hdu_list[1].data
    wlength = []
    eff = dict()
    for row in data:
        wlength.append(row['WAVELENGTH'])
        for i in range(16):  # There are 16 amps
            col_name = f'AMP{i+1:02d}'
            eff.setdefault(amp_name_map[col_name], []).append(row[col_name])
    out_data = {'amp_name': [], 'wavelength': [], 'efficiency': []}
    for k in eff:
        amp_names = [k] * len(wlength)
        out_data['amp_name'] += amp_names
        out_data['wavelength'] += wlength
        out_data['efficiency'] += eff[k]

    out_data['amp_name'] = numpy.array(out_data['amp_name'])
    out_data['wavelength'] = numpy.array(out_data['wavelength']) * u.nanometer
    out_data['efficiency'] = numpy.array(out_data['efficiency']) * u.percent
    return QTable(out_data)
Esempio n. 56
0
def test_create_histogram_table():
    '''Test create histogram table'''
    from pyirf.binning import create_histogram_table

    events = QTable({
        'reco_energy': [1, 1, 10, 100, 100, 100] * u.TeV,
    })
    bins = [0.5, 5, 50, 500] * u.TeV

    # test without weights
    hist = create_histogram_table(events, bins, key='reco_energy')
    assert np.all(hist['n'] == [2, 1, 3])
    assert np.all(hist['n_weighted'] == [2, 1, 3])

    # test with weights
    events['weight'] = [0.5, 2, 2.5, 0.5, 0.2, 0.3]
    hist = create_histogram_table(events, bins, key='reco_energy')
    assert np.all(hist['n'] == [2, 1, 3])
    assert np.all(hist['n_weighted'] == [2.5, 2.5, 1.0])

    # test with particle_types
    types = np.array(['gamma', 'electron', 'gamma', 'electron', 'gamma', 'proton'])
    events['particle_type'] = types
    hist = create_histogram_table(events, bins, key='reco_energy')

    assert np.all(hist['n'] == [2, 1, 3])
    assert np.all(hist['n_weighted'] == [2.5, 2.5, 1.0])

    assert np.all(hist['n_gamma'] == [1, 1, 1])
    assert np.all(hist['n_electron'] == [1, 0, 1])
    assert np.all(hist['n_proton'] == [0, 0, 1])

    assert np.allclose(hist['n_gamma_weighted'], [0.5, 2.5, 0.2])
    assert np.allclose(hist['n_electron_weighted'], [2, 0, 0.5])
    assert np.allclose(hist['n_proton_weighted'], [0, 0, 0.3])

    # test with empty table
    empty = events[:0]
    hist = create_histogram_table(empty, bins, key='reco_energy')
    zeros = np.zeros(3)
    assert np.all(hist['n'] == zeros)
    assert np.all(hist['n_weighted'] == zeros)
Esempio n. 57
0
def T1(request):
    T = QTable.read([
        ' a b c d',
        ' 2 c 7.0 0',
        ' 2 b 5.0 1',
        ' 2 b 6.0 2',
        ' 2 a 4.0 3',
        ' 0 a 0.0 4',
        ' 1 b 3.0 5',
        ' 1 a 2.0 6',
        ' 1 a 1.0 7',
    ],
                    format='ascii')
    T['q'] = np.arange(len(T)) * u.m
    T.meta.update({'ta': 1})
    T['c'].meta.update({'a': 1})
    T['c'].description = 'column c'
    if request.param:
        T.add_index('a')
    return T
Esempio n. 58
0
def _load_nbody() -> TableType:
    """Get Palomar 5 at pericenter Nbody Data.

    TODO move to ZENODO and download to cache instead.

    Returns
    -------
    data : `~astropy.table.QTable`

        File in "data/00590_peri.dat"

    """
    fname: str = get_pkg_data_filename(
        os.path.join("data", "00590_peri.dat"),
        package="trackstream",
    )

    data: TableType = QTable.read(fname, format="ascii.ecsv")

    return data
Esempio n. 59
0
    def test_table_write(self, serialize_method, tmpdir):
        name = str(tmpdir.join("test.ecsv"))
        self.t.write(name, serialize_method=serialize_method)
        with open(name) as fh:
            lines = fh.readlines()

        t2 = QTable.read(name)
        assert isinstance(t2['ma'], self.ma.__class__)

        if serialize_method == 'data_mask':
            # Will data_mask, we have data and mask columns and should
            # exactly round-trip.
            assert len(lines[-1].split()) == 2
            assert_masked_equal(t2['ma'], self.ma)
        else:
            # With null_value we have just a data column with null values
            # marked, so we lost information on the data below the mask.
            assert len(lines[-1].split()) == 1
            assert np.all(t2['ma'] == self.ma)
            assert np.all(t2['ma'].mask == self.mask_a)
Esempio n. 60
0
def get_mcmc_samples(filename):

    smp = Table.read(filename,
                     format='ascii',
                     names=[
                         'd1', 'd2', 'lb_blue', 'c0_blue', 'sc_blue', 'lb_red',
                         'c0_red', 'sc_red', 'kappa_blue', 'kappa_red',
                         'lb_PN', 'kappa_PN', 'M/L', 'logL'
                     ])
    smp['rho_s'] = 10.**smp['d2'] * u.Msun / u.pc**3
    smp['r_s'] = 10.**((smp['d1'] - 2. * smp['d2']) / 3.) * u.pc
    smp['M/L'] *= u.Msun / u.Lsun
    for col in ['red', 'blue']:
        smp['c0_{0}'.format(col)] *= u.mag
        smp['sc_{0}'.format(col)] *= u.mag
    for col in ['red', 'blue', 'PN']:
        smp['beta_z_{0}'.format(col)] = 1. - np.exp(-smp['lb_{0}'.format(col)])
    smp = QTable(smp)  # use Qtables instead of table

    return smp