oafuncs 0.0.80__py2.py3-none-any.whl → 0.0.82__py2.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.
Files changed (41) hide show
  1. oafuncs/__init__.py +27 -12
  2. oafuncs/oa_cmap.py +31 -52
  3. oafuncs/oa_data.py +107 -28
  4. oafuncs/oa_down/hycom_3hourly.py +151 -34
  5. oafuncs/oa_draw.py +148 -96
  6. oafuncs/oa_file.py +61 -50
  7. oafuncs/oa_nc.py +131 -53
  8. {oafuncs-0.0.80.dist-info → oafuncs-0.0.82.dist-info}/METADATA +1 -2
  9. oafuncs-0.0.82.dist-info/RECORD +24 -0
  10. oafuncs-0.0.82.dist-info/top_level.txt +1 -0
  11. oafuncs/oa_down/test.py +0 -151
  12. oafuncs/oa_s/__init__.py +0 -23
  13. oafuncs/oa_s/oa_cmap.py +0 -163
  14. oafuncs/oa_s/oa_data.py +0 -187
  15. oafuncs/oa_s/oa_draw.py +0 -451
  16. oafuncs/oa_s/oa_file.py +0 -332
  17. oafuncs/oa_s/oa_help.py +0 -39
  18. oafuncs/oa_s/oa_nc.py +0 -410
  19. oafuncs/oa_s/oa_python.py +0 -107
  20. oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/__init__.py" +0 -26
  21. oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_cmap.py" +0 -163
  22. oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_data.py" +0 -187
  23. oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_down/__init__.py" +0 -20
  24. oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_down/hycom_3hourly.py" +0 -1176
  25. oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_down/literature.py" +0 -332
  26. oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_down/test_ua.py" +0 -151
  27. oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_draw.py" +0 -451
  28. oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_file.py" +0 -332
  29. oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_help.py" +0 -39
  30. oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_nc.py" +0 -410
  31. oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_python.py" +0 -107
  32. oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_sign/__init__.py" +0 -21
  33. oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_sign/meteorological.py" +0 -168
  34. oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_sign/ocean.py" +0 -158
  35. oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_sign/scientific.py" +0 -139
  36. oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_tool/__init__.py" +0 -18
  37. oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_tool/email.py" +0 -114
  38. oafuncs-0.0.80.dist-info/RECORD +0 -51
  39. oafuncs-0.0.80.dist-info/top_level.txt +0 -2
  40. {oafuncs-0.0.80.dist-info → oafuncs-0.0.82.dist-info}/LICENSE.txt +0 -0
  41. {oafuncs-0.0.80.dist-info → oafuncs-0.0.82.dist-info}/WHEEL +0 -0
oafuncs/oa_down/test.py DELETED
@@ -1,151 +0,0 @@
1
- #!/usr/bin/env python
2
- # coding=utf-8
3
- """
4
- Author: Liu Kun && 16031215@qq.com
5
- Date: 2024-12-01 19:32:25
6
- LastEditors: Liu Kun && 16031215@qq.com
7
- LastEditTime: 2024-12-10 11:16:36
8
- FilePath: \\Python\\My_Funcs\\OAFuncs\\oafuncs\\oa_down\\test.py
9
- Description:
10
- EditPlatform: vscode
11
- ComputerInfo: XPS 15 9510
12
- SystemInfo: Windows 11
13
- Python Version: 3.12
14
- """
15
-
16
- import os
17
- import random
18
- import re
19
-
20
-
21
- def is_valid_user_agent(user_agent):
22
- # 简单的正则表达式来检查User Agent的格式
23
- # 这个正则表达式检查User Agent是否包含常见的浏览器信息格式
24
- pattern = re.compile(
25
- r"^(?:(?:Mozilla|Opera|Chrome|Safari|Edg|OPR)/[\d.]+)"
26
- r"(?:\s(?:\(.*?\)))?"
27
- r"(?:\s(?:Gecko|AppleWebKit|KHTML, like Gecko|Version|Edge|OPR)/[\d.]+)?"
28
- r"(?:\s.*?(?:rv:|Version/|Ubuntu|Macintosh|Windows|X11|Linux|CrOS|FreeBSD|OpenBSD|NetBSD|iPhone|iPad|iPod|Android|BlackBerry|BB10|Mobile|Symbian|Windows Phone|IEMobile|Opera Mini|Opera Mobi|UCBrowser|MQQBrowser|baiduboxapp|baidubrowser|Safari|Firefox|MSIE|Trident|Edge|EdgA|Chrome|CriOS|Vivaldi|Sleipnir|Midori|ELinks|Lynx|w3m|Arora|Epiphany|Konqueror|Dillo|Netscape|SeaMonkey|K-Meleon|Camino|Iceape|Galeon|GranParadiso|Iceweasel|Firefox|Fennec|Conkeror|PaleMoon|Uzbl|QupZilla|Otter|Waterfox|Basilisk|Cyberfox|PaleMoon|GNU IceCat|GNU IceWeasel|IceCat|IceWeasel|Seamonkey|Iceape|Firefox|Epiphany|Web|Safari|Android|Mobile|BlackBerry|BB10|Tablet|Silk|Kindle|FxiOS|Focus|SamsungBrowser|browser|AppleWebKit|Puffin|DuckDuckGo|YaBrowser|Yandex|Amigo|NokiaBrowser|OviBrowser|OneBrowser|Chrome|Firefox|Safari|OPR|Coast|Mercury|Silk|Skyfire|IEMobile|Bolt|Jasmine|NativeHost|Crosswalk|TizenBrowser|SailfishBrowser|SamsungBrowser|Silk-Accelerated|UCBrowser|Quark|XiaoMi|OnePlus|Vivo|Oppo|Realme|Meizu|Lenovo|Huawei|ZTE|Alcatel|Sony|Nokia|LG|HTC|Asus|Acer|Motorola|Samsung)/[\d.]+)?$"
29
- )
30
-
31
- # 使用正则表达式匹配User Agent字符串
32
- if pattern.match(user_agent):
33
- return True
34
- else:
35
- return False
36
-
37
-
38
- def get_ua():
39
- current_dir = os.path.dirname(os.path.abspath(__file__))
40
- ua_file_txt = os.path.join(current_dir, "User_Agent-list.txt")
41
-
42
- with open(ua_file_txt, "r") as f:
43
- ua_list = f.readlines()
44
- # 去掉换行符和空行
45
- ua_list = [line.strip() for line in ua_list if line.strip()]
46
-
47
- return random.choice(ua_list)
48
-
49
-
50
- def get_ua_org():
51
- ua_list = [
52
- "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 OPR/26.0.1656.60",
53
- "Opera/8.0 (Windows NT 5.1; U; en)",
54
- "Mozilla/5.0 (Windows NT 5.1; U; en; rv:1.8.1) Gecko/20061208 Firefox/2.0.0 Opera 9.50",
55
- "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 9.50",
56
- "Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11",
57
- "Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11",
58
- "Opera/9.80 (Android 2.3.4; Linux; Opera Mobi/build-1107180945; U; en-GB) Presto/2.8.149 Version/11.10",
59
- "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0",
60
- "Mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv:1.9.2.10) Gecko/20100922 Ubuntu/10.10 (maverick) Firefox/3.6.10",
61
- "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv,2.0.1) Gecko/20100101 Firefox/4.0.1",
62
- "Mozilla/5.0 (Windows NT 6.1; rv,2.0.1) Gecko/20100101 Firefox/4.0.1",
63
- "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2",
64
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36",
65
- "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
66
- "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5",
67
- "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5",
68
- "Mozilla/5.0 (iPad; U; CPU OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5",
69
- "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36",
70
- "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11",
71
- "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.133 Safari/534.16",
72
- "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
73
- "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36",
74
- "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko",
75
- "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)",
76
- "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.11 TaoBrowser/2.0 Safari/536.11",
77
- "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71 Safari/537.1 LBBROWSER",
78
- "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; LBBROWSER)",
79
- "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E; LBBROWSER)",
80
- "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; QQBrowser/7.0.3698.400)",
81
- "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E)",
82
- "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.84 Safari/535.11 SE 2.X MetaSr 1.0",
83
- "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SV1; QQDownload 732; .NET4.0C; .NET4.0E; SE 2.X MetaSr 1.0)",
84
- "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SE 2.X MetaSr 1.0; SE 2.X MetaSr 1.0; .NET CLR 2.0.50727; SE 2.X MetaSr 1.0)",
85
- "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Maxthon/4.4.3.4000 Chrome/30.0.1599.101 Safari/537.36",
86
- "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
87
- "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 UBrowser/4.0.3214.0 Safari/537.36",
88
- "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 UBrowser/6.2.4094.1 Safari/537.36",
89
- "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5",
90
- "Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5",
91
- "Mozilla/5.0 (iPad; U; CPU OS 4_2_1 like Mac OS X; zh-cn) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 Safari/6533.18.5",
92
- "Mozilla/5.0 (iPad; U; CPU OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5",
93
- "Mozilla/5.0 (Linux; U; Android 2.2.1; zh-cn; HTC_Wildfire_A3333 Build/FRG83D) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
94
- "Mozilla/5.0 (Linux; U; Android 2.3.7; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
95
- "MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
96
- "Opera/9.80 (Android 2.3.4; Linux; Opera Mobi/build-1107180945; U; en-GB) Presto/2.8.149 Version/11.10",
97
- "Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13",
98
- "Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en) AppleWebKit/534.1+ (KHTML, like Gecko) Version/6.0.0.337 Mobile Safari/534.1+",
99
- "Mozilla/5.0 (hp-tablet; Linux; hpwOS/3.0.0; U; en-US) AppleWebKit/534.6 (KHTML, like Gecko) wOSBrowser/233.70 Safari/534.6 TouchPad/1.0",
100
- "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;",
101
- "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)",
102
- "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)",
103
- "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)",
104
- "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)",
105
- "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; The World)",
106
- "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; TencentTraveler 4.0)",
107
- "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Avant Browser)",
108
- "Mozilla/5.0 (Linux; U; Android 2.3.7; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
109
- "Mozilla/5.0 (SymbianOS/9.4; Series60/5.0 NokiaN97-1/20.0.019; Profile/MIDP-2.1 Configuration/CLDC-1.1) AppleWebKit/525 (KHTML, like Gecko) BrowserNG/7.1.18124",
110
- "Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; HTC; Titan)",
111
- "UCWEB7.0.2.37/28/999",
112
- "NOKIA5700/UCWEB7.0.2.37/28/999",
113
- "Openwave/UCWEB7.0.2.37/28/999",
114
- "Openwave/UCWEB7.0.2.37/28/999",
115
- ]
116
- with open(newtxtfile, "w") as f:
117
- for line in ua_list:
118
- f.write(line + "\n")
119
- # print(f'Using User-Agent: {ua}')
120
- ua = random.choice(ua_list)
121
- return ua
122
-
123
-
124
- # get_ua_org()
125
-
126
- if __name__ == "__main__":
127
- txtfile = r"E:\Code\Python\My_Funcs\OAFuncs\oafuncs\oa_down\User_Agent-list.txt"
128
-
129
- with open(txtfile, "r") as f:
130
- lines = f.readlines()
131
- # 去掉换行符和空行
132
- lines = [line.strip() for line in lines if line.strip()]
133
- """ new_line = []
134
- for i in range(len(lines)):
135
- if '/' in lines[i]:
136
- new_line.append(lines[i])
137
- else:
138
- print(lines[i]) """
139
-
140
- new_line = []
141
- for line in lines:
142
- if is_valid_user_agent(line):
143
- # print(line)
144
- new_line.append(line)
145
- else:
146
- print(f"Invalid User-Agent: {line}")
147
-
148
- newtxtfile = r"E:\Code\Python\My_Funcs\OAFuncs\oafuncs\oa_down\ua_list_new.txt"
149
- with open(newtxtfile, "w") as f:
150
- for line in new_line:
151
- f.write(line + "\n")
oafuncs/oa_s/__init__.py DELETED
@@ -1,23 +0,0 @@
1
- #!/usr/bin/env python
2
- # coding=utf-8
3
- """
4
- Author: Liu Kun && 16031215@qq.com
5
- Date: 2024-09-17 16:09:20
6
- LastEditors: Liu Kun && 16031215@qq.com
7
- LastEditTime: 2024-12-13 10:47:40
8
- FilePath: \\Python\\My_Funcs\\OAFuncs\\oafuncs\\oa_s\\__init__.py
9
- Description:
10
- EditPlatform: vscode
11
- ComputerInfo: XPS 15 9510
12
- SystemInfo: Windows 11
13
- Python Version: 3.11
14
- """
15
-
16
- # 会导致OAFuncs直接导入所有函数,不符合模块化设计
17
- from .oa_cmap import *
18
- from .oa_data import *
19
- from .oa_draw import *
20
- from .oa_file import *
21
- from .oa_help import *
22
- from .oa_nc import *
23
- from .oa_python import *
oafuncs/oa_s/oa_cmap.py DELETED
@@ -1,163 +0,0 @@
1
- #!/usr/bin/env python
2
- # coding=utf-8
3
- """
4
- Author: Liu Kun && 16031215@qq.com
5
- Date: 2024-09-17 16:55:11
6
- LastEditors: Liu Kun && 16031215@qq.com
7
- LastEditTime: 2024-11-21 13:14:24
8
- FilePath: \\Python\\My_Funcs\\OAFuncs\\oafuncs\\oa_cmap.py
9
- Description:
10
- EditPlatform: vscode
11
- ComputerInfo: XPS 15 9510
12
- SystemInfo: Windows 11
13
- Python Version: 3.11
14
- """
15
-
16
- import matplotlib as mpl
17
- import matplotlib.pyplot as plt
18
- import numpy as np
19
-
20
- __all__ = ["show", "cmap2colors", "create_cmap", "create_cmap_rgbtxt", "choose_cmap"]
21
-
22
- # ** 将cmap用填色图可视化(官网摘抄函数)
23
- def show(colormaps: list):
24
- """
25
- Helper function to plot data with associated colormap.
26
- example:
27
- cmap = ListedColormap(["darkorange", "gold", "lawngreen", "lightseagreen"])
28
- show([cmap])
29
- """
30
- np.random.seed(19680801)
31
- data = np.random.randn(30, 30)
32
- n = len(colormaps)
33
- fig, axs = plt.subplots(1, n, figsize=(n * 2 + 2, 3), constrained_layout=True, squeeze=False)
34
- for [ax, cmap] in zip(axs.flat, colormaps):
35
- psm = ax.pcolormesh(data, cmap=cmap, rasterized=True, vmin=-4, vmax=4)
36
- fig.colorbar(psm, ax=ax)
37
- plt.show()
38
-
39
-
40
- # ** 将cmap转为list,即多个颜色的列表
41
- def cmap2colors(cmap, n=256):
42
- """
43
- cmap : cmap名称
44
- n : 提取颜色数量
45
- return : 提取的颜色列表
46
- example : out_colors = cmap2colors('viridis', 256)
47
- """
48
- c_map = mpl.colormaps.get_cmap(cmap)
49
- out_colors = [c_map(i) for i in np.linspace(0, 1, n)]
50
- return out_colors
51
-
52
-
53
- # ** 自制cmap,多色,可带位置
54
- def create_cmap(colors: list, nodes=None, under=None, over=None): # 利用颜色快速配色
55
- """
56
- func : 自制cmap,自动确定颜色位置(等比例)
57
- description : colors可以是颜色名称,也可以是十六进制颜色代码
58
- param {*} colors 颜色
59
- param {*} nodes 颜色位置,默认不提供,等间距
60
- return {*} cmap
61
- example : cmap = create_cmap(['#C2B7F3','#B3BBF2','#B0CBF1','#ACDCF0','#A8EEED'])
62
- cmap = create_cmap(['aliceblue','skyblue','deepskyblue'],[0.0,0.5,1.0])
63
- """
64
- if nodes is None: # 采取自动分配比例
65
- cmap_color = mpl.colors.LinearSegmentedColormap.from_list("mycmap", colors)
66
- else: # 按照提供比例分配
67
- cmap_color = mpl.colors.LinearSegmentedColormap.from_list("mycmap", list(zip(nodes, colors)))
68
- if under is not None:
69
- cmap_color.set_under(under)
70
- if over is not None:
71
- cmap_color.set_over(over)
72
- return cmap_color
73
-
74
-
75
- # ** 根据RGB的txt文档制作色卡(利用Grads调色盘)
76
- def create_cmap_rgbtxt(rgbtxt_file,split_mark=','): # 根据RGB的txt文档制作色卡/根据rgb值制作
77
- """
78
- func : 根据RGB的txt文档制作色卡
79
- description : rgbtxt_file='E:/python/colorbar/test.txt'
80
- param {*} rgbtxt_file txt文件路径
81
- return {*} camp
82
- example : cmap=create_cmap_rgbtxt(path,split_mark=',') #
83
-
84
- txt example : 251,251,253
85
- 225,125,25
86
- 250,205,255
87
- """
88
- with open(rgbtxt_file) as fid:
89
- data = fid.readlines()
90
- n = len(data)
91
- rgb = np.zeros((n, 3))
92
- for i in np.arange(n):
93
- rgb[i][0] = data[i].split(split_mark)[0]
94
- rgb[i][1] = data[i].split(split_mark)[1]
95
- rgb[i][2] = data[i].split(split_mark)[2]
96
- max_rgb = np.max(rgb)
97
- if max_rgb > 2: # 如果rgb值大于2,则认为是0-255的值,需要归一化
98
- rgb = rgb / 255.0
99
- icmap = mpl.colors.ListedColormap(rgb, name="my_color")
100
- return icmap
101
-
102
-
103
- def choose_cmap(cmap_name=None, query=False):
104
- """
105
- description: Choosing a colormap from the list of available colormaps or a custom colormap
106
- param {*} cmap_name:
107
- param {*} query:
108
- return {*}
109
- """
110
-
111
- my_cmap_dict = {
112
- "diverging_1": create_cmap(["#4e00b3", "#0000FF", "#00c0ff", "#a1d3ff", "#DCDCDC", "#FFD39B", "#FF8247", "#FF0000", "#FF5F9E"]),
113
- "cold_1": create_cmap(["#4e00b3", "#0000FF", "#00c0ff", "#a1d3ff", "#DCDCDC"]),
114
- "warm_1": create_cmap(["#DCDCDC", "#FFD39B", "#FF8247", "#FF0000", "#FF5F9E"]),
115
- # "land_1": create_custom(["#3E6436", "#678A59", "#91A176", "#B8A87D", "#D9CBB2"], under="#A6CEE3", over="#FFFFFF"), # 陆地颜色从深绿到浅棕,表示从植被到沙地的递减
116
- # "ocean_1": create_custom(["#126697", "#2D88B3", "#4EA1C9", "#78B9D8", "#A6CEE3"], under="#8470FF", over="#3E6436"), # 海洋颜色从深蓝到浅蓝,表示从深海到浅海的递减
117
- # "ocean_land_1": create_custom(
118
- # [
119
- # "#126697", # 深蓝(深海)
120
- # "#2D88B3", # 蓝
121
- # "#4EA1C9", # 蓝绿
122
- # "#78B9D8", # 浅蓝(浅海)
123
- # "#A6CEE3", # 浅蓝(近岸)
124
- # "#AAAAAA", # 灰色(0值,海平面)
125
- # "#D9CBB2", # 沙质土壤色(陆地开始)
126
- # "#B8A87D", # 浅棕
127
- # "#91A176", # 浅绿
128
- # "#678A59", # 中绿
129
- # "#3E6436", # 深绿(高山)
130
- # ]
131
- # ),
132
- "colorful_1": create_cmap(["#6d00db", "#9800cb", "#F2003C", "#ff4500", "#ff7f00", "#FE28A2", "#FFC0CB", "#DDA0DD", "#40E0D0", "#1a66f2", "#00f7fb", "#8fff88", "#E3FF00"]),
133
- }
134
- if query:
135
- for key, _ in my_cmap_dict.items():
136
- print(key)
137
-
138
- if cmap_name in my_cmap_dict:
139
- return my_cmap_dict[cmap_name]
140
- else:
141
- try:
142
- return mpl.colormaps.get_cmap(cmap_name)
143
- except ValueError:
144
- raise ValueError(f"Unknown cmap name: {cmap_name}")
145
-
146
-
147
- if __name__ == "__main__":
148
- # ** 测试自制cmap
149
- colors = ["#C2B7F3", "#B3BBF2", "#B0CBF1", "#ACDCF0", "#A8EEED"]
150
- nodes = [0.0, 0.2, 0.4, 0.6, 1.0]
151
- c_map = create_cmap(colors, nodes)
152
- show([c_map])
153
-
154
- # ** 测试自制diverging型cmap
155
- diverging_cmap = create_cmap(["#4e00b3", "#0000FF", "#00c0ff", "#a1d3ff", "#DCDCDC", "#FFD39B", "#FF8247", "#FF0000", "#FF5F9E"])
156
- show([diverging_cmap])
157
-
158
- # ** 测试根据RGB的txt文档制作色卡
159
- file_path = "E:/python/colorbar/test.txt"
160
- cmap_rgb = create_cmap_rgbtxt(file_path)
161
-
162
- # ** 测试将cmap转为list
163
- out_colors = cmap2colors("viridis", 256)
oafuncs/oa_s/oa_data.py DELETED
@@ -1,187 +0,0 @@
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-11-21 13:13:20
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
-
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
- __all__ = ['interp_2d', 'interp_2d_parallel']
24
-
25
- # ** 高维插值函数,插值最后两个维度
26
-
27
-
28
- def interp_2d(target_x, target_y, origin_x, origin_y, data, method='linear'):
29
- """
30
- 高维插值函数,默认插值最后两个维度,传输数据前请确保数据的维度正确
31
- 参数:
32
- target_y (array-like): 目标经度网格 1D 或 2D
33
- target_x (array-like): 目标纬度网格 1D 或 2D
34
- origin_y (array-like): 初始经度网格 1D 或 2D
35
- origin_x (array-like): 初始纬度网格 1D 或 2D
36
- data (array-like): 数据 (*, lat, lon) 2D, 3D, 4D
37
- method (str, optional): 插值方法,可选 'linear', 'nearest', 'cubic' 等,默认为 'linear'
38
- 返回:
39
- array-like: 插值结果
40
- """
41
-
42
- # 确保目标网格和初始网格都是二维的
43
- if len(target_y.shape) == 1:
44
- target_x, target_y = np.meshgrid(target_x, target_y)
45
- if len(origin_y.shape) == 1:
46
- origin_x, origin_y = np.meshgrid(origin_x, origin_y)
47
-
48
- dims = data.shape
49
- len_dims = len(dims)
50
- # print(dims[-2:])
51
- # 根据经纬度网格判断输入数据的形状是否匹配
52
-
53
- if origin_x.shape != dims[-2:] or origin_y.shape != dims[-2:]:
54
- print(origin_x.shape, dims[-2:])
55
- raise ValueError('Shape of data does not match shape of origin_x or origin_y.')
56
-
57
- # 将目标网格展平成一维数组
58
- target_points = np.column_stack((np.ravel(target_y), np.ravel(target_x)))
59
-
60
- # 将初始网格展平成一维数组
61
- origin_points = np.column_stack((np.ravel(origin_y), np.ravel(origin_x)))
62
-
63
- # 进行插值
64
- if len_dims == 2:
65
- interpolated_data = griddata(origin_points, np.ravel(data), target_points, method=method)
66
- interpolated_data = np.reshape(interpolated_data, target_y.shape)
67
- elif len_dims == 3:
68
- interpolated_data = []
69
- for i in range(dims[0]):
70
- dt = griddata(origin_points, np.ravel(data[i, :, :]), target_points, method=method)
71
- interpolated_data.append(np.reshape(dt, target_y.shape))
72
- print(f'Interpolating {i+1}/{dims[0]}...')
73
- interpolated_data = np.array(interpolated_data)
74
- elif len_dims == 4:
75
- interpolated_data = []
76
- for i in range(dims[0]):
77
- interpolated_data.append([])
78
- for j in range(dims[1]):
79
- dt = griddata(origin_points, np.ravel(data[i, j, :, :]), target_points, method=method)
80
- interpolated_data[i].append(np.reshape(dt, target_y.shape))
81
- print(f'\rInterpolating {i*dims[1]+j+1}/{dims[0]*dims[1]}...', end='')
82
- print('\n')
83
- interpolated_data = np.array(interpolated_data)
84
-
85
- return interpolated_data
86
-
87
-
88
- # ** 高维插值函数,插值最后两个维度,使用多线程进行插值
89
- # 在本地电脑上可以提速三倍左右,超算上暂时无法加速
90
- def interp_2d_parallel(target_x, target_y, origin_x, origin_y, data, method='linear'):
91
- '''
92
- param {*} target_x 目标经度网格 1D 或 2D
93
- param {*} target_y 目标纬度网格 1D 或 2D
94
- param {*} origin_x 初始经度网格 1D 或 2D
95
- param {*} origin_y 初始纬度网格 1D 或 2D
96
- param {*} data 数据 (*, lat, lon) 2D, 3D, 4D
97
- param {*} method 插值方法,可选 'linear', 'nearest', 'cubic' 等,默认为 'linear'
98
- return {*} 插值结果
99
- description : 高维插值函数,默认插值最后两个维度,传输数据前请确保数据的维度正确
100
- example : interpolated_data = interp_2d_parallel(target_x, target_y, origin_x, origin_y, data, method='linear')
101
- '''
102
- def interp_single2d(target_y, target_x, origin_y, origin_x, data, method='linear'):
103
- target_points = np.column_stack((np.ravel(target_y), np.ravel(target_x)))
104
- origin_points = np.column_stack((np.ravel(origin_y), np.ravel(origin_x)))
105
-
106
- dt = griddata(origin_points, np.ravel(data[:, :]), target_points, method=method)
107
- return np.reshape(dt, target_y.shape)
108
-
109
- def interp_single3d(i, target_y, target_x, origin_y, origin_x, data, method='linear'):
110
- target_points = np.column_stack((np.ravel(target_y), np.ravel(target_x)))
111
- origin_points = np.column_stack((np.ravel(origin_y), np.ravel(origin_x)))
112
-
113
- dt = griddata(origin_points, np.ravel(data[i, :, :]), target_points, method=method)
114
- return np.reshape(dt, target_y.shape)
115
-
116
- def interp_single4d(i, j, target_y, target_x, origin_y, origin_x, data, method='linear'):
117
- target_points = np.column_stack((np.ravel(target_y), np.ravel(target_x)))
118
- origin_points = np.column_stack((np.ravel(origin_y), np.ravel(origin_x)))
119
-
120
- dt = griddata(origin_points, np.ravel(data[i, j, :, :]), target_points, method=method)
121
- return np.reshape(dt, target_y.shape)
122
-
123
- if len(target_y.shape) == 1:
124
- target_x, target_y = np.meshgrid(target_x, target_y)
125
- if len(origin_y.shape) == 1:
126
- origin_x, origin_y = np.meshgrid(origin_x, origin_y)
127
-
128
- dims = data.shape
129
- len_dims = len(dims)
130
-
131
- if origin_x.shape != dims[-2:] or origin_y.shape != dims[-2:]:
132
- raise ValueError('数据形状与 origin_x 或 origin_y 的形状不匹配.')
133
-
134
- interpolated_data = []
135
-
136
- # 使用多线程进行插值
137
- with ThreadPoolExecutor(max_workers=mp.cpu_count()-2) as executor:
138
- print(f'Using {mp.cpu_count()-2} threads...')
139
- if len_dims == 2:
140
- interpolated_data = list(executor.map(interp_single2d, [target_y], [target_x], [origin_y], [origin_x], [data], [method]))
141
- elif len_dims == 3:
142
- interpolated_data = list(executor.map(interp_single3d, [i for i in range(dims[0])], [target_y]*dims[0], [target_x]*dims[0], [origin_y]*dims[0], [origin_x]*dims[0], [data]*dims[0], [method]*dims[0]))
143
- elif len_dims == 4:
144
- interpolated_data = list(executor.map(interp_single4d, [i for i in range(dims[0]) for j in range(dims[1])], [j for i in range(dims[0]) for j in range(dims[1])], [target_y]*dims[0]*dims[1], [target_x]*dims[0]*dims[1], [origin_y]*dims[0]*dims[1], [origin_x]*dims[0]*dims[1], [data]*dims[0]*dims[1], [method]*dims[0]*dims[1]))
145
- interpolated_data = np.array(interpolated_data).reshape(dims[0], dims[1], target_y.shape[0], target_x.shape[1])
146
-
147
- interpolated_data = np.array(interpolated_data)
148
-
149
- return interpolated_data
150
-
151
-
152
- if __name__ == '__main__':
153
- import time
154
-
155
- import matplotlib.pyplot as plt
156
-
157
- # 测试数据
158
- origin_x = np.linspace(0, 10, 11)
159
- origin_y = np.linspace(0, 10, 11)
160
- target_x = np.linspace(0, 10, 101)
161
- target_y = np.linspace(0, 10, 101)
162
- data = np.random.rand(11, 11)
163
-
164
- # 高维插值
165
- origin_x = np.linspace(0, 10, 11)
166
- origin_y = np.linspace(0, 10, 11)
167
- target_x = np.linspace(0, 10, 101)
168
- target_y = np.linspace(0, 10, 101)
169
- data = np.random.rand(10, 10, 11, 11)
170
-
171
- start = time.time()
172
- interpolated_data = interp_2d(target_x, target_y, origin_x, origin_y, data)
173
- print(f'Interpolation time: {time.time()-start:.2f}s')
174
-
175
- print(interpolated_data.shape)
176
-
177
- # 高维插值多线程
178
- start = time.time()
179
- interpolated_data = interp_2d_parallel(target_x, target_y, origin_x, origin_y, data)
180
- print(f'Interpolation time: {time.time()-start:.2f}s')
181
-
182
- print(interpolated_data.shape)
183
- print(interpolated_data[0, 0, :, :].shape)
184
- plt.figure()
185
- plt.contourf(target_x, target_y, interpolated_data[0, 0, :, :])
186
- plt.colorbar()
187
- plt.show()