oafuncs 0.0.92__tar.gz → 0.0.94__tar.gz

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.
Files changed (36) hide show
  1. {oafuncs-0.0.92/oafuncs.egg-info → oafuncs-0.0.94}/PKG-INFO +1 -1
  2. oafuncs-0.0.94/oafuncs/oa_data.py +153 -0
  3. oafuncs-0.0.94/oafuncs/oa_down/hycom_3hourly.py +1300 -0
  4. oafuncs-0.0.92/oafuncs/oa_down/hycom_3hourly.py → oafuncs-0.0.94/oafuncs/oa_down/hycom_3hourly_20250129.py +80 -82
  5. {oafuncs-0.0.92 → oafuncs-0.0.94}/oafuncs/oa_down/literature.py +7 -7
  6. {oafuncs-0.0.92 → oafuncs-0.0.94}/oafuncs/oa_file.py +6 -2
  7. {oafuncs-0.0.92 → oafuncs-0.0.94}/oafuncs/oa_nc.py +46 -28
  8. {oafuncs-0.0.92 → oafuncs-0.0.94/oafuncs.egg-info}/PKG-INFO +1 -1
  9. {oafuncs-0.0.92 → oafuncs-0.0.94}/oafuncs.egg-info/SOURCES.txt +1 -0
  10. {oafuncs-0.0.92 → oafuncs-0.0.94}/setup.py +1 -1
  11. oafuncs-0.0.92/oafuncs/oa_data.py +0 -278
  12. {oafuncs-0.0.92 → oafuncs-0.0.94}/LICENSE.txt +0 -0
  13. {oafuncs-0.0.92 → oafuncs-0.0.94}/MANIFEST.in +0 -0
  14. {oafuncs-0.0.92 → oafuncs-0.0.94}/README.md +0 -0
  15. {oafuncs-0.0.92 → oafuncs-0.0.94}/oafuncs/__init__.py +0 -0
  16. {oafuncs-0.0.92 → oafuncs-0.0.94}/oafuncs/data_store/OAFuncs.png +0 -0
  17. {oafuncs-0.0.92 → oafuncs-0.0.94}/oafuncs/oa_cmap.py +0 -0
  18. {oafuncs-0.0.92 → oafuncs-0.0.94}/oafuncs/oa_down/User_Agent-list.txt +0 -0
  19. {oafuncs-0.0.92 → oafuncs-0.0.94}/oafuncs/oa_down/__init__.py +0 -0
  20. {oafuncs-0.0.92 → oafuncs-0.0.94}/oafuncs/oa_down/idm.py +0 -0
  21. {oafuncs-0.0.92 → oafuncs-0.0.94}/oafuncs/oa_down/test_ua.py +0 -0
  22. {oafuncs-0.0.92 → oafuncs-0.0.94}/oafuncs/oa_down/user_agent.py +0 -0
  23. {oafuncs-0.0.92 → oafuncs-0.0.94}/oafuncs/oa_draw.py +0 -0
  24. {oafuncs-0.0.92 → oafuncs-0.0.94}/oafuncs/oa_help.py +0 -0
  25. {oafuncs-0.0.92 → oafuncs-0.0.94}/oafuncs/oa_python.py +0 -0
  26. {oafuncs-0.0.92 → oafuncs-0.0.94}/oafuncs/oa_sign/__init__.py +0 -0
  27. {oafuncs-0.0.92 → oafuncs-0.0.94}/oafuncs/oa_sign/meteorological.py +0 -0
  28. {oafuncs-0.0.92 → oafuncs-0.0.94}/oafuncs/oa_sign/ocean.py +0 -0
  29. {oafuncs-0.0.92 → oafuncs-0.0.94}/oafuncs/oa_sign/scientific.py +0 -0
  30. {oafuncs-0.0.92 → oafuncs-0.0.94}/oafuncs/oa_tool/__init__.py +0 -0
  31. {oafuncs-0.0.92 → oafuncs-0.0.94}/oafuncs/oa_tool/email.py +0 -0
  32. {oafuncs-0.0.92 → oafuncs-0.0.94}/oafuncs/oa_tool/parallel.py +0 -0
  33. {oafuncs-0.0.92 → oafuncs-0.0.94}/oafuncs.egg-info/dependency_links.txt +0 -0
  34. {oafuncs-0.0.92 → oafuncs-0.0.94}/oafuncs.egg-info/requires.txt +0 -0
  35. {oafuncs-0.0.92 → oafuncs-0.0.94}/oafuncs.egg-info/top_level.txt +0 -0
  36. {oafuncs-0.0.92 → oafuncs-0.0.94}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: oafuncs
3
- Version: 0.0.92
3
+ Version: 0.0.94
4
4
  Summary: Oceanic and Atmospheric Functions
5
5
  Home-page: https://github.com/Industry-Pays/OAFuncs
6
6
  Author: Kun Liu
@@ -0,0 +1,153 @@
1
+ #!/usr/bin/env python
2
+ # coding=utf-8
3
+ """
4
+ Author: Liu Kun && 16031215@qq.com
5
+ Date: 2024-09-17 17:12:47
6
+ LastEditors: Liu Kun && 16031215@qq.com
7
+ LastEditTime: 2024-12-13 19:11:08
8
+ FilePath: \\Python\\My_Funcs\\OAFuncs\\oafuncs\\oa_data.py
9
+ Description:
10
+ EditPlatform: vscode
11
+ ComputerInfo: XPS 15 9510
12
+ SystemInfo: Windows 11
13
+ Python Version: 3.11
14
+ """
15
+
16
+ import itertools
17
+ import multiprocessing as mp
18
+ from concurrent.futures import ThreadPoolExecutor
19
+
20
+ import numpy as np
21
+ from scipy.interpolate import griddata
22
+
23
+
24
+ __all__ = ["interp_2d", "ensure_list"]
25
+
26
+
27
+ def ensure_list(input_data):
28
+ """
29
+ Ensures that the input is converted into a list.
30
+
31
+ If the input is already a list, it returns it directly.
32
+ If the input is a string, it wraps it in a list and returns.
33
+ For other types of input, it converts them to a string and then wraps in a list.
34
+
35
+ :param input_data: The input which can be a list, a string, or any other type.
36
+ :return: A list containing the input or the string representation of the input.
37
+ """
38
+ if isinstance(input_data, list):
39
+ return input_data
40
+ elif isinstance(input_data, str):
41
+ return [input_data]
42
+ else:
43
+ # For non-list and non-string inputs, convert to string and wrap in a list
44
+ return [str(input_data)]
45
+
46
+
47
+
48
+ def interp_2d(target_x, target_y, origin_x, origin_y, data, method="linear", parallel=True):
49
+ """
50
+ Perform 2D interpolation on the last two dimensions of a multi-dimensional array.
51
+
52
+ Parameters:
53
+ - target_x (array-like): 1D array of target grid's x-coordinates.
54
+ - target_y (array-like): 1D array of target grid's y-coordinates.
55
+ - origin_x (array-like): 1D array of original grid's x-coordinates.
56
+ - origin_y (array-like): 1D array of original grid's y-coordinates.
57
+ - data (numpy.ndarray): Multi-dimensional array where the last two dimensions correspond to the original grid.
58
+ - method (str, optional): Interpolation method, default is 'linear'. Other options include 'nearest', 'cubic', etc.
59
+ - parallel (bool, optional): Flag to enable parallel processing. Default is True.
60
+
61
+ Returns:
62
+ - interpolated_data (numpy.ndarray): Interpolated data with the same leading dimensions as the input data, but with the last two dimensions corresponding to the target grid.
63
+
64
+ Raises:
65
+ - ValueError: If the shape of the data does not match the shape of the origin_x or origin_y grids.
66
+
67
+ Usage:
68
+ - Interpolate a 2D array:
69
+ result = interp_2d(target_x, target_y, origin_x, origin_y, data_2d)
70
+ - Interpolate a 3D array (where the last two dimensions are spatial):
71
+ result = interp_2d(target_x, target_y, origin_x, origin_y, data_3d)
72
+ - Interpolate a 4D array (where the last two dimensions are spatial):
73
+ result = interp_2d(target_x, target_y, origin_x, origin_y, data_4d)
74
+ """
75
+
76
+ def interp_single(data_slice, target_points, origin_points, method):
77
+ return griddata(origin_points, data_slice.ravel(), target_points, method=method).reshape(target_y.shape)
78
+
79
+ # 确保目标网格和初始网格都是二维的
80
+ if len(target_y.shape) == 1:
81
+ target_x, target_y = np.meshgrid(target_x, target_y)
82
+ if len(origin_y.shape) == 1:
83
+ origin_x, origin_y = np.meshgrid(origin_x, origin_y)
84
+
85
+ # 根据经纬度网格判断输入数据的形状是否匹配
86
+ if origin_x.shape != data.shape[-2:] or origin_y.shape != data.shape[-2:]:
87
+ raise ValueError("Shape of data does not match shape of origin_x or origin_y.")
88
+
89
+ # 创建网格和展平数据
90
+ target_points = np.column_stack((target_y.ravel(), target_x.ravel()))
91
+ origin_points = np.column_stack((origin_y.ravel(), origin_x.ravel()))
92
+
93
+ # 根据是否并行选择不同的执行方式
94
+ if parallel:
95
+ with ThreadPoolExecutor(max_workers=mp.cpu_count() - 2) as executor:
96
+ if len(data.shape) == 2:
97
+ interpolated_data = list(executor.map(interp_single, [data], [target_points], [origin_points], [method]))
98
+ elif len(data.shape) == 3:
99
+ interpolated_data = list(executor.map(interp_single, [data[i] for i in range(data.shape[0])], [target_points] * data.shape[0], [origin_points] * data.shape[0], [method] * data.shape[0]))
100
+ elif len(data.shape) == 4:
101
+ index_combinations = list(itertools.product(range(data.shape[0]), range(data.shape[1])))
102
+ interpolated_data = list(executor.map(interp_single, [data[i, j] for i, j in index_combinations], [target_points] * len(index_combinations), [origin_points] * len(index_combinations), [method] * len(index_combinations)))
103
+ interpolated_data = np.array(interpolated_data).reshape(data.shape[0], data.shape[1], *target_y.shape)
104
+ else:
105
+ if len(data.shape) == 2:
106
+ interpolated_data = interp_single(data, target_points, origin_points, method)
107
+ elif len(data.shape) == 3:
108
+ interpolated_data = np.stack([interp_single(data[i], target_points, origin_points, method) for i in range(data.shape[0])])
109
+ elif len(data.shape) == 4:
110
+ interpolated_data = np.stack([np.stack([interp_single(data[i, j], target_points, origin_points, method) for j in range(data.shape[1])]) for i in range(data.shape[0])])
111
+
112
+ return np.array(interpolated_data)
113
+
114
+
115
+
116
+ if __name__ == "__main__":
117
+
118
+ pass
119
+ """ import time
120
+
121
+ import matplotlib.pyplot as plt
122
+
123
+ # 测试数据
124
+ origin_x = np.linspace(0, 10, 11)
125
+ origin_y = np.linspace(0, 10, 11)
126
+ target_x = np.linspace(0, 10, 101)
127
+ target_y = np.linspace(0, 10, 101)
128
+ data = np.random.rand(11, 11)
129
+
130
+ # 高维插值
131
+ origin_x = np.linspace(0, 10, 11)
132
+ origin_y = np.linspace(0, 10, 11)
133
+ target_x = np.linspace(0, 10, 101)
134
+ target_y = np.linspace(0, 10, 101)
135
+ data = np.random.rand(10, 10, 11, 11)
136
+
137
+ start = time.time()
138
+ interpolated_data = interp_2d(target_x, target_y, origin_x, origin_y, data, parallel=False)
139
+ print(f"Interpolation time: {time.time()-start:.2f}s")
140
+
141
+ print(interpolated_data.shape)
142
+
143
+ # 高维插值多线程
144
+ start = time.time()
145
+ interpolated_data = interp_2d(target_x, target_y, origin_x, origin_y, data)
146
+ print(f"Interpolation time: {time.time()-start:.2f}s")
147
+
148
+ print(interpolated_data.shape)
149
+ print(interpolated_data[0, 0, :, :].shape)
150
+ plt.figure()
151
+ plt.contourf(target_x, target_y, interpolated_data[0, 0, :, :])
152
+ plt.colorbar()
153
+ plt.show() """