import urllib
import subprocess
from astropy.io import fits
import os

f = open('list_fits_files_sdss.csv')
content = f.readlines()
f.close()

cont_length=len(content)
i=0
for row in content:
    i=i+1
    arr=row.split(',')
    os.chdir((arr[2].replace('\n','')).replace(' ',''))
    framename=(arr[3].replace('\n','')).replace(' ','')
    hdulist = fits.open(framename)
    fits_info = hdulist[3].data[0]
    print str(i),' of ',str(cont_length),' -  ',framename

    run = fits_info['RUN']
    rerun = fits_info['RERUN']
    camcol = fits_info['CAMCOL']
    field = fits_info['FIELD']
    filt = fits_info['FILTER']
    file_name='photoField-'+str(run).zfill(6)+'-'+str(camcol)+'.fits'
    
    urllib.urlretrieve('https://data.sdss.org/sas/dr13/eboss/photoObj/'+str(rerun)+'/'+str(run)+'/'+file_name, file_name)
    

    fpm_filename = 'fpM-'+str(run).zfill(6)+'-'+filt+str(camcol)+'-'+str(field).zfill(4)+'.fit.gz'
    url_fpm = 'https://data.sdss.org/sas/dr13/eboss/photo/redux/'+str(rerun)+'/'+str(run)+'/objcs/'+str(camcol)+'/'+fpm_filename
    urllib.urlretrieve(url_fpm, fpm_filename)

    subprocess.call('gzip -d -f '+fpm_filename, shell=True)


    f = open('frame_info','w')
    f.write('photo_field: '+file_name+'\n') 
    f.write('fpM_file: '+fpm_filename.replace('.gz','')+'\n')

    f.close()
    os.chdir('../../../')