#! /bin/csh

set FILE=CS004.B0218.1.mir
set CAL=0205+322
set PBCAL=3C454.3
set NOISE=NOISE
set FLUX=URANUS

foreach i ($CAL $PBCAL $NOISE $FLUX)
  rm -rf $i.1
  uvcat vis=$FILE out=$i.1 select="source("$i"),-auto"
end

# no need to break into sources since you can select=source at any time.
# flagging and calibration are easier if all sources are in same file.

uvflag vis=3C454.3.1 'select=time(07JUL14:10:05:00,07JUL14:10:11:24.3)' flagval=flag
uvflag vis=0205+322.1 'select=time(07JUL14:11:12:00,07JUL14:11:16:02)' flagval=flag

uvflag vis=0205+322.1 'select=ant(1)(2)' line=channel,3,30,1,1 flagval=flag
uvflag vis=0205+322.1 'select=ant(1)(2)' line=channel,3,171,1,1 flagval=flag
uvflag vis=0205+322.1 'select=ant(1)(6)' line=channel,3,30,1,1 flagval=flag
uvflag vis=0205+322.1 'select=ant(1)(6)' line=channel,3,171,1,1 flagval=flag

uvflag vis=3C454.3.1 'select=ant(1)(2)' line=channel,3,30,1,1 flagval=flag
uvflag vis=3C454.3.1 'select=ant(1)(2)' line=channel,3,171,1,1 flagval=flag
uvflag vis=3C454.3.1 'select=ant(1)(6)' line=channel,3,30,1,1 flagval=flag
uvflag vis=3C454.3.1 'select=ant(1)(6)' line=channel,3,171,1,1 flagval=flag

uvflag vis=URANUS.1 'select=time(07JUL14:09:44:30,07JUL14:09:44:40)' flagval=flag

mfcal vis=URANUS.1 interval=9999 refant=7 line=channel,282,1,1,1
mfcal vis=3C454.3.1 interval=9999 refant=7 line=channel,282,1,1,1


rm -rf *.pb media1.tmp media1
#gpcopy vis=3C454.3.1 out=URANUS.1 options=nocal

#apply pb to uranus and 3c454, different pb, each derived from own data.

uvcat vis=URANUS.1 options=nocal 'select=win(2)' out=URANUS.1.pb
uvcat vis=3C454.3.1 options=nocal 'select=win(2)' out=3C454.3.1.pb

# not sure what above was about ?

# copy pb from 3c454 and apply to 0205

gpcopy vis=3C454.3.1 out=0205+322.1 options=nocal
uvcat vis=0205+322.1 out=0205+322.1.pb 'select=win(2)' options=nocal

# get flux scale from uranus

selfcal vis=URANUS.1.pb refant=7 interval=1 options=apriori,amp,noscale line=channel,1,2,13,1

gplist vis=URANUS.1.pb options=zeropha,amp > media1.tmp
grep Medians media1.tmp | tr -d Medians: > media1
cat media1

# phase selfcal on 0205 and 3c454

selfcal vis=0205+322.1.pb line=channel,1,2,13,1 interval=15 refant=7
selfcal vis=3C454.3.1.pb line=channel,1,2,13,1 interval=0.2 refant=7

# change to flux scale obtained from uranus

gplist vis=3C454.3.1.pb options=replace jyperk=@media1
gplist vis=0205+322.1.pb options=replace jyperk=@media1

rm -rf *.map *.beam *.cl *.cm

invert vis=0205+322.1.pb line=channel,1,2,13,1 map=0205+322.1.map beam=0205+322.1.beam
clean map=0205+322.1.map beam=0205+322.1.beam out=0205+322.1.cl
restor map=0205+322.1.map beam=0205+322.1.beam model=0205+322.1.cl out=0205+322.1.cm

invert vis=3C454.3.1.pb line=channel,1,2,13,1 map=3C454.3.1.map beam=3C454.3.1.beam
clean map=3C454.3.1.map beam=3C454.3.1.beam out=3C454.3.1.cl
restor map=3C454.3.1.map beam=3C454.3.1.beam model=3C454.3.1.cl out=3C454.3.1.cm


end:
