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