def main():
  iso25D = 40 / 0.396
  #dataDir = '/media/46F4A27FF4A2713B_/work2/data/'
  dataDir = '../data'
  band = setBand()
  #outputFile = dataDir+'/growthCurvePhotometry.csv'
  listFile = dataDir+'/SDSS_photo_match.csv'
  fitsDir = dataDir+'/SDSS/'
  #filledDir = 'filled_'+band+'/'
  filledDir = 'filled2/'
  imgDir = 'img/'+band
  simpleFile = dataDir+'/CALIFA_mother_simple.csv'
  maskFile = dataDir+'/maskFilenames.csv'  
  dataFile = 'list.txt'
  
  for ID in range(1, 940):
  
	  	    print 'id,', int(ID)
		    ra = GalaxyParameters.SDSS(listFile, int(ID)).ra
		    dec = GalaxyParameters.SDSS(listFile, int(ID)).dec
		    run = GalaxyParameters.SDSS(listFile, int(ID)).run
		    rerun = GalaxyParameters.SDSS(listFile, int(ID)).rerun
		    camcol = GalaxyParameters.SDSS(listFile, int(ID)).camcol		    
		    field = GalaxyParameters.SDSS(listFile, int(ID)).field
		    runstr = GalaxyParameters.SDSS(listFile, int(ID)).runstr
		    field_str = sdss.field2string(field)

		    print 'getting header info...'
		    rFile = fitsDir+'r/fpC-'+runstr+'-r'+camcol+'-'+field_str+'.fit.gz'

		    WCS=astWCS.WCS(rFile)	
		    pa = utils.convert(db.dbUtils.getFromDB('pa', dbDir+'CALIFA.sqlite', 'nadine', ' where califa_id = '+str(ID)))[0][0]
		    pa_align = utils.convert(db.dbUtils.getFromDB('pa_align', dbDir+'CALIFA.sqlite', 'nadine', ' where califa_id = '+str(ID)))[0][0]
		    ang = WCS.getRotationDeg() 
		    print ang
		    
		    pa_align2 = (360 - ang) - 90 + pa
		    #if pa_align2 >= 180:
		#	    pa_align2 = pa_align2 - 180
		 #   if pa_align2 <= 0:
		#	    pa_align2 = pa_align2 + 180
		    out = int(ID), pa, pa_align2
		    print out
		    utils.writeOut(out, "align_pa.csv")
def main():
  iso25D = 40 / 0.396
  #dataDir = '/media/46F4A27FF4A2713B_/work2/data/'
  dataDir = '../data'
  band = setBand()
  outputFile = dataDir+'/growthCurvePhotometry.csv'
  listFile = dataDir+'/SDSS_photo_match.csv'
  fitsDir = dataDir+'/SDSS/'
  #filledDir = 'filled_'+band+'/'
  filledDir = 'filled2/'
  imgDir = 'img/'+band
  simpleFile = dataDir+'/CALIFA_mother_simple.csv'
  maskFile = dataDir+'/maskFilenames.csv'  
  dataFile = 'list.txt'
  
  csvReader = csv.reader(open(dataFile, "rU"), delimiter=',')
  
  
  for row in csvReader:
	
	ID = int(string.strip(row[0]))
	ra = string.strip(row[1])
	dec = string.strip(row[2])
	run = string.strip(row[3])
	rerun = string.strip(row[4])
	camcol = string.strip(row[5])
	field = string.strip(row[6])
	runstr = sdss.run2string(run)
	field_str = sdss.field2string(field)

	#checkInput(listFile, dataDir, ID, run, rerun, camcol, runstr, band, field_str, fitsDir)
	#checkOutput(dataDir, ID, run, rerun, camcol, runstr, band, field_str, fitsDir, filledDir)
  
  
 
  #missing = np.genfromtxt('missing_'+setBand()+'.csv')
  #print missing, 'missing'  
  #ID = missing
  #csvReader = csv.reader(open(dataFile, "rU"), delimiter=',')
  #for row in csvReader:  	
  #	ID = string.strip(row[0])
	#CHECK THE DELIMITERS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	
  #	if (int(ID) in x for x in missing):
  
  #	  print ID, 'did you check the delimiters?'
  #for i, ID in enumerate(missing):
  	if ID == 577:
	  	    print 'id,', int(ID) - 1	
		    ra = GalaxyParameters.SDSS(listFile, int(ID) - 1).ra
		    dec = GalaxyParameters.SDSS(listFile, int(ID) - 1).dec
		    run = GalaxyParameters.SDSS(listFile, int(ID) - 1).run
		    rerun = GalaxyParameters.SDSS(listFile, int(ID) - 1).rerun
		    camcol = GalaxyParameters.SDSS(listFile, int(ID) - 1).camcol		    
		    field = GalaxyParameters.SDSS(listFile, int(ID) - 1).field
		    runstr = GalaxyParameters.SDSS(listFile, int(ID) - 1).runstr
		    field_str = sdss.field2string(field)
		    print ID
		    inFile = fitsDir+band+'/fpC-'+runstr+'-'+band+camcol+'-'+field_str+'.fit.gz'
		    
		    print inFile, 'input'
		    
		    gz = gzip.open(inFile)
		    imgFile = pyfits.open(gz)
		    img = imgFile[0].data
		    head = imgFile[0].header

		    print 'getting header info...'
		    rFile = fitsDir+'r/fpC-'+runstr+'-r'+camcol+'-'+field_str+'.fit.gz'
		    rgz = gzip.open(rFile)
		    imgFiler = pyfits.open(rgz)
		    try:
		      maskFile = pyfits.open(GalaxyParameters.getMaskUrl(listFile, dataDir, simpleFile, int(ID)-1))
		      mask = maskFile[0].data
		      print mask.shape
		      WCSr=astWCS.WCS(rFile)	
		      WCS=astWCS.WCS(inFile)
		      band_center = WCS.wcs2pix(WCS.getCentreWCSCoords()[0], WCS.getCentreWCSCoords()[1]) #'other band image center coords in r image coordinate system'		
		      r_center = WCS.wcs2pix(WCSr.getCentreWCSCoords()[0], WCSr.getCentreWCSCoords()[1]) #'r center coords in r image coordinate system'		  
		      shift = [band_center[0] - r_center[0], band_center[1] - r_center[1]]
		      print shift, 'shift'
		      #print type(shift)
		      #note the swap!
		      shift = [ceil(shift[1]), ceil(shift[0])]
		      print shift, img.shape
		      img = sdss.getShiftedImage(img, shift)
		      mask = sdss.getShiftedImage(mask, [-1*shift[0], -1*shift[1]])
		      outputFilename = dataDir+'/'+filledDir+'fpC-'+runstr+'-'+band+camcol+'-'+field_str+'.fits'
		      print outputFilename

		      info = (ID, shift, outputFilename)
		      Interpolation.callInpaint(img, mask, outputFilename, int(ID - 1))
		      utils.writeOut(info, band+'_interp_log.csv')
		    
		    except IOError as e:
		      utils.writeOut((ID, e), band+'_fill_errors.csv')
		      pass
def main():
    iso25D = 40 / 0.396
    # dataDir = '/media/46F4A27FF4A2713B_/work2/data/'
    dataDir = "../data"
    band = setBand()
    outputFile = dataDir + "/growthCurvePhotometry.csv"
    listFile = dataDir + "/SDSS_photo_match.csv"
    fitsDir = dataDir + "/SDSS/"
    filledDir = "filled_" + band + "/"

    imgDir = "img/" + band
    simpleFile = dataDir + "/CALIFA_mother_simple.csv"
    maskFile = dataDir + "/maskFilenames.csv"
    dataFile = "list.txt"

    csvReader = csv.reader(open(dataFile, "rU"), delimiter=",")

    """
  for row in csvReader:

	ID = int(string.strip(row[0]))
	ra = string.strip(row[1])
	dec = string.strip(row[2])
	run = string.strip(row[3])
	rerun = string.strip(row[4])
	camcol = string.strip(row[5])
	field = string.strip(row[6])
	runstr = sdss.run2string(run)
	field_str = sdss.field2string(field)
	outputFilename = dataDir+'/'+filledDir+'fpC-'+runstr+'-'+band+camcol+'-'+field_str+'.fits'

	try:
	  f = open(GalaxyParameters.getSDSSUrl(listFile, dataDir, ID-1))
	  print 'it is here', ID - 1
	  pass
	except IOError as e: 
	  print e, ID - 1
	  os.system('wget -P '+fitsDir+band+' http://das.sdss.org/imaging/'+run+'/'+rerun+'/corr/'+camcol+'/fpC-'+runstr+'-'+band+camcol+'-'+field_str+'.fit.gz')     
	  pass

	try:
	  f = open(fitsDir+'r/fpC-'+runstr+'-r'+camcol+'-'+field_str+'.fit.gz')
	  print 'it is here', 'r', ID - 1
	  pass
	except IOError as e:  
	  print e, 'r', ID - 1
	  os.system('wget -P '+fitsDir+'r/ http://das.sdss.org/imaging/'+run+'/'+rerun+'/corr/'+camcol+'/fpC-'+runstr+'-r'+camcol+'-'+field_str+'.fit.gz')
	  pass

   #checking output file
	try:
	  f = open(outputFilename)
	  print 'musu', ID
	except IOError as e:
	  utils.writeOut(str(ID))
	  pass
	  """
    missing = np.genfromtxt("missing.csv")
    for row in csvReader:

        # print '********************************', row[0]
        ID = string.strip(row[0])
        # CHECK THE DELIMITERS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        if (ID in x for x in missing):

            print ID, "did you check the delimiters?"
            if int(ID) >= 250:
                ra = string.strip(row[1])
                dec = string.strip(row[2])
                run = string.strip(row[3])
                rerun = string.strip(row[4])
                camcol = string.strip(row[5])
                field = string.strip(row[6])
                runstr = sdss.run2string(run)
                field_str = sdss.field2string(field)
                print ID
                inFile = fitsDir + band + "/fpC-" + runstr + "-" + band + camcol + "-" + field_str + ".fit.gz"
                gz = gzip.open(inFile)
                imgFile = pyfits.open(gz)
                img = imgFile[0].data
                head = imgFile[0].header

                print "getting header info..."
                rFile = fitsDir + "r/fpC-" + runstr + "-r" + camcol + "-" + field_str + ".fit.gz"
                rgz = gzip.open(rFile)
                imgFiler = pyfits.open(rgz)
                try:
                    maskFile = pyfits.open(GalaxyParameters.getMaskUrl(listFile, dataDir, simpleFile, int(ID) - 1))
                    mask = maskFile[0].data
                    print mask.shape
                    WCSr = astWCS.WCS(rFile)
                    WCS = astWCS.WCS(inFile)
                    band_center = WCS.wcs2pix(
                        WCS.getCentreWCSCoords()[0], WCS.getCentreWCSCoords()[1]
                    )  #'other band image center coords in r image coordinate system'
                    r_center = WCS.wcs2pix(
                        WCSr.getCentreWCSCoords()[0], WCSr.getCentreWCSCoords()[1]
                    )  #'r center coords in r image coordinate system'
                    shift = [band_center[0] - r_center[0], band_center[1] - r_center[1]]
                    print type(shift)
                    # note the swap!
                    shift = [ceil(shift[1]), ceil(shift[0])]
                    print shift, img.shape
                    img = sdss.getShiftedImage(img, shift)
                    mask = sdss.getShiftedImage(mask, [-1 * shift[0], -1 * shift[1]])
                    outputFilename = (
                        dataDir + "/" + filledDir + "fpC-" + runstr + "-" + band + camcol + "-" + field_str + ".fits"
                    )
                    print outputFilename

                    info = (ID, shift, outputFilename)
                    utils.writeOut(info)
                    Interpolation.callInpaint(img, mask, outputFilename, ID)
                    utils.writeOut(info)

                except IOError as e:
                    utils.writeOut((ID, e))
                    pass
            else:
                print "passing", ID
                pass