コード例 #1
0
def objcutouts(objid, size=40, outdir='./', domask=True):
    """ Make cutouts for all the measurements of one object."""

    obj = qc.query(sql="select * from nsc_dr2.object where id='%s'" % objid,
                   fmt='table',
                   profile='db01')
    meas = qc.query(sql="select * from nsc_dr2.meas where objectid='%s'" %
                    objid,
                    fmt='table',
                    profile='db01')
    nmeas = len(meas)
    print(str(nmeas) + ' measurements for ' + objid)
    meascutout(meas, obj, size=size, outdir=outdir, domask=domask)
コード例 #2
0
    def get_catalog(self,
                    query=None,
                    query_fields=None,
                    print_query=False,
                    timeout=120):
        """
        Get catalog sources around the given coordinates
        within self.radius.
        
        Args:
            query (str, optional): SQL query to generate the catalog
            query_fields (list, optional): Over-ride list of items to query
            print_query (bool): Print the SQL query generated 
        
        Returns:
            astropy.table.Table:  Catalog of sources obtained from the SQL query.
        """
        qc.set_profile(self.qc_profile)
        # Generate the query
        if query is None:
            self._gen_cat_query(query_fields)
            query = self.query
        if print_query:
            print(query)
        # Do it while silencing print statements
        result = qc.query(self.token, sql=query, timeout=timeout)
        self.catalog = convert(result, outfmt="table")

        self.catalog.meta['radius'] = self.radius
        self.catalog.meta['survey'] = self.survey
        # Validate
        self.validate_catalog()
        # Return
        return self.catalog.copy()
コード例 #3
0
ファイル: selftemplate.py プロジェクト: KyleLMatt/TempletFit
def get_data(objname, bands = ['u','g','r','i','z','Y','VR']):
    """Query the object by name, extract light curves, 
       error, filters and top N estimated periods."""
    res=qc.query(sql="""SELECT mjd,mag_auto,magerr_auto,filter,fwhm
                        FROM nsc_dr2.meas 
                        WHERE objectid='{:s}'""".format(objname),
                 fmt='table')
    res['mjd'] += np.random.randn(len(res))*10**-10
    
    selbnds = [i for i, val in enumerate(res['filter']) if val in bands]
    selfwhm = np.where(res['fwhm'] <= 4.0)[0]
    sel = [x for x in selbnds if x in selfwhm]
    res = res[sel]
    
    if len(res) <= 0:
        raise Exception('No data with low fwhm')
    
    res['fltr']   = -1
    for i in range(len(res)):
        res['fltr'][i] = bands.index(res['filter'][i])
    
    res.rename_column('mag_auto', 'mag')
    res.rename_column('magerr_auto', 'err')
    res.sort(['fltr','mjd'])
    
    return res
コード例 #4
0
    def get_gaia_data(self, index):
        # get data about star
        row = self.stars.iloc[index]
        ls_id = row.ls_id

        # get gaia id of star from decals
        q = """SELECT
                ra1, dec1, id1, ra2, dec2, id2, distance
            FROM
                ls_dr9.x1p5__tractor__gaia_edr3__gaia_source
            WHERE 
                id1 = {} """.format(ls_id)

        res = qc.query(sql=q)
        decals_data = convert(res, 'pandas')

        # get gaia id (or input id = 0 if no gaia data)
        gaia_id = 0 if len(decals_data.id2) == 0 else decals_data.id2

        # use gaia id to get info about it
        query = """SELECT 
                    source_id, ra, ra_error, dec, dec_error, parallax, pmra, pmra_error, pmdec, pmdec_error
                FROM 
                    gaiadr2.gaia_source
                WHERE 
                    source_id = {} """.format(int(gaia_id))

        gaia_data = Gaia.launch_job(query).get_results().to_pandas()

        return gaia_data
コード例 #5
0
ファイル: query_dl.py プロジェクト: JoannaSakowska/simple_adl
def query(profile, ra, dec, radius=1.0, gmax=23.5):
    """Return data queried from datalab
    Parameters
    ----------
    profile : Profile for data lab query [str]
    ra      : Right Ascension [deg]
    dec     : Declination [deg]
    radius  : radius around (ra, dec) [deg]

    Returns
    -------
    data : numpy recarray of data
    """
    qc.set_profile(profile)
    sql = f'''
    SELECT ra,
           dec,
           gmag,
           gmag-{R_g}*ebv AS gmag_dered, -- dereddend magnitude
           gerr,
           rmag-{R_r}*ebv AS rmag_dered, -- dereddend magnitude
           rmag,
           rerr,
           ebv
    FROM delvemc_y2t2.object 
    WHERE q3c_radial_query(ra,dec,{ra},{dec},{radius})
          AND chi < 3        -- for star-galaxy separation
          AND prob > 0.8     -- for star-galaxy separation
          AND abs(sharp) < 1 -- for star-galaxy separation
          AND gmag < 90      -- for quality
          AND rmag < 90      -- for quality
          AND gmag < {gmax}  -- for quality
    '''
    data = qc.query(sql=sql,fmt='structarray',timeout=300)
    return(data)
コード例 #6
0
ファイル: noao.py プロジェクト: minzastro/lookup
 def _query_to_votable(self, query):
     result = queryClient.query(self.token, adql=query, fmt='votable')
     with tempfile.TemporaryFile(mode='w+b') as f:
         for line in result:
             f.write(line.encode())
         f.seek(0)
         tbl = Table.read(f)
     return tbl
コード例 #7
0
 def _query_sql(self, query):
     print('-' * 72)
     print("QUERY")
     print(query)
     result = qc.query(query, fmt='pandas')
     print("RESULT")
     print(result)
     return result
コード例 #8
0
 def test_querysqlascii(self):
     self.qry = qry
     # Run the list command
     res = queryClient.query(TEST_TOKEN,
                             sql=self.qry,
                             out=None,
                             async_=False,
                             fmt='ascii')
     self.assertEqual(res, qryresascii)
コード例 #9
0
 def test_querysqlvotable(self):
     self.qry = qry
     # Run the list command
     res = queryClient.query(TEST_TOKEN,
                             sql=self.qry,
                             out=None,
                             async_=False,
                             fmt='votable')
     self.assertEqual(res, qryresvotablesql)
コード例 #10
0
def ls_coords_datalab(ra, dec, radius_nuclear=1.5):
    '''Query legacy survey via DataLab'''

    from dl import queryClient as qc
    result = qc.query(sql='SELECT ra,dec from smash_dr1.object LIMIT 10')
    print(result)
    exit()

    return
コード例 #11
0
 def test_querystatus(self):
     # Run the query command
     jobid = queryClient.query(TEST_TOKEN, sql=self.qry, async_=True)
     res = qstatus(jobid)
     self.assertIn(res, ['QUEUED', 'EXECUTING', 'COMPLETED'])
     # Wait a little
     time.sleep(3)
     res = qstatus(jobid)
     self.assertEqual(res, 'COMPLETED')
コード例 #12
0
    def __init__(self, file=None, ra=None, dec=None, bd=None, n_arcmin=5):
        self.n_arcmin = n_arcmin

        # brown dwarf can be ra/dec or data or class
        if ra is not None and dec is not None:
            self.ra = ra
            self.dec = dec

        elif isinstance(bd, (Table, pd.DataFrame)):
            # first change df into what want
            bd = find_info(bd)

            # basic info
            self.ra = bd['ra']
            self.dec = bd['dec']
            self.mu_a = bd['mu_alpha']
            self.mu_d = bd['mu_delta']
            self.pi = bd['pi']

        elif isinstance(bd, BrownDwarf):
            self.ra = bd.ra
            self.dec = bd.dec

            self.bd = bd

        else:
            raise Exception(
                'Brown Dwarf data needs to either be ra/dec, an astropy table or pandas table of the dwarf data, or the brown dwarf class.'
            )

        # now to grab the star info
        dl.queryClient.getClient(profile='default',
                                 svc_url='https://datalab.noirlab.edu/query')

        if file == None:
            q = """SELECT
                        ls_id, ra, dec,  dered_mag_g, dered_mag_r, dered_mag_w1, dered_mag_w2, dered_mag_w3, dered_mag_w4, dered_mag_z, gaia_phot_g_mean_mag, gaia_duplicated_source, pmdec, pmra, psfsize_g, psfsize_r, psfsize_z, ref_cat, ref_epoch, ref_id, type
                    FROM
                        ls_dr9.tractor
                    WHERE
                        't' = Q3C_RADIAL_QUERY(ra, dec,  {} , {} ,  ({}/60)) """.format(
                float(self.ra), float(self.dec), float(self.n_arcmin))
            res = qc.query(sql=q)
            self.stars = convert(res, 'pandas')

        else:
            self.file = file
            self.stars = pd.read_csv(self.file)

        self.stars = self.filter_stars_only(self.stars)
        self.stars = self.filter_stars_mag(self.stars)

        # create array of paths for each star
        self.star_paths = np.zeros(len(self.stars))
コード例 #13
0
 def test_queryadqlvotable(self):
     self.qry = qryadql
     # Run the query command
     res = queryClient.query(TEST_TOKEN,
                             adql=self.qry,
                             out=None,
                             async_=False,
                             fmt='votable')
     # remove whitespace when comparing the strings
     self.assertEqual("".join(res.split()),
                      "".join(qryresvotableadql.split()))
コード例 #14
0
 def test_querysqltovospacecsv(self):
     self.qry = qry
     # Run the list command
     res = queryClient.query(TEST_TOKEN,
                             sql=self.qry,
                             out='vos://' + self.outfile,
                             async_=False,
                             fmt='csv')
     self.assertEqual(res, 'OK')
     self.assertTrue(fileExists(self.outfile))
     # Get the results and compare
     data = get(self.outfile)
     self.assertEqual(data.decode('utf-8'), qryrescsv)
コード例 #15
0
    def getAvaliableDataset(self):
        # set default profile
        qc.set_profile('default')

        # these schemas are not astronomical datasets
        _remove = set(['ivao', 'ivao_smash', 'tap_schema', 'schema'])

        # get all schemas from DB
        _schemas = set(qc.query(self.__TOKEN_SESSION, sql='SELECT schema FROM tbl_stat').split())

        # remove non-astro schemas
        _alldatasets = sorted(list(_schemas - _remove))
        print("Datasets available in Data Lab (with current profile):\n", _alldatasets)
コード例 #16
0
    def desQuery(self,ra,dec,radius_arcmin=1,columns="*"):

        radius_degree = CoordinateParser.getMinToDegree(radius_arcmin)

        query_template = "SELECT {0:s} FROM des_dr1.main WHERE q3c_radial_query(ra,dec,{1:f},{2:f},{3:f})"
        query = query_template.format(columns, ra, dec, radius_degree)
        df = None
        try:
            result = qc.query(self.getToken(), sql=query)  # by default the result is a CSV formatted string
            result = result.decode('utf8').replace("'", '"')
            df = helpers.convert(result, 'pandas')
        except Exception as e:
            print(e)
        return df
コード例 #17
0
 def test_queryasync(self):
     # Run the query command
     jobid = queryClient.query(TEST_TOKEN, sql=self.qry, async_=True)
     if qstatus(jobid) != 'COMPLETED':
         time.sleep(2)
     # Get the results
     res = qresults(jobid)
     tab = np.loadtxt(StringIO(res),
                      unpack=False,
                      skiprows=1,
                      delimiter=',')
     self.assertTrue(np.array_equiv(tab[:, 0], qryresid.astype('float64')))
     self.assertTrue(np.allclose(tab[:, 1], qryresra))
     self.assertTrue(np.allclose(tab[:, 2], qryresdec))
コード例 #18
0
ファイル: svc_sdss.py プロジェクト: noaodatalab/specserver
    def query(self, id, fields, catalog, cond):
        '''Return a CSV string of query results on the dataset.  If an 'id'
           is supplied we query directly against the value, otherwise we can
           use an arbitrary 'cond' in the WHERE clause.
        '''

        if fields in [None, 'None', '']:
            fields = sdss_id_main

        if id not in [None, 'None', '']:
            _where = id
            qstring = 'SELECT %s FROM %s WHERE %s = %s' % \
                      (fields, catalog, sdss_id_main, \
                      toSigned(np.uint64(id), 64))
            print(qstring)
            res = qc.query(sql=qstring, fmt='table')
        else:
            _where = '' if cond.strip()[:5].lower() in ['order', 'limit'
                                                        ] else 'WHERE'
            if sdss_id_main in fields:
                qstring = 'SELECT %s FROM %s %s %s' % \
                          (fields, catalog, _where, cond)
            else:
                qstring = 'SELECT %s,%s FROM %s %s %s' % \
                          (sdss_id_main, fields, catalog, _where, cond)

            # Query the table and force the object ID to be an unsigned int.
            res = qc.query(sql=qstring, fmt='table')
            res[sdss_id_main].dtype = np.uint64

        # Return result as CSV
        ret = StringIO()
        ascii.write(res, ret, format='csv')
        retval = ret.getvalue()

        return retval
コード例 #19
0
 def test_queryadqlascii(self):
     self.qry = qryadql
     # Run the query command
     res = queryClient.query(TEST_TOKEN,
                             adql=self.qry,
                             out=None,
                             async_=False,
                             fmt='ascii')
     tab = np.loadtxt(StringIO(res),
                      unpack=False,
                      skiprows=1,
                      delimiter='\t')
     self.assertTrue(np.array_equiv(tab[:, 0], qryresid.astype('float64')))
     self.assertTrue(np.allclose(tab[:, 1], qryresra))
     self.assertTrue(np.allclose(tab[:, 2], qryresdec))
コード例 #20
0
 def test_queryadqltovospacefits(self):
     self.qry = qryadql
     # Run the list command
     res = queryClient.query(TEST_TOKEN,
                             adql=self.qry,
                             out='vos://' + self.outfile,
                             async_=False,
                             fmt='fits')
     self.assertEqual(res, 'OK')
     self.assertTrue(fileExists(self.outfile))
     # Get the results and compare
     res = get(self.outfile, self.outfile)
     outdata = Table.read(self.outfile, format='fits')
     self.assertEqual(outdata['id'].data.tostring(), qryresid.tostring())
     self.assertTrue(np.allclose(outdata['ra'].data, qryresra))
     self.assertTrue(np.allclose(outdata['dec'].data, qryresdec))
コード例 #21
0
def query_coords_ls(ra,
                    dec,
                    radius_arcsec=5,
                    radius_nuclear=1.,
                    catalog='dr8_north',
                    datalab=True,
                    check_quality=True,
                    print_results=True):
    '''Query the database to search for matches at the given coordinates'''

    #Crossmatch with photoz database
    if datalab is True:
        radius_deg = radius_arcsec / 3600.
        query = qc.query(
            sql=f"SELECT z_phot_median, z_phot_std, z_phot_l95, ra, dec, \
                             type, flux_z from ls_dr8.photo_z INNER JOIN ls_dr8.tractor \
                             ON ls_dr8.tractor.ls_id = ls_dr8.photo_z.ls_id \
                             where ra > ({ra-radius_deg}) and \
                             ra < ({ra+radius_deg}) and \
                             dec > ({dec-radius_deg}) and \
                             dec < ({dec+radius_deg})")
        result0 = query.split('\n')
        result0 = [r.split(",") for r in result0][1:-1]

        ras = [float(r[3]) for r in result0]
        decs = [float(r[4]) for r in result0]
        result = []
        if len(ras) > 0:
            # Add separation
            gal_coords = SkyCoord(ra=ras * u.deg, dec=decs * u.deg)
            cand_coord = SkyCoord(ra=ra * u.deg, dec=dec * u.deg)
            sep = cand_coord.separation(gal_coords)
            for i in np.arange(len(ras)):
                result0[i].append(float(sep[i].arcsec))
                # Check that the separation is less than required
                if float(sep[i].arcsec) < radius_arcsec:
                    result.append(result0[i])

        if print_results:
            print(
                "z_phot_median, z_phot_std, z_phot_l95, ra, dec, type, flux_z, dist(arcsec)"
            )
            for r in result:
                print(r)

        return result
コード例 #22
0
    def run(self):
        token = getUserToken(self)

        # Get the query string to be used.  This can either be supplied
        # directly or by specifying a filename.
        sql = None
        adql = None
        res = None

        if self.adql.value is None or self.adql.value == '':
            if self.sql.value is None or self.sql.value == '':
                print("Error: At least one of 'adql' or 'sql' is required.")
                sys.exit(-1)
            elif os.path.exists(self.sql.value):
                with open(self.sql.value, "r", 0) as fd:
                    sql = fd.read(os.path.getsize(self.sql.value) + 1)
                fd.close()
            else:
                sql = self.sql.value
        elif os.path.exists(self.adql.value):
            with open(self.adql.value, "r", 0) as fd:
                adql = fd.read(os.path.getsize(self.adql.value) + 1)
                fd.close()
        else:
            adql = self.adql.value

        # Execute the query.
        if self.profile.value != "default":
            if self.profile.value != "" and self.profile.value is not None:
                queryClient.set_profile(profile=self.profile.value)

        try:
            res = queryClient.query(token,
                                    adql=adql,
                                    sql=sql,
                                    fmt=self.fmt.value,
                                    out=self.out.value,
                                    async=self. async .value)

            if self. async .value:
                print(res)  # Return the JobID
            elif self.out.value == '' or self.out.value is None:
                print(res)  # Return the results
コード例 #23
0
 def test_queryadqltovospacecsv(self):
     self.qry = qryadql
     # Run the list command
     res = queryClient.query(TEST_TOKEN,
                             adql=self.qry,
                             out='vos://' + self.outfile,
                             async_=False,
                             fmt='csv')
     self.assertEqual(res, 'OK')
     self.assertTrue(fileExists(self.outfile))
     # Get the results and compare
     data = get(self.outfile)
     tab = np.loadtxt(StringIO(data.decode('utf-8')),
                      unpack=False,
                      skiprows=1,
                      delimiter=',')
     self.assertTrue(np.array_equiv(tab[:, 0], qryresid.astype('float64')))
     self.assertTrue(np.allclose(tab[:, 1], qryresra))
     self.assertTrue(np.allclose(tab[:, 2], qryresdec))
コード例 #24
0
ファイル: nsc_dwarfs_hpix.py プロジェクト: dnidever/nscdwarfs
def getData(ra, dec, radius=1.0, columns='*'):

    query_template = \
    """SELECT {0:s} FROM nsc_dr1.object
       WHERE q3c_radial_query(ra,dec,{1:f},{2:f},{3:f})"""

    query = query_template.format(columns, ra, dec, radius)
    print(query)

    try:
        result = qc.query(
            token,
            sql=query)  # by default the result is a CSV formatted string
    except Exception as e:
        print(e.message)

    df = helpers.convert(result, 'table')  # pandas

    return df
コード例 #25
0
 def test_querysqltomydb(self):
     self.qry = qry
     # Create the mydb table from a query
     res = queryClient.query(TEST_TOKEN,
                             sql=self.qry,
                             out='mydb://' + self.table,
                             async_=False)
     self.assertEqual(res, 'OK')
     res = list(self.table)
     self.assertNotEqual(res, 'relation "' + self.table + '" not known')
     # Get the results and compare
     mydbqry = 'select * from mydb://' + self.table
     res = sqlquery(mydbqry, fmt='csv', async_=False, out=None)
     tab = np.loadtxt(StringIO(res),
                      unpack=False,
                      skiprows=1,
                      delimiter=',')
     self.assertTrue(np.array_equiv(tab[:, 0], qryresid.astype('float64')))
     self.assertTrue(np.allclose(tab[:, 1], qryresra))
     self.assertTrue(np.allclose(tab[:, 2], qryresdec))
コード例 #26
0
ファイル: nsc_dwarfs_hpix.py プロジェクト: dnidever/nscdwarfs
def getDataCuts(ra,
                dec,
                radius=1.0,
                columns='ra,dec,gmag,rmag',
                colcutlo=None,
                colcuthi=None,
                classcut=None,
                fwhmcut=None,
                errcut=None):

    query_template = \
    """SELECT {0:s} FROM nsc_dr1.object
       WHERE q3c_radial_query(ra,dec,{1:f},{2:f},{3:f})"""
    #   (gmag-rmag)>({4:f}) and (gmag-rmag)<{5:f} and class_star>{6:f} and
    #   fwhm<{7:f} and gerr<{8:f} and rerr<{8:f}

    query = query_template.format(columns, ra, dec, radius, colcutlo, colcuthi,
                                  classcut, fwhmcut, errcut)
    if colcutlo is not None:
        query += " and (gmag-rmag)>(" + "{0:f}".format(colcutlo) + ")"
    if colcuthi is not None:
        query += " and (gmag-rmag)<" + "{0:f}".format(colcuthi)
    if classcut is not None:
        query += " and class_star>" + "{0:f}".format(classcut)
    if fwhmcut is not None: query += " and fwhm<" + "{0:f}".format(fwhmcut)
    if errcut is not None: query += " and gerr<" + "{0:f}".format(errcut)
    if errcut is not None: query += " and rerr<" + "{0:f}".format(errcut)
    print(query)

    try:
        result = qc.query(
            token,
            sql=query)  # by default the result is a CSV formatted string
    except Exception as e:
        print(e.message)

    df = helpers.convert(result, 'table')  # pandas

    return df
コード例 #27
0
 def get_catalog(self,
                 query=None,
                 query_fields=None,
                 print_query=False,
                 timeout=120):
     """
     Get catalog sources around the given coordinates
     within self.radius.
     
     Args:
         query (str, optional): SQL query to generate the catalog
         query_fields (list, optional): Over-ride list of items to query
         print_query (bool): Print the SQL query generated 
     
     Returns:
         astropy.table.Table:  Catalog of sources obtained from the SQL query.
     """
     qc.set_profile(self.qc_profile)
     # Generate the query
     if query is None:
         self._gen_cat_query(query_fields)
         query = self.query
     if print_query:
         print(query)
     # Do it while silencing print statements
     result = qc.query(self.token, sql=query, timeout=timeout)
     sys.stdout = open(os.devnull, "w")
     temp = convert(result)
     sys.stdout = sys.__stdout__
     self.catalog = Table.from_pandas(temp)
     # TODO:: Dig into why the heck it doesn't want to natively
     #        output to a table when it was clearly intended to with 'outfmt=table'
     # Finish
     self.catalog.meta['radius'] = self.radius
     self.catalog.meta['survey'] = self.survey
     # Validate
     self.validate_catalog()
     # Return
     return self.catalog.copy()
コード例 #28
0
def get_data(nms, bands=['u', 'g', 'r', 'i', 'z', 'Y', 'VR']):
    """Query list of objects by name, extract light curves, 
       error, filters and top N estimated periods."""
    res = qc.query(sql="""select objectid,mjd,mag_auto,magerr_auto,filter,fwhm
                          from nsc_dr2.meas 
                          where objectid in {}""".format(tuple(nms)),
                   fmt='table')
    res['mjd'] += np.random.randn(len(res)) * 1e-10

    selbnds = [i for i, val in enumerate(res['filter']) if val in bands]
    selfwhm = np.where(res['fwhm'] <= 4.0)[0]
    sel = [x for x in selbnds if x in selfwhm]
    res = res[sel]
    res['fltr'] = -1
    for i in range(len(res)):
        res['fltr'][i] = bands.index(res['filter'][i])

    res.rename_column('mag_auto', 'mag')
    res.rename_column('magerr_auto', 'err')
    res.sort(['fltr', 'mjd'])

    return res
コード例 #29
0
import specClient as spec
from PIL import Image
from io import BytesIO
import time

from dl import queryClient as qc

sql = """select specobjid 
         from sdss_dr16.specobj 
         where run2d = '103' and z > 0.02 and class = 'QSO'
             order by z 
             limit 25600"""
print('start query')
_s1 = time.time()
ids = qc.query(sql=sql).split('\n')[1:-1]
_e1 = time.time()
print('end query')

id = []
for p in ids:
    id.append(int(p))

print('start stack')
_s2 = time.time()
data = spec.stackedImage(id,
                         scale=(0.25, 0.25),
                         thickness=3,
                         inverse=False,
                         cmap='summer')
_e2 = time.time()
コード例 #30
0
def query_coords_ls(ra,
                    dec,
                    radius_arcsec=5,
                    radius_nuclear=1.,
                    catalog='dr8_north',
                    datalab=True,
                    check_quality=True):
    '''Query the database to search for matches at the given coordinates'''

    #Crossmatch with photoz database
    if datalab is True:
        from dl import queryClient as qc
        radius_deg = radius_arcsec / 3600.
        query = qc.query(
            sql=f"SELECT z_phot_median, z_phot_std, z_phot_l95, ra, dec, \
                             type, flux_z from ls_dr8.photo_z INNER JOIN ls_dr8.tractor \
                             ON ls_dr8.tractor.ls_id = ls_dr8.photo_z.ls_id \
                             where ra > ({ra-radius_deg}) and \
                             ra < ({ra+radius_deg}) and \
                             dec > ({dec-radius_deg}) and \
                             dec < ({dec+radius_deg})")
        result0 = query.split('\n')
        result0 = [r.split(",") for r in result0][1:-1]

        ras = [float(r[3]) for r in result0]
        decs = [float(r[4]) for r in result0]
        result = []
        if len(ras) > 0:
            # Add separation
            gal_coords = SkyCoord(ra=ras * u.deg, dec=decs * u.deg)
            cand_coord = SkyCoord(ra=ra * u.deg, dec=dec * u.deg)
            sep = cand_coord.separation(gal_coords)
            for i in np.arange(len(ras)):
                result0[i].append(float(sep[i].arcsec))
                # Check that the separation is less than required
                if float(sep[i].arcsec) < radius_arcsec:
                    result.append(result0[i])

        for r in result:
            print(r)
    else:
        #Read the secrets file and make the connection to the database
        info = ascii.read('./db_access.csv', format='csv')

        info_photoz = info[info['db'] == 'photoz']
        db_photoz = f"host={info_photoz['host'][0]} port={info_photoz['port'][0]} dbname={info_photoz['dbname'][0]} user={info_photoz['user'][0]} password={info_photoz['password'][0]}"
        connection_photoz = psycopg2.connect(db_photoz)
        cursor_photoz = connection_photoz.cursor()

        query = f'''SELECT z_phot_median, z_phot_std, z_phot_l95, z_phot_u95, \
                z_spec, "PARALLAX", "FLUX_Z", \
                q3c_dist({ra}, {dec}, "RA", "DEC") * 3600  as sep_arcsec, \
                "RA", "DEC", "TYPE" \
                FROM {catalog} where \
                q3c_radial_query("RA", "DEC", {ra}, {dec}, \
                {radius_arcsec} * 0.0002777)\
                ORDER BY sep_arcsec'''
        cursor_photoz.execute(query)
        result = cursor_photoz.fetchall()

    if len(result) == 0:
        return None

    nuclear = False
    check_int = None
    info_out = []
    if datalab is True:
        separations = [float(s.arcsec) for s in sep]
        if np.min(separations) <= radius_nuclear:
            nuclear = True
    else:
        if result[0][7] <= radius_nuclear:
            nuclear = True
    checked_z_std = False
    for r in result:
        #If nuclear, skip everything further than 5 arcsec
        if nuclear and r[7] > 2 * radius_nuclear:
            continue
        #Select only good mags
        flux = float(r[6])
        mag_z = -2.5 * np.log10(flux) + 22.5
        photoz, photoz_err = None, None

        # Hard limit for z-band
        if mag_z > 21:
            continue

        if mag_z < 21. and check_quality is True:
            #Get information about the distribution of errors
            #in the std of the photoz
            if checked_z_std is False:
                median_std_magz, std_magz = get_sigma_limit(cursor_photoz,
                                                            ra,
                                                            dec,
                                                            flux,
                                                            dmag=0.1,
                                                            query_radius=2,
                                                            confidence=0.95)
                checked_z_std = True

            if r[1] < median_std_magz + 2 * std_magz:
                photoz, photoz_err = r[0], r[1]
                check_int = 1
        elif mag_z < 21. and check_quality is False:
            check_int = 0
            photoz, photoz_err = r[0], r[1]
        if datalab:
            info = {
                'ls_ra': r[3],
                'ls_dec': r[4],
                'ls_sep_arcsec': r[7],
                'ls_z_spec': None,
                'ls_z_phot_median': photoz,
                'ls_z_phot_std': photoz_err,
                'ls_type': r[5],
                'ls_z_phot_l95': r[2],
                'ls_z_phot_u95': None,
                'ls_fluxz': r[6],
                'ls_photoz_checked': check_int
            }
        else:
            info = {
                'ls_ra': r[8],
                'ls_dec': r[9],
                'ls_sep_arcsec': r[7],
                'ls_z_spec': r[4],
                'ls_z_phot_median': photoz,
                'ls_z_phot_std': photoz_err,
                'ls_type': r[10],
                'ls_z_phot_l95': r[2],
                'ls_z_phot_u95': r[3],
                'ls_fluxz': r[6],
                'ls_photoz_checked': check_int
            }
        info_out.append(info)

    return info_out
コード例 #31
0
def make_4panelplot(obj, outdir):
    matplotlib.use('Agg')
    #params = {'tex.usetex': True}
    #plt.rcParams.update(params)
    #plt.rc(usetex = True)

    idv = obj['id']

    #gather all data used
    dataselect = "select mjd,ra,dec,mag_auto,raerr,decerr,filter,fwhm,exposure from nsc_dr2.meas where objectid='" + idv + "'"
    meas = qc.query(sql=dataselect, fmt='table', profile='db01')
    #obj = qc.query(sql="select id,pmra,pmdec from nsc_dr2.object where id='"+ idv +"'",fmt='table',profile='db01')
    #obj = qc.query(sql="select id,pmra,pmdec from nsc_dr2.object where id='"+ idv +"'",fmt='table')

    # Make cut on FWHM
    # maybe only use values for 0.5*fwhm_chip to 1.5*fwhm_chip
    sql = "select chip.* from nsc_dr2.chip as chip join nsc_dr2.meas as meas on chip.exposure=meas.exposure and chip.ccdnum=meas.ccdnum"
    sql += " where meas.objectid='" + idv + "'"
    chip = qc.query(sql=sql, fmt='table')
    ind3, ind4 = dln.match(chip['exposure'], meas['exposure'])
    si = np.argsort(ind4)  # sort by input meas catalog
    ind3 = ind3[si]
    ind4 = ind4[si]
    chip = chip[ind3]
    meas = meas[ind4]
    gdfwhm, = np.where((meas['fwhm'] > 0.2 * chip['fwhm'])
                       & (meas['fwhm'] < 2.0 * chip['fwhm']))
    if len(gdfwhm) == 0:
        print('All measurements have bad FWHM values')
        return
    if len(gdfwhm) < len(meas):
        print('Removing ' + str(len(meas) - len(gdfwhm)) +
              ' measurements with bad FWHM values')
        meas = meas[gdfwhm]

    plt.subplots(2, 2, figsize=(12, 8))
    plt.subplots_adjust(hspace=.4, wspace=.4)
    plt.suptitle(idv)
    meas["mjd"] -= min(meas["mjd"])
    mjd = (meas["mjd"])
    cenra = np.mean(meas["ra"])
    cendec = np.mean(meas["dec"])
    mag = meas["mag_auto"]
    filters = meas["filter"]
    #colors = ["b", "g", "r", "c", "y"]
    colordict = {
        'u': 'c',
        'g': 'b',
        'r': 'g',
        'i': 'y',
        'z': 'orange',
        'Y': 'r',
        'VR': 'purple'
    }
    ra = meas["ra"]
    raerr = meas['raerr']
    dra = ra - cenra
    dra *= 3600 * np.cos(np.deg2rad(cendec))
    dec = meas["dec"]
    decerr = meas['decerr']
    ddec = dec - cendec
    ddec *= 3600
    t = meas["mjd"] - np.mean(meas["mjd"])
    pmra = obj["pmra"] / 1000 / 365.2425  # mas/yr -> as/day
    pmdec = obj["pmdec"] / 1000 / 365.2425

    size = 15

    # filter some
    goodind = np.where(np.logical_and(abs(ddec) < 500, abs(dra) < 500))
    ddec = ddec[goodind]
    dra = dra[goodind]
    raerr = raerr[goodind]
    decerr = decerr[goodind]
    mjd = mjd[goodind]
    filters = filters[goodind]
    mag = mag[goodind]
    meandra = np.mean(dra)
    meanddec = np.mean(ddec)

    # Unique filters
    # put them in this order [u,g,r,i,z,Y,VR]
    ufilter = []
    for filt in ['u', 'g', 'r', 'i', 'z', 'Y', 'VR']:
        if filt in filters:
            ufilter.append(filt)

    # ra dec plot
    plt.subplot(2, 2, 1)
    plt.errorbar(dra,
                 ddec,
                 raerr,
                 decerr,
                 fmt='none',
                 ecolor='lightgray',
                 elinewidth=1,
                 capsize=0,
                 alpha=0.5,
                 zorder=0)
    plt.scatter(dra, ddec, c=mjd, s=size, zorder=1)
    diffra = max(dra) - min(dra)
    diffdec = max(ddec) - min(ddec)
    plt.xlim(min(dra) - diffra / 4, max(dra) + diffra / 4)
    plt.ylim(min(ddec) - diffdec / 4, max(ddec) + diffdec / 4)
    m, b = np.polyfit(dra, ddec, 1)
    plt.plot(dra, m * dra + b, c="k")
    plt.colorbar(label=r'$\Delta$ MJD')
    plt.xlabel("$\Delta$ RA (arcsec)")
    plt.ylabel("$\Delta$ DEC (arcsec)")

    # ra mjd plot
    plt.subplot(2, 2, 2)
    plt.errorbar(mjd,
                 dra,
                 yerr=raerr,
                 fmt='none',
                 ecolor='lightgray',
                 elinewidth=1,
                 capsize=0,
                 alpha=0.5,
                 zorder=0)
    count = 0
    for fil in ufilter:
        filind = np.where(filters == fil)
        #plt.scatter(mjd[filind], dra[filind], c = colors[count], label = fil, s = size, zorder=1)
        plt.scatter(mjd[filind],
                    dra[filind],
                    c=colordict[fil],
                    label=fil,
                    s=size,
                    zorder=1)
        count += 1
    plt.legend()
    diffmjd = max(mjd) - min(mjd)
    plt.xlim(min(mjd) - diffmjd / 4, max(mjd) + diffmjd / 4)
    plt.ylim(min(dra) - diffra / 4, max(dra) + diffra / 4)
    m, b = np.polyfit(mjd, dra, 1)
    plt.plot(mjd, mjd * pmra + b, c="k")
    plt.xlabel(r'$\Delta$ MJD (days)')
    plt.ylabel(r'$\Delta$ RA (arcsec)')

    # dec mjd plot
    plt.subplot(2, 2, 4)
    plt.errorbar(mjd,
                 ddec,
                 yerr=decerr,
                 fmt='none',
                 ecolor='lightgray',
                 elinewidth=1,
                 capsize=0,
                 alpha=0.5,
                 zorder=0)
    count = 0
    for fil in ufilter:
        filind = np.where(filters == fil)
        plt.scatter(mjd[filind],
                    ddec[filind],
                    c=colordict[fil],
                    label=fil,
                    s=size,
                    zorder=1)
        count += 1
    plt.legend()
    plt.xlim(min(mjd) - diffmjd / 4, max(mjd) + diffmjd / 4)
    plt.ylim(min(ddec) - diffdec / 4, max(ddec) + diffdec / 4)
    m, b = np.polyfit(mjd, ddec, 1)
    plt.plot(mjd, mjd * pmdec + b, c="k")
    plt.xlabel(r'$\Delta$ MJD (days)')
    plt.ylabel(r'$\Delta$ DEC (arcsec)')

    #magtime
    plt.subplot(2, 2, 3)
    count = 0
    for fil in ufilter:
        filind = np.where(filters == fil)
        plt.scatter(mjd[filind],
                    mag[filind],
                    c=colordict[fil],
                    label=fil,
                    s=size)
        count += 1
    plt.legend()
    diffmag = max(mag) - min(mag)
    plt.xlim(min(mjd) - diffmjd / 4, max(mjd) + diffmjd / 4)
    plt.ylim(min(mag) - diffmag / 4, max(mag) + diffmag / 4)
    plt.xlabel(r'$\Delta$ MJD (days)')
    plt.ylabel("Magnitude")

    outfile = outdir + idv + '.png'
    #outfile = outdir+idv+'_4panel.png'
    print('Saving figure to ' + outfile)
    plt.savefig(outfile, bbox_inches='tight')