oafuncs 0.0.59__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.
@@ -0,0 +1,913 @@
1
+ Metadata-Version: 2.1
2
+ Name: oafuncs
3
+ Version: 0.0.59
4
+ Summary: My short description for my project.
5
+ Home-page: https://github.com/Industry-Pays/oafuncs
6
+ Author: Kun Liu
7
+ Author-email: liukun0312@stu.ouc.edu.cn
8
+ License: MIT
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Programming Language :: Python
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3.9
13
+ Classifier: Programming Language :: Python :: 3.10
14
+ Classifier: Programming Language :: Python :: 3.11
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Programming Language :: Python :: 3.13
17
+ Classifier: Programming Language :: Python :: Implementation :: CPython
18
+ Classifier: Programming Language :: Python :: Implementation :: PyPy
19
+ Requires-Python: >=3.9.0
20
+ Description-Content-Type: text/markdown
21
+ License-File: LICENSE.txt
22
+ Requires-Dist: matplotlib
23
+ Requires-Dist: numpy
24
+ Requires-Dist: scipy
25
+ Requires-Dist: xarray
26
+ Requires-Dist: Cartopy
27
+ Requires-Dist: netCDF4
28
+ Requires-Dist: requests
29
+ Requires-Dist: bs4
30
+ Requires-Dist: rich
31
+ Requires-Dist: pathlib
32
+ Requires-Dist: pandas
33
+
34
+
35
+ # oafuncs
36
+
37
+ ## Description
38
+
39
+ Python Function
40
+
41
+ 地学领域,一些常用操作!
42
+
43
+ 只是方便日常使用,将一些复杂的操作集成到通用函数
44
+
45
+ 代码会不定期优化更新,或增或删或改...
46
+
47
+ 已有功能不会完全删除,可能是换了个函数名,或者优化了参数传递等...
48
+
49
+ <mark>注</mark>:若有需求,可邮件至`liukun0312@stu.ouc.edu.cn`,能力范围内可考虑实现
50
+
51
+ **精力有限,部分函数使用说明文档未能及时更新/添加,可参考`结构`部分简述,再使用`OAFuncs.oa_help.use('func_name')`函数调取函数本身说明**
52
+
53
+ ## PyPI
54
+
55
+ ```html
56
+ https://pypi.org/project/OAFuncs
57
+ ```
58
+
59
+ ## Github
60
+
61
+ ```html
62
+ https://github.com/Industry-Pays/OAFuncs
63
+ ```
64
+
65
+ ## Example
66
+
67
+ ```python
68
+ import oafuncs
69
+
70
+ # oafuncs.oa_cmap.*
71
+ # oafuncs.oa_data.*
72
+ # oafuncs.oa_draw.*
73
+ # oafuncs.oa_file.*
74
+ # oafuncs.oa_nc.*
75
+ # oafuncs.oa_help.*
76
+ # oafuncs.oa_sign.*
77
+ # oafuncs.oa_down.*
78
+
79
+ # 查询当前所有可用函数
80
+ oafuncs.oa_help.query()
81
+ # 根据函数名获取使用方法
82
+ oafuncs.oa_help.use('get_var')
83
+ ```
84
+
85
+ ```shell
86
+ # 此小板块于2024/10/28更新,仅为示例,不代表最新情况
87
+ 函数数量:
88
+ 32
89
+ 函数列表:
90
+ ['clear_folder', 'copy_file', 'create_5rgb_txt', 'create_custom', 'create_diverging', 'create_gif', 'extract5nc', 'extract_colors', 'get_var', 'install_lib', 'interp_2d', 'interp_2d_parallel', 'link_file', 'make_folder', 'merge5nc', 'mod_var_attr', 'plot_contourf', 'plot_contourf_cartopy', 'plot_contourf_lonlat', 'plot_quiver', 'query', 'remove', 'remove_empty_folders', 'rename_files', 'show', 'sign_in_love_ocean', 'sign_in_meteorological_home', 'sign_in_scientific_research', 'upgrade_lib', 'use', 'write2nc', 'xy2lonlat']
91
+ 模块全路径:
92
+ oafuncs.oa_nc.get_var
93
+ 函数提示:
94
+ datas = get_var(file_ecm, 'h', 't', 'u', 'v')
95
+
96
+ # 实际全路径:
97
+ datas = oafuncs.oa_nc.get_var(file_ecm, 'h', 't', 'u', 'v')
98
+ ```
99
+
100
+ ```python
101
+ import numpy as np
102
+ from oafuncs import oa_nc
103
+ # or
104
+ importoafuncs
105
+
106
+ data = np.random.rand(100, 50)
107
+ oa_nc.write2nc(r'I:\test.nc', data,
108
+ 'data', {'time': np.linspace(0, 120, 100), 'lev': np.linspace(0, 120, 50)}, 'a')
109
+
110
+ # or
111
+ oafuncs.oa_nc.write2nc(r'I:\test.nc', data,
112
+ 'data', {'time': np.linspace(0, 120, 100), 'lev': np.linspace(0, 120, 50)}, 'a')
113
+ ```
114
+
115
+ ## 结构
116
+
117
+ - **oafuncs**
118
+
119
+ - oa_down
120
+
121
+ - hycom_3hourly
122
+
123
+ - how_to_use
124
+
125
+ 2024/11/02更新
126
+
127
+ 查看如何使用本板块,主要是开发者使用
128
+
129
+ - draw_time_range
130
+
131
+ 2024/11/02更新
132
+
133
+ 绘制hycom数据集时间分布
134
+
135
+ - download
136
+
137
+ 2024/11/02更新
138
+
139
+ 下载hycom数据,3h分辨率
140
+
141
+ - get_time_list
142
+
143
+ 2024/11/12更新
144
+
145
+ 方便获取时间序列,间隔为hour
146
+
147
+ - refs_pdf
148
+
149
+ - download5doi
150
+
151
+ 2024/11/09更新
152
+
153
+ 根据doi下载文献pdf
154
+
155
+ - oa_sign
156
+
157
+ - meteorological
158
+
159
+ - sign_in_meteorological_home
160
+
161
+ 2024/10/28更新
162
+
163
+ 气象家园签到
164
+
165
+ - ocean
166
+
167
+ - sign_in_love_ocean
168
+
169
+ 2024/10/28更新
170
+
171
+ 吾爱海洋签到
172
+
173
+ - scientific
174
+
175
+ - sign_in_scientific_research
176
+
177
+ 2024/10/28更新
178
+
179
+ 科研通签到
180
+
181
+ - oa_cmap
182
+
183
+ - show
184
+
185
+ 2024/10/28更新
186
+
187
+ 展示cmap效果
188
+
189
+ - extract_colors
190
+
191
+ 2024/10/28更新
192
+
193
+ 将cmap拆分成颜色列表
194
+
195
+ - create_custom
196
+
197
+ 2024/10/28更新
198
+
199
+ 自定义cmap,可提供颜色位置
200
+
201
+ - create_diverging
202
+
203
+ 2024/10/28更新
204
+
205
+ 等比例两端型cmap
206
+
207
+ - create_5rgb_txt
208
+
209
+ 2024/10/28更新
210
+
211
+ 基于RGB文件制作cmap
212
+
213
+ - oa_data
214
+
215
+ - interp_2d
216
+
217
+ 2024/10/28更新
218
+
219
+ 二维插值
220
+
221
+ - interp_2d_parallel
222
+
223
+ 2024/10/28更新
224
+
225
+ 二维插值,并行加速
226
+
227
+ - oa_draw
228
+
229
+ - create_gif
230
+
231
+ 2024/10/28更新
232
+
233
+ 制作动图
234
+
235
+ - xy2lonlat
236
+
237
+ 2024/10/28更新
238
+
239
+ 将数字转化为经/纬度字符串
240
+
241
+ - plot_contourf
242
+
243
+ 2024/10/28更新
244
+
245
+ 粗略绘制填色图
246
+
247
+ - plot_contourf_lonlat
248
+
249
+ 2024/10/28更新
250
+
251
+ 填色图叠加经纬度
252
+
253
+ - plot_contourf_cartopy
254
+
255
+ 2024/10/28更新
256
+
257
+ 填色图加海陆线
258
+
259
+ - plot_quiver
260
+
261
+ 2024/10/28更新
262
+
263
+ 矢量图
264
+
265
+ - oa_file
266
+
267
+ - link_file
268
+
269
+ 2024/10/28更新
270
+
271
+ 链接文件
272
+
273
+ - copy_file
274
+
275
+ 2024/10/28更新
276
+
277
+ 复制文件
278
+
279
+ - rename_files
280
+
281
+ 2024/10/28更新
282
+
283
+ 按一定规则重命名文件(可多个)
284
+
285
+ - make_folder
286
+
287
+ 2024/10/28更新
288
+
289
+ 在指定路径下创建文件夹
290
+
291
+ - clear_folder
292
+
293
+ 2024/10/28更新
294
+
295
+ 清空文件夹
296
+
297
+ - remove_empty_folders
298
+
299
+ 2024/10/28更新
300
+
301
+ 删除路径下所有空的文件夹
302
+
303
+ - remove
304
+
305
+ 2024/10/28更新
306
+
307
+ 删除文件/文件夹
308
+
309
+ - file_size
310
+
311
+ 2024/11/11更新
312
+
313
+ 获取文件大小,自选单位
314
+
315
+ - oa_help
316
+
317
+ - query
318
+
319
+ 2024/10/28更新
320
+
321
+ 查询本库所有可用函数
322
+
323
+ - use
324
+
325
+ 2024/10/28更新
326
+
327
+ 获取函数路径、函数说明
328
+
329
+ - oa_nc
330
+
331
+ - get_var
332
+
333
+ 2024/10/28更新
334
+
335
+ 批量提取nc文件变量
336
+
337
+ - extract5nc
338
+
339
+ 2024/10/28更新
340
+
341
+ 从nc文件中提取变量,包含所有坐标值
342
+
343
+ - write2nc
344
+
345
+ 2024/10/28更新
346
+
347
+ 便捷将数据写入nc文件
348
+
349
+ - merge5nc
350
+
351
+ 2024/10/28更新
352
+
353
+ 合并nc文件中的某一变量
354
+
355
+ - merge5nc_vars
356
+
357
+ 2024/10/28更新
358
+
359
+ 合并nc文件中多个变量,按照同一坐标维度
360
+
361
+ - modify_var_value
362
+
363
+ 2024/10/29更新
364
+
365
+ 修改变量值
366
+
367
+ - modify_var_attr
368
+
369
+ 2024/10/29更新
370
+
371
+ 添加或修改nc文件变量属性
372
+
373
+ - rename_var_or_dim
374
+
375
+ 2024/10/29更新
376
+
377
+ 重命名变量或维度名,如果该名称涉及维度,会自动修改
378
+
379
+ - check_ncfile
380
+
381
+ 2024/11/05更新
382
+
383
+ 检查nc文件是否存在/是否有问题,可选删除
384
+
385
+ ## 依赖库
386
+
387
+ ```shell
388
+ "matplotlib"
389
+ "numpy"
390
+ "scipy"
391
+ "xarray"
392
+ "Cartopy"
393
+ "netCDF4"
394
+ ```
395
+
396
+ ------------------------------------------------------------------------------------------------------------------------------
397
+
398
+ <mark>Note</mark>:**以下内容暂不想再更新,写文件太费精力,使用oafuncs.oa_help.use('func_name')方式获取说明吧~~**
399
+
400
+ 2024/11/02
401
+
402
+ ----------------------------------------
403
+
404
+ ## 1 `oa_cmap`
405
+
406
+ ### 1.1 description
407
+
408
+ 针对cmap相关操作写了一些函数,可以生成cmap,简单可视化,以及将cmap拆分成颜色序列等等。
409
+
410
+ ### 1.2 `show(colormaps: list)`
411
+
412
+ #### 描述
413
+
414
+ 帮助函数,用于绘制与给定颜色映射(colormap)关联的数据。
415
+
416
+ #### 参数
417
+
418
+ - `colormaps` (list): 颜色映射列表。
419
+
420
+ #### 示例
421
+
422
+ ```python
423
+ cmap = mpl.colors.ListedColormap(["darkorange", "gold", "lawngreen", "lightseagreen"])
424
+ show([cmap])
425
+ ```
426
+
427
+ ### 1.3 `extract_colors(cmap, n=256)`
428
+
429
+ #### 描述
430
+
431
+ 将颜色映射(cmap)转换为颜色列表。
432
+
433
+ #### 参数
434
+
435
+ - `cmap` (str): 颜色映射名称。
436
+
437
+ - `n` (int): 颜色分割的数量,默认为256。
438
+
439
+ #### 返回
440
+
441
+ - `out_cmap` (list): 颜色列表。
442
+
443
+ #### 示例
444
+
445
+ ```python
446
+ out_cmap = extract_colors('viridis', 256)
447
+ ```
448
+
449
+ ### 1.4 `create_custom(colors: list, nodes=None)`
450
+
451
+ #### 描述
452
+
453
+ 创建自定义颜色映射(cmap),可以自动确定颜色位置(等比例)。
454
+
455
+ #### 参数
456
+
457
+ - `colors` (list): 颜色列表,可以是颜色名称或十六进制颜色代码。
458
+
459
+ - `nodes` (list, optional): 颜色位置列表,默认为None,表示等间距。
460
+
461
+ #### 返回
462
+
463
+ - `c_map` (matplotlib.colors.LinearSegmentedColormap): 自定义颜色映射。
464
+
465
+ #### 示例
466
+
467
+ ```python
468
+ c_map = create_custom(['#C2B7F3','#B3BBF2','#B0CBF1','#ACDCF0','#A8EEED'])
469
+ c_map = create_custom(['aliceblue','skyblue','deepskyblue'], [0.0, 0.5, 1.0])
470
+ ```
471
+
472
+ ### 1.5 `create_diverging(colors: list)`
473
+
474
+ #### 描述
475
+
476
+ 创建双色diverging型颜色映射(cmap),当传入颜色为偶数时,默认中间为白色。
477
+
478
+ #### 参数
479
+
480
+ - `colors` (list): 颜色列表,可以是颜色名称或十六进制颜色代码。
481
+
482
+ #### 返回
483
+
484
+ - `cmap_color` (matplotlib.colors.LinearSegmentedColormap): 自定义diverging型颜色映射。
485
+
486
+ #### 示例
487
+
488
+ ```python
489
+ diverging_cmap = create_diverging(
490
+ ["#4e00b3", "#0000FF", "#00c0ff", "#a1d3ff", "#DCDCDC", "#FFD39B", "#FF8247", "#FF0000", "#FF5F9E"])
491
+ ```
492
+
493
+ ### 1.6 `create_5rgb_txt(rgb_txt_filepath: str)`
494
+
495
+ #### 描述
496
+
497
+ 根据RGB的txt文档制作色卡。
498
+
499
+ #### 参数
500
+
501
+ - `rgb_txt_filepath` (str): RGB txt文件的路径。
502
+
503
+ #### 返回
504
+
505
+ - `icmap` (matplotlib.colors.ListedColormap): 根据RGB值创建的颜色映射。
506
+
507
+ #### 示例
508
+
509
+ ```python
510
+ cmap_color = create_5rgb_txt('./test.txt')
511
+ ```
512
+
513
+ RGB.txt格式:
514
+
515
+ ```textile
516
+ 50,54,156
517
+ ...
518
+ 255,255,255
519
+ ```
520
+
521
+ ## 2 oa_data
522
+
523
+ ### 2.1 description
524
+
525
+ 对数据进行处理,目前主要提供二维及以上数据的水平二维插值。(2D~4D)
526
+
527
+ ### 2.2 `interp_2d(target_x, target_y, origin_x, origin_y, data, method='linear')`
528
+
529
+ #### 描述
530
+
531
+ 高维插值函数,默认对数据的最后两个维度进行插值。该函数适用于二维~四维空间数据的插值,不使用并行计算。
532
+
533
+ #### 参数
534
+
535
+ - `target_x` (array-like): 目标纬度网格,可以是1D或2D数组。
536
+ - `target_y` (array-like): 目标经度网格,可以是1D或2D数组。
537
+ - `origin_x` (array-like): 初始纬度网格,与原始数据形状匹配的1D或2D数组。
538
+ - `origin_y` (array-like): 初始经度网格,与原始数据形状匹配的1D或2D数组。
539
+ - `data` (array-like): 待插值的数据,形状为(*, lat, lon),其中`*`可以是任意1~2D。
540
+ - `method` (str, optional): 插值方法,默认为'linear'。可选值还包括'nearest', 'cubic'等。
541
+
542
+ #### 返回
543
+
544
+ - `array-like`: 插值结果,形状与`target_x`和`target_y`构成的网格形状一致,并且保持原始数据除了最后两个维度之外的其他维度结构。
545
+
546
+ #### 示例
547
+
548
+ ```python
549
+ import numpy as np
550
+ import matplotlib.pyplot as plt
551
+
552
+ # 创建初始网格和数据
553
+ origin_x = np.linspace(0, 10, 11)
554
+ origin_y = np.linspace(0, 10, 11)
555
+ data = np.random.rand(10, 10, 11, 11)
556
+
557
+ # 创建目标网格
558
+ target_x = np.linspace(0, 10, 101)
559
+ target_y = np.linspace(0, 10, 101)
560
+
561
+ # 执行插值
562
+ interpolated_data = interp_2d(target_x, target_y, origin_x, origin_y, data)
563
+
564
+ # 打印插值结果形状
565
+ print(interpolated_data.shape)
566
+
567
+ # 可视化插值结果
568
+ plt.figure()
569
+ plt.contourf(target_x, target_y, interpolated_data[0, 0, :, :])
570
+ plt.colorbar()
571
+ plt.show()
572
+ ```
573
+
574
+ ### 2.3 `interp_2d_parallel(target_x, target_y, origin_x, origin_y, data, method='linear')`
575
+
576
+ #### 描述
577
+
578
+ 高维插值函数,使用多线程加速插值过程。该函数默认对数据的最后两个维度进行插值,适用于二维到四维空间数据的插值。通过`ThreadPoolExecutor`来并行处理数据的不同切片,以提升计算效率。
579
+
580
+ #### 参数
581
+
582
+ - `target_x` (array-like): 目标纬度网格,可以是1D或2D数组。
583
+ - `target_y` (array-like): 目标经度网格,可以是1D或2D数组。
584
+ - `origin_x` (array-like): 初始纬度网格,与原始数据形状匹配的1D或2D数组。
585
+ - `origin_y` (array-like): 初始经度网格,与原始数据形状匹配的1D或2D数组。
586
+ - `data` (array-like): 待插值的数据,形状为(*, lat, lon),其中`*`可以是任意维度。
587
+ - `method` (str, optional): 插值方法,默认为'linear'。可选值还包括'nearest', 'cubic'等。
588
+
589
+ #### 返回
590
+
591
+ - `array-like`: 插值结果,形状与`target_x`和`target_y`构成的网格形状一致,并且保持原始数据除了最后两个维度之外的其他维度结构。
592
+
593
+ #### 示例
594
+
595
+ ```python
596
+ import numpy as np
597
+ import matplotlib.pyplot as plt
598
+
599
+ # 创建初始网格和数据
600
+ origin_x = np.linspace(0, 10, 11)
601
+ origin_y = np.linspace(0, 10, 11)
602
+ data = np.random.rand(10, 10, 11, 11)
603
+
604
+ # 创建目标网格
605
+ target_x = np.linspace(0, 10, 101)
606
+ target_y = np.linspace(0, 10, 101)
607
+
608
+ # 执行插值
609
+ interpolated_data = interp_2d_parallel(target_x, target_y, origin_x, origin_y, data)
610
+
611
+ # 打印插值结果形状
612
+ print(interpolated_data.shape)
613
+
614
+ # 可视化插值结果
615
+ plt.figure()
616
+ plt.contourf(target_x, target_y, interpolated_data[0, 0, :, :])
617
+ plt.colorbar()
618
+ plt.show()
619
+ ```
620
+
621
+ ## 3 oa_draw
622
+
623
+ ### 3.1 description
624
+
625
+ 一些简单的绘图函数,由于绘图需要高度自定义,所以这部分仅作为速览。
626
+
627
+ ### 3.2 等待优化再写
628
+
629
+ 目前已有部分函数,可自行尝试
630
+
631
+ ```python
632
+ create_gif(image_list: list, gif_name: str, duration=0.2)
633
+
634
+
635
+ xy2lonlat(xy, lonlat='lon', decimal=2)
636
+
637
+
638
+ plot_contourf(pic_data, picname=None, c_map='rainbow', minmax=None, labels=None, ticks_space=None, ticks=None, figsize=(12, 9))
639
+
640
+
641
+ plot_contourf_lonlat(data, lon, lat, interval=5, picname=None, c_map='rainbow')
642
+
643
+
644
+ plot_quiver(u, v, lon, lat, picname=None, cmap='coolwarm', scale=0.25, width=0.002, x_space=5, y_space=5)
645
+
646
+
647
+ plot_contourf_cartopy(data, lon, lat, picname=None, cmap='rainbow', cn_fill_num=20, fig_size=(12, 9), title='Cartopy', land_color='green', ocean_color='lightgrey')
648
+ ```
649
+
650
+ ## 4 oa_file
651
+
652
+ ### 4.1 description
653
+
654
+ 对文件进行一些处理,包含文件夹、文件等处理。
655
+
656
+ ### 4.2 `link_file(src_pattern, dst)`
657
+
658
+ #### 描述
659
+
660
+ 创建符号链接,支持通配符匹配源文件。
661
+
662
+ #### 参数
663
+
664
+ - `src_pattern`: 源文件路径,支持通配符。
665
+ - `dst`: 目标路径或文件名。
666
+
667
+ #### 返回
668
+
669
+ 无返回值
670
+
671
+ #### 示例
672
+
673
+ ```python
674
+ link_file(r'/data/hejx/liukun/era5/*', r'/data/hejx/liukun/Test/')
675
+ link_file(r'/data/hejx/liukun/era5/py.o*', r'/data/hejx/liukun/Test/py.o')
676
+ link_file(r'/data/hejx/liukun/era5/py.o*', r'/data/hejx/liukun/Test')
677
+ ```
678
+
679
+ ### 4.3 `copy_file(src_pattern, dst)`
680
+
681
+ #### 描述
682
+
683
+ 复制文件或目录,支持使用通配符匹配源文件。
684
+
685
+ #### 参数
686
+
687
+ - `src_pattern`: 源文件或目录的路径,可以包含通配符来匹配多个文件。
688
+ - `dst`: 目标路径或文件名。如果指定的是目录路径,则保持源文件的文件名;如果指定的是文件名,则会覆盖同名文件。
689
+
690
+ #### 返回
691
+
692
+ 无返回值
693
+
694
+ #### 示例
695
+
696
+ 复制单个文件到指定目录,保持原文件名:
697
+
698
+ ```python
699
+ copy_file(r'/path/to/source/file.txt', r'/path/to/destination/')
700
+ ```
701
+
702
+ ### 4.4 `rename_files(directory, old_str, new_str)`
703
+
704
+ #### 描述
705
+
706
+ 在指定目录下批量重命名文件,将文件名中的指定字符串替换为新的字符串。
707
+
708
+ #### 参数
709
+
710
+ - `directory`:需要操作的目录路径,字符串类型。
711
+ - `old_str`:需要被替换的旧字符串,字符串类型。
712
+ - `new_str`:用于替换旧字符串的新字符串,字符串类型。
713
+
714
+ #### 返回
715
+
716
+ 该函数没有返回值,但会直接修改指定目录下的文件名。
717
+
718
+ #### 示例
719
+
720
+ ```python
721
+ # 调用函数
722
+ directory_path = "E:\\Code\\Matlab\\Master\\Ocean\\ROMS\\CROCO-1.3.1\\My_Models\\windfarm\\CROCO_FILES"
723
+ old_str = "croco"
724
+ new_str = "roms"
725
+ rename_files(directory_path, old_str, new_str)
726
+ ```
727
+
728
+ ### 4.5 `make_folder(rootpath: str, folder_name: str, clear=0)`
729
+
730
+ #### 描述
731
+
732
+ 创建一个新文件夹在指定的根路径下,如果文件夹已存在,可以选择是否清除其内容。函数返回创建的文件夹路径。
733
+
734
+ #### 参数
735
+
736
+ - `rootpath`:新文件夹的根路径,字符串类型。
737
+ - `folder_name`:要创建的文件夹名称,字符串类型。
738
+ - `clear`:可选参数,默认为0。如果设置为1,且文件夹已存在,则将其内容清除。
739
+
740
+ #### 返回
741
+
742
+ 函数返回创建或清理后的文件夹路径,字符串类型。
743
+
744
+ #### 示例
745
+
746
+ ```python
747
+ # 调用函数
748
+ root_path = "/path/to/root"
749
+ folder_name = "new_folder"
750
+ created_folder_path = make_folder(root_path, folder_name, clear=1)
751
+ print(f"Folder created at: {created_folder_path}")
752
+ ```
753
+
754
+ ### 4.6 `clear_folder(folder_path)`
755
+
756
+ #### 描述
757
+
758
+ 清空指定文件夹中的所有内容,包括文件、子文件夹以及符号链接。如果文件夹不存在,则不会有任何操作。
759
+
760
+ #### 参数
761
+
762
+ - `folder_path`:需要清空的文件夹路径,字符串类型。
763
+
764
+ #### 返回
765
+
766
+ 该函数没有返回值
767
+
768
+ #### 示例
769
+
770
+ ```python
771
+ # 调用函数
772
+ folder_to_clear = "/path/to/folder"
773
+ clear_folder(folder_to_clear)
774
+ ```
775
+
776
+ ### 4.7 `remove_empty_folders(path, print_info=1)`
777
+
778
+ #### 描述
779
+
780
+ 遍历指定路径下的所有文件夹,并删除空的文件夹。该函数会递归地检查每个子文件夹,如果文件夹为空,则将其删除。
781
+
782
+ #### 参数
783
+
784
+ - `path`:需要检查和删除空文件夹的路径,字符串类型。
785
+ - `print_info`:可选参数,默认为1。如果设置为1,则在删除空文件夹或跳过受保护的文件夹时打印信息。
786
+
787
+ #### 返回
788
+
789
+ 该函数没有返回值
790
+
791
+ #### 示例
792
+
793
+ ```python
794
+ # 调用函数
795
+ path_to_check = "/path/to/directory"
796
+ remove_empty_folders(path_to_check)
797
+ ```
798
+
799
+ ### 4.8 `remove(pattern)`
800
+
801
+ #### 描述
802
+
803
+ 删除与给定模式匹配的所有文件。该函数使用 `glob` 模块来匹配文件路径模式,并删除找到的所有文件。
804
+
805
+ #### 参数
806
+
807
+ - `pattern`:文件匹配模式,可以是相对路径或绝对路径,并可以使用通配符 `*` 来匹配多个文件。
808
+
809
+ #### 返回
810
+
811
+ 该函数没有返回值
812
+
813
+ #### 示例
814
+
815
+ ```python
816
+ # 调用函数
817
+ # 使用绝对路径
818
+ remove(r'E:\Code\Python\Model\WRF\Radar2\bzip2-radar-0*')
819
+
820
+ # 或者先切换到目标目录,再使用相对路径
821
+ os.chdir(r'E:\Code\Python\Model\WRF\Radar2')
822
+ remove('bzip2-radar-0*')
823
+ ```
824
+
825
+ ## 5 oa_nc
826
+
827
+ ### 5.1 description
828
+
829
+ 对nc数据进行处理,便捷提取变量、维度,以及将数据写入nc文件。
830
+
831
+ ### 5.2 `get_var(file, *vars)`
832
+
833
+ #### 描述
834
+
835
+ 从给定的 NetCDF 文件中提取并返回指定的变量数据。该函数使用 `xarray` 库来处理 NetCDF 文件。
836
+
837
+ #### 参数
838
+
839
+ - `file`:NetCDF 文件的路径,字符串类型。
840
+ - `*vars`:一个或多个变量名,这些是要从 NetCDF 文件中提取的变量。
841
+
842
+ #### 返回
843
+
844
+ - `datas`:一个列表,包含从 NetCDF 文件中提取的变量数据。
845
+
846
+ #### 示例
847
+
848
+ ```python
849
+ # 调用函数
850
+ file_path = 'path_to_your.nc'
851
+ variables = ['temperature', 'pressure']
852
+ data_list = get_var(file_path, *variables)
853
+
854
+ # 现在 data_list 包含了 'temperature' 和 'pressure' 变量的数据
855
+ ```
856
+
857
+ ### 5.3 `extract5nc(file, varname)`
858
+
859
+ #### 描述
860
+
861
+ 从 NetCDF 文件中提取指定变量的数据,并创建一个包含变量维度和对应值的字典。函数返回变量的数据数组以及维度字典。
862
+
863
+ #### 参数
864
+
865
+ - `file`:NetCDF 文件的路径,字符串类型。
866
+ - `varname`:要提取的变量名称,字符串类型。
867
+
868
+ #### 返回
869
+
870
+ - `np.array(vardata)`:提取的变量数据,转换为 NumPy 数组。
871
+ - `dimdict`:一个字典,键是变量维度名称,值是对应的维度数据。
872
+
873
+ #### 示例
874
+
875
+ ```python
876
+ # 调用函数
877
+ file_path = 'path_to_your.nc'
878
+ variable_name = 'temperature'
879
+ data_array, dimensions_dict = extract5nc(file_path, variable_name)
880
+
881
+ # 现在 data_array 包含了 'temperature' 变量的数据,dimensions_dict 包含了维度信息
882
+ ```
883
+
884
+ ### 5.4 `write2nc(file, data, varname, coords, mode)`
885
+
886
+ #### 描述
887
+
888
+ 将数据写入 NetCDF 文件。根据提供的模式(写入或追加),函数可以创建新的 NetCDF 文件,或者在现有文件中添加或替换变量和坐标。
889
+
890
+ #### 参数
891
+
892
+ - `file`:NetCDF 文件的路径,字符串类型。
893
+ - `data`:要写入的数据,NumPy 数组。
894
+ - `varname`:要创建或更新的变量名称,字符串类型。
895
+ - `coords`:坐标字典,键为维度名称,值为对应的坐标数据。
896
+ - `mode`:写入模式,'w' 表示写入(如果文件存在则删除),'a' 表示追加(如果文件不存在则创建)。
897
+
898
+ #### 返回
899
+
900
+ 该函数没有返回值
901
+
902
+ #### 示例
903
+
904
+ ```python
905
+ # 使用示例
906
+ file_path = 'output.nc'
907
+ data_to_write = np.random.rand(10, 5) # 示例数据
908
+ variable_name = 'example_data'
909
+ coordinates = {'time': np.arange(10), 'level': np.arange(5)}
910
+ write_mode = 'w' # 写入模式
911
+
912
+ write2nc(file_path, data_to_write, variable_name, coordinates, write_mode)
913
+ ```