cmaq-tools 0.1.0__tar.gz

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,10 @@
1
+ # Python-generated files
2
+ __pycache__/
3
+ *.py[oc]
4
+ build/
5
+ dist/
6
+ wheels/
7
+ *.egg-info
8
+
9
+ # Virtual environments
10
+ .venv
@@ -0,0 +1 @@
1
+ 3.13
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 CAMQ Tools Team
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,81 @@
1
+ Metadata-Version: 2.4
2
+ Name: cmaq-tools
3
+ Version: 0.1.0
4
+ Summary: A Python library for extracting and processing air quality model data from AELMO NetCDF files
5
+ Project-URL: Homepage, https://github.com/zusiven/cmaq_tools
6
+ Project-URL: Repository, https://github.com/zusiven/cmaq_tools.git
7
+ Project-URL: Documentation, https://github.com/zusiven/cmaq_tools
8
+ Author: CMAQ Tools Team
9
+ License-File: LICENSE
10
+ Keywords: aelmo,air-quality,atmospheric-modeling,data-extraction,netcdf
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Intended Audience :: Science/Research
13
+ Classifier: License :: OSI Approved :: MIT License
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Topic :: Scientific/Engineering :: Atmospheric Science
17
+ Requires-Python: >=3.11
18
+ Requires-Dist: netcdf4
19
+ Requires-Dist: polars>=1.37.1
20
+ Requires-Dist: pyproj
21
+ Requires-Dist: wztools>=2026.1.6
22
+ Requires-Dist: xarray>=2025.4.0
23
+ Provides-Extra: dev
24
+ Requires-Dist: jupyter>=1.0.0; extra == 'dev'
25
+ Requires-Dist: matplotlib>=3.5.0; extra == 'dev'
26
+ Requires-Dist: pytest-cov>=4.0.0; extra == 'dev'
27
+ Requires-Dist: pytest>=7.0.0; extra == 'dev'
28
+ Requires-Dist: twine>=6.2.0; extra == 'dev'
29
+ Provides-Extra: examples
30
+ Requires-Dist: jupyter>=1.0.0; extra == 'examples'
31
+ Requires-Dist: matplotlib>=3.5.0; extra == 'examples'
32
+ Description-Content-Type: text/markdown
33
+
34
+ # CMAQ Tools
35
+
36
+ 一个用于从 AELMO NetCDF 文件中提取和处理空气质量模型数据的 Python 库。
37
+
38
+ ## 安装
39
+
40
+ ```bash
41
+ pip install git+https://github.com/zusiven/cmaq_tools.git
42
+ ```
43
+
44
+ ## 使用方法
45
+
46
+ ```python
47
+ from cmaq_tools import AelmoExtractor
48
+ from pathlib import Path
49
+
50
+ # 初始化提取器
51
+ extractor = AelmoExtractor(
52
+ aelmo_path=Path("path/to/CCTM_AELMO_d01.nc"),
53
+ cro2d_path=Path("path/to/GRIDCRO2D_d01.nc")
54
+ )
55
+
56
+ # 提取所有数据
57
+ df = extractor.extract_data()
58
+ print(df)
59
+
60
+ # 根据经纬度提取特定位置的数据
61
+ df_point = extractor.extract_data_by_lonlat(lon=114.0, lat=38.0)
62
+ print(df_point)
63
+ ```
64
+
65
+ ## 支持的污染物
66
+
67
+ - O3 (臭氧)
68
+ - PM2.5 (细颗粒物)
69
+ - PM10 (可吸入颗粒物)
70
+ - NO2 (二氧化氮)
71
+ - CO (一氧化碳)
72
+ - NO (一氧化氮)
73
+ - SO2 (二氧化硫)
74
+
75
+ ## 依赖
76
+
77
+ - polars
78
+ - xarray
79
+ - netcdf4
80
+ - pyproj
81
+ - wztools
@@ -0,0 +1,48 @@
1
+ # CMAQ Tools
2
+
3
+ 一个用于从 AELMO NetCDF 文件中提取和处理空气质量模型数据的 Python 库。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ pip install git+https://github.com/zusiven/cmaq_tools.git
9
+ ```
10
+
11
+ ## 使用方法
12
+
13
+ ```python
14
+ from cmaq_tools import AelmoExtractor
15
+ from pathlib import Path
16
+
17
+ # 初始化提取器
18
+ extractor = AelmoExtractor(
19
+ aelmo_path=Path("path/to/CCTM_AELMO_d01.nc"),
20
+ cro2d_path=Path("path/to/GRIDCRO2D_d01.nc")
21
+ )
22
+
23
+ # 提取所有数据
24
+ df = extractor.extract_data()
25
+ print(df)
26
+
27
+ # 根据经纬度提取特定位置的数据
28
+ df_point = extractor.extract_data_by_lonlat(lon=114.0, lat=38.0)
29
+ print(df_point)
30
+ ```
31
+
32
+ ## 支持的污染物
33
+
34
+ - O3 (臭氧)
35
+ - PM2.5 (细颗粒物)
36
+ - PM10 (可吸入颗粒物)
37
+ - NO2 (二氧化氮)
38
+ - CO (一氧化碳)
39
+ - NO (一氧化氮)
40
+ - SO2 (二氧化硫)
41
+
42
+ ## 依赖
43
+
44
+ - polars
45
+ - xarray
46
+ - netcdf4
47
+ - pyproj
48
+ - wztools
@@ -0,0 +1,3 @@
1
+ from .aelmo_extractor import AelmoExtractor
2
+
3
+ __all__ = ["AelmoExtractor"]
@@ -0,0 +1,130 @@
1
+
2
+ import polars as pl
3
+ import xarray as xr
4
+ import datetime as dt
5
+ import numpy as np
6
+
7
+ from pyproj import Proj
8
+ from pathlib import Path
9
+ from wztools import fetch_nearest_point
10
+
11
+
12
+ class AelmoExtractor:
13
+ def __init__(self, aelmo_path: Path|str, cro2d_path: Path|str) -> None:
14
+ """
15
+ >>> aermo_extractor = AelmoExtractor(aelmo_path, cro2d_path)
16
+ >>> # 提取某点数据
17
+ >>> df = aermo_extractor.extract_data_by_lonlat(lon, lat)
18
+ >>>
19
+ >>> # 提起网格数据
20
+ >>> df_grid = aermo_extractor.extract_data()
21
+ """
22
+ self.aelmo_path = Path(aelmo_path)
23
+ self.cro2d_path = Path(cro2d_path)
24
+
25
+ def extract_data(self) -> pl.DataFrame:
26
+ ds = xr.open_dataset(self.aelmo_path, engine="netcdf4")
27
+
28
+ # 加载地理信息文件
29
+ ds_grid = xr.open_dataset(self.cro2d_path)
30
+ # 获取经纬度 (通常是二维数组)
31
+ lons = ds_grid['LON'].squeeze().values
32
+ lats = ds_grid['LAT'].squeeze().values
33
+
34
+ # 2. 从属性获取时间元数据
35
+ sdate = int(ds.attrs['SDATE']) # YYYYDDD
36
+ stime = int(ds.attrs['STIME']) # HHMMSS
37
+ tstep = int(ds.attrs['TSTEP']) # HHMMSS
38
+
39
+ # 3. 解析起始时间
40
+ start_dt = dt.datetime.strptime(str(sdate), '%Y%j')
41
+ # 补全 6 位时间格式 (例如 51802 -> 051802)
42
+ s_str = str(stime).zfill(6)
43
+ start_dt += dt.timedelta(hours=int(s_str[:2]), minutes=int(s_str[2:4]), seconds=int(s_str[4:6]))
44
+
45
+ # 4. 解析步长 (TSTEP)
46
+ ts_str = str(tstep).zfill(6)
47
+ tstep_delta = dt.timedelta(hours=int(ts_str[:2]), minutes=int(ts_str[2:4]), seconds=int(ts_str[4:6]))
48
+
49
+ # 5. 生成时间序列(修复 FutureWarning)
50
+ # 使用 ds.sizes 代替 ds.dims
51
+ num_steps = ds.sizes['TSTEP']
52
+ time_values = [start_dt + i * tstep_delta for i in range(num_steps)]
53
+ time_values = [t + dt.timedelta(hours=8) for t in time_values]
54
+
55
+ # 6. 重新定义坐标
56
+ ds = ds.assign_coords(time=('TSTEP', time_values)).swap_dims({'TSTEP': 'time'})
57
+
58
+ O3_data = ds.variables["O3"][:] * 1963
59
+ NO2_data = ds.variables["NO2"][:] * 1881
60
+ NO_data = ds.variables["NO"][:] * 1227
61
+ PM10_data = ds.variables["PM10"][:]
62
+ PM25_data = ds.variables["PM25"][:]
63
+ CO_data = ds.variables["CO"][:] * 1.144
64
+ SO2_data = ds.variables["SO2"][:] * 2619.6
65
+
66
+ # lons = lons.values
67
+ # lats = lats.values
68
+
69
+ dfs = []
70
+ nx = ds.sizes['COL']
71
+ ny = ds.sizes['ROW']
72
+
73
+ for index, t in enumerate(time_values):
74
+ _O3_data = O3_data[index][0].values
75
+ _PM10_data = PM10_data[index][0].values
76
+ _PM25_data = PM25_data[index][0].values
77
+ _NO_data = NO_data[index][0].values
78
+ _NO2_data = NO2_data[index][0].values
79
+ _CO_data = CO_data[index][0].values
80
+ _SO2_data = SO2_data[index][0].values
81
+
82
+ df_t = pl.DataFrame({
83
+ "time": [t] * nx * ny,
84
+ "lon": lons.flatten(),
85
+ "lat": lats.flatten(),
86
+ "O3": _O3_data.flatten(),
87
+ "PM10": _PM10_data.flatten(),
88
+ "PM25": _PM25_data.flatten(),
89
+ "NO2": _NO2_data.flatten(),
90
+ "CO": _CO_data.flatten(),
91
+ "NO": _NO_data.flatten(),
92
+ "SO2": _SO2_data.flatten(),
93
+ })
94
+ dfs.append(df_t)
95
+ # print(O3_data.shape)
96
+ # print(len(time_values))
97
+ df = pl.concat(dfs)
98
+ df = df.with_columns(
99
+ *[pl.col(col).cast(pl.Float64).round(4) for col in df.columns if col not in ["time"]]
100
+ )
101
+
102
+ return df
103
+
104
+ def extract_data_by_lonlat(self, lon, lat) -> pl.DataFrame:
105
+ df = self.extract_data()
106
+
107
+ nerest_lon, nearest_lat = fetch_nearest_point(df, tar_lon=lon, tar_lat=lat)
108
+ df = df.filter(
109
+ (pl.col("lon") == nerest_lon)
110
+ &
111
+ (pl.col("lat") == nearest_lat)
112
+ ).sort("time")
113
+
114
+ df = df.rename({"time": "datetime"})
115
+ return df
116
+
117
+
118
+
119
+ if __name__ == "__main__":
120
+ file_path = Path(r"C:\Users\Administrator\Desktop\tmp\CCTM_AELMO_d01_yixing2.nc")
121
+ cro2d_path = Path(r"C:\Users\Administrator\Desktop\tmp\GRIDCRO2D_d01_yixing2.nc")
122
+
123
+ lon = 114
124
+ lat = 38
125
+
126
+ aermo_extractor = AelmoExtractor(file_path, cro2d_path)
127
+
128
+ aermo_extractor.extract_data()
129
+ df = aermo_extractor.extract_data_by_lonlat(lon, lat)
130
+ print(df)
@@ -0,0 +1,50 @@
1
+ [project]
2
+ name = "cmaq-tools"
3
+ version = "0.1.0"
4
+ description = "A Python library for extracting and processing air quality model data from AELMO NetCDF files"
5
+ readme = "README.md"
6
+ requires-python = ">=3.11"
7
+ authors = [
8
+ {name = "CMAQ Tools Team"}
9
+ ]
10
+ keywords = ["air-quality", "atmospheric-modeling", "netcdf", "data-extraction", "aelmo"]
11
+ classifiers = [
12
+ "Development Status :: 3 - Alpha",
13
+ "Intended Audience :: Science/Research",
14
+ "License :: OSI Approved :: MIT License",
15
+ "Programming Language :: Python :: 3",
16
+ "Programming Language :: Python :: 3.11",
17
+ "Topic :: Scientific/Engineering :: Atmospheric Science",
18
+ ]
19
+ dependencies = [
20
+ "netcdf4",
21
+ "polars>=1.37.1",
22
+ "pyproj",
23
+ "wztools>=2026.1.6",
24
+ "xarray>=2025.4.0",
25
+ ]
26
+
27
+ [project.optional-dependencies]
28
+ dev = [
29
+ "pytest>=7.0.0",
30
+ "pytest-cov>=4.0.0",
31
+ "matplotlib>=3.5.0",
32
+ "jupyter>=1.0.0",
33
+ "twine>=6.2.0"
34
+ ]
35
+ examples = [
36
+ "matplotlib>=3.5.0",
37
+ "jupyter>=1.0.0",
38
+ ]
39
+
40
+ [project.urls]
41
+ Homepage = "https://github.com/zusiven/cmaq_tools"
42
+ Repository = "https://github.com/zusiven/cmaq_tools.git"
43
+ Documentation = "https://github.com/zusiven/cmaq_tools"
44
+
45
+ [build-system]
46
+ requires = ["hatchling"]
47
+ build-backend = "hatchling.build"
48
+
49
+ [tool.hatch.build.targets.wheel]
50
+ packages = ["cmaq_tools"]