oafuncs 0.0.98.38__py3-none-any.whl → 0.0.98.40__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
oafuncs/oa_cmap.py CHANGED
@@ -313,6 +313,7 @@ def random_color():
313
313
  b = random.randint(0, 255)
314
314
  return f"#{r:02x}{g:02x}{b:02x}"
315
315
 
316
+
316
317
  if __name__ == "__main__":
317
318
  # ** 测试自制cmap
318
319
  colors = ["#C2B7F3", "#B3BBF2", "#B0CBF1", "#ACDCF0", "#A8EEED"]
oafuncs/oa_draw.py CHANGED
@@ -6,7 +6,7 @@ import matplotlib.pyplot as plt
6
6
  import numpy as np
7
7
  from rich import print
8
8
 
9
- __all__ = ["fig_minus", "gif", "movie", "setup_map", "MidpointNormalize"]
9
+ __all__ = ["fig_minus", "gif", "movie", "setup_map", "ticks_symmetric"]
10
10
 
11
11
  warnings.filterwarnings("ignore")
12
12
 
@@ -247,44 +247,41 @@ def setup_map(
247
247
  from cartopy.mpl.ticker import LatitudeFormatter, LongitudeFormatter
248
248
  lon_formatter = LongitudeFormatter(zero_direction_label=False, number_format=f".{tick_decimals}f")
249
249
  lat_formatter = LatitudeFormatter(number_format=f".{tick_decimals}f")
250
-
251
- # Handle gridlines and ticks
252
- if show_gridlines:
253
- # Add gridlines with labels
254
- gl = axes.gridlines(crs=map_projection, draw_labels=show_labels, linewidth=grid_width, color=grid_color, alpha=grid_alpha, linestyle=grid_style)
255
-
256
- # Configure label positions
257
- gl.left_labels = left_labels
258
- gl.bottom_labels = bottom_labels
259
- gl.right_labels = right_labels
260
- gl.top_labels = top_labels
261
-
262
- # Set formatters
263
- gl.xformatter = lon_formatter
264
- gl.yformatter = lat_formatter
265
-
266
- # Set custom tick positions if provided
267
- if longitude_ticks is not None:
268
- gl.xlocator = mticker.FixedLocator(np.array(longitude_ticks))
269
- if latitude_ticks is not None:
270
- gl.ylocator = mticker.FixedLocator(np.array(latitude_ticks))
271
-
272
- elif show_labels:
250
+
251
+ # 只要传入经纬度数据就自动设置范围
252
+ # 范围必须在cartopy添加地图特征之后设置,因为添加特征可能会改变axes的范围
253
+ if longitude_data is not None and latitude_data is not None:
254
+ # 过滤掉NaN,避免极端值影响
255
+ lon_data = np.asarray(longitude_data)
256
+ lat_data = np.asarray(latitude_data)
257
+ lon_valid = lon_data[~np.isnan(lon_data)]
258
+ lat_valid = lat_data[~np.isnan(lat_data)]
259
+ if lon_valid.size > 0 and lat_valid.size > 0:
260
+ lon_min, lon_max = np.min(lon_valid), np.max(lon_valid)
261
+ lat_min, lat_max = np.min(lat_valid), np.max(lat_valid)
262
+ axes.set_extent([lon_min, lon_max, lat_min, lat_max], crs=map_projection)
263
+ else:
264
+ # 若全是NaN则不设置范围
265
+ pass
266
+
267
+ if show_labels:
273
268
  # Add tick labels without gridlines
274
269
  # Generate default tick positions based on current extent if not provided
275
270
  if longitude_ticks is None:
276
271
  current_extent = axes.get_extent(crs=map_projection)
277
272
  lon_range = current_extent[1] - current_extent[0]
278
273
  # Generate reasonable tick spacing
279
- tick_spacing = 1 if lon_range <= 10 else (5 if lon_range <= 30 else (10 if lon_range <= 90 else 20))
280
- longitude_ticks = np.arange(np.ceil(current_extent[0] / tick_spacing) * tick_spacing, current_extent[1] + tick_spacing, tick_spacing)
274
+ tick_spacing = 1 if lon_range <= 10 else (5 if lon_range <= 30 else (15 if lon_range <= 90 else (30 if lon_range <= 180 else 60)))
275
+ longitude_ticks = np.arange(np.ceil(current_extent[0] / tick_spacing) * tick_spacing, current_extent[1] + 0.1, tick_spacing)
276
+ # print(f"[green]Longitude ticks set to:[/green] {longitude_ticks}")
281
277
 
282
278
  if latitude_ticks is None:
283
279
  current_extent = axes.get_extent(crs=map_projection)
284
280
  lat_range = current_extent[3] - current_extent[2]
285
281
  # Generate reasonable tick spacing
286
- tick_spacing = 1 if lat_range <= 10 else (5 if lat_range <= 30 else (10 if lat_range <= 90 else 20))
287
- latitude_ticks = np.arange(np.ceil(current_extent[2] / tick_spacing) * tick_spacing, current_extent[3] + tick_spacing, tick_spacing)
282
+ tick_spacing = 1 if lat_range <= 10 else (5 if lat_range <= 30 else (15 if lat_range <= 90 else 30))
283
+ latitude_ticks = np.arange(np.ceil(current_extent[2] / tick_spacing) * tick_spacing, current_extent[3] + 0.1, tick_spacing)
284
+ # print(f"[green]Latitude ticks set to:[/green] {latitude_ticks}")
288
285
 
289
286
  # Set tick positions and formatters
290
287
  axes.set_xticks(longitude_ticks, crs=map_projection)
@@ -296,75 +293,71 @@ def setup_map(
296
293
  axes.tick_params(axis="x", labelbottom=bottom_labels, labeltop=top_labels)
297
294
  axes.tick_params(axis="y", labelleft=left_labels, labelright=right_labels)
298
295
 
299
- # 只要传入经纬度数据就自动设置范围
300
- # 范围必须在cartopy添加地图特征之后设置,因为添加特征可能会改变axes的范围
301
- if longitude_data is not None and latitude_data is not None:
302
- # 过滤掉NaN,避免极端值影响
303
- lon_data = np.asarray(longitude_data)
304
- lat_data = np.asarray(latitude_data)
305
- lon_valid = lon_data[~np.isnan(lon_data)]
306
- lat_valid = lat_data[~np.isnan(lat_data)]
307
- if lon_valid.size > 0 and lat_valid.size > 0:
308
- lon_min, lon_max = np.min(lon_valid), np.max(lon_valid)
309
- lat_min, lat_max = np.min(lat_valid), np.max(lat_valid)
310
- axes.set_extent([lon_min, lon_max, lat_min, lat_max], crs=map_projection)
311
- else:
312
- # 若全是NaN则不设置范围
313
- pass
314
- return axes
315
-
316
-
317
- class MidpointNormalize(mpl.colors.Normalize):
318
- """Custom normalization class to center a specific value.
319
-
320
- Args:
321
- vmin (float, optional): Minimum data value. Defaults to None.
322
- vmax (float, optional): Maximum data value. Defaults to None.
323
- vcenter (float, optional): Center value for normalization. Defaults to 0.
324
- clip (bool, optional): Whether to clip data outside the range. Defaults to False.
325
-
326
- Example:
327
- >>> norm = MidpointNormalize(vmin=-2, vmax=1, vcenter=0)
328
- """
296
+ # Handle gridlines and ticks
297
+ if show_gridlines:
298
+ # Add gridlines with labels
299
+ gl = axes.gridlines(crs=map_projection, draw_labels=show_labels, linewidth=grid_width, color=grid_color, alpha=grid_alpha, linestyle=grid_style)
329
300
 
330
- def __init__(self, vmin: float = None, vmax: float = None, vcenter: float = 0, clip: bool = False) -> None:
331
- self.vcenter = vcenter
332
- super().__init__(vmin, vmax, clip)
301
+ # Configure label positions
302
+ gl.left_labels = left_labels
303
+ gl.bottom_labels = bottom_labels
304
+ gl.right_labels = right_labels
305
+ gl.top_labels = top_labels
333
306
 
334
- def __call__(self, value: np.ndarray, clip: bool = None) -> np.ma.MaskedArray:
335
- # Use the clip parameter from initialization if not provided
336
- if clip is None:
337
- clip = self.clip
307
+ # Set formatters
308
+ gl.xformatter = lon_formatter
309
+ gl.yformatter = lat_formatter
338
310
 
339
- x, y = [self.vmin, self.vcenter, self.vmax], [0, 0.5, 1.0]
340
- result = np.interp(value, x, y)
311
+ # Set custom tick positions if provided
312
+ if longitude_ticks is not None:
313
+ gl.xlocator = mticker.FixedLocator(np.array(longitude_ticks))
314
+ if latitude_ticks is not None:
315
+ gl.ylocator = mticker.FixedLocator(np.array(latitude_ticks))
316
+
317
+ return axes
341
318
 
342
- # Apply clipping if requested
343
- if clip:
344
- result = np.clip(result, 0, 1)
345
319
 
346
- return np.ma.masked_array(result)
320
+ def ticks_symmetric(vmin: float, vcenter: float, vmax: float, num: int = 7) -> np.ndarray:
321
+ """
322
+ 生成以指定中心点对称分布的刻度值
347
323
 
348
- def ticks(self, num_ticks: int = 7) -> np.ndarray:
349
- """Generate ticks for the normalization range, centered around vcenter."""
350
- if self.vmin is None or self.vmax is None:
351
- raise ValueError("vmin and vmax must be set to generate ticks.")
352
-
353
- if num_ticks % 2 == 0:
354
- num_ticks += 1
355
-
356
- num_points_side = (num_ticks - 1) // 2 + 1
357
-
358
- negative_ticks = np.linspace(self.vmin, self.vcenter, num_points_side)[:-1]
359
- positive_ticks = np.linspace(self.vcenter, self.vmax, num_points_side)[1:]
360
-
361
- ticks = np.concatenate([negative_ticks, [self.vcenter], positive_ticks])
362
-
363
- return ticks
364
-
365
- def inverse(self, value: np.ndarray) -> np.ndarray:
366
- y, x = [self.vmin, self.vcenter, self.vmax], [0, 0.5, 1]
367
- return np.interp(value, x, y)
324
+ 参数:
325
+ vmin (float): 最小值
326
+ vcenter (float): 中心值
327
+ vmax (float): 最大值
328
+ num (int, optional): 期望的刻度数量(必须是奇数)。默认为7
329
+
330
+ 返回:
331
+ np.ndarray: 对称分布的刻度值数组
332
+
333
+ 异常:
334
+ ValueError: 如果输入值无效
335
+
336
+ 示例:
337
+ >>> ticks_symmetric(vmin=-10, vcenter=0, vmax=10, num=5)
338
+ array([-10., -5., 0., 5., 10.])
339
+ """
340
+ # 验证输入参数
341
+ if vmin >= vcenter:
342
+ raise ValueError(f"vmin ({vmin}) must be less than vcenter ({vcenter})")
343
+ if vcenter >= vmax:
344
+ raise ValueError(f"vcenter ({vcenter}) must be less than vmax ({vmax})")
345
+
346
+ # 确保刻度数量是奇数
347
+ if num % 2 == 0:
348
+ num += 1
349
+
350
+ # 计算每侧的点数(包括中心点)
351
+ side_points = (num - 1) // 2 + 1
352
+
353
+ # 生成左侧刻度(从最小值到中心值)
354
+ left_ticks = np.linspace(vmin, vcenter, side_points)[:-1]
355
+
356
+ # 生成右侧刻度(从中心值到最大值)
357
+ right_ticks = np.linspace(vcenter, vmax, side_points)[1:]
358
+
359
+ # 组合所有刻度
360
+ return np.concatenate([left_ticks, [vcenter], right_ticks])
368
361
 
369
362
 
370
363
  if __name__ == "__main__":
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: oafuncs
3
- Version: 0.0.98.38
3
+ Version: 0.0.98.40
4
4
  Summary: Oceanic and Atmospheric Functions
5
5
  Home-page: https://github.com/Industry-Pays/OAFuncs
6
6
  Author: Kun Liu
@@ -1,8 +1,8 @@
1
1
  oafuncs/__init__.py,sha256=T_-VtnWWllV3Q91twT5Yt2sUapeA051QbPNnBxmg9nw,1456
2
- oafuncs/oa_cmap.py,sha256=h9QrOSwbzZJXuS2pjFZkJtck1bq9itPY1natIuIRB3s,13884
2
+ oafuncs/oa_cmap.py,sha256=JwZMJ36uNwiCnzXqEtH2_PpeLtEaRaXP9YeGSl0PJSU,13886
3
3
  oafuncs/oa_data.py,sha256=u4H1ZazQf2jmGx3IAiaGxLjQHY9cQEMizRIaTQW4UiE,8075
4
4
  oafuncs/oa_date.py,sha256=aU2wVIWXyWoRiSQ9dg8sHvShFTxw86RrgbV3Q6tDjD4,6841
5
- oafuncs/oa_draw.py,sha256=UnUyOcr6KHtEWCZG0TCnHFW0jrjkAW9XWZoUDcSs6oI,14386
5
+ oafuncs/oa_draw.py,sha256=zal0Y3RPpN0TCGN4Gw9qLtjQdT6V0ZqpSUBFVOPL0x4,13952
6
6
  oafuncs/oa_file.py,sha256=fLb0gRhq2AiPl-5ASDHMrx6Z267FmhqNcTV7CdCxTdI,16934
7
7
  oafuncs/oa_help.py,sha256=0J5VaZX-cB0c090KxgmktQJBc0o00FsY-4wB8l5y00k,4178
8
8
  oafuncs/oa_nc.py,sha256=mKNxQ9jPxfRH7xINyrX7tBhitG5gmOKm6Dn7stk5mdw,15279
@@ -37,8 +37,8 @@ oafuncs/oa_sign/__init__.py,sha256=JSx1fcWpmNhQBvX_Bmq3xysfSkkFMrjbJASxV_V6aqE,1
37
37
  oafuncs/oa_sign/meteorological.py,sha256=3MSjy7HTcvz2zsITkjUMr_0Y027Gas1LFE9pk99990k,6110
38
38
  oafuncs/oa_sign/ocean.py,sha256=3uYEzaq-27yVy23IQoqy-clhWu1I_fhPFBAQyT-OF4M,5562
39
39
  oafuncs/oa_sign/scientific.py,sha256=moIl2MEY4uitbXoD596JmXookXGQtQsS-8_1NBBTx84,4689
40
- oafuncs-0.0.98.38.dist-info/licenses/LICENSE.txt,sha256=rMtLpVg8sKiSlwClfR9w_Dd_5WubTQgoOzE2PDFxzs4,1074
41
- oafuncs-0.0.98.38.dist-info/METADATA,sha256=6dL6uaNclxdpIf5Hf5D_j_SaF_YK3sF4pvy673yiUBs,4326
42
- oafuncs-0.0.98.38.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
43
- oafuncs-0.0.98.38.dist-info/top_level.txt,sha256=bgC35QkXbN4EmPHEveg_xGIZ5i9NNPYWqtJqaKqTPsQ,8
44
- oafuncs-0.0.98.38.dist-info/RECORD,,
40
+ oafuncs-0.0.98.40.dist-info/licenses/LICENSE.txt,sha256=rMtLpVg8sKiSlwClfR9w_Dd_5WubTQgoOzE2PDFxzs4,1074
41
+ oafuncs-0.0.98.40.dist-info/METADATA,sha256=oD8heaDgtaCRU2T3HJZK1UXU7hnasx6dVk9WtC9TwQ4,4326
42
+ oafuncs-0.0.98.40.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
43
+ oafuncs-0.0.98.40.dist-info/top_level.txt,sha256=bgC35QkXbN4EmPHEveg_xGIZ5i9NNPYWqtJqaKqTPsQ,8
44
+ oafuncs-0.0.98.40.dist-info/RECORD,,