oafuncs 0.0.97.13__py3-none-any.whl → 0.0.97.15__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_draw.py CHANGED
@@ -13,7 +13,6 @@ SystemInfo: Windows 11
13
13
  Python Version: 3.11
14
14
  """
15
15
 
16
-
17
16
  import warnings
18
17
 
19
18
  import cartopy.crs as ccrs
@@ -21,16 +20,15 @@ import cartopy.feature as cfeature
21
20
  import matplotlib as mpl
22
21
  import matplotlib.pyplot as plt
23
22
  import numpy as np
24
- import xarray as xr
25
23
  from cartopy.mpl.ticker import LatitudeFormatter, LongitudeFormatter
26
24
  from rich import print
27
25
 
28
- __all__ = ["fig_minus", "gif", "add_cartopy", "add_gridlines", "MidpointNormalize", "add_lonlat_unit", "contour", "contourf", "quiver"]
26
+ __all__ = ["fig_minus", "gif", "add_cartopy", "add_gridlines", "MidpointNormalize", "add_lonlat_unit"]
29
27
 
30
28
  warnings.filterwarnings("ignore")
31
29
 
32
30
 
33
- def fig_minus(ax_x=None, ax_y=None, cbar=None, decimal=None, add_space=False):
31
+ def fig_minus(ax_x: plt.Axes = None, ax_y: plt.Axes = None, cbar: mpl.colorbar.Colorbar = None, decimal: int = None, add_space: bool = False) -> plt.Axes | mpl.colorbar.Colorbar | None:
34
32
  """
35
33
  Description: 将坐标轴刻度中的负号替换为减号
36
34
 
@@ -76,7 +74,7 @@ def fig_minus(ax_x=None, ax_y=None, cbar=None, decimal=None, add_space=False):
76
74
 
77
75
 
78
76
  # ** 将生成图片/已有图片制作成动图
79
- def gif(image_list: list, gif_name: str, duration=200, resize=None): # 制作动图,默认间隔0.2
77
+ def gif(image_list: list[str], gif_name: str, duration: float = 200, resize: tuple[int, int] = None) -> None: # 制作动图,默认间隔0.2
80
78
  """
81
79
  Description
82
80
  Make gif from images
@@ -125,7 +123,7 @@ def gif(image_list: list, gif_name: str, duration=200, resize=None): # 制作
125
123
 
126
124
 
127
125
  # ** 转化经/纬度刻度
128
- def add_lonlat_unit(lon=None, lat=None, decimal=2):
126
+ def add_lonlat_unit(lon: list[float] = None, lat: list[float] = None, decimal: int = 2) -> tuple[list[str], list[str]] | list[str]:
129
127
  """
130
128
  param {*} lon : 经度列表
131
129
  param {*} lat : 纬度列表
@@ -165,15 +163,16 @@ def add_lonlat_unit(lon=None, lat=None, decimal=2):
165
163
 
166
164
 
167
165
  # ** 添加网格线
168
- def add_gridlines(ax, xline=None, yline=None, projection=ccrs.PlateCarree(), color="k", alpha=0.5, linestyle="--", linewidth=0.5):
166
+ def add_gridlines(ax: plt.Axes, xline: list[float] = None, yline: list[float] = None, projection: ccrs.Projection = ccrs.PlateCarree(), color: str = "k", alpha: float = 0.5, linestyle: str = "--", linewidth: float = 0.5) -> tuple[plt.Axes, mpl.ticker.Locator]:
169
167
  from matplotlib import ticker as mticker
168
+
170
169
  # add gridlines
171
170
  gl = ax.gridlines(crs=projection, draw_labels=True, linewidth=linewidth, color=color, alpha=alpha, linestyle=linestyle)
172
171
  gl.right_labels = False
173
172
  gl.top_labels = False
174
173
  gl.xformatter = LongitudeFormatter(zero_direction_label=False)
175
174
  gl.yformatter = LatitudeFormatter()
176
-
175
+
177
176
  if xline is not None:
178
177
  gl.xlocator = mticker.FixedLocator(np.array(xline))
179
178
  if yline is not None:
@@ -183,7 +182,7 @@ def add_gridlines(ax, xline=None, yline=None, projection=ccrs.PlateCarree(), col
183
182
 
184
183
 
185
184
  # ** 添加地图
186
- def add_cartopy(ax, lon=None, lat=None, projection=ccrs.PlateCarree(), gridlines=True, landcolor="lightgrey", oceancolor="lightblue", cartopy_linewidth=0.5):
185
+ def add_cartopy(ax: plt.Axes, lon: np.ndarray = None, lat: np.ndarray = None, projection: ccrs.Projection = ccrs.PlateCarree(), gridlines: bool = True, landcolor: str = "lightgrey", oceancolor: str = "lightblue", cartopy_linewidth: float = 0.5) -> None:
187
186
  # add coastlines
188
187
  ax.add_feature(cfeature.LAND, facecolor=landcolor)
189
188
  ax.add_feature(cfeature.OCEAN, facecolor=oceancolor)
@@ -219,139 +218,19 @@ class MidpointNormalize(mpl.colors.Normalize):
219
218
  nrom = MidpointNormalize(vmin=-2, vmax=1, vcenter=0)
220
219
  """
221
220
 
222
- def __init__(self, vmin=None, vmax=None, vcenter=None, clip=False):
221
+ def __init__(self, vmin: float = None, vmax: float = None, vcenter: float = None, clip: bool = False) -> None:
223
222
  self.vcenter = vcenter
224
223
  super().__init__(vmin, vmax, clip)
225
224
 
226
- def __call__(self, value, clip=None):
225
+ def __call__(self, value: np.ndarray, clip: bool = None) -> np.ma.MaskedArray:
227
226
  x, y = [self.vmin, self.vcenter, self.vmax], [0, 0.5, 1.0]
228
227
  return np.ma.masked_array(np.interp(value, x, y, left=-np.inf, right=np.inf))
229
228
 
230
- def inverse(self, value):
229
+ def inverse(self, value: np.ndarray) -> np.ndarray:
231
230
  y, x = [self.vmin, self.vcenter, self.vmax], [0, 0.5, 1]
232
231
  return np.interp(value, x, y, left=-np.inf, right=np.inf)
233
232
 
234
233
 
235
- # -----------------------------------------------------------------------------------------------------------------------------------------------------------------
236
-
237
- # ** 绘制填色图
238
- def contourf(data,x=None,y=None,cmap='coolwarm',show=True,store=None,cartopy=False):
239
- """
240
- Description: 绘制填色图
241
-
242
- param {*} data : 二维数据
243
- param {*} x : x轴坐标
244
- param {*} y : y轴坐标
245
- param {*} cmap : 颜色映射
246
- param {*} show : 是否显示
247
- param {*} store : 是否保存
248
- param {*} cartopy : 是否使用cartopy
249
-
250
- return {*}
251
- """
252
- data = np.array(data)
253
- if x is None or y is None:
254
- x = np.arange(data.shape[1])
255
- y = np.arange(data.shape[0])
256
- if cartopy:
257
- fig, ax = plt.subplots(subplot_kw={'projection': ccrs.PlateCarree()})
258
- add_cartopy(ax, lon=x, lat=y)
259
- ax.contourf(x, y, data, transform=ccrs.PlateCarree(), cmap=cmap)
260
- else:
261
- plt.contourf(x, y, data, cmap=cmap)
262
- plt.colorbar()
263
- plt.savefig(store, dpi=600, bbox_inches="tight") if store else plt.show()
264
- plt.close()
265
-
266
-
267
- # ** 绘制等值线图
268
- def contour(data, x=None, y=None, cmap="coolwarm", show=True, store=None, cartopy=False):
269
- """
270
- Description: 绘制等值线图
271
-
272
- param {*} data : 二维数据
273
- param {*} x : x轴坐标
274
- param {*} y : y轴坐标
275
- param {*} cmap : 颜色映射
276
- param {*} show : 是否显示
277
- param {*} store : 是否保存
278
- param {*} cartopy : 是否使用cartopy
279
-
280
- return {*}
281
- """
282
- data = np.array(data)
283
- if x is None or y is None:
284
- x = np.arange(data.shape[1])
285
- y = np.arange(data.shape[0])
286
- if cartopy:
287
- fig, ax = plt.subplots(subplot_kw={"projection": ccrs.PlateCarree()})
288
- add_cartopy(ax, lon=x, lat=y)
289
- cr = ax.contour(x, y, data, transform=ccrs.PlateCarree(), cmap=cmap)
290
- else:
291
- cr = plt.contour(x, y, data, cmap=cmap)
292
- plt.clabel(cr, inline=True, fontsize=10)
293
- plt.savefig(store, dpi=600, bbox_inches="tight") if store else plt.show()
294
- plt.close()
295
-
296
-
297
- # ** 绘制矢量场
298
- def quiver(u, v, lon, lat, picname=None, cmap="coolwarm", scale=0.25, width=0.002, x_space=5, y_space=5):
299
- """
300
- param {*} u : 二维数据
301
- param {*} v : 二维数据
302
- param {*} lon : 经度, 1D or 2D
303
- param {*} lat : 纬度, 1D or 2D
304
- param {*} picname : 图片保存的文件名(含路径)
305
- param {*} cmap : 颜色映射,默认coolwarm
306
- param {*} scale : 箭头的大小 / 缩小程度
307
- param {*} width : 箭头的宽度
308
- param {*} x_space : x轴间隔
309
- param {*} y_space : y轴间隔
310
- return {*} 无返回值
311
- """
312
- # 创建新的网格位置变量(lat_c, lon_c)
313
- if len(lon.shape) == 1 and len(lat.shape) == 1:
314
- lon_c, lat_c = np.meshgrid(lon, lat)
315
- else:
316
- lon_c, lat_c = lon, lat
317
-
318
- # 设置箭头的比例、颜色、宽度等参数
319
- # scale = 0.25 # 箭头的大小 / 缩小程度
320
- # color = '#E5D1FA'
321
- # width = 0.002 # 箭头的宽度
322
- # x_space = 1
323
- # y_space = 1
324
-
325
- # 计算矢量的大小
326
- S = xr.DataArray(np.hypot(np.array(u), np.array(v)))
327
-
328
- mean_S = S.nanmean()
329
-
330
- # 使用 plt.quiver 函数绘制矢量图
331
- # 通过设置 quiver 函数的 pivot 参数来指定箭头的位置
332
- quiver_plot = plt.quiver(
333
- lon_c[::y_space, ::x_space],
334
- lat_c[::y_space, ::x_space],
335
- u[::y_space, ::x_space],
336
- v[::y_space, ::x_space],
337
- S[::y_space, ::x_space], # 矢量的大小,可以不要
338
- pivot="middle",
339
- scale=scale,
340
- # color=color, # 矢量的颜色,单色
341
- cmap=cmap, # 矢量的颜色,多色
342
- width=width,
343
- )
344
- # plt.quiverkey(quiver_plot, X=0.90, Y=0.975, U=1, label='1 m/s', labelpos='E', fontproperties={'size': 10})
345
- plt.quiverkey(quiver_plot, X=0.87, Y=0.975, U=mean_S, label=f"{mean_S:.2f} m/s", labelpos="E", fontproperties={"size": 10})
346
- plt.colorbar(quiver_plot)
347
- plt.xlabel("X")
348
- plt.ylabel("Y")
349
-
350
- plt.savefig(picname, bbox_inches="tight") if picname is not None else plt.show()
351
- plt.clf()
352
- plt.close()
353
-
354
-
355
234
 
356
235
  if __name__ == "__main__":
357
236
  pass
oafuncs/oa_file.py CHANGED
@@ -21,7 +21,7 @@ import shutil
21
21
 
22
22
  from rich import print
23
23
 
24
- __all__ = ["find_file", "link_file", "copy_file", "rename_file", "make_folder", "clear_folder", "remove_empty_folder", "remove", "file_size", "mean_size", "make_dir", "replace_content", "move_file"]
24
+ __all__ = ["find_file", "link_file", "copy_file", "rename_file", "move_file", "clear_folder", "remove_empty_folder", "remove", "file_size", "mean_size", "make_dir", "replace_content"]
25
25
 
26
26
 
27
27
  # ** 查找文件,支持通配符
@@ -157,7 +157,6 @@ def copy_file(src_pattern, dst):
157
157
  print(f"Copy and rename file or directory: {src_file} -> {dst_file}")
158
158
 
159
159
 
160
-
161
160
  # ** 移动文件或目录,支持通配符
162
161
  def move_file(src_pattern, dst):
163
162
  """
@@ -205,7 +204,6 @@ def move_file(src_pattern, dst):
205
204
  print(f"Move and rename file or directory: {src_file} -> {dst_file}")
206
205
 
207
206
 
208
-
209
207
  # ** 重命名文件,支持通配符
210
208
  def rename_file(directory, old_str, new_str):
211
209
  """
@@ -243,26 +241,6 @@ def rename_file(directory, old_str, new_str):
243
241
  print(f"Rename file: {old_path} -> {new_path}")
244
242
 
245
243
 
246
- # ** 创建子文件夹(可选清空)
247
- def make_folder(rootpath=None, folder_name=None, clear=False) -> str:
248
- """
249
- # 描述:创建子文件夹(可选清空)
250
- # 使用示例
251
- rootpath = r'E:\\Data\\2024\\09\\17'
252
- folder_name = 'var1'
253
- newpath = make_folder(rootpath, folder_name, clear=1)
254
- param {*} rootpath # 根目录
255
- param {*} folder_name # 文件夹名称
256
- """
257
- if rootpath is None:
258
- rootpath = os.getcwd()
259
- folder_path = os.path.join(str(rootpath), str(folder_name))
260
- if clear:
261
- shutil.rmtree(folder_path, ignore_errors=True)
262
- os.makedirs(folder_path, exist_ok=True)
263
- return folder_path
264
-
265
-
266
244
  # ** 创建路径
267
245
  def make_dir(directory):
268
246
  """