#! /bin/csh

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

uvflag vis=$FILE 'select=source(B0218),-auto' 'select=time(07JUL14:10:21:30,07JUL14:10:26:00)' flagval=flag
uvflag vis=$FILE 'select=source(B0218),-auto' 'select=time(07JUL14:11:32:00,07JUL14:11:52:00)' flagval=flag
uvflag vis=$FILE 'select=source(3C454.3),-auto' 'select=time(07JUL14:10:05:00,07JUL14:10:11:24.3)' flagval=flag
uvflag vis=$FILE 'select=source(0205+322),-auto' 'select=time(07JUL14:11:12:00,07JUL14:11:16:02)' flagval=flag
uvflag vis=$FILE 'select=source(0205+322),-auto' 'select=time(07JUL14:10:05:00,07JUL14:10:20:00)' flagval=flag

uvflag vis=$FILE 'select=source(0205+322),-auto' 'select=ant(1)(2)' line=channel,3,30,1,1 flagval=flag
uvflag vis=$FILE 'select=source(0205+322),-auto' 'select=ant(1)(2)' line=channel,3,171,1,1 flagval=flag
uvflag vis=$FILE 'select=source(0205+322),-auto' 'select=ant(1)(6)' line=channel,3,30,1,1 flagval=flag
uvflag vis=$FILE 'select=source(0205+322),-auto' 'select=ant(1)(6)' line=channel,3,171,1,1 flagval=flag

uvflag vis=$FILE 'select=source(3C454.3),-auto' 'select=ant(1)(2)' line=channel,3,30,1,1 flagval=flag
uvflag vis=$FILE 'select=source(3C454.3),-auto' 'select=ant(1)(2)' line=channel,3,171,1,1 flagval=flag
uvflag vis=$FILE 'select=source(3C454.3),-auto' 'select=ant(1)(6)' line=channel,3,30,1,1 flagval=flag
uvflag vis=$FILE 'select=source(3C454.3),-auto' 'select=ant(1)(6)' line=channel,3,171,1,1 flagval=flag

uvflag vis=$FILE 'select=source(B0218),-auto' 'select=ant(1)(2)' line=channel,3,30,1,1 flagval=flag
uvflag vis=$FILE 'select=source(B0218),-auto' 'select=ant(1)(2)' line=channel,3,171,1,1 flagval=flag
uvflag vis=$FILE 'select=source(B0218),-auto' 'select=ant(1)(6)' line=channel,3,30,1,1 flagval=flag
uvflag vis=$FILE 'select=source(B0218),-auto' 'select=ant(1)(6)' line=channel,3,171,1,1 flagval=flag

uvflag vis=$FILE 'select=source(URANUS),-auto' 'select=time(07JUL14:09:44:30,07JUL14:09:44:40)' flagval=flag

# only check with the 500MHz in lsb: win(2)

#do bandpass cal  on URANUS first
uvcat vis=CS004.B0218.mir 'select=source(URANUS),-auto' out=URANUS.pb
mfcal vis=URANUS.pb line=channel,282,1,1,1 interval=9999 refant=7
gpcopy vis=URANUS.pb out=CS004.B0218.mir options=nocal
uvcat vis=CS004.B0218.mir out=CS004.B0218.mir.pb options=nocal
# phase calibration
selfcal vis=CS004.B0218.mir.pb 'select=source(0205+322),-auto' interval=30 line=wide,1,2 refant=7
uvcat vis=CS004.B0218.mir.pb 'select=source(0205+322),-auto,win(2)' out=0205+322
uvcat vis=CS004.B0218.mir.pb 'select=source(URANUS),-auto,win(2)' out=URANUS options=nocal

invert vis=0205+322 line=channel,1,2,13,1 map=0205+322.phase.map beam=0205+322.phase.beam
clean map=0205+322.phase.map beam=0205+322.phase.beam out=0205+322.phase.cl
restor map=0205+322.phase.map beam=0205+322.phase.beam model=0205+322.phase.cl out=0205+322.phase.cm
### bandpass multiplies up edge channels so flux is higher
#result:
#*********************************************************
#Source  1, Object type: gaussian
#  Peak value:                  2.358     +/-  9.2435E-02
#  Total integrated flux:       2.266
#  Offset Position (arcsec):      -0.030    -0.011
#  Positional errors (arcsec):     0.097     0.055
#  Right Ascension:                2:05:04.923
#  Declination:                    32:12:30.081
#  Major axis (arcsec):           5.817 +/-  0.229
#  Minor axis (arcsec):           3.254 +/-  0.134
#  Position angle (degrees):     -86.56 +/-       2.62
### Warning:  Failed in attempting to deconvolve
#-------------------------------------------------
#********************************************************


# improve the result with amplitude calibration

### two things: i) vis=URANUS does not have pb applied
selfcal vis=URANUS refant=7 interval=1 options=apriori,amp,noscale line=channel,1,2,13,1
### ii) larger range in median gains. I do not use gplist.

gplist vis=URANUS options=zeropha,amp > media.tmp
grep Medians media.tmp | tr -d Medians: > media
cat media
#0.86  0.87  0.00  0.81  0.98  0.57  1.34  0.93  0.83  0.95  0.90  0.00  0.87  0.99  0.84

gplist vis=CS004.B0218.mir.pb options=replace jyperk=@media
uvcat vis=CS004.B0218.mir.pb 'select=source(0205+322),-auto,win(2)' out=0205+322.gain
invert vis=0205+322.gain line=channel,1,2,13,1 map=0205+322.gain.map beam=0205+322.gain.beam
clean map=0205+322.gain.map beam=0205+322.gain.beam out=0205+322.gain.cl
restor map=0205+322.gain.map beam=0205+322.gain.beam model=0205+322.gain.cl out=0205+322.gain.cm

#result
#*******************************************************
#Source  1, Object type: gaussian
#  Peak value:                  1.726     +/-  3.5321E-02
#  Total integrated flux:       1.741
#  Offset Position (arcsec):      -0.031    -0.007
#  Positional errors (arcsec):     0.049     0.031
#  Right Ascension:                2:05:04.923
#  Declination:                    32:12:30.086
#  Major axis (arcsec):           5.640 +/-  0.117
#  Minor axis (arcsec):           3.523 +/-  0.073
#  Position angle (degrees):     -83.32 +/-       1.72
#  Deconvolution appears to produce a point source
#-------------------------------------------------
#********************************************************
