oafuncs 0.0.98.12__py3-none-any.whl → 0.0.98.14__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/_script/netcdf_merge.py +23 -32
- oafuncs/oa_tool.py +6 -1
- {oafuncs-0.0.98.12.dist-info → oafuncs-0.0.98.14.dist-info}/METADATA +1 -1
- {oafuncs-0.0.98.12.dist-info → oafuncs-0.0.98.14.dist-info}/RECORD +7 -7
- {oafuncs-0.0.98.12.dist-info → oafuncs-0.0.98.14.dist-info}/WHEEL +0 -0
- {oafuncs-0.0.98.12.dist-info → oafuncs-0.0.98.14.dist-info}/licenses/LICENSE.txt +0 -0
- {oafuncs-0.0.98.12.dist-info → oafuncs-0.0.98.14.dist-info}/top_level.txt +0 -0
oafuncs/_script/netcdf_merge.py
CHANGED
@@ -1,12 +1,26 @@
|
|
1
|
+
#!/usr/bin/env python
|
2
|
+
# coding=utf-8
|
3
|
+
"""
|
4
|
+
Author: Liu Kun && 16031215@qq.com
|
5
|
+
Date: 2025-03-30 11:16:29
|
6
|
+
LastEditors: Liu Kun && 16031215@qq.com
|
7
|
+
LastEditTime: 2025-04-25 14:23:10
|
8
|
+
FilePath: \\Python\\My_Funcs\\OAFuncs\\oafuncs\\_script\\netcdf_merge.py
|
9
|
+
Description
|
10
|
+
EditPlatform: vscode
|
11
|
+
ComputerInfo: XPS 15 9510
|
12
|
+
SystemInfo: Windows 11
|
13
|
+
Python Version: 3.12
|
14
|
+
"""
|
15
|
+
|
1
16
|
import os
|
2
17
|
from typing import List, Optional, Union
|
3
18
|
|
4
19
|
import numpy as np
|
5
20
|
import xarray as xr
|
6
|
-
from dask.diagnostics import ProgressBar
|
7
21
|
|
8
22
|
from oafuncs import pbar
|
9
|
-
|
23
|
+
import logging
|
10
24
|
|
11
25
|
|
12
26
|
def merge_nc(file_list: Union[str, List[str]], var_name: Optional[Union[str, List[str]]] = None, dim_name: Optional[str] = None, target_filename: Optional[str] = None) -> None:
|
@@ -27,6 +41,7 @@ def merge_nc(file_list: Union[str, List[str]], var_name: Optional[Union[str, Lis
|
|
27
41
|
merge(file_list, var_name=['u', 'v'], dim_name='time', target_filename='merged.nc')
|
28
42
|
merge(file_list, var_name=None, dim_name='time', target_filename='merged.nc')
|
29
43
|
"""
|
44
|
+
from oafuncs._script.netcdf_write import save_to_nc
|
30
45
|
|
31
46
|
if target_filename is None:
|
32
47
|
target_filename = "merged.nc"
|
@@ -53,7 +68,7 @@ def merge_nc(file_list: Union[str, List[str]], var_name: Optional[Union[str, Lis
|
|
53
68
|
# 初始化合并数据字典
|
54
69
|
merged_data = {}
|
55
70
|
|
56
|
-
for i, file in pbar(enumerate(file_list), description="Reading files",
|
71
|
+
for i, file in pbar(enumerate(file_list), description="Reading files", total=len(file_list)):
|
57
72
|
with xr.open_dataset(file) as ds:
|
58
73
|
for var in var_names:
|
59
74
|
data_var = ds[var]
|
@@ -66,46 +81,22 @@ def merge_nc(file_list: Union[str, List[str]], var_name: Optional[Union[str, Lis
|
|
66
81
|
else:
|
67
82
|
merged_data[var] = data_var.fillna(0)
|
68
83
|
|
69
|
-
for var in pbar(merged_data, description="Merging variables"
|
84
|
+
for var in pbar(merged_data, description="Merging variables"):
|
70
85
|
if isinstance(merged_data[var], list):
|
71
86
|
# 判断类型,时间类型用NaT填充
|
72
87
|
if np.issubdtype(merged_data[var][0].dtype, np.datetime64):
|
73
88
|
merged_data[var] = xr.concat(merged_data[var], dim=dim_name).fillna(np.datetime64("NaT"))
|
74
89
|
else:
|
75
90
|
merged_data[var] = xr.concat(merged_data[var], dim=dim_name).fillna(0)
|
76
|
-
# print(f"Variable '{var}' merged: min={merged_data[var].min().values:.3f}, max={merged_data[var].max().values:.3f}, mean={merged_data[var].mean().values:.3f}")
|
77
91
|
|
78
|
-
# 修改写入数据部分,支持压缩并设置基数和比例因子
|
79
|
-
# print("\nWriting data to file ...")
|
80
92
|
if os.path.exists(target_filename):
|
81
|
-
print("Warning: The target file already exists. Removing it ...")
|
93
|
+
# print("Warning: The target file already exists. Removing it ...")
|
94
|
+
logging.warning("The target file already exists. Removing it ...")
|
82
95
|
os.remove(target_filename)
|
83
96
|
|
84
|
-
|
85
|
-
encoding = {}
|
86
|
-
for var in merged_dataset.data_vars:
|
87
|
-
data = merged_dataset[var].values
|
88
|
-
if data.dtype.kind in {"i", "u", "f"}: # 仅对数值型数据进行压缩
|
89
|
-
# 统一调用 netcdf_write 中的 scale/offset 计算
|
90
|
-
scale_factor, add_offset = _calculate_scale_and_offset(data, n=16)
|
91
|
-
encoding[var] = {
|
92
|
-
"zlib": True,
|
93
|
-
"complevel": 4,
|
94
|
-
"dtype": "int16",
|
95
|
-
"scale_factor": scale_factor,
|
96
|
-
"add_offset": add_offset,
|
97
|
-
"_FillValue": -32767,
|
98
|
-
}
|
99
|
-
else:
|
100
|
-
encoding[var] = {"zlib": True, "complevel": 4}
|
101
|
-
|
102
|
-
# 确保写入时不会因编码问题导致数据丢失
|
103
|
-
# merged_dataset.to_netcdf(target_filename, encoding=encoding)
|
104
|
-
delayed_write = merged_dataset.to_netcdf(target_filename, encoding=encoding, compute=False)
|
105
|
-
with ProgressBar():
|
106
|
-
delayed_write.compute()
|
97
|
+
save_to_nc(target_filename, xr.Dataset(merged_data))
|
107
98
|
|
108
|
-
print(f'
|
99
|
+
print(f'File "{target_filename}" has been successfully created.')
|
109
100
|
|
110
101
|
|
111
102
|
# Example usage
|
oafuncs/oa_tool.py
CHANGED
@@ -2,6 +2,7 @@ import datetime
|
|
2
2
|
import logging
|
3
3
|
from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Union
|
4
4
|
|
5
|
+
|
5
6
|
from rich import print
|
6
7
|
|
7
8
|
from ._script.parallel import ParallelExecutor
|
@@ -174,6 +175,8 @@ def pbar(
|
|
174
175
|
"""
|
175
176
|
from ._script.cprogressbar import ColorProgressBar
|
176
177
|
import random
|
178
|
+
|
179
|
+
# number = random.randint(1, 999)
|
177
180
|
|
178
181
|
def _generate_random_color_hex():
|
179
182
|
"""Generate a random color in hexadecimal format."""
|
@@ -186,9 +189,11 @@ def pbar(
|
|
186
189
|
color = _generate_random_color_hex()
|
187
190
|
|
188
191
|
style = f"bold {color if color != 'None' else 'green'}"
|
192
|
+
# print(f"[{style}]~*^* {description} *^*~ -> {number:03d}[/{style}]")
|
189
193
|
print(f"[{style}]~*^* {description} *^*~[/{style}]")
|
190
194
|
|
191
|
-
description=''
|
195
|
+
# description=f'{number:03d}'
|
196
|
+
description = ""
|
192
197
|
|
193
198
|
return ColorProgressBar(
|
194
199
|
iterable=iterable,
|
@@ -7,12 +7,12 @@ oafuncs/oa_file.py,sha256=j9gXJgPOJsliu4IOUc4bc-luW4yBvQyNCEmMyDVjUwQ,16404
|
|
7
7
|
oafuncs/oa_help.py,sha256=_4AZgRDq5Or0vauNvq5IDDHIBoBfdOQtzak-mG1wwAw,4537
|
8
8
|
oafuncs/oa_nc.py,sha256=lLEPjj4qgdEw1al0r1nKGZUnMP_ejT8A2NKD4lrs2kc,11936
|
9
9
|
oafuncs/oa_python.py,sha256=NkopwkYFGSEuVljnTBvXCl6o2CeyRNBqRXSsUl3euEE,5192
|
10
|
-
oafuncs/oa_tool.py,sha256=
|
10
|
+
oafuncs/oa_tool.py,sha256=rpPkLqWhqMmqlCc5wjL8qMTg3gThCkSrYJckbX_0iJc,8631
|
11
11
|
oafuncs/_data/hycom.png,sha256=MadKs6Gyj5n9-TOu7L4atQfTXtF9dvN9w-tdU9IfygI,10945710
|
12
12
|
oafuncs/_data/oafuncs.png,sha256=o3VD7wm-kwDea5E98JqxXl04_78cBX7VcdUt7uQXGiU,3679898
|
13
13
|
oafuncs/_script/cprogressbar.py,sha256=UIgGcLFs-6IgWlITuBLaQqrpt4OAK3Mst5RlCiNfZdQ,15772
|
14
14
|
oafuncs/_script/email.py,sha256=lL4HGKrr524-g0xLlgs-4u7x4-u7DtgNoD9AL8XJKj4,3058
|
15
|
-
oafuncs/_script/netcdf_merge.py,sha256
|
15
|
+
oafuncs/_script/netcdf_merge.py,sha256=-Ke-Da6US0tM63Os5Uyl4fAOZ_1CNVuEpWbo_r2btGU,4299
|
16
16
|
oafuncs/_script/netcdf_modify.py,sha256=sGRUYNhfGgf9JV70rnBzw3bzuTRSXzBTL_RMDnDPeLQ,4552
|
17
17
|
oafuncs/_script/netcdf_write.py,sha256=iO1Qv9bp6RLiw1D8Nrv7tX_8X-diUZaX3Nxhk6pJ5Nw,8556
|
18
18
|
oafuncs/_script/parallel.py,sha256=T9Aie-e4LcbKlFTLZ0l4lhEN3SBVa84jRcrAsIm8s0I,8767
|
@@ -37,8 +37,8 @@ oafuncs/oa_sign/__init__.py,sha256=QKqTFrJDFK40C5uvk48GlRRbGFzO40rgkYwu6dYxatM,5
|
|
37
37
|
oafuncs/oa_sign/meteorological.py,sha256=8091SHo2L8kl4dCFmmSH5NGVHDku5i5lSiLEG5DLnOQ,6489
|
38
38
|
oafuncs/oa_sign/ocean.py,sha256=xrW-rWD7xBWsB5PuCyEwQ1Q_RDKq2KCLz-LOONHgldU,5932
|
39
39
|
oafuncs/oa_sign/scientific.py,sha256=a4JxOBgm9vzNZKpJ_GQIQf7cokkraV5nh23HGbmTYKw,5064
|
40
|
-
oafuncs-0.0.98.
|
41
|
-
oafuncs-0.0.98.
|
42
|
-
oafuncs-0.0.98.
|
43
|
-
oafuncs-0.0.98.
|
44
|
-
oafuncs-0.0.98.
|
40
|
+
oafuncs-0.0.98.14.dist-info/licenses/LICENSE.txt,sha256=rMtLpVg8sKiSlwClfR9w_Dd_5WubTQgoOzE2PDFxzs4,1074
|
41
|
+
oafuncs-0.0.98.14.dist-info/METADATA,sha256=1PncdzppTVKdYUjg_Njkg72cbwrri3GIrqexxl0_sVs,4273
|
42
|
+
oafuncs-0.0.98.14.dist-info/WHEEL,sha256=SmOxYU7pzNKBqASvQJ7DjX3XGUF92lrGhMb3R6_iiqI,91
|
43
|
+
oafuncs-0.0.98.14.dist-info/top_level.txt,sha256=bgC35QkXbN4EmPHEveg_xGIZ5i9NNPYWqtJqaKqTPsQ,8
|
44
|
+
oafuncs-0.0.98.14.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|