oafuncs 0.0.86__py2.py3-none-any.whl → 0.0.87__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/oa_help.py +21 -7
- oafuncs/oa_nc.py +109 -92
- {oafuncs-0.0.86.dist-info → oafuncs-0.0.87.dist-info}/METADATA +3 -3
- {oafuncs-0.0.86.dist-info → oafuncs-0.0.87.dist-info}/RECORD +7 -7
- {oafuncs-0.0.86.dist-info → oafuncs-0.0.87.dist-info}/LICENSE.txt +0 -0
- {oafuncs-0.0.86.dist-info → oafuncs-0.0.87.dist-info}/WHEEL +0 -0
- {oafuncs-0.0.86.dist-info → oafuncs-0.0.87.dist-info}/top_level.txt +0 -0
oafuncs/oa_help.py
CHANGED
@@ -23,8 +23,10 @@ __all__ = ["query", "use", "log"]
|
|
23
23
|
|
24
24
|
def query():
|
25
25
|
"""
|
26
|
-
|
27
|
-
|
26
|
+
Description:
|
27
|
+
Show the number of functions and the list of functions in the module.
|
28
|
+
Example:
|
29
|
+
query()
|
28
30
|
"""
|
29
31
|
funcs = [func for func in dir(oafuncs) if callable(getattr(oafuncs, func))]
|
30
32
|
print("函数数量:")
|
@@ -62,9 +64,12 @@ def _use_single(func="get_var", module="oafuncs"):
|
|
62
64
|
|
63
65
|
def use(func_name='log'):
|
64
66
|
"""
|
65
|
-
|
66
|
-
|
67
|
-
|
67
|
+
Description:
|
68
|
+
Show the full path and help of the function.
|
69
|
+
Args:
|
70
|
+
func_name: The name of the function.
|
71
|
+
Example:
|
72
|
+
use('log')
|
68
73
|
"""
|
69
74
|
found = False
|
70
75
|
# 假设oafuncs是一个包
|
@@ -105,10 +110,19 @@ def use(func_name='log'):
|
|
105
110
|
|
106
111
|
def log():
|
107
112
|
"""
|
108
|
-
|
109
|
-
|
113
|
+
Description:
|
114
|
+
Show the update log.
|
115
|
+
Example:
|
116
|
+
log()
|
110
117
|
"""
|
111
118
|
print("更新日志:")
|
119
|
+
print(
|
120
|
+
"""
|
121
|
+
2025-01-04
|
122
|
+
1. 下调Python支持版本到3.7,但未经过测试
|
123
|
+
2. 优化了部分函数说明
|
124
|
+
"""
|
125
|
+
)
|
112
126
|
print(
|
113
127
|
"""
|
114
128
|
2025-01-01
|
oafuncs/oa_nc.py
CHANGED
@@ -24,10 +24,15 @@ __all__ = ["get_var", "extract", "save", "merge", "modify", "rename", "check_fil
|
|
24
24
|
|
25
25
|
def get_var(file, *vars):
|
26
26
|
"""
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
27
|
+
Description:
|
28
|
+
Read variables from nc file
|
29
|
+
Parameters:
|
30
|
+
file: str, file path
|
31
|
+
*vars: str, variable name or variable names; should be in same size
|
32
|
+
Example:
|
33
|
+
datas = get_var(file_ecm, 'h', 't', 'u', 'v')
|
34
|
+
Return:
|
35
|
+
datas: list, variable data
|
31
36
|
"""
|
32
37
|
ds = xr.open_dataset(file)
|
33
38
|
datas = []
|
@@ -40,15 +45,15 @@ def get_var(file, *vars):
|
|
40
45
|
|
41
46
|
def extract(file, varname, only_value=True):
|
42
47
|
"""
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
48
|
+
Description:
|
49
|
+
Extract variables from nc file
|
50
|
+
Return the variable and coordinate dictionary
|
51
|
+
Parameters:
|
52
|
+
file: str, file path
|
53
|
+
varname: str, variable name
|
54
|
+
only_value: bool, whether to keep only the value of the variable and dimension
|
55
|
+
Example:
|
56
|
+
data, dimdict = extract('test.nc', 'h')
|
52
57
|
"""
|
53
58
|
ds = xr.open_dataset(file)
|
54
59
|
vardata = ds[varname]
|
@@ -103,18 +108,18 @@ def _calculate_scale_and_offset(data, n=16):
|
|
103
108
|
|
104
109
|
def save(file, data, varname=None, coords=None, mode="w", scale_offset_switch=True, compile_switch=True):
|
105
110
|
"""
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
111
|
+
Description:
|
112
|
+
Write data to NetCDF file
|
113
|
+
Parameters:
|
114
|
+
file: str, file path
|
115
|
+
data: data
|
116
|
+
varname: str, variable name
|
117
|
+
coords: dict, coordinates, key is the dimension name, value is the coordinate data
|
118
|
+
mode: str, write mode, 'w' for write, 'a' for append
|
119
|
+
scale_offset_switch: bool, whether to use scale_factor and add_offset, default is True
|
120
|
+
compile_switch: bool, whether to use compression parameters, default is True
|
121
|
+
Example:
|
122
|
+
save(r'test.nc', data, 'u', {'time': np.linspace(0, 120, 100), 'lev': np.linspace(0, 120, 50)}, 'a')
|
118
123
|
"""
|
119
124
|
# 设置压缩参数
|
120
125
|
kwargs = {"zlib": True, "complevel": 4} if compile_switch else {}
|
@@ -177,20 +182,21 @@ def save(file, data, varname=None, coords=None, mode="w", scale_offset_switch=Tr
|
|
177
182
|
|
178
183
|
def merge(file_list, var_name=None, dim_name=None, target_filename=None):
|
179
184
|
"""
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
185
|
+
Description:
|
186
|
+
Merge variables from multiple NetCDF files along a specified dimension and write to a new file.
|
187
|
+
If var_name is a string, it is considered a single variable; if it is a list and has only one element, it is also a single variable;
|
188
|
+
If the list has more than one element, it is a multi-variable; if var_name is None, all variables are merged.
|
189
|
+
|
190
|
+
Parameters:
|
191
|
+
file_list: List of NetCDF file paths
|
192
|
+
var_name: Name of the variable to be extracted or a list of variable names, default is None, which means all variables are extracted
|
193
|
+
dim_name: Dimension name used for merging
|
194
|
+
target_filename: Target file name after merging
|
195
|
+
|
196
|
+
Example:
|
197
|
+
merge(file_list, var_name='u', dim_name='time', target_filename='merged.nc')
|
198
|
+
merge(file_list, var_name=['u', 'v'], dim_name='time', target_filename='merged.nc')
|
199
|
+
merge(file_list, var_name=None, dim_name='time', target_filename='merged.nc')
|
194
200
|
"""
|
195
201
|
if isinstance(file_list, str):
|
196
202
|
file_list = [file_list]
|
@@ -249,15 +255,17 @@ def merge(file_list, var_name=None, dim_name=None, target_filename=None):
|
|
249
255
|
|
250
256
|
|
251
257
|
def _modify_var(nc_file_path, variable_name, new_value):
|
252
|
-
"""
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
258
|
+
"""
|
259
|
+
Description:
|
260
|
+
Modify the value of a variable in a NetCDF file using the netCDF4 library.
|
261
|
+
|
262
|
+
Parameters:
|
263
|
+
nc_file_path (str): The path to the NetCDF file.
|
264
|
+
variable_name (str): The name of the variable to be modified.
|
265
|
+
new_value (numpy.ndarray): The new value of the variable.
|
266
|
+
|
267
|
+
Example:
|
268
|
+
modify_var('test.nc', 'u', np.random.rand(100, 50))
|
261
269
|
"""
|
262
270
|
try:
|
263
271
|
# Open the NetCDF file
|
@@ -273,15 +281,18 @@ def _modify_var(nc_file_path, variable_name, new_value):
|
|
273
281
|
|
274
282
|
|
275
283
|
def _modify_attr(nc_file_path, variable_name, attribute_name, attribute_value):
|
276
|
-
"""
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
284
|
+
"""
|
285
|
+
Description:
|
286
|
+
Add or modify an attribute of a variable in a NetCDF file using the netCDF4 library.
|
287
|
+
|
288
|
+
Parameters:
|
289
|
+
nc_file_path (str): The path to the NetCDF file.
|
290
|
+
variable_name (str): The name of the variable to be modified.
|
291
|
+
attribute_name (str): The name of the attribute to be added or modified.
|
292
|
+
attribute_value (any): The value of the attribute.
|
293
|
+
|
294
|
+
Example:
|
295
|
+
modify_attr('test.nc', 'temperature', 'long_name', 'Temperature in Celsius')
|
285
296
|
"""
|
286
297
|
try:
|
287
298
|
ds = nc.Dataset(nc_file_path, "r+")
|
@@ -303,15 +314,18 @@ def _modify_attr(nc_file_path, variable_name, attribute_name, attribute_value):
|
|
303
314
|
|
304
315
|
def modify(nc_file,var_name,attr_name=None,new_value=None):
|
305
316
|
"""
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
317
|
+
Description:
|
318
|
+
Modify the value of a variable or the value of an attribute in a NetCDF file.
|
319
|
+
|
320
|
+
Parameters:
|
321
|
+
nc_file (str): The path to the NetCDF file.
|
322
|
+
var_name (str): The name of the variable to be modified.
|
323
|
+
attr_name (str): The name of the attribute to be modified. If None, the variable value will be modified.
|
324
|
+
new_value (any): The new value of the variable or attribute.
|
325
|
+
|
326
|
+
Example:
|
327
|
+
modify('test.nc', 'temperature', 'long_name', 'Temperature in Celsius')
|
328
|
+
modify('test.nc', 'temperature', None, np.random.rand(100, 50))
|
315
329
|
"""
|
316
330
|
if attr_name is None:
|
317
331
|
_modify_var(nc_file, var_name, new_value)
|
@@ -321,14 +335,16 @@ def modify(nc_file,var_name,attr_name=None,new_value=None):
|
|
321
335
|
|
322
336
|
def rename(ncfile_path, old_name, new_name):
|
323
337
|
"""
|
324
|
-
|
338
|
+
Description:
|
339
|
+
Rename a variable and/or dimension in a NetCDF file.
|
325
340
|
|
326
341
|
Parameters:
|
327
|
-
|
328
|
-
|
329
|
-
|
342
|
+
ncfile_path (str): The path to the NetCDF file.
|
343
|
+
old_name (str): The current name of the variable or dimension.
|
344
|
+
new_name (str): The new name to assign to the variable or dimension.
|
330
345
|
|
331
|
-
example:
|
346
|
+
example:
|
347
|
+
rename('test.nc', 'temperature', 'temp')
|
332
348
|
"""
|
333
349
|
try:
|
334
350
|
with nc.Dataset(ncfile_path, "r+") as dataset:
|
@@ -355,14 +371,15 @@ def rename(ncfile_path, old_name, new_name):
|
|
355
371
|
|
356
372
|
def check_file(ncfile, if_delete=False):
|
357
373
|
'''
|
358
|
-
Description:
|
359
|
-
|
374
|
+
Description:
|
375
|
+
Check if the NetCDF file is corrupted.
|
376
|
+
|
360
377
|
Parameters:
|
361
|
-
ncfile
|
362
|
-
if_delete:
|
363
|
-
|
364
|
-
|
365
|
-
|
378
|
+
ncfile (str): The path to the NetCDF file.
|
379
|
+
if_delete (bool): Whether to delete the file if it is corrupted, default is False.
|
380
|
+
|
381
|
+
Returns:
|
382
|
+
bool: True if the file is not corrupted, False otherwise.
|
366
383
|
'''
|
367
384
|
if not os.path.exists(ncfile):
|
368
385
|
return False
|
@@ -393,15 +410,16 @@ def check_file(ncfile, if_delete=False):
|
|
393
410
|
|
394
411
|
def convert_longitude(ds, lon_name="longitude", convert=180):
|
395
412
|
"""
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
413
|
+
Description:
|
414
|
+
Convert the longitude array to a specified range.
|
415
|
+
|
416
|
+
Parameters:
|
417
|
+
ds (xarray.Dataset): The xarray dataset containing the longitude data.
|
418
|
+
lon_name (str): The name of the longitude variable, default is "longitude".
|
419
|
+
convert (int): The target range to convert to, can be 180 or 360, default is 180.
|
420
|
+
|
421
|
+
Returns:
|
422
|
+
xarray.Dataset: The xarray dataset with the converted longitude.
|
405
423
|
"""
|
406
424
|
to_which = int(convert)
|
407
425
|
if to_which not in [180, 360]:
|
@@ -417,18 +435,17 @@ def convert_longitude(ds, lon_name="longitude", convert=180):
|
|
417
435
|
|
418
436
|
def isel(ncfile, dim_name, slice_list):
|
419
437
|
"""
|
420
|
-
Description:
|
438
|
+
Description:
|
439
|
+
Choose the data by the index of the dimension
|
421
440
|
|
422
441
|
Parameters:
|
423
442
|
ncfile: str, the path of the netCDF file
|
424
443
|
dim_name: str, the name of the dimension
|
425
444
|
slice_list: list, the index of the dimension
|
426
445
|
|
427
|
-
slice_list example: slice_list = [[y*12+m for m in range(11,14)] for y in range(84)]
|
428
|
-
or
|
429
|
-
slice_list = [y * 12 + m for y in range(84) for m in range(11, 14)]
|
430
|
-
|
431
446
|
Example:
|
447
|
+
slice_list = [[y*12+m for m in range(11,14)] for y in range(84)]
|
448
|
+
slice_list = [y * 12 + m for y in range(84) for m in range(11, 14)]
|
432
449
|
isel(ncfile, 'time', slice_list)
|
433
450
|
"""
|
434
451
|
ds = xr.open_dataset(ncfile)
|
@@ -1,7 +1,7 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: oafuncs
|
3
|
-
Version: 0.0.
|
4
|
-
Summary:
|
3
|
+
Version: 0.0.87
|
4
|
+
Summary: Oceanic and Atmospheric Functions
|
5
5
|
Home-page: https://github.com/Industry-Pays/OAFuncs
|
6
6
|
Author: Kun Liu
|
7
7
|
Author-email: liukun0312@stu.ouc.edu.cn
|
@@ -15,7 +15,7 @@ Classifier: Programming Language :: Python :: 3.11
|
|
15
15
|
Classifier: Programming Language :: Python :: 3.12
|
16
16
|
Classifier: Programming Language :: Python :: Implementation :: CPython
|
17
17
|
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
18
|
-
Requires-Python: >=3.
|
18
|
+
Requires-Python: >=3.7.0
|
19
19
|
Description-Content-Type: text/markdown
|
20
20
|
License-File: LICENSE.txt
|
21
21
|
Requires-Dist: matplotlib
|
@@ -3,8 +3,8 @@ oafuncs/oa_cmap.py,sha256=azVg9QR_IlG9lXCCXXVs1LS1kFci8yjxDmb_VA_TdTQ,7408
|
|
3
3
|
oafuncs/oa_data.py,sha256=2MkWCsESVPjZPnLnbQIwrBJUhcF9x-WXbZhni3I44ss,15617
|
4
4
|
oafuncs/oa_draw.py,sha256=PUk8DdGBUapCXrOVQ-d_DD6EccB_eHAX3r6jwrnQTk8,11126
|
5
5
|
oafuncs/oa_file.py,sha256=-AdRnFEtRTpIMXr5geYe5fEhyiMOFad5ethi_lF-Ogo,12277
|
6
|
-
oafuncs/oa_help.py,sha256=
|
7
|
-
oafuncs/oa_nc.py,sha256=
|
6
|
+
oafuncs/oa_help.py,sha256=DsX6rFPZ-ZMaexcyCXeGeB3h1zPScY6ijH-uLNVWidA,3988
|
7
|
+
oafuncs/oa_nc.py,sha256=b8pbiPSRDi7Te42UcnR0AlW1sQEVtn5VU9ERiAdX-yY,17501
|
8
8
|
oafuncs/oa_python.py,sha256=XPTP3o7zTFzfJR_YhsKfQksa3bSYwXsne9YxlJplCEA,3994
|
9
9
|
oafuncs/data_store/OAFuncs.png,sha256=w2pR7MUyeeWrT8BVTSy40EIRDIrfpdo1QvWvvjLOgjM,3258809
|
10
10
|
oafuncs/oa_down/User_Agent-list.txt,sha256=pazxSip8_lphEBOPHG902zmIBUg8sBKXgmqp_g6j_E4,661062
|
@@ -19,8 +19,8 @@ oafuncs/oa_sign/ocean.py,sha256=xrW-rWD7xBWsB5PuCyEwQ1Q_RDKq2KCLz-LOONHgldU,5932
|
|
19
19
|
oafuncs/oa_sign/scientific.py,sha256=a4JxOBgm9vzNZKpJ_GQIQf7cokkraV5nh23HGbmTYKw,5064
|
20
20
|
oafuncs/oa_tool/__init__.py,sha256=IKOlqpWlb4cMDCtq2VKR_RTxQHDNqR_vfqqsOsp_lKQ,466
|
21
21
|
oafuncs/oa_tool/email.py,sha256=4lJxV_KUzhxgLYfVwYTqp0qxRugD7fvsZkXDe5WkUKo,3052
|
22
|
-
oafuncs-0.0.
|
23
|
-
oafuncs-0.0.
|
24
|
-
oafuncs-0.0.
|
25
|
-
oafuncs-0.0.
|
26
|
-
oafuncs-0.0.
|
22
|
+
oafuncs-0.0.87.dist-info/LICENSE.txt,sha256=rMtLpVg8sKiSlwClfR9w_Dd_5WubTQgoOzE2PDFxzs4,1074
|
23
|
+
oafuncs-0.0.87.dist-info/METADATA,sha256=s_MdtiHoS3sPMPmK2J1se2gWXs34fA2tHSjQ4NqH-cs,3300
|
24
|
+
oafuncs-0.0.87.dist-info/WHEEL,sha256=pxeNX5JdtCe58PUSYP9upmc7jdRPgvT0Gm9kb1SHlVw,109
|
25
|
+
oafuncs-0.0.87.dist-info/top_level.txt,sha256=bgC35QkXbN4EmPHEveg_xGIZ5i9NNPYWqtJqaKqTPsQ,8
|
26
|
+
oafuncs-0.0.87.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|