#! /bin/csh -f
#jhz 2015-10-20 create
#jhz 2016-01-12 modify
#jhz 2016-01-27 finalize and document
#jhz 2016-03-03 modify for 2015B-S044, high spectral resolution in a hybrid
#               spectral-resolution mode for asic to image high-velocity resolution
#               CO(2-1) line from a brown dwarf   
#jhz 2016-03-07 update using a calibrated data sent by the PI
#jhz 2016-05-06 separate line and continuum               
echo "                                                                      "
echo "######################################################################"
echo "specially for the project 2015B-S044 - a narrow line system           "
echo "######################################################################"
#
#Target              (font) - 
#Bandpass calibrator (bcal) -
#Gain calibrator     (gcal, gcal2) -
#
set dt          = 151226 
echo $dt
set hour        =  04:53:17 
set sb          = usb
#set sb          = lsb
set nn          = 1 
set rxid        = 0
set rx          = rx${rxid}
set adge        =  5 
set nchan_as    = 1750
set maxtsys     = 500
set fname       = "$dt""_""$rx".$sb
set oname       = "$dt""_""$rx"
set fcal        = callisto 
set gcal1       = 0501-019
set gcal2       = 0607-085 
set gcal        = $gcal1  
set bcal        = 3c454.3
set font        = sOri-2
set refant      = 1 
set avetime     = 5
set source1     = ${font}
set DATPATH     = ../JHZ-PP2
set fname = ${DATPATH}/${fname}
###########################################
#set spectral line parameters
###########################################
#
#sline is the name of the line transition
#
if($sb == usb) then
set sline = CO21
#
#rfreq is the rest frequency in GHz
#
set rfreq = 230.5379700
endif
if($sb == lsb) then
set sline = SiO54
#
#rfreq is the rest frequency in GHz
#
set rfreq = 217.1049800
endif
#
#input calibrated uv file contains the line
#
set uvin = $fname.asic.tsys.bp.a
#
#target sources
#
set targets = $source1
#
#output calibrated uv file contains the line
#
set uvout = ${dt}.${sline}.uv
#
#line free windows for the CO spw
#
set lfwinCO = 50,200,300,450
#line free windows for the SiO spw
set lfwinSiO = 1,450,451,1248
#
#continuum windows for ASIC
set cdwin = 1,890,891,1760
#
#image cube name
#
set imnameA = ${font}.${sline}
#
#linetyp for imaging line cube
#
set linetype = vel,400,-25,0.25    # imaging velocity in a range between -25 to 25 km/s 
                                   # in a resolution of 0.25 km/s
#
#imaging size and cell size
#
set ims   = 512
set cells = 0.5
#
#clean iter, and fwhm
#
set niter = 2500
set fwhm  = 4.,4.
set rms   = 0.025
#
#define a name for high resolution spectra data file 
#
set hirsp = "$dt""_""$rx".$sb.hi
#define a name for the continuum file
set contd = "$dt""_""$rx".$sb.ch0
#
goto INSPECT         # inspect the data calibrated in pre-processing 2
#goto UVSPC-ASIC      # inspect calibrated uv spectra for ASIC data
#goto UVSPC-SWARM     # inspect calibrated uv spectra for SWARM data
#goto HSRUV           # get hi resolution spc
#goto LSRUV           # get lo resolution spc 
#goto UVLIN           # separate the line from the vis data
                      # cut the specific line data from the calibrated data set
#goto BLFLAG          # flag bad data points for ASIC chunks
#
$<
INSPECT:
echo 'INSPECT: ASIC'
uvindex  vis=$fname.asic.tsys.bp.a
$<
UVSPC-ASIC:
echo 'check spectra: ASIC -' $gcal2
smauvspec vis=$fname.asic.tsys.bp.a \
        select='source('$gcal2')' \
        interval=1000 hann=5 options=nobase,avall \
        axis=freq,both device=/xs nxy=1,1
$<
echo 'check spectra: ASIC -' $gcal
smauvspec vis=$fname.asic.tsys.bp.a \
        select='source('$gcal')' \
        interval=1000 hann=5 options=nobase,avall \
        axis=freq,both device=/xs nxy=1,1
$<
#echo 'check spectra: ASIC -' $fcal
#smauvspec vis=$fname.asic.tsys.bp.a \
#        select='source('$fcal')' \
#        interval=1000 hann=5 \
#        axis=freq,both device=/xs nxy=1,1 options=nobase,avall
$<
echo 'check spectra: ASIC -' $targets
smauvspec vis=$fname.asic.tsys.bp.a \
        select='source('$targets')' \
        interval=1000 hann=5 \
        axis=freq,both device=/xs nxy=1,1 options=nobase,avall
$<
HSRUV:
\rm -r $hirsp 
uvaver vis=$uvin select='window(40)' out=$hirsp
$<
LSRUV:
set lorsp = "$dt""_""$rx".$sb.lo
\rm -r $lorsp
uvaver vis=$uvin select='window(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39)' out=$lorsp
$<
UVLIN:
echo 'check spectra of uvdata: ' $hirsp
smauvspec vis=$hirsp \
        select='source('$targets')' \
        interval=1000 hann=5 options=nobase,avall \
        axis=freq,both device=/xs nxy=1,1
$<
smauvspec vis=$hirsp \
        select='source('$targets')' \
        interval=1000 hann=5 options=nobase,avall \
        axis=vel,both device=/xs nxy=1,1
$<
smauvspec vis=$hirsp \
        select='source('$targets')' \
        interval=1000 hann=5 options=nobase,avall \
        axis=chan,both device=/xs nxy=1,1
$<
if($sb == usb) then 
\rm -r $uvout 
echo 'line-free windows -' $lfwinCO
uvlin vis=$hirsp chans=$lfwinCO order=1 \
       mode=line select='source('$targets')' \
       out=$uvout options=nowin
endif
$<
if($sb == lsb) then
\rm -r $uvout    
echo 'line-free windows -' $lfwinSiO
uvlin vis=$lorsp chans=$lfwinSiO order=1 \
       mode=line select='source('$targets')' \
       out=$uvout options=nowin
endif
$<
\rm -r $contd 
uvlin vis=$uvin chans=$cdwin order=1 \
      mode=chan0 select='source('$targets')' \
       out=$contd options=nowin
$<
echo 'check spectra of continuum free uvdata: ' $uvout
smauvspec vis=$uvout \
        select='source('$targets')' \
        interval=1000 hann=5 options=nobase,avall \
        axis=freq,both device=/xs nxy=1,1
$<
smauvspec vis=$uvout \
        select='source('$targets')' \
        interval=1000 hann=5 options=nobase,avall \
        axis=vel,both device=/xs nxy=1,1
$<
smauvspec vis=$uvout \
        select='source('$targets')' \
        interval=1000 hann=5 options=nobase,avall \
        axis=chan,both device=/xs nxy=1,1
$<
BLFLAG:
echo 'BLFLAG -' $uvout
smablflag vis=$uvout axis=time,ampl device=/xs
$<
smauvspec vis=$uvout \
        select='source('$targets')' \
        interval=1000 hann=15 options=nobase,avall \
        axis=vel,both device=/xs nxy=1,1

exit