/usr/lib/python2.7/dist-packages/cartopy/tests/test_crs_transform_vectors.py is in python-cartopy 0.14.2+dfsg1-2build3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | # (C) British Crown Copyright 2013 - 2016, Met Office
#
# This file is part of cartopy.
#
# cartopy is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cartopy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with cartopy. If not, see <https://www.gnu.org/licenses/>.
from __future__ import (absolute_import, division, print_function)
import unittest
import warnings
import numpy as np
from numpy.testing import assert_array_almost_equal
import cartopy.crs as ccrs
class TestTransformVectors(unittest.TestCase):
def test_transform(self):
# Test some simple vectors to make sure they are transformed
# correctly.
rlons = np.array([-90., 0, 90., 180.])
rlats = np.array([0., 0., 0., 0.])
src_proj = ccrs.PlateCarree()
target_proj = ccrs.Stereographic(central_latitude=90,
central_longitude=0)
# transform grid eastward vectors
ut, vt = target_proj.transform_vectors(src_proj,
rlons,
rlats,
np.ones([4]),
np.zeros([4]))
assert_array_almost_equal(ut, np.array([0, 1, 0, -1]), decimal=2)
assert_array_almost_equal(vt, np.array([-1, 0, 1, 0]), decimal=2)
# transform grid northward vectors
ut, vt = target_proj.transform_vectors(src_proj,
rlons,
rlats,
np.zeros([4]),
np.ones([4]))
assert_array_almost_equal(ut, np.array([1, 0, -1, 0]), decimal=2)
assert_array_almost_equal(vt, np.array([0, 1, 0, -1]), decimal=2)
# transform grid north-eastward vectors
ut, vt = target_proj.transform_vectors(src_proj,
rlons,
rlats,
np.ones([4]),
np.ones([4]))
assert_array_almost_equal(ut, np.array([1, 1, -1, -1]), decimal=2)
assert_array_almost_equal(vt, np.array([-1, 1, 1, -1]), decimal=2)
def test_transform_and_inverse(self):
# Check a full circle transform back to the native projection.
x = np.arange(-60, 42.5, 2.5)
y = np.arange(30, 72.5, 2.5)
x2d, y2d = np.meshgrid(x, y)
u = np.cos(np.deg2rad(y2d))
v = np.cos(2. * np.deg2rad(x2d))
src_proj = ccrs.PlateCarree()
target_proj = ccrs.Stereographic(central_latitude=90,
central_longitude=0)
proj_xyz = target_proj.transform_points(src_proj, x2d, y2d)
xt, yt = proj_xyz[..., 0], proj_xyz[..., 1]
ut, vt = target_proj.transform_vectors(src_proj, x2d, y2d, u, v)
utt, vtt = src_proj.transform_vectors(target_proj, xt, yt, ut, vt)
assert_array_almost_equal(u, utt, decimal=4)
assert_array_almost_equal(v, vtt, decimal=4)
def test_invalid_input_domain(self):
# If an input coordinate is outside the input projection domain
# we should be able to handle it correctly.
rlon = np.array([270.])
rlat = np.array([0.])
u = np.array([1.])
v = np.array([0.])
src_proj = ccrs.PlateCarree()
target_proj = ccrs.Stereographic(central_latitude=90,
central_longitude=0)
ut, vt = target_proj.transform_vectors(src_proj, rlon, rlat, u, v)
assert_array_almost_equal(ut, np.array([0]), decimal=2)
assert_array_almost_equal(vt, np.array([-1]), decimal=2)
def test_invalid_x_domain(self):
# If the point we need to calculate the vector angle falls outside the
# source projection x-domain it should be handled correctly as long as
# it is not a corner point.
rlon = np.array([180.])
rlat = np.array([0.])
u = np.array([1.])
v = np.array([0.])
src_proj = ccrs.PlateCarree()
target_proj = ccrs.Stereographic(central_latitude=90,
central_longitude=0)
ut, vt = target_proj.transform_vectors(src_proj, rlon, rlat, u, v)
assert_array_almost_equal(ut, np.array([-1]), decimal=2)
assert_array_almost_equal(vt, np.array([0.]), decimal=2)
def test_invalid_y_domain(self):
# If the point we need to calculate the vector angle falls outside the
# source projection y-domain it should be handled correctly as long as
# it is not a corner point.
rlon = np.array([0.])
rlat = np.array([90.])
u = np.array([0.])
v = np.array([1.])
src_proj = ccrs.PlateCarree()
target_proj = ccrs.Stereographic(central_latitude=90,
central_longitude=0)
ut, vt = target_proj.transform_vectors(src_proj, rlon, rlat, u, v)
assert_array_almost_equal(ut, np.array([0.]), decimal=2)
assert_array_almost_equal(vt, np.array([1.]), decimal=2)
def test_invalid_xy_domain_corner(self):
# If the point we need to calculate the vector angle falls outside the
# source projection x and y-domain it should be handled correctly.
rlon = np.array([180.])
rlat = np.array([90.])
u = np.array([1.])
v = np.array([1.])
src_proj = ccrs.PlateCarree()
target_proj = ccrs.Stereographic(central_latitude=90,
central_longitude=0)
ut, vt = target_proj.transform_vectors(src_proj, rlon, rlat, u, v)
assert_array_almost_equal(ut, np.array([0.]), decimal=2)
assert_array_almost_equal(vt, np.array([-2**.5]), decimal=2)
def test_invalid_x_domain_corner(self):
# If the point we need to calculate the vector angle falls outside the
# source projection x-domain and is a corner point, it may be handled
# incorrectly and a warning should be raised.
rlon = np.array([180.])
rlat = np.array([90.])
u = np.array([1.])
v = np.array([-1.])
src_proj = ccrs.PlateCarree()
target_proj = ccrs.Stereographic(central_latitude=90,
central_longitude=0)
with warnings.catch_warnings():
warnings.simplefilter('error')
with self.assertRaises(UserWarning):
ut, vt = target_proj.transform_vectors(
src_proj, rlon, rlat, u, v)
def test_invalid_y_domain_corner(self):
# If the point we need to calculate the vector angle falls outside the
# source projection y-domain and is a corner point, it may be handled
# incorrectly and a warning should be raised.
rlon = np.array([180.])
rlat = np.array([90.])
u = np.array([-1.])
v = np.array([1.])
src_proj = ccrs.PlateCarree()
target_proj = ccrs.Stereographic(central_latitude=90,
central_longitude=0)
with warnings.catch_warnings():
warnings.simplefilter('error')
with self.assertRaises(UserWarning):
ut, vt = target_proj.transform_vectors(
src_proj, rlon, rlat, u, v)
|