Skip to content

Commit ae301c7

Browse files
simplest aggregation code
Fixes #1
1 parent 13a35c0 commit ae301c7

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

requirements.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: satellite_pm25_raster2polygon
2+
channels:
3+
- conda-forge
4+
- defaults
5+
dependencies:
6+
- python=3.10
7+
- netcdf4=1.6.2
8+
- xarray=2023.12.0
9+
- rasterio=1.2.10
10+
- rasterstats=0.19.0
11+
- geopandas=0.9.0
12+
- pip=23.1.2
13+
- pip:
14+
- requests==2.31.0
15+
- hydra-core==1.2.0
16+
- snakemake==7.30.1
17+
- selenium==4.16.0
18+
- webdriver_manager==4.0.1
19+

src/aggregate.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import xarray
2+
import rasterio
3+
import rasterstats
4+
import pandas as pd
5+
import geopandas as gpd
6+
import numpy as np
7+
8+
ds = xarray.open_dataset("data/satellite_pm25/V5GL04.HybridPM25.NorthAmerica.202201-202212.nc")
9+
layer = getattr(ds, 'GWRPM25')
10+
11+
# == compute zonal stats of layer
12+
13+
# obtain affine transform/boundaries
14+
dims = layer.dims
15+
assert len(dims) == 2, "netcdf coordinates must be 2d"
16+
lon = layer['lon'].values
17+
lat = layer['lat'].values
18+
transform = rasterio.transform.from_origin(
19+
lon[0], lat[-1], lon[1] - lon[0], lat[1] - lat[0]
20+
)
21+
22+
# == load shapefile from
23+
shape_path = 'data/shapefile_cb_county_2015/shapefile.shp'
24+
polygon = gpd.read_file(shape_path)
25+
26+
# compute zonal stats
27+
stats = rasterstats.zonal_stats(
28+
shape_path,
29+
layer.values[::-1],
30+
stats="mean",
31+
affine=transform,
32+
all_touched=True,
33+
#geojson_out=True,
34+
nodata=np.nan #if cfg.job.nodata == "nan" else cfg.job.nodata,
35+
)
36+
#gdf = gpd.GeoDataFrame.from_features(stats)
37+
df = pd.DataFrame(stats, index=polygon.GEOID)
38+
39+
df.to_csv('data/satellite_pm25_raster2polygon/county_pm25.csv')

0 commit comments

Comments
 (0)