casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
sdreduce.py
Go to the documentation of this file.
00001 #
00002 # This file was generated using xslt from its XML file
00003 #
00004 # Copyright 2009, Associated Universities Inc., Washington DC
00005 #
00006 import sys
00007 import os
00008 from  casac import *
00009 import string
00010 from taskinit import casalog
00011 #from taskmanager import tm
00012 import task_sdreduce
00013 def sdreduce(infile='', antenna=0, fluxunit='', telescopeparm='', specunit='', restfreq='', frame='', doppler='', calmode='none', fraction='10%', noff=-1, width=0.5, elongated=False, markonly=False, plotpointings=False, scanlist=[], field='', iflist=[], pollist=[], channelrange=[], average=False, scanaverage=False, timeaverage=False, tweight='none', averageall=False, polaverage=False, pweight='none', tau=0.0, kernel='none', kwidth=5, chanwidth='5', masklist=[], maskmode='auto', thresh=5.0, avg_limit=4, edge=[0], blfunc='none', order=5, npiece=2, applyfft=True, fftmethod='fft', fftthresh=3.0, addwn=[], rejwn=[], clipthresh=3.0, clipniter=0, verifycal=False, verifysm=False, verifybl=False, verbosebl=True, bloutput=True, blformat='', showprogress=True, minnrow=1000, outfile='', outform='ASAP', overwrite=False, plotlevel=0):
00014 
00015         """ASAP SD task: do sdcal, sdsmooth, and sdbaseline in one task
00016   Keyword arguments:
00017         infile -- name of input SD dataset
00018         antenna -- antenna name or id (only effective for MS input). 
00019         fluxunit -- units for line flux
00020                 options: 'K','Jy',''
00021                 default: '' (keep current fluxunit)
00022                 WARNING: For GBT data, see description below.
00023             >>> fluxunit expandable parameter
00024                  telescopeparm -- the telescope characteristics
00025                         options: (str) name or (list) list of gain info
00026                         default: '' (none set)
00027                         example: if telescopeparm='', it tries to get the telescope
00028                                  name from the data.
00029                                  Full antenna parameters (diameter,ap.eff.) known
00030                                  to ASAP are
00031                                  'ATPKSMB', 'ATPKSHOH', 'ATMOPRA', 'DSS-43',
00032                                  'CEDUNA','HOBART'. For GBT, it fixes default fluxunit
00033                                  to 'K' first then convert to a new fluxunit.
00034                                  telescopeparm=[104.9,0.43] diameter(m), ap.eff.
00035                                  telescopeparm=[0.743] gain in Jy/K
00036                                  telescopeparm='FIX' to change default fluxunit
00037                                  see description below
00038 
00039         specunit -- units for spectral axis
00040                 options: (str) 'channel','km/s','GHz','MHz','kHz','Hz',''
00041                 default: '' (=current)
00042                 example: this will be the units for masklist
00043             >>> specunit expandable parameters
00044                  restfreq -- rest frequency
00045                          available type includes float, int, string, list of float, 
00046                          list of int, list of string, and list of dictionary. the 
00047                          default unit of restfreq in case of float, int, or string 
00048                          without unit is Hz. string input can be a value only 
00049                          (treated as Hz) or a value followed by unit for which 'GHz',
00050                          'MHz','kHz',and 'Hz' are available. 
00051                          a list can be used to set different rest frequencies for 
00052                          each IF. the length of list input must be nIF. dictionary 
00053                          input should be a pair of molecule name and frequency with 
00054                          keys of 'name' and 'value', respectively. values in the 
00055                          dictionary input follows the same manner as for single 
00056                          float or string input. 
00057                          example: 345.796
00058                                   '1420MHz'
00059                                   [345.8, 347.0, 356.7]
00060                                   ['345.8MHz', '347.0MHz', '356.7MHz']
00061                                   [{'name':'CO','value':345}]
00062         frame -- frequency frame for spectral axis
00063                 options: (str) 'LSRK','REST','TOPO','LSRD','BARY',
00064                          'GEO','GALACTO','LGROUP','CMB'
00065                 default: currently set frame in scantable
00066                 WARNING: frame='REST' not yet implemented
00067         doppler -- doppler mode
00068                 options: (str) 'RADIO','OPTICAL','Z','BETA','GAMMA'
00069                 default: currently set doppler in scantable
00070         calmode -- calibration mode
00071                 options: 'ps','nod','fs','fsotf','quotient','none'
00072                 default: 'none'
00073                 example: choose mode 'none' if you have
00074                          already calibrated and want to
00075                          try baselines or averaging
00076             >>> calmode expandable parameter
00077                  fraction -- Edge marking parameter for 'otf' and 'otfraster'.
00078                              specify a number of OFF scans as a fraction of 
00079                              total number of data points. 
00080                          default: '10%'
00081                          options: '20%' in string style or float value less 
00082                                   than 1.0 (e.g. 0.15).
00083                                   'auto' is available only for 'otfraster'. 
00084                  noff -- Edge marking parameter for 'otfraster'.
00085                          It is used to specify a number of OFF scans near 
00086                          edge directly. Value of noff comes before setting 
00087                          by fraction.
00088                          default: -1 (use fraction)
00089                          options: any positive integer
00090                  width -- Edge marking parameter for 'otf'.
00091                           Pixel width with respect to a median spatial 
00092                           separation between neighboring two data in time.
00093                           Default will be fine in most cases.
00094                          default: 0.5
00095                          options: float value
00096                  elongated -- Edge marking parameter for 'otf'.
00097                               Set True only if observed area is elongeted 
00098                               in one direction.
00099                          default: False
00100                  markonly -- Set True if you want to save data just after 
00101                              edge marking (i.e. uncalibrated data) to see 
00102                              how OFF scans are defined.
00103                          default: False
00104         scanlist -- list of scan numbers to process
00105                 default: [] (use all scans)
00106                 example: [21,22,23,24]
00107                 this selection is in addition to field, iflist, and pollist
00108         field -- selection string for selecting scans by name
00109                 default: '' (no name selection)
00110                 example: 'FLS3a*'
00111                 this selection is in addition to scanlist, iflist, and pollist
00112         iflist -- list of IF id numbers to select
00113                 default: [] (use all IFs)
00114                 example: [15]
00115                 this selection is in addition to scanlist, field, and pollist
00116         pollist -- list of polarization id numbers to select
00117                 default: [] (use all polarizations)
00118                 example: [1]
00119                 this selection is in addition to scanlist, field, and iflist
00120         channelrange -- channel range selection
00121                 default: [] (use all channel)
00122                 example: [0,5000]
00123                 Note that specified values are recognized as 'channel' 
00124                 regardless of the value of specunit 
00125         average -- averaging on spectral data 
00126                 options: (bool) True,False
00127                 default: False
00128 
00129             >>>average expandable parameter
00130                  scanaverage -- average integrations within scans
00131                          options: (bool) True,False
00132                          default: False
00133                          example: if True, this happens in read-in
00134                          For GBT, set False!
00135                  timeaverage -- average times for multiple scan cycles
00136                          options: (bool) True,False
00137                          default: False
00138                          example: if True, this happens after calibration
00139                  tweight -- weighting for time average
00140                          options: 'none'
00141                                   'var'   (1/var(spec) weighted)
00142                                   'tsys'  (1/Tsys**2 weighted)
00143                                   'tint'  (integration time weighted)
00144                                   'tintsys'  (Tint/Tsys**2)
00145                                   'median'  ( median averaging)
00146                          default: 'none'
00147                  averageall -- average multi-resolution spectra
00148                                spectra are averaged by referring 
00149                                their frequency coverage
00150                          default: False
00151                  polaverage -- average polarizations
00152                          options: (bool) True,False
00153                          default: False
00154                  pweight -- weighting for polarization average
00155                          options: 'none'
00156                                   'var'  (1/var(spec) weighted)
00157                                   'tsys' (1/Tsys**2 weighted)
00158 
00159         tau -- atmospheric optical depth
00160                 default: 0.0 (no correction)
00161         kernel -- type of spectral smoothing
00162                 options: 'none','hanning','gaussian','boxcar','regrid'
00163                 default: 'none' (no smoothing)
00164 
00165             >>>kernel expandable parameter
00166                  kwidth -- width of spectral smoothing kernel
00167                          options: (int) in channels
00168                          default: 5
00169                          example: 5 or 10 seem to be popular for boxcar
00170                                   ignored for hanning (fixed at 5 chans)
00171                                   (0 will turn off gaussian or boxcar)
00172                  chanwidth -- channel width of regridded spectra
00173                          default: '5' (in channels)
00174                          example: '500MHz', '0.2km/s'
00175         masklist -- list or string of mask regions to INCLUDE in BASELINE fit
00176                     a string masklist allows per IF selection of channels.
00177                 default: [] (entire spectrum)
00178                 example: [[1000,3000],[5000,7000]]
00179                          '0:1000~3000;5000~7000, 1:200~350;450~600'
00180                          when maskmode is 'auto' or 'interact', this mask 
00181                          will be applied first before fitting as base mask
00182         maskmode -- mode of setting additional channel masks for baselining
00183                 options: (str) 'auto','list','interact'
00184                 default: 'auto'
00185                 example: maskmode='auto' runs linefinder to detect line regions 
00186                          to be excluded from fitting. this mode requires three 
00187                          expandable parameters: thresh, avg_limit, and edge.
00188                          USE WITH CARE! May need to tweak the expandable parameters.
00189                          maskmode='list' uses the given masklist only: 
00190                          no additional masks applied.
00191                          maskmode='interact' allows users to manually modify the 
00192                          mask regions by dragging mouse on the spectrum plotter GUI.
00193                          use LEFT or RIGHT button to add or delete regions, 
00194                          respectively.
00195 
00196             >>> maskmode expandable parameters
00197                  thresh -- S/N threshold for linefinder
00198                          default: 5
00199                          example: a single channel S/N ratio above which the channel is
00200                                   considered to be a detection
00201                  avg_limit -- channel averaging for broad lines
00202                          default: 4
00203                          example: a number of consecutive channels not greater than
00204                                   this parameter can be averaged to search for broad lines
00205                  edge -- channels to drop at beginning and end of spectrum
00206                          default: 0
00207                          example: [1000] drops 1000 channels at beginning AND end
00208                                   [1000,500] drops 1000 from beginning and 500 from end
00209 
00210                  Note: For bad baselines threshold should be increased,
00211                  and avg_limit decreased (or even switched off completely by
00212                  setting this parameter to 1) to avoid detecting baseline
00213                  undulations instead of real lines.
00214 
00215         blfunc -- baseline model function
00216                 options: (str) 'none','poly','chebyshev','cspline','sinusoid'
00217                 default: 'none' (no baselining)
00218                 example: blfunc='poly' uses a single polynomial line of 
00219                          any order which should be given as an expandable 
00220                          parameter 'order' to fit baseline. 
00221                          blfunc='chebyshev' uses Chebyshev polynomials. 
00222                          blfunc='cspline' uses a cubic spline function, a piecewise 
00223                          cubic polynomial having C2-continuity (i.e., the second 
00224                          derivative is continuous at the joining points).
00225                          blfunc='sinusoid' uses a combination of sinusoidal curves. 
00226             >>> blfunc expandable parameters
00227                  order -- order of baseline polynomial
00228                          options: (int) (<0 turns off baseline fitting)
00229                          default: 5
00230                          example: typically in range 2-9 (higher values
00231                                   seem to be needed for GBT)
00232                  npiece -- number of the element polynomials of cubic spline curve
00233                          options: (int) (<0 turns off baseline fitting)
00234                          default: 2
00235                  applyfft -- automatically set wave numbers of sinusoidal functions
00236                          for fitting by applying some method like FFT.
00237                          options: (bool) True, False
00238                          default: True
00239                  fftmethod -- method to be used when applyfft=True. Now only
00240                          'fft' is available and it is the default.
00241                  fftthresh -- threshold to select wave numbers to be used for
00242                          sinusoidal fitting. both (float) and (str) accepted.
00243                          given a float value, the unit is set to sigma.
00244                          for string values, allowed formats include:
00245                              'xsigma' or 'x' (= x-sigma level. e.g., '3sigma'), or
00246                              'topx' (= the x strongest ones, e.g. 'top5').
00247                          default is 3.0 (unit: sigma).
00248                  addwn -- additional wave number(s) of sinusoids to be used
00249                          for fitting.
00250                          (list) and (int) are accepted to specify every
00251                          wave numbers. also (str) can be used in case
00252                          you need to specify wave numbers in a certain range,
00253                          e.g., 'a-b' (= a, a+1, a+2, ..., b-1, b),
00254                                '<a'  (= 0,1,...,a-2,a-1),
00255                                '>=a' (= a, a+1, ... up to the maximum wave
00256                                       number corresponding to the Nyquist
00257                                       frequency for the case of FFT).
00258                          default: []
00259                  rejwn -- wave number(s) of sinusoid NOT to be used for fitting.
00260                          can be set just as addwn but has higher priority:
00261                          wave numbers which are specified both in addwn
00262                          and rejwn will NOT be used.
00263                          default: []
00264                  clipthresh -- clipping threshold for iterative fitting
00265                          default: 3
00266                  clipniter -- maximum iteration number
00267                          default: 0 (no iteration, i.e., no clipping)
00268 
00269         verifycal -- verify the results of calibration
00270                 options: (bool) True,False
00271                 default: False
00272                 WARNING: Currently verifying parameters just asks whether you 
00273                          accept the displayed calibraion/fit and if not, 
00274                          continues without doing any calibraion/baseline fit.
00275         verifysm -- verify the results of smoothing
00276                 options: (bool) True,False
00277                 default: False
00278                 Note: verification is not yet available for kernel='regrid'
00279         verifybl -- verify the results of baseline fitting
00280                 options: (bool) True,False
00281                 default: False
00282                 NOTE: Currently available only when blfunc='poly'
00283         verbosebl -- output fitting results to logger
00284                 default: True
00285                 example: If False, the fitting results including coefficients, 
00286                          residual rms, etc., are not output to the CASA logger, 
00287                          while the processing speed gets faster
00288         bloutput -- output fitting results to a text file
00289                 default: True
00290                 example: If False, the fitting results including coefficients, 
00291                          residual rms, etc., are not output to a text file 
00292                          (<outfile>_blparam.txt), while the processing 
00293                          speed gets faster
00294         blformat -- format of the logger output and text file specified with bloutput
00295                 options: '', 'csv'
00296                 default: '' (same as in the past, easy to read but huge)
00297         showprogress -- show progress status for large data
00298                 default: True
00299         minnrow -- minimum number of input spectra to show progress status
00300                 default: 1000
00301         outfile -- Name of output file
00302                 default: '' (<infile>_cal)
00303         outform -- format of output file
00304                 options: 'ASCII','SDFITS','MS','ASAP'
00305                 default: 'ASAP'
00306                 example: the ASAP format is easiest for further sd
00307                          processing; use MS for CASA imaging.
00308                          If ASCII, then will append some stuff to
00309                          the outfile name
00310         overwrite -- overwrite the output file if already exists
00311                 options: (bool) True,False
00312                 default: False
00313                 WARNING: if outform='ASCII', this parameter is ignored
00314         plotlevel -- control for plotting of results
00315                 options: (int) 0=none, 1=some, 2=more, <0=hardcopy
00316                 default: 0 (no plotting)
00317                 example: plotlevel<0 as abs(plotlevel), e.g.
00318                          -1 => hardcopy of final plot (will be named
00319                          <outfile>_calspec.eps)
00320                 WARNING: be careful plotting in fsotf mode!
00321 
00322 
00323         DESCRIPTION:
00324 
00325         Task sdreduce performs data selection, calibration, and/or spectral
00326         baseline fitting for single-dish spectra. This task internally calls the
00327         tasks, sdcal, sdsmooth, and sdbaseline and it can be used to run all the
00328         three steps in one task execution.
00329         By setting calmode='none', one can run sdreduce on already calibrated data,
00330         for further selection, averaging and atmospheric optical depth correction.
00331         To save the output spectra in a certain range of channels, you set the 
00332         range in channelrange.
00333 
00334         If you give multiple IFs in iflist, then your scantable will have
00335         multiple IFs by default. Averaging of multi-resolution (multi-IFs)
00336         spectra can be achieved by setting a sub-parameter of average, 
00337         averageall, to True. It handles multi-IFs by selecting overlaps in 
00338         frequency coverages and assigning new IFs in the output spectra.
00339 
00340         ASAP recognizes the data of the "AT" telescopes, but currently
00341         does not know about the GBT or any other telescope. This task
00342         does know about GBT. Telescope name is obtained from the data.
00343         If you wish to change the fluxunit (see below), by leaving
00344         the sub-parameter telescopeparm unset (telescopeparm=''),
00345         it will use internal telescope parameters for
00346         flux conversion for the data from AT telescopes and it will use an
00347         approximate aperture efficiency conversion for the GBT data.
00348         If you give telescopeparm a list, then if the list has a single float it
00349         is assumed to be the gain in Jy/K, if two or more elements they are assumed
00350         to be telescope diameter (m) and aperture efficiency
00351         respectively.
00352 
00353         Note that sdreduce assumes that the fluxunit is set correctly in
00354         the data already.  If not, then set telescopeparm='FIX' and it
00355         will set the default units to fluxunit without conversion.
00356         NOTE: If the data in infile is an ms from GBT and the default flux
00357         unit is missing and this task automatically fixes the default fluxunit
00358         to 'K' before the conversion.
00359 
00360         WARNING for the GBT raw SDFITS format data as input:
00361         SDtasks are able to handle GBT raw SDFITS format data since the 
00362         data filler is available. However, the functionality is not well 
00363         tested yet, so that there may be unknown bugs.  
00364 
00365   
00366         """
00367         if type(scanlist)==int: scanlist=[scanlist]
00368         if type(iflist)==int: iflist=[iflist]
00369         if type(pollist)==int: pollist=[pollist]
00370         if type(channelrange)==int: channelrange=[channelrange]
00371         if type(edge)==int: edge=[edge]
00372 
00373 #
00374 #    The following is work around to avoid a bug with current python translation
00375 #
00376         mytmp = {}
00377 
00378         mytmp['infile'] = infile
00379         mytmp['antenna'] = antenna
00380         mytmp['fluxunit'] = fluxunit
00381         mytmp['telescopeparm'] = telescopeparm
00382         mytmp['specunit'] = specunit
00383         mytmp['restfreq'] = restfreq
00384         mytmp['frame'] = frame
00385         mytmp['doppler'] = doppler
00386         mytmp['calmode'] = calmode
00387         mytmp['fraction'] = fraction
00388         mytmp['noff'] = noff
00389         mytmp['width'] = width
00390         mytmp['elongated'] = elongated
00391         mytmp['markonly'] = markonly
00392         mytmp['plotpointings'] = plotpointings
00393         mytmp['scanlist'] = scanlist
00394         mytmp['field'] = field
00395         mytmp['iflist'] = iflist
00396         mytmp['pollist'] = pollist
00397         mytmp['channelrange'] = channelrange
00398         mytmp['average'] = average
00399         mytmp['scanaverage'] = scanaverage
00400         mytmp['timeaverage'] = timeaverage
00401         mytmp['tweight'] = tweight
00402         mytmp['averageall'] = averageall
00403         mytmp['polaverage'] = polaverage
00404         mytmp['pweight'] = pweight
00405         mytmp['tau'] = tau
00406         mytmp['kernel'] = kernel
00407         mytmp['kwidth'] = kwidth
00408         mytmp['chanwidth'] = chanwidth
00409         mytmp['masklist'] = masklist
00410         mytmp['maskmode'] = maskmode
00411         mytmp['thresh'] = thresh
00412         mytmp['avg_limit'] = avg_limit
00413         mytmp['edge'] = edge
00414         mytmp['blfunc'] = blfunc
00415         mytmp['order'] = order
00416         mytmp['npiece'] = npiece
00417         mytmp['applyfft'] = applyfft
00418         mytmp['fftmethod'] = fftmethod
00419         mytmp['fftthresh'] = fftthresh
00420         mytmp['addwn'] = addwn
00421         mytmp['rejwn'] = rejwn
00422         mytmp['clipthresh'] = clipthresh
00423         mytmp['clipniter'] = clipniter
00424         mytmp['verifycal'] = verifycal
00425         mytmp['verifysm'] = verifysm
00426         mytmp['verifybl'] = verifybl
00427         mytmp['verbosebl'] = verbosebl
00428         mytmp['bloutput'] = bloutput
00429         mytmp['blformat'] = blformat
00430         mytmp['showprogress'] = showprogress
00431         mytmp['minnrow'] = minnrow
00432         mytmp['outfile'] = outfile
00433         mytmp['outform'] = outform
00434         mytmp['overwrite'] = overwrite
00435         mytmp['plotlevel'] = plotlevel
00436         pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/'
00437         trec = casac.utils().torecord(pathname+'sdreduce.xml')
00438 
00439         casalog.origin('sdreduce')
00440         if trec.has_key('sdreduce') and casac.utils().verify(mytmp, trec['sdreduce']) :
00441             result = task_sdreduce.sdreduce(infile, antenna, fluxunit, telescopeparm, specunit, restfreq, frame, doppler, calmode, fraction, noff, width, elongated, markonly, plotpointings, scanlist, field, iflist, pollist, channelrange, average, scanaverage, timeaverage, tweight, averageall, polaverage, pweight, tau, kernel, kwidth, chanwidth, masklist, maskmode, thresh, avg_limit, edge, blfunc, order, npiece, applyfft, fftmethod, fftthresh, addwn, rejwn, clipthresh, clipniter, verifycal, verifysm, verifybl, verbosebl, bloutput, blformat, showprogress, minnrow, outfile, outform, overwrite, plotlevel)
00442 
00443         else :
00444           result = False
00445         return result