Beispiel #1
0
def findindex2Dsphere(azimg,elimg,az,el):
    """
    finds nearest row,column index of projection on 2-D sphere.
    E.g. an astronomical or auroral camera image

    inputs:
    azimg: azimuth of image pixels (deg)
    elimg: elevation of image pixels (deg)
    az: azimuth of point you seek to find the index for (deg)
    el: elevation of point you seek to find the index for (deg)

    output:
    row, column of 2-D image closest to az,el


    brute force method of finding the row,col with the closest az,el using haversine method
    useful for:
    azimuth, elevation
    right ascension, declination
    latitude, longitude
    """
    az = atleast_1d(az); el = atleast_1d(el)
    assert azimg.ndim == 2 and elimg.ndim == 2
    assert isinstance(az[0],float) and isinstance(el[0],float)

    adist = angledist(azimg,elimg,az,el)
    return unravel_index(adist.argmin(), azimg.shape)
Beispiel #2
0
def findindex2Dsphere(azimg, elimg, az, el):
    """
    finds nearest row,column index of projection on 2-D sphere.
    E.g. an astronomical or auroral camera image

    inputs:
    azimg: azimuth of image pixels (deg)
    elimg: elevation of image pixels (deg)
    az: azimuth of point you seek to find the index for (deg)
    el: elevation of point you seek to find the index for (deg)

    output:
    row, column of 2-D image closest to az,el


    brute force method of finding the row,col with the closest az,el using haversine method
    useful for:
    azimuth, elevation
    right ascension, declination
    latitude, longitude
    """
    az = atleast_1d(az)
    el = atleast_1d(el)
    assert azimg.ndim == 2 and elimg.ndim == 2
    assert isinstance(az[0], float) and isinstance(el[0], float)

    adist = angledist(azimg, elimg, az, el)
    return unravel_index(adist.argmin(), azimg.shape)
Beispiel #3
0
    def findLSQ(self,nearrow,nearcol,odir):
        polycoeff = np.polyfit(nearcol,nearrow,deg=1,full=False)
#%% columns (x)  to cut from picture
        # NOT range, NEED dtype= for arange api
        cutcol = np.arange(self.superx, dtype=int)
#%% rows (y) to cut from picture
        cutrow = np.rint(np.polyval(polycoeff,cutcol)).astype(int)
        assert (cutrow>=0).all() and (cutrow<self.supery).all(),'impossible least squares fit for 1-D cut\n is your video orientation correct? check the params of video hdf5 file'
        # DONT DO THIS: cutrow.clip(0,self.supery,cutrow)
#%% angle from magnetic zenith corresponding to those pixels
        radecMagzen = azel2radec(self.Baz,self.Bel,self.lat,self.lon,self.Bepoch)
        assert len(radecMagzen) == 2
        logging.info('mag. zen. ra,dec {}'.format(radecMagzen))

        if False: #astropy
            angledist_deg = angledist(      radecMagzen[0],radecMagzen[1], self.ra[cutrow, cutcol], self.dec[cutrow, cutcol])
        else: #meeus
            angledist_deg = angledist_meeus(radecMagzen[0],radecMagzen[1], self.ra[cutrow, cutcol], self.dec[cutrow, cutcol])
#%% assemble angular distances
        self.angle_deg,self.angleMagzenind = self.sky2beam(angledist_deg)

        self.cutrow = cutrow
        self.cutcol = cutcol
#%% meager self-check of result
        if self.arbfov is not None:
            expect_diffang = self.arbfov / self.ncutpix
            diffang = np.diff(self.angle_deg)
            diffoutlier = max(abs(expect_diffang-diffang.min()),
                              abs(expect_diffang-diffang.max()))
            assert_allclose(expect_diffang, diffang.mean(), rtol=0.01),'large bias in camera angle vector detected'
#            assert diffoutlier < expect_diffang,'large jump in camera angle vector detected' #TODO arbitrary

        if self.verbose:
            plotlsq_rc(nearrow,nearcol,cutrow,cutcol,
                       self.ra[cutrow, cutcol],
                       self.dec[cutrow,cutcol],
                       #angledist_deg,
                       self.angle_deg,
                       self.name,odir)
Beispiel #4
0
def findClosestAzel(az,el,azpts,elpts,discardEdgepix=True):
    """
    assumes that azpts, elpts are each list of 1-D arrays or 2-D arrays
    az: 2-D Numpy array of azimuths in the image
    el: 2-D Numpy array of elevations in the image
    azpts: 1-D or 2-D Numpy array of azimuth points to see where nearest neighbor index is
    elpts: 1-D or 2-D Numpy array of azimuth points to see where nearest neighbor index is
    """
    assert az.ndim     == 2
    assert az.shape    == el.shape
    assert azpts.shape == elpts.shape

    az = ma.masked_invalid(az)
    el = ma.masked_invalid(el)
    nearRow = []; nearCol=[]
    # can be FAR FAR faster than scipy.spatial.distance.cdist()
    for apts,epts in zip(azpts,elpts): #list of arrays or 2-D array
        apts = atleast_1d(apts); epts = atleast_1d(epts) # needed
        assert apts.size==epts.size
        r = empty(apts.size,dtype=int); c = empty(apts.size,dtype=int)
        for i,(apt,ept) in enumerate(zip(apts,epts)):
            #we do this point by point because we need to know the closest pixel for each point
            errang = angledist(az,el, apt,ept)

            """
            THIS UNRAVEL_INDEX MUST BE ORDER = 'C'
            """
            r[i], c[i] = unravel_index(errang.argmin(), az.shape,order='C')

        if discardEdgepix:
            mask = ((c==0) | (c == az.shape[1]-1) |
                    (r==0) | (r == az.shape[0]-1))
        else:
            mask = False

        nearRow.append(ma.array(r,mask=mask))
        nearCol.append(ma.array(c,mask=mask))

    return nearRow,nearCol
Beispiel #5
0
def test_haversine():
    assert_almost_equal(angledist(35,23,84,20),45.482789587392013)
    #%% compare with astropy
    assert_almost_equal(45.482789587392013,angledist_astropy(35,23, 84,20))
Beispiel #6
0
def test_haversine():
    assert_almost_equal(angledist(35, 23, 84, 20), ha)
    #%% compare with astropy
    assert_almost_equal(ha, angledist_meeus(35, 23, 84, 20))
Beispiel #7
0
def test_haversine():
    assert_almost_equal(angledist(35, 23, 84, 20), 45.482789587392013)
    #%% compare with astropy
    assert_almost_equal(45.482789587392013, angledist_meeus(35, 23, 84, 20))
Beispiel #8
0
#!/usr/bin/env python
from numpy.testing import assert_allclose
from pymap3d.haversine import angledist, angledist_meeus


if __name__ == '__main__':  # pragma: no cover
    from argparse import ArgumentParser
    p = ArgumentParser(description="computes angular distance between two points in sky")
    p.add_argument('r0', help='right ascension: first point [deg]', type=float)
    p.add_argument('d0', help='declination: first point [deg]', type=float)
    p.add_argument('r1', help='right ascension: 2nd point [deg]', type=float)
    p.add_argument('d1', help='declination: 2nd point [degrees]', type=float)
    a = p.parse_args()

    dist_deg = angledist_meeus(a.r0, a.d0, a.r1, a.d1)
    dist_deg_astropy = angledist(a.r0, a.d0, a.r1, a.d1)

    print('{:.6f} deg sep'.format(dist_deg))

    assert_allclose(dist_deg, dist_deg_astropy)