def source_list(ns): # figure out spectral index parameters if spectral_index is not None: spi_def = Meow.Parm(spectral_index) freq0_def = ref_freq else: spi_def = freq0_def = None if spectral_index1 is not None: spi_def1 = Meow.Parm(spectral_index1) freq0_def1 = ref_freq else: spi_def1 = freq0_def1 = None # and flux parameters i_def = Meow.Parm(1) quv_def = Meow.Parm(0) dir1 = Meow.Direction(ns, "3C343.1", 4.356645791155902, 1.092208429052697) dir0 = Meow.Direction(ns, "3C343", 4.3396003966265599, 1.0953677174056471) src1 = Meow.PointSource(ns, "3C343.1", dir1, I=Meow.Parm(6.02061051), Q=Meow.Parm(0.0179716185), U=quv_def, V=quv_def, spi=spi_def1, freq0=freq0_def1) src0 = Meow.PointSource(ns, "3C343", dir0, I=Meow.Parm(1.83336309), Q=Meow.Parm(0.0241450607), U=quv_def, V=quv_def, spi=spi_def1, freq0=freq0_def1) ## define a parmgroup for source parameters pg_src = ParmGroup.ParmGroup("source", src1.coherency().search(tags="solvable") + src0.coherency().search(tags="solvable"), table_name="sources.mep") ParmGroup.SolveJob("cal_sources", "Calibrate sources", pg_src) return [src1, src0]
def compute_jones(Jones, sources, stations=None, inspectors=[], meqmaker=None, label='R', **kw): """Creates the Z Jones for ionospheric phase, given TECs (per source, per station).""" stations = stations or Context.array.stations ns = Jones.Subscope() # get reference source if ref_source: # treat as index first dir0 = None try: dir0 = sources[int(ref_source)].direction except: pass # else treat as name, find in list if not dir0: for src0 in sources: if src0.name == ref_source: dir0 = src0.direction break # else treat as direction string if not dir0: ff = list(ref_source.split()) if len(ff) < 2 or len(ff) > 3: raise RuntimeError( "invalid reference dir '%s' specified for %s-Jones" % (ref_source, label)) global dm if not dm: raise RuntimeError( "pyrap measures module not available, cannot use direction strings for %s-Jones" % label) if len(ff) == 2: ff = ['J2000'] + ff # treat as direction measure try: dmdir = dm.direction(*ff) except: raise RuntimeError( "invalid reference dir '%s' specified for %s-Jones" % (ref_source, label)) # convert to J2000 and make direction object dmdir = dm.measure(dmdir, 'J2000') ra, dec = dm.getvalue(dmdir)[0].get_value(), dm.getvalue( dmdir)[1].get_value() dir0 = Meow.Direction(ns, "refdir", ra, dec, static=True) else: dir0 = Context.observation.phase_centre # make refraction scale node scale = ns.scale(0) << Meq.Parm(0, tags="refraction") xyz0 = Context.array.xyz0() if coord_approx: # get PA, and assume it's the same over the whole field pa = ns.pa0 << Meq.ParAngle(dir0.radec(), xyz0) # second column of the Rot(-PA) matrix. Multiply this by del to get a rotation of (0,del) into the lm plane. # The third component (0) is for convenience, as it immediately gives us dl,dm,dn, since we assume dn~0 rot_pa = ns.rotpa0 << Meq.Composer(Meq.Sin(pa), Meq.Cos(pa), 0) # el0: elevation of field centre el0 = dir0.el() if do_extinction: ns.inv_ext0 << Meq.Sin(el0) # inverse of extinction towards el0 # station UVWs uvw = Context.array.uvw() # now loop over sources for isrc, src in enumerate(sources): # reference direction: no refraction at all if src.direction is dir0: for p in stations: Jones(src, p) << 1 continue # dEl is source elevation minus el0 # ddEl = scale*dEl: amount by which source refracts (negative means field is compressed) el = src.direction.el() ns.dEl(src) << el - el0 ddel = ns.ddEl(src) << ns.dEl(src) * scale # get el1: refracted elevation angle if not coord_approx or do_extinction: el1 = ns.el1(src) << el + ddel # compute extinction component if do_extinction: # compute inverse of extinction towards the refracted direction el1 iext = ns.inv_ext(src) << Meq.Sin(el1) # # and differential extinction is then ext1/ext0 ext = ns.dext(src) << ns.inv_ext0 / iext # Compute dlmn offset in lm plane. if coord_approx: # Approximate mode: ddel is added to elevation, so to get the lm offset, we need # to apply Rot(PA) to the column vector (0,ddel), and then take the sine of the result. dlmn = ns.dlmn(src) << Meq.Sin(ddel * rot_pa) else: ns.azel1(src) << Meq.Composer(src.direction.az(), el1) ns.radec1(src) << Meq.RADec(ns.azel1(src), xyz0) ns.lmn1(src) << Meq.LMN(Context.observation.radec0(), ns.radec1(src)) dlmn = ns.dlmn(src) << ns.lmn1(src) - src.lmn() # get per-station phases for p in stations: if do_extinction: Jones(src, p) << ext * (ns.phase(src, p) << Meq.VisPhaseShift( lmn=dlmn, uvw=uvw(p))) else: Jones(src, p) << Meq.VisPhaseShift(lmn=dlmn, uvw=uvw(p)) # make bookmarks srcnames = [src.name for src in sources] meqmaker.make_bookmark_set(Jones, [(src, p) for src in srcnames for p in stations], "%s: inspector plot" % label, "%s: by source-station" % label, freqmean=True) inspectors.append(ns.inspector(label,'scale') << \ StdTrees.define_inspector(ns.scale,[0],label=label)) inspectors.append(ns.inspector(label,'delta-el') << \ StdTrees.define_inspector(ns.ddEl,srcnames,label=label)) inspectors.append(ns.inspector(label,'delta-el') << \ StdTrees.define_inspector(ns.ddEl,srcnames,label=label)) inspectors.append(ns.inspector(label,'dlmn') << \ StdTrees.define_inspector(ns.dlmn,srcnames,label=label)) if do_extinction: inspectors.append(ns.inspector(label,'inv-ext') << \ StdTrees.define_inspector(ns.inv_ext,srcnames,label=label)) inspectors.append(ns.inspector(label,'diff-ext') << \ StdTrees.define_inspector(ns.dext,srcnames,label=label)) # make parmgroups and solvejobs global pg pg = ParmGroup.ParmGroup(label, [scale], table_name="%s.fmep" % label, bookmark=False) # make solvejobs ParmGroup.SolveJob("cal_" + label, "Calibrate %s (differential refraction)" % label, pg) return Jones
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