|
1 | | -import warnings |
2 | | - |
3 | 1 | import cartopy.crs as ccrs |
4 | 2 | import matplotlib.pyplot as plt |
5 | 3 | import numpy as np |
@@ -32,98 +30,6 @@ def sample_data_map(nlons, nlats): |
32 | 30 | return lon, lat, data |
33 | 31 |
|
34 | 32 |
|
35 | | -# ============================================================================= |
36 | | - |
37 | | - |
38 | | -# from xarray |
39 | | -def infer_interval_breaks(x, y, clip=False): |
40 | | - """find edges of gridcells, given their centers""" |
41 | | - |
42 | | - # TODO: require cartopy >= 0.21 when removing this function |
43 | | - warnings.warn( |
44 | | - "It's no longer necessary to compute the edges of the array. This is now done" |
45 | | - "in matplotlib. This function will be removed from mplotutils in a future " |
46 | | - "version.", |
47 | | - FutureWarning, |
48 | | - ) |
49 | | - |
50 | | - if len(x.shape) == 1: |
51 | | - x = _infer_interval_breaks(x) |
52 | | - y = _infer_interval_breaks(y) |
53 | | - else: |
54 | | - # we have to infer the intervals on both axes |
55 | | - x = _infer_interval_breaks(x, axis=1) |
56 | | - x = _infer_interval_breaks(x, axis=0) |
57 | | - y = _infer_interval_breaks(y, axis=1) |
58 | | - y = _infer_interval_breaks(y, axis=0) |
59 | | - |
60 | | - if clip: |
61 | | - y = np.clip(y, -90, 90) |
62 | | - |
63 | | - return x, y |
64 | | - |
65 | | - |
66 | | -# from xarray |
67 | | -def _infer_interval_breaks(coord, axis=0): |
68 | | - """ |
69 | | - >>> _infer_interval_breaks(np.arange(5)) |
70 | | - array([-0.5, 0.5, 1.5, 2.5, 3.5, 4.5]) |
71 | | - >>> _infer_interval_breaks([[0, 1], [3, 4]], axis=1) |
72 | | - array([[-0.5, 0.5, 1.5], |
73 | | - [ 2.5, 3.5, 4.5]]) |
74 | | - """ |
75 | | - |
76 | | - if not _is_monotonic(coord, axis=axis): |
77 | | - raise ValueError( |
78 | | - "The input coordinate is not sorted in increasing " |
79 | | - "order along axis %d. This can lead to unexpected " |
80 | | - "results. Consider calling the `sortby` method on " |
81 | | - "the input DataArray. To plot data with categorical " |
82 | | - "axes, consider using the `heatmap` function from " |
83 | | - "the `seaborn` statistical plotting library." % axis |
84 | | - ) |
85 | | - |
86 | | - coord = np.asarray(coord) |
87 | | - deltas = 0.5 * np.diff(coord, axis=axis) |
88 | | - if deltas.size == 0: |
89 | | - deltas = np.array(0.0) |
90 | | - first = np.take(coord, [0], axis=axis) - np.take(deltas, [0], axis=axis) |
91 | | - last = np.take(coord, [-1], axis=axis) + np.take(deltas, [-1], axis=axis) |
92 | | - trim_last = tuple( |
93 | | - slice(None, -1) if n == axis else slice(None) for n in range(coord.ndim) |
94 | | - ) |
95 | | - return np.concatenate([first, coord[trim_last] + deltas, last], axis=axis) |
96 | | - |
97 | | - |
98 | | -# from xarray |
99 | | -def _is_monotonic(coord, axis=0): |
100 | | - """ |
101 | | - >>> _is_monotonic(np.array([0, 1, 2])) |
102 | | - True |
103 | | - >>> _is_monotonic(np.array([2, 1, 0])) |
104 | | - True |
105 | | - >>> _is_monotonic(np.array([0, 2, 1])) |
106 | | - False |
107 | | - """ |
108 | | - coord = np.asarray(coord) |
109 | | - |
110 | | - if coord.shape[axis] < 3: |
111 | | - return True |
112 | | - else: |
113 | | - n = coord.shape[axis] |
114 | | - delta_pos = coord.take(np.arange(1, n), axis=axis) >= coord.take( |
115 | | - np.arange(0, n - 1), axis=axis |
116 | | - ) |
117 | | - delta_neg = coord.take(np.arange(1, n), axis=axis) <= coord.take( |
118 | | - np.arange(0, n - 1), axis=axis |
119 | | - ) |
120 | | - |
121 | | - return np.all(delta_pos) or np.all(delta_neg) |
122 | | - |
123 | | - |
124 | | -# ============================================================================= |
125 | | - |
126 | | - |
127 | 33 | def cyclic_dataarray(obj, coord="lon"): |
128 | 34 | """Add a cyclic coordinate point to a DataArray or Dataset along a dimension. |
129 | 35 |
|
|
0 commit comments