oafuncs 0.0.80__py2.py3-none-any.whl → 0.0.81__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.
- oafuncs/__init__.py +27 -12
- oafuncs/oa_cmap.py +31 -52
- oafuncs/oa_nc.py +120 -10
- {oafuncs-0.0.80.dist-info → oafuncs-0.0.81.dist-info}/METADATA +1 -2
- {oafuncs-0.0.80.dist-info → oafuncs-0.0.81.dist-info}/RECORD +8 -8
- oafuncs-0.0.81.dist-info/top_level.txt +1 -0
- oafuncs-0.0.80.dist-info/top_level.txt +0 -2
- {oafuncs-0.0.80.dist-info → oafuncs-0.0.81.dist-info}/LICENSE.txt +0 -0
- {oafuncs-0.0.80.dist-info → oafuncs-0.0.81.dist-info}/WHEEL +0 -0
oafuncs/__init__.py
CHANGED
@@ -4,26 +4,41 @@
|
|
4
4
|
Author: Liu Kun && 16031215@qq.com
|
5
5
|
Date: 2024-09-17 16:09:20
|
6
6
|
LastEditors: Liu Kun && 16031215@qq.com
|
7
|
-
LastEditTime: 2024-12-13
|
8
|
-
FilePath: \\Python\\My_Funcs\\OAFuncs\\oafuncs\\__init__.py
|
7
|
+
LastEditTime: 2024-12-13 12:31:06
|
8
|
+
FilePath: \\Python\\My_Funcs\\OAFuncs\\oafuncs\\oa_s\\__init__.py
|
9
9
|
Description:
|
10
10
|
EditPlatform: vscode
|
11
11
|
ComputerInfo: XPS 15 9510
|
12
12
|
SystemInfo: Windows 11
|
13
|
-
Python Version: 3.
|
13
|
+
Python Version: 3.12
|
14
14
|
"""
|
15
15
|
|
16
|
+
|
16
17
|
# 会导致OAFuncs直接导入所有函数,不符合模块化设计
|
17
|
-
from oafuncs.oa_s import
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
oa_python,
|
25
|
-
)
|
18
|
+
# from oafuncs.oa_s.oa_cmap import *
|
19
|
+
# from oafuncs.oa_s.oa_data import *
|
20
|
+
# from oafuncs.oa_s.oa_draw import *
|
21
|
+
# from oafuncs.oa_s.oa_file import *
|
22
|
+
# from oafuncs.oa_s.oa_help import *
|
23
|
+
# from oafuncs.oa_s.oa_nc import *
|
24
|
+
# from oafuncs.oa_s.oa_python import *
|
26
25
|
|
26
|
+
# ------------------- 2024-12-13 12:31:06 -------------------
|
27
|
+
# path: My_Funcs/OAFuncs/oafuncs/
|
28
|
+
from .oa_cmap import *
|
29
|
+
from .oa_data import *
|
30
|
+
from .oa_draw import *
|
31
|
+
from .oa_file import *
|
32
|
+
from .oa_help import *
|
33
|
+
from .oa_nc import *
|
34
|
+
from .oa_python import *
|
35
|
+
# ------------------- 2024-12-13 12:31:06 -------------------
|
36
|
+
# path: My_Funcs/OAFuncs/oafuncs/oa_down/
|
27
37
|
from .oa_down import *
|
38
|
+
# ------------------- 2024-12-13 12:31:06 -------------------
|
39
|
+
# path: My_Funcs/OAFuncs/oafuncs/oa_sign/
|
28
40
|
from .oa_sign import *
|
41
|
+
# ------------------- 2024-12-13 12:31:06 -------------------
|
42
|
+
# path: My_Funcs/OAFuncs/oafuncs/oa_tool/
|
29
43
|
from .oa_tool import *
|
44
|
+
# ------------------- 2024-12-13 12:31:06 -------------------
|
oafuncs/oa_cmap.py
CHANGED
@@ -17,11 +17,9 @@ import matplotlib as mpl
|
|
17
17
|
import matplotlib.pyplot as plt
|
18
18
|
import numpy as np
|
19
19
|
|
20
|
-
__all__ = ["show", "
|
20
|
+
__all__ = ["show", "cmap2colors", "create_cmap", "create_cmap_rgbtxt", "choose_cmap"]
|
21
21
|
|
22
22
|
# ** 将cmap用填色图可视化(官网摘抄函数)
|
23
|
-
|
24
|
-
|
25
23
|
def show(colormaps: list):
|
26
24
|
"""
|
27
25
|
Helper function to plot data with associated colormap.
|
@@ -40,28 +38,28 @@ def show(colormaps: list):
|
|
40
38
|
|
41
39
|
|
42
40
|
# ** 将cmap转为list,即多个颜色的列表
|
43
|
-
def
|
41
|
+
def cmap2colors(cmap, n=256):
|
44
42
|
"""
|
45
43
|
cmap : cmap名称
|
46
44
|
n : 提取颜色数量
|
47
45
|
return : 提取的颜色列表
|
48
|
-
example :
|
46
|
+
example : out_colors = cmap2colors('viridis', 256)
|
49
47
|
"""
|
50
48
|
c_map = mpl.colormaps.get_cmap(cmap)
|
51
|
-
|
52
|
-
return
|
49
|
+
out_colors = [c_map(i) for i in np.linspace(0, 1, n)]
|
50
|
+
return out_colors
|
53
51
|
|
54
52
|
|
55
53
|
# ** 自制cmap,多色,可带位置
|
56
|
-
def
|
54
|
+
def create_cmap(colors: list, nodes=None, under=None, over=None): # 利用颜色快速配色
|
57
55
|
"""
|
58
56
|
func : 自制cmap,自动确定颜色位置(等比例)
|
59
57
|
description : colors可以是颜色名称,也可以是十六进制颜色代码
|
60
58
|
param {*} colors 颜色
|
61
59
|
param {*} nodes 颜色位置,默认不提供,等间距
|
62
|
-
return {*}
|
63
|
-
example :
|
64
|
-
|
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])
|
65
63
|
"""
|
66
64
|
if nodes is None: # 采取自动分配比例
|
67
65
|
cmap_color = mpl.colors.LinearSegmentedColormap.from_list("mycmap", colors)
|
@@ -74,46 +72,27 @@ def create_custom(colors: list, nodes=None, under=None, over=None): # 利用颜
|
|
74
72
|
return cmap_color
|
75
73
|
|
76
74
|
|
77
|
-
# ** 自制diverging型cmap,默认中间为白色
|
78
|
-
|
79
|
-
|
80
|
-
def create_diverging(colors: list):
|
81
|
-
"""
|
82
|
-
func : 自制cmap,双色,中间默认为白色;如果输入偶数个颜色,则中间为白,如果奇数个颜色,则中间色为中间色
|
83
|
-
description : colors可以是颜色名称,也可以是十六进制颜色代码
|
84
|
-
param {*} colors
|
85
|
-
return {*}
|
86
|
-
example : diverging_cmap = mk_cmap_diverging(["#00c0ff", "#a1d3ff", "#DCDCDC", "#FFD39B", "#FF8247"])
|
87
|
-
"""
|
88
|
-
# 自定义颜色位置
|
89
|
-
n = len(colors)
|
90
|
-
nodes = np.linspace(0.0, 1.0, n + 1 if n % 2 == 0 else n)
|
91
|
-
newcolors = colors
|
92
|
-
if n % 2 == 0:
|
93
|
-
newcolors.insert(int(n / 2), "#ffffff") # 偶数个颜色,中间为白色
|
94
|
-
cmap_color = mpl.colors.LinearSegmentedColormap.from_list("mycmap", list(zip(nodes, newcolors)))
|
95
|
-
return cmap_color
|
96
|
-
|
97
|
-
|
98
75
|
# ** 根据RGB的txt文档制作色卡(利用Grads调色盘)
|
99
|
-
|
100
|
-
|
101
|
-
def create_5rgb_txt(rgb_txt_filepath: str): # 根据RGB的txt文档制作色卡/根据rgb值制作
|
76
|
+
def create_cmap_rgbtxt(rgbtxt_file,split_mark=','): # 根据RGB的txt文档制作色卡/根据rgb值制作
|
102
77
|
"""
|
103
78
|
func : 根据RGB的txt文档制作色卡
|
104
|
-
description :
|
105
|
-
param {*}
|
79
|
+
description : rgbtxt_file='E:/python/colorbar/test.txt'
|
80
|
+
param {*} rgbtxt_file txt文件路径
|
106
81
|
return {*} camp
|
107
|
-
example :
|
82
|
+
example : cmap=create_cmap_rgbtxt(path,split_mark=',') #
|
83
|
+
|
84
|
+
txt example : 251,251,253
|
85
|
+
225,125,25
|
86
|
+
250,205,255
|
108
87
|
"""
|
109
|
-
with open(
|
88
|
+
with open(rgbtxt_file) as fid:
|
110
89
|
data = fid.readlines()
|
111
90
|
n = len(data)
|
112
91
|
rgb = np.zeros((n, 3))
|
113
92
|
for i in np.arange(n):
|
114
|
-
rgb[i][0] = data[i].split(
|
115
|
-
rgb[i][1] = data[i].split(
|
116
|
-
rgb[i][2] = data[i].split(
|
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]
|
117
96
|
max_rgb = np.max(rgb)
|
118
97
|
if max_rgb > 2: # 如果rgb值大于2,则认为是0-255的值,需要归一化
|
119
98
|
rgb = rgb / 255.0
|
@@ -121,7 +100,7 @@ def create_5rgb_txt(rgb_txt_filepath: str): # 根据RGB的txt文档制作色卡
|
|
121
100
|
return icmap
|
122
101
|
|
123
102
|
|
124
|
-
def
|
103
|
+
def choose_cmap(cmap_name=None, query=False):
|
125
104
|
"""
|
126
105
|
description: Choosing a colormap from the list of available colormaps or a custom colormap
|
127
106
|
param {*} cmap_name:
|
@@ -130,9 +109,9 @@ def my_cmap(cmap_name=None, query=False):
|
|
130
109
|
"""
|
131
110
|
|
132
111
|
my_cmap_dict = {
|
133
|
-
"diverging_1":
|
134
|
-
"cold_1":
|
135
|
-
"warm_1":
|
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"]),
|
136
115
|
# "land_1": create_custom(["#3E6436", "#678A59", "#91A176", "#B8A87D", "#D9CBB2"], under="#A6CEE3", over="#FFFFFF"), # 陆地颜色从深绿到浅棕,表示从植被到沙地的递减
|
137
116
|
# "ocean_1": create_custom(["#126697", "#2D88B3", "#4EA1C9", "#78B9D8", "#A6CEE3"], under="#8470FF", over="#3E6436"), # 海洋颜色从深蓝到浅蓝,表示从深海到浅海的递减
|
138
117
|
# "ocean_land_1": create_custom(
|
@@ -150,7 +129,7 @@ def my_cmap(cmap_name=None, query=False):
|
|
150
129
|
# "#3E6436", # 深绿(高山)
|
151
130
|
# ]
|
152
131
|
# ),
|
153
|
-
"colorful_1":
|
132
|
+
"colorful_1": create_cmap(["#6d00db", "#9800cb", "#F2003C", "#ff4500", "#ff7f00", "#FE28A2", "#FFC0CB", "#DDA0DD", "#40E0D0", "#1a66f2", "#00f7fb", "#8fff88", "#E3FF00"]),
|
154
133
|
}
|
155
134
|
if query:
|
156
135
|
for key, _ in my_cmap_dict.items():
|
@@ -160,7 +139,7 @@ def my_cmap(cmap_name=None, query=False):
|
|
160
139
|
return my_cmap_dict[cmap_name]
|
161
140
|
else:
|
162
141
|
try:
|
163
|
-
return mpl.
|
142
|
+
return mpl.colormaps.get_cmap(cmap_name)
|
164
143
|
except ValueError:
|
165
144
|
raise ValueError(f"Unknown cmap name: {cmap_name}")
|
166
145
|
|
@@ -169,16 +148,16 @@ if __name__ == "__main__":
|
|
169
148
|
# ** 测试自制cmap
|
170
149
|
colors = ["#C2B7F3", "#B3BBF2", "#B0CBF1", "#ACDCF0", "#A8EEED"]
|
171
150
|
nodes = [0.0, 0.2, 0.4, 0.6, 1.0]
|
172
|
-
c_map =
|
151
|
+
c_map = create_cmap(colors, nodes)
|
173
152
|
show([c_map])
|
174
153
|
|
175
154
|
# ** 测试自制diverging型cmap
|
176
|
-
diverging_cmap =
|
155
|
+
diverging_cmap = create_cmap(["#4e00b3", "#0000FF", "#00c0ff", "#a1d3ff", "#DCDCDC", "#FFD39B", "#FF8247", "#FF0000", "#FF5F9E"])
|
177
156
|
show([diverging_cmap])
|
178
157
|
|
179
158
|
# ** 测试根据RGB的txt文档制作色卡
|
180
159
|
file_path = "E:/python/colorbar/test.txt"
|
181
|
-
|
160
|
+
cmap_rgb = create_cmap_rgbtxt(file_path)
|
182
161
|
|
183
162
|
# ** 测试将cmap转为list
|
184
|
-
|
163
|
+
out_colors = cmap2colors("viridis", 256)
|
oafuncs/oa_nc.py
CHANGED
@@ -19,7 +19,7 @@ import netCDF4 as nc
|
|
19
19
|
import numpy as np
|
20
20
|
import xarray as xr
|
21
21
|
|
22
|
-
__all__ = ["get_var", "extract5nc", "write2nc", "merge5nc", "modify_var_value", "modify_var_attr", "rename_var_or_dim", "check_ncfile"]
|
22
|
+
__all__ = ["get_var", "extract5nc", "write2nc", "merge5nc", "modify_var_value", "modify_var_attr", "rename_var_or_dim", "check_ncfile", "longitude_change", "nc_isel"]
|
23
23
|
|
24
24
|
|
25
25
|
def get_var(file, *vars):
|
@@ -38,7 +38,7 @@ def get_var(file, *vars):
|
|
38
38
|
return datas
|
39
39
|
|
40
40
|
|
41
|
-
def extract5nc(file, varname):
|
41
|
+
def extract5nc(file, varname, only_value=True):
|
42
42
|
"""
|
43
43
|
描述:
|
44
44
|
1、提取nc文件中的变量
|
@@ -47,16 +47,22 @@ def extract5nc(file, varname):
|
|
47
47
|
参数:
|
48
48
|
file: 文件路径
|
49
49
|
varname: 变量名
|
50
|
+
only_value: 变量和维度是否只保留数值
|
50
51
|
example: data, dimdict = extract5nc(file_ecm, 'h')
|
51
52
|
"""
|
52
53
|
ds = xr.open_dataset(file)
|
53
54
|
vardata = ds[varname]
|
55
|
+
ds.close()
|
54
56
|
dims = vardata.dims
|
55
57
|
dimdict = {}
|
56
58
|
for dim in dims:
|
57
|
-
|
58
|
-
|
59
|
-
|
59
|
+
if only_value:
|
60
|
+
dimdict[dim] = vardata[dim].values
|
61
|
+
else:
|
62
|
+
dimdict[dim] = ds[dim]
|
63
|
+
if only_value:
|
64
|
+
vardata = np.array(vardata)
|
65
|
+
return vardata, dimdict
|
60
66
|
|
61
67
|
|
62
68
|
def _numpy_to_nc_type(numpy_type):
|
@@ -76,15 +82,27 @@ def _numpy_to_nc_type(numpy_type):
|
|
76
82
|
return numpy_to_nc.get(str(numpy_type), "f4") # 默认使用 'float32'
|
77
83
|
|
78
84
|
|
79
|
-
def
|
85
|
+
def _calculate_scale_and_offset(data, n=16):
|
86
|
+
data_min, data_max = np.nanmin(data), np.nanmax(data)
|
87
|
+
scale_factor = (data_max - data_min) / (2 ** n - 1)
|
88
|
+
add_offset = data_min + 2 ** (n - 1) * scale_factor
|
89
|
+
# S = Q * scale_factor + add_offset
|
90
|
+
return scale_factor, add_offset
|
91
|
+
|
92
|
+
|
93
|
+
def write2nc(file, data, varname=None, coords=None, mode='w', scale_offset_switch=True, compile_switch=True):
|
80
94
|
"""
|
81
95
|
description: 写入数据到nc文件
|
96
|
+
|
82
97
|
参数:
|
83
98
|
file: 文件路径
|
84
99
|
data: 数据
|
85
100
|
varname: 变量名
|
86
101
|
coords: 坐标,字典,键为维度名称,值为坐标数据
|
87
102
|
mode: 写入模式,'w'为写入,'a'为追加
|
103
|
+
scale_offset_switch: 是否使用scale_factor和add_offset,默认为True
|
104
|
+
compile_switch: 是否使用压缩参数,默认为True
|
105
|
+
|
88
106
|
example: write2nc(r'test.nc', data, 'data', {'time': np.linspace(0, 120, 100), 'lev': np.linspace(0, 120, 50)}, 'a')
|
89
107
|
"""
|
90
108
|
# 判断mode是写入还是追加
|
@@ -96,6 +114,21 @@ def write2nc(file, data, varname, coords, mode):
|
|
96
114
|
if not os.path.exists(file):
|
97
115
|
print("Warning: File doesn't exist. Creating a new file.")
|
98
116
|
mode = "w"
|
117
|
+
|
118
|
+
complete = False
|
119
|
+
if varname is None and coords is None:
|
120
|
+
try:
|
121
|
+
data.to_netcdf(file)
|
122
|
+
complete = True
|
123
|
+
# 不能在这里return
|
124
|
+
except AttributeError:
|
125
|
+
raise ValueError("If varname and coords are None, data must be a DataArray.")
|
126
|
+
|
127
|
+
if complete:
|
128
|
+
return
|
129
|
+
|
130
|
+
kwargs = {'zlib': True, 'complevel': 4} # 压缩参数
|
131
|
+
# kwargs = {"compression": 'zlib', "complevel": 4} # 压缩参数
|
99
132
|
|
100
133
|
# 打开 NetCDF 文件
|
101
134
|
with nc.Dataset(file, mode, format="NETCDF4") as ncfile:
|
@@ -116,8 +149,17 @@ def write2nc(file, data, varname, coords, mode):
|
|
116
149
|
if add_coords:
|
117
150
|
# 创建新坐标
|
118
151
|
ncfile.createDimension(dim, len(coord_data))
|
119
|
-
|
152
|
+
if compile_switch:
|
153
|
+
ncfile.createVariable(dim, _numpy_to_nc_type(coord_data.dtype), (dim,), **kwargs)
|
154
|
+
else:
|
155
|
+
ncfile.createVariable(dim, _numpy_to_nc_type(coord_data.dtype), (dim,))
|
120
156
|
ncfile.variables[dim][:] = np.array(coord_data)
|
157
|
+
|
158
|
+
if isinstance(coord_data, xr.DataArray):
|
159
|
+
current_var = ncfile.variables[dim]
|
160
|
+
if coord_data.attrs:
|
161
|
+
for key, value in coord_data.attrs.items():
|
162
|
+
current_var.setncattr(key, value)
|
121
163
|
|
122
164
|
# 判断变量是否存在,若存在,则删除原变量
|
123
165
|
add_var = True
|
@@ -127,22 +169,48 @@ def write2nc(file, data, varname, coords, mode):
|
|
127
169
|
raise ValueError("Shape of data does not match the variable shape.")
|
128
170
|
else:
|
129
171
|
# 写入数据
|
130
|
-
ncfile.variables[varname][:] = data
|
172
|
+
ncfile.variables[varname][:] = np.array(data)
|
131
173
|
add_var = False
|
132
174
|
print(f"Warning: Variable '{varname}' already exists. Replacing it.")
|
133
175
|
|
134
176
|
if add_var:
|
135
177
|
# 创建变量及其维度
|
136
178
|
dim_names = tuple(coords.keys()) # 使用coords传入的维度名称
|
137
|
-
|
179
|
+
if scale_offset_switch:
|
180
|
+
scale_factor, add_offset = _calculate_scale_and_offset(np.array(data))
|
181
|
+
_FillValue = -32767
|
182
|
+
missing_value = -32767
|
183
|
+
dtype = 'i2' # short类型
|
184
|
+
else:
|
185
|
+
dtype = _numpy_to_nc_type(data.dtype)
|
186
|
+
|
187
|
+
if compile_switch:
|
188
|
+
ncfile.createVariable(varname, dtype, dim_names, **kwargs)
|
189
|
+
else:
|
190
|
+
ncfile.createVariable(varname, dtype, dim_names)
|
191
|
+
|
192
|
+
if scale_offset_switch: # 需要在写入数据之前设置scale_factor和add_offset
|
193
|
+
ncfile.variables[varname].setncattr('scale_factor', scale_factor)
|
194
|
+
ncfile.variables[varname].setncattr('add_offset', add_offset)
|
195
|
+
ncfile.variables[varname].setncattr('_FillValue', _FillValue)
|
196
|
+
ncfile.variables[varname].setncattr('missing_value', missing_value)
|
197
|
+
|
138
198
|
# ncfile.createVariable('data', 'f4', ('time','lev'))
|
139
199
|
|
140
200
|
# 写入数据
|
141
|
-
ncfile.variables[varname][:] = data
|
201
|
+
ncfile.variables[varname][:] = np.array(data)
|
142
202
|
|
143
203
|
# 判断维度是否匹配
|
144
204
|
if len(data.shape) != len(coords):
|
145
205
|
raise ValueError("Number of dimensions does not match the data shape.")
|
206
|
+
# 判断data是否带有属性信息,如果有,写入属性信息
|
207
|
+
if isinstance(data, xr.DataArray):
|
208
|
+
current_var = ncfile.variables[varname]
|
209
|
+
if data.attrs:
|
210
|
+
for key, value in data.attrs.items():
|
211
|
+
if key in ["scale_factor", "add_offset", "_FillValue", "missing_value"] and scale_offset_switch:
|
212
|
+
continue
|
213
|
+
current_var.setncattr(key, value)
|
146
214
|
|
147
215
|
|
148
216
|
def merge5nc(file_list, var_name=None, dim_name=None, target_filename=None):
|
@@ -330,6 +398,48 @@ def check_ncfile(ncfile, if_delete=False):
|
|
330
398
|
return False
|
331
399
|
|
332
400
|
|
401
|
+
def longitude_change(ds, lon_name="longitude", to_which="180"):
|
402
|
+
"""
|
403
|
+
将经度转换为 -180 到 180 之间
|
404
|
+
|
405
|
+
参数:
|
406
|
+
lon (numpy.ndarray): 经度数组
|
407
|
+
|
408
|
+
返回值:
|
409
|
+
numpy.ndarray: 转换后的经度数组
|
410
|
+
"""
|
411
|
+
# return (lon + 180) % 360 - 180
|
412
|
+
# ds = ds.assign_coords(longitude=(((ds.longitude + 180) % 360) - 180)).sortby("longitude")
|
413
|
+
if to_which == "180":
|
414
|
+
# ds = ds.assign_coords(**{lon_name: (((ds[lon_name] + 180) % 360) - 180)}).sortby(lon_name)
|
415
|
+
ds = ds.assign_coords(**{lon_name: (ds[lon_name] + 180) % 360 - 180}).sortby(lon_name)
|
416
|
+
elif to_which == "360":
|
417
|
+
# -180 to 180 to 0 to 360
|
418
|
+
ds = ds.assign_coords(**{lon_name: (ds[lon_name] + 360) % 360}).sortby(lon_name)
|
419
|
+
return ds
|
420
|
+
|
421
|
+
|
422
|
+
def nc_isel(ncfile, dim_name, slice_list):
|
423
|
+
"""
|
424
|
+
Description: Choose the data by the index of the dimension
|
425
|
+
|
426
|
+
Parameters:
|
427
|
+
ncfile: str, the path of the netCDF file
|
428
|
+
dim_name: str, the name of the dimension
|
429
|
+
slice_list: list, the index of the dimension
|
430
|
+
|
431
|
+
slice_list example: slice_list = [[y*12+m for m in range(11,14)] for y in range(84)]
|
432
|
+
or
|
433
|
+
slice_list = [y * 12 + m for y in range(84) for m in range(11, 14)]
|
434
|
+
"""
|
435
|
+
ds = xr.open_dataset(ncfile)
|
436
|
+
slice_list = np.array(slice_list).flatten()
|
437
|
+
slice_list = [int(i) for i in slice_list]
|
438
|
+
ds_new = ds.isel(**{dim_name: slice_list})
|
439
|
+
ds.close()
|
440
|
+
return ds_new
|
441
|
+
|
442
|
+
|
333
443
|
if __name__ == "__main__":
|
334
444
|
data = np.random.rand(100, 50)
|
335
445
|
write2nc(r"test.nc", data, "data", {"time": np.linspace(0, 120, 100), "lev": np.linspace(0, 120, 50)}, "a")
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: oafuncs
|
3
|
-
Version: 0.0.
|
3
|
+
Version: 0.0.81
|
4
4
|
Summary: My short description for my project.
|
5
5
|
Home-page: https://github.com/Industry-Pays/OAFuncs
|
6
6
|
Author: Kun Liu
|
@@ -13,7 +13,6 @@ Classifier: Programming Language :: Python :: 3.9
|
|
13
13
|
Classifier: Programming Language :: Python :: 3.10
|
14
14
|
Classifier: Programming Language :: Python :: 3.11
|
15
15
|
Classifier: Programming Language :: Python :: 3.12
|
16
|
-
Classifier: Programming Language :: Python :: 3.13
|
17
16
|
Classifier: Programming Language :: Python :: Implementation :: CPython
|
18
17
|
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
19
18
|
Requires-Python: >=3.9.0
|
@@ -1,10 +1,10 @@
|
|
1
|
-
oafuncs/__init__.py,sha256=
|
2
|
-
oafuncs/oa_cmap.py,sha256=
|
1
|
+
oafuncs/__init__.py,sha256=glcIlhQ9xSK4WtL58dq7Od2S3JPqsuEyhUQ-VWO8hOc,1426
|
2
|
+
oafuncs/oa_cmap.py,sha256=N-jt5CjRTXIjur6QAh90L8hAr9n3D-1HRSkKT98hUx8,6449
|
3
3
|
oafuncs/oa_data.py,sha256=H9qZrUziOpc456iIL-1lBwSkBPApl2rlR-ajZg-mDMs,8119
|
4
4
|
oafuncs/oa_draw.py,sha256=R5KONDf3Rp8STXepawtYUTdbcfAK1h6AYh8xiOfac3g,18860
|
5
5
|
oafuncs/oa_file.py,sha256=iHgv0CTH4k_7YUnQ8-qQbLoz_f2lUmVhzGWQ2LkPFP8,11624
|
6
6
|
oafuncs/oa_help.py,sha256=ppNktmtNzs15R20MD1bM7yImlTQ_ngMwvoIglePOKXA,1000
|
7
|
-
oafuncs/oa_nc.py,sha256=
|
7
|
+
oafuncs/oa_nc.py,sha256=4JUKM1H2JXhrk7sioUjAjhj1CHcyWHnSzXD8kXG9BFo,17198
|
8
8
|
oafuncs/oa_python.py,sha256=XPTP3o7zTFzfJR_YhsKfQksa3bSYwXsne9YxlJplCEA,3994
|
9
9
|
oafuncs/oa_down/User_Agent-list.txt,sha256=pazxSip8_lphEBOPHG902zmIBUg8sBKXgmqp_g6j_E4,661062
|
10
10
|
oafuncs/oa_down/__init__.py,sha256=nY5X7gM1jw7DJxyooR2UJSq4difkw-flz2Ucr_OuDbA,540
|
@@ -44,8 +44,8 @@ oafuncs - 副本/oa_sign/ocean.py,sha256=xrW-rWD7xBWsB5PuCyEwQ1Q_RDKq2KCLz-LOONH
|
|
44
44
|
oafuncs - 副本/oa_sign/scientific.py,sha256=a4JxOBgm9vzNZKpJ_GQIQf7cokkraV5nh23HGbmTYKw,5064
|
45
45
|
oafuncs - 副本/oa_tool/__init__.py,sha256=IKOlqpWlb4cMDCtq2VKR_RTxQHDNqR_vfqqsOsp_lKQ,466
|
46
46
|
oafuncs - 副本/oa_tool/email.py,sha256=4lJxV_KUzhxgLYfVwYTqp0qxRugD7fvsZkXDe5WkUKo,3052
|
47
|
-
oafuncs-0.0.
|
48
|
-
oafuncs-0.0.
|
49
|
-
oafuncs-0.0.
|
50
|
-
oafuncs-0.0.
|
51
|
-
oafuncs-0.0.
|
47
|
+
oafuncs-0.0.81.dist-info/LICENSE.txt,sha256=rMtLpVg8sKiSlwClfR9w_Dd_5WubTQgoOzE2PDFxzs4,1074
|
48
|
+
oafuncs-0.0.81.dist-info/METADATA,sha256=Kj7RhiA65yX1UsSu0jCJyeDT4XVhfAY2zInl4Rh3Pvc,22429
|
49
|
+
oafuncs-0.0.81.dist-info/WHEEL,sha256=pxeNX5JdtCe58PUSYP9upmc7jdRPgvT0Gm9kb1SHlVw,109
|
50
|
+
oafuncs-0.0.81.dist-info/top_level.txt,sha256=bgC35QkXbN4EmPHEveg_xGIZ5i9NNPYWqtJqaKqTPsQ,8
|
51
|
+
oafuncs-0.0.81.dist-info/RECORD,,
|
@@ -0,0 +1 @@
|
|
1
|
+
oafuncs
|
File without changes
|
File without changes
|