<commit_before>from __future__ import print_function
import sys, urllib
import numpy as np
import matplotlib.pyplot as plt
import pylab 
from matplotlib.ticker import MaxNLocator
import argparse
from astropy.io import votable
import os
import astropy.wcs
from astropy.io import fits
from astropy.coordinates import SkyCoord
from astropy import units as u
from scipy.ndimage.filters import gaussian_filter
import warnings
warnings.filterwarnings("ignore")

def deg2hour(ra, dec, sep=":"):
    
    if ( type(ra) is str and type(dec) is str ):
        return ra, dec
        
    c = SkyCoord(ra, dec, frame='icrs', unit='deg')
    
    ra = c.ra.to_string(unit=u.hourangle, sep=sep, precision=2, pad=True)
    dec = c.dec.to_string(sep=sep, precision=2, alwayssign=True, pad=True)
    
    return str(ra), str(dec)
    
    
def hour2deg(ra, dec):
    
    if ( type(ra) is float and type(dec) is float ):
        return ra, dec
        
    c = SkyCoord(ra, dec, frame='icrs', unit=(u.hourangle, u.deg))
    
    ra = c.ra.deg
    dec = c.dec.deg
    
    return ra, dec


def get_offset(ra1, dec1, ra2, dec2):


    '''
    Returns offset from ra1, dec1 position to ra2, dec2.
    
    returns ( East Offset [arcsec], 	North Offset [arcsec] )

    '''

    ra1, dec1 = hour2deg(ra1, dec1)
    ra2, dec2 = hour2deg(ra2, dec2)
    
    return np.round((ra2 - ra1) * np.cos(np.deg2rad(dec1))*3600,2), np.round((dec2-dec1)*3600, 2)
    
def query_ps1_catalogue(ra, dec, radius_deg, minmag=15, maxmag=18.5):
    '''
    Sends a VO query to the PS1 catalogue.
    Filters the result by mangitude.
    '''

        
    url = "http://gsss.stsci.edu/webservices/vo/CatalogSearch.aspx?CAT=PS1V3OBJECTS&RA=%.5f&DEC=%.5f&SR=%.5f"%(ra, dec, radius_deg)
    
    u = urllib.urlopen(url)
    f = open("/tmp/ps1_cat.xml", "w")
    f.writelines(u.readlines())
    f.close()
    
    # Read RA, Dec and magnitude from XML format USNO catalog
    catalog = votable.parse_single_table("/tmp/ps1_cat.xml").to_table()
    
    #print (catalog["gMeanPSFMag"], catalog["rMeanPSFMag"])
    
    #mask = (catalog["nDetections"]>4) * ((catalog["rMeanPSFMag"] > minmag) * (catalog["rMeanPSFMag"] < maxmag)) | ((catalog["gMeanPSFMag"] > minmag) * (catalog["gMeanPSFMag"] < maxmag))

    mask = (catalog["nDetections"]>4) * (catalog["rMeanPSFMag"] > minmag) * (catalog["rMeanPSFMag"] < maxmag) #*(catalog["rMeanPSFMag"] > minmag) * (catalog["rMeanPSFMag"] < maxmag) 
    catalog = catalog[mask]

    
    newcat = np.zeros(len(catalog), dtype=[("ra", np.double), ("dec", np.double), ("mag", np.float)])
    newcat["ra"] = catalog["RAmean"]
    newcat["dec"] = catalog["DECmean"]
    newcat["mag"] = catalog["rMeanPSFMag"]
    
    #print (newcat)

    return newcat

def get_cutout(ra, dec, name, rad, debug=True):
    
    try:
        ra=float(ra)
        dec=float(dec)
    except:
        ra, dec = hour2deg(ra, dec) 
        
    catalog = query_ps1_catalogue(ra, dec, rad)
    
    if (debug):
        print (catalog)

    
    # Construct URL to download DSS image cutout, and save to tmp.fits
    image_index_url_red = 'http://ps1images.stsci.edu/cgi-bin/ps1filenames.py?ra={0}&dec={1}&filters=i'.format(ra, dec)
    image_index_url_green = 'http://ps1images.stsci.edu/cgi-bin/ps1filenames.py?ra={0}&dec={1}&filters=r'.format(ra, dec)
    image_index_url_blue = 'http://ps1images.stsci.edu/cgi-bin/ps1filenames.py?ra={0}&dec={1}&filters=g'.format(ra, dec)

    urllib.urlretrieve(image_index_url_red, '/tmp/image_index_red.txt')
    urllib.urlretrieve(image_index_url_green, '/tmp/image_index_green.txt')
    urllib.urlretrieve(image_index_url_blue, '/tmp/image_index_blue.txt')

    ix_red = np.genfromtxt('/tmp/image_index_red.txt', names=True, dtype=None)
    ix_green = np.genfromtxt('/tmp/image_index_green.txt', names=True, dtype=None)
    ix_blue = np.genfromtxt('/tmp/image_index_blue.txt', names=True, dtype=None)
    
    image_url = "http://ps1images.stsci.edu/cgi-bin/fitscut.cgi?red=%s&green=%s&blue=%s&filetypes=stack&auxiliary=data&size=%d&ra=%.6f&dec=%.6f"%\
    (ix_red["filename"], ix_green["filename"], ix_blue["filename"], rad*3600*4, ra, dec)

    if (debug):
        print (image_url)
        print ("Downloading PS1 r-band image...")
    urllib.urlretrieve(image_url, '/tmp/tmp_%s.jpg'%name)
    
    
def get_finder(ra, dec, name, rad, debug=False, starlist=None, telescope="P200", directory=".", minmag=15, maxmag=18.5, mag=np.nan):

        
    try:
        ra=float(ra)
        dec=float(dec)
    except:
        ra, dec = hour2deg(ra, dec) 


    catalog = query_ps1_catalogue(ra, dec, (rad/2.)*0.95, minmag=minmag, maxmag=maxmag)
    
    if (debug):
        print (catalog)

    
    if (len(catalog)<2):
        catalog = query_ps1_catalogue(ra, dec, (rad/2.)*0.95, minmag=minmag, maxmag=maxmag+0.5)

    if (len(catalog)<2):
        print ("Restarting with larger radius %.2f arcmin"%(rad*1.5*60))
        get_finder(ra, dec, name, rad*1.5, directory=directory, minmag=minmag, maxmag=maxmag+0.5, mag=mag, starlist=starlist, telescope=telescope)
        return
        
    if (not catalog is None and len(catalog)>0):
        np.random.shuffle(catalog)

    if (debug):
        print (catalog)

    no_self_object = (np.abs(catalog["ra"]-ra)*np.cos(np.deg2rad(dec))>2./3600)*(np.abs(catalog["dec"]-dec)>2./3600)
    catalog = catalog[no_self_object]
    
    
    # Construct URL to download DSS image cutout, and save to tmp.fits
    image_index_url = 'http://ps1images.stsci.edu/cgi-bin/ps1filenames.py?ra={0}&dec={1}&filters=r'.format(ra, dec)
    urllib.urlretrieve(image_index_url, '/tmp/image_index.txt')
    ix = np.genfromtxt('/tmp/image_index.txt', names=True, dtype=None)
    
    image_url = "http://ps1images.stsci.edu/cgi-bin/fitscut.cgi?red=%s&format=fits&size=%d&ra=%.6f&dec=%.6f"%(ix["filename"], rad*3600 *4, ra, dec)

    if (debug):
        print (image_url)
        print ("Downloading PS1 r-band image...")
    urllib.urlretrieve(image_url, '/tmp/tmp.fits')
    
    try:
        ps1_image = fits.open("/tmp/tmp.fits")
    #If there was an error with the fits, we shall go for the DSS image
    except IOError:
        try:<commit_msg>Bug with offsets fixed. Different filters chosen for the composite image.<commit_after>from __future__ import print_function
import sys, urllib
import numpy as np
import matplotlib.pyplot as plt
import pylab 
from matplotlib.ticker import MaxNLocator
import argparse
from astropy.io import votable
import os
import astropy.wcs
from astropy.io import fits
from astropy.coordinates import SkyCoord
from astropy import units as u
from scipy.ndimage.filters import gaussian_filter
import warnings
warnings.filterwarnings("ignore")

def deg2hour(ra, dec, sep=":"):
    
    if ( type(ra) is str and type(dec) is str ):
        return ra, dec
        
    c = SkyCoord(ra, dec, frame='icrs', unit='deg')
    
    ra = c.ra.to_string(unit=u.hourangle, sep=sep, precision=2, pad=True)
    dec = c.dec.to_string(sep=sep, precision=2, alwayssign=True, pad=True)
    
    return str(ra), str(dec)
    
    
def hour2deg(ra, dec):
    
    try:
        
        ra = float(ra)
        dec = float(dec)
        
    except:
        c = SkyCoord(ra, dec, frame='icrs', unit=(u.hourangle, u.deg))
        
        ra = c.ra.deg
        dec = c.dec.deg
    
    return ra, dec


def get_offset(ra1, dec1, ra2, dec2):


    '''
    Returns offset from ra1, dec1 position to ra2, dec2.
    
    returns ( East Offset [arcsec], 	North Offset [arcsec] )

    '''

    ra1, dec1 = hour2deg(ra1, dec1)
    ra2, dec2 = hour2deg(ra2, dec2)   
    
    return np.round((ra2 - ra1) * np.cos(np.deg2rad(dec1))*3600,2), np.round((dec2-dec1)*3600, 2)
    
def query_ps1_catalogue(ra, dec, radius_deg, minmag=15, maxmag=18.5):
    '''
    Sends a VO query to the PS1 catalogue.
    Filters the result by mangitude.
    '''

        
    url = "http://gsss.stsci.edu/webservices/vo/CatalogSearch.aspx?CAT=PS1V3OBJECTS&RA=%.5f&DEC=%.5f&SR=%.5f"%(ra, dec, radius_deg)
    
    u = urllib.urlopen(url)
    f = open("/tmp/ps1_cat.xml", "w")
    f.writelines(u.readlines())
    f.close()
    
    # Read RA, Dec and magnitude from XML format USNO catalog
    catalog = votable.parse_single_table("/tmp/ps1_cat.xml").to_table()
    
    #print (catalog["gMeanPSFMag"], catalog["rMeanPSFMag"])
    
    #mask = (catalog["nDetections"]>4) * ((catalog["rMeanPSFMag"] > minmag) * (catalog["rMeanPSFMag"] < maxmag)) | ((catalog["gMeanPSFMag"] > minmag) * (catalog["gMeanPSFMag"] < maxmag))

    mask = (catalog["nDetections"]>4) * (catalog["rMeanPSFMag"] > minmag) * (catalog["rMeanPSFMag"] < maxmag) #*(catalog["rMeanPSFMag"] > minmag) * (catalog["rMeanPSFMag"] < maxmag) 
    catalog = catalog[mask]

    
    newcat = np.zeros(len(catalog), dtype=[("ra", np.double), ("dec", np.double), ("mag", np.float)])
    newcat["ra"] = catalog["RAmean"]
    newcat["dec"] = catalog["DECmean"]
    newcat["mag"] = catalog["rMeanPSFMag"]
    
    #print (newcat)

    return newcat

def get_cutout(ra, dec, name, rad, debug=True):
    
    try:
        ra=float(ra)
        dec=float(dec)
    except:
        ra, dec = hour2deg(ra, dec) 
        
    catalog = query_ps1_catalogue(ra, dec, rad)
    
    if (debug):
        print (catalog)

    
    # Construct URL to download DSS image cutout, and save to tmp.fits
    image_index_url_red = 'http://ps1images.stsci.edu/cgi-bin/ps1filenames.py?ra={0}&dec={1}&filters=y'.format(ra, dec)
    image_index_url_green = 'http://ps1images.stsci.edu/cgi-bin/ps1filenames.py?ra={0}&dec={1}&filters=i'.format(ra, dec)
    image_index_url_blue = 'http://ps1images.stsci.edu/cgi-bin/ps1filenames.py?ra={0}&dec={1}&filters=g'.format(ra, dec)

    urllib.urlretrieve(image_index_url_red, '/tmp/image_index_red.txt')
    urllib.urlretrieve(image_index_url_green, '/tmp/image_index_green.txt')
    urllib.urlretrieve(image_index_url_blue, '/tmp/image_index_blue.txt')

    ix_red = np.genfromtxt('/tmp/image_index_red.txt', names=True, dtype=None)
    ix_green = np.genfromtxt('/tmp/image_index_green.txt', names=True, dtype=None)
    ix_blue = np.genfromtxt('/tmp/image_index_blue.txt', names=True, dtype=None)
    
    image_url = "http://ps1images.stsci.edu/cgi-bin/fitscut.cgi?red=%s&green=%s&blue=%s&filetypes=stack&auxiliary=data&size=%d&ra=%.6f&dec=%.6f&output_size=256"%\
    (ix_red["filename"], ix_green["filename"], ix_blue["filename"], rad*3600*4, ra, dec)

    if (debug):
        print (image_url)
        print ("Downloading PS1 r-band image...")
    urllib.urlretrieve(image_url, '/tmp/tmp_%s.jpg'%name)
    
    
def get_finder(ra, dec, name, rad, debug=False, starlist=None, telescope="P200", directory=".", minmag=15, maxmag=18.5, mag=np.nan):

        
    try:
        ra=float(ra)
        dec=float(dec)
    except:
        ra, dec = hour2deg(ra, dec) 

    catalog = query_ps1_catalogue(ra, dec, (rad/2.)*0.95, minmag=minmag, maxmag=maxmag)
    
    if (debug):
        print (catalog)

    
    if (len(catalog)<2):
        catalog = query_ps1_catalogue(ra, dec, (rad/2.)*0.95, minmag=minmag, maxmag=maxmag+0.5)

    if (len(catalog)<2):
        print ("Restarting with larger radius %.2f arcmin"%(rad*1.5*60))
        get_finder(ra, dec, name, rad*1.5, directory=directory, minmag=minmag, maxmag=maxmag+0.5, mag=mag, starlist=starlist, telescope=telescope)
        return
        
    if (not catalog is None and len(catalog)>0):
        np.random.shuffle(catalog)

    if (debug):
        print (catalog)

    no_self_object = (np.abs(catalog["ra"]-ra)*np.cos(np.deg2rad(dec))>2./3600)*(np.abs(catalog["dec"]-dec)>2./3600)
    catalog = catalog[no_self_object]
    
    print (catalog)
    
    # Construct URL to download DSS image cutout, and save to tmp.fits
    image_index_url = 'http://ps1images.stsci.edu/cgi-bin/ps1filenames.py?ra={0}&dec={1}&filters=r'.format(ra, dec)
    urllib.urlretrieve(image_index_url, '/tmp/image_index.txt')
    ix = np.genfromtxt('/tmp/image_index.txt', names=True, dtype=None)
    
    image_url = "http://ps1images.stsci.edu/cgi-bin/fitscut.cgi?red=%s&format=fits&size=%d&ra=%.6f&dec=%.6f"%(ix["filename"], rad*3600 *4, ra, dec)

    if (debug):
        print (image_url)
        print ("Downloading PS1 r-band image...")
    urllib.urlretrieve(image_url, '/tmp/tmp.fits')
    
    try:
        ps1_image = fits.open("/tmp/tmp.fits")
    #If there was an error with the fits, we shall go for the DSS image
    except IOError:
        try:
{'fraction_non_alphanumeric': 0.15737915529147262, 'fraction_numerical': 0.021920668058455117, 'mean_word_length': 3.332173913043478, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3055602', 'n_tokens_mistral': 4840, 'n_tokens_neox': 4629, 'n_words': 892}