#! /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 first
uvcat vis=CS004.B0218.mir 'select=source(3C454.3),-auto' out=3C454.3
mfcal vis=3C454.3 line=channel,282,1,1,1 interval=9999 refant=7
gpcopy vis=3C454.3 out=CS004.B0218.mir options=nocal

### could have done instead:
### mfcal vis=CS004.B0218.mir 'select=source(3C454.3),-auto' line=channel,282,1,1,1 interval=9999 refant=7
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

#result:
### 0205 has pb derived from 3c454.3 applied. has mutiplied up
### channels at edge of band so average is higher. 
#Source  1, Object type: gaussian
#  Peak value:                  2.318     +/-  3.6163E-02
#  Total integrated flux:       2.289
#  Offset Position (arcsec):      -0.030     0.000
#  Positional errors (arcsec):     0.036     0.024
#  Right Ascension:                2:05:04.923
#  Declination:                    32:12:30.093
#  Major axis (arcsec):           5.464 +/-  0.086
#  Minor axis (arcsec):           3.560 +/-  0.056
#  Position angle (degrees):     -83.87 +/-       1.45
#  Deconvolution appears to produce a point source
#-------------------------------------------------
#********************************************************

# improve the result with amplitude calibration
selfcal vis=URANUS refant=7 interval=1 options=apriori,amp,noscale line=channel,1,2,13,1
gplist vis=URANUS options=zeropha,amp > media.tmp
grep Medians media.tmp | tr -d Medians: > media
cat media
#0.86  0.85  0.00  0.82  0.88  0.96  1.20  0.95  0.79  0.98  0.91  0.00  0.86  1.01  0.85

### unanus also has pb from 3c454 applied, so average across band is higher and gains jyperk are lower. This effectively re-normalizes 0205 data.
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.894     +/-  1.3652E-02
#  Total integrated flux:       1.911
#  Offset Position (arcsec):      -0.031     0.000
#  Positional errors (arcsec):     0.017     0.011
#  Right Ascension:                2:05:04.923
#  Declination:                    32:12:30.093
#  Major axis (arcsec):           5.538 +/-  0.040
#  Minor axis (arcsec):           3.589 +/-  0.026
#  Position angle (degrees):     -83.29 +/-       0.66
#  Deconvolved Major, minor axes (arcsec):    0.654    0.160
#  Deconvolved Position angle (degrees):      78.2
#-------------------------------------------------
#********************************************************
