<commit_before>import os
import shutil
import requests as re
import astropy.units as u
from astropy.io import fits
from astroquery.vizier import Vizier


# Create the download file directory.
data_dir = os.path.expanduser('~/astro_data/')
if not os.path.exists(data_dir):
    os.mkdir(data_dir)


## Download digital sky survey fits.
urlroot = 'https://archive.stsci.edu/cgi-bin/dss_search?'
v = 'poss2ukstu_red'
e = 'J2000'
ra = str(1) + '%3A' + str(2) + '%3A' + str(03.3)
de = '%2B' + str(0) + '%3A' + str(1) + '%3A' + str(02.2)
h = str(10)
w = str(10)
f = 'fits'
urlend = '&c=none&s=on&fov=NONE&v3='
result = re.get(urlroot+'v='+v+'&r='+ra+'&d='+de+'&e='+e+'&h='+h+'&w='+w+'&f='+f+urlend, stream=True)

with open(data_dir + 'dss_image.fits', 'wb') as out_file:
    shutil.copyfileobj(result.raw, out_file)
del result


## Query on vizier and save to FITS bin table
# Query setup
Vizier.ROW_LIMIT = -1
target = 'NGC6121' # Another name: M4
r = 0.4

# Making query
# 2MASS catalog
tmc = Vizier.query_region(target, radius=r*u.deg, catalog='II/246/out') 
# PPMXL proper motion
ppmxl = Vizier.query_region(target, radius=r*u.deg, catalog='I/317/sample') 

# Get catalog only
jhk = tmc[0]
pm = ppmxl[0]

# Retrive data only from 2MASS columns [3,4,9,11,13,15,17,19,21]
col1 = fits.Column(name=jhk.colnames[3], format=jhk.dtype[3], array=jhk[jhk.colnames[3]])
col2 = fits.Column(name=jhk.colnames[4], format=jhk.dtype[4], array=jhk[jhk.colnames[4]])
col3 = fits.Column(name=jhk.colnames[9], format=jhk.dtype[9], array=jhk[jhk.colnames[9]])
col4 = fits.Column(name=jhk.colnames[11], format=jhk.dtype[11], array=jhk[jhk.colnames[11]])
col5 = fits.Column(name=jhk.colnames[13], format=jhk.dtype[13], array=jhk[jhk.colnames[13]])
col6 = fits.Column(name=jhk.colnames[15], format=jhk.dtype[15], array=jhk[jhk.colnames[15]])
col7 = fits.Column(name=jhk.colnames[17], format=jhk.dtype[17], array=jhk[jhk.colnames[17]])
col8 = fits.Column(name=jhk.colnames[19], format=jhk.dtype[19], array=jhk[jhk.colnames[19]])

cols = fits.ColDefs([col1, col2, col3, col4, col5, col6, col7, col8])
tmc_hdu = fits.BinTableHDU.from_columns(cols)
tmc_hdu.writeto(data_dir + '/' + target + '_tmc.fits')<commit_msg>Add try: except: to query on vizier.<commit_after>import os
import lxml
import shutil
import requests as re
import astropy.units as u
from astroquery.vizier import Vizier
from astropy.io import fits
from astropy.coordinates import SkyCoord
from bs4 import BeautifulSoup


# Create the download file directory.
data_dir = os.path.expanduser('~/astro_data/')
if not os.path.exists(data_dir):
    os.mkdir(data_dir)


## Download digital sky survey fits.
urlroot = 'https://archive.stsci.edu/cgi-bin/dss_search?'
v = 'poss2ukstu_red'
e = 'J2000'
ra = str(1) + '%3A' + str(2) + '%3A' + str(03.3)
de = '%2B' + str(0) + '%3A' + str(1) + '%3A' + str(02.2)
h = str(10)
w = str(10)
f = 'fits'
urlend = '&c=none&s=on&fov=NONE&v3='
result = re.get(urlroot+'v='+v+'&r='+ra+'&d='+de+'&e='+e+'&h='+h+'&w='+w+'&f='+f+urlend, stream=True)

with open(data_dir + 'dss_image.fits', 'wb') as out_file:
    shutil.copyfileobj(result.raw, out_file)
del result


## Query on vizier and save to FITS bin table
# Query setup
Vizier.ROW_LIMIT = -1
target = 'M4' # Another name: M4
r = 0.01

# Making query
# 2MASS catalog
try:
    tmc = Vizier.query_region(target, radius = r*u.deg, catalog = 'II/246/out')
except:
    payloads = {'-r': (None, 'SNV'), '-name': (None, target)}
    results = re.post('http://cds.u-strasbg.fr/cgi-bin/Sesame', data = payloads)
    soup = BeautifulSoup(results.text, 'lxml')
    pos = SkyCoord(soup.find_all('pre')[0].string.split('')[0], frame='icrs', unit=(u.hourangle, u.deg))
    tmc = Vizier.query_region(pos, radius=r*u.deg, catalog='II/246/out')

# PPMXL proper motion
#ppmxl = Vizier.query_region(target, radius=r*u.deg, catalog='I/317/sample') 

# Get catalog only
jhk = tmc[0]
#pm = ppmxl[0]

# Retrive data only from 2MASS columns [3,4,9,11,13,15,17,19,21]
col1 = fits.Column(name=jhk.colnames[3], format=jhk.dtype[3], array=jhk[jhk.colnames[3]])
col2 = fits.Column(name=jhk.colnames[4], format=jhk.dtype[4], array=jhk[jhk.colnames[4]])
col3 = fits.Column(name=jhk.colnames[9], format=jhk.dtype[9], array=jhk[jhk.colnames[9]])
col4 = fits.Column(name=jhk.colnames[11], format=jhk.dtype[11], array=jhk[jhk.colnames[11]])
col5 = fits.Column(name=jhk.colnames[13], format=jhk.dtype[13], array=jhk[jhk.colnames[13]])
col6 = fits.Column(name=jhk.colnames[15], format=jhk.dtype[15], array=jhk[jhk.colnames[15]])
col7 = fits.Column(name=jhk.colnames[17], format=jhk.dtype[17], array=jhk[jhk.colnames[17]])
col8 = fits.Column(name=jhk.colnames[19], format=jhk.dtype[19], array=jhk[jhk.colnames[19]])

cols = fits.ColDefs([col1, col2, col3, col4, col5, col6, col7, col8])
tmc_hdu = fits.BinTableHDU.from_columns(cols)
tmc_hdu.writeto(data_dir + target + '_tmc.fits')
{'fraction_non_alphanumeric': 0.19250566193123328, 'fraction_numerical': 0.047354333950998556, 'mean_word_length': 5.654794520547945, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6412067', 'n_tokens_mistral': 2237, 'n_tokens_neox': 2112, 'n_words': 384}