
# script to image individual lines
# first split the channels where the line is,
# and substract continuum before imaging

# NOTE the images in the archive of the whole spws are of 
# lower spectral resolution and the continuum was not substracted!

# selected the channels to split in plotms with the original spectral resolution
split(vis = 'uid___A002_Xa0b40d_X3cb8_spw1.ms',
      outputvis = 'iras16293_CH3OH.ms', datacolumn = 'data', 
      field = 'IRAS16293-2422', spw = '0:1200~1550')





#----------------------------
# CH3OH (spw1)
# CH3OH vt=0 7( 1, 7)- 6( 1, 6)++ @ 335.58200 GHz

# plotms iras16293_CH3OH.ms in amp vs freq averaging time.
# transform frame=LSRK rest Freq 335729 
# visualize amp vs velocity ---> the source velocity ~3 km/s is visible
# the line is between -10 km/s and ~18 km/s remember this for clean
# plot amp vs channel to identify the continuum channels
# substract continuum:

uvcontsub(vis = 'iras16293_CH3OH.ms', field = 'IRAS16293-2422', 
          fitspw = '0:10~40;125~140;220~240;300~340')

# observed spectral velocity ~0.22 km/s
# image width 0.25 km/s
# start -10km/s 
# To cover 28 km/s with 0.25 resolution need 112 chans

# expected sensitivity from Sensitivity calculator
# dec -24:00:00
# Freq 335 GHz
# Bandwidth 0.25 km/s
# Integration time 14 min
# sensitivity 8 mJy 
# 


# before the final cleaning, I did a dirty image to check the channel seleccion was okay!
clean(vis = 'iras16293_CH3OH.ms.contsub',
  imagename = 'iras16293_CH3OH',
  field = 'IRAS16293-2422', 
  spw = '0',
  mode = 'velocity',
  nchan = 112, # modify depending on spectral resolution
  start = '-10km/s', # modify depending on spectral resolution
  width = '0.25km/s', # see notas_project, obtained in obs is ~0.22km/s
  restfreq = '335582.00MHz', 
  outframe = 'LSRK',
  interactive = T,
  #interactive = F,
  niter = 10000, 
  #niter = 0, # dirty image
  threshold = '24mJy', # use 3x8mJy (measured in dirty image in clean channel)
  imsize = [300, 300],
#imsize = [280, 280], 
  cell = '0.1arcsec', 
  weighting='briggs',
  robust=0.5)

# To make it fast just two masks around the two bright sources, no added masks around 
# the residuals appearing later.
# The threshold is reached after 500 iterations.
# The rms measured is ~6-7 mJy.


