Ejemplo n.º 1
0
def add_spex(input_path, database):
    """
    Function for adding the SpeX Prism Spectral Library to the database.

    Parameters
    ----------
    input_path : str
        Path of the data folder.
    database : h5py._hl.files.File
        The HDF5 database.

    Returns
    -------
    NoneType
        None
    """

    distance_url = 'https://people.phys.ethz.ch/~stolkert/species/distance.dat'
    distance_file = os.path.join(input_path, 'distance.dat')

    if not os.path.isfile(distance_file):
        urllib.request.urlretrieve(distance_url, distance_file)

    distance_data = pd.pandas.read_csv(
        distance_file,
        usecols=[0, 3, 4],
        names=['object', 'distance', 'distance_error'],
        delimiter=',',
        dtype={
            'object': str,
            'distance': float,
            'distance_error': float
        })

    database.create_group('spectra/spex')

    data_path = os.path.join(input_path, 'spex')

    if not os.path.exists(data_path):
        os.makedirs(data_path)

    url_all = 'http://svo2.cab.inta-csic.es/vocats/v2/spex/' \
              'cs.php?RA=180.000000&DEC=0.000000&SR=180.000000&VERB=2'

    xml_file_spex = os.path.join(data_path, 'spex.xml')

    if not os.path.isfile(xml_file_spex):
        urllib.request.urlretrieve(url_all, xml_file_spex)

    table = parse_single_table(xml_file_spex)
    # name = table.array['name']
    twomass = table.array['name2m']
    url = table.array['access_url']

    unique_id = []

    for i, item in enumerate(url):
        if twomass[i] not in unique_id:
            xml_file_1 = os.path.join(data_path,
                                      twomass[i].decode('utf-8') + '.xml')

            if not os.path.isfile(xml_file_1):
                urllib.request.urlretrieve(item.decode('utf-8'), xml_file_1)

            table = parse_single_table(xml_file_1)
            name = table.array['ID']
            name = name[0].decode('utf-8')
            url = table.array['access_url']

            print_message = f'Downloading SpeX Prism Spectral Library... {name}'
            print(f'\r{print_message:<72}', end='')

            xml_file_2 = os.path.join(data_path, f'spex_{name}.xml')

            if not os.path.isfile(xml_file_2):
                urllib.request.urlretrieve(url[0].decode('utf-8'), xml_file_2)

            unique_id.append(twomass[i])

    print_message = 'Downloading SpeX Prism Spectral Library... [DONE]'
    print(f'\r{print_message:<72}')

    h_twomass = photometry.SyntheticPhotometry('2MASS/2MASS.H')

    # 2MASS H band zero point for 0 mag (Cogen et al. 2003)
    h_zp = 1.133e-9  # (W m-2 um-1)

    for votable in os.listdir(data_path):
        if votable.startswith('spex_') and votable.endswith('.xml'):
            xml_file = os.path.join(data_path, votable)

            table = parse_single_table(xml_file)

            wavelength = table.array['wavelength']  # (Angstrom)
            flux = table.array['flux']  # Normalized units

            wavelength = np.array(wavelength * 1e-4)  # (um)
            flux = np.array(flux)  # (a.u.)
            error = np.full(flux.size, np.nan)

            # 2MASS magnitudes
            j_mag = table.get_field_by_id('jmag').value
            h_mag = table.get_field_by_id('hmag').value
            ks_mag = table.get_field_by_id('ksmag').value

            j_mag = j_mag.decode('utf-8')
            h_mag = h_mag.decode('utf-8')
            ks_mag = ks_mag.decode('utf-8')

            if j_mag == '':
                j_mag = np.nan
            else:
                j_mag = float(j_mag)

            if h_mag == '':
                h_mag = np.nan
            else:
                h_mag = float(h_mag)

            if ks_mag == '':
                ks_mag = np.nan
            else:
                ks_mag = float(ks_mag)

            name = table.get_field_by_id('name').value
            name = name.decode('utf-8')

            twomass_id = table.get_field_by_id('name2m').value
            twomass_id = twomass_id.decode('utf-8')

            try:
                sptype = table.get_field_by_id('nirspty').value
                sptype = sptype.decode('utf-8')

            except KeyError:
                try:
                    sptype = table.get_field_by_id('optspty').value
                    sptype = sptype.decode('utf-8')

                except KeyError:
                    sptype = 'None'

            sptype = data_util.update_sptype(np.array([sptype]))[0].strip()

            h_flux, _ = h_twomass.magnitude_to_flux(h_mag,
                                                    error=None,
                                                    zp_flux=h_zp)
            phot = h_twomass.spectrum_to_flux(wavelength,
                                              flux)  # Normalized units

            flux *= h_flux / phot[0]  # (W m-2 um-1)

            spdata = np.vstack([wavelength, flux, error])

            # simbad_id, distance = query_util.get_distance(f'2MASS {twomass_id}')
            simbad_id = query_util.get_simbad(f'2MASS {twomass_id}')

            if simbad_id is not None:
                simbad_id = simbad_id.decode('utf-8')

                dist_select = distance_data.loc[distance_data['object'] ==
                                                simbad_id]

                if not dist_select.empty:
                    distance = (dist_select['distance'],
                                dist_select['distance_error'])
                else:
                    distance = (np.nan, np.nan)

            else:
                distance = (np.nan, np.nan)

            if sptype[0] in ['M', 'L', 'T'] and len(sptype) == 2:
                print_message = f'Adding SpeX Prism Spectral Library... {name}'
                print(f'\r{print_message:<72}', end='')

                dset = database.create_dataset(f'spectra/spex/{name}',
                                               data=spdata)

                dset.attrs['name'] = str(name).encode()
                dset.attrs['sptype'] = str(sptype).encode()
                dset.attrs['simbad'] = str(simbad_id).encode()
                dset.attrs['2MASS/2MASS.J'] = j_mag
                dset.attrs['2MASS/2MASS.H'] = h_mag
                dset.attrs['2MASS/2MASS.Ks'] = ks_mag
                dset.attrs['distance'] = distance[0]  # (pc)
                dset.attrs['distance_error'] = distance[1]  # (pc)

    print_message = 'Adding SpeX Prism Spectral Library... [DONE]'
    print(f'\r{print_message:<72}')

    database.close()
Ejemplo n.º 2
0
def add_vlm_plx(input_path, database):
    """
    Function for adding the Database of Ultracool
    Parallaxes to the database.

    Parameters
    ----------
    input_path : str
        Data folder.
    database : h5py._hl.files.File
        HDF5 database.

    Returns
    -------
    NoneType
        None
    """

    data_file = os.path.join(input_path, "vlm-plx-all.fits")

    url = ("http://www.as.utexas.edu/~tdupuy/plx/"
           "Database_of_Ultracool_Parallaxes_files/vlm-plx-all.fits")

    if not os.path.isfile(data_file):
        print(
            "Downloading Database of Ultracool Parallaxes (307 kB)...",
            end="",
            flush=True,
        )
        urllib.request.urlretrieve(url, data_file)
        print(" [DONE]")

    print("Adding Database of Ultracool Parallaxes...", end="", flush=True)

    database.create_group("photometry/vlm-plx")

    with fits.open(data_file) as hdu_list:
        phot_data = hdu_list[1].data

    parallax = phot_data["PLX"]  # (mas)
    parallax_error = phot_data["EPLX"]  # (mas)

    name = phot_data["NAME"]
    name = np.core.defchararray.strip(name)

    sptype = phot_data["OSPTSTR"]
    sptype = np.core.defchararray.strip(sptype)

    sptype_nir = phot_data["ISPTSTR"]
    sptype_nir = np.core.defchararray.strip(sptype_nir)

    for i, item in enumerate(sptype):
        if item == "null":
            sptype[i] = sptype_nir[i]

    flag = phot_data["FLAG"]
    flag = np.core.defchararray.strip(flag)

    sptype = data_util.update_sptype(sptype)

    dtype = h5py.special_dtype(vlen=str)

    dset = database.create_dataset("photometry/vlm-plx/name",
                                   (np.size(name), ),
                                   dtype=dtype)
    dset[...] = name

    dset = database.create_dataset("photometry/vlm-plx/sptype",
                                   (np.size(sptype), ),
                                   dtype=dtype)
    dset[...] = sptype

    dset = database.create_dataset("photometry/vlm-plx/flag",
                                   (np.size(flag), ),
                                   dtype=dtype)
    dset[...] = flag

    database.create_dataset("photometry/vlm-plx/ra",
                            data=phot_data["RA"])  # (deg)
    database.create_dataset("photometry/vlm-plx/dec",
                            data=phot_data["DEC"])  # (deg)
    database.create_dataset("photometry/vlm-plx/parallax", data=parallax)
    database.create_dataset("photometry/vlm-plx/parallax_error",
                            data=parallax_error)
    database.create_dataset("photometry/vlm-plx/MKO/NSFCam.Y",
                            data=phot_data["YMAG"])
    database.create_dataset("photometry/vlm-plx/MKO/NSFCam.J",
                            data=phot_data["JMAG"])
    database.create_dataset("photometry/vlm-plx/MKO/NSFCam.H",
                            data=phot_data["HMAG"])
    database.create_dataset("photometry/vlm-plx/MKO/NSFCam.K",
                            data=phot_data["KMAG"])
    database.create_dataset("photometry/vlm-plx/MKO/NSFCam.Lp",
                            data=phot_data["LMAG"])
    database.create_dataset("photometry/vlm-plx/MKO/NSFCam.Mp",
                            data=phot_data["MMAG"])
    database.create_dataset("photometry/vlm-plx/2MASS/2MASS.J",
                            data=phot_data["J2MAG"])
    database.create_dataset("photometry/vlm-plx/2MASS/2MASS.H",
                            data=phot_data["H2MAG"])
    database.create_dataset("photometry/vlm-plx/2MASS/2MASS.Ks",
                            data=phot_data["K2MAG"])

    print(" [DONE]")

    database.close()
Ejemplo n.º 3
0
def add_spex(input_path: str, database: h5py._hl.files.File) -> None:
    """
    Function for adding the SpeX Prism Spectral Library to the database.

    Parameters
    ----------
    input_path : str
        Path of the data folder.
    database : h5py._hl.files.File
        The HDF5 database.

    Returns
    -------
    NoneType
        None
    """

    parallax_url = "https://home.strw.leidenuniv.nl/~stolker/species/parallax.dat"
    parallax_file = os.path.join(input_path, "parallax.dat")

    if not os.path.isfile(parallax_file):
        urllib.request.urlretrieve(parallax_url, parallax_file)

    parallax_data = pd.pandas.read_csv(
        parallax_file,
        usecols=[0, 1, 2],
        names=["object", "parallax", "parallax_error"],
        delimiter=",",
        dtype={"object": str, "parallax": float, "parallax_error": float},
    )

    database.create_group("spectra/spex")

    data_path = os.path.join(input_path, "spex")

    if not os.path.exists(data_path):
        os.makedirs(data_path)

    url_all = "http://svo2.cab.inta-csic.es/vocats/v2/spex/cs.php?" \
              "RA=180.000000&DEC=0.000000&SR=180.000000&VERB=2"

    xml_file_spex = os.path.join(data_path, "spex.xml")

    if not os.path.isfile(xml_file_spex):
        urllib.request.urlretrieve(url_all, xml_file_spex)

    table = parse_single_table(xml_file_spex)
    # name = table.array['name']
    twomass = table.array["name2m"]
    url = table.array["access_url"]

    unique_id = []

    print_message = ""

    for i, item in enumerate(url):
        if twomass[i] not in unique_id:

            if isinstance(twomass[i], str):
                xml_file_1 = os.path.join(data_path, twomass[i] + ".xml")
            else:
                # Use decode for backward compatibility
                xml_file_1 = os.path.join(
                    data_path, twomass[i].decode("utf-8") + ".xml"
                )

            if not os.path.isfile(xml_file_1):
                if isinstance(item, str):
                    urllib.request.urlretrieve(item, xml_file_1)
                else:
                    urllib.request.urlretrieve(item.decode("utf-8"), xml_file_1)

            table = parse_single_table(xml_file_1)
            name = table.array["ID"]
            url = table.array["access_url"]

            if isinstance(name[0], str):
                name = name[0]
            else:
                name = name[0].decode("utf-8")

            empty_message = len(print_message) * " "
            print(f"\r{empty_message}", end="")

            print_message = f"Downloading SpeX Prism Spectral Library... {name}"
            print(f"\r{print_message}", end="")

            xml_file_2 = os.path.join(data_path, f"spex_{name}.xml")

            if not os.path.isfile(xml_file_2):
                if isinstance(url[0], str):
                    urllib.request.urlretrieve(url[0], xml_file_2)
                else:
                    urllib.request.urlretrieve(url[0].decode("utf-8"), xml_file_2)

            unique_id.append(twomass[i])

    empty_message = len(print_message) * " "
    print(f"\r{empty_message}", end="")

    print_message = "Downloading SpeX Prism Spectral Library... [DONE]"
    print(f"\r{print_message}")

    h_twomass = photometry.SyntheticPhotometry("2MASS/2MASS.H")

    # 2MASS H band zero point for 0 mag (Cogen et al. 2003)
    h_zp = 1.133e-9  # (W m-2 um-1)

    for votable in os.listdir(data_path):
        if votable.startswith("spex_") and votable.endswith(".xml"):
            xml_file = os.path.join(data_path, votable)

            table = parse_single_table(xml_file)

            wavelength = table.array["wavelength"]  # (Angstrom)
            flux = table.array["flux"]  # Normalized units

            wavelength = np.array(wavelength * 1e-4)  # (um)
            flux = np.array(flux)  # (a.u.)
            error = np.full(flux.size, np.nan)

            # 2MASS magnitudes
            j_mag = table.get_field_by_id("jmag").value
            h_mag = table.get_field_by_id("hmag").value
            ks_mag = table.get_field_by_id("ksmag").value

            if not isinstance(j_mag, str):
                j_mag = j_mag.decode("utf-8")

            if not isinstance(h_mag, str):
                h_mag = h_mag.decode("utf-8")

            if not isinstance(ks_mag, str):
                ks_mag = ks_mag.decode("utf-8")

            if j_mag == "":
                j_mag = np.nan
            else:
                j_mag = float(j_mag)

            if h_mag == "":
                h_mag = np.nan
            else:
                h_mag = float(h_mag)

            if ks_mag == "":
                ks_mag = np.nan
            else:
                ks_mag = float(ks_mag)

            name = table.get_field_by_id("name").value

            if not isinstance(name, str):
                name = name.decode("utf-8")

            twomass_id = table.get_field_by_id("name2m").value

            if not isinstance(twomass_id, str):
                twomass_id = twomass_id.decode("utf-8")

            # Optical spectral type

            try:
                sptype_opt = table.get_field_by_id("optspty").value

                if not isinstance(sptype_opt, str):
                    sptype_opt = sptype_opt.decode("utf-8")

                sptype_opt = data_util.update_sptype(np.array([sptype_opt]))[0]

            except KeyError:
                sptype_opt = None

            # Near-infrared spectral type

            try:
                sptype_nir = table.get_field_by_id("nirspty").value

                if not isinstance(sptype_nir, str):
                    sptype_nir = sptype_nir.decode("utf-8")

                sptype_nir = data_util.update_sptype(np.array([sptype_nir]))[0]

            except KeyError:
                sptype_nir = None

            h_flux, _ = h_twomass.magnitude_to_flux(h_mag, error=None, zp_flux=h_zp)
            phot = h_twomass.spectrum_to_flux(wavelength, flux)  # Normalized units

            flux *= h_flux / phot[0]  # (W m-2 um-1)

            spdata = np.column_stack([wavelength, flux, error])

            simbad_id = query_util.get_simbad(f"2MASS {twomass_id}")

            if simbad_id is not None:
                if not isinstance(simbad_id, str):
                    simbad_id = simbad_id.decode("utf-8")

                par_select = parallax_data[parallax_data["object"] == simbad_id]

                if not par_select.empty:
                    parallax = (
                        par_select["parallax"].values[0],
                        par_select["parallax_error"].values[0],
                    )

                else:
                    parallax = (np.nan, np.nan)

            else:
                parallax = (np.nan, np.nan)

            print_message = f"Adding spectra... {name}"
            print(f"\r{print_message:<72}", end="")

            dset = database.create_dataset(f"spectra/spex/{name}", data=spdata)

            dset.attrs["name"] = str(name).encode()

            if sptype_opt is not None:
                dset.attrs["sptype"] = str(sptype_opt).encode()
            elif sptype_nir is not None:
                dset.attrs["sptype"] = str(sptype_nir).encode()
            else:
                dset.attrs["sptype"] = str("None").encode()

            dset.attrs["simbad"] = str(simbad_id).encode()
            dset.attrs["2MASS/2MASS.J"] = j_mag
            dset.attrs["2MASS/2MASS.H"] = h_mag
            dset.attrs["2MASS/2MASS.Ks"] = ks_mag
            dset.attrs["parallax"] = parallax[0]  # (mas)
            dset.attrs["parallax_error"] = parallax[1]  # (mas)

    print_message = "Adding spectra... [DONE]"
    print(f"\r{print_message:<72}")

    database.close()
Ejemplo n.º 4
0
def add_vlm_plx(input_path,
                database):
    """
    Function for adding the Database of Ultracool Parallaxes to the database.

    Parameters
    ----------
    input_path : str
        Data folder.
    database : h5py._hl.files.File
        HDF5 database.

    Returns
    -------
    NoneType
        None
    """

    data_file = os.path.join(input_path, 'vlm-plx-all.fits')

    url = 'http://www.as.utexas.edu/~tdupuy/plx/' \
          'Database_of_Ultracool_Parallaxes_files/vlm-plx-all.fits'

    if not os.path.isfile(data_file):
        print('Downloading Database of Ultracool Parallaxes (307 kB)...', end='', flush=True)
        urllib.request.urlretrieve(url, data_file)
        print(' [DONE]')

    print('Adding Database of Ultracool Parallaxes...', end='', flush=True)

    database.create_group('photometry/vlm-plx')

    with fits.open(data_file) as hdu_list:
        phot_data = hdu_list[1].data

    parallax = phot_data['PLX']  # (mas)
    parallax_error = phot_data['EPLX']  # (mas)
    distance = 1./(parallax*1e-3)  # (pc)

    distance_minus = distance - 1./((parallax+parallax_error)*1e-3)  # (pc)
    distance_plus = 1./((parallax-parallax_error)*1e-3) - distance  # (pc)
    distance_error = (distance_plus+distance_minus)/2.  # (pc)

    name = phot_data['NAME']
    name = np.core.defchararray.strip(name)

    sptype = phot_data['ISPTSTR']
    sptype = np.core.defchararray.strip(sptype)

    sptype_op = phot_data['OSPTSTR']
    sptype_op = np.core.defchararray.strip(sptype_op)

    for i, item in enumerate(sptype):
        if item == 'null':
            sptype[i] = sptype_op[i]

    flag = phot_data['FLAG']
    flag = np.core.defchararray.strip(flag)

    sptype = data_util.update_sptype(sptype)

    dtype = h5py.special_dtype(vlen=str)

    dset = database.create_dataset('photometry/vlm-plx/name', (np.size(name), ), dtype=dtype)
    dset[...] = name

    dset = database.create_dataset('photometry/vlm-plx/sptype', (np.size(sptype), ), dtype=dtype)
    dset[...] = sptype

    dset = database.create_dataset('photometry/vlm-plx/flag', (np.size(flag), ), dtype=dtype)
    dset[...] = flag

    database.create_dataset('photometry/vlm-plx/ra', data=phot_data['RA'])  # (deg)
    database.create_dataset('photometry/vlm-plx/dec', data=phot_data['DEC'])  # (deg)
    database.create_dataset('photometry/vlm-plx/parallax', data=parallax)
    database.create_dataset('photometry/vlm-plx/parallax_error', data=parallax_error)
    database.create_dataset('photometry/vlm-plx/distance', data=distance)
    database.create_dataset('photometry/vlm-plx/distance_error', data=distance_error)
    database.create_dataset('photometry/vlm-plx/MKO/NSFCam.Y', data=phot_data['YMAG'])
    database.create_dataset('photometry/vlm-plx/MKO/NSFCam.J', data=phot_data['JMAG'])
    database.create_dataset('photometry/vlm-plx/MKO/NSFCam.H', data=phot_data['HMAG'])
    database.create_dataset('photometry/vlm-plx/MKO/NSFCam.K', data=phot_data['KMAG'])
    database.create_dataset('photometry/vlm-plx/MKO/NSFCam.Lp', data=phot_data['LMAG'])
    database.create_dataset('photometry/vlm-plx/MKO/NSFCam.Mp', data=phot_data['MMAG'])
    database.create_dataset('photometry/vlm-plx/2MASS/2MASS.J', data=phot_data['J2MAG'])
    database.create_dataset('photometry/vlm-plx/2MASS/2MASS.H', data=phot_data['H2MAG'])
    database.create_dataset('photometry/vlm-plx/2MASS/2MASS.Ks', data=phot_data['K2MAG'])

    print(' [DONE]')

    database.close()
Ejemplo n.º 5
0
def add_irtf(input_path: str,
             database: h5py._hl.files.File,
             sptypes: Optional[List[str]] = None) -> None:
    """
    Function for adding the IRTF Spectral Library to the database.

    Parameters
    ----------
    input_path : str
        Path of the data folder.
    database : h5py._hl.files.File
        Database.
    sptypes : list(str, ), None
        List with the spectral types ('F', 'G', 'K', 'M', 'L', 'T'). All spectral types are
        included if set to ``None``.

    Returns
    -------
    NoneType
        None
    """

    if sptypes is None:
        sptypes = ['F', 'G', 'K', 'M', 'L', 'T']

    distance_url = 'https://people.phys.ethz.ch/~stolkert/species/distance.dat'
    distance_file = os.path.join(input_path, 'distance.dat')

    if not os.path.isfile(distance_file):
        urllib.request.urlretrieve(distance_url, distance_file)

    distance_data = pd.pandas.read_csv(distance_file,
                                       usecols=[0, 3, 4],
                                       names=['object', 'distance', 'distance_error'],
                                       delimiter=',',
                                       dtype={'object': str,
                                              'distance': float,
                                              'distance_error': float})

    datadir = os.path.join(input_path, 'irtf')

    if not os.path.exists(datadir):
        os.makedirs(datadir)

    data_file = {'F': os.path.join(input_path, 'irtf/F_fits_091201.tar'),
                 'G': os.path.join(input_path, 'irtf/G_fits_091201.tar'),
                 'K': os.path.join(input_path, 'irtf/K_fits_091201.tar'),
                 'M': os.path.join(input_path, 'irtf/M_fits_091201.tar'),
                 'L': os.path.join(input_path, 'irtf/L_fits_091201.tar'),
                 'T': os.path.join(input_path, 'irtf/T_fits_091201.tar')}

    data_folder = {'F': os.path.join(input_path, 'irtf/F_fits_091201'),
                   'G': os.path.join(input_path, 'irtf/G_fits_091201'),
                   'K': os.path.join(input_path, 'irtf/K_fits_091201'),
                   'M': os.path.join(input_path, 'irtf/M_fits_091201'),
                   'L': os.path.join(input_path, 'irtf/L_fits_091201'),
                   'T': os.path.join(input_path, 'irtf/T_fits_091201')}

    data_type = {'F': 'F stars (4.4 MB)',
                 'G': 'G stars (5.6 MB)',
                 'K': 'K stars (5.5 MB)',
                 'M': 'M stars (7.5 MB)',
                 'L': 'L dwarfs (850 kB)',
                 'T': 'T dwarfs (100 kB)'}

    url_root = 'http://irtfweb.ifa.hawaii.edu/~spex/IRTF_Spectral_Library/Data/'

    url = {'F': url_root+'F_fits_091201.tar',
           'G': url_root+'G_fits_091201.tar',
           'K': url_root+'K_fits_091201.tar',
           'M': url_root+'M_fits_091201.tar',
           'L': url_root+'L_fits_091201.tar',
           'T': url_root+'T_fits_091201.tar'}

    for item in sptypes:
        if not os.path.isfile(data_file[item]):
            print(f'Downloading IRTF Spectral Library - {data_type[item]}...', end='', flush=True)
            urllib.request.urlretrieve(url[item], data_file[item])
            print(' [DONE]')

    print('Unpacking IRTF Spectral Library...', end='', flush=True)

    for item in sptypes:
        tar = tarfile.open(data_file[item])
        tar.extractall(path=datadir)
        tar.close()

    print(' [DONE]')

    database.create_group('spectra/irtf')

    for item in sptypes:
        for root, _, files in os.walk(data_folder[item]):

            for _, filename in enumerate(files):
                if filename[-9:] != '_ext.fits':
                    fitsfile = os.path.join(root, filename)

                    spdata, header = fits.getdata(fitsfile, header=True)

                    name = header['OBJECT']
                    sptype = header['SPTYPE']

                    if name[-2:] == 'AB':
                        name = name[:-2]
                    elif name[-3:] == 'ABC':
                        name = name[:-3]

                    spt_split = sptype.split()

                    if item in ['L', 'T'] or spt_split[1][0] == 'V':
                        print_message = f'Adding IRTF Spectral Library... {name}'
                        print(f'\r{print_message:<70}', end='')

                        simbad_id = query_util.get_simbad(name)

                        if simbad_id is not None:
                            simbad_id = simbad_id.decode('utf-8')

                            dist_select = distance_data.loc[distance_data['object'] == simbad_id]

                            if not dist_select.empty:
                                distance = (dist_select['distance'], dist_select['distance_error'])
                            else:
                                simbad_id, distance = query_util.get_distance(name)

                        else:
                            distance = (np.nan, np.nan)

                        sptype = data_util.update_sptype(np.array([sptype]))[0]

                        dset = database.create_dataset(f'spectra/irtf/{name}',
                                                       data=spdata)

                        dset.attrs['name'] = str(name).encode()
                        dset.attrs['sptype'] = str(sptype).encode()
                        dset.attrs['simbad'] = str(simbad_id).encode()
                        dset.attrs['distance'] = distance[0]
                        dset.attrs['distance_error'] = distance[1]

    print_message = 'Adding IRTF Spectral Library... [DONE]'
    print(f'\r{print_message:<70}')

    database.close()
Ejemplo n.º 6
0
def add_vlm_plx(input_path,
                database):
    """
    Function for adding the Database of Ultracool Parallaxes to the database.

    Parameters
    ----------
    input_path : str
    database : h5py._hl.files.File

    Returns
    -------
    NoneType
        None
    """

    data_file = os.path.join(input_path, 'vlm-plx-all.fits')

    url = 'http://www.as.utexas.edu/~tdupuy/plx/' \
          'Database_of_Ultracool_Parallaxes_files/vlm-plx-all.fits'

    if not os.path.isfile(data_file):
        sys.stdout.write('Downloading Database of Ultracool Parallaxes (307 kB)...')
        sys.stdout.flush()

        urlretrieve(url, data_file)

        sys.stdout.write(' [DONE]\n')
        sys.stdout.flush()

    sys.stdout.write('Adding Database of Ultracool Parallaxes...')
    sys.stdout.flush()

    group = 'photometry/vlm-plx'

    database.create_group(group)

    hdulist = fits.open(data_file)
    photdata = hdulist[1].data

    plx = photdata['PLX']  # [mas]
    distance = 1./(plx*1e-3)  # [pc]

    name = photdata['NAME']
    name = np.core.defchararray.strip(name)

    sptype = photdata['ISPTSTR']
    sptype = np.core.defchararray.strip(sptype)

    sptype_op = photdata['OSPTSTR']
    sptype_op = np.core.defchararray.strip(sptype_op)

    for i, item in enumerate(sptype):
        if item == 'null':
            sptype[i] = sptype_op[i]

    flag = photdata['FLAG']
    flag = np.core.defchararray.strip(flag)

    sptype = data_util.update_sptype(sptype)

    dtype = h5py.special_dtype(vlen=bytes)

    dset = database.create_dataset(group+'/name', (np.size(name), ), dtype=dtype)
    dset[...] = name

    dset = database.create_dataset(group+'/sptype', (np.size(sptype), ), dtype=dtype)
    dset[...] = sptype

    dset = database.create_dataset(group+'/flag', (np.size(flag), ), dtype=dtype)
    dset[...] = flag

    database.create_dataset(group+'/distance', data=distance, dtype='f')
    database.create_dataset(group+'/MKO/NSFCam.Y', data=photdata['YMAG'], dtype='f')
    database.create_dataset(group+'/MKO/NSFCam.J', data=photdata['JMAG'], dtype='f')
    database.create_dataset(group+'/MKO/NSFCam.H', data=photdata['HMAG'], dtype='f')
    database.create_dataset(group+'/MKO/NSFCam.K', data=photdata['KMAG'], dtype='f')
    database.create_dataset(group+'/MKO/NSFCam.Lp', data=photdata['LMAG'], dtype='f')
    database.create_dataset(group+'/MKO/NSFCam.Mp', data=photdata['MMAG'], dtype='f')
    database.create_dataset(group+'/2MASS/2MASS.J', data=photdata['J2MAG'], dtype='f')
    database.create_dataset(group+'/2MASS/2MASS.H', data=photdata['H2MAG'], dtype='f')
    database.create_dataset(group+'/2MASS/2MASS.Ks', data=photdata['K2MAG'], dtype='f')

    sys.stdout.write(' [DONE]\n')
    sys.stdout.flush()

    sys.stdout.write('Querying SIMBAD...')
    sys.stdout.flush()

    simbad_id = queries.get_simbad(name)

    dset = database.create_dataset(group+'/simbad', (np.size(simbad_id), ), dtype=dtype)
    dset[...] = simbad_id

    sys.stdout.write(' [DONE]\n')
    sys.stdout.flush()

    database.close()
Ejemplo n.º 7
0
def add_irtf(input_path, database, sptypes):
    """
    Function to add the IRTF Spectral Library to the database.

    Parameters
    ----------
    input_path : str
        Path of the data folder.
    database : h5py._hl.files.File
        Database.
    sptypes : tuple(str, )
        Spectral types ('F', 'G', 'K', 'M', 'L', 'T').

    Returns
    -------
    NoneType
        None
    """

    datadir = os.path.join(input_path, 'irtf')

    if not os.path.exists(datadir):
        os.makedirs(datadir)

    data_file = {
        'F': os.path.join(input_path, 'irtf/F_fits_091201.tar'),
        'G': os.path.join(input_path, 'irtf/G_fits_091201.tar'),
        'K': os.path.join(input_path, 'irtf/K_fits_091201.tar'),
        'M': os.path.join(input_path, 'irtf/M_fits_091201.tar'),
        'L': os.path.join(input_path, 'irtf/L_fits_091201.tar'),
        'T': os.path.join(input_path, 'irtf/T_fits_091201.tar')
    }

    data_folder = {
        'F': os.path.join(input_path, 'irtf/F_fits_091201'),
        'G': os.path.join(input_path, 'irtf/G_fits_091201'),
        'K': os.path.join(input_path, 'irtf/K_fits_091201'),
        'M': os.path.join(input_path, 'irtf/M_fits_091201'),
        'L': os.path.join(input_path, 'irtf/L_fits_091201'),
        'T': os.path.join(input_path, 'irtf/T_fits_091201')
    }

    data_type = {
        'F': 'F stars (4.4 MB)',
        'G': 'G stars (5.6 MB)',
        'K': 'K stars (5.5 MB)',
        'M': 'M stars (7.5 MB)',
        'L': 'L dwarfs (850 kB)',
        'T': 'T dwarfs (100 kB)'
    }

    url_root = 'http://irtfweb.ifa.hawaii.edu/~spex/IRTF_Spectral_Library/Data/'

    url = {
        'F': url_root + 'F_fits_091201.tar',
        'G': url_root + 'G_fits_091201.tar',
        'K': url_root + 'K_fits_091201.tar',
        'M': url_root + 'M_fits_091201.tar',
        'L': url_root + 'L_fits_091201.tar',
        'T': url_root + 'T_fits_091201.tar'
    }

    for item in sptypes:
        if not os.path.isfile(data_file[item]):
            sys.stdout.write('Downloading IRTF Spectral Library - ' +
                             data_type[item] + '...')
            sys.stdout.flush()

            urlretrieve(url[item], data_file[item])

            sys.stdout.write(' [DONE]\n')
            sys.stdout.flush()

    sys.stdout.write('Unpacking IRTF Spectral Library...')
    sys.stdout.flush()

    for item in sptypes:
        tar = tarfile.open(data_file[item])
        tar.extractall(path=datadir)
        tar.close()

    sys.stdout.write(' [DONE]\n')
    sys.stdout.flush()

    database.create_group('spectra/irtf')

    for item in sptypes:
        for root, _, files in os.walk(data_folder[item]):

            for _, filename in enumerate(files):
                if filename[-9:] != '_ext.fits':
                    fitsfile = os.path.join(root, filename)
                    spdata, header = fits.getdata(fitsfile, header=True)

                    name = header['OBJECT']
                    sptype = header['SPTYPE']

                    if name[-2:] == 'AB':
                        name = name[:-2]
                    elif name[-3:] == 'ABC':
                        name = name[:-3]

                    spt_split = sptype.split()

                    if item in ('L', 'T') or spt_split[1][0] == 'V':
                        sys.stdout.write('\rAdding IRTF Spectral Library... ' +
                                         '{:<40}'.format(name))
                        sys.stdout.flush()

                        simbad_id, distance = queries.get_distance(
                            name)  # [pc]

                        sptype = data_util.update_sptype(np.array([sptype]))[0]

                        dset = database.create_dataset('spectra/irtf/' + name,
                                                       data=spdata,
                                                       dtype='f')

                        dset.attrs['name'] = str(name).encode()
                        dset.attrs['sptype'] = str(sptype).encode()
                        dset.attrs['simbad'] = str(simbad_id).encode()
                        dset.attrs['distance'] = distance

    sys.stdout.write('\rAdding IRTF Spectral Library... ' +
                     '{:<40}'.format('[DONE]') + '\n')
    sys.stdout.flush()

    database.close()
Ejemplo n.º 8
0
def add_spex(input_path, database):
    """
    Function for adding the SpeX Prism Spectral Library to the database.

    Parameters
    ----------
    input_path : str
        Path of the data folder.
    database : h5py._hl.files.File
        Database.

    Returns
    -------
    NoneType
        None
    """

    database.create_group('spectra/spex')

    data_path = os.path.join(input_path, 'spex')

    if not os.path.exists(data_path):
        os.makedirs(data_path)

    url_all = 'http://svo2.cab.inta-csic.es/vocats/v2/spex/' \
              'cs.php?RA=180.000000&DEC=0.000000&SR=180.000000&VERB=2'

    xml_file = os.path.join(data_path, 'spex.xml')

    urlretrieve(url_all, xml_file)

    table = parse_single_table(xml_file)
    name = table.array['name']
    twomass = table.array['name2m']
    url = table.array['access_url']

    os.remove(xml_file)

    for i, item in enumerate(url):
        xml_file = os.path.join(data_path, twomass[i].decode('utf-8') + '.xml')
        urlretrieve(item.decode('utf-8'), xml_file)

        table = parse_single_table(xml_file)
        name = table.array['ID']
        name = name[0].decode('utf-8')
        url = table.array['access_url']

        sys.stdout.write('\rDownloading SpeX Prism Spectral Library... ' +
                         '{:<40}'.format(name))
        sys.stdout.flush()

        os.remove(xml_file)

        xml_file = os.path.join(data_path, name + '.xml')
        urlretrieve(url[0].decode('utf-8'), xml_file)

    sys.stdout.write('\rDownloading SpeX Prism Spectral Library... ' +
                     '{:<40}'.format('[DONE]') + '\n')
    sys.stdout.flush()

    h_twomass = photometry.SyntheticPhotometry('2MASS/2MASS.H')

    transmission = read_filter.ReadFilter('2MASS/2MASS.H')
    transmission.get_filter()

    # 2MASS H band zero point for 0 mag (Cogen et al. 2003)
    h_zp = 1.133e-9  # [W m-2 micron-1]

    for votable in os.listdir(data_path):
        if votable.endswith('.xml'):
            xml_file = os.path.join(data_path, votable)

            table = parse_single_table(xml_file)

            wavelength = table.array['wavelength']  # [Angstrom]
            flux = table.array['flux']  # Normalized units

            wavelength = np.array(wavelength * 1e-4)  # [micron]
            flux = np.array(flux)

            # 2MASS magnitudes
            j_mag = table.get_field_by_id('jmag').value
            h_mag = table.get_field_by_id('hmag').value
            ks_mag = table.get_field_by_id('ksmag').value

            if j_mag == b'':
                j_mag = np.nan
            else:
                j_mag = float(j_mag)

            if h_mag == b'':
                h_mag = np.nan
            else:
                h_mag = float(h_mag)

            if ks_mag == b'':
                ks_mag = np.nan
            else:
                ks_mag = float(ks_mag)

            name = table.get_field_by_id('name').value
            name = name.decode('utf-8')
            twomass_id = table.get_field_by_id('name2m').value

            sys.stdout.write('\rAdding SpeX Prism Spectral Library... ' +
                             '{:<40}'.format(name))
            sys.stdout.flush()

            try:
                sptype = table.get_field_by_id('nirspty').value
                sptype = sptype.decode('utf-8')

            except KeyError:
                try:
                    sptype = table.get_field_by_id('optspty').value
                    sptype = sptype.decode('utf-8')

                except KeyError:
                    sptype = 'None'

            sptype = data_util.update_sptype(np.array([sptype]))[0]

            h_flux, _ = h_twomass.magnitude_to_flux(h_mag, None, h_zp)
            phot = h_twomass.spectrum_to_photometry(wavelength,
                                                    flux)  # Normalized units

            flux *= h_flux / phot  # [W m-2 micron-1]

            spdata = np.vstack((wavelength, flux))

            simbad_id, distance = queries.get_distance(
                '2MASS ' + twomass_id.decode('utf-8'))  # [pc]

            dset = database.create_dataset('spectra/spex/' + name, data=spdata)

            dset.attrs['name'] = str(name)
            dset.attrs['sptype'] = str(sptype)
            dset.attrs['simbad'] = str(simbad_id)
            dset.attrs['2MASS/2MASS.J'] = j_mag
            dset.attrs['2MASS/2MASS.H'] = h_mag
            dset.attrs['2MASS/2MASS.Ks'] = ks_mag
            dset.attrs['distance'] = distance  # [pc]

    sys.stdout.write('\rAdding SpeX Prism Spectral Library... ' +
                     '{:<40}'.format('[DONE]') + '\n')
    sys.stdout.flush()

    database.close()
Ejemplo n.º 9
0
def add_leggett(input_path, database):
    """
    Function for adding the Database of Ultracool Parallaxes to the database.

    Parameters
    ----------
    input_path : str
        Path of the data folder.
    database : h5py._hl.files.File
        Database.

    Returns
    -------
    NoneType
        None
    """

    data_file1 = os.path.join(input_path, '2010_phot.xls')
    url1 = 'http://staff.gemini.edu/~sleggett/2010_phot.xls'

    data_file2 = os.path.join(input_path, 'datafile8.txt')
    url2 = 'http://staff.gemini.edu/~sleggett/datafile8.txt'

    if not os.path.isfile(data_file1):
        print('Downloading Leggett L and T Dwarf Data (88 kB)...',
              end='',
              flush=True)
        urllib.request.urlretrieve(url1, data_file1)
        print(' [DONE]')

    if not os.path.isfile(data_file2):
        print('Downloading Leggett T6+ and Y Dwarf Data (44 kB)...',
              end='',
              flush=True)
        urllib.request.urlretrieve(url2, data_file2)
        print(' [DONE]')

    print('Adding Leggett L and T Dwarf Data...', end='', flush=True)

    group = 'photometry/leggett'

    database.create_group(group)

    dataframe = pd.pandas.read_excel(data_file1)
    dataframe.columns = dataframe.columns.str.replace('\'', '')

    modulus = np.asarray(dataframe['M-m'])  # M-m (mag)
    modulus_error = np.asarray(dataframe['Mmerr'])  # M-m (mag)

    distance = 10.**(-modulus / 5. + 1.)  # (pc)
    distance_lower = distance - 10.**(-(modulus + modulus_error) / 5. + 1.
                                      )  # (pc)
    distance_upper = 10.**(-(modulus - modulus_error) / 5. +
                           1.) - distance  # (pc)
    distance_error = (distance_lower + distance_upper) / 2.

    name = np.asarray(dataframe['Name'])

    sptype = np.asarray(dataframe['Type'])
    sptype = data_util.update_sptype(sptype)

    mag_y = np.asarray(dataframe['Y'])
    mag_j = np.asarray(dataframe['J'])
    mag_h = np.asarray(dataframe['H'])
    mag_k = np.asarray(dataframe['K'])
    mag_lp = np.asarray(dataframe['L'])
    mag_mp = np.asarray(dataframe['M'])
    mag_ch1 = np.asarray(dataframe['Ch1'])
    mag_ch2 = np.asarray(dataframe['Ch2'])
    mag_ch3 = np.asarray(dataframe['Ch3'])
    mag_ch4 = np.asarray(dataframe['Ch4'])
    mag_w1 = np.repeat(np.nan, np.size(name))
    mag_w2 = np.repeat(np.nan, np.size(name))
    mag_w3 = np.repeat(np.nan, np.size(name))
    mag_w4 = np.repeat(np.nan, np.size(name))

    print(' [DONE]')
    print('Adding Leggett T6+ and Y Dwarf Data...', end='', flush=True)

    file_io = open(data_file2, 'r')
    lines = file_io.readlines()[69:]

    for item in lines:
        name = np.append(name, item[0:16])

        spt_tmp = item[62:66]
        if spt_tmp[0] == '2':
            spt_tmp = 'T' + spt_tmp[1]
        elif spt_tmp[0] == '3':
            spt_tmp = 'Y' + spt_tmp[1]

        sptype = np.append(sptype, spt_tmp)

        modulus = float(item[67:73])  # M-m (mag)
        if modulus == 999.:
            modulus = np.nan

        distance = np.append(distance, 10.**(-modulus / 5. + 1.))  # (pc)

        mag = np.zeros(14)

        mag[0] = float(item[95:101])  # MKO Y
        mag[1] = float(item[102:107])  # MKO J
        mag[2] = float(item[108:114])  # MKO H
        mag[3] = float(item[115:121])  # MKO K
        mag[4] = float(item[122:128])  # MKO L'
        mag[5] = float(item[129:135])  # MKO M'
        mag[6] = float(item[136:142])  # Spitzer/IRAC 3.6 um
        mag[7] = float(item[143:149])  # Spitzer/IRAC 4.5 um
        mag[8] = float(item[150:156])  # Spitzer/IRAC 5.8 um
        mag[9] = float(item[157:163])  # Spitzer/IRAC 8.0 um
        mag[10] = float(item[164:170])  # WISE W1
        mag[11] = float(item[171:176])  # WISE W2
        mag[12] = float(item[177:183])  # WISE W3
        mag[13] = float(item[184:190])  # WISE W4

        for j, mag_item in enumerate(mag):
            if mag_item == 999.:
                mag[j] = np.nan

        mag_y = np.append(mag_y, mag[0])
        mag_j = np.append(mag_j, mag[1])
        mag_h = np.append(mag_h, mag[2])
        mag_k = np.append(mag_k, mag[3])
        mag_lp = np.append(mag_lp, mag[4])
        mag_mp = np.append(mag_mp, mag[5])
        mag_ch1 = np.append(mag_ch1, mag[6])
        mag_ch2 = np.append(mag_ch2, mag[7])
        mag_ch3 = np.append(mag_ch3, mag[8])
        mag_ch4 = np.append(mag_ch4, mag[9])
        mag_w1 = np.append(mag_w1, mag[10])
        mag_w2 = np.append(mag_w2, mag[11])
        mag_w3 = np.append(mag_w3, mag[12])
        mag_w4 = np.append(mag_w4, mag[13])

    file_io.close()

    dtype = h5py.special_dtype(vlen=str)

    dset = database.create_dataset(group + '/name', (np.size(name), ),
                                   dtype=dtype)
    dset[...] = name

    dset = database.create_dataset(group + '/sptype', (np.size(sptype), ),
                                   dtype=dtype)
    dset[...] = sptype

    flag = np.repeat('null', np.size(name))

    dset = database.create_dataset(group + '/flag', (np.size(flag), ),
                                   dtype=dtype)
    dset[...] = flag

    database.create_dataset(group + '/distance', data=distance)
    database.create_dataset(group + '/distance_error', data=distance_error)
    database.create_dataset(group + '/MKO/NSFCam.Y', data=mag_y)
    database.create_dataset(group + '/MKO/NSFCam.J', data=mag_j)
    database.create_dataset(group + '/MKO/NSFCam.H', data=mag_h)
    database.create_dataset(group + '/MKO/NSFCam.K', data=mag_k)
    database.create_dataset(group + '/MKO/NSFCam.Lp', data=mag_lp)
    database.create_dataset(group + '/MKO/NSFCam.Mp', data=mag_mp)
    database.create_dataset(group + '/Spitzer/IRAC.I1', data=mag_ch1)
    database.create_dataset(group + '/Spitzer/IRAC.I2', data=mag_ch2)
    database.create_dataset(group + '/Spitzer/IRAC.I3', data=mag_ch3)
    database.create_dataset(group + '/Spitzer/IRAC.I4', data=mag_ch4)
    database.create_dataset(group + '/WISE/WISE.W1', data=mag_w1)
    database.create_dataset(group + '/WISE/WISE.W2', data=mag_w2)
    database.create_dataset(group + '/WISE/WISE.W3', data=mag_w3)
    database.create_dataset(group + '/WISE/WISE.W4', data=mag_w4)

    print(' [DONE]')

    database.close()
Ejemplo n.º 10
0
def add_leggett(input_path, database):
    """
    Function for adding the Database of Ultracool Parallaxes to the database.

    Parameters
    ----------
    input_path : str
        Path of the data folder.
    database : h5py._hl.files.File
        Database.

    Returns
    -------
    NoneType
        None
    """

    data_file1 = os.path.join(input_path, "2010_phot.xls")
    url1 = "http://staff.gemini.edu/~sleggett/2010_phot.xls"

    data_file2 = os.path.join(input_path, "datafile8.txt")
    url2 = "http://staff.gemini.edu/~sleggett/datafile8.txt"

    if not os.path.isfile(data_file1):
        print("Downloading Leggett L and T Dwarf Data (88 kB)...",
              end="",
              flush=True)
        urllib.request.urlretrieve(url1, data_file1)
        print(" [DONE]")

    if not os.path.isfile(data_file2):
        print("Downloading Leggett T6+ and Y Dwarf Data (44 kB)...",
              end="",
              flush=True)
        urllib.request.urlretrieve(url2, data_file2)
        print(" [DONE]")

    print("Adding Leggett L and T Dwarf Data...", end="", flush=True)

    group = "photometry/leggett"

    database.create_group(group)

    dataframe = pd.pandas.read_excel(data_file1)
    dataframe.columns = dataframe.columns.str.replace("'", "")

    modulus = np.asarray(dataframe["M-m"])  # M-m (mag)
    modulus_error = np.asarray(dataframe["Mmerr"])  # M-m (mag)

    distance = 10.0**(-modulus / 5.0 + 1.0)  # (pc)
    distance_lower = distance - 10.0**(-(modulus + modulus_error) / 5.0 + 1.0
                                       )  # (pc)
    distance_upper = 10.0**(-(modulus - modulus_error) / 5.0 +
                            1.0) - distance  # (pc)
    distance_error = (distance_lower + distance_upper) / 2.0

    name = np.asarray(dataframe["Name"])

    # Near-infrared spectral type
    sptype = np.asarray(dataframe["Type"])
    sptype = data_util.update_sptype(sptype)
    sptype = np.asarray(sptype)

    mag_y = np.asarray(dataframe["Y"])
    mag_j = np.asarray(dataframe["J"])
    mag_h = np.asarray(dataframe["H"])
    mag_k = np.asarray(dataframe["K"])
    mag_lp = np.asarray(dataframe["L"])
    mag_mp = np.asarray(dataframe["M"])
    mag_ch1 = np.asarray(dataframe["Ch1"])
    mag_ch2 = np.asarray(dataframe["Ch2"])
    mag_ch3 = np.asarray(dataframe["Ch3"])
    mag_ch4 = np.asarray(dataframe["Ch4"])
    mag_w1 = np.repeat(np.nan, np.size(name))
    mag_w2 = np.repeat(np.nan, np.size(name))
    mag_w3 = np.repeat(np.nan, np.size(name))
    mag_w4 = np.repeat(np.nan, np.size(name))

    print(" [DONE]")
    print("Adding Leggett T6+ and Y Dwarf Data...", end="", flush=True)

    file_io = open(data_file2, "r")
    lines = file_io.readlines()[69:]

    for item in lines:
        name = np.append(name, item[0:16])

        spt_tmp = item[62:66]
        if spt_tmp[0] == "2":
            spt_tmp = "T" + spt_tmp[1]
        elif spt_tmp[0] == "3":
            spt_tmp = "Y" + spt_tmp[1]

        sptype = np.append(sptype, spt_tmp)

        modulus = float(item[67:73])  # M-m (mag)
        if modulus == 999.0:
            modulus = np.nan

        distance = np.append(distance, 10.0**(-modulus / 5.0 + 1.0))  # (pc)

        mag = np.zeros(14)

        mag[0] = float(item[95:101])  # MKO Y
        mag[1] = float(item[102:107])  # MKO J
        mag[2] = float(item[108:114])  # MKO H
        mag[3] = float(item[115:121])  # MKO K
        mag[4] = float(item[122:128])  # MKO L'
        mag[5] = float(item[129:135])  # MKO M'
        mag[6] = float(item[136:142])  # Spitzer/IRAC 3.6 um
        mag[7] = float(item[143:149])  # Spitzer/IRAC 4.5 um
        mag[8] = float(item[150:156])  # Spitzer/IRAC 5.8 um
        mag[9] = float(item[157:163])  # Spitzer/IRAC 8.0 um
        mag[10] = float(item[164:170])  # WISE W1
        mag[11] = float(item[171:176])  # WISE W2
        mag[12] = float(item[177:183])  # WISE W3
        mag[13] = float(item[184:190])  # WISE W4

        for j, mag_item in enumerate(mag):
            if mag_item == 999.0:
                mag[j] = np.nan

        mag_y = np.append(mag_y, mag[0])
        mag_j = np.append(mag_j, mag[1])
        mag_h = np.append(mag_h, mag[2])
        mag_k = np.append(mag_k, mag[3])
        mag_lp = np.append(mag_lp, mag[4])
        mag_mp = np.append(mag_mp, mag[5])
        mag_ch1 = np.append(mag_ch1, mag[6])
        mag_ch2 = np.append(mag_ch2, mag[7])
        mag_ch3 = np.append(mag_ch3, mag[8])
        mag_ch4 = np.append(mag_ch4, mag[9])
        mag_w1 = np.append(mag_w1, mag[10])
        mag_w2 = np.append(mag_w2, mag[11])
        mag_w3 = np.append(mag_w3, mag[12])
        mag_w4 = np.append(mag_w4, mag[13])

    file_io.close()

    dtype = h5py.special_dtype(vlen=str)

    dset = database.create_dataset(group + "/name", (np.size(name), ),
                                   dtype=dtype)
    dset[...] = name

    dset = database.create_dataset(group + "/sptype", (np.size(sptype), ),
                                   dtype=dtype)
    dset[...] = sptype

    flag = np.repeat("null", np.size(name))

    dset = database.create_dataset(group + "/flag", (np.size(flag), ),
                                   dtype=dtype)
    dset[...] = flag

    database.create_dataset(group + "/distance", data=distance)
    database.create_dataset(group + "/distance_error", data=distance_error)
    database.create_dataset(group + "/MKO/NSFCam.Y", data=mag_y)
    database.create_dataset(group + "/MKO/NSFCam.J", data=mag_j)
    database.create_dataset(group + "/MKO/NSFCam.H", data=mag_h)
    database.create_dataset(group + "/MKO/NSFCam.K", data=mag_k)
    database.create_dataset(group + "/MKO/NSFCam.Lp", data=mag_lp)
    database.create_dataset(group + "/MKO/NSFCam.Mp", data=mag_mp)
    database.create_dataset(group + "/Spitzer/IRAC.I1", data=mag_ch1)
    database.create_dataset(group + "/Spitzer/IRAC.I2", data=mag_ch2)
    database.create_dataset(group + "/Spitzer/IRAC.I3", data=mag_ch3)
    database.create_dataset(group + "/Spitzer/IRAC.I4", data=mag_ch4)
    database.create_dataset(group + "/WISE/WISE.W1", data=mag_w1)
    database.create_dataset(group + "/WISE/WISE.W2", data=mag_w2)
    database.create_dataset(group + "/WISE/WISE.W3", data=mag_w3)
    database.create_dataset(group + "/WISE/WISE.W4", data=mag_w4)

    print(" [DONE]")

    database.close()
Ejemplo n.º 11
0
def add_mamajek(input_path, database):
    """
    Function for adding "A Modern Mean Dwarf Stellar Color and Effective Temperature Sequence".
    http://www.pas.rochester.edu/~emamajek/EEM_dwarf_UBVIJHK_colors_Teff.txt

    Parameters
    ----------
    input_path : str
        Path of the data folder.
    database : h5py._hl.files.File
        Database.

    Returns
    -------
    NoneType
        None
    """

    data_file = os.path.join(input_path, 'EEM_dwarf_UBVIJHK_colors_Teff.txt')

    url = 'http://www.pas.rochester.edu/~emamajek/EEM_dwarf_UBVIJHK_colors_Teff.txt'

    if not os.path.isfile(data_file):
        sys.stdout.write(
            'Downloading Stellar Colors and Effective Temperatures (53 kB)...')
        sys.stdout.flush()

        urlretrieve(url, data_file)

        sys.stdout.write(' [DONE]\n')
        sys.stdout.flush()

    sys.stdout.write('Adding Stellar Colors and Effective Temperatures...')
    sys.stdout.flush()

    group = 'photometry/mamajek'

    database.create_group(group)

    dataframe = pd.read_csv(
        data_file,
        delimiter=r"\s+",
        nrows=124,
        header=22,
        na_values=['...', '....', '.....', ',..', '19.52:'],
        dtype={
            'SpT': str,
            'Teff': float,
            'logT': float,
            'BCv': float,
            'U-B': float,
            'Mv': float,
            'logL': float,
            'B-V': float,
            'Bt-Vt': float,
            'V-Rc': float,
            'V-Ic': float,
            'V-Ks': float,
            'J-H': float,
            'H-Ks': float,
            'Ks-W1': float,
            'W1-W2': float,
            'W1-W3': float,
            'W1-W4': float,
            'Msun': float,
            'logAge': str,
            'b-y': float,
            'M_J': float,
            'M_Ks': float,
            'Mbol': float,
            'i-z': float,
            'z-Y': float,
            'R_Rsun': float,
            'SpT': str,
            'G-V': float,
            'Bp-Rp': float,
            'M_G': float,
            'G-Rp': float
        })

    dataframe.columns = dataframe.columns.str.replace('#', '')

    sptype = np.asarray(dataframe['SpT'])
    sptype = data_util.update_sptype(sptype)

    flag = np.repeat('star', sptype.size)
    distance = np.repeat(10., sptype.size)  # [pc]

    v_mag = dataframe['Mv']  # [mag]
    b_mag = dataframe['B-V'] + dataframe['Mv']  # [mag]
    u_mag = dataframe['U-B'] + b_mag  # [mag]

    j_mag = dataframe['M_J']  # [mag]
    ks_mag = dataframe['M_Ks']  # [mag]
    h_mag = dataframe['H-Ks'] + ks_mag  # [mag]

    w1_mag = -dataframe['Ks-W1'] + ks_mag  # [mag]
    w2_mag = -dataframe['W1-W2'] + w1_mag  # [mag]
    w3_mag = -dataframe['W1-W3'] + w1_mag  # [mag]
    w4_mag = -dataframe['W1-W4'] + w1_mag  # [mag]

    dtype = h5py.special_dtype(vlen=bytes)

    dset = database.create_dataset(group + '/sptype', (np.size(sptype), ),
                                   dtype=dtype)
    dset[...] = sptype

    dset = database.create_dataset(group + '/flag', (np.size(flag), ),
                                   dtype=dtype)
    dset[...] = flag

    database.create_dataset(group + '/distance', data=distance, dtype='f')
    database.create_dataset(group + '/Teff', data=dataframe['Teff'], dtype='f')
    database.create_dataset(group + '/U', data=u_mag, dtype='f')
    database.create_dataset(group + '/B', data=b_mag, dtype='f')
    database.create_dataset(group + '/V', data=v_mag, dtype='f')
    database.create_dataset(group + '/J', data=j_mag, dtype='f')
    database.create_dataset(group + '/H', data=h_mag, dtype='f')
    database.create_dataset(group + '/Ks', data=ks_mag, dtype='f')
    database.create_dataset(group + '/W1', data=w1_mag, dtype='f')
    database.create_dataset(group + '/W2', data=w2_mag, dtype='f')
    database.create_dataset(group + '/W3', data=w3_mag, dtype='f')
    database.create_dataset(group + '/W4', data=w4_mag, dtype='f')

    sys.stdout.write(' [DONE]\n')
    sys.stdout.flush()

    database.close()
Ejemplo n.º 12
0
def add_irtf(input_path: str,
             database: h5py._hl.files.File,
             sptypes: Optional[List[str]] = None) -> None:
    """
    Function for adding the IRTF Spectral Library to the database.

    Parameters
    ----------
    input_path : str
        Path of the data folder.
    database : h5py._hl.files.File
        Database.
    sptypes : list(str), None
        List with the spectral types ('F', 'G', 'K', 'M', 'L', 'T'). All spectral types are
        included if set to ``None``.

    Returns
    -------
    NoneType
        None
    """

    if sptypes is None:
        sptypes = ["F", "G", "K", "M", "L", "T"]

    parallax_url = "https://home.strw.leidenuniv.nl/~stolker/species/parallax.dat"
    parallax_file = os.path.join(input_path, "parallax.dat")

    if not os.path.isfile(parallax_file):
        urllib.request.urlretrieve(parallax_url, parallax_file)

    parallax_data = pd.pandas.read_csv(
        parallax_file,
        usecols=[0, 1, 2],
        names=["object", "parallax", "parallax_error"],
        delimiter=",",
        dtype={
            "object": str,
            "parallax": float,
            "parallax_error": float
        },
    )

    datadir = os.path.join(input_path, "irtf")

    if not os.path.exists(datadir):
        os.makedirs(datadir)

    data_file = {
        "F": os.path.join(input_path, "irtf/F_fits_091201.tar"),
        "G": os.path.join(input_path, "irtf/G_fits_091201.tar"),
        "K": os.path.join(input_path, "irtf/K_fits_091201.tar"),
        "M": os.path.join(input_path, "irtf/M_fits_091201.tar"),
        "L": os.path.join(input_path, "irtf/L_fits_091201.tar"),
        "T": os.path.join(input_path, "irtf/T_fits_091201.tar"),
    }

    data_folder = {
        "F": os.path.join(input_path, "irtf/F_fits_091201"),
        "G": os.path.join(input_path, "irtf/G_fits_091201"),
        "K": os.path.join(input_path, "irtf/K_fits_091201"),
        "M": os.path.join(input_path, "irtf/M_fits_091201"),
        "L": os.path.join(input_path, "irtf/L_fits_091201"),
        "T": os.path.join(input_path, "irtf/T_fits_091201"),
    }

    data_type = {
        "F": "F stars (4.4 MB)",
        "G": "G stars (5.6 MB)",
        "K": "K stars (5.5 MB)",
        "M": "M stars (7.5 MB)",
        "L": "L dwarfs (850 kB)",
        "T": "T dwarfs (100 kB)",
    }

    url_root = "http://irtfweb.ifa.hawaii.edu/~spex/IRTF_Spectral_Library/Data/"

    url = {
        "F": url_root + "F_fits_091201.tar",
        "G": url_root + "G_fits_091201.tar",
        "K": url_root + "K_fits_091201.tar",
        "M": url_root + "M_fits_091201.tar",
        "L": url_root + "L_fits_091201.tar",
        "T": url_root + "T_fits_091201.tar",
    }

    for item in sptypes:
        if not os.path.isfile(data_file[item]):
            print(
                f"Downloading IRTF Spectral Library - {data_type[item]}...",
                end="",
                flush=True,
            )
            urllib.request.urlretrieve(url[item], data_file[item])
            print(" [DONE]")

    print("Unpacking IRTF Spectral Library...", end="", flush=True)

    for item in sptypes:
        tar = tarfile.open(data_file[item])
        tar.extractall(path=datadir)
        tar.close()

    print(" [DONE]")

    database.create_group("spectra/irtf")

    print_message = ""

    for item in sptypes:
        for root, _, files in os.walk(data_folder[item]):

            for _, filename in enumerate(files):
                if filename[-9:] != "_ext.fits":
                    fitsfile = os.path.join(root, filename)

                    spdata, header = fits.getdata(fitsfile, header=True)
                    spdata = np.transpose(spdata)

                    name = header["OBJECT"]
                    sptype = header["SPTYPE"]

                    if name[-2:] == "AB":
                        name = name[:-2]
                    elif name[-3:] == "ABC":
                        name = name[:-3]

                    spt_split = sptype.split()

                    if item in ["L", "T"] or spt_split[1][0] == "V":
                        empty_message = len(print_message) * " "
                        print(f"\r{empty_message}", end="")

                        print_message = f"Adding spectra... {name}"
                        print(f"\r{print_message}", end="")

                        simbad_id = query_util.get_simbad(name)

                        if simbad_id is not None:
                            # For backward compatibility
                            if not isinstance(simbad_id, str):
                                simbad_id = simbad_id.decode("utf-8")

                            par_select = parallax_data[parallax_data["object"]
                                                       == simbad_id]

                            if not par_select.empty:
                                parallax = (
                                    par_select["parallax"],
                                    par_select["parallax_error"],
                                )
                            else:
                                simbad_id, parallax = query_util.get_parallax(
                                    name)

                        else:
                            parallax = (np.nan, np.nan)

                        sptype = data_util.update_sptype(np.array([sptype]))[0]

                        dset = database.create_dataset(f"spectra/irtf/{name}",
                                                       data=spdata)

                        dset.attrs["name"] = str(name).encode()
                        dset.attrs["sptype"] = str(sptype).encode()
                        dset.attrs["simbad"] = str(simbad_id).encode()
                        dset.attrs["parallax"] = parallax[0]
                        dset.attrs["parallax_error"] = parallax[1]

    empty_message = len(print_message) * " "
    print(f"\r{empty_message}", end="")

    print_message = "Adding spectra... [DONE]"
    print(f"\r{print_message}")

    database.close()