Cycles-utils 1.0.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.
- Cycles-utils-1.0.0/Cycles_utils.egg-info/PKG-INFO +23 -0
- Cycles-utils-1.0.0/Cycles_utils.egg-info/SOURCES.txt +18 -0
- Cycles-utils-1.0.0/Cycles_utils.egg-info/dependency_links.txt +1 -0
- Cycles-utils-1.0.0/Cycles_utils.egg-info/requires.txt +9 -0
- Cycles-utils-1.0.0/Cycles_utils.egg-info/top_level.txt +1 -0
- Cycles-utils-1.0.0/LICENSE +21 -0
- Cycles-utils-1.0.0/PKG-INFO +23 -0
- Cycles-utils-1.0.0/README.md +10 -0
- Cycles-utils-1.0.0/cycles/__init__.py +3 -0
- Cycles-utils-1.0.0/cycles/cycles_read.py +124 -0
- Cycles-utils-1.0.0/cycles/gadm/__init__.py +2 -0
- Cycles-utils-1.0.0/cycles/gadm/gadm.py +74 -0
- Cycles-utils-1.0.0/cycles/gssurgo/__init__.py +7 -0
- Cycles-utils-1.0.0/cycles/gssurgo/gssurgo.py +85 -0
- Cycles-utils-1.0.0/cycles/soil/__init__.py +4 -0
- Cycles-utils-1.0.0/cycles/soil/soil.py +76 -0
- Cycles-utils-1.0.0/cycles/soilgrids/__init__.py +6 -0
- Cycles-utils-1.0.0/cycles/soilgrids/soilgrids.py +89 -0
- Cycles-utils-1.0.0/setup.cfg +4 -0
- Cycles-utils-1.0.0/setup.py +22 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: Cycles-utils
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Python scripts to build Cycles input files and post-process Cycles output files
|
|
5
|
+
Home-page: https://github.com/PSUmodeling/Cycles-utils
|
|
6
|
+
Author: Yuning Shi
|
|
7
|
+
Author-email: shiyuning@gmail.com
|
|
8
|
+
License: MIT
|
|
9
|
+
Requires-Python: >=3.6
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
Provides-Extra: soilgrids
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
|
|
14
|
+
# Cycles-utils
|
|
15
|
+
Library for building [Cycles](https://github.com/PSUmodeling/Cycles) input files and post-processing Cycles output files.
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
To install:
|
|
20
|
+
|
|
21
|
+
```shell
|
|
22
|
+
pip install Cycles-utils
|
|
23
|
+
```
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
setup.py
|
|
4
|
+
Cycles_utils.egg-info/PKG-INFO
|
|
5
|
+
Cycles_utils.egg-info/SOURCES.txt
|
|
6
|
+
Cycles_utils.egg-info/dependency_links.txt
|
|
7
|
+
Cycles_utils.egg-info/requires.txt
|
|
8
|
+
Cycles_utils.egg-info/top_level.txt
|
|
9
|
+
cycles/__init__.py
|
|
10
|
+
cycles/cycles_read.py
|
|
11
|
+
cycles/gadm/__init__.py
|
|
12
|
+
cycles/gadm/gadm.py
|
|
13
|
+
cycles/gssurgo/__init__.py
|
|
14
|
+
cycles/gssurgo/gssurgo.py
|
|
15
|
+
cycles/soil/__init__.py
|
|
16
|
+
cycles/soil/soil.py
|
|
17
|
+
cycles/soilgrids/__init__.py
|
|
18
|
+
cycles/soilgrids/soilgrids.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
cycles
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Penn State Earth System Modeling 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,23 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: Cycles-utils
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Python scripts to build Cycles input files and post-process Cycles output files
|
|
5
|
+
Home-page: https://github.com/PSUmodeling/Cycles-utils
|
|
6
|
+
Author: Yuning Shi
|
|
7
|
+
Author-email: shiyuning@gmail.com
|
|
8
|
+
License: MIT
|
|
9
|
+
Requires-Python: >=3.6
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
Provides-Extra: soilgrids
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
|
|
14
|
+
# Cycles-utils
|
|
15
|
+
Library for building [Cycles](https://github.com/PSUmodeling/Cycles) input files and post-processing Cycles output files.
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
To install:
|
|
20
|
+
|
|
21
|
+
```shell
|
|
22
|
+
pip install Cycles-utils
|
|
23
|
+
```
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import pandas as pd
|
|
2
|
+
|
|
3
|
+
HARVEST_TOOLS = [
|
|
4
|
+
'grain_harvest',
|
|
5
|
+
'harvest_grain',
|
|
6
|
+
'grainharvest',
|
|
7
|
+
'harvestgrain',
|
|
8
|
+
'forage_harvest',
|
|
9
|
+
'harvest_forage',
|
|
10
|
+
'forageharvest',
|
|
11
|
+
'harvestforage',
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def read_season(cycles_path, simulation):
|
|
16
|
+
'''Read season output file for harvested crop, harvest time, plant time, and yield
|
|
17
|
+
'''
|
|
18
|
+
df = pd.read_csv(
|
|
19
|
+
f'{cycles_path}/output/{simulation}/harvest.txt',
|
|
20
|
+
sep='\t',
|
|
21
|
+
header=0,
|
|
22
|
+
skiprows=[1],
|
|
23
|
+
skipinitialspace=True,
|
|
24
|
+
)
|
|
25
|
+
df = df.rename(columns=lambda x: x.strip().lower().replace(' ', '_'))
|
|
26
|
+
df['crop'] = df['crop'].str.strip()
|
|
27
|
+
|
|
28
|
+
for col in ['date', 'plant_date']: df[col] = pd.to_datetime(df[col])
|
|
29
|
+
|
|
30
|
+
return df
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def read_operations(cycles_path, operation):
|
|
34
|
+
with open(f'{cycles_path}/input/{operation}.operation') as f:
|
|
35
|
+
lines = f.read().splitlines()
|
|
36
|
+
|
|
37
|
+
lines = [line for line in lines if (not line.strip().startswith('#')) and len(line.strip()) > 0]
|
|
38
|
+
|
|
39
|
+
operations = []
|
|
40
|
+
k = 0
|
|
41
|
+
while k < len(lines):
|
|
42
|
+
if lines[k] == 'FIXED_FERTILIZATION':
|
|
43
|
+
operations.append(
|
|
44
|
+
{
|
|
45
|
+
'type': 'fertilization',
|
|
46
|
+
'year': int(lines[k + 1].split()[1]),
|
|
47
|
+
'doy': int(lines[k + 2].split()[1]),
|
|
48
|
+
'source': lines[k + 3].split()[1],
|
|
49
|
+
'mass': lines[k + 4].split()[1],
|
|
50
|
+
}
|
|
51
|
+
)
|
|
52
|
+
k += 5
|
|
53
|
+
elif lines[k] == 'TILLAGE':
|
|
54
|
+
if lines[k + 3].split()[1].strip().lower() in HARVEST_TOOLS:
|
|
55
|
+
operations.append(
|
|
56
|
+
{
|
|
57
|
+
'type': 'harvest',
|
|
58
|
+
'year': int(lines[k + 1].split()[1]),
|
|
59
|
+
'doy': int(lines[k + 2].split()[1]),
|
|
60
|
+
'crop': lines[k + 7].split()[1],
|
|
61
|
+
}
|
|
62
|
+
)
|
|
63
|
+
elif lines[k + 3].split()[1].strip().lower() == 'kill_crop':
|
|
64
|
+
operations.append(
|
|
65
|
+
{
|
|
66
|
+
'type': 'kill',
|
|
67
|
+
'year': int(lines[k + 1].split()[1]),
|
|
68
|
+
'doy': int(lines[k + 2].split()[1]),
|
|
69
|
+
'crop': lines[k + 7].split()[1],
|
|
70
|
+
}
|
|
71
|
+
)
|
|
72
|
+
else:
|
|
73
|
+
operations.append(
|
|
74
|
+
{
|
|
75
|
+
'type': 'tillage',
|
|
76
|
+
'year': int(lines[k + 1].split()[1]),
|
|
77
|
+
'doy': int(lines[k + 2].split()[1]),
|
|
78
|
+
'tool': lines[k + 3].split()[1],
|
|
79
|
+
}
|
|
80
|
+
)
|
|
81
|
+
k += 8
|
|
82
|
+
elif lines[k] == 'PLANTING':
|
|
83
|
+
operations.append(
|
|
84
|
+
{
|
|
85
|
+
'type': 'planting',
|
|
86
|
+
'year': int(lines[k + 1].split()[1]),
|
|
87
|
+
'doy': int(lines[k + 2].split()[1]),
|
|
88
|
+
'crop': lines[k + 8].split()[1],
|
|
89
|
+
}
|
|
90
|
+
)
|
|
91
|
+
k += 9
|
|
92
|
+
else:
|
|
93
|
+
k += 1
|
|
94
|
+
|
|
95
|
+
df = pd.DataFrame(operations)
|
|
96
|
+
|
|
97
|
+
return df
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def read_weather(cycles_path, weather, start_year=0, end_year=9999):
|
|
101
|
+
NUM_HEADER_LINES = 4
|
|
102
|
+
columns = {
|
|
103
|
+
'YEAR': int,
|
|
104
|
+
'DOY': int,
|
|
105
|
+
'PP': float,
|
|
106
|
+
'TX': float,
|
|
107
|
+
'TN': float,
|
|
108
|
+
'SOLAR': float,
|
|
109
|
+
'RHX': float,
|
|
110
|
+
'RHN': float,
|
|
111
|
+
'WIND': float,
|
|
112
|
+
}
|
|
113
|
+
df = pd.read_csv(
|
|
114
|
+
f,
|
|
115
|
+
usecols=list(range(len(columns))),
|
|
116
|
+
names=columns.keys(),
|
|
117
|
+
comment='#',
|
|
118
|
+
sep='\s+',
|
|
119
|
+
na_values=[-999],
|
|
120
|
+
)
|
|
121
|
+
df = df.iloc[NUM_HEADER_LINES:, :]
|
|
122
|
+
df = df.astype(columns)
|
|
123
|
+
|
|
124
|
+
return df[(df['YEAR'] <= end_year) & (df['YEAR'] >= start_year)]
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import geopandas as gpd
|
|
2
|
+
|
|
3
|
+
GADM = lambda path, country, level: f'{path}/gadm41_{country}_{level}.shp'
|
|
4
|
+
GADM_LEVELS = {
|
|
5
|
+
'country': 0,
|
|
6
|
+
'state': 1,
|
|
7
|
+
'county': 2,
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
# For USA
|
|
11
|
+
STATE_ABBREVIATIONS = {
|
|
12
|
+
'USA.1_1': 'AL',
|
|
13
|
+
'USA.2_1': 'AK',
|
|
14
|
+
'USA.3_1': 'AZ',
|
|
15
|
+
'USA.4_1': 'AR',
|
|
16
|
+
'USA.5_1': 'CA',
|
|
17
|
+
'USA.6_1': 'CO',
|
|
18
|
+
'USA.7_1': 'CT',
|
|
19
|
+
'USA.8_1': 'DE',
|
|
20
|
+
'USA.9_1': 'DC',
|
|
21
|
+
'USA.10_1': 'FL',
|
|
22
|
+
'USA.11_1': 'GA',
|
|
23
|
+
'USA.12_1': 'HI',
|
|
24
|
+
'USA.13_1': 'ID',
|
|
25
|
+
'USA.14_1': 'IL',
|
|
26
|
+
'USA.15_1': 'IN',
|
|
27
|
+
'USA.16_1': 'IA',
|
|
28
|
+
'USA.17_1': 'KS',
|
|
29
|
+
'USA.18_1': 'KY',
|
|
30
|
+
'USA.19_1': 'LA',
|
|
31
|
+
'USA.20_1': 'ME',
|
|
32
|
+
'USA.21_1': 'MD',
|
|
33
|
+
'USA.22_1': 'MA',
|
|
34
|
+
'USA.23_1': 'MI',
|
|
35
|
+
'USA.24_1': 'MN',
|
|
36
|
+
'USA.25_1': 'MS',
|
|
37
|
+
'USA.26_1': 'MO',
|
|
38
|
+
'USA.27_1': 'MT',
|
|
39
|
+
'USA.28_1': 'NE',
|
|
40
|
+
'USA.29_1': 'NV',
|
|
41
|
+
'USA.30_1': 'NH',
|
|
42
|
+
'USA.31_1': 'NJ',
|
|
43
|
+
'USA.32_1': 'NM',
|
|
44
|
+
'USA.33_1': 'NY',
|
|
45
|
+
'USA.34_1': 'NC',
|
|
46
|
+
'USA.35_1': 'ND',
|
|
47
|
+
'USA.36_1': 'OH',
|
|
48
|
+
'USA.37_1': 'OK',
|
|
49
|
+
'USA.38_1': 'OR',
|
|
50
|
+
'USA.39_1': 'PA',
|
|
51
|
+
'USA.40_1': 'RI',
|
|
52
|
+
'USA.41_1': 'SC',
|
|
53
|
+
'USA.42_1': 'SD',
|
|
54
|
+
'USA.43_1': 'TN',
|
|
55
|
+
'USA.44_1': 'TX',
|
|
56
|
+
'USA.45_1': 'UT',
|
|
57
|
+
'USA.46_1': 'VT',
|
|
58
|
+
'USA.47_1': 'VA',
|
|
59
|
+
'USA.48_1': 'WA',
|
|
60
|
+
'USA.49_1': 'WV',
|
|
61
|
+
'USA.50_1': 'WI',
|
|
62
|
+
'USA.51_1': 'WY',
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def read_gadm(path, country, level, conus=True):
|
|
67
|
+
level = GADM_LEVELS[level.lower()]
|
|
68
|
+
gdf = gpd.read_file(GADM(path, country, level))
|
|
69
|
+
gdf.set_index(f'GID_{level}', inplace=True)
|
|
70
|
+
gdf['GID'] = gdf.index
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
# Generate a CONUS GeoDataFrame by removing Alaska and Hawaii
|
|
74
|
+
return gdf[~gdf['NAME_1'].isin(['Alaska', 'Hawaii'])] if country == 'USA' and conus else gdf
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
from . gssurgo import GSSURGO_NON_SOIL_TYPES
|
|
2
|
+
from . gssurgo import GSSURGO_PARAMETERS
|
|
3
|
+
from . gssurgo import GSSURGO_URBAN_TYPES
|
|
4
|
+
from . gssurgo import NAD83
|
|
5
|
+
from . gssurgo import get_soil_profile_parameters
|
|
6
|
+
from . gssurgo import read_state_gssurgo
|
|
7
|
+
from . gssurgo import read_state_luts
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import geopandas as gpd
|
|
2
|
+
import pandas as pd
|
|
3
|
+
|
|
4
|
+
GSSURGO = lambda path, state: f'{path}/gSSURGO_{state}.gdb/'
|
|
5
|
+
GSSURGO_LUT = lambda path, lut, state: f'{path}/{lut}_{state}.csv'
|
|
6
|
+
GSSURGO_PARAMETERS = {
|
|
7
|
+
'clay': {'variable': 'claytotal_r', 'multiplier': 1.0}, # %
|
|
8
|
+
'sand': {'variable': 'sandtotal_r', 'multiplier': 1.0}, # %
|
|
9
|
+
'soc': {'variable': 'om_r', 'multiplier': 0.58}, # %
|
|
10
|
+
'bulk_density': {'variable': 'dbthirdbar_r', 'multiplier': 1.0}, # Mg/m3
|
|
11
|
+
'top': {'variable': 'hzdept_r', 'multiplier': 0.01}, # m
|
|
12
|
+
'bottom': {'variable': 'hzdepb_r', 'multiplier': 0.01}, # m
|
|
13
|
+
}
|
|
14
|
+
GSSURGO_NON_SOIL_TYPES = [
|
|
15
|
+
'Acidic rock land',
|
|
16
|
+
'Area not surveyed',
|
|
17
|
+
'Dam',
|
|
18
|
+
'Dumps',
|
|
19
|
+
'Levee',
|
|
20
|
+
'No Digital Data Available',
|
|
21
|
+
'Pits',
|
|
22
|
+
'Water',
|
|
23
|
+
]
|
|
24
|
+
GSSURGO_URBAN_TYPES = [
|
|
25
|
+
'Udorthents',
|
|
26
|
+
'Urban land',
|
|
27
|
+
]
|
|
28
|
+
NAD83 = 'epsg:5070' # NAD83 / Conus Albers, CRS of gSSURGO
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def read_state_luts(path, state_abbreviation, group=False):
|
|
32
|
+
tables = {
|
|
33
|
+
'component': ['mukey', 'cokey', 'majcompflag'],
|
|
34
|
+
'chorizon': ['hzname', 'hzdept_r', 'hzdepb_r', 'sandtotal_r', 'silttotal_r', 'claytotal_r', 'om_r', 'dbthirdbar_r', 'cokey'],
|
|
35
|
+
'muaggatt': ['hydgrpdcd', 'muname', 'slopegradwta', 'mukey'],
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
gssurgo_luts = {}
|
|
39
|
+
for t in tables:
|
|
40
|
+
gssurgo_luts[t] = pd.read_csv(
|
|
41
|
+
GSSURGO_LUT(path, t, state_abbreviation),
|
|
42
|
+
usecols=tables[t],
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
# Rename table columns
|
|
46
|
+
gssurgo_luts['chorizon'] = gssurgo_luts['chorizon'].rename(
|
|
47
|
+
columns={GSSURGO_PARAMETERS[v]['variable']: v for v in GSSURGO_PARAMETERS}
|
|
48
|
+
)
|
|
49
|
+
# Convert units (note that organic matter is also converted to soil organic carbon in this case)
|
|
50
|
+
for v in GSSURGO_PARAMETERS: gssurgo_luts['chorizon'][v] *= GSSURGO_PARAMETERS[v]['multiplier']
|
|
51
|
+
|
|
52
|
+
# In the gSSURGO database many map units are the same soil texture with different slopes, etc. To find the dominant
|
|
53
|
+
# soil series, same soil texture with different slopes should be aggregated together. Therefore we use the map unit
|
|
54
|
+
# names to identify the same soil textures among different soil map units.
|
|
55
|
+
if group:
|
|
56
|
+
gssurgo_luts['muaggatt']['muname'] = gssurgo_luts['muaggatt']['muname'].map(lambda name: name.split(',')[0])
|
|
57
|
+
|
|
58
|
+
return gssurgo_luts
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def read_state_gssurgo(path, state_abbreviation, group=False):
|
|
62
|
+
gdf = gpd.read_file(
|
|
63
|
+
GSSURGO(path, state_abbreviation),
|
|
64
|
+
layer='MUPOLYGON',
|
|
65
|
+
)
|
|
66
|
+
gdf.columns = [x.lower() for x in gdf.columns]
|
|
67
|
+
gdf.mukey = gdf.mukey.astype(int)
|
|
68
|
+
|
|
69
|
+
luts = read_state_luts(path, state_abbreviation, group=group)
|
|
70
|
+
|
|
71
|
+
# Merge the mapunit polygon table with the mapunit aggregated attribute table
|
|
72
|
+
gdf = gdf.merge(luts['muaggatt'], on='mukey')
|
|
73
|
+
|
|
74
|
+
return gdf, luts
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def get_soil_profile_parameters(luts, mukey):
|
|
78
|
+
df = luts['component'][luts['component'].mukey == int(mukey)].merge(luts['chorizon'], on='cokey')
|
|
79
|
+
if not df[df['majcompflag'] == 'Yes'].empty:
|
|
80
|
+
df = df[df['majcompflag'] == 'Yes'].sort_values(by='top')
|
|
81
|
+
else:
|
|
82
|
+
print(f'{index} {t} no major component')
|
|
83
|
+
df = df.sort_values(by='top')
|
|
84
|
+
|
|
85
|
+
return df[df['hzname'] != 'R']
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import pandas as pd
|
|
3
|
+
|
|
4
|
+
SOIL_PARAMETERS = ['clay', 'sand', 'soc', 'bulk_density']
|
|
5
|
+
SOIL_LAYERS = [
|
|
6
|
+
# units: top (m), bottom (m), thickness (m), NO3 (kg/ha), NH4 (Kg/ha)
|
|
7
|
+
{'top': 0, 'bottom': 0.05, 'thickness': 0.05, 'no3': 10, 'nh4': 1},
|
|
8
|
+
{'top': 0.05, 'bottom': 0.1, 'thickness': 0.05, 'no3': 10, 'nh4': 1},
|
|
9
|
+
{'top': 0.1, 'bottom': 0.2, 'thickness': 0.1, 'no3': 7, 'nh4': 1},
|
|
10
|
+
{'top': 0.2, 'bottom': 0.4, 'thickness': 0.2, 'no3': 4, 'nh4': 1},
|
|
11
|
+
{'top': 0.4, 'bottom': 0.6, 'thickness': 0.2, 'no3': 2, 'nh4': 1},
|
|
12
|
+
{'top': 0.6, 'bottom': 0.8, 'thickness': 0.2, 'no3': 1, 'nh4': 1},
|
|
13
|
+
{'top': 0.8, 'bottom': 1.0, 'thickness': 0.2, 'no3': 1, 'nh4': 1},
|
|
14
|
+
{'top': 1.0, 'bottom': 1.2, 'thickness': 0.2, 'no3': 1, 'nh4': 1},
|
|
15
|
+
{'top': 1.2, 'bottom': 1.4, 'thickness': 0.2, 'no3': 1, 'nh4': 1},
|
|
16
|
+
{'top': 1.4, 'bottom': 1.6, 'thickness': 0.2, 'no3': 1, 'nh4': 1},
|
|
17
|
+
{'top': 1.6, 'bottom': 1.8, 'thickness': 0.2, 'no3': 1, 'nh4': 1},
|
|
18
|
+
{'top': 1.8, 'bottom': 2.0, 'thickness': 0.2, 'no3': 1, 'nh4': 1},
|
|
19
|
+
]
|
|
20
|
+
CURVE_NUMBERS = {
|
|
21
|
+
#row crops, SR, Good
|
|
22
|
+
'A': 67,
|
|
23
|
+
'B': 78,
|
|
24
|
+
'C': 85,
|
|
25
|
+
'D': 89,
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def overlapping_depth(top1, bottom1, top2, bottom2):
|
|
30
|
+
return max(0.0, min(bottom1, bottom2) - max(top1, top2))
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def calculate_parameter(soil_df, parameter, top, bottom):
|
|
34
|
+
soil_df['weight'] = soil_df.apply(lambda x: overlapping_depth(x['top'], x['bottom'], top, bottom) / (bottom - top), axis=1)
|
|
35
|
+
soil_df = soil_df[soil_df['weight'] > 0]
|
|
36
|
+
|
|
37
|
+
return np.sum(np.array(soil_df[parameter] * soil_df['weight'])) / sum(soil_df['weight'])
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def generate_soil_file(fn, desc, hsg, slope, soil_depth, soil_df):
|
|
41
|
+
df = pd.DataFrame.from_dict(
|
|
42
|
+
{v: [layer[v] for layer in SOIL_LAYERS if layer['bottom'] <= soil_depth] for v in SOIL_LAYERS[0]}
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
df['layer'] = range(1, len(df) + 1)
|
|
46
|
+
|
|
47
|
+
for v in SOIL_PARAMETERS:
|
|
48
|
+
df[v] = df.apply(lambda x: calculate_parameter(soil_df, v, x['top'], x['bottom']), axis=1)
|
|
49
|
+
|
|
50
|
+
cn = -999 if not hsg else CURVE_NUMBERS[hsg[0]]
|
|
51
|
+
|
|
52
|
+
with open(fn, 'w') as f:
|
|
53
|
+
f.write(desc)
|
|
54
|
+
|
|
55
|
+
f.write("%-15s\t%d\n" % ("CURVE_NUMBER", cn))
|
|
56
|
+
f.write("%-15s\t%.2f\n" % ("SLOPE", slope))
|
|
57
|
+
|
|
58
|
+
f.write("%-15s\t%d\n" % ("TOTAL_LAYERS", len(df)))
|
|
59
|
+
f.write(('%-7s\t'*12 + '%s\n') % (
|
|
60
|
+
"LAYER", "THICK", "CLAY", "SAND", "SOC", "BD", "FC", "PWP", "SON", "NO3", "NH4", "BYP_H", "BYP_V"
|
|
61
|
+
))
|
|
62
|
+
|
|
63
|
+
f.write(('%-7s\t'*12 + '%s\n') % (
|
|
64
|
+
"#", "m", "%", "%", "%", "Mg/m3", "m3/m3", "m3/m3", "kg/ha", "kg/ha", "kg/ha", "-", "-"
|
|
65
|
+
))
|
|
66
|
+
|
|
67
|
+
for _, row in df.iterrows():
|
|
68
|
+
f.write('%-7d\t' % row['layer'])
|
|
69
|
+
f.write('%-7.2f\t' % float(row['thickness']))
|
|
70
|
+
f.write('%-7s\t' % '-999' if np.isnan(row['clay']) else '%-7.1f\t' % float(row['clay']))
|
|
71
|
+
f.write('%-7s\t' % '-999' if np.isnan(row['sand']) else '%-7.1f\t' % float(row['sand']))
|
|
72
|
+
f.write('%-7s\t' % '-999' if np.isnan(row['soc']) else '%-7.2f\t' % float(row['soc']))
|
|
73
|
+
f.write('%-7s\t' % '-999' if np.isnan(row['bulk_density']) else '%-7.2f\t' % float(row['bulk_density']))
|
|
74
|
+
f.write(('%-7d\t'*3 + '%-7.1f\t'*2 + '%-7.1f\t%.1f\n') % (
|
|
75
|
+
-999, -999, -999, float(row['no3']), float(row['nh4']), 0.0, 0.0
|
|
76
|
+
))
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
from . soilgrids import HOMOLOSINE
|
|
2
|
+
from . soilgrids import SOILGRIDS_LAYERS
|
|
3
|
+
from . soilgrids import SOILGRIDS_PROPERTIES
|
|
4
|
+
from . soilgrids import download_soilgrids_data
|
|
5
|
+
from . soilgrids import read_soilgrids_maps
|
|
6
|
+
from . soilgrids import reproject_match_soilgrids_maps
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import geopandas as gpd
|
|
2
|
+
import pandas as pd
|
|
3
|
+
import rioxarray
|
|
4
|
+
from owslib.wcs import WebCoverageService
|
|
5
|
+
from rasterio.enums import Resampling
|
|
6
|
+
from shapely.geometry import Point
|
|
7
|
+
|
|
8
|
+
SOILGRIDS_PROPERTIES = {
|
|
9
|
+
'clay': {'name': 'clay', 'multiplier': 0.1}, # %
|
|
10
|
+
'sand': {'name': 'sand', 'multiplier': 0.1}, # %
|
|
11
|
+
'soc': {'name': 'soc', 'multiplier': 0.01}, # %
|
|
12
|
+
'bulk_density': {'name': 'bdod', 'multiplier': 0.01}, # Mg/m3
|
|
13
|
+
}
|
|
14
|
+
SOILGRIDS_LAYERS = {
|
|
15
|
+
# units: m
|
|
16
|
+
'0-5cm': {'top': 0, 'bottom': 0.05, 'thickness': 0.05},
|
|
17
|
+
'5-15cm': {'top': 0.05, 'bottom': 0.15, 'thickness': 0.10},
|
|
18
|
+
'15-30cm': {'top': 0.15, 'bottom': 0.3, 'thickness': 0.15},
|
|
19
|
+
'30-60cm': {'top': 0.3, 'bottom': 0.6, 'thickness': 0.3},
|
|
20
|
+
'60-100cm': {'top': 0.6, 'bottom': 1.0, 'thickness': 0.4},
|
|
21
|
+
'100-200cm': {'top': 1.0, 'bottom': 2.0, 'thickness': 1.0},
|
|
22
|
+
}
|
|
23
|
+
HOMOLOSINE = 'urn:ogc:def:crs:EPSG::152160' # Interrupted Goode Homolosine, CRS of SoilGrids
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
# Read state SoilGrids data
|
|
27
|
+
def read_soilgrids_maps(path, state_id, layers, parameters, crs=HOMOLOSINE):
|
|
28
|
+
soilgrids_xds = {}
|
|
29
|
+
for layer in layers:
|
|
30
|
+
for v in parameters:
|
|
31
|
+
soilgrids_xds[f'{v}_{layer}'] = rioxarray.open_rasterio(f'{path}/{state_id}/{SOILGRIDS_PROPERTIES[v]["name"]}_{layer}.tif', masked=True).rio.reproject(crs)
|
|
32
|
+
|
|
33
|
+
return soilgrids_xds
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def reproject_match_soilgrids_maps(soilgrids_xds, reference_xds, reference_name, boundary, layers, parameters):
|
|
37
|
+
reference_xds = reference_xds.rio.clip([boundary], from_disk=True)
|
|
38
|
+
df = pd.DataFrame(reference_xds[0].to_series().rename(reference_name))
|
|
39
|
+
|
|
40
|
+
for v in parameters:
|
|
41
|
+
for layer in layers:
|
|
42
|
+
soil_xds = soilgrids_xds[f'{v}_{layer}'].rio.reproject_match(reference_xds, resampling=Resampling.nearest)
|
|
43
|
+
soil_xds = soil_xds.rio.clip([boundary], from_disk=True)
|
|
44
|
+
|
|
45
|
+
soil_df = pd.DataFrame(soil_xds[0].to_series().rename(f'{v}_{layer}')) * SOILGRIDS_PROPERTIES[v]['multiplier']
|
|
46
|
+
df = pd.concat([df, soil_df], axis=1)
|
|
47
|
+
|
|
48
|
+
return df
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
"""Convert bounding boxes to SoilGrids CRS
|
|
52
|
+
"""
|
|
53
|
+
def get_bounding_box(reference_shp, bbox, crs):
|
|
54
|
+
d = {'col1': ['NW', 'SE'], 'geometry': [Point(bbox[0], bbox[3]), Point(bbox[2], bbox[1])]}
|
|
55
|
+
gdf = gpd.GeoDataFrame(d, crs=crs).set_index('col1')
|
|
56
|
+
|
|
57
|
+
converted = gdf.to_crs(gpd.read_file(reference_shp).crs)
|
|
58
|
+
|
|
59
|
+
return [
|
|
60
|
+
converted.loc['NW', 'geometry'].xy[0][0],
|
|
61
|
+
converted.loc['SE', 'geometry'].xy[1][0],
|
|
62
|
+
converted.loc['SE', 'geometry'].xy[0][0],
|
|
63
|
+
converted.loc['NW', 'geometry'].xy[1][0],
|
|
64
|
+
]
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
"""Use WebCoverageService to get SoilGrids data
|
|
68
|
+
bbox should be in the order of [west, south, east, north]
|
|
69
|
+
"""
|
|
70
|
+
def download_soilgrids_data(layers, parameters, path, bbox, crs):
|
|
71
|
+
# Convert bounding box to SoilGrids CRS
|
|
72
|
+
bbox = get_bounding_box(bbox, crs)
|
|
73
|
+
|
|
74
|
+
for v in [SOILGRIDS_PROPERTIES[param]['name'] for param in parameters]:
|
|
75
|
+
for layer in layers:
|
|
76
|
+
wcs = WebCoverageService(f'http://maps.isric.org/mapserv?map=/map/{v}.map', version='1.0.0')
|
|
77
|
+
while True:
|
|
78
|
+
try:
|
|
79
|
+
response = wcs.getCoverage(
|
|
80
|
+
identifier=f'{v}_{layer}_mean',
|
|
81
|
+
crs=HOMOLOSINE,
|
|
82
|
+
bbox=bbox,
|
|
83
|
+
resx=250, resy=250,
|
|
84
|
+
format='GEOTIFF_INT16')
|
|
85
|
+
|
|
86
|
+
with open(f'{path}/{v}_{layer}.tif', 'wb') as file: file.write(response.read())
|
|
87
|
+
break
|
|
88
|
+
except:
|
|
89
|
+
continue
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
from setuptools import setup, find_packages
|
|
2
|
+
|
|
3
|
+
with open('README.md', 'r') as f:
|
|
4
|
+
long_description = f.read()
|
|
5
|
+
|
|
6
|
+
setup(
|
|
7
|
+
name='Cycles-utils',
|
|
8
|
+
version='1.0.0',
|
|
9
|
+
author='Yuning Shi',
|
|
10
|
+
author_email="shiyuning@gmail.com",
|
|
11
|
+
packages=find_packages(),
|
|
12
|
+
description='Python scripts to build Cycles input files and post-process Cycles output files',
|
|
13
|
+
long_description=long_description,
|
|
14
|
+
long_description_content_type='text/markdown',
|
|
15
|
+
url='https://github.com/PSUmodeling/Cycles-utils',
|
|
16
|
+
license='MIT',
|
|
17
|
+
python_requires='>=3.6',
|
|
18
|
+
install_requires=['numpy>=1.19.5', 'geopandas>=0.9.0', 'pandas>=1.2.4'],
|
|
19
|
+
extras_require = {
|
|
20
|
+
'soilgrids': ['rioxarray>=0.5.0', 'owslib>=0.24.1', 'rasterio>=1.2.3', 'shapely>=1.7.1'],
|
|
21
|
+
}
|
|
22
|
+
)
|