コード例 #1
0
def setupObsInstance():
    # Observation interval (approx vernal equinox)
    beginTime = datetime(2011, 3, 20, 6, 0, 0)
    endTime = datetime(2011, 3, 21, 6, 0, 0)
    stepTime = timedelta(minutes=60)
    td = endTime-beginTime
    Times = []
    nrTimSamps = int(td.total_seconds()/stepTime.seconds)+1
    for ti in range(0, nrTimSamps):
        Times.append(beginTime+ti*stepTime)

    # Source direction
    #   CasA:
    # celSrcTheta_CasA = np.pi/2-1.026515
    # celSrcPhi_CasA = 6.123487
    #   Celestial origin:
    celSrcTheta = 0.4*math.pi/2
    celSrcEl = 0.6  # (math.pi/2-celSrcTheta)
    celSrcPhi = 0.0
    celSrcDir = celSrcPhi, celSrcEl, 'J2000'

    # Station position and rotation
    #   Alt1 arbitrarily:
    me = measures()
    stnPos_meWGS = measures().position('wgs84', '0deg', '0deg', '0m')
    stnPos_meITRF = me.measure(stnPos_meWGS, 'ITRF')
    stnPos = stnPos_meITRF['m2']['value']*sph2crt_me(stnPos_meITRF)[:,np.newaxis]
    stnRot = antpat.reps.sphgridfun.pntsonsphere.rot3Dmat(0., 0., 1*math.pi/2)
    #   Alt2 set via official LOFAR geodetic data:
    stnPos, stnRot = gi.getArrayBandParams('LOFAR', 'SE607', 'LBA')

    return Times, celSrcDir, stnPos, stnRot
コード例 #2
0
ファイル: tests.py プロジェクト: 2baOrNot2ba/dreamBeam
def setupObsInstance():
    # Observation interval (approx vernal equinox)
    beginTime = datetime(2011, 3, 20, 0, 0, 0)
    endTime = datetime(2011, 3, 21, 0, 0, 0)
    stepTime = timedelta(minutes=60)
    td = endTime-beginTime
    Times = []
    nrTimSamps = int(td.total_seconds()/stepTime.seconds)+1
    for ti in range(0, nrTimSamps):
        Times.append(beginTime+ti*stepTime)

    # Source direction
    #   CasA:
    # celSrcTheta_CasA = np.pi/2-1.026515
    # celSrcPhi_CasA = 6.123487
    #   Celestial origin:
    celSrcTheta = 0.0*math.pi/2
    celSrcPhi = 0.
    celSrcDir = celSrcPhi, (math.pi/2-celSrcTheta), 'J2000'

    # Station position and rotation
    #   Alt1 arbitrarily:
    me = measures()
    stnPos_meWGS = measures().position('wgs84','0deg','0deg','0m')
    stnPos_meITRF = me.measure(stnPos_meWGS,'ITRF')
    stnPos = stnPos_meITRF['m2']['value']*sph2crt_me(stnPos_meITRF)[:,np.newaxis]
    stnRot = antpat.reps.sphgridfun.pntsonsphere.rot3Dmat(0.,0.,1*math.pi/2)
    #   Alt2 set via official LOFAR geodetic data:
    # stnPos, stnRot, stnRelPos = getArrayBandParams('SE607', 'LBA')

    return Times, celSrcDir, stnPos, stnRot
コード例 #3
0
def setEpoch(obsTimesArr, obsTimeUnit):
    stnPos_me = measures().position('ITRF', '0m', '0m', '0m')
    me = measures()
    # Set position of reference frame w.r.t. ITRF
    me.doframe(stnPos_me)
    timEpoch = me.epoch('UTC', quantity(obsTimesArr, obsTimeUnit))
    me.doframe(timEpoch)
    return me
コード例 #4
0
def getParallacticRot(obsTimes, stnPos, srcDir, doPolPrec=True):
    #Convert python times to pyrap times
    obsTimes_lst = []
    for obsTime in obsTimes:
        obsTimes_lst.append(quantity(obsTime.isoformat()).get_value())
    obsTimes_me = quantity(obsTimes_lst, 'd')
    print obsTimes_me
    #Convert source direction to pyrap
    srcTheta, srcPhi, srcRefFrame = srcDir
    srcDir = srcRefFrame, str(srcPhi), str(math.pi / 2 - srcTheta)
    srcDir_me = measures().direction(srcDir[0], srcDir[1] + 'rad',
                                     srcDir[2] + 'rad')
    stnPos_me = measures().position('ITRF',
                                    str(stnPos[0, 0]) + 'm',
                                    str(stnPos[1, 0]) + 'm',
                                    str(stnPos[2, 0]) + 'm')

    obsTimesArr = obsTimes_me.get_value()
    obsTimeUnit = obsTimes_me.get_unit()
    paraMat = np.zeros((len(obsTimesArr), 2, 2))

    me = measures()
    #Set position of reference frame w.r.t. ITRF
    me.doframe(stnPos_me)

    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, srcDir_me)
    for ti in range(len(obsTimesArr)):
        #Set current time in reference frame
        timEpoch = me.epoch('UTC', quantity(obsTimesArr[ti], obsTimeUnit))
        me.doframe(timEpoch)

        #Compute polariz comps in spherical sys to cartesian Station coord sys
        #paraMtc=computeParaMat_tc('J2000', 'ITRF', srcDir_me, me)

        #Alternatively:
        paraMme = computeParaMat_me('J2000', 'AZEL', srcDir_me, me)

        paraM = paraMme

        if doPolPrec:
            #With precession:
            paraM = paraM * precMat
            #else:
            #Do not apply precession rotation of polarimetric frame.
            #This is then the apparent polarization frame.

        paraMat[ti, :, :] = paraM
    return paraMat
コード例 #5
0
def get_zenith_uvw(station, lwatime):
    """
    Given a Station instance and a LWATime instance, return the (u,v,w)
    coordinates of the baselines.
    """

    # This should be compatible with what data2ms does/did.
    dm = measures()
    zenith = dm.direction('AZEL', '0deg', '90deg')
    position = dm.position(*station.casa_position)
    epoch = dm.epoch(*lwatime.casa_epoch)
    dm.doframe(zenith)
    dm.doframe(position)
    dm.doframe(epoch)

    nant = len(station.antennas)
    nbl = nant * (nant + 1) // 2
    pos = numpy.zeros((nant, 3), dtype=numpy.float64)
    for i in range(nant):
        ant = station.antennas[i]
        position = dm.position('ITRF', *['%.6fm' % v for v in ant.ecef])
        baseline = dm.as_baseline(position)
        uvw = dm.to_uvw(baseline)
        pos[i, :] = uvw['xyz'].get_value()

    uvw = numpy.zeros((nbl, 3), dtype=numpy.float64)
    k = 0
    for i in range(nant):
        a1 = pos[i, :]
        for j in range(i, nant):
            a2 = pos[j, :]
            uvw[k, :] = a1 - a2
            k += 1

    return uvw
コード例 #6
0
ファイル: peeler.py プロジェクト: torrance/mwaprocessing
def phase_rotate(uvw, times, data, ant1, ant2, obspos, phasecentre, antennas, lambdas):
    data = data.copy()
    dm = measures()
    dm.do_frame(obspos)
    dm.do_frame(phasecentre)

    # Recalculate uvw for new phase position
    new_uvw = np.zeros_like(uvw)

    # Process visibilities by time so that we calculate antenna baselines
    # just once
    for time in set(times):
        epoch = dm.epoch('UTC', quantity(time, 's'))
        dm.do_frame(epoch)
        baselines = dm.as_baseline(antennas)
        antenna_uvw = np.reshape(dm.to_uvw(baselines)['xyz'].get_value(), (-1, 3))

        # Select only those rows for the current time
        # and update uvw values
        idx = times == time
        new_uvw[idx] = antenna_uvw[ant1[idx]] - antenna_uvw[ant2[idx]]

    # Calculate phase offset
    woffset = -2j * np.pi * (new_uvw.T[2] - uvw.T[2])
    data *= np.exp(woffset[:, np.newaxis] / lambdas)[:, :, np.newaxis]

    return new_uvw, data
コード例 #7
0
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
コード例 #8
0
def calculate_hourangles(location, utc_time, direction):
    """ Return hour angles for location, utc_time, and direction

    :param utc_time: Time(Iterable)
    :param location: EarthLocation
    :param direction: SkyCoord source
    :return: Angle
    """

    assert isinstance(location, EarthLocation)
    assert isinstance(utc_time, Time)
    assert isinstance(direction, SkyCoord)

    from casacore.measures import measures
    dm = measures()
    casa_location = dm.position('itrf', str(location.x), str(location.y),
                                str(location.z))
    dm.doframe(casa_location)
    casa_direction = dm.direction('j2000', angle_to_quanta(direction.ra),
                                  angle_to_quanta(direction.dec))
    has = list()
    unit = "rad"
    for utc in utc_time:
        casa_utc_time = dm.epoch('utc', str(utc.mjd) + 'd')
        dm.doframe(casa_utc_time)
        casa_hadec = dm.measure(casa_direction, 'hadec')
        has.append(casa_hadec['m0']['value'])
        assert unit == casa_hadec['m0']['unit']
    return Angle(has, unit=unit)
コード例 #9
0
    def ITRF_to_J2000(time, x, y, z):
        dm = cm.measures()
        dm.do_frame(dm.epoch('UTC', cq.quantity(time, 's')))

        ITRF_position = dm.position(rf='ITRF',
                                    v0=cq.quantity(x, 'm'),
                                    v1=cq.quantity(y, 'm'),
                                    v2=cq.quantity(z, 'm'))
        dm.do_frame(ITRF_position)

        ITRFLL_position = dm.measure(ITRF_position, 'ITRFLL')
        height = ITRFLL_position['m2']

        ITRFLL_direction = dm.direction('ITRFLL',
                                        v0=ITRFLL_position['m0'],
                                        v1=ITRFLL_position['m1'])

        J2000_direction = dm.measure(ITRFLL_direction, 'J2000')
        J2000_position = dm.position(rf='ITRF',
                                     v0=J2000_direction['m0'],
                                     v1=J2000_direction['m1'],
                                     v2=height)

        (az, el,
         r) = (J2000_position['m0']['value'], J2000_position['m1']['value'],
               J2000_position['m2']['value'])
        return (az, el, r)
コード例 #10
0
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
コード例 #11
0
def calculate_parallactic_angles(location, utc_time, direction):
    """ Return hour angles for location, utc_time, and direction

    :param utc_time: Time(Iterable)
    :param location: EarthLocation
    :param direction: SkyCoord source
    :return: Angle
    """

    assert isinstance(location, EarthLocation)
    assert isinstance(utc_time, Time)
    assert isinstance(direction, SkyCoord)

    from casacore.measures import measures
    dm = measures()
    from casacore import quanta

    casa_location = dm.position('itrf', str(location.x), str(location.y),
                                str(location.z))
    dm.doframe(casa_location)
    casa_direction = dm.direction('j2000', angle_to_quanta(direction.ra),
                                  angle_to_quanta(direction.dec))
    pas = list()
    unit = "rad"
    zenith = dm.direction('AZEL', '0deg', '90deg')

    for utc in utc_time:
        casa_utc_time = dm.epoch('utc', str(utc.mjd) + 'd')
        dm.doframe(casa_utc_time)
        casa_posangle = dm.posangle(casa_direction, zenith).canonical()
        pas.append(casa_posangle._get_value()[0])
        assert unit == casa_posangle.get_unit(), casa_posangle.get_unit()

    return Angle(pas, unit=unit)
コード例 #12
0
def calculate_azel(location, utc_time, direction):
    """ Return az el for a location, utc_time, and direction

    :param utc_time: Time(Iterable)
    :param location: EarthLocation
    :param direction: SkyCoord source
    :return: astropy Angle, Angle
    """
    assert isinstance(location, EarthLocation)
    assert isinstance(utc_time, Time)
    assert isinstance(direction, SkyCoord)

    # Use the casa measures
    from casacore.measures import measures
    dm = measures()
    casa_location = dm.position('itrf', str(location.x), str(location.y),
                                str(location.z))
    dm.doframe(casa_location)
    casa_direction = dm.direction('j2000', angle_to_quanta(direction.ra),
                                  angle_to_quanta(direction.dec))
    azs = list()
    els = list()
    unit0 = 'rad'
    unit1 = 'rad'
    for utc in utc_time:
        casa_utc_time = dm.epoch('utc', str(utc.mjd) + 'd')
        dm.doframe(casa_utc_time)
        casa_azel = dm.measure(casa_direction, 'azel')
        assert unit0 == casa_azel['m0']['unit']
        assert unit1 == casa_azel['m1']['unit']
        azs.append(casa_azel['m0']['value'])
        els.append(casa_azel['m1']['value'])
    return Angle(azs, unit=unit0), Angle(els, unit=unit1)
コード例 #13
0
 def testSpecificLocation(self):
     last = 73647.97547170892
     dm = measures()
     position = dm.position("itrf", "1m", "1m", "1m")
     self.assertTrue(
         abs(coordinates.mjd2lst(self.MJD, position) - last) < 1.0)
     self.assertTrue(
         abs(coordinates.mjds2lst(self.MJDs, position) - last) < 1.0)
コード例 #14
0
ファイル: jones.py プロジェクト: 2baOrNot2ba/dreamBeam
 def computeJonesRes_overfield(self):
     paraRot = np.zeros(self.jonesrbasis.shape[0:-2]+(2, 2))
     me = measures()
     me.doframe(measures().position('ITRF', '0m', '0m', '0m'))
     self.jonesbasis = np.zeros(self.jonesrbasis.shape)
     timEpoch = me.epoch('UTC', quantity(self.obsTimes, self.obsTimeUnit))
     me.doframe(timEpoch)
     for idxi in range(self.jonesrbasis.shape[0]):
         for idxj in range(self.jonesrbasis.shape[1]):
             jonesrbasis_to = np.asmatrix(convertBasis(me,
                                         self.jonesrbasis[idxi, idxj, :, :],
                                               self.jonesrmeta['refFrame'],
                                               self.jonesmeta['refFrame']))
             jonesbasisMat = getSph2CartTransf(jonesrbasis_to[..., 0])
             paraRot[idxi, idxj, :, :] = jonesbasisMat[:, 1:].H*jonesrbasis_to[:, 1:]
             self.jonesbasis[idxi, idxj, :, :] = jonesbasisMat
     self.jones = np.matmul(paraRot, self.jonesr)
     self.thisjones = paraRot
コード例 #15
0
def convertBasis(me, rbasis, from_refFrame, to_refFrame):
    basis = np.zeros((3, 3))
    for comp in range(3):
        vr = np.squeeze(rbasis[:, comp])
        (az, el) = crt2sph(vr)
        vr_sph_me = measures().direction(from_refFrame, quantity(az, 'rad'),
                                         quantity(el, 'rad'))
        v_sph_me = me.measure(vr_sph_me, to_refFrame)
        v_me = sph2crt_me(v_sph_me)
        basis[:, comp] = v_me
    return basis
コード例 #16
0
 def _calculate_uvw(self):
     uvw_machine = UVW(self.antenna_positions)
     uvw_machine.set_direction(self.direction)
     position = measures().position("ITRF", *[quantity(x, "m") for x in self.position])
     uvw_machine.set_position(position)
     self._uvw = np.empty(shape=(self.raw_data.shape[0],self.n_ant,self.n_ant,3), dtype=np.float64)
     for i,t in enumerate(self.time):
         uvw_machine.set_time(t.epoch())
         self._uvw[i] = uvw_machine()
     self._uvw_valid = True
     self._data_valid = False
コード例 #17
0
ファイル: jones.py プロジェクト: 2baOrNot2ba/dreamBeam
    def computeJonesRes_overtime(self):
        """Compute the resulting Jones matrix when the parallactic rotation
        matrix is applied to a source brightness. The structure is:

         jones[ti, sphcompIdx, skycompIdx] =
             paraRot[timeIdx,sphcompIdx,compIdx]*jonesr[compIdx,skycompIdx]

        """
        nrOfTimes = len(self.obsTimes)
        paraRot = np.zeros((nrOfTimes, 2, 2))
        me = measures()
        me.doframe(measures().position('ITRF', '0m', '0m', '0m'))
        self.jonesbasis = np.zeros((nrOfTimes, 3, 3))
        (az_from, el_from) = crt2sph(self.jonesrbasis[:, 0])
        r_sph_me = measures().direction(self.jonesrmeta['refFrame'],
                                        quantity(az_from, 'rad'),
                                        quantity(el_from, 'rad'))
        for ti in range(0, nrOfTimes):
            # Set current time in reference frame
            timEpoch = me.epoch('UTC', quantity(self.obsTimes[ti],
                                                self.obsTimeUnit))
            me.doframe(timEpoch)
            # paraRot[ti,:,:]=computeParaMat_me(self.jonesrmeta['refFrame'],
            #                         self.jonesmeta['refFrame'], r_sph_me, me)

            jonesrbasis_to = np.asmatrix(convertBasis(me, self.jonesrbasis,
                                                   self.jonesrmeta['refFrame'],
                                                   self.jonesmeta['refFrame']))
            jonesrbasis_to2 = computeSphBasis(self.jonesrmeta['refFrame'],
                                              self.jonesmeta['refFrame'],
                                              0, r_sph_me, me)
            jonesbasisMat = getSph2CartTransf(jonesrbasis_to[:, 0])
            #print("to", jonesrbasis_to)
            paraRot[ti, :, :] = jonesbasisMat[:, 1:].H*jonesrbasis_to[:, 1:]
            # paraRot[ti,:,:]=jonesrbasis_to2*jonesbasisMat[:,1:]
            self.jonesbasis[ti, :, :] = jonesbasisMat
        self.jones = np.matmul(paraRot, self.jonesr)
        self.thisjones = paraRot
コード例 #18
0
 def __init__(self, datafile, rcu_mode, subband, integration_time, antfile="", start_time=None, direction=None, station_name=""):
     self._datafile = datafile
     self.subband = subband
     self.integration_time = integration_time
     self.rcu_mode = RCUMode(rcu_mode)
     self._set_frequency(subband)
     self.station_name = station_name
     self._set_antenna_field(antfile)
     self._set_raw_data (datafile)
     self._set_time(start_time)
     if direction == None:
         self.direction = measures().direction("AZELGEO", "0deg", "90deg")
     else:
         self.direction = direction
コード例 #19
0
def CEL2TOPOpnts(obsTimes, stnPos, celPnt):
    #Convert python times to pyrap times
    obsTimes_lst = []
    for obsTime in obsTimes:
        obsTimes_lst.append(quantity(obsTime.isoformat()).get_value())
    obsTimes_me = quantity(obsTimes_lst, 'd')
    #Convert source direction to pyrap
    celPntTheta, celPntPhi, celPntRefFrame = celPnt
    celPnt = celPntRefFrame, str(celPntPhi), str(math.pi / 2 - celPntTheta)
    celPnt_me = measures().direction(celPnt[0], celPnt[1] + 'rad',
                                     celPnt[2] + 'rad')
    stnPos_me = measures().position('ITRF',
                                    str(stnPos[0, 0]) + 'm',
                                    str(stnPos[1, 0]) + 'm',
                                    str(stnPos[2, 0]) + 'm')
    celPntBasis = getSph2CartTransf(sph2crt_me(celPnt_me))
    obsTimesArr = obsTimes_me.get_value()
    obsTimeUnit = obsTimes_me.get_unit()
    #CelRot=zeros((len(obsTimesArr),2,2))
    rotang = np.zeros(len(obsTimesArr))
    me = measures()
    #Set position of reference frame w.r.t. ITRF
    me.doframe(stnPos_me)

    me.doframe(me.epoch('UTC', quantity(obsTimesArr[0], obsTimeUnit)))
    CelRot0 = getRotbetweenRefFrames(celPntRefFrame, 'ITRF', me)
    rotang[0] = 0.0

    for ti in range(1, len(obsTimesArr)):
        #Set current time in reference frame
        timEpoch = me.epoch('UTC', quantity(obsTimesArr[ti], obsTimeUnit))
        me.doframe(timEpoch)
        #Incomplete
        CelRot = getRotbetweenRefFrames(celPntRefFrame, 'ITRF', me)
        IncRot = CelRot * CelRot0.T
        rotang[ti] = rotzMat2ang(IncRot)
    return CelRot0, rotang
コード例 #20
0
 def computeJonesRes_overfield(self):
     """Compute the PJones over field of directions for one frequency.
     """
     pjones = np.zeros(self.jonesrbasis_from.shape[0:-2] + (2, 2))
     me = measures()
     me.doframe(measures().position(self._ecef_frame, '0m', '0m', '0m'))
     self.jonesbasis = np.zeros(self.jonesrbasis_from.shape)
     if self.refframe_r == self._eci_frame:
         convert2irf = self._ecef_frame
         jonesrbasis_from = self.jonesrbasis_from
         jr_refframe = self.refframe_r
     else:
         convert2irf = self._eci_frame
         jonesrbasis_from = np.matmul(self.ITRF2stnrot.T,
                                      self.jonesrbasis_from)
         jr_refframe = self._ecef_frame
     timEpoch = me.epoch('UTC', quantity(self.obsTimes, self.obsTimeUnit))
     me.doframe(timEpoch)
     for idxi in range(self.jonesrbasis_from.shape[0]):
         for idxj in range(self.jonesrbasis_from.shape[1]):
             jonesrbasis_to = np.asmatrix(
                 convertBasis(me, jonesrbasis_from[idxi, idxj, :, :],
                              jr_refframe, convert2irf))
             if convert2irf == self._ecef_frame:
                 jonesrbasis_to = np.matmul(self.ITRF2stnrot,
                                            jonesrbasis_to)
             jonesbasisMat = getSph2CartTransf(jonesrbasis_to[..., 0])
             pjones[idxi, idxj, :, :] = jonesbasisMat[:, 1:].H \
                 * jonesrbasis_to[:, 1:]
             self.jonesbasis[idxi, idxj, :, :] = jonesbasisMat
     if convert2irf == self._ecef_frame:
         self.refframe = 'STN'  # Final Ref frame is station
     else:
         self.refframe = self._eci_frame
     self.jones = np.matmul(pjones, self.jonesr)
     self.thisjones = pjones
コード例 #21
0
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 (casacore 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
コード例 #22
0
def get_zenith(station, lwatime):
    """
    Given a Station instance and a LWATime instance, return the RA and Dec
    coordiantes of the zenith in radians (J2000).
    """

    # This should be compatible with what data2ms does/did.
    dm = measures()
    zenith = dm.direction('AZEL', '0deg', '90deg')
    position = dm.position(*station.casa_position)
    epoch = dm.epoch(*lwatime.casa_epoch)
    dm.doframe(zenith)
    dm.doframe(position)
    dm.doframe(epoch)
    pointing = dm.measure(zenith, 'J2000')
    return pointing['m0']['value'], pointing['m1']['value']
コード例 #23
0
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 (casacore 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
コード例 #24
0
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
コード例 #25
0
def timestamp_to_ms_epoch(ts):
    ''' Convert an timestamp to seconds (epoch values)
        epoch suitable for using in a Measurement Set
        
    Parameters
    ----------
    ts : A timestamp object.

    Returns
    -------
    t : float
      The epoch time ``t`` in seconds suitable for fields in 
      measurement sets.
    '''
    dm = measures()
    epoch = dm.epoch(rf='utc', v0=ts.isoformat())
    epoch_d = epoch['m0']['value']
    epoch_s = epoch_d*24*60*60.0
    return epoch_s
コード例 #26
0
ファイル: brightsource.py プロジェクト: Error323/tkp
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 casacore
    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
コード例 #27
0
def sky_all(request, observatory, datetime, timezone, template_name):
	me = measures()
	if observatory == "ovro":
		obs_position = me.observatory("CARMA")
	elif observatory == "onsala":
		obs_position = me.observatory("OSO")
	elif observatory == "exloo":
		obs_position = me.observatory("LOFAR")
	elif observatory == "birr":
		obs_position = me.position("ITRF", "3801633.52806m", "-529021.899396m", "5076997.185m")
	elif observatory == "mwa":
		obs_position = me.observatory("MWA")
	elif observatory == "gmrt":
		obs_position = me.observatory("GMRT")
	else:
		obs_position = me.observatory("CARMA")

	dt_fmt = '%Y/%m/%d/%H:%M:%S'
	dt = py_datetime.datetime.strptime(datetime, dt_fmt)
	tz = py_datetime.timedelta(hours=int(timezone[:3]), minutes=int(timezone[3:]))
	time = me.epoch('UTC', (dt - tz).strftime(dt_fmt))

	# Set the position and epoch of the measures object
	me.do_frame(obs_position)
	me.do_frame(time)

	sources = []
	galaxy = []
	FLOAT_PRECISION = 5 # > arc-minute accuracy, limit to reduce JSON size.
	for src in models.SkyObject.objects.all():
		d_sky = skyObject2pyrap(me, src) # J2000 direction
		d_ground = me.measure(d_sky, 'AZEL') # Convert to AZ,EL
		az = d_ground['m0']['value']
		el = d_ground['m1']['value']
		info = {'Name':str(src.name), 'AZ': round(az, FLOAT_PRECISION), 'EL': round(el, FLOAT_PRECISION)}
		if src.name == 'gp':
			galaxy.append(info)
		else:
			sources.append(info)
	
	return HttpResponse(json.dumps({'sources': sources, 'galaxy': galaxy_up(galaxy)}))
コード例 #28
0
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 casacore
    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
コード例 #29
0
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
コード例 #30
0
ファイル: export.py プロジェクト: eoincarley/iLiSA
def ITRF2lonlat(x_itrf, y_itrf, z_itrf):
    """\
    Convert ITRF cartesian position coordinates to WGS84 latitude and longitude

    Parameters
    ----------
    x_itrf: float
        X coordinate of position in ITRF system in meters.
    y_itrf: float
        Y coordinate of position in ITRF system in meters.
    z_itrf: float
        Z coordinate of position in ITRF system in meters.

    Returns
    -------
    lon: float
        Longitude in degrees
    lat: float
        Latitude in degrees
    hgt: float
        Height above surface in meters.

    Examples
    --------
    >>> from ilisa.antennameta.export import ITRF2lonlat
    >>> ITRF2lonlat(3370286.88256, 712053.913283, 5349991.484)
    57.39876274671682, 11.929671631184405, 41.63424105290324
    """
    dm = measures()
    posstr = ["{}m".format(crd) for crd in (x_itrf, y_itrf, z_itrf)]
    p_itrf = dm.position('itrf', *posstr)
    p_wgs84 = dm.measure(p_itrf, 'wgs84')
    _ref = dm.get_ref(p_wgs84)
    lon = quantity(p_wgs84['m0']).get('deg').get_value()
    lat = quantity(p_wgs84['m1']).get('deg').get_value()
    hgt = quantity(p_wgs84['m2']).get('m').get_value()
    return lon, lat, hgt
コード例 #31
0
#!/usr/bin/env python

from __future__ import print_function
import sys
from lofarstation.stationdata import XSTData
from casacore.measures import measures
import ilisa.antennameta.calibrationtables as calibrationtables
import ilisa.observations.dataIO as dataIO

XSTfilepath = sys.argv[1]
stnid = "SE607"

obsinfo = dataIO.parse_xstextfilepath(XSTfilepath)
(RA,DEC,ref) = obsinfo['pointing']
pnting = measures().direction(ref, RA+'rad', DEC+'rad')

sd = XSTData(XSTfilepath, subband=int(obsinfo['subband']),
             rcu_mode=int(obsinfo['rcumode']), station_name=stnid,
             integration_time=float(obsinfo['integration']), direction=pnting)

print(sd.time[0])
print("{} MHz".format(sd.frequency / 1e6))

caltabpath = calibrationtables.findcaltabpath(obsinfo['rcumode'], stnid)
print(caltabpath)
sd.set_station_cal(caltabpath)
sd.write_ms(XSTfilepath[:-4]+".ms")
コード例 #32
0
ファイル: calibration.py プロジェクト: eoincarley/iLiSA
def gsmcal(dataff, filenr, sampnr, fluxpersterradian):
    ccm = measures()
    gs_model = 'LFSM'
    imsize = 200
    fluxperbeam = True
    l = numpy.linspace(-1, 1, imsize)
    m = numpy.linspace(-1, 1, imsize)
    ll, mm = numpy.meshgrid(l, m)
    #normcolor = colors.LogNorm()
    # The code below is almost cut-n-pasted from imaging.image()
    polrep = 'stokes'
    lofar_datatype = dataIO.datafolder_type(dataff)
    fluxperbeam = not fluxpersterradian
    if lofar_datatype != 'acc' and lofar_datatype != 'xst':
        raise RuntimeError(
            "Datafolder '{}'\n not ACC or XST type data.".format(dataff))
    cvcobj = dataIO.CVCfiles(dataff)
    calibrated = False
    if cvcobj.scanrecinfo.calibrationfile:
        calibrated = True
    stnid = cvcobj.scanrecinfo.get_stnid()
    lon, lat, h = imaging.ITRF2lonlat(cvcobj.stn_pos[0, 0],
                                      cvcobj.stn_pos[1, 0], cvcobj.stn_pos[2,
                                                                           0])
    stn_pos_x, stn_pos_y, stn_pos_z = cvcobj.stn_pos[0, 0], cvcobj.stn_pos[1, 0], \
                                      cvcobj.stn_pos[2, 0]
    ccm.doframe(
        ccm.position('ITRF',
                     str(stn_pos_x) + 'm',
                     str(stn_pos_y) + 'm',
                     str(stn_pos_z) + 'm'))
    for fileidx in range(filenr, cvcobj.getnrfiles()):
        integration = cvcobj.scanrecinfo.get_integration()
        intgs = len(cvcobj.samptimeset[fileidx])
        for tidx in range(sampnr, intgs):
            t = cvcobj.samptimeset[fileidx][tidx]
            freq = cvcobj.freqset[fileidx][tidx]
            img = skymodels.globaldiffuseskymodel(t, (lon, lat, h),
                                                  freq,
                                                  gs_model=gs_model,
                                                  imsize=imsize)
            ccm.doframe(ccm.epoch('UTC', t.isoformat('T')))
            phaseref_ccm = ccm.measure(
                ccm.direction('AZEL', '0.0rad',
                              str(numpy.deg2rad(90)) + 'rad'), 'J2000')
            phaseref = (phaseref_ccm['m0']['value'],
                        phaseref_ccm['m1']['value'], phaseref_ccm['refer'])
            uvw_sl = imaging.calc_uvw(t, phaseref, cvcobj.stn_pos,
                                      cvcobj.stn_antpos)
            vis_mod = vcz(ll,
                          mm,
                          img,
                          freq,
                          uvw_sl,
                          imag_is_fd=not (fluxperbeam))
            cvcpol_lin = dataIO.cvc2polrep(cvcobj[fileidx], crlpolrep='lin')
            cvpol_lin = cvcpol_lin[:, :, tidx, ...].squeeze()
            cvpol_x = cvpol_lin[0, 0, ...].squeeze()
            cvpol_y = cvpol_lin[1, 1, ...].squeeze()
            vis_meas_xx = cvpol_x
            vis_meas_yy = cvpol_y
            g_xx = stefcal(vis_meas_xx, vis_mod / 2.0)
            g_yy = stefcal(vis_meas_yy, vis_mod / 2.0)
            inv_g_xx = 1 / g_xx
            inv_g_yy = 1 / g_yy
            vis_cal_xx = inv_g_xx[:, numpy.newaxis] * vis_meas_xx * numpy.conj(
                inv_g_xx)
            vis_cal_yy = inv_g_yy[:, numpy.newaxis] * vis_meas_yy * numpy.conj(
                inv_g_yy)
            vis_cal_I = vis_cal_xx + vis_cal_yy
            vis_resid_I = vis_cal_I - vis_mod
            xstpol = numpy.array([[vis_cal_I,
                                   numpy.zeros_like(vis_mod)],
                                  [numpy.zeros_like(vis_mod), vis_resid_I]])
            skyimages, _l, _m = imaging.beamformed_image(
                xstpol,
                uvw_sl.T,
                freq,
                use_autocorr=True,
                lmsize=2.0,
                nrpix=imsize,
                polrep='linear',
                fluxperbeam=fluxperbeam)
            imaging.plotskyimage(_l,
                                 _m,
                                 skyimages,
                                 'linear',
                                 t,
                                 freq,
                                 stnid,
                                 integration,
                                 phaseref,
                                 calibrated,
                                 pbcor=False,
                                 maskhrz=False,
                                 fluxperbeam=fluxperbeam)
コード例 #33
0
ファイル: test_coordinates.py プロジェクト: Error323/tkp
 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)
コード例 #34
0
def main(msnames, sourcelist="CasA CygA VirA TauA", elevlimit='10deg'):
    def get_mean_time(ms):
        obstable = pt.table(ms+'::OBSERVATION', ack=False)
        meantime = np.mean(obstable.col('TIME_RANGE')[0])
        return meantime

    if msnames[0] == '[':
        mslist = msnames.lstrip('[').rstrip(']').split(',')
        for ms in mslist:
            ms = ms.strip("\'\"")
            if os.path.isdir(ms):
                msname = ms
                break
    else:
        msname = msnames
    print 'gen_demixing_sources.py Using MS:',msname
    reftime = int(round(get_mean_time(msname)))
    dm = measures()
    #position of CS004
    dm.doframe(dm.position('ITRF','3.82659e+06m', '460866.m', '5.0649e+06m'))
    dm.doframe(dm.epoch('utc', qa.quantity(reftime, 's') ))
    ele_rad = qa.quantity(elevlimit.strip(' []\'\"')).get_value('rad')
    outstring = "["
    sources = [source.strip(' []\'\"') for source in sourcelist.split()]
    for source in sources:
        if source == 'CasA':
            CasA_dir = dm.direction('J2000', '23h23m27.9s','+58d48m42s')
            azeldir =  dm.measure(CasA_dir,'AZEL')
            if qa.quantity(azeldir['m1']).get_value('rad') > ele_rad :
                if len(outstring) > 1:
                    outstring +=','
                outstring += 'CasA'
        elif source == 'CygA':
            CygA_dir = dm.direction('J2000', '19h59m28.3s','+40d44m02s')
            azeldir =  dm.measure(CygA_dir,'AZEL')
            if qa.quantity(azeldir['m1']).get_value('rad') > ele_rad :
                if len(outstring) > 1:
                    outstring +=','
                outstring += 'CygA'
        elif source == 'VirA':
            VirA_dir = dm.direction('J2000', '12h30m49.4233s', '+12d23m28.043s')
            azeldir =  dm.measure(VirA_dir,'AZEL')
            if qa.quantity(azeldir['m1']).get_value('rad') > ele_rad :
                if len(outstring) > 1:
                    outstring +=','
                outstring += 'VirA'
        elif source == 'TauA':
            TauA_dir = dm.direction('J2000', '05h34m32.0s','+22d00m52s')
            azeldir =  dm.measure(TauA_dir,'AZEL')
            if qa.quantity(azeldir['m1']).get_value('rad') > ele_rad :
                if len(outstring) > 1:
                    outstring +=','
                outstring += 'TauA'
        elif source == 'HerA':
            HerA_dir = dm.direction('J2000', '16h51m08.1s','+04d59m33s')
            azeldir =  dm.measure(HerA_dir,'AZEL')
            if qa.quantity(azeldir['m1']).get_value('rad') > ele_rad :
                if len(outstring) > 1:
                    outstring +=','
                outstring += 'HerA'
        else:
            raise ValueError('gen_demixing_sources.py: Unknown Source '+source+'\n'
                             'Supported sources: CasA, CygA, VirA, TauA, HerA')
    outstring += "]"
    outrec = { 'demixsources' : outstring }
    #outrec['debugout'] = outstring+' ; '+qa.quantity(reftime, 's').formatted('YMD')
    return outrec
コード例 #35
0
ファイル: azel.py プロジェクト: tammojan/measuresexample
# Get the azimuth and elevation of Jupiter from Dwingeloo at a given time.
# Usage: python azel.py

from casacore.measures import measures
from casacore.quanta import quantity

dm = measures()

dm.do_frame(dm.observatory("DWL"))
dm.do_frame(dm.epoch("UTC", "2016-06-28 19:54"))

# Do the actual conversion of the named direction
azeldir = dm.measure(dm.direction("Jupiter"), "AZEL")

# Use quanta to convert the returned radians into degrees
print quantity(dm.get_value(azeldir)[0]).get("deg")
print quantity(dm.get_value(azeldir)[1]).get("deg")
コード例 #36
0
ファイル: peeler.py プロジェクト: torrance/mwaprocessing
def peel(uvw, times, original, ant1, ant2, partition, antennas, freqs, obspos, ra0, dec0, args, passno):
    # TODO: thread xx and yy streams
    data = original.copy()

    # Add the current model of the source back into the data for subsequent passes
    if passno > 0:
        for source in partition:
            data += source.visibility(uvw, freqs, ra0, dec0)

    # Phase rotate visibilities onto source to peel
    # TODO: rotate onto centroid of partition
    _ra0, _dec0 = partition[0].ra, partition[0].dec
    dm = measures()
    phasecentre = dm.direction(
        'j2000',
        quantity(_ra0, 'rad'),
        quantity(_dec0, 'rad'),
    )

    uvw_rotated, rotated = phase_rotate(
        uvw,
        times,
        data,
        ant1,
        ant2,
        obspos,
        phasecentre,
        antennas,
        speed_of_light / freqs,
    )

    # Filter out baselines beneath minuv length
    uvw_length = (uvw_rotated.T[0]**2 + uvw_rotated.T[1]**2) > args.minuv
    uvw_rotated = uvw_rotated[uvw_length, :]
    rotated = rotated[uvw_length, :]

    # Average bands into chunks of width
    # On first pass, average entire band
    if passno == 0:
        width = len(freqs)
    else:
        width = args.width

    chans = len(freqs)
    averaged = np.zeros((rotated.shape[0], chans // width, 2), dtype=np.complex)
    for j in range(chans // width):
        with warnings.catch_warnings():
            # Ignore "Mean of empty slice" warnings (due to NaN values)
            warnings.simplefilter("ignore", category=RuntimeWarning)
            np.nanmean(rotated[:, j:j+width, :], 1, out=averaged[:, j, :])

    widefreqs = np.array([
        np.mean(freqs[i:i+width]) for i in range(0, chans, width)
    ])

    # Fit model to data
    # Fit to xx (0) and yy (3)
    if passno == 0:
        x0 = [p for source in partition for p in source.minimal_params]
        ret = least_squares(
            minimal_residual,
            x0,
            #method='lm', # Doesn't work in multithreaded environment
            args=(
                uvw_rotated,
                widefreqs,
                partition,
                averaged,
                _ra0,
                _dec0
            )
        )
        if not ret.success:
            print("Failed to converge")

        # Save final values, accounting for potentially
        # different number of params per source
        i = 0
        for source in partition:
            print(np.exp(source.XX2), np.exp(source.YY2))
            n = len(source.minimal_params)
            source.minimal_params = ret.x[i:i+n]
            i += n

        x0 = [p for source in partition for p in source.reduced_params]
        ret = least_squares(
            reduced_residual,
            x0,
            #method='lm', # Doesn't work in multithreaded environment
            args=(
                uvw_rotated,
                widefreqs,
                partition,
                averaged,
                _ra0,
                _dec0
            )
        )
        if not ret.success:
            print("Failed to converge")

        # Save final values, accounting for potentially
        # different number of params per source
        i = 0
        for source in partition:
            print(np.exp(source.XX2), np.exp(source.YY2), source.major, source.minor)
            n = len(source.reduced_params)
            source.reduced_params = ret.x[i:i+n]
            i += n
    else:
        x0 = [p for source in partition for p in source.params]

        ret = least_squares(
            residual,
            x0,
            #method='lm', # Doesn't work in multithreaded environment
            args=(
                uvw_rotated,
                widefreqs,
                partition,
                averaged,
                _ra0,
                _dec0
            ),
        )

        # Save final values, accounting for potentially
        # different number of params per source
        i = 0
        for source in partition:
            n = len(source.params)
            source.params = ret.x[i:i+n]
            i += n

    # Subtract updated source model from data
    for source in partition:
        data -= source.visibility(uvw, freqs, ra0, dec0)

    return np.subtract(data, original, out=data)
コード例 #37
0
ファイル: peeler.py プロジェクト: torrance/mwaprocessing
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--ms', help="The measurement set from which to peel")
    parser.add_argument('--meta', help="The observation metafits file")
    parser.add_argument('--model', help="The skymodel to peel, in aoskymodel 1.1 format")
    parser.add_argument('--aegean', help="Aegean CSV")
    parser.add_argument('--datacolumn', default='CORRECTED_DATA')
    parser.add_argument('--width', type=int, required=True)
    parser.add_argument('--passes', type=int, default=2)
    parser.add_argument('--minuv', type=float, default=0, help="Fit models only on baselines above this minimum uv distance (metres)")
    parser.add_argument('--workers', type=int, default=0)
    parser.add_argument('--threshold', type=float, default=0.8)
    args = parser.parse_args()

    metafits = getheader(args.meta)
    date = Time(metafits['DATE-OBS'], location=mwa_pb.config.MWAPOS)
    delays = [int(d) for d in metafits['DELAYS'].split(',')]
    delays = [delays, delays] # Shuts up mwa_pb
    location = mwa_pb.config.MWAPOS

    print("Observation date: ", date)
    print("Delays: ", delays)

    # Retrieve telescope position
    dm = measures()
    obs = table(args.ms + '/OBSERVATION', ack=False)
    if 'TELESCOPE_NAME' in obs.colnames():
        names = obs.getcol('TELESCOPE_NAME')
        if len(names) == 1:
            obspos = dm.observatory(names[0])
            dm.do_frame(obspos)

        else:
            print("Failed to work out the telescope name of this observation")
            exit(1)
    else:
        print("Measurement set did not provide the telescope name")
        exit(1)
    print("Observation taken using telescope %s" % names[0])

    ms = table(args.ms, readonly=False, ack=False)
    freqs = table(args.ms + '/SPECTRAL_WINDOW', ack=False).getcell('CHAN_FREQ', 0)
    midfreq = (max(freqs) + min(freqs)) / 2
    lambdas = speed_of_light / freqs
    ra0, dec0 = table(args.ms + '/FIELD', ack=False).getcell('PHASE_DIR', 0)[0]  # Phase centre in radians
    chans, pols = ms.getcell(args.datacolumn, 0).shape
    print("There are %d channels" % chans)

    # Check whether width evenly divides the total channels
    if chans % args.width != 0:
        print("Width (%d) does not evenly divide channel number (%d)" % (args.width, chans))
        exit(1)

    widefreqs = np.array([
        np.mean(freqs[i:i+args.width]) for i in range(0, len(freqs), args.width)
    ])
    widelambdas = speed_of_light / widefreqs

    antennas = table(args.ms + '/ANTENNA', ack=False).getcol('POSITION')
    antennas = dm.position(
        'itrf',
        quantity(antennas.T[0], 'm'),
        quantity(antennas.T[1], 'm'),
        quantity(antennas.T[2], 'm'),
    )

    # Create PEELED_DATA column if it does not exist
    if ('CORRECTED_DATA' not in ms.colnames()):
        print("Creating CORRECTED_DATA column...", end=" ")
        sys.stdout.flush()
        coldesc = ms.getcoldesc('DATA')
        coldesc['name'] = 'CORRECTED_DATA'
        ms.addcols(coldesc)

        peeled = ms.getcol(args.datacolumn)
        ms.putcol('CORRECTED_DATA', peeled)
        print("Done")

    # Load models
    if args.model:
        with open(args.model) as f:
            models = model_parser(f)
            comps = [c for m in models for c in m.components]
    elif args.aegean:
        with open(args.aegean) as f:
            comps = aegean_parser(f, midfreq)
    else:
        print("No model (--aegean or --model) specified", file=sys.stderr)
        exit(1)
    print("Initialised %d model sources" % len(comps))


    # Calculate Jones matrices for each model component
    # JBeams[widefreq, component, row, col]
    JBeams = []
    for widefreq in widefreqs:
        altaz = [radec_to_altaz(comp.ra, comp.dec, date, location) for comp in comps]
        altaz = np.array(zip(*altaz))
        JBeam = pb.MWA_Tile_full_EE(np.pi/2 - altaz[0], altaz[1], widefreq, delays=delays, jones=True)
        JBeams.append(JBeam)

    sources = []
    for i, comp in enumerate(comps):
        xx_fluxes = []
        yy_fluxes = []
        for j, widefreq in enumerate(widefreqs):
            stokes = comp.flux(widefreq) # Model flux per Stokes paramter

            # XX = I + V; XY = U + iV; YY = Q - iU; YY = I -Q
            linear = np.array([
                [stokes[0] + stokes[3], stokes[2] + 1j * stokes[3]],
                [stokes[1] - 1j * stokes[2], stokes[0] - stokes[1]],
            ])

            # apparent = JBeam x linear x (JBeam)^H ..... where H is the Hermitian transpose
            JBeam = JBeams[j][i]
            apparent = np.matmul(np.matmul(JBeam, linear), np.conj(JBeam.T))

            # For now (FIX?) we just take the real part
            xx_fluxes.append(np.real(apparent[0, 0]))
            yy_fluxes.append(np.real(apparent[1, 1]))

        xx_fluxes, yy_fluxes = np.array(xx_fluxes), np.array(yy_fluxes)

        # Estimate initial parameters
        log_widefreqs = np.log(widefreqs)
        log_xx_fluxes = np.log(xx_fluxes)
        log_yy_fluxes = np.log(yy_fluxes)
        xx3 = np.polyfit(log_widefreqs, log_xx_fluxes, 0)
        yy3 = np.polyfit(log_widefreqs, log_yy_fluxes, 0)

        sources.append(
            #Point(0, 0, xx3, 0, 0, yy3, comp.ra, comp.dec),
            Gaussian(0, 0, xx3, 0, 0, yy3, 0.1, 0.1, 0, comp.ra, comp.dec),
        )

    # Group sources by a spatial threshold
    threshold = (4 / 60) * np.pi / 180
    partitions = spatial_partition(sources, threshold)

    # Read data minus flagged rows
    tbl = taql("select * from $ms where not FLAG_ROW")
    uvw = tbl.getcol('UVW')
    uvw.flags.writeable = False
    times = tbl.getcol('TIME_CENTROID')
    times.flags.writeable = False
    ant1 = tbl.getcol('ANTENNA1')
    ant1.flags.writeable = False
    ant2 = tbl.getcol('ANTENNA2')
    ant2.flags.writeable = False
    data = tbl.getcol(args.datacolumn)[:, :, [True, False, False, True]].copy() # Just XX, YY

    # Handle flags by setting entries that are flagged as NaN
    flags = tbl.getcol('FLAG')[:, :, [True, False, False, True]]
    data[flags] = np.nan

    # For some reason, it is necessary to rotate onto
    # the current phase direction. Somehow this makes future offsets
    # internally self-consistent.
    dm = measures()
    phasecentre = dm.direction(
        'j2000',
        quantity(ra0, 'rad'),
        quantity(dec0, 'rad'),
    )

    uvw, data = phase_rotate(
        uvw,
        times,
        data,
        ant1,
        ant2,
        obspos,
        phasecentre,
        antennas,
        speed_of_light / freqs,
    )
    # tbl.putcol('UVW', uvw)
    # d = tbl.getcol('DATA')
    # d[:, :, [True, False, False, True]] = data
    # tbl.putcol('DATA', d)

    if args.workers > 0:
        pool = Pool(args.workers)

    for passno in range(args.passes):
        i = 0

        # Order sources by apparent flux at midfreq
        partitions = sorted(
            partitions, reverse=True, key=lambda xs: sum([x.flux(midfreq) for x in xs])
        )

        print("Beginning pass %d...   0%%" % (passno + 1), end="")
        sys.stdout.flush()
        while i < len(partitions):
            # TODO: use prior partitions to estimate updated values for l,m of this partition

            # Find the next batch of partitions that are within some threshold
            # of the currently brightest partition due to process.
            fluxlimit = args.threshold * sum([
                x.flux(midfreq) for x in partitions[i]
            ])
            for n, partition in enumerate(partitions[i:] + [None]):
                if partition is None:
                    break
                elif sum([x.flux(midfreq) for x in partition]) < fluxlimit:
                    break

            batch = partitions[i:i+n]

            data.flags.writeable = False
            if args.workers:
                diffs = pool.imap_unordered(
                    peel_star,
                    zip(
                        [uvw] * len(batch),
                        [times] * len(batch),
                        [data] * len(batch),
                        [ant1] * len(batch),
                        [ant2] * len(batch),
                        batch,
                        [antennas] * len(batch),
                        [freqs] * len(batch),
                        [obspos] * len(batch),
                        [ra0] * len(batch),
                        [dec0] * len(batch),
                        [args] * len(batch),
                        [passno] * len(batch),
                    )
                )
            else:
                diffs = imap(
                    peel,
                    [uvw] * len(batch),
                    [times] * len(batch),
                    [data] * len(batch),
                    [ant1] * len(batch),
                    [ant2] * len(batch),
                    batch,
                    [antennas] * len(batch),
                    [freqs] * len(batch),
                    [obspos] * len(batch),
                    [ra0] * len(batch),
                    [dec0] * len(batch),
                    [args] * len(batch),
                    [passno] * len(batch),
                )
            data.flags.writeable = True

            data = data.copy()  # Avoid changing data for running threads
            for j, diff in enumerate(diffs):
                data += diff
                print("\b\b\b\b% 3d%%" % ((i + j + 1) / len(partitions) * 100), end="")
                sys.stdout.flush()

            i += n

        print("")

    print("\n Finishing peeling. Writing data back to disk...", end=" ")
    sys.stdout.flush()
    peeled = tbl.getcol(args.datacolumn)
    peeled[:, :, 0] = data[:, :, 0]
    peeled[:, :, 3] = data[:, :, 1]
    tbl.putcol('CORRECTED_DATA', peeled)
    ms.close()
    with open('peeled.reg', 'w') as f:
        to_ds9_regions(f, partitions)
    print("Done")
コード例 #38
0
#!/usr/bin/env python

from __future__ import print_function
from lofarstation.stationdata import ACCData
from casacore.measures import measures
import glob
import sys
import numpy as np

CasA = measures().direction("J2000", "23h23m26s", "+58d48m00s")
#CygA = measures().direction("J2000", "19h59m28.3566s", "+40d44m02.096s")


def beam_from_acc_dir(accdir, rcu, outnpy):
    dyn_spec = []
    for accfile in sorted(glob.glob("{}/*_acc_*.dat".format(accdir))):
        acc = ACCData(accfile,
                      station_name="SE607",
                      rcu_mode=rcu,
                      direction=CasA)
        dyn_spec.append(
            acc.data.reshape((acc.data.shape[0], -1)).mean(axis=1).real)
        print(accfile)
    dyn_spec = np.array(dyn_spec)
    np.save(outnpy, dyn_spec)


def main():
    if len(sys.argv) != 4:
        print("Usage: {} <acc_directory> <rcu> <outnpy>".format(sys.argv[0]),
              file=sys.stderr)
コード例 #39
0
ファイル: elevation.py プロジェクト: tikk3r/lofar-weights
from pylab import *
#import pyrap.quanta as qa
#import pyrap.tables as pt
#import pyrap.measures as pm
import casacore.quanta as qa
import casacore.tables as pt
import casacore.measures as pm
import sys, glob
import numpy as np

#for obs in sorted(glob.glob('c09-o0*/3c380/')):
msname = sys.argv[1]
print 'MS file: ', 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' )
x = qa.quantity(pos[ant_no, 0], 'm')
#y = qa.quantity( pos[ant_no,1], 'm' )
y = qa.quantity(pos[ant_no, 1], 'm')
#z = qa.quantity( pos[ant_no,2], 'm' )
z = qa.quantity(pos[ant_no, 2], 'm')
position = me.position('WGS84', x, y, z)
コード例 #40
0
ファイル: test_brightsource.py プロジェクト: jdswinbank/tkp
 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))