# 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')
des = fits.getdata('J_MNRAS_489_5301_desy3-lt.fits')
print(len(des))
des.dtype
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.')
try:
print(qc.schema('nsc_dr1.object',format='json',profile='db01'))
except Exception as e:
print(e.message)
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)
"""
res = qc.query(sql=query3,fmt='table',profile='db01')
len(res)
des = Table(des)
des
df = des.to_pandas()
type(df)
df
%%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)
# 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))
"""
xmatch = qc.query(sql=query_xmatch,fmt='table',profile='db01')
len(xmatch)