-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_aws.py
120 lines (92 loc) · 3.68 KB
/
make_aws.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from osgeo import gdal
gdal.UseExceptions()
import pandas as pd
import numpy as np
from numba import njit, prange
with open('data_dir.txt') as f:
data_dir = f.readline()
data_dir = data_dir.rstrip()
muagg_df = pd.read_csv(data_dir+"gnatsgo/muaggatt.csv")
keep_cols = [x for x in muagg_df.columns if "aws" in x] + ["mukey"]
muagg_df = muagg_df[keep_cols]
@njit
def make_aws_dict(mukey, aws25, aws50, aws100, aws150):
d = dict()
for i in range(mukey.size):
d[mukey[i]] = np.array([aws25[i], aws50[i], aws100[i], aws150[i]])
return d
aws_dict = make_aws_dict(np.array(muagg_df.mukey),
np.array(muagg_df.aws025wta),
np.array(muagg_df.aws050wta),
np.array(muagg_df.aws0100wta),
np.array(muagg_df.aws0150wta))
crop_df = pd.read_csv("cdl_rz_cn_temp.csv")
crop_df["cdl_code"] = crop_df["CDL code"].astype("int")
crop_df["depth"] = crop_df["Rooting depth (m)"] * 100
@njit
def make_rz_dict(crop_code, depth):
d = dict()
for i in range(crop_code.size):
d[crop_code[i]] = depth[i]
return d
rz_dict = make_rz_dict(np.array(crop_df.cdl_code),
np.array(crop_df.depth))
@njit
def calc_aws(cdl_code, mukey, rz_dict, aws_dict):
if cdl_code == 0 or mukey == 0:
return 0
rz_depth = rz_dict[cdl_code]
if np.isnan(rz_depth):
return 0
aws25, aws50, aws100, aws150 = aws_dict[mukey]
if rz_depth < 25:
aws = aws25 * rz_depth / 25
elif rz_depth >= 25 and rz_depth < 50:
marginal_frac = (rz_depth - 25) / 25
marginal_aws = marginal_frac * (aws50 - aws25)
aws = aws25 + marginal_aws
elif rz_depth >= 50 and rz_depth < 100:
marignal_frac = (rz_depth - 50) / 50
marginal_aws = marginal_frac * (aws100 - aws50)
aws = aws50 + marginal_aws
elif rz_depth >= 100 and rz_depth < 150:
marginal_frac = (rz_depth - 100) / 50
marginal_aws = marginal_frac * (aws150 - aws100)
aws = aws100 + marginal_aws
elif rz_depth >= 150:
rz_beyond_150 = rz_depth - 150
deepest_awc = (aws150 - aws100) / 50
aws = aws150 + rz_beyond_150 * deepest_awc
# return value in mm
return aws * 10
######################################################################
mukey_ras = gdal.Open(data_dir+"gnatsgo/FY2024_gNATSGO_mukey_grid.tif")
for yr in range(2016, 2024):
cdl_ras = gdal.Open(data_dir+f"cdl_tifs/{yr}_30m_cdls.tif")
#print(cdl_ras.GetGeoTransform())
mukey_gt = mukey_ras.GetGeoTransform()
cdl_gt = cdl_ras.GetGeoTransform()
# mukey and cdl use same CRS but GTs are offset
# one grid cell in each direction
# (writing this to work more generally)
mukey_xoff = max(0, -int((mukey_gt[0] - cdl_gt[0]) / 30))
mukey_yoff = max(0, int((mukey_gt[3] - cdl_gt[3]) / 30))
cdl_xoff = max(0, int((mukey_gt[0] - cdl_gt[0]) / 30))
cdl_yoff = max(0, -int((mukey_gt[3] - cdl_gt[3]) / 30))
print("loading mukey")
# mukey raster is bigger in both directions than cdl
mukey_arr = mukey_ras.ReadAsArray(xoff=mukey_xoff, yoff=mukey_yoff,
xsize=cdl_ras.RasterXSize-cdl_xoff,
ysize=cdl_ras.RasterYSize-cdl_yoff)
print("loading cdl")
cdl_arr = cdl_ras.ReadAsArray(xoff=cdl_xoff, yoff=cdl_yoff,
ysize=cdl_ras.RasterYSize-cdl_yoff)
@njit(parallel=True)
def make_aws_arr(cdl_arr, mukey_arr, rz_dict, aws_dict):
dims = cdl_arr.shape
aws = np.zeros(dims, dtype=np.int16)
for i in range(dims[0]):
for j in prange(dims[1]):
aws[i, j] = calc_aws(cdl_arr[i, j], mukey_arr[i, j],
rz_dict, aws_dict)
return aws