#!/bin/csh

echo Calculate consistency of bima holograms with 4 ovro reference antennas

goto start

foreach ANT ( m8 m9 m10 m11 m12 m13 m14 m15 )
  rm -r $ANT.pha $ANT.amp
  imcat in="$ANT.r*.h.pha" out=$ANT.pha options=relax
  imcat in="$ANT.r*.h.amp" out=$ANT.amp options=relax
end


foreach ANT ( m8 m9 m10 m11 m12 m13 m14 m15 )
  cgdisp in="$ANT.amp,$ANT.pha" device=/xs type=pix,con region=quart labtyp=absnat levs1=-100,-50,0,50,100
end

foreach ANT ( m8 m9 m10 m11 m12 m13 m14 m15 )
  rm -r $ANT.amp.avemaths $ANT.pha.avemaths
  avmaths in="$ANT.amp" out="$ANT.amp.avemaths" options=subtract 'region=image(1,4)'
  avmaths in="$ANT.pha" out="$ANT.pha.avemaths" options=subtract 'region=image(1,4)'
end

foreach ANT ( m8 m9 m10 m11 m12 m13 m14 m15 )
  cgdisp in="$ANT.amp.avemaths,$ANT.pha.avemaths" device=/xs type=pix,con region=quart labtyp=absnat levs1=-100,-50,0,50,100
end

goto end

start:

echo Average over the 4 reference images

foreach ANT ( m8 m9 m10 m11 m12 m13 m14 m15 )
  rm -r $ANT.amp.ave $ANT.pha.ave
  avmaths in="$ANT.amp" out="$ANT.amp.ave" options=replace 'region=image(1,4)'
  avmaths in="$ANT.pha" out="$ANT.pha.ave" options=replace 'region=image(1,4)'
end

echo Interpolate holograms onto a fine grid to define pixel values close to adjusters.

foreach ANT ( m8 m9 m10 m11 m12 m13 m14 m15 )
  rm -r $ANT.amp.regrid $ANT.pha.regrid
  regrid in="$ANT.amp.ave" out="$ANT.amp.regrid" axes=1,2 desc=0,321,-6.942886,640,0,321,6.942886,640
  regrid in="$ANT.pha.ave" out="$ANT.pha.regrid" axes=1,2 desc=0,321,-6.942886,640,0,321,6.942886,640
end

echo Reverse direction of Y axis for BIMA holograms

foreach ANT ( m8 m9 m10 m11 m12 m13 m14 m15 )
  rm -r $ANT.amp.reorder $ANT.pha.reorder
  reorder in="$ANT.amp.regrid" out="$ANT.amp.reorder" mode=1-23
  reorder in="$ANT.pha.regrid" out="$ANT.pha.reorder" mode=1-23
end

foreach ANT ( m8 m9 m10 m11 m12 m13 m14 m15 )
  cgdisp in="$ANT.amp.regrid,$ANT.pha.regrid" device=/xs type=pix,con region=quart labtyp=absnat levs1=-200,-150,-100,-50,0,50,100,150,200
end

cgdisp device=m10.pha.reorder.ps/cps in=m10.pha.reorder,m10.pha.reorder type=pix,con region=quart labtyp=absnat levs1=29.4,58.8,88.2,117.6,147,176.4,205.8,235.2,264.6,294,323.4,352.8,-29.4,-58.8,-88.2,-117.6,-147,-176.4,-205.8,-235.2,-264.6,-294,-323.4,-352.8

cgdisp device=m12.pha.reorder.ps/cps in=m12.pha.reorder type=con region=quart labtyp=absnat levs1=29.4,58.8,88.2,117.6,147,176.4,205.8,235.2,264.6,294,323.4,352.8,-29.4,-58.8,-88.2,-117.6,-147,-176.4,-205.8,-235.2,-264.6,-294,-323.4,-352.8

end:
