hestia-earth-models 0.59.3__py3-none-any.whl → 0.59.5__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.
Potentially problematic release.
This version of hestia-earth-models might be problematic. Click here for more details.
- hestia_earth/models/cycle/liveAnimal.py +3 -0
- hestia_earth/models/cycle/milkYield.py +1 -1
- hestia_earth/models/cycle/utils.py +1 -1
- hestia_earth/models/geospatialDatabase/potentialEvapotranspirationLongTermAnnualMean.py +2 -2
- hestia_earth/models/geospatialDatabase/potentialEvapotranspirationMonthly.py +99 -0
- hestia_earth/models/geospatialDatabase/precipitationMonthly.py +100 -0
- hestia_earth/models/geospatialDatabase/temperatureAnnual.py +2 -6
- hestia_earth/models/geospatialDatabase/temperatureLongTermAnnualMean.py +2 -3
- hestia_earth/models/geospatialDatabase/temperatureMonthly.py +98 -0
- hestia_earth/models/geospatialDatabase/utils.py +13 -1
- hestia_earth/models/ipcc2019/organicCarbonPerHa.py +72 -135
- hestia_earth/models/linkedImpactAssessment/__init__.py +78 -43
- hestia_earth/models/mocking/search-results.json +8 -47
- hestia_earth/models/schmidt2007/n2OToAirWasteTreatmentDirect.py +58 -0
- hestia_earth/models/schmidt2007/nh3ToAirWasteTreatment.py +58 -0
- hestia_earth/models/site/management.py +106 -13
- hestia_earth/models/site/pre_checks/cache_geospatialDatabase.py +27 -7
- hestia_earth/models/site/soilMeasurement.py +9 -9
- hestia_earth/models/site/utils.py +2 -6
- hestia_earth/models/utils/__init__.py +9 -0
- hestia_earth/models/utils/blank_node.py +3 -3
- hestia_earth/models/utils/site.py +8 -5
- hestia_earth/models/utils/term.py +0 -23
- hestia_earth/models/version.py +1 -1
- {hestia_earth_models-0.59.3.dist-info → hestia_earth_models-0.59.5.dist-info}/METADATA +2 -2
- {hestia_earth_models-0.59.3.dist-info → hestia_earth_models-0.59.5.dist-info}/RECORD +39 -29
- tests/models/geospatialDatabase/test_potentialEvapotranspirationMonthly.py +20 -0
- tests/models/geospatialDatabase/test_precipitationMonthly.py +20 -0
- tests/models/geospatialDatabase/test_temperatureMonthly.py +20 -0
- tests/models/ipcc2019/test_organicCarbonPerHa.py +8 -39
- tests/models/schmidt2007/test_n2OToAirWasteTreatmentDirect.py +45 -0
- tests/models/schmidt2007/test_nh3ToAirWasteTreatment.py +45 -0
- tests/models/site/test_management.py +37 -16
- tests/models/site/test_soilMeasurement.py +40 -21
- tests/models/utils/test_site.py +1 -1
- tests/models/utils/test_term.py +1 -8
- {hestia_earth_models-0.59.3.dist-info → hestia_earth_models-0.59.5.dist-info}/LICENSE +0 -0
- {hestia_earth_models-0.59.3.dist-info → hestia_earth_models-0.59.5.dist-info}/WHEEL +0 -0
- {hestia_earth_models-0.59.3.dist-info → hestia_earth_models-0.59.5.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
from hestia_earth.schema import EmissionMethodTier
|
|
2
|
+
from hestia_earth.utils.tools import list_sum
|
|
3
|
+
|
|
4
|
+
from hestia_earth.models.log import logRequirements, logShouldRun
|
|
5
|
+
from hestia_earth.models.utils.emission import _new_emission
|
|
6
|
+
from .utils import get_waste_values
|
|
7
|
+
from . import MODEL
|
|
8
|
+
|
|
9
|
+
REQUIREMENTS = {
|
|
10
|
+
"Cycle": {
|
|
11
|
+
"or": {
|
|
12
|
+
"product": [
|
|
13
|
+
{"@type": "Product", "value": "", "term.termType": "waste"}
|
|
14
|
+
],
|
|
15
|
+
"completeness.waste": ""
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
RETURNS = {
|
|
20
|
+
"Emission": [{
|
|
21
|
+
"value": "",
|
|
22
|
+
"methodTier": "tier 1"
|
|
23
|
+
}]
|
|
24
|
+
}
|
|
25
|
+
LOOKUPS = {
|
|
26
|
+
"waste": "n2oEfSchmidt2007"
|
|
27
|
+
}
|
|
28
|
+
TERM_ID = 'n2OToAirWasteTreatmentDirect'
|
|
29
|
+
TIER = EmissionMethodTier.TIER_1.value
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _emission(value: float):
|
|
33
|
+
emission = _new_emission(TERM_ID, MODEL)
|
|
34
|
+
emission['value'] = [value]
|
|
35
|
+
emission['methodTier'] = TIER
|
|
36
|
+
return emission
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _run(waste_values: list):
|
|
40
|
+
value = list_sum(waste_values)
|
|
41
|
+
return [_emission(value)]
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _should_run(cycle: dict):
|
|
45
|
+
waste_values = get_waste_values(TERM_ID, cycle, LOOKUPS['waste'])
|
|
46
|
+
has_waste = len(waste_values) > 0
|
|
47
|
+
|
|
48
|
+
logRequirements(cycle, model=MODEL, term=TERM_ID,
|
|
49
|
+
has_waste=has_waste)
|
|
50
|
+
|
|
51
|
+
should_run = any([has_waste])
|
|
52
|
+
logShouldRun(cycle, MODEL, TERM_ID, should_run, methodTier=TIER)
|
|
53
|
+
return should_run, waste_values
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def run(cycle: dict):
|
|
57
|
+
should_run, waste_values = _should_run(cycle)
|
|
58
|
+
return _run(waste_values) if should_run else []
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
from hestia_earth.schema import EmissionMethodTier
|
|
2
|
+
from hestia_earth.utils.tools import list_sum
|
|
3
|
+
|
|
4
|
+
from hestia_earth.models.log import logRequirements, logShouldRun
|
|
5
|
+
from hestia_earth.models.utils.emission import _new_emission
|
|
6
|
+
from .utils import get_waste_values
|
|
7
|
+
from . import MODEL
|
|
8
|
+
|
|
9
|
+
REQUIREMENTS = {
|
|
10
|
+
"Cycle": {
|
|
11
|
+
"or": {
|
|
12
|
+
"product": [
|
|
13
|
+
{"@type": "Product", "value": "", "term.termType": "waste"}
|
|
14
|
+
],
|
|
15
|
+
"completeness.waste": ""
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
RETURNS = {
|
|
20
|
+
"Emission": [{
|
|
21
|
+
"value": "",
|
|
22
|
+
"methodTier": "tier 1"
|
|
23
|
+
}]
|
|
24
|
+
}
|
|
25
|
+
LOOKUPS = {
|
|
26
|
+
"waste": "nh3EfSchmidt2007"
|
|
27
|
+
}
|
|
28
|
+
TERM_ID = 'nh3ToAirWasteTreatment'
|
|
29
|
+
TIER = EmissionMethodTier.TIER_1.value
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _emission(value: float):
|
|
33
|
+
emission = _new_emission(TERM_ID, MODEL)
|
|
34
|
+
emission['value'] = [value]
|
|
35
|
+
emission['methodTier'] = TIER
|
|
36
|
+
return emission
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _run(waste_values: list):
|
|
40
|
+
value = list_sum(waste_values)
|
|
41
|
+
return [_emission(value)]
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _should_run(cycle: dict):
|
|
45
|
+
waste_values = get_waste_values(TERM_ID, cycle, LOOKUPS['waste'])
|
|
46
|
+
has_waste = len(waste_values) > 0
|
|
47
|
+
|
|
48
|
+
logRequirements(cycle, model=MODEL, term=TERM_ID,
|
|
49
|
+
has_waste=has_waste)
|
|
50
|
+
|
|
51
|
+
should_run = any([has_waste])
|
|
52
|
+
logShouldRun(cycle, MODEL, TERM_ID, should_run, methodTier=TIER)
|
|
53
|
+
return should_run, waste_values
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def run(cycle: dict):
|
|
57
|
+
should_run, waste_values = _should_run(cycle)
|
|
58
|
+
return _run(waste_values) if should_run else []
|
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
"""
|
|
2
2
|
Management node with data gap-filled data from cycles.
|
|
3
3
|
"""
|
|
4
|
-
from typing import List
|
|
4
|
+
from typing import List, Any
|
|
5
5
|
from functools import reduce
|
|
6
6
|
from hestia_earth.schema import SchemaType, TermTermType
|
|
7
7
|
from hestia_earth.utils.api import download_hestia
|
|
8
8
|
from hestia_earth.utils.model import filter_list_term_type, linked_node
|
|
9
|
-
from hestia_earth.utils.tools import flatten
|
|
9
|
+
from hestia_earth.utils.tools import flatten, safe_parse_float
|
|
10
10
|
|
|
11
11
|
from hestia_earth.models.log import logRequirements, logShouldRun, log_blank_nodes_id
|
|
12
|
-
from hestia_earth.models.utils.site import related_cycles
|
|
13
12
|
from hestia_earth.models.utils.term import get_lookup_value
|
|
14
13
|
from hestia_earth.models.utils.blank_node import get_node_value
|
|
14
|
+
from hestia_earth.models.utils.site import related_cycles
|
|
15
15
|
from . import MODEL
|
|
16
16
|
|
|
17
17
|
REQUIREMENTS = {
|
|
@@ -37,6 +37,16 @@ REQUIREMENTS = {
|
|
|
37
37
|
],
|
|
38
38
|
"value": ""
|
|
39
39
|
}
|
|
40
|
+
],
|
|
41
|
+
"inputs": [
|
|
42
|
+
{
|
|
43
|
+
"@type": "Input",
|
|
44
|
+
"term.termType": [
|
|
45
|
+
"inorganicFertiliser",
|
|
46
|
+
"organicFertiliser",
|
|
47
|
+
"soilAmendment"
|
|
48
|
+
]
|
|
49
|
+
}
|
|
40
50
|
]
|
|
41
51
|
}]
|
|
42
52
|
}
|
|
@@ -53,9 +63,45 @@ RETURNS = {
|
|
|
53
63
|
"startDate": ""
|
|
54
64
|
}]
|
|
55
65
|
}
|
|
56
|
-
|
|
66
|
+
LOOKUPS = {
|
|
67
|
+
"inorganicFertiliser": "nitrogenContent",
|
|
68
|
+
"organicFertiliser": "ANIMAL_MANURE",
|
|
69
|
+
"soilAmendment": "PRACTICE_INCREASING_C_INPUT"
|
|
70
|
+
}
|
|
57
71
|
MODEL_KEY = 'management'
|
|
58
72
|
LAND_COVER_KEY = 'landCoverId'
|
|
73
|
+
ANIMAL_MANURE_USED_TERM_ID = "animalManureUsed"
|
|
74
|
+
INORGANIC_NITROGEN_FERTILISER_USED_TERM_ID = "inorganicNitrogenFertiliserUsed"
|
|
75
|
+
ORGANIC_FERTILISER_USED_TERM_ID = "organicFertiliserUsed"
|
|
76
|
+
AMENDMENT_INCREASING_C_USED_TERM_ID = "amendmentIncreasingSoilCarbonUsed"
|
|
77
|
+
INPUT_RULES = {
|
|
78
|
+
TermTermType.INORGANICFERTILISER.value: (
|
|
79
|
+
(
|
|
80
|
+
TermTermType.INORGANICFERTILISER.value, # Lookup column
|
|
81
|
+
lambda x: safe_parse_float(x) > 0, # Condition
|
|
82
|
+
INORGANIC_NITROGEN_FERTILISER_USED_TERM_ID # New term.
|
|
83
|
+
),
|
|
84
|
+
),
|
|
85
|
+
TermTermType.SOILAMENDMENT.value: (
|
|
86
|
+
(
|
|
87
|
+
TermTermType.SOILAMENDMENT.value,
|
|
88
|
+
lambda x: x is True,
|
|
89
|
+
AMENDMENT_INCREASING_C_USED_TERM_ID
|
|
90
|
+
),
|
|
91
|
+
),
|
|
92
|
+
TermTermType.ORGANICFERTILISER.value: (
|
|
93
|
+
(
|
|
94
|
+
TermTermType.SOILAMENDMENT.value,
|
|
95
|
+
lambda x: x is True,
|
|
96
|
+
ORGANIC_FERTILISER_USED_TERM_ID
|
|
97
|
+
),
|
|
98
|
+
(
|
|
99
|
+
TermTermType.ORGANICFERTILISER.value,
|
|
100
|
+
lambda x: x is True,
|
|
101
|
+
ANIMAL_MANURE_USED_TERM_ID
|
|
102
|
+
)
|
|
103
|
+
)
|
|
104
|
+
}
|
|
59
105
|
|
|
60
106
|
|
|
61
107
|
def management(data: dict):
|
|
@@ -74,7 +120,9 @@ def _include_start_end(cycle: dict, values: list):
|
|
|
74
120
|
return [(_include(cycle, ['startDate', 'endDate']) | v) for v in values]
|
|
75
121
|
|
|
76
122
|
|
|
77
|
-
def _copy_item_if_exists(source: dict, keys: List[str] =
|
|
123
|
+
def _copy_item_if_exists(source: dict, keys: List[str] = None, dest: dict = None) -> dict:
|
|
124
|
+
keys = keys or []
|
|
125
|
+
dest = dest or {}
|
|
78
126
|
return reduce(lambda p, c: p | ({c: source[c]} if c in source else {}), keys, dest)
|
|
79
127
|
|
|
80
128
|
|
|
@@ -96,7 +144,51 @@ def _get_items_with_relevant_term_type(cycles: List[dict], item_name: str, relev
|
|
|
96
144
|
)
|
|
97
145
|
|
|
98
146
|
|
|
99
|
-
def
|
|
147
|
+
def _get_lookup_with_debug(term: dict, column: str) -> Any:
|
|
148
|
+
get_lookup_value(term, column, model_key=MODEL_KEY, land_cover_key=LAND_COVER_KEY)
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def _data_from_input(cycle: dict, term_id: str) -> dict:
|
|
152
|
+
return {
|
|
153
|
+
"term": {
|
|
154
|
+
"@type": "Term",
|
|
155
|
+
"@id": term_id,
|
|
156
|
+
"termType": "landUseManagement"
|
|
157
|
+
},
|
|
158
|
+
"value": True,
|
|
159
|
+
"startDate": cycle["startDate"],
|
|
160
|
+
"endDate": cycle["endDate"]
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def _process_rule(cycle, term, term_type) -> List:
|
|
165
|
+
relevant_terms = []
|
|
166
|
+
for column, condition, new_term in INPUT_RULES[term_type]:
|
|
167
|
+
lookup_result = _get_lookup_with_debug(term, LOOKUPS[column])
|
|
168
|
+
|
|
169
|
+
if condition(lookup_result):
|
|
170
|
+
relevant_terms.append(_data_from_input(cycle=cycle, term_id=new_term))
|
|
171
|
+
|
|
172
|
+
return relevant_terms
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def _get_relevant_inputs(cycles: List[dict]) -> List:
|
|
176
|
+
relevant_inputs = []
|
|
177
|
+
for cycle in [c for c in cycles if "inputs" in c]:
|
|
178
|
+
for i in cycle["inputs"]:
|
|
179
|
+
if i.get("term", {}).get("termType", "") in INPUT_RULES:
|
|
180
|
+
relevant_inputs.extend(
|
|
181
|
+
_process_rule(
|
|
182
|
+
cycle=cycle,
|
|
183
|
+
term=i.get("term", {}),
|
|
184
|
+
term_type=i.get("term", {}).get("termType", "")
|
|
185
|
+
)
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
return relevant_inputs
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def _should_run(site: dict):
|
|
100
192
|
# Only get related cycles once.
|
|
101
193
|
cycles = related_cycles(site.get("@id"))
|
|
102
194
|
|
|
@@ -148,6 +240,7 @@ def should_run(site: dict):
|
|
|
148
240
|
)
|
|
149
241
|
]
|
|
150
242
|
|
|
243
|
+
relevant_inputs = _get_relevant_inputs(cycles)
|
|
151
244
|
logRequirements(
|
|
152
245
|
site,
|
|
153
246
|
model=MODEL,
|
|
@@ -155,14 +248,14 @@ def should_run(site: dict):
|
|
|
155
248
|
model_key=MODEL_KEY,
|
|
156
249
|
products_crop_forage_ids=log_blank_nodes_id(products_crop_forage),
|
|
157
250
|
products_land_cover_ids=log_blank_nodes_id(products_land_cover),
|
|
158
|
-
practice_ids=log_blank_nodes_id(practices)
|
|
251
|
+
practice_ids=log_blank_nodes_id(practices),
|
|
252
|
+
inputs=log_blank_nodes_id(relevant_inputs)
|
|
159
253
|
)
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
return _should_run, products_crop_forage + products_land_cover, practices
|
|
254
|
+
should_run = any(products_crop_forage + products_land_cover + practices + relevant_inputs)
|
|
255
|
+
logShouldRun(site, MODEL, None, should_run=should_run, model_key=MODEL_KEY)
|
|
256
|
+
return should_run, products_crop_forage + products_land_cover, practices, relevant_inputs
|
|
164
257
|
|
|
165
258
|
|
|
166
259
|
def run(site: dict):
|
|
167
|
-
|
|
168
|
-
return list(map(management, products + practices)) if
|
|
260
|
+
should_run, products, practices, inputs = _should_run(site)
|
|
261
|
+
return list(map(management, products + practices + inputs)) if should_run else []
|
|
@@ -4,10 +4,10 @@ Pre Checks Cache Geospatial Database
|
|
|
4
4
|
This model caches results from Geospatial Database.
|
|
5
5
|
"""
|
|
6
6
|
from functools import reduce
|
|
7
|
-
from hestia_earth.utils.tools import flatten
|
|
7
|
+
from hestia_earth.utils.tools import flatten, non_empty_list
|
|
8
8
|
|
|
9
9
|
from hestia_earth.models.log import debugValues
|
|
10
|
-
from hestia_earth.models.utils import CACHE_KEY, cached_value
|
|
10
|
+
from hestia_earth.models.utils import CACHE_KEY, cached_value, first_day_of_month, last_day_of_month
|
|
11
11
|
from hestia_earth.models.utils.site import CACHE_YEARS_KEY
|
|
12
12
|
from hestia_earth.models.geospatialDatabase.utils import (
|
|
13
13
|
MAX_AREA_SIZE, CACHE_VALUE, CACHE_AREA_SIZE,
|
|
@@ -34,7 +34,12 @@ def cache_site_results(results: list, collections: list, area_size: int = None):
|
|
|
34
34
|
collection = collections[index]
|
|
35
35
|
name = collection.get('name')
|
|
36
36
|
value = results[index]
|
|
37
|
-
|
|
37
|
+
cache_sub_key = '-'.join(non_empty_list([
|
|
38
|
+
collection.get('year'),
|
|
39
|
+
collection.get('start_date'),
|
|
40
|
+
collection.get('end_date')
|
|
41
|
+
]))
|
|
42
|
+
data = (group.get(name, {}) | {cache_sub_key: value} if cache_sub_key else value)
|
|
38
43
|
return group | {name: data}
|
|
39
44
|
|
|
40
45
|
return reduce(_combine_result, range(0, len(results)), {}) | (
|
|
@@ -42,13 +47,28 @@ def cache_site_results(results: list, collections: list, area_size: int = None):
|
|
|
42
47
|
)
|
|
43
48
|
|
|
44
49
|
|
|
50
|
+
def _extend_collection_by_month(year: int):
|
|
51
|
+
return [{
|
|
52
|
+
'start_date': first_day_of_month(year, month).strftime('%Y-%m-%d'),
|
|
53
|
+
'end_date': last_day_of_month(year, month).strftime('%Y-%m-%d')
|
|
54
|
+
} for month in range(1, 13)]
|
|
55
|
+
|
|
56
|
+
|
|
45
57
|
def _extend_collection(name: str, collection: dict, years: list = []):
|
|
46
58
|
data = collection | {'name': name, 'collection': _collection_name(collection.get('collection'))}
|
|
59
|
+
|
|
60
|
+
year_params = [{'year': str(year)} for year in years]
|
|
61
|
+
# fetch from first year to last
|
|
62
|
+
month_years = range(years[0], years[-1] + 1) if len(years) > 1 else years
|
|
63
|
+
month_params = flatten(map(_extend_collection_by_month, month_years))
|
|
64
|
+
|
|
47
65
|
return [
|
|
48
|
-
data |
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
] if '
|
|
66
|
+
(data | params) for params in year_params
|
|
67
|
+
] if name.endswith('Annual') else [
|
|
68
|
+
(data | params) for params in month_params
|
|
69
|
+
] if name.endswith('Monthly') else [
|
|
70
|
+
data
|
|
71
|
+
]
|
|
52
72
|
|
|
53
73
|
|
|
54
74
|
def _extend_collections(values: list, years: list = []):
|
|
@@ -16,7 +16,7 @@ from . import MODEL
|
|
|
16
16
|
REQUIREMENTS = {
|
|
17
17
|
"Site": {
|
|
18
18
|
"measurements": [
|
|
19
|
-
{"@type": "Measurement", "depthUpper": "", "depthLower": ""}
|
|
19
|
+
{"@type": "Measurement", "depthUpper": "", "depthLower": "", "value": ""}
|
|
20
20
|
]
|
|
21
21
|
}
|
|
22
22
|
}
|
|
@@ -159,21 +159,21 @@ def _get_needed_depths(site: dict) -> list:
|
|
|
159
159
|
|
|
160
160
|
def _should_run(site: dict, model_key: str):
|
|
161
161
|
# we only work with measurements with depths
|
|
162
|
-
measurements = [
|
|
163
|
-
m
|
|
164
|
-
|
|
165
|
-
]
|
|
162
|
+
measurements = [m for m in site.get("measurements", []) if all([
|
|
163
|
+
get_lookup_value(m.get("term", {}), LOOKUPS["measurement"][0], model=MODEL, model_key=model_key),
|
|
164
|
+
m.get('value', [])
|
|
165
|
+
])]
|
|
166
166
|
|
|
167
167
|
measurements_with_depths = [m for m in measurements if all([
|
|
168
|
-
"depthUpper" in m
|
|
169
|
-
"depthLower" in m
|
|
168
|
+
"depthUpper" in m,
|
|
169
|
+
"depthLower" in m,
|
|
170
170
|
(int(m.get("depthUpper", 0)), int(m.get("depthLower", 0))) not in STANDARD_DEPTHS
|
|
171
171
|
])]
|
|
172
172
|
has_measurements_with_depths = len(measurements_with_depths) > 0
|
|
173
173
|
|
|
174
174
|
measurements_missing_depth_recommended = [m for m in measurements if all([
|
|
175
|
-
"depthUpper" not in m
|
|
176
|
-
"depthLower" not in m
|
|
175
|
+
"depthUpper" not in m,
|
|
176
|
+
"depthLower" not in m,
|
|
177
177
|
not get_lookup_value(m.get("term", {}), LOOKUPS["measurement"][1], model=MODEL, model_key=model_key)
|
|
178
178
|
])]
|
|
179
179
|
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
from functools import reduce
|
|
2
|
-
import datetime
|
|
3
2
|
from hestia_earth.schema import TermTermType
|
|
4
3
|
from hestia_earth.utils.tools import non_empty_list
|
|
5
4
|
from hestia_earth.utils.date import DAY
|
|
6
5
|
|
|
7
|
-
from hestia_earth.models.utils import _omit
|
|
6
|
+
from hestia_earth.models.utils import _omit, first_day_of_month, last_day_of_month
|
|
8
7
|
from hestia_earth.models.utils.measurement import _new_measurement, measurement_value, has_all_months
|
|
9
8
|
|
|
10
9
|
|
|
@@ -68,10 +67,7 @@ def _group_by_month(term_id: str, dates: list, values: list):
|
|
|
68
67
|
|
|
69
68
|
def map_to_month(data: list, year: int, month: int):
|
|
70
69
|
# make sure we got all the necessary days
|
|
71
|
-
|
|
72
|
-
last_day_of_month = datetime.date(year + int(month / 12), (month % 12) + 1, 1) - datetime.timedelta(days=1)
|
|
73
|
-
|
|
74
|
-
difference = last_day_of_month - first_day_of_month
|
|
70
|
+
difference = last_day_of_month(year, month) - first_day_of_month(year, month)
|
|
75
71
|
days_in_month = round(difference.days + difference.seconds / DAY, 1) + 1
|
|
76
72
|
|
|
77
73
|
return measurement_value({
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
from os.path import dirname, abspath
|
|
2
2
|
import sys
|
|
3
|
+
import datetime
|
|
3
4
|
from functools import reduce
|
|
4
5
|
import operator
|
|
5
6
|
from typing import Any
|
|
@@ -126,3 +127,11 @@ def _get_by_key(x, y):
|
|
|
126
127
|
|
|
127
128
|
|
|
128
129
|
def get_dict_key(value: dict, key: str): return reduce(lambda x, y: _get_by_key(x, y), key.split('.'), value)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def first_day_of_month(year: int, month: int):
|
|
133
|
+
return datetime.date(int(year), int(month), 1)
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def last_day_of_month(year: int, month: int):
|
|
137
|
+
return datetime.date(int(year) + int(int(month) / 12), (int(month) % 12) + 1, 1) - datetime.timedelta(days=1)
|
|
@@ -37,9 +37,9 @@ from .term import get_lookup_value
|
|
|
37
37
|
|
|
38
38
|
|
|
39
39
|
def group_by_keys(group_keys: list = ['term']):
|
|
40
|
-
def run(group: dict,
|
|
41
|
-
group_key = '-'.join(non_empty_list(map(lambda v:
|
|
42
|
-
group[group_key] = group.get(group_key, []) + [
|
|
40
|
+
def run(group: dict, node: dict):
|
|
41
|
+
group_key = '-'.join(non_empty_list(map(lambda v: node.get(v, {}).get('@id'), group_keys)))
|
|
42
|
+
group[group_key] = group.get(group_key, []) + [node]
|
|
43
43
|
return group
|
|
44
44
|
return run
|
|
45
45
|
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
from hestia_earth.schema import SchemaType, SiteSiteType, TermTermType
|
|
2
|
-
from hestia_earth.utils.api import
|
|
2
|
+
from hestia_earth.utils.api import find_related
|
|
3
3
|
from hestia_earth.utils.lookup import download_lookup, get_table_value, column_name
|
|
4
4
|
from hestia_earth.utils.tools import non_empty_list, safe_parse_date
|
|
5
5
|
|
|
6
6
|
from hestia_earth.models.log import debugMissingLookup
|
|
7
|
-
from . import cached_value
|
|
7
|
+
from . import cached_value, _load_calculated_node
|
|
8
8
|
|
|
9
9
|
CACHE_YEARS_KEY = 'years'
|
|
10
10
|
WATER_TYPES = [
|
|
@@ -47,8 +47,7 @@ def is_site(site: dict): return site.get('@type', site.get('type')) == SchemaTyp
|
|
|
47
47
|
def related_cycles(site_id: str):
|
|
48
48
|
"""
|
|
49
49
|
Get the list of `Cycle` related to the `Site`.
|
|
50
|
-
|
|
51
|
-
In Hestia, a `Cycle` must have a link to a `Site`, therefore a `Site` can be related to many `Cycle`s.
|
|
50
|
+
Gets the `recalculated` data if available, else `original`.
|
|
52
51
|
|
|
53
52
|
Parameters
|
|
54
53
|
----------
|
|
@@ -61,7 +60,7 @@ def related_cycles(site_id: str):
|
|
|
61
60
|
The related `Cycle`s as `dict`.
|
|
62
61
|
"""
|
|
63
62
|
nodes = find_related(SchemaType.SITE, site_id, SchemaType.CYCLE)
|
|
64
|
-
return non_empty_list(map(lambda node:
|
|
63
|
+
return non_empty_list(map(lambda node: _load_calculated_node(node, SchemaType.CYCLE), nodes or []))
|
|
65
64
|
|
|
66
65
|
|
|
67
66
|
def _cycle_end_year(cycle: dict):
|
|
@@ -75,6 +74,10 @@ def related_years(site: dict):
|
|
|
75
74
|
)
|
|
76
75
|
|
|
77
76
|
|
|
77
|
+
def related_months(site: dict):
|
|
78
|
+
return cached_value(site)
|
|
79
|
+
|
|
80
|
+
|
|
78
81
|
def valid_site_type(site: dict, site_types=[SiteSiteType.CROPLAND.value, SiteSiteType.PERMANENT_PASTURE.value]):
|
|
79
82
|
"""
|
|
80
83
|
Check if the site `siteType` is allowed.
|
|
@@ -603,26 +603,3 @@ def get_pasture_system_terms():
|
|
|
603
603
|
'name': 'pasture'
|
|
604
604
|
}, limit=LIMIT)
|
|
605
605
|
return list(map(lambda n: n["@id"], terms))
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
def get_long_fallow_land_cover_terms():
|
|
609
|
-
"""
|
|
610
|
-
Find all `landCover` terms with the name `long fallow`:
|
|
611
|
-
https://hestia.earth/glossary?termType=landCover&query=long%fallow
|
|
612
|
-
|
|
613
|
-
Returns
|
|
614
|
-
-------
|
|
615
|
-
list
|
|
616
|
-
List of matching term `@id` as `str`.
|
|
617
|
-
"""
|
|
618
|
-
terms = search({
|
|
619
|
-
"bool": {
|
|
620
|
-
"must": [
|
|
621
|
-
{"match": {"@type": SchemaType.TERM.value}},
|
|
622
|
-
{"match": {"termType.keyword": TermTermType.LANDCOVER.value}},
|
|
623
|
-
{"match_phrase_prefix": {"name": "long"}},
|
|
624
|
-
{"match": {"name": "fallow"}}
|
|
625
|
-
],
|
|
626
|
-
}
|
|
627
|
-
}, limit=LIMIT)
|
|
628
|
-
return list(map(lambda n: n["@id"], terms))
|
hestia_earth/models/version.py
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
VERSION = '0.59.
|
|
1
|
+
VERSION = '0.59.5'
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: hestia-earth-models
|
|
3
|
-
Version: 0.59.
|
|
3
|
+
Version: 0.59.5
|
|
4
4
|
Summary: Hestia's set of modules for filling gaps in the activity data using external datasets (e.g. populating soil properties with a geospatial dataset using provided coordinates) and internal lookups (e.g. populating machinery use from fuel use). Includes rules for when gaps should be filled versus not (e.g. never gap fill yield, gap fill crop residue if yield provided etc.).
|
|
5
5
|
Home-page: https://gitlab.com/hestia-earth/hestia-engine-models
|
|
6
6
|
Author: Hestia Team
|
|
@@ -18,7 +18,7 @@ Requires-Dist: CurrencyConverter ==0.16.8
|
|
|
18
18
|
Requires-Dist: haversine >=2.7.0
|
|
19
19
|
Requires-Dist: pydash
|
|
20
20
|
Provides-Extra: spatial
|
|
21
|
-
Requires-Dist: hestia-earth.earth-engine >=0.4.
|
|
21
|
+
Requires-Dist: hestia-earth.earth-engine >=0.4.7 ; extra == 'spatial'
|
|
22
22
|
|
|
23
23
|
# Hestia Engine Models
|
|
24
24
|
|