def make_source (ns,name,l,m,I=1): """Makes a source with a direction, using the current option set. Returns None for sources out of the "sky". (l^2+m^2>1)"""; l = math.sin(l); m = math.sin(m); if l*l + m*m <= 1: srcdir = Meow.LMDirection(ns,name,l,m); if source_spi is not None: freq0 = source_freq0*1e+6 if source_freq0 else Meow.Context.observation.freq0(); spi = source_spi if source_spi_2 is None else [ source_spi,source_spi_2 ]; else: spi = freq0 = None; if source_pol: Q = I*source_qi; U = I*source_ui; V = I*source_vi; else: Q = U = V = None; if source_type == "point": return Meow.PointSource(ns,name,srcdir,I=I,Q=Q,U=U,V=V,spi=spi,freq0=freq0); elif source_type == "gaussian": s1,s2 = gauss_smaj*ARCSEC,gauss_smin*ARCSEC; pa = gauss_pa*DEG; return Meow.GaussianSource(ns,name,srcdir,I=I,Q=Q,U=U,V=V,spi=spi,freq0=freq0, lproj=s1*math.sin(pa),mproj=s1*math.cos(pa),ratio=s2/s1); # else fall through and return none return None;
def make_source(ns, name, az, el, I=1): """Makes a source with a fixed azimuth/elevation direction """ srcdir = Meow.AzElDirection(ns, name, az, el) if source_spi is not None: freq0 = source_freq0 * 1e+6 if source_freq0 else Meow.Context.observation.freq0( ) else: freq0 = None if source_pol: Q = I * source_qi U = I * source_ui V = I * source_vi else: Q = U = V = None if source_type == "point": return Meow.PointSource(ns, name, srcdir, I=I, Q=Q, U=U, V=V, spi=source_spi, freq0=freq0) elif source_type == "gaussian": s1, s2 = gauss_smaj * ARCSEC, gauss_smin * ARCSEC pa = gauss_pa * DEG return Meow.GaussianSource(ns, name, srcdir, I=I, Q=Q, U=U, V=V, spi=source_spi, freq0=freq0, lproj=s1 * math.sin(pa), mproj=s1 * math.cos(pa), ratio=s2 / s1) # else fall through and return none return None
def source_list (self,ns,max_sources=None,**kw): """Reads LSM and returns a list of Meow objects. ns is node scope in which they will be created. Keyword arguments may be used to indicate which of the source attributes are to be created as Parms, use e.g. I=Meow.Parm(tags="flux") for this. The use_parms option may override this. """; if self.filename is None: return []; if self.lsm is None: self.load(ns); # all=1 returns unsorted list, so use a large count instead, to get a sorted list plist = self.lsm.queryLSM(count=9999999); # parse the beam expression if self.beam_expr is not None: try: beam_func = eval("lambda r,fq:"+self.beam_expr); except: raise RuntimeError("invalid beam expression"); else: beam_func = None; # make list of direction,punit,I,I_apparent tuples parm = Meow.Parm(tags="source solvable"); srclist = []; for pu in plist: ra,dec,I,Q,U,V,spi,freq0,RM = pu.getEssentialParms(ns); if self.solve_pos: ra = parm.new(ra); dec = parm.new(dec); direction = Meow.Direction(ns,pu.name,ra,dec,static=not self.solve_pos); Iapp = I; if beam_func is not None: # if phase centre is already set (i.e. static), then lmn will be computed here, and we # can apply a beam expression lmn = direction.lmn_static(); if lmn is not None: r = sqrt(lmn[0]**2+lmn[1]**2); Iapp = I*beam_func(r,freq0*1e-9 or 1.4); # use 1.4 GHz if ref frequency not specified # append to list srclist.append((pu.name,direction,pu,I,Iapp)); # sort list by decreasing apparent flux from past.builtins import cmp from functools import cmp_to_key srclist.sort(key=cmp_to_key(lambda a,b:cmp(b[4],a[4]))); srclist_full = srclist; # extract active subset srclist = self._subset_parser.apply(self.lsm_subset,srclist_full,names=[src[0] for src in srclist_full]); # extract solvable subset solve_subset = self._subset_parser.apply(self.solve_subset,srclist_full,names=[src[0] for src in srclist_full]); solve_subset = set([src[0] for src in solve_subset]); # make copy of kw dict to be used for sources not in solvable set kw_nonsolve = dict(kw); # and update kw dict to be used for sources in solvable set if self.solvable_sources: if self.solve_I: kw.setdefault("I",parm); if self.solve_Q: kw.setdefault("Q",parm); if self.solve_U: kw.setdefault("U",parm); if self.solve_V: kw.setdefault("V",parm); if self.solve_spi: kw.setdefault("spi",parm); if self.solve_RM: kw.setdefault("RM",parm); if self.solve_pos: kw.setdefault("ra",parm); kw.setdefault("dec",parm); if self.solve_shape: kw.setdefault("sx",parm); kw.setdefault("sy",parm); kw.setdefault("phi",parm); # make Meow list source_model = [] ## Note: conversion from AIPS++ componentlist Gaussians to Gaussian Nodes ### eX, eY : multiply by 2 ### eP: change sign for name,direction,pu,I,Iapp in srclist: # print "%-20s %12f %12f"%(pu.name,I,Iapp); src = {}; ( src['ra'],src['dec'], src['I'],src['Q'],src['U'],src['V'], src['spi'],src['freq0'],src['RM'] ) = pu.getEssentialParms(ns) (eX,eY,eP) = pu.getExtParms() # scale 2 difference src['sx'] = eX*2 src['sy'] = eY*2 src['phi'] = -eP # override zero values with None so that Meow can make smaller trees if not src['RM']: src['RM'] = None; if not src['spi']: src['spi'] = None; if src['RM'] is None: src['freq0'] = None; ## construct parms or constants for source attributes ## if source is in solvable set (solvable_source_set of None means all are solvable), ## use the kw dict, else use the nonsolve dict for source parameters if name in solve_subset: solvable = True; kwdict = kw; else: solvable = False; kwdict = kw_nonsolve; for key,value in src.items(): meowparm = kwdict.get(key); if isinstance(meowparm,Meow.Parm): src[key] = meowparm.new(value); elif meowparm is not None: src[key] = value; if eX or eY or eP: # Gaussians if eY: size,phi = [src['sx'],src['sy']],src['phi']; else: size,phi = src['sx'],None; src = Meow.GaussianSource(ns,name=pu.name, I=src['I'],Q=src['Q'],U=src['U'],V=src['V'], direction=direction, spi=src['spi'],freq0=src['freq0'],RM=src['RM'], size=size,phi=phi); else: src = Meow.PointSource(ns,name=pu.name, I=src['I'],Q=src['Q'],U=src['U'],V=src['V'], direction=direction, spi=src['spi'],freq0=src['freq0'],RM=src['RM']); # check for beam LM if pu._lm is not None: src.set_attr('beam_lm',pu._lm); src.solvable = solvable; src.set_attr('Iapp',Iapp); source_model.append(src); return source_model;
def source_list(self, ns, max_sources=None, **kw): """Reads LSM and returns a list of Meow objects. ns is node scope in which they will be created. Keyword arguments may be used to indicate which of the source attributes are to be created as Parms, use e.g. I=Meow.Parm(tags="flux") for this. The use_parms option may override this. """ if self.filename is None: return [] # load the sky model if self.lsm is None: self.lsm = Tigger.load(self.filename) # sort by brightness import functools from past.builtins import cmp from functools import cmp_to_key sources = sorted( self.lsm.sources, key=cmp_to_key(lambda a, b: cmp(b.brightness(), a.brightness()))) # extract subset, if specified sources = SourceSubsetSelector.filter_subset(self.lsm_subset, sources, self._getTagValue) # get nulls subset if self.null_subset: nulls = set([ src.name for src in SourceSubsetSelector.filter_subset( self.null_subset, sources) ]) else: nulls = set() parm = Meow.Parm(tags="source solvable") # make copy of kw dict to be used for sources not in solvable set kw_nonsolve = dict(kw) # and update kw dict to be used for sources in solvable set # this will be a dict of lists of solvable subgroups parms = [] subgroups = {} if self.solvable_sources: subgroup_order = [] for sgname in _SubgroupOrder: if getattr(self, 'solve_%s' % sgname): sg = subgroups[sgname] = [] subgroup_order.append(sgname) # make Meow list source_model = [] for src in sources: is_null = src.name in nulls # this will be True if this source has solvable parms solvable = self.solvable_sources and not is_null and ( not self.lsm_solvable_tag or getattr(src, self.lsm_solvable_tag, False)) if solvable: # independent groups? if self.lsm_solve_group_tag: independent_sg = sgname = "%s:%s" % ( self.lsm_solve_group_tag, getattr(src, self.lsm_solve_group_tag, "unknown")) else: independent_sg = "" sgname = 'source:%s' % src.name if sgname in subgroups: sgsource = subgroups[sgname] else: sgsource = subgroups[sgname] = [] subgroup_order.append(sgname) # make dict of source parametrs: for each parameter we have a value,subgroup pair if is_null: attrs = dict(ra=src.pos.ra, dec=src.pos.dec, I=0, Q=None, U=None, V=None, RM=None, spi=None, freq0=None) else: attrs = dict( ra=src.pos.ra, dec=src.pos.dec, I=src.flux.I, Q=getattr(src.flux, 'Q', None), U=getattr(src.flux, 'U', None), V=getattr(src.flux, 'V', None), RM=getattr(src.flux, 'rm', None), freq0=getattr(src.flux, 'freq0', None) or (src.spectrum and getattr(src.spectrum, 'freq0', None)), spi=src.spectrum and getattr(src.spectrum, 'spi', None)) if not is_null and isinstance(src.shape, ModelClasses.Gaussian): attrs['lproj'] = src.shape.ex * math.sin(src.shape.pa) attrs['mproj'] = src.shape.ex * math.cos(src.shape.pa) attrs['ratio'] = src.shape.ey / src.shape.ex # construct parms or constants for source attributes, depending on whether the source is solvable or not # If source is solvable and this particular attribute is solvable, replace # value in attrs dict with a Meq.Parm. if solvable: for parmname, value in list(attrs.items()): sgname = _Subgroups.get(parmname, None) if sgname in subgroups: solvable = True parm = attrs[parmname] = ns[src.name]( parmname) << Meq.Parm(value or 0, tags=["solvable", sgname], solve_group=independent_sg) subgroups[sgname].append(parm) sgsource.append(parm) parms.append(parm) # construct a direction direction = Meow.Direction(ns, src.name, attrs['ra'], attrs['dec'], static=not solvable or not self.solve_pos) # construct a point source or gaussian or FITS image, depending on source shape class if src.shape is None or is_null: msrc = Meow.PointSource(ns, name=src.name, I=attrs['I'], Q=attrs['Q'], U=attrs['U'], V=attrs['V'], direction=direction, spi=attrs['spi'], freq0=attrs['freq0'], RM=attrs['RM']) elif isinstance(src.shape, ModelClasses.Gaussian): msrc = Meow.GaussianSource(ns, name=src.name, I=attrs['I'], Q=attrs['Q'], U=attrs['U'], V=attrs['V'], direction=direction, spi=attrs['spi'], freq0=attrs['freq0'], lproj=attrs['lproj'], mproj=attrs['mproj'], ratio=attrs['ratio']) if solvable and 'shape' in subgroups: subgroups['pos'] += direction.get_solvables() elif isinstance(src.shape, ModelClasses.FITSImage): msrc = Meow.FITSImageComponent(ns, name=src.name, filename=src.shape.filename, direction=direction) msrc.set_options(fft_pad_factor=(src.shape.pad or 2)) msrc.solvable = solvable # copy standard attributes from sub-objects for subobj in src.flux, src.shape, src.spectrum: if subobj: for attr, val in src.flux.getAttributes(): msrc.set_attr(attr, val) # copy all extra attrs from source object for attr, val in src.getExtraAttributes(): msrc.set_attr(attr, val) # make sure Iapp exists (init with I if it doesn't) if msrc.get_attr('Iapp', None) is None: msrc.set_attr('Iapp', src.flux.I) source_model.append(msrc) # if any solvable parms were made, make a parmgroup and solve job for them if parms: if os.path.isdir(self.filename): table_name = os.path.join(self.filename, "sources.fmep") else: table_name = os.path.splitext(self.filename)[0] + ".fmep" # make list of Subgroup objects for every non-empty subgroup sgs = [] for sgname in subgroup_order: sglist = subgroups.get(sgname, None) if sglist: sgs.append(Meow.ParmGroup.Subgroup(sgname, sglist)) # make main parm group pg_src = Meow.ParmGroup.ParmGroup("source parameters", parms, subgroups=sgs, table_name=table_name, table_in_ms=False, bookmark=True) # now make a solvejobs for the source Meow.ParmGroup.SolveJob("cal_source", "Solve for source parameters", pg_src) return source_model