from 	astropy import coordinates as coord
from 	astropy import wcs
from 	astropy.io import fits
from 	astropy import units as u
from 	misc import bcolors
import	numpy as np
import	os

def convert_hms_dd(RA, DEC):

	'''
	Convert HMS to DD system
	'''

	if (':' in RA) and (':' in DEC):
		Coord_dd	= coord.SkyCoord(RA, DEC, unit=(u.hour,u.degree), frame='icrs')
		RA_dd		= Coord_dd.ra.deg
		Dec_dd		= Coord_dd.dec.deg

	elif (not (':' in RA) and not (':' in DEC)) and (('.' in RA) and ('.' in DEC)):
		RA_dd, Dec_dd	= float(RA), float(DEC)

	else:
		print(bcolors.FAIL + 'Coordinates have wrong format.' + bcolors.ENDC)
		sys.exit()

	return RA_dd, Dec_dd

def get_header(FILE, KEYWORD):

	'''
	Get keyword from fits file
	'''

	header	= fits.getheader(FILE)
	return header[KEYWORD]

def pix2arcsec(FITS):

	'''
	Get pixel scale
	'''

	hdu		= fits.open(FITS)
	if len(hdu) > 1:
		header	= fits.getheader(FITS, 0)
		header	+= fits.getheader(FITS, 1)
	else:
		header	= fits.getheader(FITS)

	hdu_wcs			= wcs.WCS(header)
	return np.median(wcs.utils.proj_plane_pixel_scales(hdu_wcs)) * 3600
	

def sky2xy (FITS, RA=False, DEC=False, CAT=None):

	'''
	Coordinate transformation: sky -> xy
	'''

	if CAT == None:
		if RA != False and  DEC != False:
			cmd=('sky2xy %s %s %s | grep -v off' %(FITS, RA, DEC))
			program_call = os.popen(cmd)
			xy = []
			for line in program_call:
				xy=np.array(line.strip().split()[-2:]).astype(float)
			if len(xy) > 0:
				return xy

	else:
		cmd 	=("more %s | awk '{print $1,$2}' > %s" %(CAT, CAT.replace(CAT.split('.')[-1], 'reg')))
		os.system(cmd)
		cmd 	= ("sky2xy %s @%s | grep -v off | awk '{print $5, $6}'" %(FITS, CAT.replace(CAT.split('.')[-1], 'reg')))
		cat 	= os.popen(cmd)


		xy	= []
		for line in cat:
			xy.append(list(map(float, line.replace('\n', '').split())))

		return np.array(xy)

def xy2sky (FITSFILE,X,Y):

	'''
	Coordinate transformation: xy -> sky
	'''

	program_call = os.popen('xy2sky %s %s %s' %(FITSFILE, X, Y))
	sky = []

	for line in program_call:
		sky.append(line.strip().split()[:2])

	return sky

[((35, 10, 35, 30), 'astropy.io.fits.getheader', 'fits.getheader', ({(35, 25, 35, 29): 'FILE'}, {}), '(FILE)', False, 'from astropy.io import fits\n'), ((44, 8, 44, 23), 'astropy.io.fits.open', 'fits.open', ({(44, 18, 44, 22): 'FITS'}, {}), '(FITS)', False, 'from astropy.io import fits\n'), ((51, 13, 51, 28), 'astropy.wcs.WCS', 'wcs.WCS', ({(51, 21, 51, 27): 'header'}, {}), '(header)', False, 'from astropy import wcs\n'), ((90, 16, 90, 61), 'os.popen', 'os.popen', ({(90, 25, 90, 60): "'xy2sky %s %s %s' % (FITSFILE, X, Y)"}, {}), "('xy2sky %s %s %s' % (FITSFILE, X, Y))", False, 'import os\n'), ((16, 13, 16, 74), 'astropy.coordinates.SkyCoord', 'coord.SkyCoord', (), '', True, 'from astropy import coordinates as coord\n'), ((46, 11, 46, 34), 'astropy.io.fits.getheader', 'fits.getheader', ({(46, 26, 46, 30): 'FITS', (46, 32, 46, 33): '0'}, {}), '(FITS, 0)', False, 'from astropy.io import fits\n'), ((47, 12, 47, 35), 'astropy.io.fits.getheader', 'fits.getheader', ({(47, 27, 47, 31): 'FITS', (47, 33, 47, 34): '(1)'}, {}), '(FITS, 1)', False, 'from astropy.io import fits\n'), ((49, 11, 49, 31), 'astropy.io.fits.getheader', 'fits.getheader', ({(49, 26, 49, 30): 'FITS'}, {}), '(FITS)', False, 'from astropy.io import fits\n'), ((73, 2, 73, 16), 'os.system', 'os.system', ({(73, 12, 73, 15): 'cmd'}, {}), '(cmd)', False, 'import os\n'), ((75, 9, 75, 22), 'os.popen', 'os.popen', ({(75, 18, 75, 21): 'cmd'}, {}), '(cmd)', False, 'import os\n'), ((82, 9, 82, 21), 'numpy.array', 'np.array', ({(82, 18, 82, 20): 'xy'}, {}), '(xy)', True, 'import numpy as np\n'), ((52, 18, 52, 60), 'astropy.wcs.utils.proj_plane_pixel_scales', 'wcs.utils.proj_plane_pixel_scales', ({(52, 52, 52, 59): 'hdu_wcs'}, {}), '(hdu_wcs)', False, 'from astropy import wcs\n'), ((64, 18, 64, 31), 'os.popen', 'os.popen', ({(64, 27, 64, 30): 'cmd'}, {}), '(cmd)', False, 'import os\n')]