|
| 1 | +URL = 'https://cdsarc.u-strasbg.fr/ftp/cats/I/239/tyc_main.dat' |
| 2 | + |
| 3 | +PANDAS_MESSAGE = """Skyfield needs Pandas to load the Tycho2 catalog |
| 4 | +
|
| 5 | +To load the Tycho2 star catalog, Skyfield needs the Pandas data |
| 6 | +analysis toolkit. Try installing it using your usual Python package |
| 7 | +installer, like "pip install pandas" or "conda install pandas". |
| 8 | +""" |
| 9 | + |
| 10 | +_COLUMN_NAMES = ( |
| 11 | + 'Catalog', 'TYC', 'Proxy', 'RAhms', 'DEdms', 'Vmag', '---', |
| 12 | + 'r_Vmag', 'RAdeg', 'DEdeg', 'AstroRef', 'Plx', 'pmRA', |
| 13 | + 'pmDE', 'e_RAdeg', 'e_DEdeg', 'e_Plx', 'e_pmRA', 'e_pmDE', 'DE:RA', |
| 14 | + 'Plx:RA', 'Plx:DE', 'pmRA:RA', 'pmRA:DE', 'pmRA:Plx', 'pmDE:RA', |
| 15 | + 'pmDE:DE', 'pmDE:Plx', 'pmDE:pmRA', 'Nastro', 'F2', 'HIP', 'BTmag', |
| 16 | + 'e_BTmag', 'VTmag', 'e_VTmag', 'r_BTmag', 'B-V', 'e_B-V', '---(2)', |
| 17 | + 'Q', 'Fs', 'Source', 'Nphoto', 'VTscat', 'VTmax', 'VTmin', 'Var', |
| 18 | + 'VarFlag', 'MultFlag', 'morePhoto', 'm_HIP', 'PPM', 'HD', 'BD', 'CoD', |
| 19 | + 'CPD', 'Remark', |
| 20 | +) |
| 21 | + |
| 22 | +def load_dataframe(fobj): |
| 23 | + """Given an open file for ``tyc_main.dat``, return a parsed dataframe. |
| 24 | +
|
| 25 | + If the file is gzipped, it will be automatically uncompressed. |
| 26 | +
|
| 27 | + """ |
| 28 | + try: |
| 29 | + from pandas import read_csv |
| 30 | + except ImportError: |
| 31 | + raise ImportError(PANDAS_MESSAGE) |
| 32 | + |
| 33 | + fobj.seek(0) |
| 34 | + magic = fobj.read(2) |
| 35 | + compression = 'gzip' if (magic == b'\x1f\x8b') else None |
| 36 | + fobj.seek(0) |
| 37 | + |
| 38 | + df = read_csv( |
| 39 | + fobj, sep='|', names=_COLUMN_NAMES, compression=compression, |
| 40 | + usecols=['TYC', 'Vmag', 'RAdeg', 'DEdeg', 'Plx', 'pmRA', 'pmDE'], |
| 41 | + na_values=[' ', ' ', ' ', ' '], |
| 42 | + ) |
| 43 | + df.columns = ( |
| 44 | + 'tyc', 'magnitude', 'ra_degrees', 'dec_degrees', |
| 45 | + 'parallax_mas', 'ra_mas_per_year', 'dec_mas_per_year', |
| 46 | + ) |
| 47 | + df = df.assign( |
| 48 | + ra_hours = df['ra_degrees'] / 15.0, |
| 49 | + epoch_year = 1991.25, |
| 50 | + ) |
| 51 | + return df.set_index('tyc') |
0 commit comments