#! /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)
# phase calibration
selfcal vis=CS004.B0218.mir 'select=source(0205+322),-auto' interval=30 line=wide,1,2 refant=7
uvcat vis=CS004.B0218.mir 'select=source(0205+322),-auto,win(2)' out=0205+322
uvcat vis=CS004.B0218.mir 'select=source(3C454.3),-auto,win(2)' out=3C454.3
### OK if no previous bandpass in 3c454.3
uvcat vis=CS004.B0218.mir 'select=source(URANUS),-auto,win(2)' out=URANUS options=nocal
### nocal why ? maybe I guess why later

invert vis=0205+322 line=channel,1,2,13,1 map=0205+322.phase.map beam=0205+322.phase.beam

### I would have used line-wide,1,2,1,1 sam as in selfcal, but should be almost same as line=channel,1,2,13,1
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:
#*************************************************
#Source  1, Object type: gaussian
#  Peak value:                  1.989     +/-  3.7390E-02
#  Total integrated flux:       1.972
#  Offset Position (arcsec):      -0.004     0.013
#  Positional errors (arcsec):     0.045     0.029
#  Right Ascension:                2:05:04.925
#  Declination:                    32:12:30.106
#  Major axis (arcsec):           5.529 +/-  0.105
#  Minor axis (arcsec):           3.532 +/-  0.067
#  Position angle (degrees):     -87.85 +/-       1.67
#  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
#1.00  0.87  0.00  0.79  1.08  1.01  1.27  0.96  0.96  1.06  1.00  0.00  0.96  1.07  0.90

gplist vis=CS004.B0218.mir options=replace jyperk=@media
uvcat vis=CS004.B0218.mir '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.897     +/-  1.3670E-02
#  Total integrated flux:       1.911
#  Offset Position (arcsec):      -0.007     0.016
#  Positional errors (arcsec):     0.017     0.011
#  Right Ascension:                2:05:04.925
#  Declination:                    32:12:30.109
#  Major axis (arcsec):           5.519 +/-  0.040
#  Minor axis (arcsec):           3.594 +/-  0.026
#  Position angle (degrees):     -83.27 +/-       0.67
#  Deconvolved Major, minor axes (arcsec):    0.511    0.138
#  Deconvolved Position angle (degrees):      60.6
#-------------------------------------------------
### amplitude calib made 0205 a little less like a point source !
#*****************************************************************
# add bandpass calibration
mfcal vis=3C454.3 line=channel,15,1,1,1 interval=9999 refant=7 
gpcopy vis=3C454.3 out=0205+322.gain options=nocal
uvcat vis=0205+322.gain out=0205+322.pb
### this is tricky. uvio averages channel,1,2,13,1, before the bandpass
so bandpass is not applied to the one averaged output channel from uvio
invert vis=0205+322.pb line=channel,1,2,13,1 map=0205+322.pb.map beam=0205+322.pb.beam
clean map=0205+322.pb.map beam=0205+322.pb.beam out=0205+322.pb.cl
restor map=0205+322.pb.map beam=0205+322.pb.beam model=0205+322.pb.cl out=0205+322.pb.cm

#result:
#*******************************************************************
#Source  1, Object type: gaussian
#  Peak value:                  1.883     +/-  1.2665E-02
#  Total integrated flux:       1.907
#  Offset Position (arcsec):      -0.015     0.004
#  Positional errors (arcsec):     0.016     0.011
#  Right Ascension:                2:05:04.925
#  Declination:                    32:12:30.097
#  Major axis (arcsec):           5.527 +/-  0.038
#  Minor axis (arcsec):           3.611 +/-  0.025
#  Position angle (degrees):     -82.77 +/-       0.63
#  Deconvolved Major, minor axes (arcsec):    0.531    0.446
#  Deconvolved Position angle (degrees):     -49.4
#-------------------------------------------------
#**********************************************:***********************
