Exemple #1
0
def panstarrs_query(ra_deg, dec_deg, rad_deg, maxmag=20,
                    maxsources=10000):
    """
    Query PanSTARRS @ VizieR using astroquery.vizier
    :param ra_deg: RA in degrees
    :param dec_deg: Declination in degrees
    :param rad_deg: field radius in degrees
    :param maxmag: upper limit R magnitude (optional)
    :param maxsources: maximum number of sources
    :return: astropy.table object
    """
#    vquery = Vizier(columns=['objID', 'RAJ2000', 'DEJ2000',
#                             'e_RAJ2000', 'e_DEJ2000',
#                             'gmag', 'e_gmag',
#                             'rmag', 'e_rmag',
#                             'imag', 'e_imag',
#                             'zmag', 'e_zmag',
#                             'ymag', 'e_ymag'],
#                    column_filters={"rmag":
#                                    ("<%f" % maxmag)},
#                    row_limit=maxsources)
    vquery = Vizier(columns=['objID', 'RAJ2000', 'DEJ2000',
                             'e_RAJ2000', 'e_DEJ2000',
                             'gmag', 'e_gmag',
                             'rmag', 'e_rmag',
                             'imag', 'e_imag',
                             'zmag', 'e_zmag',
                             'ymag', 'e_ymag'],
                    column_filters={"rmag":
                                    ("<%f" % maxmag)},
                    row_limit=maxsources)

    field = coord.SkyCoord(ra=ra_deg, dec=dec_deg,
                           unit=(u.deg, u.deg),
                           frame='icrs')
    return vquery.query_region(field,
                               width=("%fd" % rad_deg),
                               catalog="II/349/ps1")[0]
Exemple #2
0
def _2MASS_query(ra_deg, dec_deg, rad_deg, maxmag=20, maxsources=-1):
    """
    Query 2MASS @ VizieR using astroquery.vizier
    parameters: ra_deg, dec_deg, rad_deg: RA, Dec, field
                                          radius in degrees
                maxmag: upper limit magnitude (optional)
                maxsources: maximum number of sources
    returns: astropy.table object
    """
    vquery = Vizier(
        columns=[
            '+_r', '2MASS', 'RAJ2000', 'DEJ2000', 'Jmag', 'e_Jmag', 'Jsnr',
            'Hmag', 'e_Hmag', 'Hsnr', 'Kmag', 'e_Kmag', 'Ksnr', 'dup', 'Ndet'
        ],
        #                column_filters={"gmag":
        #                                ("<%f" % maxmag)
        #                               "imag":
        #                                ("<%f" % maxmag)},
        row_limit=maxsources)
    field = SkyCoord(ra=ra_deg, dec=dec_deg, unit=(u.deg, u.deg), frame='icrs')
    return vquery.query_region(field,
                               width=("%fd" % rad_deg),
                               catalog="II/246")[0]
Exemple #3
0
def USNO_A2_query(ra_deg, dec_deg, rad_deg, maxmag=20,
               maxsources=-1):
    """
    Query USNO_A2 @ VizieR using astroquery.vizier
    parameters: ra_deg, dec_deg, rad_deg: RA, Dec, field
                                          radius in degrees
                maxmag: upper limit G magnitude (optional)
                maxsources: maximum number of sources
    returns: astropy.table object
    """
    vquery = Vizier(columns=['+_r', 'USNO-A2.0', 'RAJ2000', 'DEJ2000',
                             'Bmag','Rmag'],
    #                column_filters={"gmag":
    #                                ("<%f" % maxmag)
    #                               "imag":
    #                                ("<%f" % maxmag)},
                    row_limit = maxsources)
    field = SkyCoord(ra=ra_deg, dec=dec_deg,
                           unit=(u.deg, u.deg),
                           frame='icrs')
    return vquery.query_region(field,
                               width=("%fd" % rad_deg),
                               catalog="I/252/out")[0]
Exemple #4
0
def gaia_query(ra_deg, dec_deg, rad_deg, maxmag=18, maxsources=10000):
    """
    Query Gaia DR1 @ VizieR using astroquery.vizier
    :param ra_deg: RA in degrees
    :param dec_deg: Declination in degrees
    :param rad_deg: field radius in degrees
    :param maxmag: upper limit G magnitude (optional)
    :param maxsources: maximum number of sources
    :return: astropy.table object
    """
    vquery = Vizier(
        columns=['Source', 'RA_ICRS', 'DE_ICRS', 'Gmag', "Teff", "RPlx"],
        column_filters={"Gmag": ("<%f" % maxmag)},
        row_limit=maxsources)

    field = coord.SkyCoord(ra=ra_deg,
                           dec=dec_deg,
                           unit=(u.deg, u.deg),
                           frame='icrs')

    return vquery.query_region(field,
                               width=("%fd" % rad_deg),
                               catalog="I/345/gaia2")[0]
Exemple #5
0
def get_gaia(basePrefix="lris_targsNGC2506",
             groupInclude=[1]):
    """ Gets the Gaia info for a table 
    Parameters
    --------------
    basePrefix: str
        "lris_targsNGC2506" is the list of solar analog candidates
        "lris_alignmentNGC2506" is the list of alignment stars
    groupInclude: list
        List of integers describing the groups of stars to include
        These are priority levels on stars with 1 the highest
    """
    
    
    fullDat = ascii.read('../pan_starrs/pro/output/{}.csv'.format(basePrefix))
    pts = np.zeros(len(fullDat),dtype=np.bool)
    for oneGroup in groupInclude:
        pts = pts | (fullDat['GROUP'] == oneGroup)
    
    dat = fullDat[pts]
    
    coor = SkyCoord(dat['RA'],dat['DEC'],unit=(u.deg,u.deg))
    
    ## Set up Vizier Query
    vquery = Vizier(row_limit = 1)
    outList = vquery.query_region(coor,radius=1*u.arcsec,catalog="I/345/gaia2")
    outDat = outList[0]
    
    for oneColumn in dat.colnames:
        outDat["{}_PS".format(oneColumn)] = dat[oneColumn]
    
    outDat['diffRA'] = (outDat['RA_ICRS'] - outDat['RA_PS']) * 3600.
    outDat['diffDEC'] = (outDat['DE_ICRS'] - outDat['DEC_PS']) * 3600.
    
    for oneFormat in ['fits','csv']:
        outDat.write('lists/gaia_coord/gaia_{}.{}'.format(basePrefix,oneFormat),
                overwrite=True)
Exemple #6
0
def search_star(**kwargs):
    """ Searches position on VizieR and returns a catalogue.

    Parameters:
        coord (str, SkyCoord): Coordinate to perform the search.
        code (str): Gaia Source_id of the star
        columns (list): List of strings with the name of the columns to retrieve.
        radius (int, float, unit.quantity): Radius to search around coordinate
        catalog (str): VizieR catalogue to search.

    Returns:
        catalogue(astropy.Table): An astropy Table with the catalogue informations.
    """
    input_tests.check_kwargs(kwargs,
                             allowed_kwargs=[
                                 'catalog', 'code', 'columns', 'coord', 'log',
                                 'radius'
                             ])
    row_limit = 100
    if 'log' in kwargs and kwargs['log']:
        print('\nDownloading star parameters from {}'.format(
            kwargs['catalog']))
    vquery = Vizier(columns=kwargs['columns'],
                    row_limit=row_limit,
                    timeout=600)
    if 'code' in kwargs:
        catalogue = vquery.query_constraints(catalog=kwargs['catalog'],
                                             Source=kwargs['code'],
                                             cache=False)
    elif 'coord' in kwargs:
        catalogue = vquery.query_region(kwargs['coord'],
                                        radius=kwargs['radius'],
                                        catalog=kwargs['catalog'],
                                        cache=False)
    else:
        raise ValueError('At least a code or coord should be given as input')
    return catalogue
Exemple #7
0
def download_vizier(pointing,
                    radius,
                    catalog='UCAC4',
                    band='i',
                    maglimit=None):
    catalogs = {'UCAC4': 'I/322A', 'Gaia': 'I/345/gaia2'}
    if catalog not in catalogs.keys():
        print(f'{catalog} not in {catalogs.keys()}')
        raise NotImplementedError
    if band not in ['r', 'i']:
        print(f'Band {band} not supported')
        raise NotImplementedError

    columns = {
        'UCAC4': ['_RAJ2000', '_DEJ2000', 'rmag', 'imag'],
        'Gaia': ['RA_ICRS', 'DE_ICRS', 'Gmag', 'RPmag']
    }
    ra_colname = {'UCAC4': '_RAJ2000', 'Gaia': 'RA_ICRS'}
    dec_colname = {'UCAC4': '_DEJ2000', 'Gaia': 'DE_ICRS'}
    mag_colname = {'UCAC4': f'{band}mag', 'Gaia': 'RPmag'}
    filter_string = '>0' if maglimit is None else f"<{maglimit}"
    column_filter = {mag_colname[catalog]: filter_string}

    v = Vizier(columns=columns[catalog], column_filters=column_filter)
    v.ROW_LIMIT = 2e4

    try:
        stars = Table(
            v.query_region(pointing,
                           catalog=catalogs[catalog],
                           radius=c.Angle(radius, "deg"))[0])
        stars.add_column(Column(data=stars[ra_colname[catalog]], name='RA'))
        stars.add_column(Column(data=stars[dec_colname[catalog]], name='DEC'))
        stars.add_column(Column(data=stars[mag_colname[catalog]], name='mag'))
    except:
        stars = None
    return stars
Exemple #8
0
 def query_EPIC(self, ID=None, radius = 10.0*u.arcsec):
     import warnings
     from astropy.utils.metadata import MergeConflictWarning
     warnings.filterwarnings("ignore", category = MergeConflictWarning)
     
     if ID is None:
         ID = self.IDs['EPIC']
             
     tbl = Table(names = ('ID',), dtype = (int,))
     
     for i, id in tqdm(enumerate(ID)):
         id = id.replace('EPIC ','')
         if not isinstance(id, str):
             add_empty_row(tbl)
         else:
             
             v = Vizier(column_filters={"ID":f"=={id}", 'OType':'STAR'})
             job = v.get_catalogs('IV/34/epic')
             
             ridx = job[0]['ID'].quantity == int(id)
             
             if len(job[0][ridx]) > 0:
                 tbl = avstack([tbl, job[0][ridx][0]])  
             else:
                 add_empty_row(tbl)
     
     self.EPIC = tbl
     
     # Fill in blank IDs where possible
     if not hasattr(self,'simbad'):
         self.query_simbad(ID)        
     for i in range(len(self.IDs)): 
         if len(self.IDs['2MASS'][i])==0:
             self.IDs['2MASS'][i] = '2MASS J'+self.EPIC['_2MASS'][i]
     self.query_simbad(self.IDs['2MASS'])
     
     return self.EPIC
Exemple #9
0
    def make_jmmc_query(self, mag_range=(1, 6), ra_range=(30, 100), dec_range=(30, 100)):
        """
        Query the JMMC catalog using VizieR
        :param mag_range: the magnitude range you wish to constrain the query by
        :param ra_range: the RA range you wish to constrain the query by
        :param dec_range: The DEC range you wish to constrain the query by
        :return: appends results to the catalogs results array
        """
        columns = ['RAJ2000','DEJ2000','2MASS','Tessmag','Teff','R*','M*','logg','Dis','Gmag','Vmag','Bmag']
        v = Vizier()
        v.ROW_LIMIT = -1
        print("Retrieving Catalogue")
        local_dat = [d for d in os.listdir() if '.dat' in d]

        result = v.query_constraints(catalog="II/346/jsdc_v2",
                                     Bmag='>%s & <%s' %(mag_range[0], mag_range[1]),
                                     RAJ2000=">%s & <%s"%(ra_range[0], ra_range[1]),
                                     DEJ2000='>%s & <%s'%(dec_range[0], dec_range[1]))

        if result:
            good_val = np.where(~np.isnan(result[0]['Dis']))
            self.jmmc = result[0][good_val]
            self.catalogs.append(self.jmmc)
            self.cat_names.append("JMMC")
Exemple #10
0
    def make_charm2_query(self, mag_range=(1, 6), ra_range=(30, 100), dec_range=(30, 100)):
        """
        Query the CHARM2 catalog using VizieR
        :param mag_range: the magnitude range you wish to constrain the query by
        :param ra_range: the RA range you wish to constrain the query by
        :param dec_range: The DEC range you wish to constrain the query by
        :return: appends results to the catalogs results array
        """
        columns = ['N', 'Type','Id1', 'Method', 'Lambda', 'UD', 'e_UD', 'LD', 'e_LD', 'RAJ2000', 'DEJ2000', 'Vmag', 'Kmag', 'Bmag']
        v = Vizier(columns=columns)
        v.ROW_LIMIT = -1
        print("Retrieving Catalogue")
        local_dat = [d for d in os.listdir() if '.dat' in d]

        result = v.query_constraints(catalog="J/A+A/431/773",
                                     Bmag='>%s & <%s' %(mag_range[0], mag_range[1]),
                                     RAJ2000=">%s & <%s"%(ra_range[0], ra_range[1]),
                                     DEJ2000='>%s & <%s'%(dec_range[0], dec_range[1]))

        if result:
            good_val = np.where(~np.isnan(result[0]['UD']))
            self.charm2 = result[0][good_val]
            self.catalogs.append(self.charm2)
            self.cat_names.append("CHARM2")
Exemple #11
0
    def make_gaia_query(self, mag_range=(1, 6), ra_range=(30, 100), dec_range=(30, 100)):
        '''
        Query gaia's data release 2 (DR2) using VizieR
        :param mag_range: the magnitude range you wish to constrain the query by
        :param ra_range: the RA range you wish to constrain the query by
        :param dec_range: The DEC range you wish to constrain the query by
        :return: appends results to the catalogs results array
        '''
        columns = ['N', 'RAJ2000','DEJ2000','Gmag','BPmag','RPmag','Teff','Rad','Lum','Plx']
        v = Vizier(columns=columns)
        v.ROW_LIMIT = -1
        print("Retrieving Catalogue")
        Vizier.query_constraints_async()
        result = v.query_constraints(catalog="I/345/gaia2",
                                     BPmag='>%s & <%s' %(mag_range[0], mag_range[1]),
                                     RAJ2000=">%s & <%s"%(ra_range[0], ra_range[1]),
                                     DEJ2000='>%s & <%s'%(dec_range[0], dec_range[1]))

        asdf=123
        if result:
            good_vals = np.where(~np.isnan(result[0]['Rad']))
            self.gaia = result[0][good_vals]
            self.catalogs.append(self.gaia)
            self.cat_names.append("GAIA")
Exemple #12
0
    def __query_circle(self, ra, dec, radius, catalog):
        """Vizier.query_region in a circle FoV  using astroquery module."""

        radius_str = str(radius) + 'd'

        catalog_constraints = Vizier(catalog=[self.catalog],
                                     columns=[
                                         '*', '_RAJ2000', '_DEJ2000',
                                         self.column_1, self.column_2
                                     ],
                                     column_filters={
                                         self.column_1: self.filter_1,
                                         self.column_2: self.filter_2
                                     })

        catalog_constraints.ROW_LIMIT = -1  # completed catalog
        query_result = catalog_constraints.query_region(SkyCoord(ra=ra,
                                                                 dec=dec,
                                                                 unit=(u.deg,
                                                                       u.deg),
                                                                 frame='icrs'),
                                                        radius=radius_str)

        return query_result
Exemple #13
0
    def __query_box(self, ra, dec, base, height, catalog):
        """Vizier.query_region in a square/rectangular FoV using astroquery module."""

        base_str, height_str = str(base) + 'd', str(height) + 'd'

        catalog_constraints = Vizier(catalog=[self.catalog],
                                     columns=[
                                         '*', '_RAJ2000', '_DEJ2000',
                                         self.column_1, self.column_2
                                     ],
                                     column_filters={
                                         self.column_1: self.filter_1,
                                         self.column_2: self.filter_2
                                     })

        catalog_constraints.ROW_LIMIT = -1  # completed catalog
        query_result = catalog_constraints.query_region(SkyCoord(ra=ra,
                                                                 dec=dec,
                                                                 unit=(u.deg,
                                                                       u.deg),
                                                                 frame='icrs'),
                                                        width=height_str,
                                                        height=base_str)
        return query_result
 def catalog_info(self, id):
     """Takes KIC_ID, returns stellar information from online catalog using Vizier"""
     if type(id) is not int:
         raise TypeError('KIC_ID ID must be of type "int"')
     columns = [
         "Teff", "log(g)", "Rad", "E_Rad", "e_Rad", "Mass", "E_Mass",
         "e_Mass"
     ]
     catalog = "J/ApJS/229/30/catalog"
     result = (Vizier(columns=columns).query_constraints(
         KIC=id, catalog=catalog)[0].as_array())
     Teff = result[0][0]
     logg = result[0][1]
     radius = result[0][2]
     radius_max = result[0][3]
     radius_min = result[0][4]
     mass = result[0][5]
     mass_max = result[0][6]
     mass_min = result[0][7]
     lum = self.star_luminosity(Teff, radius)
     ld, mass, mass_min, mass_max, radius, radius_min, radius_max = tls.catalog_info(
         KIC_ID=id)
     return (ld, Teff, lum, logg, radius, radius_min, radius_max, mass,
             mass_min, mass_max)
Exemple #15
0
def sdss_query(ra_deg, dec_deg, rad_deg, maxmag=20, maxsources=-1):
    """
    Query SDSS DR12 @ VizieR using astroquery.vizier
    parameters: ra_deg, dec_deg, rad_deg: RA, Dec, field
                                          radius in degrees
                maxmag: upper limit magnitude (optional)
                maxsources: maximum number of sources
    returns: astropy.table object
    """
    vquery = Vizier(
        columns=[
            '+_r', 'objID', 'RA_ICRS', 'e_RA_ICRS', 'DE_ICRS', 'e_DE_ICRS',
            'umag', 'e_umag', 'gmag', 'e_gmag', 'rmag', 'e_rmag', 'imag',
            'e_imag', 'zmag', 'e_zmag', 'zsp', 'spCl', 'subCl'
        ],
        #                column_filters={"gmag":
        #                                ("<%f" % maxmag),
        #                               "imag":
        #                                ("<%f" % maxmag)},
        row_limit=maxsources)
    field = SkyCoord(ra=ra_deg, dec=dec_deg, unit=(u.deg, u.deg), frame='icrs')
    return vquery.query_region(field,
                               width=("%fd" % rad_deg),
                               catalog="V/147/sdss12")[0]
Exemple #16
0
    def __init__(self, *args, **kwargs):
        """
        The constructor ...
        :param kwargs:
        """

        # Call the constructor of the base class
        super(S4G, self).__init__(*args, **kwargs)

        # Names
        self.ngc_name = None
        self.ngc_name_nospaces = None

        # The Vizier querying object, specifying the necessary columns for this class
        self.vizier = Vizier(columns=[
            'Name', 'RAJ2000', 'DEJ2000', 'Dmean', 'e_Dmean', 'amaj', 'ell',
            '[3.6]', '[4.5]', 'e_[3.6]', 'e_[4.5]', 'PA'
        ])
        self.vizier.ROW_LIMIT = -1

        # The DustPedia database
        #self.database = DustPediaDatabase()

        # The path for the S4G decomposition models
        #self.components_2d_s4g_path = None

        # The galaxy properties object
        self.properties = None

        # The dictionary of components
        self.components = dict()

        #
        self.disk_pa = None
        self.disk = None
        self.bulge = None
Exemple #17
0
def stars_moc(field, radius, moc_order=15):
    """
    MOC with the field area cover by bright stars (V<15).
    (HST Guide Star Catalog, Version 2.3.2 (GSC2.3))
    """
    v = Vizier(columns=['RAJ2000', 'DEJ2000', 'Vmag'],
               column_filters={"Vmag": "<15"},
               row_limit=np.inf,
               timeout=6000)

    vrsp = v.query_region(field,
                          radius=(radius + 1.5 * u.arcmin),
                          catalog='I/305')

    moc_stars = MOC()
    if vrsp:
        stars = vrsp[0]
        stars_coords = SkyCoord(ra=stars['RAJ2000'], dec=stars['DEJ2000'])
        stars_radius = (16 - stars['Vmag']) * 6 * u.arcsec  # Aird+2015

        for coord, r in zip(stars_coords, stars_radius):
            moc_stars += catalog_to_moc(coord, r, moc_order)

    return moc_stars
Exemple #18
0
def get_2mass_cat(tab):
    """
    Get 2MASS catalog
    
    Parameters
    ----------
    tab: astropy table
        Table of objects to search. Must have "ra" and "dec" columns for sources
    """

    vizier = Vizier(catalog='II/246/out')
    vizier.ROW_LIMIT = 99999

    ra_med = np.median(tab['ra'])
    dec_med = np.median(tab['dec'])
    med_coord = SkyCoord(ra_med * u.deg, dec_med * u.deg)
    coord_tab = SkyCoord(tab['ra'] * u.deg, tab['dec'] * u.deg)
    sep = med_coord.separation(coord_tab)

    search_rad = np.max(sep) + 10. * u.arcsec

    tm_res = vizier.query_region(med_coord, radius=search_rad)[0]

    tm_coord = SkyCoord(tm_res['RAJ2000'], tm_res['DEJ2000'])
    idx, d2d, d3d = coord_tab.match_to_catalog_sky(tm_coord)

    max_sep = 1.0 * u.arcsec

    sep_constraint = d2d < max_sep
    bad_sep = d2d > max_sep

    fig, axArr = plt.subplots(3, 2, figsize=(10, 10))  #,sharey=True)

    #axArr[0].plot(tab['J mag'],diff,'.')
    for bandInd, oneBand in enumerate(['J', 'H', 'K']):

        tab['{}mag_2mass'.format(oneBand)] = tm_res['{}mag'.format(
            oneBand)][idx]
        tab['{}mag_2mass'.format(oneBand)][bad_sep] = np.nan

        diff = tab['{} mag'.format(oneBand)] - tab['{}mag_2mass'.format(
            oneBand)]

        plot_density_scatter(axArr[bandInd, 0], tab['{} mag'.format(oneBand)],
                             diff)
        axArr[bandInd, 0].set_ylim(-0.2, 0.2)
        axArr[bandInd, 0].set_ylabel(r"{} (UKIRT) - {} (2MASS)".format(
            oneBand, oneBand))
        axArr[bandInd, 0].set_xlabel("{} (UKIRT)".format(oneBand))

        plot_density_scatter(axArr[bandInd, 1], tab['J mag'] - tab['K mag'],
                             diff)
        axArr[bandInd, 1].set_xlabel("J (UKIRT) - K (UKIRT)")
        axArr[bandInd, 1].set_ylim(-0.2, 0.2)


#    minmax_show = [np.nanmin(tab['J mag']),np.nanmax(tab['Jmag_2mass'])]
#plt.plot(minmax_show,1.0)
    plt.show()

    return tm_res
Exemple #19
0
result_table.add_column(const,name="Constellation", index = 2)

otype = result_table['OTYPE_3']
internal_id = [otype[i]+'_'+const_abr[i]+'_'+str(i+1) for i in range(len(const))]
result_table.add_column(internal_id, name = "Internal ID Number", index = 0)

result_table.write("messier_objects.csv",format="csv",overwrite="True")#creates a csv file

Simbad.reset_votable_fields()#renders the prev changes to simbad class temporary.
print('\n')
print('Done')
print('\n')
print('Downloading NGC catalogue...')

#CHANGE GET_CONSTELLATION EQUINOX FROM B1875 TO 2000
v = Vizier(columns = ['Name','Type','mag','RA (deg)','Dec (deg)'])#Columns added to table
v.ROW_LIMIT = -1
result_table = v.get_catalogs("VII/118/ngc2000")[0]

result_table['Name'] = ['IC '+x[1:] if x[0]=='I' else 'NGC '+x for x in result_table['Name']]
result_table['Type'] = ['Gal' if x=='Gx' else 'OpC' if x=='OC' else 'GlC' if x=='Gb' else 'PN' if x=='Pl' else 'Str' if x=='*' or x=='D*' or x=='***' else '-' if x == '' or x=='-' or x=='?' else x for x in result_table['Type']]

coords = SkyCoord(result_table['_RAJ2000'],result_table['_DEJ2000'],unit="deg")
const = coords.get_constellation()
const_abr = coords.get_constellation(short_name = "True")
result_table.add_column(const,name="Constellation", index = 2)

otype = result_table['Type']
internal_id = [otype[i]+'_'+const_abr[i]+'_'+str(i+1) if otype[i]!='-' else 'notype'+'_'+const_abr[i]+'_'+str(i+1) for i in range(len(const))]
result_table.add_column(internal_id, name = "Internal ID Number", index = 0)
result_table.write("NGC.csv",format="csv",overwrite="True")
Exemple #20
0
def tmass(obsids_table,
          data_folder,
          moc_folder,
          opt_moc=None,
          radius=15 * u.arcmin,
          moc_order=16,
          overwrite=True):
    """
    Get 2MASS data using astroquery and Vizier
    For each observation in obsids_table, saves a fits file with
    name 'OBS_ID.fits' in 'data_folder/groups'.

    The function sends a Vizier query and selects all sources within 'radius'
    arcmin of the RA,DEC of the observation, then it filters the result
    selecting the sources in the corresponding MOC stored in 'moc_folder/mocs'
    (moc_order must be consistent with the order used to calculate the moc).

    If overwrite is True, always create a new fits file. If False, checks for
    an existing file and uses it to calculate the number of WISE sources
    in the field. If it doesn't exist, creates the file.

    The function returns obsids_table with an additional column 'NSRC_2M'
    with the number of sources in the field.
    """
    # Groups folder
    groups_folder = os.path.join(data_folder, 'groups')
    if not os.path.exists(groups_folder):
        os.makedirs(groups_folder)

    moc_folder = os.path.join(moc_folder, 'mocs')

    if opt_moc is not None:
        moc_optsurvey = MOC()
        read_moc_fits(moc_optsurvey, opt_moc)

    nsources_field = np.full((len(obsids_table), ), np.nan)
    hp = HEALPix(nside=2**moc_order, order='nested', frame=ICRS())

    v = Vizier(columns=[
        'Cntr', 'RAJ2000', 'DEJ2000', 'errMaj', 'errMin', 'errPA', 'Qflg'
    ],
               row_limit=np.inf,
               timeout=6000)

    for i, row in enumerate(tqdm(obsids_table, desc="Making 2MASS groups")):
        ## Group file name
        field_table_file = os.path.join(data_folder, groups_folder,
                                        '{}.fits'.format(row['OBS_ID']))
        is_field_table = os.path.exists(field_table_file)

        if overwrite or (not overwrite and not is_field_table):
            ## Select all sources in the field
            field_coords = SkyCoord(ra=row['RA'] * u.deg,
                                    dec=row['DEC'] * u.deg)

            vrsp = v.query_region_async(field_coords,
                                        radius=radius,
                                        catalog='II/246/out',
                                        return_type='asu-tsv')

            # Fix bug in the vizier response
            # (returns the id as a short int and fails to load
            # properly as an astropy table)
            with open('/tmp/tmp.tab', 'wb') as tmpfile:
                tmpfile.write(vrsp.content)

            src_table = Table.read('/tmp/tmp.tab', format='ascii.tab')
            src_table = src_table[2:]

            objid = np.array(src_table['Cntr']).astype(np.int64)
            ra = np.array(src_table['RAJ2000']).astype(np.float) * u.deg
            dec = np.array(src_table['DEJ2000']).astype(np.float) * u.deg

            errMaj = np.array(src_table['errMaj']).astype(np.float) * u.arcsec
            #            err_ra[err_ra == '            '] = '-1'
            #            err_ra = err_ra.astype(np.float) * u.arcsec
            #            err_ra[err_ra == -1] = np.nan

            errMin = np.array(src_table['errMin']).astype(np.float) * u.arcsec
            #            err_dec[err_dec == '            '] = '-1'
            #            err_dec = err_dec.astype(np.float) * u.arcsec
            #            err_dec[err_dec == -1] = np.nan

            errPA = np.array(src_table['errPA']).astype(np.float) * u.deg
            flag = np.array(src_table['Qflg'])

            src_table = Table([objid, ra, dec, errMaj, errMin, errPA, flag],
                              names=[
                                  'objID', 'RAJ2000', 'DEJ2000', 'errMaj',
                                  'errMin', 'errPA', 'Qflg'
                              ])
            # Filter table
            # Sources detected with SNR>=5 in J, H or K
            flgJ = [f[0] in ['A', 'B', 'C'] for f in src_table['Qflg']]
            flgH = [f[1] in ['A', 'B', 'C'] for f in src_table['Qflg']]
            flgK = [f[2] in ['A', 'B', 'C'] for f in src_table['Qflg']]
            msk_good = np.logical_and(flgJ, np.logical_and(flgH, flgK))
            src_table_new = src_table[msk_good]

            ## Select sources in the non-overlaping area
            moc_field = MOC()
            read_moc_fits(
                moc_field,
                os.path.join(moc_folder, '{}.moc'.format(row['OBS_ID'])))
            if opt_moc is not None:
                moc_field = moc_optsurvey.intersection(moc_field)

            inmoc_table = sources_inmoc(src_table_new,
                                        hp,
                                        moc_field,
                                        moc_order=moc_order,
                                        ra='RAJ2000',
                                        dec='DEJ2000')
            ## Save sources
            field_table_file = os.path.join(data_folder, groups_folder,
                                            '{}.fits'.format(row['OBS_ID']))

            inmoc_table.meta['description'] = '2MASS'
            inmoc_table.remove_column('Qflg')
            inmoc_table.write(field_table_file, overwrite=True)

        else:
            inmoc_table = Table.read(field_table_file)

        nsources_field[i] = len(inmoc_table)

    colsrc = Table.Column(nsources_field, name='NSRC_2M')
    obsids_table.add_column(colsrc)

    return obsids_table
Exemple #21
0
 def get_catalogue(self, catalogue, boundary_value="nan"):
     cat_ids = {
         "SUMSS": "VIII/81B/sumss212",
         "NVSS": "VIII/65/nvss",
         "MGPS2": "VIII/82/mgpscat"
     }
     if catalogue not in cat_ids:
         self.logger.error("Requested catalogue not recongised.")
         return
     elif catalogue == "SUMSS":
         search_cat = [cat_ids["SUMSS"], cat_ids["MGPS2"]]
     elif catalogue == "NVSS":
         search_cat = [
             cat_ids["NVSS"],
         ]
     else:
         search_cat = [
             cat_ids["MGPS2"],
         ]
     if not self.wcs:
         self.load_wcs()
     if not self.header:
         self.load_header()
     if not self.centre:
         self.load_position_dimensions()
     if np.abs(self.size_x -
               self.size_y) > max([self.size_x, self.size_y]) * 0.05:
         self.logger.warning(
             "Non square image detected! Will pad the Vizier search area by 1.6 (default 1.3)."
         )
         pad = 1.6
     else:
         pad = 1.3
     # if not self.data:
     #     self.load_data()
     v = Vizier(columns=["_r", "_RAJ2000", "_DEJ2000", "**"])
     v.ROW_LIMIT = -1
     self.logger.info("Querying {} using Vizier...".format(catalogue))
     result = v.query_region(self.centre,
                             radius=self.radius * pad,
                             catalog=search_cat)
     if catalogue == "SUMSS":
         catalogs_returned = result.keys()
         #Searching the galactic plane survey as well, check if there are any found in that one
         if len(catalogs_returned) == 2:
             #Here we have to merge them together, MGPS2 has two extra columns: 'MGPS' and 'E' pandas can just merge them
             df_result = result[cat_ids["SUMSS"]].to_pandas()
             mgps2_result = result[cat_ids["MGPS2"]].to_pandas()
             # mgps2_result.drop(["MGPS", "E"])
             #And just append the mgps2 sources to the sumss result
             df_result = df_result.append(mgps2_result)
         else:
             df_result = result[0].to_pandas()
     else:
         df_result = result[0].to_pandas()
     if catalogue == "NVSS":
         df_result["PA"].fillna(0.0, inplace=True)
         self.raw_nvss_sources = df_result
         self.logger.info("{} sources obtained.".format(catalogue))
         self.logger.info(
             "Filtering {} sources to only those within the image area...".
             format(catalogue))
         self.nvss_sources = self._filter_catalogue(
             self.raw_nvss_sources, boundary_value=boundary_value)
         return self.nvss_sources
     else:
         self.raw_sumss_sources = df_result
         self.logger.info("{} sources obtained.".format(catalogue))
         self.logger.info(
             "Filtering {} sources to only those within the image area...".
             format(catalogue))
         self.sumss_sources = self._filter_catalogue(
             self.raw_sumss_sources, boundary_value=boundary_value)
         return self.sumss_sources
Exemple #22
0
def pstarrs(obsids_table,
            data_folder,
            moc_folder,
            nir_moc=None,
            radius=15 * u.arcmin,
            moc_order=16,
            overwrite=True):
    """
    Get Pan-STARRS data using astroquery and Vizier.
    For each observation in obsids_table, saves a fits file with
    name 'OBS_ID.fits' in 'data_folder/groups'.

    The function sends a Vizier query and selects all sources within 'radius'
    arcmin of the RA,DEC of the observation, then it filters the result
    selecting the sources in the corresponding MOC stored in 'moc_folder/mocs'
    (moc_order must be consistent with the order used to calculate the MOC).

    If overwrite is True, always create a new fits file. If False, checks for
    an existing file and uses it to calculate the number of Pan-STARRS sources
    in the field. If it doesn't exist, creates the file.

    The function returns obsids_table with an additional column 'NSRC_PS' with
    the number of sources in the field.
    """

    # Groups folder
    if nir_moc is None:
        groups_folder = os.path.join(data_folder, 'groups')
    else:
        root, _ = os.path.splitext(os.path.basename(nir_moc))
        survey = root.split('_')[-1]
        groups_folder = os.path.join(data_folder, 'groups_' + survey)

        moc_nirsurvey = MOC()
        read_moc_fits(moc_nirsurvey, nir_moc)

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

    moc_folder = os.path.join(moc_folder, 'mocs')

    nsources_field = np.full((len(obsids_table), ), np.nan)
    hp = HEALPix(nside=2**moc_order, order='nested', frame=ICRS())

    v = Vizier(columns=[
        'objID', 'RAJ2000', 'DEJ2000', 'e_RAJ2000', 'e_DEJ2000', 'Nd', 'Qual'
    ],
               column_filters={"Nd": ">1"},
               row_limit=np.inf,
               timeout=6000)

    for i, row in enumerate(tqdm(obsids_table,
                                 desc="Making Pan-STARRS groups")):
        ## Group file name
        field_table_file = os.path.join(data_folder, groups_folder,
                                        '{}.fits'.format(row['OBS_ID']))
        is_field_table = os.path.exists(field_table_file)

        if overwrite or (not overwrite and not is_field_table):
            ## Select all sources in the field
            field_coords = SkyCoord(ra=row['RA'] * u.deg,
                                    dec=row['DEC'] * u.deg)

            vrsp = v.query_region_async(field_coords,
                                        radius=radius,
                                        catalog='II/349',
                                        return_type='asu-tsv')

            # Fix bug in the vizier response
            # (returns the objID as a short int and fails to load
            # properly as an astropy table)
            with open('/tmp/tmp.tab', 'wb') as tmpfile:
                tmpfile.write(vrsp.content)

            src_table = Table.read('/tmp/tmp.tab', format='ascii.tab')
            src_table = src_table[2:]

            objid = np.array(src_table['objID']).astype(np.int64)
            ra = np.array(src_table['RAJ2000']).astype(np.float) * u.deg
            dec = np.array(src_table['DEJ2000']).astype(np.float) * u.deg

            err_ra = np.array(src_table['e_RAJ2000'])
            err_ra[err_ra == '            '] = '-1'
            err_ra = err_ra.astype(np.float) * u.arcsec
            err_ra[err_ra == -1] = np.nan

            err_dec = np.array(src_table['e_DEJ2000'])
            err_dec[err_dec == '            '] = '-1'
            err_dec = err_dec.astype(np.float) * u.arcsec
            err_dec[err_dec == -1] = np.nan

            flag = np.array(src_table['Qual']).astype(np.int32)

            src_table = Table([objid, ra, dec, err_ra, err_dec, flag],
                              names=[
                                  'objID', 'RAJ2000', 'DEJ2000', 'e_RAJ2000',
                                  'e_DEJ2000', 'Qual'
                              ])
            # Filter table
            msk_good = (src_table['Qual'] & 16) != 0
            src_table_new = src_table[msk_good]

            ## Select sources in the non-overlaping area
            moc_field = MOC()
            read_moc_fits(
                moc_field,
                os.path.join(moc_folder, '{}.moc'.format(row['OBS_ID'])))

            if nir_moc is not None:
                moc_field = moc_nirsurvey.intersection(moc_field)

            inmoc_table = sources_inmoc(src_table_new,
                                        hp,
                                        moc_field,
                                        moc_order=moc_order,
                                        ra='RAJ2000',
                                        dec='DEJ2000')
            ## Save sources
            inmoc_table.remove_columns(['Qual'])
            inmoc_table.meta['description'] = 'Pan-STARRS'
            inmoc_table.write(field_table_file, overwrite=True)

        else:
            inmoc_table = Table.read(field_table_file)

        nsources_field[i] = len(inmoc_table)

    colsrc = Table.Column(nsources_field, name='NSRC_PS')
    obsids_table.add_column(colsrc)

    return obsids_table
Exemple #23
0
def wise(obsids_table,
         data_folder,
         moc_folder,
         nir_moc=None,
         opt_moc=None,
         radius=15 * u.arcmin,
         moc_order=16,
         overwrite=True):
    """
    Get All-WISE data using astroquery and Vizier
    For each observation in obsids_table, saves a fits file with
    name 'OBS_ID.fits' in 'data_folder/groups'.

    The function sends a Vizier query and selects all sources within 'radius'
    arcmin of the RA,DEC of the observation, then it filters the result
    selecting the sources in the corresponding MOC stored in 'moc_folder/mocs'
    (moc_order must be consistent with the order used to calculate the moc).

    If overwrite is True, always create a new fits file. If False, checks for
    an existing file and uses it to calculate the number of WISE sources
    in the field. If it doesn't exist, creates the file.

    The function returns obsids_table with an additional column 'NSRC_WS'
    with the number of sources in the field.
    """
    # Groups folder
    if nir_moc is None:
        groups_folder = os.path.join(data_folder, 'groups')
    else:
        root, _ = os.path.splitext(os.path.basename(nir_moc))
        survey = root.split('_')[-1]
        groups_folder = os.path.join(data_folder, 'groups_' + survey)

        moc_nirsurvey = MOC()
        read_moc_fits(moc_nirsurvey, nir_moc)

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

    moc_folder = os.path.join(moc_folder, 'mocs')

    if opt_moc is not None:
        moc_optsurvey = MOC()
        read_moc_fits(moc_optsurvey, opt_moc)

    nsources_field = np.full((len(obsids_table), ), np.nan)
    hp = HEALPix(nside=2**moc_order, order='nested', frame=ICRS())

    v = Vizier(columns=['ID', 'RAJ2000', 'DEJ2000', 'eeMaj', 'eeMin', 'eePA'],
               row_limit=np.inf,
               timeout=6000)

    for i, row in enumerate(tqdm(obsids_table, desc="Making WISE groups")):
        ## Group file name
        field_table_file = os.path.join(data_folder, groups_folder,
                                        '{}.fits'.format(row['OBS_ID']))
        is_field_table = os.path.exists(field_table_file)

        if overwrite or (not overwrite and not is_field_table):
            ## Select all sources in the field
            field_coords = SkyCoord(ra=row['RA'] * u.deg,
                                    dec=row['DEC'] * u.deg)

            vrsp = v.query_region(field_coords,
                                  radius=radius,
                                  catalog='II/328/allwise')

            ## Select sources in the non-overlaping area
            moc_field = MOC()
            read_moc_fits(
                moc_field,
                os.path.join(moc_folder, '{}.moc'.format(row['OBS_ID'])))
            if opt_moc is not None:
                moc_field = moc_optsurvey.intersection(moc_field)

            if nir_moc is not None:
                moc_field = moc_nirsurvey.intersection(moc_field)

            inmoc_table = sources_inmoc(vrsp[0],
                                        hp,
                                        moc_field,
                                        moc_order=moc_order,
                                        ra='RAJ2000',
                                        dec='DEJ2000')
            ## Save sources
            field_table_file = os.path.join(data_folder, groups_folder,
                                            '{}.fits'.format(row['OBS_ID']))

            inmoc_table.meta['description'] = 'AllWISE'
            inmoc_table.write(field_table_file, overwrite=True)

        else:
            inmoc_table = Table.read(field_table_file)

        nsources_field[i] = len(inmoc_table)

    colsrc = Table.Column(nsources_field, name='NSRC_WS')
    obsids_table.add_column(colsrc)

    return obsids_table
Exemple #24
0
            'DEJ2000',
            'e_RAJ2000',
            'e_DEJ2000',
            'EpRA',
            'gmag'
        ]
        for icat in range(len(names)):
            Ncatname = Nnames[icat]
            catname = names[icat]
            link = links[icat]
            test = True
            try:
                table = Vizier(columns=['all']).query_region(
                    coord.SkyCoord(ra=str(r0),
                                   dec=str(d0),
                                   unit=(u.deg, u.deg),
                                   frame='icrs'),
                    Angle(fov, "deg"),
                    catalog=[link])[0]
                print(
                    Ncatname + ': SUCCESS!  NUMBER OF STARS IN FOV=' +
                    str(fov * 3600) + ' arcsec: ', str(len(table)))

                with open(outfile, 'a') as f:  # 'a'
                    f.write(Ncatname + ': SUCCESS!  NUMBER OF STARS IN FOV=' +
                            str(fov * 3600) + ' arcsec: ' + str(len(table)) +
                            '\n')
                    for star in table:
                        ra = star[signs[6 * icat]]
                        dec = star[signs[6 * icat + 1]]
                        mag = star[signs[6 * icat + 5]]
Exemple #25
0
def get_gaia_coords(img,
                    ota,
                    inst,
                    output='test.gaia',
                    cluster=False,
                    **kwargs):
    """
    Query the online Gaia DR1 based on the central coordinates of the current
    OTA. If the ``cluster`` flag is set to ``True``, the querey will avoid
    a crowded region based on coordinates and a radius set by the user in
    the configuration files.

    Parameters
    ----------
    img : ODIImage or StackedImage object
        Name of image
    ota : str
        Name of OTA
    int : str
        Version of ODI used, ``podi`` or ``5odi``

    """
    from astropy import units as u
    from astropy.coordinates import SkyCoord
    try:
        from astroquery.vizier import Vizier
        from astropy import __version__ as astropyversion
    except ImportError:
        print "astroquery not installed"
        print "try  pip --user --no-deps install astroquery or contact admin"
    hdulist = fits.open(img.f)
    if ota == 'None':
        hdu_ota = hdulist[0]

    else:
        hdu_ota = odi.tan_header_fix(hdulist[ota])

    w = WCS(hdu_ota.header)

    naxis1 = hdu_ota.header['NAXIS1']
    naxis2 = hdu_ota.header['NAXIS2']
    ota_center_radec = w.wcs_pix2world([[naxis1 / 2., naxis2 / 2.]], 1)

    corners = w.calc_footprint()

    center_skycoord = SkyCoord(ota_center_radec[0][0] * u.deg,
                               ota_center_radec[0][1] * u.deg,
                               frame='icrs')
    corner_skycoord = SkyCoord(corners[0, 0] * u.deg,
                               corners[0, 1] * u.deg,
                               frame='icrs')
    cone_radius = center_skycoord.separation(corner_skycoord).value
    # tqdm.write('{:4.0f} {:4.0f} {:6.4f}'.format(naxis1/2., naxis2/2., cone_radius))

    #Set up vizier query for Gaia DR1
    #Taken from example at: github.com/mommermi/photometrypipeline
    vquery = Vizier(columns=[
        'RA_ICRS', 'DE_ICRS', 'e_RA_ICRS', 'e_DE_ICRS', 'phot_g_mean_mag'
    ],
                    column_filters={"phot_g_mean_mag": ("<%f" % 21.0)},
                    row_limit=-1)
    gaia_table = vquery.query_region(SkyCoord(ra=ota_center_radec[0][0],
                                              dec=ota_center_radec[0][1],
                                              unit=(u.deg, u.deg),
                                              frame='icrs'),
                                     radius=cone_radius * u.deg,
                                     catalog=['I/337/gaia'])[0]

    # print gaia_table

    hdulist.close()
    if cluster == True:
        try:
            racenter = kwargs['racenter']
            deccenter = kwargs['deccenter']
            min_radius = kwargs['min_radius']
            G_lim = kwargs['G_lim']
        except KeyError:
            print 'Must provide racenter, deccenter, and min_radius'
        cluster_center = SkyCoord(racenter * u.degree,
                                  deccenter * u.degree,
                                  frame='icrs')
        gaia_coords = SkyCoord(gaia_table['RA_ICRS'],
                               gaia_table['DE_ICRS'],
                               frame='icrs')
        dist_from_center = cluster_center.separation(gaia_coords).arcmin
        gaia_table['dis'] = dist_from_center
        # ota_gaia_df = ota_gaia_df[(ota_gaia_df.dis >= min_radius) &
        #   (ota_gaia_df.phot_g_mean_mag <= G_lim)]
        gaia_table = gaia_table[gaia_table['dis'] > min_radius]

    ra_min, ra_max = min(corners[:, 0]), max(corners[:, 0])
    dec_min, dec_max = min(corners[:, 1]), max(corners[:, 1])
    # print ra_min, ra_max, dec_min, dec_max

    gaia_table_cut = gaia_table[(gaia_table['RA_ICRS'] > ra_min)
                                & (gaia_table['RA_ICRS'] < ra_max) &
                                (gaia_table['DE_ICRS'] > dec_min) &
                                (gaia_table['DE_ICRS'] < dec_max)]
    gaia_table_cut['e_RA_ICRS'].convert_unit_to(u.deg)
    gaia_table_cut['e_DE_ICRS'].convert_unit_to(u.deg)

    ota_gaia_df = gaia_table_cut.to_pandas()

    cols_needed = ['RA_ICRS', 'DE_ICRS', '__Gmag_', 'e_RA_ICRS', 'e_DE_ICRS']

    ota_gaia_df = ota_gaia_df[cols_needed]
    ota_gaia_df.columns = ['ra', 'dec', 'phot_g_mean_mag', 'e_ra', 'e_dec']

    gaia_catalog_out = output
    ota_gaia_df.to_csv(
        gaia_catalog_out,
        columns=['ra', 'dec', 'phot_g_mean_mag', 'e_ra', 'e_dec'],
        index=False)
    return ota_gaia_df
import matplotlib
matplotlib.use('Qt5Agg')

import matplotlib.pyplot as plt
# import seaborn as sns
import aplpy

# sns.set_style('whitegrid')
# plt.close('all')
# sns.set_context('talk')

# v = Vizier(columns=['_RAJ2000', '_DEJ2000',
#                     'RA_ICRS', 'e_RA_ICRS', 'DE_ICRS', 'e_DE_ICRS', 'Source', '<Gmag>'],
#            column_filters={'<Gmag>': '<22'})
v = Vizier()
v.ROW_LIMIT = -1
v.TIMEOUT = 3600


def load_sta_eop(_inp, _date, station_name='KP-VLBA'):
    const = constants()
    ''' load cats '''
    _, sta, eops = load_cats(_inp, 'DUMMY', 'S', [station_name], _date)
    ''' calculate site positions in geodetic coordinate frame
    + transformation matrix VW from VEN to the Earth-fixed coordinate frame '''
    for ii, st in enumerate(sta):
        sta[ii].geodetic(const)

    return sta, eops
Exemple #27
0
def download_nir(aperture, coords_host, host_file_path):
    host_file = open(host_file_path, 'a')
    # Searching for Wise data
    if aperture == 'default/Petro':
        # searching for UKIDSS
        print('Searching UKIDSS data...')
        v = Vizier(
            columns=['RAJ2000', 'DEJ2000', 'pYmag', 'pJmag', 'pHmag', 'pKmag', 'e_pYmag', 'e_pJmag', 'e_pHmag',
                     'e_pKmag', "+_r"])
        result = v.query_region(coords_host, radius=0.0014 * units.deg, catalog=['II/314/las8'])

        try:

            obj = result[0]

            Y, J, H, K = tools.vega_to_ab(obj['pYmag'][0], 'Y'), tools.vega_to_ab(obj['pJmag'][0], 'J'), \
                         tools.vega_to_ab(obj['pHmag'][0], 'H'), tools.vega_to_ab(obj['pKmag'][0], 'K')
            e_Y, e_J, e_H, e_K = obj['e_pYmag'][0], obj['e_pJmag'][0], obj['e_pHmag'][0], obj['e_pKmag'][0]

            if np.isfinite(K):
                host_file.write(tools.format_host_photo('K', 21874, K, e_K, 'UKIDSS', 'Petrosian'))
            if np.isfinite(H):
                host_file.write(tools.format_host_photo('H', 16206, H, e_H, 'UKIDSS', 'Petrosian'))
            if np.isfinite(J):
                host_file.write(tools.format_host_photo('J', 12418, J, e_J, 'UKIDSS', 'Petrosian'))
            if np.isfinite(Y):
                host_file.write(tools.format_host_photo('Y', 10170, Y, e_Y, 'UKIDSS', 'Petrosian'))

        except:
            print('No UKIDSS data found')
            pass

        # Searching for 2MASS data if UKIDSS data were not found
        if len(result) == 0:
            print('Searching 2MASS data...')
            v = Vizier(
                columns=['RAJ2000', 'DEJ2000', 'Jmag', 'Hmag', 'Kmag', 'e_Jmag', 'e_Hmag', 'e_Kmag', "+_r"])
            result = v.query_region(coords_host, radius=0.0014 * units.deg, catalog=['II/246'])
            try:
                obj = result[0]
                J, H, Ks = tools.vega_to_ab(obj['Jmag'][0], 'J'), tools.vega_to_ab(obj['Hmag'][0], 'H'), \
                           tools.vega_to_ab(obj['Kmag'][0], 'Ks')
                e_J, e_H, e_Ks = obj['e_Jmag'][0], obj['e_Hmag'][0], obj['e_Kmag'][0]

                if np.isfinite(Ks):
                    host_file.write(tools.format_host_photo('Ks', 21590, Ks, e_Ks, '2MASS', 'default'))
                if np.isfinite(H):
                    host_file.write(tools.format_host_photo('H', 16620, H, e_H, '2MASS', 'default'))
                if np.isfinite(J):
                    host_file.write(tools.format_host_photo('J', 12350, J, e_J, '2MASS', 'default'))
            except:
                print('No 2MASS Data found')
                pass
    elif aperture == 'standard/PSF':
        # searching for UKIDSS
        print('Searching UKIDSS data...')
        v = Vizier(
            columns=['RAJ2000', 'DEJ2000', 'pYmag', 'pJmag', 'pHmag', 'pKmag', 'e_pYmag', 'e_pJmag', 'e_pHmag',
                     'e_pKmag', "+_r"])
        result = v.query_region(coords_host, radius=0.0014 * units.deg, catalog=['II/314/las8'])

        try:

            obj = result[0]

            Y, J, H, K = tools.vega_to_ab(obj['pYmag'][0], 'Y'), tools.vega_to_ab(obj['pJmag'][0], 'J'), \
                         tools.vega_to_ab(obj['pHmag'][0], 'H'), tools.vega_to_ab(obj['pKmag'][0], 'K')
            e_Y, e_J, e_H, e_K = obj['e_pYmag'][0], obj['e_pJmag'][0], obj['e_pHmag'][0], obj['e_pKmag'][0]

            if np.isfinite(K):
                host_file.write(tools.format_host_photo('K', 21874, K, e_K, 'UKIDSS', 'PSF'))
            if np.isfinite(H):
                host_file.write(tools.format_host_photo('H', 16206, H, e_H, 'UKIDSS', 'PSF'))
            if np.isfinite(J):
                host_file.write(tools.format_host_photo('J', 12418, J, e_J, 'UKIDSS', 'PSF'))
            if np.isfinite(Y):
                host_file.write(tools.format_host_photo('Y', 10170, Y, e_Y, 'UKIDSS', 'PSF'))

        except:
            print('No UKIDSS data found')
            pass

        # Searching for 2MASS data if UKIDSS data were not found
        if len(result) == 0:
            print('Searching 2MASS data...')
            v = Vizier(
                columns=['RAJ2000', 'DEJ2000', 'Jstdap', 'Hstdap', 'Kstdap', 'e_Jstdap', 'e_Hstdap', 'e_Kstdap',
                         "+_r"])
            result = v.query_region(coords_host, radius=0.0014 * units.deg, catalog=['II/246'])
            try:
                obj = result[0]
                J, H, Ks = tools.vega_to_ab(obj['Jstdap'][0], 'J'), tools.vega_to_ab(obj['Hstdap'][0], 'H'), \
                           tools.vega_to_ab(obj['Kstdap'][0], 'Ks')
                e_J, e_H, e_Ks = obj['e_Jstdap'][0], obj['e_Hstdap'][0], obj['e_Kstdap'][0]

                if np.isfinite(Ks):
                    host_file.write(tools.format_host_photo('Ks', 21590, Ks, e_Ks, '2MASS', 'standard'))
                if np.isfinite(H):
                    host_file.write(tools.format_host_photo('H', 16620, H, e_H, '2MASS', 'standard'))
                if np.isfinite(J):
                    host_file.write(tools.format_host_photo('J', 12350, J, e_J, '2MASS', 'standard'))
            except:
                print('No 2MASS Data found')
                pass
    host_file.close()
Exemple #28
0
def download_opt(aperture, coords_host, host_file_path):
    host_file = open(host_file_path, 'a')
    dec_host = coords_host.dec.deg

    if aperture == 'Kron/Petro':
        print('Searching SDSS data...')
        v = Vizier(
            columns=['RAJ2000', 'DEJ2000', 'petroMag_g', 'petroMagErr_g', 'petroMag_r', 'petroMagErr_r', 'petroMag_i', 'petroMagErr_i', 'petroMag_z', 'petroMagErr_z', "+_r"])
        result_sdss = v.query_region(coords_host, radius=0.0014 * units.deg, catalog=['V/147/sdss12'])
        try:
            obj = result_sdss[0]

            z, i, r, g = obj['zPmag'][0], obj['iPmag'][0], obj['rPmag'][0], obj['gPmag'][0]
            e_z, e_i, e_r, e_g = obj['e_zPmag'][0], obj['e_iPmag'][0], obj['e_rPmag'][0], \
                                      obj['e_gPmag'][0]

            if np.isfinite(z):
                host_file.write(tools.format_host_photo('z', 8932, z, e_z, 'SDSS', 'Petrosian'))
            if np.isfinite(i):
                host_file.write(tools.format_host_photo('i', 7480, i, e_i, 'SDSS', 'Petrosian'))
            if np.isfinite(r):
                host_file.write(tools.format_host_photo('r', 6166, r, e_r, 'SDSS', 'Petrosian'))
            if np.isfinite(g):
                host_file.write(tools.format_host_photo('g', 4686, g, e_g, 'SDSS', 'Petrosian'))

        except:
            print('No SDSS Data found')
            pass

        if len(result_sdss) == 0:
            result_pan = []
            if dec_host > -30:
                print('Searching PAN-STARRS data...')
                v = Vizier(
                    columns=['RAJ2000', 'DEJ2000', 'objID', 'yKmag', 'zKmag', 'iKmag', 'rKmag', 'gKmag', 'e_yKmag',
                             'e_zKmag',
                             'e_iKmag', 'e_rKmag', 'e_gKmag', "+_r"])
                result_pan = v.query_region(coords_host, radius=0.0014 * units.deg, catalog=['II/349/ps1'])
                try:
                    obj = result_pan[0]

                    y, z, i, r, g = obj['yKmag'][0], obj['zKmag'][0], obj['iKmag'][0], obj['rKmag'][0], obj['gKmag'][0]
                    e_y, e_z, e_i, e_r, e_g = obj['e_yKmag'][0], obj['e_zKmag'][0], obj['e_iKmag'][0], obj['e_rKmag'][0], \
                                              obj['e_gKmag'][0]

                    if np.isfinite(y):
                        host_file.write(tools.format_host_photo('y', 9633, y, e_y, 'PAN-STARRS', 'Kron'))
                    if np.isfinite(z):
                        host_file.write(tools.format_host_photo('z', 8679, z, e_z, 'PAN-STARRS', 'Kron'))
                    if np.isfinite(i):
                        host_file.write(tools.format_host_photo('i', 7545, i, e_i, 'PAN-STARRS', 'Kron'))
                    if np.isfinite(r):
                        host_file.write(tools.format_host_photo('r', 6215, r, e_r, 'PAN-STARRS', 'Kron'))
                    if np.isfinite(g):
                        host_file.write(tools.format_host_photo('g', 4866, g, e_g, 'PAN-STARRS', 'Kron'))

                except:
                    print('No PAN-STARRS Data found')
                    pass

            if dec_host <= -30 | (len(result_pan) == 0 and dec_host <= 0):
                print('Searching DES data...')
                v = Vizier(
                    columns=['RAJ2000', 'DEJ2000', 'Ymag', 'zmag', 'imag', 'rmag', 'gmag', 'e_Ymag', 'e_zmag', 'e_imag',
                             'e_rmag', 'e_gmag', "+_r"])
                result_des = v.query_region(coords_host,
                                        radius=0.0014 * units.deg,
                                        catalog=['II/357/des_dr1'])
                try:
                    obj = result_des[0]
                    Y, z, i, r, g = obj['Ymag'][0], obj['zmag'][0], obj['imag'][0], obj['rmag'][0], obj['gmag'][0]
                    e_Y, e_z, e_i, e_r, e_g = obj['e_Ymag'][0], obj['e_zmag'][0], obj['e_imag'][0], obj['e_rmag'][0], \
                                              obj['e_gmag'][0]
                    if np.isfinite(Y):
                        host_file.write(tools.format_host_photo('Y', 10305, Y, e_Y, 'DES', 'Kron'))
                    if np.isfinite(z):
                        host_file.write(tools.format_host_photo('z', 8660, z, e_z, 'DES', 'Kron'))
                    if np.isfinite(i):
                        host_file.write(tools.format_host_photo('i', 7520, i, e_i, 'DES', 'Kron'))
                    if np.isfinite(r):
                        host_file.write(tools.format_host_photo('r', 6170, r, e_r, 'DES', 'Kron'))
                    if np.isfinite(g):
                        host_file.write(tools.format_host_photo('g', 4810, g, e_g, 'DES', 'Kron'))
                except:
                    print('No DES Data found')
                    pass

                if len(result_des) == 0:
                    print('Searching SkyMapper data...')
                    v = Vizier(
                        columns=['RAJ2000', 'DEJ2000', 'vPetro', 'gPetro', 'rPetro', 'iPetro',
                                 'zPetro',
                                 'e_vPetro',
                                 'e_gPetro', 'e_rPetro', 'e_iPetro', 'e_zPetro', "+_r"])
                    result = v.query_region(coords_host, radius=0.0014 * units.deg, catalog=['II/358/smss'])
                    try:
                        obj = result[0]
                        z, i, r, g, v = obj['zPetro'][0], obj['iPetro'][0], obj['rPetro'][0], \
                                           obj['gPetro'][0], obj['vPetro'][0]
                        e_z, e_i, e_r, e_g, e_v = obj['e_zPetro'][0], obj['e_iPetro'][0], \
                                                       obj['e_rPetro'][0], obj['e_gPetro'][0], obj['e_vPetro'][0]

                        if np.isfinite(z):
                            host_file.write(tools.format_host_photo('z', 9091, z, e_z, 'SkyMapper', 'Petrosian'))
                        if np.isfinite(i):
                            host_file.write(tools.format_host_photo('i', 7712, i, e_i, 'SkyMapper', 'Petrosian'))
                        if np.isfinite(r):
                            host_file.write(tools.format_host_photo('r', 6040, r, e_r, 'SkyMapper', 'Petrosian'))
                        if np.isfinite(g):
                            host_file.write(tools.format_host_photo('g', 4968, g, e_g, 'SkyMapper', 'Petrosian'))
                        if np.isfinite(v):
                            host_file.write(tools.format_host_photo('v', 3870, v, e_v, 'SkyMapper', 'Petrosian'))
                    except:
                        print('No SkyMapper Data found')
                        pass

        # Searching SDSS u band photometry
        v = Vizier(
            columns=['RAJ2000', 'DEJ2000', 'petroMag_u', 'petroMagErr_u', "+_r"])
        result = v.query_region(coords_host, radius=0.0014 * units.deg, catalog=['V/147/sdss12'])
        try:
            obj = result[0]
            u, e_u = obj['uPmag'][0], obj['e_uPmag'][0]
            if np.isfinite(u):
                host_file.write(tools.format_host_photo('u', 3551, u, e_u, 'SDSS', 'Petrosian'))
            else:
                print('No SDSS u band mag found')
                try:
                    #print('Searching SkyMapper u band data...')
                    v = Vizier(
                        columns=['RAJ2000', 'DEJ2000', 'uPetro', 'e_uPetro', "+_r"])
                    result = v.query_region(coords_host, radius=0.0014 * units.deg, catalog=['II/358/smss'])
                    obj = result[0]
                    u, e_u = obj['uPetro'][0], obj['e_uPetro'][0]
                    if np.isfinite(u):
                        host_file.write(tools.format_host_photo('u', 3551, u, e_u, 'SkyMapper', 'Petrosian'))
                except:

                    pass
        except:
           pass


    elif aperture == 'PSF':
        print('Searching SDSS data...')
        v = Vizier(
            columns=['RAJ2000', 'DEJ2000', 'gpmag', 'e_gpmag', 'rpmag', 'e_rpmag', 'ipmag', 'e_ipmag', 'zpmag', 'e_zpmag',
                     "+_r"])
        result_sdss = v.query_region(coords_host, radius=0.0014 * units.deg, catalog=['V/147/sdss12'])

        try:
            obj = result_sdss[0]

            z, i, r, g = obj['zpmag'][0], obj['ipmag'][0], obj['rpmag'][0], obj['gpmag'][0]
            e_z, e_i, e_r, e_g = obj['e_zpmag'][0], obj['e_ipmag'][0], obj['e_rpmag'][0], \
                                 obj['e_gpmag'][0]

            if np.isfinite(z):
                host_file.write(tools.format_host_photo('z', 8932, z, e_z, 'SDSS', 'Model'))
            if np.isfinite(i):
                host_file.write(tools.format_host_photo('i', 7480, i, e_i, 'SDSS', 'Model'))
            if np.isfinite(r):
                host_file.write(tools.format_host_photo('r', 6166, r, e_r, 'SDSS', 'Model'))
            if np.isfinite(g):
                host_file.write(tools.format_host_photo('g', 4686, g, e_g, 'SDSS', 'Model'))

        except:
            print('No SDSS Data found')
            pass

        if len(result_sdss) == 0:
            result_pan = []
            if dec_host > -30:
                print('Searching PAN-STARRS data...')
                v = Vizier(
                    columns=['RAJ2000', 'DEJ2000', 'objID', 'ymag', 'zmag', 'imag', 'rmag', 'gmag', 'e_ymag',
                             'e_zmag',
                             'e_imag', 'e_rmag', 'e_gmag', "+_r"])
                result_pan = v.query_region(coords_host, radius=0.0014 * units.deg, catalog=['II/349/ps1'])
                try:
                    obj = result_pan[0]

                    y, z, i, r, g = obj['ymag'][0], obj['zmag'][0], obj['imag'][0], obj['rmag'][0], obj['gmag'][0]
                    e_y, e_z, e_i, e_r, e_g = obj['e_ymag'][0], obj['e_zmag'][0], obj['e_imag'][0], obj['e_rmag'][0], \
                                              obj['e_gmag'][0]

                    if np.isfinite(y):
                        host_file.write(tools.format_host_photo('y', 9633, y, e_y, 'PAN-STARRS', 'PSF'))
                    if np.isfinite(z):
                        host_file.write(tools.format_host_photo('z', 8679, z, e_z, 'PAN-STARRS', 'PSF'))
                    if np.isfinite(i):
                        host_file.write(tools.format_host_photo('i', 7545, i, e_i, 'PAN-STARRS', 'PSF'))
                    if np.isfinite(r):
                        host_file.write(tools.format_host_photo('r', 6215, r, e_r, 'PAN-STARRS', 'PSF'))
                    if np.isfinite(g):
                        host_file.write(tools.format_host_photo('g', 4866, g, e_g, 'PAN-STARRS', 'PSF'))

                except:
                    print('No PAN-STARRS Data found')
                    pass

            if dec_host <= -30 | (len(result_pan) == 0 and dec_host <= 0):
                print('Searching DES data...')
                v = Vizier(
                    columns=['RAJ2000', 'DEJ2000', 'YmagPSF', 'zmagPSF', 'imagPSF', 'rmagPSF', 'gmagPSF', 'e_YmagPSF',
                             'e_zmagPSF', 'e_imagPSF', 'e_rmagPSF', 'e_gmagPSF', "+_r"])
                result_des = v.query_region(coords_host,
                                        radius=0.0014 * units.deg,
                                        catalog=['II/357/des_dr1'])
                try:
                    obj = result_des[0]
                    Y, z, i, r, g = obj['YmagPSF'][0], obj['zmagPSF'][0], obj['imagPSF'][0], obj['rmagPSF'][0], \
                                    obj['gmagPSF'][0]
                    e_Y, e_z, e_i, e_r, e_g = obj['e_YmagPSF'][0], obj['e_zmagPSF'][0], obj['e_imagPSF'][0], \
                                              obj['e_rmagPSF'][0], \
                                              obj['e_gmagPSF'][0]
                    if np.isfinite(Y):
                        host_file.write(tools.format_host_photo('Y', 10305, Y, e_Y, 'DES', 'PSF'))
                    if np.isfinite(z):
                        host_file.write(tools.format_host_photo('z', 8660, z, e_z, 'DES', 'PSF'))
                    if np.isfinite(i):
                        host_file.write(tools.format_host_photo('i', 7520, i, e_i, 'DES', 'PSF'))
                    if np.isfinite(r):
                        host_file.write(tools.format_host_photo('r', 6170, r, e_r, 'DES', 'PSF'))
                    if np.isfinite(g):
                        host_file.write(tools.format_host_photo('g', 4810, g, e_g, 'DES', 'PSF'))
                except:
                    print('No DES Data found')
                    pass

                if len(result_des) == 0:
                    print('Searching SkyMapper data...')
                    v = Vizier(
                        columns=['RAJ2000', 'DEJ2000', 'vPSF', 'gPSF', 'rPSF', 'iPSF',
                                 'zPSF', 'e_vPSF', 'e_gPSF', 'e_rPSF', 'e_iPSF', 'e_zPSF', "+_r"])
                    result = v.query_region(coords_host, radius=0.0014 * units.deg, catalog=['II/358/smss'])
                    try:
                        obj = result[0]
                        z, i, r, g, v =obj['zPSF'][0], obj['iPSF'][0], obj['rPSF'][0], \
                                           obj['gPSF'][0], obj['vPSF'][0]
                        e_z, e_i, e_r, e_g, e_v = obj['e_zPSF'][0], obj['e_iPSF'][0], \
                                                       obj['e_rPSF'][0], obj['e_gPSF'][0], obj['e_vPSF'][0]

                        if np.isfinite(z):
                            host_file.write(tools.format_host_photo('z', 9091, z, e_z, 'SkyMapper', 'PSF'))
                        if np.isfinite(i):
                            host_file.write(tools.format_host_photo('i', 7712, i, e_i, 'SkyMapper', 'PSF'))
                        if np.isfinite(r):
                            host_file.write(tools.format_host_photo('r', 6040, r, e_r, 'SkyMapper', 'PSF'))
                        if np.isfinite(g):
                            host_file.write(tools.format_host_photo('g', 4968, g, e_g, 'SkyMapper', 'PSF'))
                        if np.isfinite(v):
                            host_file.write(tools.format_host_photo('v', 3870, v, e_v, 'SkyMapper', 'PSF'))
                    except:
                        print('No SkyMapper Data found')
                        pass

        # Searching SDSS u band photometry
        v = Vizier(
            columns=['RAJ2000', 'DEJ2000', 'umag', 'e_umag', "+_r"])
        result = v.query_region(coords_host, radius=0.0014 * units.deg, catalog=['V/147/sdss12'])
        try:
            obj = result[0]
            u, e_u = obj['upmag'][0], obj['e_upmag'][0]
            if np.isfinite(u):
                host_file.write(tools.format_host_photo('u', 3551, u, e_u, 'SDSS', 'PSF'))
            else:
                print('No SDSS Data found')
                try:
                    v = Vizier(
                        columns=['RAJ2000', 'DEJ2000', 'uPetro', 'e_uPetro', "+_r"])
                    result = v.query_region(coords_host, radius=0.0014 * units.deg, catalog=['II/358/smss'])
                    obj = result[0]
                    u, e_u = obj['uPSF'][0], obj['e_uPSF'][0]
                    if np.isfinite(u):
                        host_file.write(tools.format_host_photo('u', 3498, u, e_u, 'SkyMapper', 'PSF'))
                except:
                    pass
        except:
            pass
    host_file.close()
Exemple #29
0
class SdssSpec:
    sdss = Vizier(columns=[
        'RA_ICRS', 'DE_ICRS', 'umag', 'gmag', 'rmag', 'imag', 'zmag', 'subCl'
    ],
                  column_filters={"SpObjID": ">0"})
    sources = None
    stats = None
    m_stats = None

    def __init__(self, row_limit=100000, path=None):
        """
        Class to download and handle SDSS spectroscopic identificated sources

        :param row_limit: Number sources to download
        :type row_limit: int
        :param path: Path to a file with the data from SDSS DR12. Default is None.
        :type path: str
        """
        self.sdss.ROW_LIMIT = row_limit

        if path is not None:
            self.sources = Table.read(path)

    def download_classification(self, cache=True, r_min=20):
        """
        Downloads the classifications and magnitudes of sources from the SDSS DR12 catalog

        :param cache:
            If a path was set, cache=True means that it will use the data from the file.
            False means that it will download a new set. Default is True.
        :type cache: bool
        :param r_min: Minimal brightness in the r-band
        :type r_min: float
        :return: Table with the downloaded data
        :rtype: astropy.table.Table
        """
        try:
            if cache and self.sources is not None:
                return self.sources
            # query the SDSS DR12 for objects with a spectral identification
            # and a set sub-class identification with the
            # astroquery vizier class
            self.sources = self.sdss.query_constraints(
                catalog='V/147/sdss12',
                SpObjID='>0',
                subCl='!= ',
                rmag='<{}'.format(r_min))[0]
            self.sources = __sub_class_rename__(self.sources)
            self.__stats__()
            return self.sources
        except IndexError:
            return None

    def __stats__(self):
        """
        Counts how often a spectral type appears in the data set

        :return: The counts of the spectral type
        :type: astropy.table.Table
        """
        sub_classes, sub_class_counts = np.unique(self.sources['subCl'],
                                                  return_counts=True)
        self.stats = Table()
        self.stats['subCl'] = sub_classes
        self.stats['count'] = sub_class_counts
        self.__main_identification_stats__()
        return self.stats

    def __main_identification_stats__(self):
        """
        Returns the main identification of the sources and how often they are in the sample.
        A main identification is for a star the main spectral type.
        Galaxies and QSO's are compressed to only one type (AGN and STARFORMING).
        :return: Dict with the main identifications as keys and the values are how often they are in the sample.
        :rtype: dict
        """
        m_stats = {}
        for s in self.stats:
            sub_cl = s['subCl']

            # if the length is two, it is likely a star identification
            if len(sub_cl) == 2:
                if sub_cl[-1].isdigit():
                    # take only the spectral identification key
                    sub_cl = sub_cl[0]
                    m_stats = __add_counts__(m_stats, sub_cl, s)
                # if the second key is not a number
                else:
                    if 'OB' in sub_cl:
                        sub_cl = 'O'
                    m_stats = __add_counts__(m_stats, sub_cl, s)
            # if the length is larger than two
            else:
                # Compress galaxy and QSO's types
                if sub_cl == 'BROADLINE':
                    sub_cl = "AGN"
                elif sub_cl == 'STARBURST':
                    sub_cl = 'GALAXY'
                elif sub_cl == 'STARFORMING':
                    sub_cl = 'GALAXY'
                elif sub_cl == 'Ldwarf':
                    sub_cl = 'L'
                elif sub_cl == 'Carbon_lines':
                    sub_cl = 'Carbon'
                m_stats = __add_counts__(m_stats, sub_cl, s)
        self.m_stats = m_stats
        return m_stats

    def show_sub_classes(self):
        """
        Plots the distribution of different main spectral identifications
        as a bar plot

        :return:
        """
        pl.clf()
        sp = pl.subplot()
        x = np.zeros((len(self.m_stats), 2))
        for i, k in enumerate(self.m_stats):
            x[i, 0] = i
            x[i, 1] = self.m_stats[k]
        sp.bar(x[:, 0], x[:, 1])
        pl.xticks(x[:, 0], list(self.m_stats.keys()), rotation='vertical')
        sp.set_ylabel('counts')
        sp.set_xlabel('main subclass type')
        sp.set_yscale('log')
        pl.show()
Exemple #30
0
"""
This is the code used to generate the polynomial relations
used in sedkit's calculations
"""
from pkg_resources import resource_filename

import astropy.io.ascii as ii
import astropy.units as q
import astropy.table as at
from astroquery.vizier import Vizier
from bokeh.plotting import figure, show
import numpy as np

from . import utilities as u

V = Vizier(columns=["**"])


class SpectralTypeRadius:
    def __init__(self, orders=(5, 3), name='Spectral Type vs. Radius'):
        """Initialize the object

        Parameters
        ----------
        order: int
            The order polynomial to fit to the spt-radius data
        """
        self.name = name
        self.generate(orders)

    def get_radius(self, spt, plot=False):