In [18]:
# Python 2/3 compatibility
from __future__ import print_function # to use print() as a function in Python 2

try:
    input = raw_input # use 'input' function in both Python 2 and 3
except NameError:
    pass

# std lib
from astropy.table import Table
from io import BytesIO

# 3rd party
import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.ticker import NullFormatter
from astropy.utils.data import download_file  #import file from URL
from scipy.stats import binned_statistic_2d
from astropy.io import fits
%matplotlib inline

# Data Lab
from dl import authClient as ac, queryClient as qc, storeClient as sc, helpers
from dl.helpers.utils import convert
from getpass import getpass

print('Done importing')
Done importing
In [2]:
des = fits.getdata('J_MNRAS_489_5301_desy3-lt.fits')
In [3]:
print(len(des))
11745
In [6]:
des.dtype
Out[6]:
dtype((numpy.record, [('ID', '>i4'), ('RADEG', '>f8'), ('DEDEG', '>f8'), ('SPTYPEPH', '>f4'), ('NBANDS', '>i4'), ('XI2CLASS', '>f8'), ('DISTAVG', '>f8'), ('DISTZ', '>f8'), ('HPIX512', '>i4'), ('PEC', '>i4'), ('IPSF', '>f8'), ('E_IPSF', '>f8'), ('ZPSF', '>f8'), ('E_ZPSF', '>f8'), ('YAUTO', '>f8'), ('E_YAUTO', '>f8'), ('JAP3', '>f8'), ('E_JAP3', '>f8'), ('HAP3', '>f8'), ('E_HAP3', '>f8'), ('KSAP3', '>f8'), ('E_KSAP3', '>f8'), ('W1MAG', '>f8'), ('E_W1MAG', '>f8'), ('W2MAG', '>f8'), ('E_W2MAG', '>f8')]))
In [8]:
token = ac.login('ameisner',getpass("Enter password: (+ENTER) "))
if not ac.isValidToken(token):
    raise Exception('Token is not valid. Please check your usename/password and execute this cell again.')
Enter password: (+ENTER) ········
In [11]:
try:
    print(qc.schema('nsc_dr1.object',format='json',profile='db01'))
except Exception as e:
    print(e.message)
Schema: nsc_dr1
 Table: object

     Column Name   Description
     -----------   -----------
              ra   Right ascension (J2000.0) of object, in degrees
             dec   Declination (J2000.0) of object, in degrees
            glon   Galactic Longitude
            glat   Galactic Latitude
            elon   Ecliptic Longitude
            elat   Ecliptic Latitude
           raerr   Uncertainty of RA
          decerr   Uncertainty in DEC
            pmra   Proper motion in RA*cos(DEC)
           pmdec   Proper motion in DEC
         pmraerr   Uncertainty in PMRA
        pmdecerr   Uncertainty in PMDEC
             mjd   Mean Modified Julian Date
        deltamjd   Range of Modified Julian Date
            umag   Weighted-average u-band magnitude
            urms   RMS scatter in u from multiple measurements of this object
            uerr   Uncertainty in mean u-band magnitude
          uasemi   u-band profile RMS along major axis, in arcseconds
          ubsemi   u-band profile RMS along minor axis, in arcseconds
          utheta   u-band position-angle measured CCW East of North, in degrees
            gmag   Weighted-average g-band magnitude
            grms   RMS scatter in g from multiple measurements of this object
            gerr   Uncertainty in mean g-band magnitude
          gasemi   g-band profile RMS along major axis, in arcseconds
          gbsemi   g-band profile RMS along minor axis, in arcseconds
          gtheta   g-band position-angle measured CCW East of North, in degrees
            rmag   Weighted-average r-band magnitude
            rrms   RMS scatter in r from multiple measurements of this object
            rerr   Uncertainty in mean r-band magnitude
          rasemi   r-band profile RMS along major axis, in arcseconds
          rbsemi   r-band profile RMS along minor axis, in arcseconds
          rtheta   r-band position-angle measured CCW East of North, in degrees
            imag   Weighted-average i-band magnitude
            irms   RMS scatter in i from multiple measurements of this object
            ierr   Uncertainty in mean i-band magnitude
          iasemi   i-band profile RMS along major axis, in arcseconds
          ibsemi   i-band profile RMS along minor axis, in arcseconds
          itheta   i-band position-angle measured CCW East of North, in degrees
            zmag   Weighted-average z-band magnitude
            zrms   RMS scatter in z from multiple measurements of this object
            zerr   Uncertainty in mean z-band magnitude
          zasemi   z-band profile RMS along major axis, in arcseconds
          zbsemi   z-band profile RMS along minor axis, in arcseconds
          ztheta   z-band position-angle measured CCW East of North, in degrees
            ymag   Weighted-average Y-band magnitude
            yrms   RMS scatter in Y from multiple measurements of this object
            yerr   Uncertainty in mean Y-band magnitude
          yasemi   Y-band profile RMS along major axis, in arcseconds
          ybsemi   Y-band profile RMS along minor axis, in arcseconds
          ytheta   Y-band position-angle measured CCW East of North, in degrees
           vrmag   Weighted-average VR-band magnitude
           vrrms   RMS scatter in VR from multiple measurements of this object
           vrerr   Uncertainty in mean VR-band magnitude
         vrasemi   VR-band profile RMS along major axis, in arcseconds
         vrbsemi   VR-band profile RMS along minor axis, in arcseconds
         vrtheta   VR-band position-angle measured CCW East of North, in degrees
              x2   Mean variance (second moment) along RA
           x2err   Uncertainty in mean variance along RA
              y2   Mean variance (second moment) along DEC
           y2err   Uncertainty in mean variance along DEC
              xy   Mean covariance between RA and DEC
           xyerr   Uncertainty in mean covariance between RA and DEC
           asemi   Mean profile RMS along major axis, in arcseconds
        asemierr   Uncertainty in mean RMS along major axis
           bsemi   Mean profile RMS along minor axis, in arcseconds
        bsemierr   Uncertainty in mean RMS along minor axis
           theta   Position angle measured CCW East of North, in degrees
        thetaerr   Uncertainty in THETA
     ellipticity   Object ellipticity, 1 - BSEMI/ASEMI
            fwhm   FWHM assuming a gaussian core, in arcseconds
      class_star   Star/Galaxy classifier
             ebv   SFD E(B-V) reddening
             u_g   Computed (u-g) color
             g_r   Computed (g-r) color
             r_i   Computed (r-i) color
             i_z   Computed (i-z) color
             z_y   Computed (z-Y) color
       random_id   Random ID in the range 0.0 => 100.0
             pix   Healpix nside=128 pixel number
            ndet   Number of detections in all bands
           nphot   Number of photometric detections in all bands
            htm9   HTM index (order 9 => ~10 arcmin size)
         ring256   HEALPIX index (Nsides 256, Ring scheme => ~14 arcmin size)
        nest4096   HEALPIX index (Nsides 4096, Nest scheme => ~52 arcsec size
           ndetu   Number of detections in u
          nphotu   Number of photometric detections in u
           ndetg   Number of detections in g
          nphotg   Number of photometric detections in g
           ndetr   Number of detections in r
          nphotr   Number of photometric detections in r
           ndeti   Number of detections in i
          nphoti   Number of photometric detections in i
           ndetz   Number of detections in z
          nphotz   Number of photometric detections in z
           ndety   Number of detections in Y
          nphoty   Number of photometric detections in Y
          ndetvr   Number of detections in VR
         nphotvr   Number of photometric detections in VR
           flags   OR-combined SExtractor FLAG value
              id   Unique object ID

In [12]:
query3="""
select * 
from nsc_dr2.hpm 
where (abs(pmra)>50 or abs(pmdec)>50) and ndet>10 and class_star>0.8
   and q3c_radial_query(ra,dec,45,0,15)
   """
In [16]:
res = qc.query(sql=query3,fmt='table',profile='db01')
In [14]:
len(res)
Out[14]:
28624
In [20]:
des = Table(des)
In [21]:
des
Out[21]:
Table length=11745
IDRADEGDEDEGSPTYPEPHNBANDSXI2CLASSDISTAVGDISTZHPIX512PECIPSFE_IPSFZPSFE_ZPSFYAUTOE_YAUTOJAP3E_JAP3HAP3E_HAP3KSAP3E_KSAP3W1MAGE_W1MAGW2MAGE_W2MAG
int32float64float64float32int32float64float64float64int32int32float64float64float64float64float64float64float64float64float64float64float64float64float64float64float64float64
30251303618.383074-51.73295210.055.944515277.50128270.82232240815022.5714440.04140335720.9378220.02245240320.3941820.06891329618.7228490.03731772-9999.0-9999.017.7736260.10899788-9999.0-9999.0-9999.0-9999.0
71547410330.9374-57.2314810.073.9938006308.3687306.911352915933022.7360920.04368474721.2094650.02703216320.8824480.0882811818.8033890.0559277318.3095150.0781037317.8605380.1060787917.6040.17517.293-9999.0
28886716114.237402-56.9909810.051.5716019423.55643432.305272231754023.3720.0768770521.9533540.04346076821.5666470.153168219.7380140.1363815-9999.0-9999.018.3610670.21355835-9999.0-9999.0-9999.0-9999.0
38348897758.13261-49.1831727.0836.21944826.54640427.084422196428023.7838950.2085637521.529480.0374859120.2805270.0812629217.8199830.0357598318.2309970.1226131918.1913640.1691598417.8840.14215.2960.058
35687787650.330784-60.19451511.051.7570083166.10141162.184682147768021.8776910.0351155120.3245320.01532127419.888510.07612163618.0171930.02921861-9999.0-9999.016.7790530.049350604-9999.0-9999.0-9999.0-9999.0
9454304534.492831.1903710.072.0944626375.59894383.85351142874023.2738420.0807553321.6952290.0406772521.2371580.1468666619.503980.1306000418.8484440.156604618.5099560.2301201817.8140.217.242-9999.0
151675786348.2356-61.84113710.062.7445805299.22055290.448942951275022.5252650.03401521621.089750.02076087920.57550.0878535818.8505570.051523075-9999.0-9999.017.7936480.1208738417.6140.15617.438-9999.0
48415784160.698914-51.34498210.088.450613163.54118158.748752195775021.2132320.01012816919.7779520.005538381619.3815820.027109817.526140.03514173616.9625680.0354634516.4682450.04722717416.290.04316.0610.098
133695937352.54565-46.86276610.055.1386247392.19415365.725162968636023.3241180.0970402721.5901740.03650835521.2855550.162225819.354510.11033197519.0791850.19149317-9999.0-9999.0-9999.0-9999.0-9999.0-9999.0
213746245326.3004-57.41039310.063.2933145355.293352.777562937178023.0007970.0679350721.5119060.0379194221.2898730.1254541319.399570.12197684518.586120.11785977-9999.0-9999.017.7160.18117.471-9999.0
..............................................................................
22862714223.339895-44.89195610.0712.90795348.38162336.931582243952022.8017060.06617319621.412110.02745188221.230920.1708219.4123290.11635893618.7340160.1529349718.3267630.1960196317.4210.13117.479-9999.0
130332878350.96796-61.66815611.060.3234805266.54227263.120852950072022.9025630.07016858521.3752570.02942702220.9531920.1592073418.9400580.049731225-9999.0-9999.017.8441450.1195818717.4390.14717.172-9999.0
172933482358.48264-45.89710610.062.1547785409.36505405.159822969726023.2426640.11813860421.8125320.04430035521.3593310.162212419.6945320.161005818.799730.1543842118.6814460.27046824-9999.0-9999.0-9999.0-9999.0
23303656523.725773-4.64693711.085.0914536221.7162213.63261128087022.4885850.04678188320.922820.0178741220.650220.0697255818.6917250.06315926517.907410.05375736217.4912450.07800159617.060.1116.5430.263
29156189715.543482-44.8050410.062.6237476351.68973355.36232265517022.9034160.0636605721.5277580.0337882621.0357020.112091219.3907220.0935655518.6033440.09983455418.1507640.13497484-9999.0-9999.0-9999.0-9999.0
172097055309.24188-52.37646510.050.68550944427.6131433.770782944288023.4038370.1260417621.9607030.0370709421.6169340.2088017619.6182380.11649921-9999.0-9999.018.4948620.22136508-9999.0-9999.0-9999.0-9999.0
122586549350.9275-1.37983410.064.714307312.00964319.550661202317022.5609040.05179670521.2970980.02888977920.7797740.1123479819.0095710.0932055618.376450.1148673318.0281580.13732727-9999.0-9999.0-9999.0-9999.0
87117581337.97092-59.44003312.0822.069214227.93562219.021882914113122.9587750.0571339421.494870.02423982121.2299020.1305245319.2650220.09063893618.5340520.11179630518.044420.139996217.0990.11216.5960.242
12966922638.0769920.82015211.076.410885297.82126298.28511137140023.3339860.08796938521.647640.0369062821.1642740.1183446219.1966570.0855568518.8183820.1472201218.2123950.170220217.4940.17117.014-9999.0
205190738323.80814-53.0023811.052.9331472324.53156303.921942939985023.3692170.1504165821.6882920.0591370521.5206130.1918823419.2804810.07608844-9999.0-9999.018.3004650.1907637-9999.0-9999.0-9999.0-9999.0
In [22]:
df = des.to_pandas()
In [23]:
type(df)
Out[23]:
pandas.core.frame.DataFrame
In [24]:
df
Out[24]:
ID RADEG DEDEG SPTYPEPH NBANDS XI2CLASS DISTAVG DISTZ HPIX512 PEC ... JAP3 E_JAP3 HAP3 E_HAP3 KSAP3 E_KSAP3 W1MAG E_W1MAG W2MAG E_W2MAG
0 302513036 18.383074 -51.732952 10.0 5 5.944515 277.501280 270.82230 2240815 0 ... 18.722849 0.037318 -9999.000000 -9999.000000 17.773626 0.108998 -9999.000 -9999.000 -9999.000 -9999.000
1 71547410 330.937400 -57.231480 10.0 7 3.993801 308.368700 306.91135 2915933 0 ... 18.803389 0.055928 18.309515 0.078104 17.860538 0.106079 17.604 0.175 17.293 -9999.000
2 288867161 14.237402 -56.990980 10.0 5 1.571602 423.556430 432.30527 2231754 0 ... 19.738014 0.136381 -9999.000000 -9999.000000 18.361067 0.213558 -9999.000 -9999.000 -9999.000 -9999.000
3 383488977 58.132610 -49.183170 27.0 8 36.219448 26.546404 27.08442 2196428 0 ... 17.819983 0.035760 18.230997 0.122613 18.191364 0.169160 17.884 0.142 15.296 0.058
4 356877876 50.330784 -60.194515 11.0 5 1.757008 166.101410 162.18468 2147768 0 ... 18.017193 0.029219 -9999.000000 -9999.000000 16.779053 0.049351 -9999.000 -9999.000 -9999.000 -9999.000
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
11740 172097055 309.241880 -52.376465 10.0 5 0.685509 427.613100 433.77078 2944288 0 ... 19.618238 0.116499 -9999.000000 -9999.000000 18.494862 0.221365 -9999.000 -9999.000 -9999.000 -9999.000
11741 122586549 350.927500 -1.379834 10.0 6 4.714307 312.009640 319.55066 1202317 0 ... 19.009571 0.093206 18.376450 0.114867 18.028158 0.137327 -9999.000 -9999.000 -9999.000 -9999.000
11742 87117581 337.970920 -59.440033 12.0 8 22.069214 227.935620 219.02188 2914113 1 ... 19.265022 0.090639 18.534052 0.111796 18.044420 0.139996 17.099 0.112 16.596 0.242
11743 129669226 38.076992 0.820152 11.0 7 6.410885 297.821260 298.28510 1137140 0 ... 19.196657 0.085557 18.818382 0.147220 18.212395 0.170220 17.494 0.171 17.014 -9999.000
11744 205190738 323.808140 -53.002380 11.0 5 2.933147 324.531560 303.92194 2939985 0 ... 19.280481 0.076088 -9999.000000 -9999.000000 18.300465 0.190764 -9999.000 -9999.000 -9999.000 -9999.000

11745 rows × 26 columns

In [50]:
%%time

# Preemptively drop the table in case it already exists (mydb does not overwrite well right now)
qc.mydb_drop('des_bds')

# Import the dataframe as mybd table
qc.mydb_import('des_bds',df)
CPU times: user 358 ms, sys: 9 ms, total: 367 ms
Wall time: 11.7 s
Out[50]:
'OK'
In [51]:
# query for matching the two catalogs
query_xmatch="""
SELECT N.*
FROM mydb://des_bds as d, nsc_dr2.object as N
WHERE q3c_join(N.ra, N.dec, d.radeg, d.dedeg, (1/3600.0))
"""
In [52]:
xmatch = qc.query(sql=query_xmatch,fmt='table',profile='db01')
---------------------------------------------------------------------------
queryClientError                          Traceback (most recent call last)
<ipython-input-52-ad4831cdf66c> in <module>
----> 1 xmatch = qc.query(sql=query_xmatch,fmt='table',profile='db01')

/data0/sw/anaconda3/lib/python3.7/site-packages/noaodatalab-2.18.10-py3.7.egg/dl/Util.py in __call__(self, *args, **kw)
     80             return function(self.obj, *args, **kw)
     81         else:
---> 82             return function(*args, **kw)
     83 
     84     def __repr__(self):

/data0/sw/anaconda3/lib/python3.7/site-packages/noaodatalab-2.18.10-py3.7.egg/dl/queryClient.py in query(token, adql, sql, fmt, out, async_, drop, profile, **kw)
    536     return qc_client._query (token=def_token(token), adql=adql, sql=sql, 
    537                              fmt=fmt, out=out, async_=async_, drop=drop, profile=profile,
--> 538                              **kw)
    539 
    540 

/data0/sw/anaconda3/lib/python3.7/site-packages/noaodatalab-2.18.10-py3.7.egg/dl/queryClient.py in _query(self, token, adql, sql, fmt, out, async_, drop, profile, **kw)
   1919         r = requests.get (dburl, headers=headers, timeout=timeout)
   1920         if r.status_code != 200:
-> 1921             raise queryClientError (r.text)
   1922         resp = qcToString(r.content)
   1923 

queryClientError: Error: QM: MyDB table des_bds does not exist
In [28]:
len(xmatch)
Out[28]:
28624
In [ ]: