Ejemplo n.º 1
0
 def antenna_positions(self, antenna_positions):
     self._antenna_positions = antenna_positions
     x = quantity(self.antenna_positions[:, 0], "m")
     y = quantity(self.antenna_positions[:, 1], "m")
     z = quantity(self.antenna_positions[:, 2], "m")
     self._itrf_baselines = self._measures.baseline('itrf', x, y, z)
     self._up_to_date = False
Ejemplo n.º 2
0
    def earthmagnetic(self, rf='', v0='0G', v1='0..', v2='90..', off=None):
        """Defines an earthmagnetic measure. It needs a reference code,
        earthmagnetic quantity values (see introduction for the action on a
        scalar quantity with either a vector or scalar value, and when a
        vector of quantities is given) if the reference code is not for a
        model, and optionally it can specify an offset, which in itself has
        to be a earthmagnetic. In general you specify a model (*IGRF* is the
        default and the only one known) and convert it to an explicit field.
        (See http://fdd.gsfc.nasa.gov/IGRF.html for information on the
        International Geomagnetic Reference Field). The earthmagnetic quantity
        values should be either longitude (angle), latitude(angle) and
        length(field strength); or x,y,z (field).
        See :func:`~casacore.quanta.quantity` for possible angle formats.

        :param rf: reference code string; Allowable reference
                   codes are: *IGRF*
        :param v0: longitude or x as quantity or string
        :param v1: latitude or y as quantity or string
        :param v2: height or z as quantity or string
        :param off: an optional offset measure of same type

        """
        loc = {'type': "earthmagnetic", 'refer': rf}
        loc['m0'] = dq.quantity(v0)
        loc['m1'] = dq.quantity(v1)
        loc['m2'] = dq.quantity(v2)
        if is_measure(off):
            if not off['type'] == "earthmagnetic":
                raise TypeError('Illegal offset type specified.')
            loc["offset"] = off
        return self.measure(loc, rf)
Ejemplo n.º 3
0
    def todoppler(self, rf, v0, rfq):
        """Convert a radialvelocity measure or a frequency measure to a
        doppler measure. In the case of a frequency, a rest frequency has
        to be specified. The type of doppler wanted (e.g. *RADIO*) has to be
        specified.

        :param rf: doppler reference code (see :meth:`doppler`)
        :param v0: a radialvelocity or frequency measure
        :param rfq: frequency measure or quantity

        Example::

            f = dm.frequency('lsrk','1410MHz')     # specify a frequency
            dm.todoppler('radio', f, dm.constants('HI')) # give doppler, using HI rest

        """
        if is_measure(rfq) and rfq['type'] == 'frequency':
            rfq = dq.quantity(rfq['m0'])
        elif isinstance(rfq, str):
            rfq = dq.quantity(rfq)
        if is_measure(v0):
            if v0['type'] == 'radialvelocity':
                return self.todop(v0, dq.quantity(1., 'Hz'))
            elif v0['type'] == 'frequency' and dq.is_quantity(rfq) \
                    and rfq.conforms(dq.quantity('Hz')):
                return self.todop(v0, rfq)
            else:
                raise TypeError('Illegal Doppler or rest frequency specified')
        else:
            raise TypeError('Illegal Frequency specified')
Ejemplo n.º 4
0
    def uvw(self, rf='', v0='0..', v1='', v2='', off=None):
        """Defines a uvw measure. It has to specify a reference code, uvw
        quantity values (see introduction for the action on a scalar quantity
        with either a vector or scalar value, and when a vector of quantities
        is given), and optionally it can specify an offset, which in itself
        has to be a uvw.

        :param rf: reference code string; Allowable reference
                   codes are: *ITRF* and :meth:`direction` codes
                   Note that additional ones may become available. Check with::

                       dm.list_codes(dm.uvw())


        :param v0: longitude or x as quantity or string
        :param v1: latitude or y as quantity or string
        :param v2: height or z as quantity or string
        :param off: an optional offset measure of same type

        """
        loc = {'type': "uvw", 'refer': rf}
        loc['m0'] = dq.quantity(v0)
        loc['m1'] = dq.quantity(v1)
        loc['m2'] = dq.quantity(v2)
        if is_measure(off):
            if not off['type'] == "uvw":
                raise TypeError('Illegal offset type specified.')
            loc["offset"] = off
        return self.measure(loc, rf)
Ejemplo n.º 5
0
    def earthmagnetic(self, rf='', v0='0G', v1='0..', v2='90..', off=None):
        """Defines an earthmagnetic measure. It needs a reference code,
        earthmagnetic quantity values (see introduction for the action on a
        scalar quantity with either a vector or scalar value, and when a
        vector of quantities is given) if the reference code is not for a
        model, and optionally it can specify an offset, which in itself has
        to be a earthmagnetic. In general you specify a model (*IGRF* is the
        default and the only one known) and convert it to an explicit field.
        (See http://fdd.gsfc.nasa.gov/IGRF.html for information on the
        International Geomagnetic Reference Field). The earthmagnetic quantity
        values should be either longitude (angle), latitude(angle) and
        length(field strength); or x,y,z (field).
        See :func:`~casacore.quanta.quantity` for possible angle formats.

        :param rf: reference code string; Allowable reference
                   codes are: *IGRF*
        :param v0: longitude or x as quantity or string
        :param v1: latitude or y as quantity or string
        :param v2: height or z as quantity or string
        :param off: an optional offset measure of same type

        """
        loc = {'type': "earthmagnetic", 'refer': rf}
        loc['m0'] = dq.quantity(v0)
        loc['m1'] = dq.quantity(v1)
        loc['m2'] = dq.quantity(v2)
        if is_measure(off):
            if not off['type'] == "earthmagnetic":
                raise TypeError('Illegal offset type specified.')
            loc["offset"] = off
        return self.measure(loc, rf)
Ejemplo n.º 6
0
    def tofrequency(self, rf, v0, rfq):
        """Convert a Doppler type value (e.g. in radio mode) to a
        frequency. The type of frequency (e.g. LSRK) and a rest frequency
        (either as a frequency quantity (e.g. ``dm.constants('HI'))`` or
        a frequency measure (e.g. ``dm.frequency('rest','5100MHz'))`` should
        be specified.

        :param rf: frequency reference code (see :meth:`frequency`)
        :param v0: a doppler measure
        :param rfq: frequency measure or quantity

        Example::

            dop = dm.doppler('radio',0.4)
            freq = dm.tofrequency('lsrk', dop, dm.constants('HI'))

        """
        if is_measure(rfq) and rfq['type'] == 'frequency':
            rfq = dq.quantity(rfq['m0'])
        elif isinstance(rfq, string_types):
            rfq = dq.quantity(rfq)
        if is_measure(v0) and v0['type'] == 'doppler' \
                and dq.is_quantity(rfq) \
                and rfq.conforms(dq.quantity('Hz')):
            return self.doptofreq(v0, rf, rfq)
        else:
            raise TypeError('Illegal Doppler or rest frequency specified')
Ejemplo n.º 7
0
    def todoppler(self, rf, v0, rfq):
        """Convert a radialvelocity measure or a frequency measure to a
        doppler measure. In the case of a frequency, a rest frequency has
        to be specified. The type of doppler wanted (e.g. *RADIO*) has to be
        specified.

        :param rf: doppler reference code (see :meth:`doppler`)
        :param v0: a radialvelocity or frequency measure
        :param rfq: frequency measure or quantity

        Example::

            f = dm.frequency('lsrk','1410MHz')     # specify a frequency
            dm.todoppler('radio', f, dm.constants('HI')) # give doppler, using HI rest

        """
        if is_measure(rfq) and rfq['type'] == 'frequency':
            rfq = dq.quantity(rfq['m0'])
        elif isinstance(rfq, string_types):
            rfq = dq.quantity(rfq)
        if is_measure(v0):
            if v0['type'] == 'radialvelocity':
                return self.todop(v0, dq.quantity(1., 'Hz'))
            elif v0['type'] == 'frequency' and dq.is_quantity(rfq) \
                    and rfq.conforms(dq.quantity('Hz')):
                return self.todop(v0, rfq)
            else:
                raise TypeError('Illegal Doppler or rest frequency specified')
        else:
            raise TypeError('Illegal Frequency specified')
Ejemplo n.º 8
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)
Ejemplo n.º 9
0
    def tofrequency(self, rf, v0, rfq):
        """Convert a Doppler type value (e.g. in radio mode) to a
        frequency. The type of frequency (e.g. LSRK) and a rest frequency
        (either as a frequency quantity (e.g. ``dm.constants('HI'))`` or
        a frequency measure (e.g. ``dm.frequency('rest','5100MHz'))`` should
        be specified.

        :param rf: frequency reference code (see :meth:`frequency`)
        :param v0: a doppler measure
        :param rfq: frequency measure or quantity

        Example::

            dop = dm.doppler('radio',0.4)
            freq = dm.tofrequency('lsrk', dop, dm.constants('HI'))

        """
        if is_measure(rfq) and rfq['type'] == 'frequency':
            rfq = dq.quantity(rfq['m0'])
        elif isinstance(rfq, str):
            rfq = dq.quantity(rfq)
        if is_measure(v0) and v0['type'] == 'doppler' \
                and dq.is_quantity(rfq) \
                and rfq.conforms(dq.quantity('Hz')):
            return self.doptofreq(v0, rf, rfq)
        else:
            raise TypeError('Illegal Doppler or rest frequency specified')
Ejemplo n.º 10
0
    def uvw(self, rf='', v0='0..', v1='', v2='', off=None):
        """Defines a uvw measure. It has to specify a reference code, uvw
        quantity values (see introduction for the action on a scalar quantity
        with either a vector or scalar value, and when a vector of quantities
        is given), and optionally it can specify an offset, which in itself
        has to be a uvw.

        :param rf: reference code string; Allowable reference
                   codes are: *ITRF* and :meth:`direction` codes
                   Note that additional ones may become available. Check with::

                       dm.list_codes(dm.uvw())


        :param v0: longitude or x as quantity or string
        :param v1: latitude or y as quantity or string
        :param v2: height or z as quantity or string
        :param off: an optional offset measure of same type

        """
        loc = {'type': "uvw", 'refer': rf}
        loc['m0'] = dq.quantity(v0)
        loc['m1'] = dq.quantity(v1)
        loc['m2'] = dq.quantity(v2)
        if is_measure(off):
            if not off['type'] == "uvw":
                raise TypeError('Illegal offset type specified.')
            loc["offset"] = off
        return self.measure(loc, rf)
Ejemplo n.º 11
0
def pyTimes2meTimes(pyTimes):
    obsTimes_lst = []
    for obsTime in pyTimes:
        obsTimes_lst.append(quantity(obsTime.isoformat()).get_value())
    obsTimes_me = quantity(obsTimes_lst, 'd')
    obsTimesArr = obsTimes_me.get_value()
    obsTimeUnit = obsTimes_me.get_unit()
    return obsTimesArr, obsTimeUnit
def calculate_separation(skymodel, clusterfile, ra0, dec0, measure):
    fh = open(skymodel, 'r')
    fullset = fh.readlines()
    fh.close()
    S = {}
    for cl in fullset:
        if (not cl.startswith('#')) and len(cl) > 1:
            cl1 = cl.split()
            S[cl1[0]] = cl1[1:]

    fh = open(clusterfile, 'r')
    fullset = fh.readlines()
    fh.close()

    # determine number of clusters
    ci = 0
    for cl in fullset:
        if (not cl.startswith('#')) and len(cl) > 1:
            ci += 1
    K = ci

    ra0_q = quantity(ra0, 'rad')
    dec0_q = quantity(dec0, 'rad')
    target = measure.direction('j2000', ra0_q, dec0_q)

    separations = np.zeros(K, dtype=np.float32)
    azimuths = np.zeros(K, dtype=np.float32)
    elevations = np.zeros(K, dtype=np.float32)
    ck = 0
    for cl in fullset:
        if (not cl.startswith('#')) and len(cl) > 1:
            cl1 = cl.split()
            for sname in cl1[
                    2:3]:  # only consider the first source of each cluster
                # 3:ra 3:dec sI 0 0 0 sP 0 0 0 0 0 0 freq0
                sinfo = S[sname]
                mra = (float(sinfo[0]) + float(sinfo[1]) / 60. +
                       float(sinfo[2]) / 3600.) * 360. / 24. * math.pi / 180.0
                mdec = (float(sinfo[3]) + float(sinfo[4]) / 60. +
                        float(sinfo[5]) / 3600.) * math.pi / 180.0
                mra_q = quantity(mra, 'rad')
                mdec_q = quantity(mdec, 'rad')
                cluster_dir = measure.direction('j2000', mra_q, mdec_q)
                if ck < K - 1:
                    cluster_dir = measure.direction('j2000', mra_q, mdec_q)
                else:  # last cluster is target
                    cluster_dir = measure.direction('j2000', ra0_q, dec0_q)
                separation = measure.separation(target, cluster_dir)
                separations[ck] = separation.get_value()
                # get elevation of this dir
                azel = measure.measure(cluster_dir, 'AZEL')
                azimuths[ck] = azel['m0']['value'] / math.pi * 180
                elevations[ck] = azel['m1']['value'] / math.pi * 180
            ck += 1

    return separations, azimuths, elevations
Ejemplo n.º 13
0
 def __init__(self, obsTimespy):
     super(PJones, self).__init__()
     obsTimes_lst = []
     for obsTimepy in obsTimespy:
         obsTimes_lst.append(quantity(obsTimepy.isoformat()).get_value())
     obsTimes_me = quantity(obsTimes_lst, 'd')
     self.obsTimes = obsTimes_me.get_value()
     self.obsTimeUnit = obsTimes_me.get_unit()
     self.jonesmeta = {}
     self.jonesmeta['refFrame'] = 'ITRF'
Ejemplo n.º 14
0
 def __init__(self, obsTimespy, ITRF2stnrot, do_parallactic_rot=True):
     super(PJones, self).__init__()
     obsTimes_lst = []
     for obsTimepy in obsTimespy:
         obsTimes_lst.append(quantity(obsTimepy.isoformat()).get_value())
     obsTimes_me = quantity(obsTimes_lst, 'd')
     self.obsTimes = obsTimes_me.get_value()
     self.obsTimeUnit = obsTimes_me.get_unit()
     self.ITRF2stnrot = ITRF2stnrot
     self.do_parallactic_rot = do_parallactic_rot
Ejemplo n.º 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
Ejemplo n.º 16
0
 def getBeam(self):
     """
     Return the beam size of the image
     """
     this_pim = pim.image(self.imagename)
     info_dict = this_pim.info()['imageinfo']['restoringbeam']
     # get beam info
     bpar_ma = quanta.quantity(info_dict['major']).get_value('arcsec')
     bpar_mi = quanta.quantity(info_dict['minor']).get_value('arcsec')
     bpar_pa = quanta.quantity(info_dict['positionangle']).get_value('deg')
     #print('\n{0} - Beam: maj {1:0.3f} (arcsec), min {2:2.3f} (arcsec), pa {3:0.2f} (deg)'.format(img, bpar_ma, bpar_mi,bpar_pa))
     return (bpar_ma,bpar_mi,bpar_pa)
Ejemplo n.º 17
0
    def riseset(self, crd, ev="5deg"):
        """This will give the rise/set times of a source. It needs the
        position in the frame, and a time. If the latter is not set, the
        current time will be used.

        :param crd: a direction measure
        :param ev: the elevation limit as a quantity or string
        :returns: The returned value is a `dict` with a
                  'solved' key, which is `False` if the source is always
                  below or above the horizon. In that case the rise and set
                  fields will all have a string value. The `dict` also returns
                  a rise and set `dict`, with  'last'  and 'utc' keys showing
                  the rise and set times as epochs.

        """

        a = self.rise(crd, ev)
        if isinstance(a['rise'], string_types):
            return {
                "rise": {
                    "last": a[0],
                    "utc": a[0]
                },
                "set": {
                    "last": a[1],
                    "utc": a[1]
                },
                "solved": False
            }
        ofe = self.measure(self._framestack["epoch"], "utc")
        if not is_measure(ofe):
            ofe = self.epoch('utc', 'today')
        x = a.copy()
        for k in x:
            x[k] = self.measure(
                self.epoch(
                    "last",
                    a[k].totime(),
                    off=self.epoch(
                        "r_utc",
                        (dq.quantity(ofe["m0"]) + dq.quantity("0.5d")))),
                "utc")
        return {
            "rise": {
                "last": self.epoch("last", a["rise"].totime()),
                "utc": x["rise"]
            },
            "set": {
                "last": self.epoch("last", a["set"].totime()),
                "utc": x["set"]
            },
            "solved": True
        }
Ejemplo n.º 18
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
Ejemplo n.º 19
0
    def expand(self, v):
        """Calculates the differences between a series of given measure values:
        it calculates baseline values from position values.

        :params v: a measure (of type 'baseline', 'position' or 'uvw')
        :returns: a `dict` with the value for key `measures` being a measure
                  and the value for key `xyz` a quantity containing the
                  differences.

        Example::

            >>> from casacore.quanta import quantity
            >>> x = quantity([10,50],'m')
            >>> y = quantity([20,100],'m')
            >>> z = quantity([30,150],'m')
            >>> sb = dm.baseline('itrf', x, y, z)
            >>> out = dm.expand(sb)
            >>> print out['xyz']
            [40.000000000000014, 80.0, 120.0] m

        """
        if not is_measure(v) or v['type'] not in ['baseline',
                                                  'position', 'uvw']:
            raise TypeError("Can only expand baselines, positions, or uvw")
        vw = v.copy()
        vw['type'] = "uvw"
        vw['refer'] = "J2000"
        outm = _measures.expand(self, vw)
        outm['xyz'] = dq.quantity(outm['xyz'])
        outm['measure']['type'] = v['type']
        outm['measure']['refer'] = v['refer']
        return outm
Ejemplo n.º 20
0
    def frequency(self, rf='', v0='0Hz', off=None):
        """Defines a frequency measure. It has to specify a reference code,
        frequency quantity value (see introduction for the action on a scalar
        quantity with either a vector or scalar value, and when a vector of
        quantities is given), and optionally it can specify an offset, which
        in itself has to be a frequency.

        :param rf: reference code string; Allowable reference
                   codes are: *REST LSRK LSRD BARY GEO TOPO GALACTO*
                   Note that additional ones may become available. Check with::

                       dm.list_codes(dm.frequency())

        :param v0: frequency value as quantity or string. The frequency
                   quantity values should be in one of the recognised units
                   (examples all give same frequency):

                   * value with time units: a period (0.5s)
                   * value as frequency: 2Hz
                   * value in angular frequency: 720deg/s
                   * value as length: 149896km
                   * value as wave number: 4.19169e-8m-1
                   * value as enery (h.nu): 8.27134e-9ueV
                   * value as momentum: 4.42044e-42kg.m

        :param off: an optional offset measure of same type

        """
        loc = {'type': "frequency", 'refer': rf, 'm0': dq.quantity(v0)}
        if is_measure(off):
            if not off['type'] == "frequency":
                raise TypeError('Illegal offset type specified.')
            loc["offset"] = off
        return self.measure(loc, rf)
Ejemplo n.º 21
0
    def expand(self, v):
        """Calculates the differences between a series of given measure values:
        it calculates baseline values from position values.

        :params v: a measure (of type 'baseline', 'position' or 'uvw')
        :returns: a `dict` with the value for key `measures` being a measure
                  and the value for key `xyz` a quantity containing the
                  differences.

        Example::

            >>> from casacore.quanta import quantity
            >>> x = quantity([10,50],'m')
            >>> y = quantity([20,100],'m')
            >>> z = quantity([30,150],'m')
            >>> sb = dm.baseline('itrf', x, y, z)
            >>> out = dm.expand(sb)
            >>> print out['xyz']
            [40.000000000000014, 80.0, 120.0] m

        """
        if not is_measure(v) or v['type'] not in ['baseline',
                                                  'position', 'uvw']:
            raise TypeError("Can only expand baselines, positions, or uvw")
        vw = v.copy()
        vw['type'] = "uvw"
        vw['refer'] = "J2000"
        outm = _measures.expand(self, vw)
        outm['xyz'] = dq.quantity(outm['xyz'])
        outm['measure']['type'] = v['type']
        outm['measure']['refer'] = v['refer']
        return outm
Ejemplo n.º 22
0
    def epoch(self, rf='', v0='0.0d', off=None):
        """
        Defines an epoch measure. It has to specify a reference code, an epoch
        quantity value (see introduction for the action on a scalar quantity
        with either a vector or scalar value, and when a vector of quantities
        is given), and optionally it can specify an offset, which in itself
        has to be an epoch.

        :param rf: reference code string; Allowable reference
                   codes are: *UTC TAI LAST LMST GMST1 GAST UT1 UT2 TDT TCG
                   TDB TCB*
                   Note that additional ones may become available. Check with::

                       dm.list_codes(dm.position())

        :param v0: time as quantity or string
        :param off: an optional offset measure of same type

        """
        loc = {'type': 'epoch', 'refer': rf}
        loc['m0'] = dq.quantity(v0)
        if is_measure(off):
            if not off['type'] == "epoch":
                raise TypeError('Illegal offset type specified.')
            loc["offset"] = off
        return self.measure(loc, rf)
Ejemplo n.º 23
0
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
Ejemplo n.º 24
0
    def radialvelocity(self, rf='', v0='0m/s', off=None):
        """Defines a radialvelocity measure. It has to specify a reference
        code, radialvelocity quantity value (see introduction for the action
        on a scalar quantity with either a vector or scalar value, and when
        a vector of quantities is given), and optionally it can specify an
        offset, which in itself has to be a radialvelocity.


        :param rf: reference code string; Allowable reference
                   codes are: *LSRK LSRD BARY GEO TOPO GALACTO*
                   Note that additional ones may become available. Check with::

                       dm.list_codes(dm.radialvelocity())

        :param v0: longitude or x as quantity or string
        :param off: an optional offset measure of same type

        """
        loc = {'type': "radialvelocity",
               'refer': rf,
               'm0': dq.quantity(v0)}
        if is_measure(off):
            if not off['type'] == "radialvelocity":
                raise TypeError('Illegal offset type specified.')
            loc["offset"] = off
        return self.measure(loc, rf)
Ejemplo n.º 25
0
    def doppler(self, rf='', v0=0.0, off=None):
        """Defines a doppler measure. It has to specify a reference code,
        doppler quantity value (see introduction for the action on a scalar
        quantity with either a vector or scalar value, and when a vector of
        quantities is given), and optionally it can specify an offset, which
        in itself has to be a doppler.

        :param rf: reference code string; Allowable reference
                   codes are: *RADIO OPTICAL Z RATIO RELATIVISTIC BETA GAMMA*.
                   Note that additional ones may become available. Check with::

                       dm.list_codes(dm.doppler())

        :param v0: doppler ratio as quantity, string or float value. It
                   should be either non-dimensioned to specify a ratio of
                   the light velocity, or in velocity. (examples all give
                   same doppler):
        :param off: an optional offset measure of same type

        Example::

            >>> from casacore import quanta
            >>> dm.doppler('radio', 0.4)
            >>> dm.doppler('radio', '0.4')
            >>> dm.doppler('RADIO', quanta.constants['c']*0.4))

        """
        if isinstance(v0, float):
            v0 = str(v0)
        loc = {'type': "doppler", 'refer': rf, 'm0': dq.quantity(v0)}
        if is_measure(off):
            if not off['type'] == "doppler":
                raise TypeError('Illegal offset type specified.')
            loc["offset"] = off
        return self.measure(loc, rf)
Ejemplo n.º 26
0
    def radialvelocity(self, rf='', v0='0m/s', off=None):
        """Defines a radialvelocity measure. It has to specify a reference
        code, radialvelocity quantity value (see introduction for the action
        on a scalar quantity with either a vector or scalar value, and when
        a vector of quantities is given), and optionally it can specify an
        offset, which in itself has to be a radialvelocity.


        :param rf: reference code string; Allowable reference
                   codes are: *LSRK LSRD BARY GEO TOPO GALACTO*
                   Note that additional ones may become available. Check with::

                       dm.list_codes(dm.radialvelocity())

        :param v0: longitude or x as quantity or string
        :param off: an optional offset measure of same type

        """
        loc = {'type': "radialvelocity",
               'refer': rf,
               'm0': dq.quantity(v0)}
        if is_measure(off):
            if not off['type'] == "radialvelocity":
                raise TypeError('Illegal offset type specified.')
            loc["offset"] = off
        return self.measure(loc, rf)
Ejemplo n.º 27
0
    def epoch(self, rf='', v0='0.0d', off=None):
        """
        Defines an epoch measure. It has to specify a reference code, an epoch
        quantity value (see introduction for the action on a scalar quantity
        with either a vector or scalar value, and when a vector of quantities
        is given), and optionally it can specify an offset, which in itself
        has to be an epoch.

        :param rf: reference code string; Allowable reference
                   codes are: *UTC TAI LAST LMST GMST1 GAST UT1 UT2 TDT TCG
                   TDB TCB*
                   Note that additional ones may become available. Check with::

                       dm.list_codes(dm.position())

        :param v0: time as quantity or string
        :param off: an optional offset measure of same type

        """
        loc = {'type': 'epoch', 'refer': rf}
        loc['m0'] = dq.quantity(v0)
        if is_measure(off):
            if not off['type'] == "epoch":
                raise TypeError('Illegal offset type specified.')
            loc["offset"] = off
        return self.measure(loc, rf)
Ejemplo n.º 28
0
def mjd2datetime(mjd):
    """
    Convert a Modified Julian Date to datetime via 'unix time' representation.

    NB 'unix time' is defined by the casacore/casacore package.
    """
    q = quantity("%sd" % mjd)
    return datetime.datetime.fromtimestamp(q.to_unix_time())
Ejemplo n.º 29
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
Ejemplo n.º 30
0
def mjd2datetime(mjd):
    """
    Convert a Modified Julian Date to datetime via 'unix time' representation.

    NB 'unix time' is defined by the casacore/casacore package.
    """
    q = quantity("%sd" % mjd)
    return datetime.datetime.fromtimestamp(q.to_unix_time())
Ejemplo n.º 31
0
    def riseset(self, crd, ev="5deg"):
        """This will give the rise/set times of a source. It needs the
        position in the frame, and a time. If the latter is not set, the
        current time will be used.

        :param crd: a direction measure
        :param ev: the elevation limit as a quantity or string
        :returns: The returned value is a `dict` with a
                  'solved' key, which is `False` if the source is always
                  below or above the horizon. In that case the rise and set
                  fields will all have a string value. The `dict` also returns
                  a rise and set `dict`, with  'last'  and 'utc' keys showing
                  the rise and set times as epochs.

        """

        a = self.rise(crd, ev)
        if isinstance(a['rise'], str):
            return {"rise": {"last": a[0], "utc": a[0]},
                    "set": {"last": a[1], "utc": a[1]},
                    "solved": False}
        ofe = self.measure(self._framestack["epoch"], "utc")
        if not is_measure(ofe):
            ofe = self.epoch('utc', 'today')
        x = a.copy()
        for k in x:
            x[k] = self.measure(
                self.epoch("last",
                           a[k].totime(),
                           off=self.epoch("r_utc",
                                          (dq.quantity(ofe["m0"])
                                           + dq.quantity("0.5d")
                                           ))
                           ),
                "utc")
        return {"rise": {"last": self.epoch("last",
                                            a["rise"].totime()),
                         "utc": x["rise"]},

                "set": {"last": self.epoch("last",
                                           a["set"].totime()),
                        "utc": x["set"]},
                "solved": True
                }
Ejemplo n.º 32
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
Ejemplo n.º 33
0
 def set_time(self, value):
     if is_measure(value):
         self.set_measure(value)
     elif isinstance(value, datetime_casacore):
         self.set_measure(value.epoch())
     elif isinstance(value, datetime.datetime):
         self.set_measure(datetime_casacore.from_datetime(value).epoch())
     elif isinstance(value, float):
         self.set_measure(self._measures.epoch("UTC", quantity(value, "s")))
     else:
         raise TypeError("Unsupported type {} in set_time".format(
             type(value)))
Ejemplo n.º 34
0
    def rise(self, crd, ev='5deg'):
        """This method will give the rise/set hour-angles of a source. It
        needs the position in the frame, and a time. If the latter is not
        set, the current time will be used.

        :param crd: a direction measure
        :param ev: the elevation limit as a quantity or string
        :returns: `dict` with rise and set sidereal time quantities or a 2
                   strings "below" or "above"
        """
        if not is_measure(crd):
            raise TypeError('No rise/set coordinates specified')
        ps = self._getwhere()
        self._fillnow()
        hd = self.measure(crd, "hadec")
        c = self.measure(crd, "app")
        evq = dq.quantity(ev)
        hdm1 = dq.quantity(hd["m1"])
        psm1 = dq.quantity(ps["m1"])
        ct = (dq.sin(dq.quantity(ev)) - (dq.sin(hdm1) * dq.sin(psm1))) \
             / (dq.cos(hdm1) * dq.cos(psm1))

        if ct.get_value() >= 1:
            return {'rise': 'below', 'set': 'below'}
        if ct.get_value() <= -1:
            return {'rise': 'above', 'set': 'above'}
        a = dq.acos(ct)
        return dict(rise=dq.quantity(c["m0"]).norm(0) - a,
                    set=dq.quantity(c["m0"]).norm(0) + a)
Ejemplo n.º 35
0
    def rise(self, crd, ev='5deg'):
        """This method will give the rise/set hour-angles of a source. It
        needs the position in the frame, and a time. If the latter is not
        set, the current time will be used.

        :param crd: a direction measure
        :param ev: the elevation limit as a quantity or string
        :returns: `dict` with rise and set sidereal time quantities or a 2
                   strings "below" or "above"
        """
        if not is_measure(crd):
            raise TypeError('No rise/set coordinates specified')
        ps = self._getwhere()
        self._fillnow()
        hd = self.measure(crd, "hadec")
        c = self.measure(crd, "app")
        evq = dq.quantity(ev)
        hdm1 = dq.quantity(hd["m1"])
        psm1 = dq.quantity(ps["m1"])
        ct = (dq.sin(dq.quantity(ev)) - (dq.sin(hdm1) * dq.sin(psm1))) \
             / (dq.cos(hdm1) * dq.cos(psm1))

        if ct.get_value() >= 1:
            return {'rise': 'below', 'set': 'below'}
        if ct.get_value() <= -1:
            return {'rise': 'above', 'set': 'above'}
        a = dq.acos(ct)
        return dict(rise=dq.quantity(c["m0"]).norm(0) - a,
                    set=dq.quantity(c["m0"]).norm(0) + a)
Ejemplo n.º 36
0
    def touvw(self, v):
        """Calculates a uvw measure from a baseline. The baseline can consist
        of a vector of actual baseline positions. Note that the baseline does
        not have to be a proper baseline, but can be a series of positions
        (to  call positions baselines see :meth:`asbaseline` ) for speed
        reasons: operations are linear and can be done on positions, which
        are converted to baseline values at the end (with :meth:`expand` ).

        Whatever the reference code of the baseline, the returned uvw will be
        given in J2000. If the dot argument is given, that variable will be
        filled with a quantity array consisting of the time derivative of the
        uvw (note that only the sidereal rate is taken into account; not
        precession, earth tides and similar variations, which are much
        smaller). If the xyz variable is given, it will be filled with the
        quantity values of the uvw measure.

        The values of the input baselines can be given as a quantity
        vector per x, y or z value.

        uvw coordinates are calculated for a certain direction in the sky
        hence the frame has to contain the direction for the calculation to
        work. Since the baseline and the sky rotate with respect of each
        other, the time should be specified as well.

        Example::

            >>> dm.do_frame(dm.observatory('atca'))
            >>> dm.do_frame(dm.source('1934-638'))
            >>> dm.do_frame(dm.epoch('utc', 'today'))
            >>> b = dm.baseline('itrf', '10m', '20m', '30m')

        """
        if is_measure(v) and v['type'] == 'baseline':
            m = _measures.uvw(self, v)
            m['xyz'] = dq.quantity(m['xyz'])
            m['dot'] = dq.quantity(m['dot'])
            return m
        else:
            raise TypeError('Illegal Baseline specified')
Ejemplo n.º 37
0
    def touvw(self, v):
        """Calculates a uvw measure from a baseline. The baseline can consist
        of a vector of actual baseline positions. Note that the baseline does
        not have to be a proper baseline, but can be a series of positions
        (to  call positions baselines see :meth:`asbaseline` ) for speed
        reasons: operations are linear and can be done on positions, which
        are converted to baseline values at the end (with :meth:`expand` ).

        Whatever the reference code of the baseline, the returned uvw will be
        given in J2000. If the dot argument is given, that variable will be
        filled with a quantity array consisting of the time derivative of the
        uvw (note that only the sidereal rate is taken into account; not
        precession, earth tides and similar variations, which are much
        smaller). If the xyz variable is given, it will be filled with the
        quantity values of the uvw measure.

        The values of the input baselines can be given as a quantity
        vector per x, y or z value.

        uvw coordinates are calculated for a certain direction in the sky
        hence the frame has to contain the direction for the calculation to
        work. Since the baseline and the sky rotate with respect of each
        other, the time should be specified as well.

        Example::

            >>> dm.do_frame(dm.observatory('atca'))
            >>> dm.do_frame(dm.source('1934-638'))
            >>> dm.do_frame(dm.epoch('utc', 'today'))
            >>> b = dm.baseline('itrf', '10m', '20m', '30m')

        """
        if is_measure(v) and v['type'] == 'baseline':
            m = _measures.uvw(self, v)
            m['xyz'] = dq.quantity(m['xyz'])
            m['dot'] = dq.quantity(m['dot'])
            return m
        else:
            raise TypeError('Illegal Baseline specified')
Ejemplo n.º 38
0
    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
Ejemplo n.º 39
0
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
Ejemplo n.º 40
0
    def position(self, rf='', v0='0..', v1='90..', v2='0m', off=None):
        """Defines a position measure. It has to specify a reference code,
        position quantity values (see introduction for the action on a
        scalar quantity with either a vector or scalar value, and when a
        vector of quantities is given), and optionally it can specify an
        offset, which in itself has to be a position.
        Note that additional ones may become available. Check with::

            dm.list_codes(dm.position())

        The position quantity values should be either longitude (angle),
        latitude(angle) and height(length); or x,y,z (length). See
        :func:`~casacore.quanta.quantity` for possible angle formats.

        :param rf: reference code string; Allowable reference
                   codes are: *WGS84* *ITRF* (World Geodetic System and
                   International Terrestrial Reference Frame)
        :param v0: longitude or x as quantity or string
        :param v1: latitude or y as quantity or string
        :param v2: height or z as quantity or string
        :param off: an optional offset measure of same type

        Example::

            dm.position('wgs84','30deg','40deg','10m')
            dm.observatory('ATCA')

        """
        loc = {'type': 'position', 'refer': rf}
        loc['m0'] = dq.quantity(v0)
        loc['m1'] = dq.quantity(v1)
        loc['m2'] = dq.quantity(v2)
        if is_measure(off):
            if not off['type'] == "position":
                raise TypeError('Illegal offset type specified.')
            loc["offset"] = off
        return self.measure(loc, rf)
Ejemplo n.º 41
0
    def direction(self, rf='', v0='0..', v1='90..', off=None):
        """Defines a direction measure. It has to specify a reference code,
        direction quantity values (see introduction for the action on a
        scalar quantity with either a vector or scalar value, and when a
        vector of quantities is given), and optionally it can specify an
        offset, which in itself has to be a direction.

        :param rf: reference code string; allowable reference codes are:
                   J2000 JMEAN  JTRUE APP B1950 BMEAN BTRUE GALACTIC HADEC
                   AZEL SUPERGAL ECLIPTIC MECLIPTIC TECLIPTIC MERCURY VENUS
                   MARS JUPITER SATURN URANUS NEPTUNE PLUTO MOON SUN COMET.
                   Note that additional ones may become available. Check with::

                       dm.list_codes(dm.direction())

        :param v0, v1: Direction quantity values should be
                       longitude (angle) and latitude (angle) or strings
                       parsable by :func:`~casacore.quanta.quantity`.
                       None are needed for planets: the frame epoch defines
                       coordinates. See :func:`~casacore.quanta.quantity` for
                       possible angle formats.
        :param off: an optional offset measure of same type

        Example::

            >>> dm.direction('j2000','30deg','40deg')
            >>> dm.direction('mars')

        """
        loc = {'type': 'direction', 'refer': rf}
        loc['m0'] = dq.quantity(v0)
        loc['m1'] = dq.quantity(v1)
        if is_measure(off):
            if not off['type'] == "direction":
                raise TypeError('Illegal offset type specified.')
            loc["offset"] = off
        return self.measure(loc, rf)
Ejemplo n.º 42
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
Ejemplo n.º 43
0
    def position(self, rf='', v0='0..', v1='90..', v2='0m', off=None):
        """Defines a position measure. It has to specify a reference code,
        position quantity values (see introduction for the action on a
        scalar quantity with either a vector or scalar value, and when a
        vector of quantities is given), and optionally it can specify an
        offset, which in itself has to be a position.
        Note that additional ones may become available. Check with::

            dm.list_codes(dm.position())

        The position quantity values should be either longitude (angle),
        latitude(angle) and height(length); or x,y,z (length). See
        :func:`~casacore.quanta.quantity` for possible angle formats.

        :param rf: reference code string; Allowable reference
                   codes are: *WGS84* *ITRF* (World Geodetic System and
                   International Terrestrial Reference Frame)
        :param v0: longitude or x as quantity or string
        :param v1: latitude or y as quantity or string
        :param v2: height or z as quantity or string
        :param off: an optional offset measure of same type

        Example::

            dm.position('wgs84','30deg','40deg','10m')
            dm.observatory('ATCA')

        """
        loc = {'type': 'position', 'refer': rf}
        loc['m0'] = dq.quantity(v0)
        loc['m1'] = dq.quantity(v1)
        loc['m2'] = dq.quantity(v2)
        if is_measure(off):
            if not off['type'] == "position":
                raise TypeError('Illegal offset type specified.')
            loc["offset"] = off
        return self.measure(loc, rf)
Ejemplo n.º 44
0
    def direction(self, rf='', v0='0..', v1='90..', off=None):
        """Defines a direction measure. It has to specify a reference code,
        direction quantity values (see introduction for the action on a
        scalar quantity with either a vector or scalar value, and when a
        vector of quantities is given), and optionally it can specify an
        offset, which in itself has to be a direction.

        :param rf: reference code string; allowable reference codes are:
                   J2000 JMEAN  JTRUE APP B1950 BMEAN BTRUE GALACTIC HADEC
                   AZEL SUPERGAL ECLIPTIC MECLIPTIC TECLIPTIC MERCURY VENUS
                   MARS JUPITER SATURN URANUS NEPTUNE PLUTO MOON SUN COMET.
                   Note that additional ones may become available. Check with::

                       dm.list_codes(dm.direction())

        :param v0, v1: Direction quantity values should be
                       longitude (angle) and latitude (angle) or strings
                       parsable by :func:`~casacore.quanta.quantity`.
                       None are needed for planets: the frame epoch defines
                       coordinates. See :func:`~casacore.quanta.quantity` for
                       possible angle formats.
        :param off: an optional offset measure of same type

        Example::

            >>> dm.direction('j2000','30deg','40deg')
            >>> dm.direction('mars')

        """
        loc = {'type': 'direction', 'refer': rf}
        loc['m0'] = dq.quantity(v0)
        loc['m1'] = dq.quantity(v1)
        if is_measure(off):
            if not off['type'] == "direction":
                raise TypeError('Illegal offset type specified.')
            loc["offset"] = off
        return self.measure(loc, rf)
Ejemplo n.º 45
0
def hmstora(rah, ram, ras):
    """Convert RA in hours, minutes, seconds format to decimal
    degrees format.

    Keyword arguments:
    rah,ram,ras -- RA values (h,m,s)

    Return value:
    radegs -- RA in decimal degrees

    """
    sign, rah, ram, ras = propagate_sign(rah, ram, ras)
    ra = quantity("%s%dH%dM%f" % (sign, rah, ram, ras)).get_value()
    if abs(ra) >= 360:
        raise ValueError("coordinates out of range")
    return ra
Ejemplo n.º 46
0
def dmstodec(decd, decm, decs):
    """Convert Dec in degrees, minutes, seconds format to decimal
    degrees format.

    Keyword arguments:
    decd, decm, decs -- list of Dec values (d,m,s)

    Return value:
    decdegs -- Dec in decimal degrees

    """
    sign, decd, decm, decs = propagate_sign(decd, decm, decs)
    dec = quantity("%s%dD%dM%f" % (sign, decd, decm, decs)).get_value()
    if abs(dec) > 90:
        raise ValueError("coordinates out of range")
    return dec
Ejemplo n.º 47
0
def dmstodec(decd, decm, decs):
    """Convert Dec in degrees, minutes, seconds format to decimal
    degrees format.

    Keyword arguments:
    decd, decm, decs -- list of Dec values (d,m,s)

    Return value:
    decdegs -- Dec in decimal degrees

    """
    sign, decd, decm, decs = propagate_sign(decd, decm, decs)
    dec = quantity("%s%dD%dM%f" % (sign, decd, decm, decs)).get_value()
    if abs(dec) > 90:
        raise ValueError("coordinates out of range")
    return dec
Ejemplo n.º 48
0
    def getvalue(self, v):
        """
        Return a list of quantities making up the measures' value.

        :param v: a measure
        """
        if not is_measure(v):
            raise TypeError('Incorrect input type for getvalue()')
        import re
        rx = re.compile("m\d+")
        out = []
        keys = v.keys()[:]
        keys.sort()
        for key in keys:
            if re.match(rx, key):
                out.append(dq.quantity(v.get(key)))
        return out
Ejemplo n.º 49
0
 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
Ejemplo n.º 50
0
    def doppler(self, rf='', v0=0.0, off=None):
        """Defines a doppler measure. It has to specify a reference code,
        doppler quantity value (see introduction for the action on a scalar
        quantity with either a vector or scalar value, and when a vector of
        quantities is given), and optionally it can specify an offset, which
        in itself has to be a doppler.

        :param rf: reference code string; Allowable reference
                   codes are: *RADIO OPTICAL Z RATIO RELATIVISTIC BETA GAMMA*.
                   Note that additional ones may become available. Check with::

                       dm.list_codes(dm.doppler())

        :param v0: doppler ratio as quantity, string or float value. It
                   should be either non-dimensioned to specify a ratio of
                   the light velocity, or in velocity. (examples all give
                   same doppler):
        :param off: an optional offset measure of same type

        Example::

            >>> from casacore import quanta
            >>> dm.doppler('radio', 0.4)
            >>> dm.doppler('radio', '0.4')
            >>> dm.doppler('RADIO', quanta.constants['c']*0.4))

        """
        if isinstance(v0, float):
            v0 = str(v0)
        loc = {'type': "doppler",
               'refer': rf,
               'm0': dq.quantity(v0)}
        if is_measure(off):
            if not off['type'] == "doppler":
                raise TypeError('Illegal offset type specified.')
            loc["offset"] = off
        return self.measure(loc, rf)
Ejemplo n.º 51
0
    def frequency(self, rf='', v0='0Hz', off=None):
        """Defines a frequency measure. It has to specify a reference code,
        frequency quantity value (see introduction for the action on a scalar
        quantity with either a vector or scalar value, and when a vector of
        quantities is given), and optionally it can specify an offset, which
        in itself has to be a frequency.

        :param rf: reference code string; Allowable reference
                   codes are: *REST LSRK LSRD BARY GEO TOPO GALACTO*
                   Note that additional ones may become available. Check with::

                       dm.list_codes(dm.frequency())

        :param v0: frequency value as quantity or string. The frequency
                   quantity values should be in one of the recognised units
                   (examples all give same frequency):

                   * value with time units: a period (0.5s)
                   * value as frequency: 2Hz
                   * value in angular frequency: 720deg/s
                   * value as length: 149896km
                   * value as wave number: 4.19169e-8m-1
                   * value as enery (h.nu): 8.27134e-9ueV
                   * value as momentum: 4.42044e-42kg.m

        :param off: an optional offset measure of same type

        """
        loc = {'type': "frequency",
               'refer': rf,
               'm0': dq.quantity(v0)}
        if is_measure(off):
            if not off['type'] == "frequency":
                raise TypeError('Illegal offset type specified.')
            loc["offset"] = off
        return self.measure(loc, rf)
Ejemplo n.º 52
0
 def format_quantum(self, val, unit):
     q=quanta.quantity(val,unit)
     if q.canonical().get_unit() in ['rad','s']:
         return quanta.quantity(val, 'm').formatted()[:-1]+unit
     else:
         return q.formatted()
Ejemplo n.º 53
0
def main(images, vertices, outfits, maxwidth=0):
    """
    Creates mosaic

    Parameters
    ----------
    images : str or list of str
        List of filenames of facet images. May be given as a list or as a string
        (e.g., '[image1, image2]'
    vertices : str or list of str
        List of filenames of facet vertices files. May be given as
        a list or as a string (e.g., '[vert1, vert2]'
    outfits : str
        Filename of output FITS mosaic
    maxwidth : int, optional
        Maximum number of pixels to consider for the width of the mosaic
        [default 0 = unlimited] This can be helpful at high declination.

    """
    if type(images) is str:
        images = images.strip('[]').split(',')
        images = [im.strip() for im in images]
    if type(vertices) is str:
        vertices = vertices.strip('[]').split(',')
        vertices = [v.strip() for v in vertices]

    formstr = '{0:45s}  {1:45s} {2:s}  {3:s} {4:s} {5:s}'
    print formstr.format("-----","--------","------------","-------","-------","------")
    print formstr.format("Image", "FC reg","Norm. weight", "Maj(ac)", "Min(ac)","PA(deg)")
    print formstr.format("-----","--------","------------","-------","-------","------")

    psf_fwhm = [] # resolution
    frequency = [] # frequency of images (should be equal?)
    for i in range(len(images)):
        this_pim = pim.image(images[i])
        info_dict = this_pim.info()['imageinfo']['restoringbeam']
        bpar_ma = quanta.quantity(info_dict['major']).get_value('deg')
        bpar_mi = quanta.quantity(info_dict['minor']).get_value('deg')
        bpar_pa = quanta.quantity(info_dict['positionangle']).get_value('deg')
        psf_fwhm.append([bpar_ma, bpar_mi, bpar_pa])
        frequency.append(this_pim.info()['coordinates']['spectral2']['restfreq'])
        print '{0:45.45s}  {1:45.45s} {2:0.2f}          {3:0.2f}    {4:0.2f}    {5:0.2f}'.format(images[i], vertices[i], 0, bpar_ma*60, bpar_mi*60,bpar_pa)

    psf_fwhm = np.array(psf_fwhm)
    frequency = np.array(frequency)
    mean_psf_fwhm = np.mean(psf_fwhm, axis=0)
    mean_frequency = np.mean(frequency)

    # Initialize some vectors
    declims = [] # store the limits of the declination axes
    raleft = []
    raright = []
    rainc = [] # store the r.a. increments in case they differ
    decinc = [] # store the dec increments in case they differ
    pims = [] # stores the casacore images of the data

    # Get image frames for input images
    for im in images:
        image = pim.image(im)
        sptcoords = image.coordinates().get_coordinate('spectral')
        nc = sptcoords.get_axis_size()

        # Get Stokes axis. Ensure we are working with the Stokes parameter requested.
        stkcoords = image.coordinates().get_coordinate('stokes')
        if stkcoords.get_axis_size() == 1:
            assert(stkcoords.get_stokes()[0] == 'I')
        else:
            stks = stkcoords.get_stokes().index('I')
            image = image.subimage(blc=(0, stks), trc=(nc-1, stks), dropdegenerate=False)
        ns = 1

        dircoords = image.coordinates().get_coordinate('direction')
        nx = dircoords.get_axis_size(axis=1)
        ny = dircoords.get_axis_size(axis=0)
        inc = dircoords.get_increment()
        ref = dircoords.get_referencepixel()
        val = dircoords.get_referencevalue()
        # wsclean image header is weird
        if val[1]<0:
            val[1]+=2*np.pi
        ra_axis = (range(nx)-ref[1])*inc[1]+val[1]
        dec_axis = (range(ny)-ref[0])*inc[0]+val[0]
        rainc.append(inc[1])
        decinc.append(inc[0])
        declims.append(min(dec_axis))
        declims.append(max(dec_axis))
        mean_ra = np.mean(ra_axis)
        raleft.append((ra_axis[0]-mean_ra)*np.cos(val[0])+mean_ra)
        raright.append((ra_axis[-1]-mean_ra)*np.cos(val[0])+mean_ra)
        pims.append(image)

    # Generate the mosaic coordinate frame
    master_dec = np.arange(min(declims),max(declims),min(decinc))
    if max(raleft)-min(raright) > 5.*np.pi/3.: # crossed RA=0
        for i in range(len(raright)):
            raright[i] = raright[i]-2.*np.pi
    master_ra = np.arange(max(raleft),min(raright),max(rainc))
    lmra = len(master_ra)
    if maxwidth != 0:
        if lmra > maxwidth:
            xboundary = (lmra-maxwidth)/2
            master_ra = master_ra[xboundary:-xboundary]
    print "Found ra,dec pixel increments (arcsec):"
    print np.array(rainc)*206265.,np.array(decinc)*206265.
    ma = pims[-1].coordinates()
    ma['direction'].set_referencepixel([len(master_dec)/2,len(master_ra)/2])
    ma['direction'].set_increment([decinc[np.argmin(np.abs(decinc))],rainc[np.argmin(np.abs(rainc))]])
    ma['direction'].set_referencevalue([master_dec[len(master_dec)/2],master_ra[len(master_ra)/2]])

    # Initialize the arrays for the output image, sensitivity, and weights
    master_im = np.zeros((len(master_dec),len(master_ra)))
    master_mask = np.zeros((len(master_dec),len(master_ra)))

    # Reproject the images onto the master grid, weight and normalize
    for i, im in enumerate(pims):
        print 'doing image',i
        im, mask = mask_vertices(im, vertices[i])
        im = im.regrid([2,3],ma,outshape=(int(nc),int(ns),len(master_dec),len(master_ra)))
        mask = mask.regrid([2,3],ma,outshape=(int(nc),int(ns),len(master_dec),len(master_ra)))
        master_im += np.squeeze(im.getdata())
        master_mask += np.squeeze(mask.getdata())

    blank=np.ones_like(im)*np.nan
    master_im=np.where(master_mask,master_im,blank)

    # Write fits files
    arrax = np.zeros( (1,1, len(master_im[:,0]), len(master_im[0,:])) )
    arrax[0,0,:,:] = master_im

    # Open new casa image for mosaic
    new_pim = pim.image('',shape=(1,1, len(master_dec),len(master_ra)), coordsys=ma)
    new_pim.putdata(arrax)
    # Write fits
    new_pim.tofits(outfits, overwrite=True)

    # need to add new beam info (not sure if this is possible with casacore)
    hdu = pyfits.open(outfits,mode='update')
    header = hdu[0].header
    header.update('BMAJ',mean_psf_fwhm[0])
    header.update('BMIN',mean_psf_fwhm[1])
    header.update('BPA',mean_psf_fwhm[2])
    header.update('BUNIT',pims[-1].info()['unit'])
    header.update('RESTFRQ',mean_frequency)
    header.update('RESTFREQ',mean_frequency)
    newhdu = pyfits.PrimaryHDU(data=hdu[0].data, header=header)
    newhdu.writeto(outfits,clobber=True)
Ejemplo n.º 54
0


# NB: datetime is not sensitive to leap seconds.
# However, leap seconds were first introduced in 1972.
# So there are no leap seconds between the start of the
# Modified Julian epoch and the start of the Unix epoch,
# so this calculation is safe.
#julian_epoch = datetime.datetime(1858, 11, 17)
#unix_epoch = datetime.datetime(1970, 1, 1, 0, 0)
#delta = unix_epoch - julian_epoch
#deltaseconds = delta.total_seconds()
#unix_epoch = 3506716800

# The above is equivalent to this:
unix_epoch = quantity("1970-01-01T00:00:00").get_value('s')

def julian2unix(timestamp):
    """
    Convert a modifed julian timestamp (number of seconds since 17 November
    1858) to Unix timestamp (number of seconds since 1 January 1970).

    Args:
        timestamp (numbers.Number): Number of seconds since the Unix epoch.

    Returns:
        numbers.Number: Number of seconds since the modified Julian epoch.
    """
    return timestamp - unix_epoch

Ejemplo n.º 55
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
Ejemplo n.º 56
0
 def format_date(self, val, unit):
     if val==numpy.floor(val):
         # Do not show time part if 0
         return quanta.quantity(val,unit).formatted('YMD_ONLY')
     else:
         return quanta.quantity(val,unit).formatted('DMY')
Ejemplo n.º 57
0
    def format_cell(self, val, colkeywords):
        out=""

        # String arrays are returned as dict, undo that for printing
        if isinstance(val, dict):
            tmpdict=numpy.array(val['array'])
            tmpdict.reshape(val['shape'])
            # Leave out quotes around strings
            numpy.set_printoptions(formatter={'all':lambda x: str(x)})
            out+=numpy.array2string(tmpdict, separator=', ')
            numpy.set_printoptions(formatter=None)
        else:
            valtype='other'
            singleUnit=('QuantumUnits' in colkeywords and (numpy.array(colkeywords['QuantumUnits'])==numpy.array(colkeywords['QuantumUnits'])[0]).all())
            if colkeywords.get('MEASINFO',{}).get('type')=='epoch' and singleUnit:
                # Format a date/time. Use quanta for scalars, use numpy for array logic around it (quanta does not support higher dimensional arrays)
                valtype='epoch'
                if isinstance(val, numpy.ndarray):
                    numpy.set_printoptions(formatter={'all':lambda x: self.format_date(x,colkeywords['QuantumUnits'][0])})
                    out+=numpy.array2string(val,separator=', ')
                    numpy.set_printoptions(formatter=None)
                else:
                    out+=self.format_date(val,colkeywords['QuantumUnits'][0])
            elif colkeywords.get('MEASINFO',{}).get('type')=='direction' and singleUnit and val.shape==(1,2):
                # Format one direction. TODO: extend to array of directions
                    valtype='direction'
                    out+="["
                    part=quanta.quantity(val[0,0],'rad').formatted("TIME",precision=9)
                    part=re.sub(r'(\d+):(\d+):(.*)',r'\1h\2m\3',part)
                    out+=part+", "
                    part=quanta.quantity(val[0,1],'rad').formatted("ANGLE",precision=9)
                    part=re.sub(r'(\d+)\.(\d+)\.(.*)',r'\1d\2m\3',part)
                    out+=part+"]"
            elif isinstance(val, numpy.ndarray) and singleUnit:
                # Format any array with units
                valtype='quanta'
                numpy.set_printoptions(formatter={'all':lambda x: self.format_quantum(x, colkeywords['QuantumUnits'][0])})
                out+=numpy.array2string(val,separator=', ')
                numpy.set_printoptions(formatter=None)
            elif isinstance(val, numpy.ndarray):
                valtype='other'
                # Undo quotes around strings
                numpy.set_printoptions(formatter={'all':lambda x: str(x)})
                out+=numpy.array2string(val,separator=', ')
                numpy.set_printoptions(formatter=None)
            elif singleUnit:
                valtype='onequantum'
                out+=self.format_quantum(val, colkeywords['QuantumUnits'][0])
            else:
                valtype='other'
                out+=str(val)

        if 'QuantumUnits' in colkeywords and valtype=='other':
            # Print units if they haven't been taken care of
            if not (numpy.array(colkeywords['QuantumUnits'])==numpy.array(colkeywords['QuantumUnits'])[0]).all():
                # Multiple different units for element in an array. TODO: do this properly
                # For now, just print the units and let the user figure out what it means
                out+=" "+str(colkeywords['QuantumUnits'])
            else:
                out+=" "+colkeywords['QuantumUnits'][0]

        # Numpy sometimes adds double newlines, don't do that
        out=out.replace('\n\n','\n')
        #return valtype+": "+out
        return out
Ejemplo n.º 58
0
# 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")