from astropy.io import fits
from astropy.wcs import WCS
from astropy.coordinates import SkyCoord
from astropy import units as u
import os

files = [f for f in os.listdir('.') if os.path.isfile(f)]
for f in files:
    print f
    #filename = 'spitzer/50069931.50069931-15.IRAC.3.median_mosaic.fits'
    filename = f
    hdulist = fits.open(filename)
    dat = hdulist[0].data
    shp = dat.shape
    w = WCS(filename)
    f = open('all_lsf_psg.cat', 'r')
    f_assoc = open('assoc', 'aw')
    for line in f:
        data_arr = line.split('   ')
        data_arr[2] = data_arr[2].replace('\n','')
        x, y = w.wcs_world2pix(float(data_arr[1]), float(data_arr[2]),0)
        if (x>0) and (y>0) and (y<shp[0]) and (x<shp[1]):
            if dat[int(y)][int(x)] >0:
                print x,y,dat[int(y)][int(x)]
                f_assoc.write(str(x)+'  '+str(y)+'  '+data_arr[0]+'\n')