# Define the AIPS tasks to use in the e-MERLIN pipeline.
# mkargo 2011

import os
from AIPS import AIPS, AIPSDisk
from AIPSTask import AIPSTask, AIPSList
from AIPSData import AIPSUVData, AIPSImage, AIPSCat
from tasks import *
import tasks

def runfitld(datain, indisk):
	fitld = AIPSTask('FITLD')
	fitld.datain = datain		# "datain" is correct here (use "indata" in most other places)
	fitld.outdisk = indisk
	fitld.digicor = -1
	fitld.douvcomp = -1
	fitld.go()


def runmsort(indata):
	print 'Running MSORT.'
	msort = AIPSTask('MSORT')
	msort.indata = indata
	msort.outdata = indata
	msort.sort = 'TB'
	msort.go()

def runindxr(indata):
	print 'Running INDXR.'
	indxr = AIPSTask('INDXR')
	indxr.indata = indata
	indxr.go()

def runfring(indata, calsour, timer, docalib, gainuse, flagver, doband, bpver, refant, refantlist, solint, aparm, dparm, snver, snr, bchan, echan):
	print 'Running FRING on ', format(calsour)
	fring = AIPSTask('FRING')
	fring.indata = indata
	fring.calsour[1:] = calsour
	fring.timer[1:] = timer
	fring.docalib = docalib
	fring.gainuse = gainuse
	fring.flagver = flagver
	fring.doband = doband
	fring.bpver = bpver
	fring.refant = refant
	fring.search[1:] = refantlist
	fring.solint = solint
	fring.cmethod = 'DFT'
        fring.aparm[1:] = aparm
        fring.aparm[7] = snr
        fring.dparm[1:] = dparm
	fring.snver = snver
	fring.bchan = bchan
	fring.echan = echan
	fring.go()

def runsnplt(indata, inext, invers, sources, nplots, optype, dotv):
	snplt = AIPSTask('SNPLT')
	snplt.indata = indata
	snplt.inext= inext
	snplt.invers = invers
	snplt.sources[1:] = sources
	snplt.nplots = nplots
	snplt.optype = optype
	snplt.dotv = dotv
	snplt.go()

def runlwpla(indata, outfile):
	plver=0
	for tab in indata.tables :
		if 'PL' in tab[1] :
			plver = plver + 1
	if os.path.exists(outfile) :
		os.remove(outfile)
	lwpla = AIPSTask('LWPLA')
	lwpla.indata = indata
	lwpla.plver = 1
	lwpla.inver = plver
	lwpla.outfile = outfile
	lwpla.msgkill = -10
	lwpla.go()
	# Remove PL tables
	indata.zap_table('PL', -1)

def runclcal(indata, sources, calsour, opcode, interpol, snver, inver, gainver, gainuse, refant):
	clcal = AIPSTask('CLCAL')
	clcal.indata = indata
	clcal.sources[1:] = sources
	clcal.calsour[1:] = calsour
	clcal.opcode = opcode
	clcal.interpol = interpol
	clcal.snver = snver
	clcal.inver = inver
	clcal.gainver = gainver
	clcal.gainuse = gainuse
	clcal.refant = refant
	clcal.go()


def runpossm(indata, sources, timer, anten, basel, aparm, bparm, bif, eif, docalib, gainuse, flagver, stokes, doband, bpver, codetype, solint, nplots, dotv, freqid):
	possm = AIPSTask('POSSM')
	possm.indata = indata
	possm.sources[1:] = sources
	possm.timer[1:] = timer
	possm.aparm[1:] = aparm[1:]
	possm.bparm[1:] = bparm[1:]
	possm.antennas[1:] = anten
	possm.baseline[1:] = basel
	possm.bif =  bif
	possm.eif = eif
	possm.docalib = docalib
	possm.gainuse = gainuse
	possm.flagver = flagver
	possm.stokes = stokes
	possm.doband = doband
	possm.bpver = bpver
	possm.codetype = codetype
	possm.solint = solint
	possm.nplots = nplots
	possm.dotv = dotv
	possm.freqid = 1
	possm.go()

def runbpass(indata, calsour, refant, bpassprm, bif, eif, soltyp, solint, docal):
	bpass = AIPSTask('BPASS')
	bpass.indata = indata
	bpass.calsour[1:] = calsour[1:]
	bpass.refant = refant
	bpass.bpassprm[1:] = bpassprm[1:]
	bpass.bif = bif
	bpass.eif = eif
	bpass.soltype = soltyp
	bpass.solint = solint
	bpass.docal = docal
	bpass.go()

def runsetjy(indata, sources, bif, eif, zerosp, optype):
	setjy = AIPSTask('SETJY')
	setjy.indata = indata
	setjy.sources[1:] = sources
	setjy.bif = bif
	setjy.eif = eif
	setjy.zerosp[1:] = zerosp, 0, 0, 0
	setjy.optype = optype
	setjy.go()

def runcalib(indata, calsour, timer, uvrang, docalib, gainuse, flagver, doband, bpver, cmethod, refant, solint, aparm, doflag, soltype, solmode, minamper, minphser, cparm, snver, antwt, weightit):
	calib = AIPSTask('CALIB')
	calib.indata = indata
	calib.calsour[1:] = calsour
	calib.timer[1:] = timer[1:]
	calib.uvrange[1:] = uvrang[1:]
	calib.docalib = docalib
	calib.gainuse = gainuse
	calib.flagver = flagver
	calib.doband = doband
	calib.bpver = bpver
	calib.cmethod = cmethod
	calib.refant = refant
	calib.solint = solint
	if(solmode == 'P'):
		calib.aparm[1] = 3
	else:
		calib.aparm[1] = 4
	calib.aparm[6] = 3
	calib.aparm[7] = 5
	calib.cparm[1] = 10
	calib.doflag = doflag
	calib.soltype = soltype
	calib.solmode = solmode
	calib.minamper = minamper
	calib.minphser = minphser
	calib.cparm[1:] = cparm[1:]
	calib.snver = snver
	calib.antwt[1:] = antwt[1:]
	calib.weightit = weightit
	calib.go()


def runselfcalib(indata, in2data, calsour, timer, uvrang, docalib, gainuse, flagver, doband, bpver, cmethod, refant, solint, aparm, doflag, soltype, solmode, minamper, minphser, cparm, snver, antwt, weightit):
	# CALIB procedure for self-calibration
	calib = AIPSTask('CALIB')
	calib.indata = indata
	calib.in2data = in2data
	calib.calsour[1:] = calsour
	calib.timer[1:] = timer[1:]
	calib.uvrange[1:] = uvrang[1:]
	calib.docalib = docalib
	calib.gainuse = gainuse
	calib.flagver = flagver
	calib.doband = doband
	calib.bpver = bpver
	calib.cmethod = cmethod
	calib.refant = refant
	calib.solint = solint
#	calib.aparm[1:] = aparm[1:]
	if(solmode == 'P'):
		calib.aparm[1] = 3
	else:
		calib.aparm[1] = 4
	calib.aparm[6] = 3
	calib.aparm[7] = 5
	calib.cparm[1] = 10
	calib.doflag = doflag
	calib.soltype = soltype
	calib.solmode = solmode
	calib.minamper = minamper
	calib.minphser = minphser
	calib.cparm[1:] = cparm[1:]
	calib.snver = snver
	calib.antwt[1:] = antwt[1:]
	calib.weightit = weightit
	calib.go()



def runuvplt(indata, sources, stokes, timer, anten, basel, bif, eif, docalib, gainuse, flagver, doband, bpver, aparm, bparm, doweight, refant, dotv):
	uvplt = AIPSTask('UVPLT')
	uvplt.indata = indata
	uvplt.sources = sources
	uvplt.stokes = stokes
	uvplt.timerang = timer
	uvplt.antennas[1:] = anten
	uvplt.baseline[1:] = basel
	uvplt.bif = bif
	uvplt.eif = eif
	uvplt.docalib = docalib
	uvplt.gainuse = gainuse
	uvplt.flagver = flagver
	uvplt.doband = doband
	uvplt.bpver = bpver
	uvplt.aparm[1:] = aparm[1:]
	uvplt.bparm[1:] = bparm[1:]
	uvplt.doweight = doweight
	uvplt.refant = refant
	uvplt.dotv = dotv
	uvplt.go()



def runvplot(indata, sources, stokes, timer, anten, basel, bif, eif, bchan, echan, docalib, gainuse, flagver, doband, bpver, aparm, bparm, refant, dotv, nplots):
	vplot = AIPSTask('VPLOT')
	vplot.indata = indata
	vplot.sources[1:] = sources
	vplot.stokes = stokes
	vplot.timerang[1:] = timer
	vplot.antennas[1:] = anten
	vplot.baseline[1:] = basel
	vplot.bchan = bchan
	vplot.echan = echan
	vplot.avgchan = 1
	vplot.bif = bif
	vplot.eif = eif
	vplot.docalib = docalib
	vplot.gainuse = gainuse
	vplot.flagver = flagver
	vplot.doband = doband
	vplot.bpver = bpver
	vplot.aparm[1:] = aparm
	vplot.bparm[1:] = bparm
	vplot.crowded = 0
	vplot.refant = refant
	vplot.dotv = dotv
	vplot.xinc = 1
	vplot.nplots = nplots
	vplot.go()


def rungetjy(indata, sources, calsour, bif, eif, snver):
	getjy = AIPSTask('GETJY')
	getjy.indata = indata
	getjy.sources[1:] = sources
	getjy.calsour[1:] = calsour
	getjy.bif = bif
	getjy.eif = eif
	getjy.snver = snver
#	getjy.inp()
	getjy.go()


def runimagr(indata, sources, docalib, gainuse, flagver, doband, bpver, bchan, echan, nchav, chinc, cellsiz, imsiz, niter, dotv, outdisk):
	imagr = AIPSTask('IMAGR')
	imagr.indata = indata
	imagr.sources[1:] = sources
	imagr.outname = sources[0]
	imagr.outdisk = outdisk
	imagr.docalib = docalib
	imagr.gainuse = gainuse
	imagr.flagver = flagver
	imagr.doband = doband
	imagr.bpver = bpver
	imagr.bchan = bchan
	imagr.echan = echan
	imagr.nchav = nchav
	imagr.chinc = chinc
	imagr.cellsize[1:] = cellsiz
	imagr.imsize[1:] = imsiz
	imagr.niter = niter
	imagr.dotv = dotv
	imagr.go()


def runkntr(image, factor):
	imean = AIPSTask('IMEAN')
	imean.indata = image
	imean()
	kntr = AIPSTask('KNTR')
	kntr.dogrey = -1
	kntr.dovect = -1
	kntr.docont = 1
	kntr.indata = image
	kntr.blc[1:] = 0,0
	kntr.trc[1:] = 0,0
#	kntr.clev = 3*imean.pixstd
#	kntr.levs[1:] = -1,1,2,4,6,8,16,32,64,128
	kntr.cbpl = 16
	kntr.dotv = -1
	kntr.plev = 1
	rms = imean.pixstd
	peak = image.header.datamax
	# Set the kntr levels based on dynamic range
	firstlev = factor*abs(rms/peak)*300.
	kntr.levs[1] = firstlev * (-1.)
	kntr.levs[2] = firstlev
	i = 3
	while (kntr.levs[i-1]*2. < 100. and i<=30):
		kntr.levs[i] = 2 * kntr.levs[i - 1]
		i += 1
	kntr.go()


def aipsuvname(aipsdata):
	return aipsdata.name + '.' + aipsdata.klass + '.' + str(aipsdata.seq)

