casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
task_predictcomp.py
Go to the documentation of this file.
00001 from taskinit import casalog, cltool, imtool, metool, qa
00002 from plotcomp import plotcomp
00003 from predictcomp_helper import *
00004 import pylab as pl
00005 import os
00006 
00007 def predictcomp(objname=None, standard=None, epoch=None,
00008                 minfreq=None, maxfreq=None, nfreqs=None, prefix=None,
00009                 antennalist=None, showplot=None, savefig=None, symb=None,
00010                 include0amp=None, include0bl=None, blunit=None, bl0flux=None):
00011     """
00012     Writes a component list named clist to disk and returns a dict of
00013     {'clist': clist,
00014      'objname': objname,
00015      'angdiam': angular diameter in radians (if used in clist),
00016      'standard': standard,
00017      'epoch': epoch,
00018      'freqs': pl.array of frequencies, in GHz,
00019      'uvrange': pl.array of baseline lengths, in m,
00020      'amps':  pl.array of predicted visibility amplitudes, in Jy,
00021      'savedfig': False or, if made, the filename of a plot.}
00022     or False on error.
00023 
00024     objname: An object supported by standard.
00025     standard: A standard for calculating flux densities, as in setjy.
00026               Default: 'Butler-JPL-Horizons 2010'
00027     epoch: The epoch to use for the calculations.   Irrelevant for
00028            extrasolar standards.
00029     minfreq: The minimum frequency to use.
00030              Example: '342.0GHz'
00031     maxfreq: The maximum frequency to use.
00032              Default: minfreq
00033              Example: '346.0GHz'
00034              Example: '', anything <= 0, or None: use minfreq.
00035     nfreqs:  The number of frequencies to use.
00036              Default: 1 if minfreq == maxfreq,
00037                       2 otherwise.
00038     prefix: The component list will be saved to
00039               prefix + 'spw0_<objname>_<minfreq><epoch>.cl'
00040             Default: ''
00041     antennalist: An array configuration file as used by simdata.
00042                  If given, a plot of S vs. |u| will be made.
00043                  Default: '' (None, just make clist.)
00044     showplot: Whether or not to show the plot on screen.
00045               Subparameter of antennalist.
00046               Default: Necessarily False if antennalist is not specified.
00047                        True otherwise.
00048     savefig: Filename for saving a plot of S vs. |u|.
00049              Subparameter of antennalist.
00050              Default: False (necessarily if antennalist is not specified)
00051              Examples: True (save to prefix + '.png')
00052                        'myplot.png' (save to myplot.png) 
00053     symb: One of matplotlib's codes for plot symbols: .:,o^v<>s+xDd234hH|_
00054           default: ',':  The smallest points I could find.
00055     include0amp: Force the lower limit of the amplitude axis to 0.
00056                  Default: False
00057     include0bl: Force the lower limit of the baseline length axis to 0.
00058     blunit: Unit of the baseline length 
00059     bl0flux: show zero baseline flux
00060     """
00061     retval = False
00062     try:
00063         casalog.origin('predictcomp')
00064         # some parameter minimally required
00065         if objname=='':
00066           raise Exception, "Error, objname is undefined"
00067         if minfreq=='':
00068           raise Exception, "Error, minfreq is undefined" 
00069         minfreqq = qa.quantity(minfreq)
00070         minfreqHz = qa.convert(minfreqq, 'Hz')['value']
00071         try:
00072             maxfreqq = qa.quantity(maxfreq)
00073         except Exception, instance:
00074             maxfreqq = minfreqq
00075         frequnit = maxfreqq['unit']
00076         maxfreqHz = qa.convert(maxfreqq, 'Hz')['value']
00077         if maxfreqHz <= 0.0:
00078             maxfreqq = minfreqq
00079             maxfreqHz = minfreqHz
00080         if minfreqHz != maxfreqHz:
00081             if nfreqs < 2:
00082                 nfreqs = 2
00083         else:
00084             nfreqs = 1
00085         freqs = pl.linspace(minfreqHz, maxfreqHz, nfreqs)
00086 
00087         myme = metool()
00088         mepoch = myme.epoch('UTC', epoch)
00089         if not prefix:
00090             ## meanfreq = {'value': 0.5 * (minfreqHz + maxfreqHz),
00091             ##             'unit': frequnit}
00092             ## prefix = "%s%s_%.7g" % (objname, epoch.replace('/', '-'),
00093             ##                         minfreqq['value'])
00094             ## if minfreqHz != maxfreqHz:
00095             ##     prefix += "to" + maxfreq
00096             ## else:
00097             ##     prefix += minfreqq['unit']
00098             ## prefix += "_"
00099             prefix = ''
00100 
00101         # Get clist
00102         myim = imtool()
00103         if hasattr(myim, 'predictcomp'):
00104             casalog.post('local im instance created', 'DEBUG1')
00105         else:
00106             casalog.post('Error creating a local im instance.', 'SEVERE')
00107             return False
00108         #print "FREQS=",freqs
00109         if standard=='Butler-JPL-Horizons 2012':
00110             clist = predictSolarObjectCompList(objname, mepoch, freqs.tolist(), prefix)
00111         else:
00112             clist = myim.predictcomp(objname, standard, mepoch, freqs.tolist(), prefix)
00113         #print "created componentlist =",clist
00114         if os.path.isdir(clist):
00115             # The spw0 is useless here, but it is added by FluxStandard for the sake of setjy.
00116             casalog.post('The component list was saved to ' + clist)
00117             
00118             retval = {'clist': clist,
00119                       'objname': objname,
00120                       'standard': standard,
00121                       'epoch': mepoch,
00122                       'freqs (GHz)': 1.0e-9 * freqs,
00123                       'antennalist': antennalist}
00124             mycl = cltool()
00125             mycl.open(clist)
00126             comp = mycl.getcomponent(0)
00127             zeroblf=comp['flux']['value']
00128             if standard=='Butler-JPL-Horizons 2012':
00129               f0=comp['spectrum']['frequency']['m0']['value']
00130             else:
00131               f0=retval['freqs (GHz)'][0]
00132             casalog.post("Zero baseline flux %s @ %sGHz " % (zeroblf, f0),'INFO')
00133             mycl.close(False)               # False prevents the stupid warning.
00134             for k in ('shape', 'spectrum'):
00135                 retval[k] = comp[k]
00136             if antennalist:
00137                 retval['savedfig'] = savefig
00138                 if not bl0flux:
00139                   zeroblf=[0.0]
00140                 retval.update(plotcomp(retval, showplot, wantdict=True, symb=symb,
00141                                        include0amp=include0amp, include0bl=include0bl, blunit=blunit, bl0flux=zeroblf[0]))
00142             else:
00143                 retval['savedfig'] = None
00144         else:
00145             casalog.post("There was an error in making the component list.",
00146                          'SEVERE')
00147 
00148     except Exception, instance:
00149         casalog.post(instance, 'SEVERE')
00150     return retval