def radec_to_azel(pointing, time, position): # All inputs as measures # Returns a measure giving az/el of ra/dec at given time & position dm = measures() dm.do_frame(time) dm.do_frame(position) return dm.measure(pointing, 'azel')
def _synthesize_uvw(cls, station_ECEF, time, a1, a2, phase_ref): """ Synthesizes new UVW coordinates based on time according to NRAO CASA convention (same as in fixvis) User should check these UVW coordinates carefully - if time centroid was used to compute original uvw coordinates the centroids of these new coordinates may be wrong, depending on whether data timesteps were heavily flagged. station_ECEF: ITRF station coordinates read from MS::ANTENNA time: time column, preferably time centroid (padded to nrow = unique time * unique bl) a1: ANTENNA_1 index (padded to nrow = unique time * unique bl) a2: ANTENNA_2 index (padded to nrow = unique time * unique bl) phase_ref: phase reference centre in radians """ assert time.size == a1.size assert a1.size == a2.size dm = measures() epoch = dm.epoch("UT1", quantity(time[0], "s")) refdir = dm.direction("j2000", quantity(phase_ref[0], "rad"), quantity(phase_ref[1], "rad")) obs = dm.position("ITRF", quantity(station_ECEF[0, 0], "m"), quantity(station_ECEF[0, 1], "m"), quantity(station_ECEF[0, 2], "m")) #setup local horizon coordinate frame with antenna 0 as reference position dm.do_frame(obs) dm.do_frame(refdir) dm.do_frame(epoch) ants = np.concatenate((a1, a2)) unique_ants = np.unique(ants) unique_time = np.unique(time) na = unique_ants.size nbl = na * (na - 1) / 2 + na ntime = unique_time.size assert time.size == nbl * ntime, "Input arrays must be padded to include autocorrelations, all baselines and all time" antenna_indicies = DataDictionaryManager.antenna_indicies( na, auto_correlations=True) new_uvw = np.zeros((ntime * nbl, 3)) for ti, t in enumerate(unique_time): epoch = dm.epoch("UT1", quantity(t, "s")) dm.do_frame(epoch) station_uv = np.zeros_like(station_ECEF) for iapos, apos in enumerate(station_ECEF): station_uv[iapos] = dm.to_uvw( dm.baseline("ITRF", quantity([apos[0], station_ECEF[0, 0]], "m"), quantity([apos[1], station_ECEF[0, 1]], "m"), quantity([apos[2], station_ECEF[0, 2]], "m")))["xyz"].get_value()[0:3] for bl in xrange(nbl): blants = antenna_indicies[bl] bla1 = blants[0] bla2 = blants[1] new_uvw[ti * nbl + bl, :] = station_uv[bla1] - station_uv[ bla2] # same as in CASA convention (Convention for UVW calculations in CASA, Rau 2013) return new_uvw
def getJonesByAntFld(model,obsTimes,stnName,srcDirection,freq=75.0E6): if freq > 100.0E6: rcumode=5 else: rcumode=3 AntFld=parseAntennaField(stnName) stnLoc=stnName[0:2] if rcumode==3: AntBand='LBA' elif rcumode==5: if stnLoc=='CS' or stnLoc=='RS': AntBand='HBA0' else: AntBand='HBA' stnPos=np.matrix(AntFld[AntBand]['POSITION']).T stnRot=np.matrix(AntFld[AntBand]['ROTATION_MATRIX']) me=measures() stnPos_me=me.position('ITRF',str(stnPos[0,0])+'m',str(stnPos[1,0])+'m',str(stnPos[2,0])+'m') #print stnPos[0,0],stnPos[1,0],stnPos[2,0] #print stnRot if model == "dipole": return dipoleJones.getDipJones(obsTimes,stnPos_me,stnRot,srcDirection) elif model == "Hamaker": return getHamakerJones(obsTimes,stnPos_me,stnRot,srcDirection,freq) else: error("Unknown antenna model (choose from 'dipole','Hamaker')")
def getPiercePoints(time,source_positions,station_positions,height=450.e3): me=measures() piercepoints=PPdummy() piercepoints.positions_xyz=np.zeros((source_positions.shape[0],station_positions.shape[0],3),dtype=np.float64) piercepoints.positions=np.zeros((source_positions.shape[0],station_positions.shape[0],2),dtype=np.float64) piercepoints.zenith_angles=np.zeros((source_positions.shape[0],station_positions.shape[0]),dtype=np.float64) for isrc in range(source_positions.shape[0]) : radec=source_positions[isrc] for istat in range(station_positions.shape[0]): stat_pos=station_positions[istat] azel=radec2azel(radec[0],radec[1],time=str(time)+'s',pos=stat_pos) az=azel['m0']['value']; el=azel['m1']['value']; lonlat=getLonLatStation(az,el,pos=stat_pos); lon=lonlat['m0']['value']; lat=lonlat['m1']['value']; # convert to itrf coordinates on sphere with radius 1 diritrf=[cos(lat)*cos(lon),cos(lat)*sin(lon),sin(lat)] (pp,am)=getPP(h=height,mPosition=stat_pos,direction=diritrf) piercepoints.positions_xyz[isrc,istat]=np.array(pp[0]) piercepoints.zenith_angles[isrc,istat]=np.arccos(1./am) pp1position=me.position("ITRF",str(pp[0,0])+'m',str(pp[0,1])+'m',str(pp[0,2])+'m') # print "pp", degrees(pp1position['m0']['value']),degrees(pp1position['m1']['value']) piercepoints.positions[isrc,istat,0] = pp1position['m0']['value']; piercepoints.positions[isrc,istat,1] = pp1position['m1']['value']; return piercepoints
def convert_coordsystem(ra, dec, insys, outsys): """ Convert RA & dec (given in decimal degrees) between equinoxes. """ dm = measures() if insys == CoordSystem.FK4: insys = "B1950" elif insys == CoordSystem.FK5: insys = "J2000" else: raise Exception("Unknown Coordinate System") if outsys == CoordSystem.FK4: outsys = "B1950" elif outsys == CoordSystem.FK5: outsys = "J2000" else: raise Exception("Unknown Coordinate System") result = dm.measure( dm.direction(insys, "%fdeg" % ra, "%fdeg" % dec), outsys ) ra = math.degrees(result['m0']['value']) % 360 # 0 < ra < 360 dec = math.degrees(result['m1']['value']) return ra, dec
def convert_coordsystem(ra, dec, insys, outsys): """ Convert RA & dec (given in decimal degrees) between equinoxes. """ dm = measures() if insys == CoordSystem.FK4: insys = "B1950" elif insys == CoordSystem.FK5: insys = "J2000" else: raise Exception("Unknown Coordinate System") if outsys == CoordSystem.FK4: outsys = "B1950" elif outsys == CoordSystem.FK5: outsys = "J2000" else: raise Exception("Unknown Coordinate System") result = dm.measure(dm.direction(insys, "%fdeg" % ra, "%fdeg" % dec), outsys) ra = math.degrees(result['m0']['value']) % 360 # 0 < ra < 360 dec = math.degrees(result['m1']['value']) return ra, dec
def get_station_position(ms, antenna): # Returns ITRF coordinates of the given antenna ID as a position measure fieldtable = table(ms.getkeyword('LOFAR_ANTENNA_FIELD')) postable = fieldtable.query('ANTENNA_ID==%d' % (antenna,)) x, y, z = postable.getcol('POSITION')[0] dm = measures() return dm.position("itrf", "%fm" % (x,), "%fm" % (y,), "%fm" % (z,))
def get_time(t): time_start = qa.quantity(t, 's') me = pm.measures() dict_time_start_MDJ = me.epoch('utc', time_start) time_start_MDJ = dict_time_start_MDJ['m0']['value'] JD = time_start_MDJ + 2400000.5 - 2415020 d = ephem.Date(JD) return d.datetime() #.isoformat().replace("T","/")
def GiveStrDate(tt): time_start = qa.quantity(tt, 's') me = pm.measures() dict_time_start_MDJ = me.epoch('utc', time_start) time_start_MDJ=dict_time_start_MDJ['m0']['value'] JD=time_start_MDJ+2400000.5-2415020 d=ephem.Date(JD) return d.datetime().isoformat().replace("T","/")
def getLonLat(pos): #converts ITRF pos in xyz to lon lat me = measures() a = me.measure( me.position('ITRF', str(pos[0]) + 'm', str(pos[1]) + 'm', str(pos[2]) + 'm'), "ITRF") return (a['m0']['value'], a['m1']['value'])
def GiveDate(tt): import pyrap.quanta as qa import pyrap.measures as pm time_start = qa.quantity(tt, 's') me = pm.measures() dict_time_start_MDJ = me.epoch('utc', time_start) time_start_MDJ = dict_time_start_MDJ['m0']['value'] JD = time_start_MDJ + 2400000.5 - 2415020 d = ephem.Date(JD) return d.datetime().isoformat().replace("T", "/")
def getLonLatStation(az=0,el=0,pos=posCS002): #gets converts local station direction to ITRF lon/lat if not isinstance(az,str): az=str(az)+'rad'; if not isinstance(el,str): el=str(el)+'rad'; me=measures() me.do_frame(me.position('ITRF',str(pos[0])+'m',str(pos[1])+'m',str(pos[2])+'m')) #me.do_frame(me.epoch('utc', 'today')) direction=me.direction("AZEL",az,el) return me.measure(direction,"ITRF");
def convert(fileitrf, save_enu=True, plot=False): """ xyz : positions of layouts (numpy array) """ #if anttab: xyz = table(anttab).getcol("POSITION") import numpy as np import pyrap.quanta import ipdb import matplotlib.pyplot as plt from pyrap.measures import measures import os name = os.path.basename(fileitrf).split('.')[0] f = open(fileitrf, 'r') hd = f.readline() tabx = [] taby = [] tabz = [] for line in f: line = line.strip() columns = line.split() print columns tabx.append(columns[0]) taby.append(columns[1]) tabz.append(columns[2]) xyz = np.array([tabx, taby, tabz]).T.astype(float) dm = measures() dq = pyrap.quanta DEG = 180. / np.pi EarthRad = 6471. deg2m = 2 * np.pi * EarthRad / 360. # Arc length subtended by 1 deg at Earth Radius distance #ipdb.set_trace() p1 = dm.position('itrf', *[dq.quantity(xyz[:, i], 'm') for i in range(3)]) lon = p1['m0']['value'] lat = p1['m1']['value'] dx = (lon - lon[0]) * np.cos(lat[0]) * DEG * deg2m dy = (lat - lat[0]) * DEG * deg2m #ipdb.set_trace() if save_enu: np.savetxt('%s.enu.txt' % name, np.array([dx * 1e3, dy * 1e3]).T) #if show_base: # pos = table(base).getcol("POSITION") # p1 = dm.position('itrf',*[dq.quantity(pos[:,i],'m') for i in range(3)]) # lon = p1['m0']['value'] # lat = p1['m1']['value'] # dx = (lon-lon[0])*np.cos(lat[0])*DEG*deg2m # dy = (lat-lat[0])*DEG*deg2m if plot: plt.plot(dx, dy, 'rx') return dx, dy
def gaussian_cutoff(limit, fwhm, ra, dec): limit = float(limit) fwhm = float(fwhm) dm = measures() centre = dm.direction(EPOCH, ra, dec) constant = -4 * math.log(2) / float(fwhm)**2 def flux_limit(ra, dec): target = dm.direction(EPOCH, ra, dec) distance = dm.separation(centre, target).get_value() return limit * (1 - math.exp(constant * distance**2)) return flux_limit
def convert(fileitrf, save_enu=True, plot=False): """ xyz : positions of layouts (numpy array) """ #if anttab: xyz = table(anttab).getcol("POSITION") import numpy as np import pyrap.quanta import ipdb import matplotlib.pyplot as plt from pyrap.measures import measures import os name=os.path.basename(fileitrf).split('.')[0] f=open(fileitrf,'r') hd=f.readline() tabx=[] taby=[] tabz=[] for line in f: line=line.strip() columns=line.split() print columns tabx.append(columns[0]) taby.append(columns[1]) tabz.append(columns[2]) xyz=np.array([tabx,taby,tabz]).T.astype(float) dm = measures() dq = pyrap.quanta DEG = 180./np.pi EarthRad=6471. deg2m = 2*np.pi*EarthRad/360. # Arc length subtended by 1 deg at Earth Radius distance #ipdb.set_trace() p1 = dm.position('itrf',*[dq.quantity(xyz[:,i],'m') for i in range(3)]) lon = p1['m0']['value'] lat = p1['m1']['value'] dx = (lon-lon[0])*np.cos(lat[0])*DEG*deg2m dy = (lat-lat[0])*DEG*deg2m #ipdb.set_trace() if save_enu : np.savetxt('%s.enu.txt'%name,np.array([dx*1e3,dy*1e3]).T) #if show_base: # pos = table(base).getcol("POSITION") # p1 = dm.position('itrf',*[dq.quantity(pos[:,i],'m') for i in range(3)]) # lon = p1['m0']['value'] # lat = p1['m1']['value'] # dx = (lon-lon[0])*np.cos(lat[0])*DEG*deg2m # dy = (lat-lat[0])*DEG*deg2m if plot: plt.plot(dx,dy,'rx') return dx,dy
def getAzEl(pointing,time,position,ha_limit=-1000): if HAS_PYRAP: if ha_limit==-1000: azel=radec2azel(pointing[0],pointing[1],time=str(time)+'s',pos=position); az=azel['m0']['value'] el=azel['m1']['value'] else: me=measures() p=me.position("ITRF",str(position[0])+'m',str(position[1])+'m',str(position[2])+'m') t=me.epoch("UTC",qu.quantity(str(time)+'s')) phasedir=me.direction('J2000',str(pointing[0])+'rad',str(pointing[1])+'rad') me.doframe(p) me.doframe(t) hadec=me.measure(phasedir,"HADEC") if abs(hadec['m0']['value'])>ha_limit: print "below horizon",tab.taql('calc ctod($time s)')[0],degrees(hadec['m0']['value']),degrees(hadec['m1']['value']) return 999,999 else: azel=me.measure(phasedir,"AZEL") az=azel['m0']['value']; el=azel['m1']['value']; elif HAS_EPHEM: if ha_limit!=-1000: print "limiting on HA/DEC not implemented for PyEphem yet, ignoring" location_lat, location_lon, location_height = ITRFToWGS84(position[0], position[1], position[2]) location = ephem.Observer() # convert geodetic latitude to geocentric # flattening, f, defined above for WGS84 stuff geodet_lat = math.radians(location_lat) tan_geocentric_latitude = math.tan(geodet_lat) * (1 - f) **2 geocentric_latitude = GeodeticToGeocentricLat(geodet_lat, location_height) location.lat = geocentric_latitude location.lon = math.radians(location_lon) location.elevation = location_height location.pressure = 0.0 # convert to Dublin Julian Date for PyEphem location.date = time/86400.0 - 15019.5 lst = location.sidereal_time() equatorial = ephem.Equatorial(str(12.0 * math.degrees(pointing[0])/180),str(math.degrees(pointing[1]))) body = ephem.FixedBody() body._ra = equatorial.ra body._dec = equatorial.dec body._epoch = equatorial.epoch body.compute(location) az = math.degrees(body.az) * math.pi / 180.0 el = math.degrees(body.alt) * math.pi / 180.0 else: print ('failure to get azimuth and elevation! Exiting!') return -1,-1 return az,el
def radec2azel(ra,dec,time, pos): me=measures(); if type(ra)!=str: ra=str(ra)+'rad'; if type(dec)!=str: dec=str(dec)+'rad'; phasedir=me.direction('J2000',ra,dec) t=me.epoch("UTC",qu.quantity(time)); me.do_frame(t); p = me.position('ITRF',str(pos[0])+'m',str(pos[1])+'m',str(pos[2])+'m') me.do_frame(p); azel = me.measure(phasedir,'azel'); return azel;
def azel2radec(az,el,time, pos): me=measures(); if type(az)!=str: az=str(az)+'rad'; if type(el)!=str: el=str(el)+'rad'; phasedir=me.direction('AZEL',az,el) t=me.epoch("UTC",qu.quantity(time)); me.do_frame(t); p = me.position('ITRF',str(pos[0])+'m',str(pos[1])+'m',str(pos[2])+'m') me.do_frame(p); radec = me.measure(phasedir,'RADEC'); return radec;
def radec2azel(ra,dec,time, pos): me=measures(); if type(ra)!=str: ra=str(ra)+'rad'; if type(dec)!=str: dec=str(dec)+'rad'; phasedir=me.direction('J2000',ra,dec) t=me.epoch("UTC",qa.quantity(time)); me.do_frame(t); p = me.position('ITRF',str(pos[0])+'m',str(pos[1])+'m',str(pos[2])+'m') me.do_frame(p); azel = me.measure(phasedir,'azel'); return azel;
def azel2radec(az,el,time, pos): me=measures(); if type(az)!=str: az=str(az)+'rad'; if type(el)!=str: el=str(el)+'rad'; phasedir=me.direction('AZEL',az,el) t=me.epoch("UTC",qa.quantity(time)); me.do_frame(t); p = me.position('ITRF',str(pos[0])+'m',str(pos[1])+'m',str(pos[2])+'m') me.do_frame(p); radec = me.measure(phasedir,'RADEC'); return radec;
def getuvw(ra,dec,time, pos1,pos2): me=measures(); if type(ra)!=str: ra=str(ra)+'rad'; if type(dec)!=str: dec=str(dec)+'rad'; phasedir=me.direction('J2000',ra,dec) me.do_frame(phasedir); t=me.epoch("UTC",qu.quantity(time)); me.do_frame(t); p = me.position('ITRF',str(pos1[0])+'m',str(pos1[1])+'m',str(pos1[2])+'m') bl = me.baseline('ITRF',str(pos2[0]-pos1[0])+'m',str(pos2[1]-pos1[1])+'m',str(pos2[2]-pos1[2])+'m') print bl me.do_frame(p); print me.to_uvw(bl)['xyz']
def mjd2lst(mjd, position=None): """ Converts a Modified Julian Date into Local Apparent Sidereal Time in seconds at a given position. If position is None, we default to the reference position of CS002. mjd -- Modified Julian Date (float, in days) position -- Position (pyrap measure) """ dm = measures() position = position or dm.position("ITRF", "%fm" % ITRF_X, "%fm" % ITRF_Y, "%fm" % ITRF_Z) dm.do_frame(position) last = dm.measure(dm.epoch("UTC", "%fd" % mjd), "LAST") fractional_day = last['m0']['value'] % 1 return fractional_day * 24 * SECONDS_IN_HOUR
def getlonlatheight(az, el, position, h=ION_HEIGHT): if HAS_PYRAP: lonlat = getLonLatStation(az, el, pos=position) lon = lonlat['m0']['value'] lat = lonlat['m1']['value'] # convert to itrf coordinates on sphere with radius 1 diritrf = [cos(lat) * cos(lon), cos(lat) * sin(lon), sin(lat)] # calculate piercepoint in xyz(code from Bas vd Tol) pos_pp = (ppx1, ppy1, ppz1, am1) = getPP(h=h, mPosition=position, direction=diritrf) #get pp in lon,lat h me = measures() pp1position = me.position("ITRF", str(ppx1) + 'm', str(ppy1) + 'm', str(ppz1) + 'm') lonpp = degrees(pp1position['m0']['value']) latpp = degrees(pp1position['m1']['value']) height = pp1position['m2']['value'] / 1.0e3 elif HAS_EPHEM: slantRange = 3.0e20 # seem to need a large value to # get near the WNB measures equivalent # lat,lon,ht = aer2ecef(math.degrees(body.az), math.degrees(body.alt), slantRange, math.degrees(geocentric_latitude), location_lon, ION_HEIGHT) location_lat, location_lon, location_height = ITRFToWGS84( position[0], position[1], position[2]) lat, lon, ht = aer2ecef(math.degrees(az), math.degrees(el), slantRange, location_lat, location_lon, ION_HEIGHT) # convert to itrf coordinates on sphere with radius 1 lat = math.radians(lat) lon = math.radians(lon) diritrf = [cos(lat) * cos(lon), cos(lat) * sin(lon), sin(lat)] # calculate piercepoint in xyz(code from Bas vd Tol) pos_pp = (ppx1, ppy1, ppz1, am1) = getPP(h=ION_HEIGHT, mPosition=position, direction=diritrf) #get pp in lon,lat h latpp, lonpp, height = ITRFToWGS84(ppx1, ppy1, ppz1) else: print('unable to compute position parameters - exiting!') return -1, -1, -1 return latpp, lonpp, height, lon, lat, am1
def mjd2lst(mjd, position=None): """ Converts a Modified Julian Date into Local Apparent Sidereal Time in seconds at a given position. If position is None, we default to the reference position of CS002. mjd -- Modified Julian Date (float, in days) position -- Position (pyrap measure) """ dm = measures() position = position or dm.position( "ITRF", "%fm" % ITRF_X, "%fm" % ITRF_Y, "%fm" % ITRF_Z ) dm.do_frame(position) last = dm.measure(dm.epoch("UTC", "%fd" % mjd), "LAST") fractional_day = last['m0']['value'] % 1 return fractional_day * 24 * SECONDS_IN_HOUR
def radec2azel(ra, dec, time, pos): me = measures() if type(ra) != str: ra = str(ra) + 'rad' if type(dec) != str: dec = str(dec) + 'rad' phasedir = me.direction('J2000', ra, dec) t = me.epoch("UTC", qu.quantity(time)) me.do_frame(t) p = me.position('ITRF', str(pos[0]) + 'm', str(pos[1]) + 'm', str(pos[2]) + 'm') me.do_frame(p) #print ("input radec2azel",phasedir,ra,dec,p,t) azel = me.measure(phasedir, 'azel') return azel
def gal_to_eq(lon_l, lat_b): """Find the Galactic co-ordinates of a source given the equatorial co-ordinates Keyword arguments: (l, b) -- Galactic longitude and latitude, in decimal degrees Return value: (alpha, delta) -- RA, Dec in decimal degrees """ dm = measures() result = dm.measure( dm.direction("GALACTIC", "%fdeg" % lon_l, "%fdeg" % lat_b), "J2000") ra = math.degrees(result['m0']['value']) % 360 # 0 < ra < 360 dec = math.degrees(result['m1']['value']) return ra, dec
def is_bright_source_near(accessor, distance=20): """ Checks if there is any of the bright radio sources defined in targets near the center of the image. :param accessor: a TKP accessor :param distance: maximum allowed distance of a bright source (in degrees) :returns: False if not bright source is near, description of source if a bright source is near """ #TODO: this function should be split up and tested more atomically # The measures object is our interface to pyrap m = measures() # First, you need to set the reference frame -- ie, the time # -- used for the calculations to come. Time as MJD in seconds. starttime = int(accessor.taustart_ts.strftime("%s")) starttime_mjd = unix2julian(starttime) m.do_frame(m.epoch("UTC", "%ss" % starttime_mjd)) # Now check and ensure the ephemeris in use is actually valid for this # data. if not check_for_valid_ephemeris(m): logger.warn("Bright source check failed due to invalid ephemeris") return "Invalid ephemeris" # Second, you need to set your image pointing. pointing = m.direction( "J2000", "%sdeg" % accessor.centre_ra, "%sdeg" % accessor.centre_decl ) for name, position in targets.items(): if not position: direction = m.direction(name) else: direction = m.direction( "J2000", "%srad" % position['ra'], "%srad" % position['dec'] ) separation = m.separation(pointing, direction).get_value("deg") if separation < distance: return "Pointing is %s degrees from %s." % (separation, name) return False
def is_bright_source_near(accessor, distance=20): """ Checks if there is any of the bright radio sources defined in targets near the center of the image. :param accessor: a TKP accessor :param distance: maximum allowed distance of a bright source (in degrees) :returns: False if not bright source is near, description of source if a bright source is near """ #TODO: this function should be split up and tested more atomically # The measures object is our interface to pyrap m = measures() # First, you need to set the reference frame -- ie, the time # -- used for the calculations to come. Time as MJD in seconds. starttime = int(accessor.taustart_ts.strftime("%s")) starttime_mjd = unix2julian(starttime) m.do_frame(m.epoch("UTC", "%ss" % starttime_mjd)) # Now check and ensure the ephemeris in use is actually valid for this # data. if not check_for_valid_ephemeris(m): logger.warn("Bright source check failed due to invalid ephemeris") return "Invalid ephemeris" # Second, you need to set your image pointing. pointing = m.direction("J2000", "%sdeg" % accessor.centre_ra, "%sdeg" % accessor.centre_decl) for name, position in targets.items(): if not position: direction = m.direction(name) else: direction = m.direction("J2000", "%srad" % position['ra'], "%srad" % position['dec']) separation = m.separation(pointing, direction).get_value("deg") if separation < distance: return "Pointing is %s degrees from %s." % (separation, name) return False
def gal_to_eq(lon_l, lat_b): """Find the Galactic co-ordinates of a source given the equatorial co-ordinates Keyword arguments: (l, b) -- Galactic longitude and latitude, in decimal degrees Return value: (alpha, delta) -- RA, Dec in decimal degrees """ dm = measures() result = dm.measure( dm.direction("GALACTIC", "%fdeg" % lon_l, "%fdeg" % lat_b), "J2000" ) ra = math.degrees(result['m0']['value']) % 360 # 0 < ra < 360 dec = math.degrees(result['m1']['value']) return ra, dec
def printJones(args,model): stnName=args[0] bTime = datetime.strptime(args[1], "%Y-%m-%d %H:%M:%S") duration =timedelta(0,float(args[2])) stepTime =timedelta(0,float(args[3])) ra=args[4]+'rad' dec=args[5]+'rad' freq=args[6] eTime = bTime+duration Times=[] for ti in range(0,duration.seconds/stepTime.seconds): Times.append( (quantity((bTime+ti*stepTime).isoformat())).get_value() ) #obsTimes.append(beginTime+ti*stepTime) obsTimes=quantity(Times,'d') srcDir=measures().direction('J2000', ra,dec) Jn=getJonesByAntFld(model,obsTimes,stnName,srcDir,freq) #plotJonesGnuplot(quantity(obsTimes.get_value()[0],obsTimes.get_unit()).formatted("YMD"),stepTime.seconds,Jn) for ti in range(0,duration.seconds/stepTime.seconds): print obsTimes.get_value()[ti],Jn[ti,0,0].real,Jn[ti,0,0].imag,Jn[ti,0,1].real,Jn[ti,0,1].imag,Jn[ti,1,0].real,Jn[ti,1,0].imag,Jn[ti,1,1].real,Jn[ti,1,1].imag
def getuvw(ra, dec, time, pos1, pos2): me = measures() if type(ra) != str: ra = str(ra) + 'rad' if type(dec) != str: dec = str(dec) + 'rad' phasedir = me.direction('J2000', ra, dec) me.do_frame(phasedir) t = me.epoch("UTC", qu.quantity(time)) me.do_frame(t) p = me.position('ITRF', str(pos1[0]) + 'm', str(pos1[1]) + 'm', str(pos1[2]) + 'm') bl = me.baseline('ITRF', str(pos2[0] - pos1[0]) + 'm', str(pos2[1] - pos1[1]) + 'm', str(pos2[2] - pos1[2]) + 'm') #print (bl) me.do_frame(p)
def getlonlatheight(az,el,position,h=ION_HEIGHT): if HAS_PYRAP: lonlat=getLonLatStation(az,el,pos=position); lon=lonlat['m0']['value']; lat=lonlat['m1']['value']; # convert to itrf coordinates on sphere with radius 1 diritrf=[cos(lat)*cos(lon),cos(lat)*sin(lon),sin(lat)] # calculate piercepoint in xyz(code from Bas vd Tol) pos_pp = (ppx1,ppy1,ppz1,am1)=getPP(h=h,mPosition=position,direction=diritrf) #get pp in lon,lat h me=measures(); pp1position=me.position("ITRF",str(ppx1)+'m',str(ppy1)+'m',str(ppz1)+'m') lonpp = degrees(pp1position['m0']['value']); latpp = degrees(pp1position['m1']['value']); height = pp1position['m2']['value'] / 1.0e3 elif HAS_EPHEM: slantRange = 3.0e20 # seem to need a large value to # get near the WNB measures equivalent # lat,lon,ht = aer2ecef(math.degrees(body.az), math.degrees(body.alt), slantRange, math.degrees(geocentric_latitude), location_lon, ION_HEIGHT) location_lat, location_lon, location_height = ITRFToWGS84(position[0], position[1], position[2]) lat,lon,ht = aer2ecef(math.degrees(az), math.degrees(el), slantRange, location_lat, location_lon, ION_HEIGHT) # convert to itrf coordinates on sphere with radius 1 lat = math.radians(lat) lon = math.radians(lon) diritrf=[cos(lat)*cos(lon),cos(lat)*sin(lon),sin(lat)] # calculate piercepoint in xyz(code from Bas vd Tol) pos_pp = (ppx1,ppy1,ppz1,am1)=getPP(h=ION_HEIGHT,mPosition=position,direction=diritrf) #get pp in lon,lat h latpp, lonpp, height = ITRFToWGS84(ppx1,ppy1,ppz1) else: print ('unable to compute position parameters - exiting!') return -1,-1,-1 return latpp, lonpp, height,lon,lat,am1
def main(ms_input, min_separation = 30, outputimage = None): """ Print seperation of the phase reference center of an input MS Parameters ---------- ms_input : str String from the list (map) of the calibrator MSs Returns ------- 0 --just for printing """ msname = input2strlist_nomapfile(ms_input)[0] # Create a measures object me = pm.measures() # Open the measurement set and the antenna and pointing table ms = pt.table(msname) # Get the position of the first antenna and set it as reference frame ant_table = pt.table(msname + '::ANTENNA') ant_no = 0 pos = ant_table.getcol('POSITION') x = qa.quantity( pos[ant_no,0], 'm' ) y = qa.quantity( pos[ant_no,1], 'm' ) z = qa.quantity( pos[ant_no,2], 'm' ) position = me.position( 'wgs84', x, y, z ) me.doframe( position ) ant_table.close() # Get the first pointing of the first antenna field_table = pt.table(msname + '::FIELD') field_no = 0 direction = field_table.getcol('PHASE_DIR') ra = direction[ ant_no, field_no, 0 ] dec = direction[ ant_no, field_no, 1 ] targets.insert(0, {'name' : 'Pointing', 'ra' : ra, 'dec' : dec}) field_table.close() # Get a ordered list of unique time stamps from the measurement set time_table = pt.taql('select TIME from $1 orderby distinct TIME', tables = [ms]) time = time_table.getcol('TIME') time1 = time/3600.0 time1 = time1 - pylab.floor(time1[0]/24)*24 ra_qa = qa.quantity( targets[0]['ra'], 'rad' ) dec_qa = qa.quantity( targets[0]['dec'], 'rad' ) pointing = me.direction('j2000', ra_qa, dec_qa) separations = [] print('SEPARATION from A-Team sources') print('------------------------------') print('The minimal accepted distance to an A-Team source is: ' + str(min_separation) + ' deg.') for target in targets: t = qa.quantity(time[0], 's') t1 = me.epoch('utc', t) me.doframe(t1) if 'ra' in target.keys(): ra_qa = qa.quantity( target['ra'], 'rad' ) dec_qa = qa.quantity( target['dec'], 'rad' ) direction = me.direction('j2000', ra_qa, dec_qa) else : direction = me.direction(target['name']) separations.append(me.separation(pointing, direction)) # Loop through all time stamps and calculate the elevation of the pointing el = [] for t in time: t_qa = qa.quantity(t, 's') t1 = me.epoch('utc', t_qa) me.doframe(t1) a = me.measure(direction, 'azel') elevation = a['m1'] el.append(elevation['value']/pylab.pi*180) el = numpy.array(el) pylab.plot(time1, el) if target['name'] != 'Pointing': print(target['name'] + ': ' + str(me.separation(pointing, direction))) if int(float(min_separation)) > int(float(str(me.separation(pointing, direction)).split(' deg')[0])): print('WARNING: The A-Team source ' + target['name'] + ' is closer than ' + str(min_separation) + ' deg to the phase reference center. Calibration might not perform as expected.') print('------------------------------') pylab.title('Pointing Elevation') pylab.title('Elevation') pylab.ylabel('Elevation (deg)'); pylab.xlabel('Time (h)'); pylab.legend( [ target['name'] + '(' + separation.to_string() + ')' for target, separation in zip(targets, separations) ]) if outputimage != None: inspection_directory = os.path.dirname(outputimage) if not os.path.exists(inspection_directory) and inspection_directory != '': os.makedirs(inspection_directory) print('Directory ' + inspection_directory + ' created.') elif inspection_directory != '': print('Directory ' + inspection_directory + ' already exists.') print('Plotting A-Team elevation to: ' + outputimage) pylab.savefig(outputimage) sys.exit(0)
def main((frequency, msname, minst, maxst, refms)): # Arguments as a tuple to make threading easier assert maxst+1 > minst # Set frequency of the MS to the specified frequency freqmhz = int(frequency/1.e6) print frequency,freqmhz t = pt.table(msname+'/SPECTRAL_WINDOW',readonly=False,ack=False) t.putcol('REF_FREQUENCY',np.array([frequency])) t.putcol('CHAN_FREQ',np.array([[frequency]])) t.close() t = pt.table(msname,ack=False) timecol = t.getcol('TIME') msreftime = min(timecol) print 'time reference', msreftime JD = msreftime/3600./24. + 2400000.5 print 'MS JD', JD # makeresponseimage abs=true frames=1 ms=freq00.MS size=350 cellsize=1200arcsec stations=0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59 hs = 100. ds = 1 # downsample, ** was 2 ** xvals = np.arange(-hs,hs+1.) X,Y = np.meshgrid(xvals,xvals) R=np.hypot(X,Y) #w = wcs.WCS(naxes=2) #w.wcs.crpix=[151,151] #w.wcs.cdelt=array([165./301.,165./301.]) #w.wcs.crval=[0.,90.] #w.wcs.ctype=['RA---ZEA','DEC--ZEA'] h = pyfits.open('wcs_azimuth_201.fits') w = pywcs.WCS(h[0].header) els = np.zeros(R.shape) azs = np.zeros(R.shape) for i in range(R.shape[0]): for j in range(R.shape[1]): azel = w.wcs_pix2sky([[i,j]],0) if azel[0][1]<0.: els[i,j]=np.nan azs[i,j]=np.nan elif ((i-hs)**2+(j-hs)**2)**0.5 > 1.2*hs: els[i,j]=np.nan azs[i,j]=np.nan else: els[i,j]=azel[0][1]*np.pi/180. azs[i,j]=azel[0][0]*np.pi/180. ra = np.zeros(R.shape) dec = np.zeros(R.shape) t = pt.table(msname+'/ANTENNA',ack=False) stcol = t.getcol('NAME') poscol = t.getcol('POSITION') #stations = range(len(stcol)) stations = range(minst,maxst+1) print len(stations), 'stations' beamintmap = np.zeros((2,len(stations),len(range(0,len(xvals),ds)),len(range(0,len(xvals),ds)))) beamtmpmap = np.zeros((2,len(stations),len(xvals),len(xvals))) azmap = np.zeros((len(range(0,len(xvals),ds)),len(range(0,len(xvals),ds)))) elmap = np.zeros((len(range(0,len(xvals),ds)),len(range(0,len(xvals),ds)))) sr = lsr.stationresponse(msname, useElementResponse=True) stll = {} for line in open('stations_positions.txt'): sline = line.split() stll[sline[0]]=[float(sline[2])*np.pi/180.,float(sline[3])*np.pi/180.] # lon, lat t = time.time() # directionmap contains itrf directions for every x,y point directionmap=np.zeros((len(xvals),len(xvals),3)) evals=0 for ss in range(len(stations)): # Convert azel to RA/DEC for this station meas=pm.measures() # Set station position meas.do_frame(meas.position('ITRF',*(str(coord)+"m" for coord in poscol[ss]))) # Set reference time meas.do_frame(meas.epoch('utc',pyrap.quanta.quantity(msreftime,'s'))) for i in range(0,len(xvals),ds): for j in range(0,len(xvals),ds): if not np.isnan(els[i,j]): radecmeas=meas.measure(meas.direction('AZEL', str(azs[i,j])+' rad', str(els[i,j])+' rad'),'J2000') ra[i,j]=radecmeas['m0']['value'] dec[i,j]=radecmeas['m1']['value'] directionmap*=0.; for i in range(0,len(xvals),ds): for j in range(0,len(xvals),ds): if not np.isnan(dec[i,j]): sr.setDirection(ra[i,j],dec[i,j]) tmpdirection=sr.getDirection(msreftime) directionmap[i,j,:]=tmpdirection if refms is None: print "Using all points" pointsgenerator=allpointsgenerator(); else: print "Using points in",refms pointsgenerator=mypointsgenerator(refms,stcol[ss]) for i,j in pointsgenerator: azmap[i/ds,j/ds]=azs[i,j] elmap[i/ds,j/ds]=els[i,j] #print dec[i,j] #exit() lenxvals=len(xvals) tmpra=0. tmpdec=0. if not np.isnan(dec[i,j]): thisra=ra[i,j] thisdec=dec[i,j] sr.setRefDelay(thisra,thisdec) sr.setRefTile(thisra,thisdec) refdelay=sr.getRefDelay(msreftime) reftile=sr.getRefTile(msreftime) beamtmpmap*=0. for x in xrange(lenxvals): for y in xrange(lenxvals): if not np.isnan(dec[x,y]): #tmpra = ra[x,y] #tmpdec = dec[x,y] #sr.setDirection(tmpra,tmpdec) #mydirection=sr.getDirection(msreftime) mydirection=directionmap[x,y] bmj=sr.evaluateFreqITRF(msreftime,stations[ss],frequency,mydirection,refdelay,reftile) #bmj1=sr.evaluateFreq(msreftime,stations[ss],frequency) evals+=1 #beamtmpmap[ss,x,y]=np.sum(np.abs(bmj)) beamtmpmap[0,ss,x,y]=np.sqrt(np.abs(bmj[0,0])**2+np.abs(bmj[0,1])**2) beamtmpmap[1,ss,x,y]=np.sqrt(np.abs(bmj[1,1])**2+np.abs(bmj[1,0])**2) beamintmap[0,ss,j/ds,i/ds]=np.sum(beamtmpmap[0,ss,:,:])#*cosel) beamintmap[1,ss,j/ds,i/ds]=np.sum(beamtmpmap[1,ss,:,:])#*cosel) print ss,'/',len(stations),'-',i/ds,'/',len(range(0,len(xvals),ds)),':',int(time.time()-t),'sec elapsed so far,',evals,'beam evaluations' header = h[0].header #header['CRPIX1']=51. #header['CRPIX2']=51. #pixsize = header['CDELT1'] #pixsize *= 2. #header['CDELT1']=pixsize #header['CDELT2']=pixsize for stationnr in range(minst, maxst+1): stationname=stcol[stationnr] hdu = pyfits.PrimaryHDU(header=header,data=beamintmap[0,stationnr-minst,]) hdu.writeto('beamcubexx-%s-%dMHz.fits'%(stationname,freqmhz),clobber=True) hdu = pyfits.PrimaryHDU(header=header,data=beamintmap[1,stationnr-minst,]) hdu.writeto('beamcubeyy-%s-%dMHz.fits'%(stationname,freqmhz),clobber=True)
def getAzEl(pointing, time, position, ha_limit=-1000): if HAS_PYRAP: if ha_limit == -1000: azel = radec2azel(pointing[0], pointing[1], time=str(time) + 's', pos=position) az = azel['m0']['value'] el = azel['m1']['value'] else: me = measures() p = me.position("ITRF", str(position[0]) + 'm', str(position[1]) + 'm', str(position[2]) + 'm') t = me.epoch("UTC", qu.quantity(str(time) + 's')) phasedir = me.direction('J2000', str(pointing[0]) + 'rad', str(pointing[1]) + 'rad') me.doframe(p) me.doframe(t) hadec = me.measure(phasedir, "HADEC") if abs(hadec['m0']['value']) > ha_limit: print("below horizon", tab.taql('calc ctod($time s)')[0], degrees(hadec['m0']['value']), degrees(hadec['m1']['value'])) return 999, 999 else: azel = me.measure(phasedir, "AZELGEO") az = azel['m0']['value'] el = azel['m1']['value'] elif HAS_EPHEM: if ha_limit != -1000: print( "limiting on HA/DEC not implemented for PyEphem yet, ignoring") location_lat, location_lon, location_height = ITRFToWGS84( position[0], position[1], position[2]) location = ephem.Observer() # convert geodetic latitude to geocentric # flattening, f, defined above for WGS84 stuff geodet_lat = math.radians(location_lat) tan_geocentric_latitude = math.tan(geodet_lat) * (1 - f)**2 geocentric_latitude = GeodeticToGeocentricLat(geodet_lat, location_height) location.lat = geocentric_latitude location.lon = math.radians(location_lon) location.elevation = location_height location.pressure = 0.0 # convert to Dublin Julian Date for PyEphem location.date = time / 86400.0 - 15019.5 lst = location.sidereal_time() equatorial = ephem.Equatorial( str(12.0 * math.degrees(pointing[0]) / 180), str(math.degrees(pointing[1]))) body = ephem.FixedBody() body._ra = equatorial.ra body._dec = equatorial.dec body._epoch = equatorial.epoch body.compute(location) az = math.degrees(body.az) * math.pi / 180.0 el = math.degrees(body.alt) * math.pi / 180.0 else: print('failure to get azimuth and elevation! Exiting!') return -1, -1 return az, el
def synthesized_uvw(ants, time, phase_dir, auto_correlations): """ Synthesizes new UVW coordinates based on time according to NRAO CASA convention (same as in fixvis) User should check these UVW coordinates carefully: if time centroid was used to compute original uvw coordinates the centroids of these new coordinates may be wrong, depending on whether data timesteps were heavily flagged. """ pytest.importorskip('pyrap') from pyrap.measures import measures from pyrap.quanta import quantity as q dm = measures() epoch = dm.epoch("UT1", q(time[0], "s")) ref_dir = dm.direction("j2000", q(phase_dir[0], "rad"), q(phase_dir[1], "rad")) ox, oy, oz = ants[0] obs = dm.position("ITRF", q(ox, "m"), q(oy, "m"), q(oz, "m")) # Setup local horizon coordinate frame with antenna 0 as reference position dm.do_frame(obs) dm.do_frame(ref_dir) dm.do_frame(epoch) ant1, ant2 = np.triu_indices(ants.shape[0], 0 if auto_correlations else 1) ant1 = ant1.astype(np.int32) ant2 = ant2.astype(np.int32) ntime = time.shape[0] nbl = ant1.shape[0] rows = ntime * nbl uvw = np.empty((rows, 3), dtype=np.float64) # For each timestep for ti, t in enumerate(time): epoch = dm.epoch("UT1", q(t, "s")) dm.do_frame(epoch) ant_uvw = np.zeros_like(ants) # Calculate antenna UVW positions for ai, (x, y, z) in enumerate(ants): bl = dm.baseline("ITRF", q([x, ox], "m"), q([y, oy], "m"), q([z, oz], "m")) ant_uvw[ai] = dm.to_uvw(bl)["xyz"].get_value()[0:3] # Now calculate baseline UVW positions # noting that ant1 - ant2 is the CASA convention base = ti*nbl uvw[base:base + nbl, :] = ant_uvw[ant1] - ant_uvw[ant2] return ant1, ant2, uvw
#!/usr/bin/python from pylab import * import pyrap.tables as tab from pyrap.measures import measures import pyrap.quanta as qa; me=measures() light_speed=299792458. class stationBeam(): def __init__(self,ms,times=[],direction=[]): myt=tab.table(ms+'/LOFAR_ANTENNA_FIELD') self.flags=myt.getcol("ELEMENT_FLAG") self.offsets=myt.getcol("ELEMENT_OFFSET") myt=tab.table(ms+'/ANTENNA') self.fieldcenter=myt.getcol("LOFAR_PHASE_REFERENCE") self.stationcenter=myt.getcol("POSITION") self.offsetshift=self.fieldcenter-self.stationcenter self.ms=ms self.refdir=tab.table(ms+'FIELD').getcol('PHASE_DIR')[0][0] itrfdir=[] self.times=times self.direction=direction self.initiated=False self.init_times() def init_times(self): if not list(self.direction): print "cannot init, set direction first" if not list(self.times): print "cannot init, set times first"
def synthesize_uvw(station_ECEF, time, a1, a2, phase_ref, stopctr_units=["rad", "rad"], stopctr_epoch="j2000", time_TZ="UTC", time_unit="s", posframe="ITRF", posunits=["m", "m", "m"]): """ Synthesizes new UVW coordinates based on time according to NRAO CASA convention (same as in fixvis) station_ECEF: ITRF station coordinates read from MS::ANTENNA time: time column, preferably time centroid a1: ANTENNA_1 index a2: ANTENNA_2 index phase_ref: phase reference centre in radians returns dictionary of dense uvw coordinates and indices: { "UVW": shape (nbl * ntime, 3), "TIME_CENTROID": shape (nbl * ntime,), "ANTENNA_1": shape (nbl * ntime,), "ANTENNA_2": shape (nbl * ntime,) } Note: input and output antenna indexes may not have the same order or be flipped in 1 to 2 index Note: This operation CANNOT be applied blockwise due to a casacore.measures threadsafety issue """ assert time.size == a1.size assert a1.size == a2.size ants = np.concatenate((a1, a2)) unique_ants = np.unique(ants) unique_time = np.unique(time) na = unique_ants.size nbl = na * (na - 1) // 2 + na ntime = unique_time.size # keep a full uvw array for all antennae - including those # dropped by previous calibration and CASA splitting padded_uvw = np.zeros((ntime * nbl, 3), dtype=np.float64) antindices = np.stack(np.triu_indices(na, 0), axis=1) padded_time = unique_time.repeat(nbl) padded_a1 = np.tile(antindices[:, 0], (1, ntime)).ravel() padded_a2 = np.tile(antindices[:, 1], (1, ntime)).ravel() dm = measures() epoch = dm.epoch(time_TZ, quantity(time[0], time_unit)) refdir = dm.direction(stopctr_epoch, quantity(phase_ref[0, 0], stopctr_units[0]), quantity(phase_ref[0, 1], stopctr_units[1])) obs = dm.position(posframe, quantity(station_ECEF[0, 0], posunits[0]), quantity(station_ECEF[0, 1], posunits[1]), quantity(station_ECEF[0, 2], posunits[2])) #setup local horizon coordinate frame with antenna 0 as reference position dm.do_frame(obs) dm.do_frame(refdir) dm.do_frame(epoch) p = progress(unique_time.size) for ti, t in enumerate(unique_time): p.increment() epoch = dm.epoch("UT1", quantity(t, "s")) dm.do_frame(epoch) station_uv = np.zeros_like(station_ECEF) for iapos, apos in enumerate(station_ECEF): compuvw = dm.to_uvw( dm.baseline( posframe, quantity([apos[0], station_ECEF[0, 0]], posunits[0]), quantity([apos[1], station_ECEF[0, 1]], posunits[1]), quantity([apos[2], station_ECEF[0, 2]], posunits[2]))) station_uv[iapos] = compuvw["xyz"].get_value()[0:3] for bl in range(nbl): blants = antindices[bl] bla1 = blants[0] bla2 = blants[1] # same as in CASA convention (Convention for UVW calculations in CASA, Rau 2013) padded_uvw[ti * nbl + bl, :] = station_uv[bla1] - station_uv[bla2] return dict( zip(["UVW", "TIME_CENTROID", "ANTENNA1", "ANTENNA2"], [padded_uvw, padded_time, padded_a1, padded_a2]))
def test_uvw_new_casacore(msname): r''' Test if UVW = UVW_FROM_ITRF(XYZ_ANTENNA2 - XYZ_ANTENNA1) ''' dm = measures() tab = table(msname) ant1 = tab.getcol('ANTENNA1') ant2 = tab.getcol('ANTENNA2') uvw = tab.getcol('UVW') time = tab.getcol('TIME') time_epochs = [ dm.epoch('UTC', quantity(mjds, 's')) for mjds in concatenate([time[:3000], time[-3000:]], axis=0) ] field_table = table(tab.getkeyword('FIELD')) phase_dir = field_table.getcol('PHASE_DIR')[0, 0, :] phase_dir_meas_info = field_table.getcolkeyword('PHASE_DIR', 'MEASINFO') phase_dir_units = field_table.getcolkeyword('PHASE_DIR', 'QuantumUnits') phase_dir_quantities = [ '%r%s' % (angle, unit) for angle, unit in zip(phase_dir, phase_dir_units) ] lofar = dm.position('ITRF', '3826577.462m', '461022.624m', '5064892.526m') dm.do_frame(lofar) dm.do_frame( dm.direction(phase_dir_meas_info['Ref'], phase_dir_quantities[0], phase_dir_quantities[1])) ant_table = table(tab.getkeyword('ANTENNA')) xyz = ant_table.getcol('POSITION') xyz_1 = array([xyz[ant, :] for ant in ant1]) xyz_2 = array([xyz[ant, :] for ant in ant2]) delta_xyz = (xyz_2 - xyz_1) uvw_computed = [] for ant1_xyz, ant2_xyz, epoch in zip(xyz_1[:2000], xyz_2[:2000], time_epochs): dm.do_frame(epoch) ant_1_quant = [quantity(p, 'm') for p in ant1_xyz] ant_2_quant = [quantity(p, 'm') for p in ant2_xyz] ant_pos = dm.position( 'ITRF', list(map(list, list(zip(ant_1_quant, ant_2_quant))))) bl_quant = [quantity(value, 'm') for value in dxyz] #print bl_quant bl = dm.baseline('ITRF', bl_quant[0], bl_quant[1], bl_quant[2]) #print bl uvw_computed.append(dm.to_uvw(bl)) for a1, a2, uvw_ms, uvw_comp in zip(ant1, ant2, uvw, uvw_computed): uvw_comp = array(uvw_comp['xyz'].get_value('m')) if norm(uvw_ms) > 0.0: arg = inner(uvw_ms, uvw_comp) / (norm(uvw_ms)**2) if arg > 1.0: arg = 1.0 if arg < -1.0: arg = -1.0 angle_mas = arccos(arg) * 180 * 3600 * 1000. / pi if angle_mas > 50.0: fmt = 'Angle UVW computed / MS %03d--%03d = %.3f mas (%.3f deg)' raise ValueError( fmt % (a1, a2, angle_mas, angle_mas / 1000. / 3600.0)) diff = uvw_ms - uvw_comp if norm(diff) > 1e-3: raise ValueError('UVW computed - UVW MS %03d--%03d = %.3f mm' % (a1, a2, norm(diff) * 1e3)) return True
def __init__(self, parameters): print "\033[94mPython MyATerm constructor\033[0m" print parameters.keys() #self.img = pyrap.images.image('beam.img').getdata()[0,0,:,:] # Read the necessary parameters from the parameterset. ms = str(parameters["ms"]) beamname = str(parameters['beamname']) support = int(str(parameters['SpheSupport'])) print "Reading beam patterns with prefix: ", beamname # 8 FITS files (4 pol each for real & imag parts) per station per frequency # HDUs are ordered as xx_re, xx_im, xy_re, xy_im, yx_re, yx_im, yy_re, yy_im hdulist = [] # Holds all the images for one station for pol in ['_xx', '_xy', '_yx', '_yy']: for comp in ['_re', '_im']: temphdu = pyfits.open(beamname + pol + comp + '.fits')[0] # Extract the first HDU hdulist.append(temphdu) # Get the frequency range from the FITS header. hdu0 = hdulist[0] nfreq = hdu0.header['NAXIS3'] dim = float(hdu0.header['NAXIS1']) # Assume this is a square image. self.Im_per_station = 8 self.pa_step = 5 # Expose this as a parameter to the user. self.freqlist = [] # in Hertz if 'GFREQ1' in hdu0.header: for keyindex in range(1, nfreq + 1): self.freqlist.append(hdu0.header['GFREQ%i' % keyindex]) else: #for keyindex in range(1,nfreq+1): #self.freqlist.append( pass # Compute a set of angles by which the beams need to be rotated every timestamp. dm = measures() field = 0 # this is used as the FIELD_ID - assume this to be zero for now; this is the field whose observed times we need from the MS. zenith = dm.direction('AZEL', '0deg', '90deg') tab = pt.table('TEST.MS') antpos = pt.table(tab.getkeyword("ANTENNA")).getcol("POSITION") ra, dec = pt.table(tab.getkeyword("FIELD")).getcol( "PHASE_DIR", field, 1)[0][0] # make position measure from antenna 0 pos0 = dm.position('itrf', *[dq.quantity(x, 'm') for x in antpos[0]]) dm.do_frame(pos0) # make direction measure from field centre fld = dm.direction('J2000', dq.quantity(ra, "rad"), dq.quantity(dec, "rad")) tab = tab.query("FIELD_ID==%d" % field) # get unique times times = numpy.array( sorted(set(tab.getcol("TIME")[~tab.getcol("FLAG_ROW")]))) pa_times = [(dm.do_frame(dm.epoch("UTC", dq.quantity(t, "s"))) and (dm.posangle(fld, zenith).get_value("deg"), t)) for t in times] pa_times = sorted(pa_times) start_pa = pa_times[0][0] pa_filtered = [pa_times[0]] for tup in pa_times: if tup[0] - start_pa < self.pa_step: continue pa_filtered.append(tup) start_pa = tup[0] print pa_filtered # Downsample / Interpolate (upsample?) the beam patterns to the support function size. import time t1 = time.time() print 'Start time: ', t1 self.beams = [] '''for polcomp in range(0,self.Im_per_station): for freq in range(0,nfreq): hdulist[polcomp].data[freq,:,:] = scipy.ndimage.zoom(hdulist[polcomp].data[freq,:,:] tempbeam = hdulist[polcomp].data[freq,:,:] #print tempbeam.shape temprotbeam = scipy.ndimage.rotate(tempbeam,p_angles[i]) #print "Temprotbeam dimensions: ", temprotbeam.shape aterms[polcomp,freq,:,:]= scipy.ndimage.zoom(temprotbeam,support/float(temprotbeam.shape[0]))''' self.times_filtered = [] for pa_t in pa_filtered: print "pa_t: ", pa_t print "printed" aterms = numpy.zeros( (self.Im_per_station, nfreq, support, support)) for polcomp in range(0, self.Im_per_station): for freq in range(0, nfreq): tempbeam = hdulist[polcomp].data[freq, :, :] temprotbeam = tempbeam #scipy.ndimage.rotate(tempbeam,pa_t[0]) aterms[polcomp, freq, :, :] = temprotbeam[ 0:support, 0: support] #scipy.ndimage.zoom(temprotbeam,support/float(temprotbeam.shape[0])) self.beams.append((pa_t[1], aterms)) self.times_filtered.append(pa_t[1]) t2 = time.time() print 'Stop time: ', t2 print "Time to pre-compute a-terms: ", (t2 - t1) / 60.0, " minutes."
def azishe(config='$CFG'): # Get parameters from json file with open(II(config)) as jsn_std: jparams = json.load(jsn_std) # Remove empty strings and convert unicode characters to strings params = {} for key, val in jparams.iteritems(): # Make sure all keys are strings _key = str(key) # ignore empty strings and comments if val=="" or _key=="#": pass # convert unicode values to strings elif isinstance(val,unicode): params[_key] = str(val).lower() else: params[_key] = val get_opts = lambda prefix: filter(lambda a: a[0].startswith(prefix), params.items()) # Retrieve MS and imager options ms_dict = dict([(key.split('ms_')[-1],val) for (key,val) in get_opts('ms_') ] ) im_dict = dict([(key.split('im_')[-1],val) for (key,val) in get_opts('im_') ] ) # Seperate deconvolution settings _deconv = {} for dcv in 'lwimager wsclean moresane casa'.split(): if params[dcv]: _deconv.update( {dcv:dict([(key.split(dcv+'_')[-1],val) for (key,val) in get_opts(dcv+'_') ] )} ) # Set imaging options im.IMAGER = imager.IMAGER = params['imager'].lower() for opt in 'npix weight robust mode stokes'.split(): if opt in im_dict.keys(): setattr(im,opt,im_dict.pop(opt)) im.cellsize = '%farcsec'%(im_dict.pop('cellsize')) im.stokes = im.stokes.upper() weight_fov = im_dict.pop('weight_fov') if weight_fov: im.weight_fov = '%farcmin'%weight_fov # Create empty MS synthesis = ms_dict.pop('synthesis') scalenoise = 1 if synthesis > 12: scalenoise = math.sqrt(12.0/synthesis) msname = II('rodrigues%f.MS'%(time.time())) obs = params['observatory'].lower() antennas = _OBS[obs] freq0 = ms_dict.pop('freq0')*1e6 dfreq = ms_dict.pop('dfreq')*1e3 direction = "J2000,%s,%s"%(ms_dict.pop('ra'),ms_dict.pop('dec')) ms.create_empty_ms(msname=msname,synthesis=synthesis,freq0=freq0, dfreq=dfreq,tel=obs,pos='%s/%s'%(OBSDIR,antennas), direction=direction,**ms_dict) if exists(msname): v.MS = msname else: abort("Something went wrong while creating the empty MS. Please check logs for details") makedir(DESTDIR) ms.plot_uvcov(ms=.1,width=10,height=10,dpi=150,save="$OUTFILE-uvcov.png") # Set noise for simulation spwtab = ms.ms(subtable="SPECTRAL_WINDOW") freq0 = spwtab.getcol("CHAN_FREQ")[ms.SPWID,0] spwtab.close() if params['add_noise']: noise = compute_vis_noise(sefd=get_sefd(freq0)) * scalenoise else: noise = 0 # Simulate Visibilities _katalog = params['katalog_id'] if _katalog: katalog = '%s/%s'%(KATDIR,_KATALOG[params['katalog_id']]) lsmname = II('${OUTDIR>/}${MS:BASE}.lsm.html') radius = float(params['radius']) fluxrange = params['fluxrange'].split('-') if len(fluxrange)>1: fluxrange = map(float,fluxrange) elif len(fluxrange)==1: fluxrange = [0,float(fluxrange[0])] select = '' fits = verify_sky(katalog) == 'FITS' if radius or fluxrange: if radius: select += '--select="r<%fdeg" '%radius if fluxrange: select += '--select="I<%f" '%fluxrange[1] select += '--select="I>%f" '%fluxrange[0] if not fits: x.sh('tigger-convert $select --recenter=$direction $katalog $lsmname -f') else: from pyrap.measures import measures dm = measures() direction = dm.direction('J2000',ms_opts[ra],ms_opts[dec]) ra = np.rad2deg(direction['m0']['value']) dec = np.rad2deg(direction['m1']['value']) hdu = pyfits.open(temp_file) hdu[0].hdr['CRVAL1'] = ra hdu[0].hdr['CRVAL2'] = dec hdu.writeto(tmp_file,clobber=True) simsky(lsmname=lsmname,tdlsec='turbo-sim:custom',noise=noise,column='CORRECTED_DATA') skymodel = params['sky_model'] if skymodel: simsky(lsmname=skymodel,noise=0 if katalog else noise,addToCol='CORRECTED_DATA') ## Finally Lets image # make dirty map im.make_image(psf=True,**im_dict) # Deconvolve for deconv in _deconv: deconv = deconv.lower() if deconv in STAND_ALONE_DECONV: im.make_image(algorithm=deconv,restore=_deconv[deconv],restore_lsm=False,**im_dict) else: im.IMAGER = deconv im.make_image(dirty=False,restore=_deconv[deconv],restore_lsm=False,**im_dict) xo.sh('tar -czvf ${OUTDIR>/}${MS:BASE}.tar.gz $msname')
def getHamakerJones(obsTimes,stnPos,stnRot,srcDirection,freq, doInvJ=False,doPolPrec=True,doCirc=False, showJones=False,doNormalize=False): obsTimesArr=obsTimes.get_value(); obsTimeUnit=obsTimes.get_unit() if doInvJ==True: InvJonesHam=zeros((len(obsTimesArr),2,2),dtype=complex) else: JonesHam=zeros((len(obsTimesArr),2,2),dtype=complex) me=measures() #Set position of reference frame w.r.t. ITRF me.doframe(stnPos) if doPolPrec: #Get sky precession rotation matrix #(Assuming no change over data interval) me.doframe(me.epoch('UTC',quantity(obsTimesArr[0],obsTimeUnit))) precMat=getSkyPrecessionMat(me,srcDirection) for ti in range(len(obsTimesArr)): #Set current time in reference frame me.doframe(me.epoch('UTC',quantity(obsTimesArr[ti],obsTimeUnit))) #print quantity(obsTimesArr[ti],obsTimeUnit) azel=me.measure(srcDirection,'AZEL') az=azel['m0']['value'] el=azel['m1']['value'] phihatcrt=np.matrix([-cos(az),sin(az),0]) thetahatcrt=np.matrix([sin(el)*sin(az),sin(el)*cos(az),-cos(el)]) #print "AZ",azel['m0']['value']/pi*180 #print "EL",azel['m1']['value']/pi*180 #print azel['m0']['value']/pi*180,azel['m1']['value']/pi*180 stnNorthOffsetAng=arccos( -(stnRot[0,1]*stnRot[0,2]+stnRot[1,1]*stnRot[1,2])/( sqrt(stnRot[0,1]**2+stnRot[1,1]**2)* sqrt(stnRot[0,2]**2+stnRot[1,2]**2))) phi=-az+stnNorthOffsetAng-pi/2.0 #Added -pi/2 theta=el (JXphi,JXtheta,JYphi,JYtheta)=HamakerJones.getJonesByAzEl(freq,phi,theta) JonesElemMat=np.matrix([[JXphi,JXtheta],[JYphi,JYtheta]]) #Compute parallactic rotation #Convert phase ref dir to current polar ITRF srcITRFpolar=me.measure(srcDirection,'ITRF') #Convert polar ITRF to cartesian ITRF srcITRFlon=srcITRFpolar['m0']['value'] srcITRFlat=srcITRFpolar['m1']['value'] lmnITRF =sph2crtGeo(srcITRFlon,srcITRFlat) srcN_ITRFpolar=me.measure( me.direction('J2000', str(srcDirection['m0']['value'])+'rad', str(srcDirection['m1']['value']+math.pi/2.0)+'rad') ,'ITRF') srcE_ITRFpolar=me.measure( me.direction('J2000', str(srcDirection['m0']['value']+math.pi/2.0)+'rad', str(0.0)+'rad') ,'ITRF') north=sph2crtGeo(srcN_ITRFpolar['m0']['value'],srcN_ITRFpolar['m1']['value']) east=sph2crtGeo(srcE_ITRFpolar['m0']['value'],srcE_ITRFpolar['m1']['value']) #print "N*E", north*east.T local_pointing= ((stnRot.T* lmnITRF.T).T) local_north= ((stnRot.T* north.T).T) local_east= np.cross (local_north.squeeze(), local_pointing.squeeze()) parallacticRot=np.matrix( [[(local_east*phihatcrt.T)[0,0],(local_north*phihatcrt.T)[0,0]], [(local_east*thetahatcrt.T)[0,0],(local_north*thetahatcrt.T)[0,0]]]) #print local_pointing*thetahatcrt.T #parallacticRot=np.matrix( # [[(local_east*thetahatcrt.T)[0,0],(local_north*thetahatcrt.T)[0,0]], # [(local_east*phihatcrt.T)[0,0],(local_north*phihatcrt.T)[0,0]]]) #print parallacticRot #bisectRot2D=bisectRot[0:2,0:2] #JonesHamMat=JonesElemMat*bisectRot2D * parallacticRot JonesHamMat=JonesElemMat * parallacticRot if doPolPrec: #With precession: JonesHamMat=JonesHamMat * precMat #else: #Do not apply precession rotation of polarimetric frame. #This is then the apparent polarization frame. if doCirc: #Let the brightness components be in circular basis: JonesHamMat=JonesHamMat*lin2circH if showJones: print JonesHamMat pass if doInvJ==True : IJonesHamMat=np.linalg.inv(JonesHamMat) if doNormalize: #JonesDipMat=JonesDipMat/sqrt(abs(np.linalg.det(JonesDipMat))) g=sqrt(np.matrix.trace(IJonesHamMat*IJonesHamMat.H)/2.0) IJonesHamMat=IJonesHamMat/g InvJonesHam[ti,:,:]=IJonesHamMat else: JonesHam[ti,:,:]=JonesHamMat if doInvJ==True: return InvJonesHam else: return JonesHam
if len(args) >0: model=args.pop(0) if options.obsID != "null" : args.append(' ') args.append(parseParset.getStartTime(options.obsID)) args.append(parseParset.getDuration(options.obsID)) args.append(parseParset.getStep(options.obsID)) args.append(str(parseParset.getRA(options.obsID))) args.append(str(parseParset.getDEC(options.obsID))) stnName,bTime,duration,stepTime,ra,dec=args2inpparms(args) freqs=parseParset.getSubbandFreqs(options.obsID) #freqs=freqs[0:2] stnNames=parseParset.getStnNames(options.obsID) #stnNames=[stnNames[0]] for stnName in stnNames: print "Station: ", stnName printJones(stnName,bTime,duration,stepTime,ra,dec,freqs,model) elif len(args) == 7: stnName,bTime,duration,stepTime,ra,dec=args2inpparms(args) freqs=[float(args[6])] eTime = bTime+duration Times=[] for ti in range(0,int(duration.total_seconds()/stepTime.seconds)): Times.append( (quantity((bTime+ti*stepTime).isoformat())).get_value() ) obsTimes=quantity(Times,'d') srcDir=measures().direction('J2000', ra,dec) #plotJones(stnName,obsTimes,freqs) printJones(stnName,obsTimes,srcDir,freqs,model) else : opt.error("incorrect number of arguments")
def test_invalid_ephemeris(self): dm = measures() # No(?) ephemeris we're likely to come across is valid at MJD 0. dm.do_frame(dm.epoch("UTC", "0.0d")) self.assertFalse(tkp.quality.brightsource.check_for_valid_ephemeris(dm))
def get_pointing_direction(ms): # Returns J2000 RA, dec of reference direction in radians fieldtable = table(ms.getkeyword('FIELD')) ra, dec = fieldtable.getcol('REFERENCE_DIR')[0][0] dm = measures() return dm.direction('j2000', "%frad" % (ra,), "%frad" % (dec,))
def testspecificlocation(self): last = 73647.97547170892 dm = measures() position = dm.position("itrf", "1m", "1m", "1m") self.assertTrue(abs(coordinates.jd2lst(self.JD, position) - last) < 1.0)
def azishe(config='$CFG'): # Get parameters from json file with open(II(config)) as jsn_std: jparams = json.load(jsn_std) # Remove empty strings and convert unicode characters to strings params = {} for key, val in jparams.iteritems(): # Make sure all keys are strings _key = str(key) # ignore empty strings and comments if val == "" or _key == "#": pass # convert unicode values to strings elif isinstance(val, unicode): params[_key] = str(val).lower() else: params[_key] = val get_opts = lambda prefix: filter(lambda a: a[0].startswith(prefix), params.items()) # Retrieve MS and imager options ms_dict = dict([(key.split('ms_')[-1], val) for (key, val) in get_opts('ms_')]) im_dict = dict([(key.split('im_')[-1], val) for (key, val) in get_opts('im_')]) # Seperate deconvolution settings _deconv = {} for dcv in 'lwimager wsclean moresane casa'.split(): if params[dcv]: _deconv.update({ dcv: dict([(key.split(dcv + '_')[-1], val) for (key, val) in get_opts(dcv + '_')]) }) # Set imaging options im.IMAGER = imager.IMAGER = params['imager'].lower() for opt in 'npix weight robust mode stokes'.split(): if opt in im_dict.keys(): setattr(im, opt, im_dict.pop(opt)) im.cellsize = '%farcsec' % (im_dict.pop('cellsize')) im.stokes = im.stokes.upper() weight_fov = im_dict.pop('weight_fov') if weight_fov: im.weight_fov = '%farcmin' % weight_fov # Create empty MS synthesis = ms_dict.pop('synthesis') scalenoise = 1 if synthesis > 12: scalenoise = math.sqrt(12.0 / synthesis) msname = II('rodrigues%f.MS' % (time.time())) obs = params['observatory'].lower() antennas = _OBS[obs] freq0 = ms_dict.pop('freq0') * 1e6 dfreq = ms_dict.pop('dfreq') * 1e3 direction = "J2000,%s,%s" % (ms_dict.pop('ra'), ms_dict.pop('dec')) ms.create_empty_ms(msname=msname, synthesis=synthesis, freq0=freq0, dfreq=dfreq, tel=obs, pos='%s/%s' % (OBSDIR, antennas), direction=direction, **ms_dict) if exists(msname): v.MS = msname else: abort( "Something went wrong while creating the empty MS. Please check logs for details" ) makedir(DESTDIR) ms.plot_uvcov(ms=.1, width=10, height=10, dpi=150, save="$OUTFILE-uvcov.png") # Set noise for simulation spwtab = ms.ms(subtable="SPECTRAL_WINDOW") freq0 = spwtab.getcol("CHAN_FREQ")[ms.SPWID, 0] spwtab.close() if params['add_noise']: noise = compute_vis_noise(sefd=get_sefd(freq0)) * scalenoise else: noise = 0 # Simulate Visibilities _katalog = params['katalog_id'] if _katalog: katalog = '%s/%s' % (KATDIR, _KATALOG[params['katalog_id']]) lsmname = II('${OUTDIR>/}${MS:BASE}.lsm.html') radius = float(params['radius']) fluxrange = params['fluxrange'].split('-') if len(fluxrange) > 1: fluxrange = map(float, fluxrange) elif len(fluxrange) == 1: fluxrange = [0, float(fluxrange[0])] select = '' fits = verify_sky(katalog) == 'FITS' if radius or fluxrange: if radius: select += '--select="r<%fdeg" ' % radius if fluxrange: select += '--select="I<%f" ' % fluxrange[1] select += '--select="I>%f" ' % fluxrange[0] if not fits: x.sh( 'tigger-convert $select --recenter=$direction $katalog $lsmname -f' ) else: from pyrap.measures import measures dm = measures() direction = dm.direction('J2000', ms_opts[ra], ms_opts[dec]) ra = np.rad2deg(direction['m0']['value']) dec = np.rad2deg(direction['m1']['value']) hdu = pyfits.open(temp_file) hdu[0].hdr['CRVAL1'] = ra hdu[0].hdr['CRVAL2'] = dec hdu.writeto(tmp_file, clobber=True) simsky(lsmname=lsmname, tdlsec='turbo-sim:custom', noise=noise, column='CORRECTED_DATA') skymodel = params['sky_model'] if skymodel: simsky(lsmname=skymodel, noise=0 if katalog else noise, addToCol='CORRECTED_DATA') ## Finally Lets image # make dirty map im.make_image(psf=True, **im_dict) # Deconvolve for deconv in _deconv: deconv = deconv.lower() if deconv in STAND_ALONE_DECONV: im.make_image(algorithm=deconv, restore=_deconv[deconv], restore_lsm=False, **im_dict) else: im.IMAGER = deconv im.make_image(dirty=False, restore=_deconv[deconv], restore_lsm=False, **im_dict) xo.sh('tar -czvf ${OUTDIR>/}${MS:BASE}.tar.gz $msname')
# uvw.py :: John Swinbank, November 2012 # # Calculates the uvw position corresponding to a particular baseline assuming # we are looking at the zenith. # This is a demo which takes no arguments -- change the positions of the # antennae by editing the script. Positions are ITRF2005. from pyrap.measures import measures #reference = [3826577.066110000, 461022.947639000, 5064892.786] # Reference position of CS002 from CS002-AntennaField.conf reference = [3826577.1095, 461022.900196, 5064892.758] # LOFAR_PHASE_REFERENCE from MS cs002d00 = [3826579.4925, 461005.105196, 5064892.578] # Read from MeasurementSet cs002d01 = [3826578.0655, 461002.706196, 5064893.866] # Read from MeasurementSet # The measures object is our interface to casacore dm = measures() # # Set up the reference frame used for the conversion. # # The date is required by casacore, but since we are looking at the zenith, # the value specified isn't important. dm.do_frame(dm.epoch("UTC", "today")) # Use the reference position given above. dm.do_frame(dm.position("ITRF", "%fm" % reference[0], "%fm" % reference[1], "%fm" % reference[2])) # Reference direction is the zenith. dm.do_frame(dm.direction("AZEL", "0deg", "90deg"))
{'name' : 'HerA', 'ra' : 4.4119087330382163, 'dec' : 0.087135562905816893}, {'name' : 'VirA', 'ra' : 3.276086511413598, 'dec' : 0.21626589533567378}, {'name' : 'HydraA', 'ra' : 2.4352, 'dec' : -0.21099}, #{'name' : 'JUPITER'}, {'name' : 'SUN'}] if len(sys.argv) == 2: msname = sys.argv[1] else: print "Usage" print " plot_Ateam_elevation.py <msname>" # Create a measures object me = pm.measures() # Open the measurement set and the antenna and pointing table ms = pt.table(msname, ack=False) # Get the position of the first antenna and set it as reference frame ant_table = pt.table(msname + '/ANTENNA', ack=False) ant_no = 0 pos = ant_table.getcol('POSITION') x = qa.quantity( pos[ant_no,0], 'm' ) y = qa.quantity( pos[ant_no,1], 'm' ) z = qa.quantity( pos[ant_no,2], 'm' ) position = me.position( 'wgs84', x, y, z ) me.doframe( position ) ant_table.close()
def test_invalid_ephemeris(self): dm = measures() # No(?) ephemeris we're likely to come across is valid at MJD 0. dm.do_frame(dm.epoch("UTC", "0.0d")) self.assertFalse( tkp.quality.brightsource.check_for_valid_ephemeris(dm))
#!/usr/bin/python from pylab import * import pyrap.tables as tab from pyrap.measures import measures import pyrap.quanta as qa me = measures() light_speed = 299792458. class stationBeam(): def __init__(self, ms, times=[], direction=[]): myt = tab.table(ms + '/LOFAR_ANTENNA_FIELD') self.flags = myt.getcol("ELEMENT_FLAG") self.offsets = myt.getcol("ELEMENT_OFFSET") myt = tab.table(ms + '/ANTENNA') self.fieldcenter = myt.getcol("LOFAR_PHASE_REFERENCE") self.stationcenter = myt.getcol("POSITION") self.offsetshift = self.fieldcenter - self.stationcenter self.ms = ms self.refdir = tab.table(ms + 'FIELD').getcol('PHASE_DIR')[0][0] itrfdir = [] self.times = times self.direction = direction self.initiated = False self.init_times() def init_times(self): if not list(self.direction): print "cannot init, set direction first" if not list(self.times):
# It has to be changed from "if stnLoc=='CS' or stnLoc=='RS':" to # "if stnLoc=='CS': # or stnLoc=='RS':", i.e. to comment out 'RS' part. # It makes sense as all RS stations have just only one ear of 48 tiles similar # to International stations with 96 tiles, thus antenna must be "HBA" instead of "HBA0" #stations=["CS001", "CS002", "CS003", "CS004", "CS005", "CS006", "CS007", # "CS011", "CS013", "CS017", "CS021", "CS024", "CS026", "CS028", # "CS030", "CS031", "CS032", "CS101", "CS103", "CS201", "CS301", # "CS302", "CS401", "CS501", "DE601", "DE602", "DE603", "DE604", # "DE605", "FR606", "SE607", "UK608", "DE609", "PL610", "PL611", "PL612"] # m a i n if __name__=="__main__": subwidth=100./512. obstimes=quantity([55159.77650462962963, 55159.77650462962963], 'd') direction=measures().direction('J2000', str(6.123487681)+'rad', str(1.0265154)+'rad') out = open("casa_beamcorr_pkg.py", "wt") out.write("casa_beamcorr={") for ss in xrange(len(stations)): print "%s, #%d of %d" % (stations[ss], ss+1, len(stations)) out.write("\"%s\" : [" % (stations[ss])) for ii in xrange(51, 1536, 6): # freq between 10 MHz and 300 MHz outline="" for jj in xrange(ii, ii+6 > 1536 and 1536 or ii+6): if jj != ii: outline += ", " freq=jj*subwidth+subwidth/2. # in MHz Jn=aj.getJonesByAntFld("Hamaker", obstimes, stations[ss], direction, freq*1.e6) bc_casa = 1./np.abs(0.5*(Jn[0,0,0]*np.conj(Jn[0,0,0]) + Jn[0,0,1]*np.conj(Jn[0,0,1]) + \ Jn[0,1,0]*np.conj(Jn[0,1,0]) + Jn[0,1,1]*np.conj(Jn[0,1,1]))) outline+="[%lf, %lf]" % (freq, bc_casa)
def __init__(self, parameters) : print "\033[94mPython MyATerm constructor\033[0m" print parameters.keys() #self.img = pyrap.images.image('beam.img').getdata()[0,0,:,:] # Read the necessary parameters from the parameterset. ms = str(parameters["ms"]) beamname = str(parameters['beamname']) support = int(str(parameters['SpheSupport'])) print "Reading beam patterns with prefix: ", beamname # 8 FITS files (4 pol each for real & imag parts) per station per frequency # HDUs are ordered as xx_re, xx_im, xy_re, xy_im, yx_re, yx_im, yy_re, yy_im hdulist = [] # Holds all the images for one station for pol in ['_xx','_xy','_yx','_yy']: for comp in ['_re','_im']: temphdu = pyfits.open(beamname+pol+comp+'.fits')[0]; # Extract the first HDU hdulist.append(temphdu) # Get the frequency range from the FITS header. hdu0 = hdulist[0] nfreq = hdu0.header['NAXIS3'] dim = float(hdu0.header['NAXIS1']) # Assume this is a square image. self.Im_per_station = 8 self.pa_step = 5 # Expose this as a parameter to the user. self.freqlist = [] # in Hertz if 'GFREQ1' in hdu0.header: for keyindex in range(1,nfreq+1): self.freqlist.append(hdu0.header['GFREQ%i'%keyindex]) else: #for keyindex in range(1,nfreq+1): #self.freqlist.append( pass # Compute a set of angles by which the beams need to be rotated every timestamp. dm = measures() field = 0 # this is used as the FIELD_ID - assume this to be zero for now; this is the field whose observed times we need from the MS. zenith = dm.direction('AZEL','0deg','90deg') tab = pt.table('TEST.MS') antpos = pt.table(tab.getkeyword("ANTENNA")).getcol("POSITION"); ra,dec = pt.table(tab.getkeyword("FIELD")).getcol("PHASE_DIR",field,1)[0][0] # make position measure from antenna 0 pos0 = dm.position('itrf',*[ dq.quantity(x,'m') for x in antpos[0]]) dm.do_frame(pos0); # make direction measure from field centre fld = dm.direction('J2000',dq.quantity(ra,"rad"),dq.quantity(dec,"rad")) tab = tab.query("FIELD_ID==%d"%field); # get unique times times = numpy.array(sorted(set(tab.getcol("TIME")[~tab.getcol("FLAG_ROW")]))); pa_times = [ (dm.do_frame(dm.epoch("UTC",dq.quantity(t,"s"))) and (dm.posangle(fld,zenith).get_value("deg"),t)) for t in times ] pa_times = sorted(pa_times) start_pa = pa_times[0][0]; pa_filtered = [pa_times[0]]; for tup in pa_times: if tup[0] - start_pa < self.pa_step: continue; pa_filtered.append(tup) start_pa = tup[0] print pa_filtered # Downsample / Interpolate (upsample?) the beam patterns to the support function size. import time t1 = time.time() print 'Start time: ', t1 self.beams = [] '''for polcomp in range(0,self.Im_per_station): for freq in range(0,nfreq): hdulist[polcomp].data[freq,:,:] = scipy.ndimage.zoom(hdulist[polcomp].data[freq,:,:] tempbeam = hdulist[polcomp].data[freq,:,:] #print tempbeam.shape temprotbeam = scipy.ndimage.rotate(tempbeam,p_angles[i]) #print "Temprotbeam dimensions: ", temprotbeam.shape aterms[polcomp,freq,:,:]= scipy.ndimage.zoom(temprotbeam,support/float(temprotbeam.shape[0]))''' self.times_filtered = [] for pa_t in pa_filtered: print "pa_t: ", pa_t print "printed" aterms = numpy.zeros((self.Im_per_station,nfreq,support,support)) for polcomp in range(0,self.Im_per_station): for freq in range(0,nfreq): tempbeam = hdulist[polcomp].data[freq,:,:] temprotbeam = tempbeam;#scipy.ndimage.rotate(tempbeam,pa_t[0]) aterms[polcomp,freq,:,:]= temprotbeam[0:support,0:support]#scipy.ndimage.zoom(temprotbeam,support/float(temprotbeam.shape[0])) self.beams.append((pa_t[1],aterms)) self.times_filtered.append(pa_t[1]) t2 = time.time() print 'Stop time: ', t2 print "Time to pre-compute a-terms: ", (t2-t1)/60.0, " minutes."
def main((frequency, msname, minst, maxst, refms)): # Arguments as a tuple to make threading easier assert maxst + 1 > minst # Set frequency of the MS to the specified frequency freqmhz = int(frequency / 1.e6) print frequency, freqmhz t = pt.table(msname + '/SPECTRAL_WINDOW', readonly=False, ack=False) t.putcol('REF_FREQUENCY', np.array([frequency])) t.putcol('CHAN_FREQ', np.array([[frequency]])) t.close() t = pt.table(msname, ack=False) timecol = t.getcol('TIME') msreftime = min(timecol) print 'time reference', msreftime JD = msreftime / 3600. / 24. + 2400000.5 print 'MS JD', JD # makeresponseimage abs=true frames=1 ms=freq00.MS size=350 cellsize=1200arcsec stations=0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59 hs = 100. ds = 1 # downsample, ** was 2 ** xvals = np.arange(-hs, hs + 1.) X, Y = np.meshgrid(xvals, xvals) R = np.hypot(X, Y) #w = wcs.WCS(naxes=2) #w.wcs.crpix=[151,151] #w.wcs.cdelt=array([165./301.,165./301.]) #w.wcs.crval=[0.,90.] #w.wcs.ctype=['RA---ZEA','DEC--ZEA'] h = pyfits.open('wcs_azimuth_201.fits') w = pywcs.WCS(h[0].header) els = np.zeros(R.shape) azs = np.zeros(R.shape) for i in range(R.shape[0]): for j in range(R.shape[1]): azel = w.wcs_pix2sky([[i, j]], 0) if azel[0][1] < 0.: els[i, j] = np.nan azs[i, j] = np.nan elif ((i - hs)**2 + (j - hs)**2)**0.5 > 1.2 * hs: els[i, j] = np.nan azs[i, j] = np.nan else: els[i, j] = azel[0][1] * np.pi / 180. azs[i, j] = azel[0][0] * np.pi / 180. ra = np.zeros(R.shape) dec = np.zeros(R.shape) t = pt.table(msname + '/ANTENNA', ack=False) stcol = t.getcol('NAME') poscol = t.getcol('POSITION') #stations = range(len(stcol)) stations = range(minst, maxst + 1) print len(stations), 'stations' beamintmap = np.zeros( (2, len(stations), len(range(0, len(xvals), ds)), len(range(0, len(xvals), ds)))) beamtmpmap = np.zeros((2, len(stations), len(xvals), len(xvals))) azmap = np.zeros((len(range(0, len(xvals), ds)), len(range(0, len(xvals), ds)))) elmap = np.zeros((len(range(0, len(xvals), ds)), len(range(0, len(xvals), ds)))) sr = lsr.stationresponse(msname, useElementResponse=True) stll = {} for line in open('stations_positions.txt'): sline = line.split() stll[sline[0]] = [ float(sline[2]) * np.pi / 180., float(sline[3]) * np.pi / 180. ] # lon, lat t = time.time() # directionmap contains itrf directions for every x,y point directionmap = np.zeros((len(xvals), len(xvals), 3)) evals = 0 for ss in range(len(stations)): # Convert azel to RA/DEC for this station meas = pm.measures() # Set station position meas.do_frame( meas.position('ITRF', *(str(coord) + "m" for coord in poscol[ss]))) # Set reference time meas.do_frame(meas.epoch('utc', pyrap.quanta.quantity(msreftime, 's'))) for i in range(0, len(xvals), ds): for j in range(0, len(xvals), ds): if not np.isnan(els[i, j]): radecmeas = meas.measure( meas.direction('AZEL', str(azs[i, j]) + ' rad', str(els[i, j]) + ' rad'), 'J2000') ra[i, j] = radecmeas['m0']['value'] dec[i, j] = radecmeas['m1']['value'] directionmap *= 0. for i in range(0, len(xvals), ds): for j in range(0, len(xvals), ds): if not np.isnan(dec[i, j]): sr.setDirection(ra[i, j], dec[i, j]) tmpdirection = sr.getDirection(msreftime) directionmap[i, j, :] = tmpdirection if refms is None: print "Using all points" pointsgenerator = allpointsgenerator() else: print "Using points in", refms pointsgenerator = mypointsgenerator(refms, stcol[ss]) for i, j in pointsgenerator: azmap[i / ds, j / ds] = azs[i, j] elmap[i / ds, j / ds] = els[i, j] #print dec[i,j] #exit() lenxvals = len(xvals) tmpra = 0. tmpdec = 0. if not np.isnan(dec[i, j]): thisra = ra[i, j] thisdec = dec[i, j] sr.setRefDelay(thisra, thisdec) sr.setRefTile(thisra, thisdec) refdelay = sr.getRefDelay(msreftime) reftile = sr.getRefTile(msreftime) beamtmpmap *= 0. for x in xrange(lenxvals): for y in xrange(lenxvals): if not np.isnan(dec[x, y]): #tmpra = ra[x,y] #tmpdec = dec[x,y] #sr.setDirection(tmpra,tmpdec) #mydirection=sr.getDirection(msreftime) mydirection = directionmap[x, y] bmj = sr.evaluateFreqITRF(msreftime, stations[ss], frequency, mydirection, refdelay, reftile) #bmj1=sr.evaluateFreq(msreftime,stations[ss],frequency) evals += 1 #beamtmpmap[ss,x,y]=np.sum(np.abs(bmj)) beamtmpmap[0, ss, x, y] = np.sqrt( np.abs(bmj[0, 0])**2 + np.abs(bmj[0, 1])**2) beamtmpmap[1, ss, x, y] = np.sqrt( np.abs(bmj[1, 1])**2 + np.abs(bmj[1, 0])**2) beamintmap[0, ss, j / ds, i / ds] = np.sum(beamtmpmap[0, ss, :, :]) #*cosel) beamintmap[1, ss, j / ds, i / ds] = np.sum(beamtmpmap[1, ss, :, :]) #*cosel) print ss, '/', len(stations), '-', i / ds, '/', len( range(0, len(xvals), ds)), ':', int( time.time() - t), 'sec elapsed so far,', evals, 'beam evaluations' header = h[0].header #header['CRPIX1']=51. #header['CRPIX2']=51. #pixsize = header['CDELT1'] #pixsize *= 2. #header['CDELT1']=pixsize #header['CDELT2']=pixsize for stationnr in range(minst, maxst + 1): stationname = stcol[stationnr] hdu = pyfits.PrimaryHDU(header=header, data=beamintmap[0, stationnr - minst, ]) hdu.writeto('beamcubexx-%s-%dMHz.fits' % (stationname, freqmhz), clobber=True) hdu = pyfits.PrimaryHDU(header=header, data=beamintmap[1, stationnr - minst, ]) hdu.writeto('beamcubeyy-%s-%dMHz.fits' % (stationname, freqmhz), clobber=True)
badtiles=float(opts.badtiles) # getting pulsar RA/DEC coord=raw.get_coordinates().getHMSDMS() raj=coord.split()[0] decj=coord.split()[-1] # getting Galactic coordinates (gl, gb) = eq2gal(raj, decj) # getting AZ/Elevation star = ephem.FixedBody() star._ra = raj star._dec = decj if opts.model == 'hamaker_carozzi': direction=measures().direction('J2000', str(float(repr(star._ra)))+'rad', str(float(repr(star._dec)))+'rad') # getting start date/time and mid-point of obs start_mjd = float(raw.start_time().strtempo()) midpoint_mjd = start_mjd + 0.5*(tobs/86400.) midpoint_dublin_day = midpoint_mjd - 15019.5 # in Dublin Days, Dublin day = JD - 2415020 midpoint = ephem.Date(midpoint_dublin_day) observer = ephem.Observer() observer.date = midpoint try: observer.long = opts.longitude except: # for now old versions of ephem has longitude attribute "long" # and newer version have both "long" and "lon". I am checking this here # in case, "long" will become deprecated
'ra': 3.276086511413598, 'dec': 0.21626589533567378 }, { 'name': 'SUN' }, { 'name': 'JUPITER' }] if len(sys.argv) == 2: msname = sys.argv[1] else: print "Usage" print " plot_Ateam_elevation.py <msname>" # Create a measures object me = pm.measures() # Open the measurement set and the antenna and pointing table ms = pt.table(msname) # Get the position of the first antenna and set it as reference frame ant_table = pt.table(msname + '/ANTENNA') ant_no = 0 pos = ant_table.getcol('POSITION') x = qa.quantity(pos[ant_no, 0], 'm') y = qa.quantity(pos[ant_no, 1], 'm') z = qa.quantity(pos[ant_no, 2], 'm') position = me.position('wgs84', x, y, z) me.doframe(position) #print position