hestia-earth-models 0.59.1__py3-none-any.whl → 0.59.2__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.

Files changed (33) hide show
  1. hestia_earth/models/cycle/irrigatedTypeUnspecified.py +6 -7
  2. hestia_earth/models/impact_assessment/irrigated.py +4 -1
  3. hestia_earth/models/ipcc2006/n2OToAirCropResidueDecompositionIndirect.py +3 -3
  4. hestia_earth/models/ipcc2006/n2OToAirExcretaIndirect.py +3 -3
  5. hestia_earth/models/ipcc2006/n2OToAirInorganicFertiliserIndirect.py +5 -4
  6. hestia_earth/models/ipcc2006/n2OToAirOrganicFertiliserIndirect.py +5 -4
  7. hestia_earth/models/ipcc2006/utils.py +0 -16
  8. hestia_earth/models/ipcc2019/n2OToAirCropResidueDecompositionDirect.py +8 -7
  9. hestia_earth/models/ipcc2019/n2OToAirCropResidueDecompositionIndirect.py +100 -0
  10. hestia_earth/models/ipcc2019/n2OToAirExcretaIndirect.py +100 -0
  11. hestia_earth/models/ipcc2019/n2OToAirInorganicFertiliserIndirect.py +54 -61
  12. hestia_earth/models/ipcc2019/n2OToAirOrganicFertiliserIndirect.py +58 -66
  13. hestia_earth/models/ipcc2019/nh3ToAirOrganicFertiliser.py +104 -0
  14. hestia_earth/models/ipcc2019/noxToAirOrganicFertiliser.py +104 -0
  15. hestia_earth/models/ipcc2019/organicCarbonPerHa.py +56 -18
  16. hestia_earth/models/ipcc2019/utils.py +28 -16
  17. hestia_earth/models/site/soilMeasurement.py +197 -0
  18. hestia_earth/models/utils/cycle.py +7 -6
  19. hestia_earth/models/utils/emission.py +15 -0
  20. hestia_earth/models/version.py +1 -1
  21. {hestia_earth_models-0.59.1.dist-info → hestia_earth_models-0.59.2.dist-info}/METADATA +1 -1
  22. {hestia_earth_models-0.59.1.dist-info → hestia_earth_models-0.59.2.dist-info}/RECORD +33 -23
  23. tests/models/ipcc2019/test_n2OToAirCropResidueDecompositionIndirect.py +71 -0
  24. tests/models/ipcc2019/test_n2OToAirExcretaIndirect.py +71 -0
  25. tests/models/ipcc2019/test_n2OToAirInorganicFertiliserIndirect.py +36 -13
  26. tests/models/ipcc2019/test_n2OToAirOrganicFertiliserIndirect.py +36 -13
  27. tests/models/ipcc2019/test_nh3ToAirOrganicFertiliser.py +35 -0
  28. tests/models/ipcc2019/test_noxToAirOrganicFertiliser.py +35 -0
  29. tests/models/ipcc2019/test_organicCarbonPerHa.py +45 -4
  30. tests/models/site/test_soilMeasurement.py +159 -0
  31. {hestia_earth_models-0.59.1.dist-info → hestia_earth_models-0.59.2.dist-info}/LICENSE +0 -0
  32. {hestia_earth_models-0.59.1.dist-info → hestia_earth_models-0.59.2.dist-info}/WHEEL +0 -0
  33. {hestia_earth_models-0.59.1.dist-info → hestia_earth_models-0.59.2.dist-info}/top_level.txt +0 -0
@@ -1,7 +1,7 @@
1
1
  from hestia_earth.schema import TermTermType, CycleFunctionalUnit
2
- from hestia_earth.utils.model import filter_list_term_type
3
2
 
4
3
  from hestia_earth.models.log import logRequirements, logShouldRun
4
+ from hestia_earth.models.utils.cycle import is_irrigated
5
5
  from hestia_earth.models.utils.practice import _new_practice
6
6
  from hestia_earth.models.utils.input import get_total_irrigation_m3
7
7
  from hestia_earth.models.utils.completeness import _is_term_type_complete
@@ -12,7 +12,7 @@ REQUIREMENTS = {
12
12
  "completeness.water": "",
13
13
  "functionalUnit": "1 ha",
14
14
  "none": {
15
- "practices": [{"@type": "Practice", "units": ["%", "% area"], "term.termType": "waterRegime"}]
15
+ "practices": [{"@type": "Practice", "value": "> 0", "term.termType": "waterRegime"}]
16
16
  },
17
17
  "optional": {
18
18
  "inputs": [{"@type": "Input", "term.termType": "water", "value": ""}]
@@ -24,6 +24,9 @@ RETURNS = {
24
24
  "value": ""
25
25
  }]
26
26
  }
27
+ LOOKUPS = {
28
+ "waterRegime": "irrigated"
29
+ }
27
30
  TERM_ID = 'irrigatedTypeUnspecified'
28
31
  MIN_IRRIGATION_M3 = 250
29
32
 
@@ -34,9 +37,6 @@ def _practice(value: float):
34
37
  return practice
35
38
 
36
39
 
37
- def _is_irrigation_practice(practice: dict): return practice.get('term', {}).get('units', '') in ['%', '% area']
38
-
39
-
40
40
  def _run(irrigation_m3: float):
41
41
  value = 100 if irrigation_m3 > MIN_IRRIGATION_M3 else 0
42
42
  return [_practice(value)]
@@ -47,8 +47,7 @@ def _should_run(cycle: dict):
47
47
  water_complete = _is_term_type_complete(cycle, TermTermType.WATER)
48
48
  irrigation_value_m3 = get_total_irrigation_m3(cycle)
49
49
 
50
- water_regime = filter_list_term_type(cycle.get('practices', []), TermTermType.WATERREGIME)
51
- no_irrigation_practice = not any(map(_is_irrigation_practice, water_regime))
50
+ no_irrigation_practice = not is_irrigated(cycle, model=MODEL, term=TERM_ID)
52
51
 
53
52
  logRequirements(cycle, model=MODEL, term=TERM_ID,
54
53
  no_irrigation_practice=no_irrigation_practice,
@@ -18,10 +18,13 @@ REQUIREMENTS = {
18
18
  RETURNS = {
19
19
  "`true` if the `Cycle` was irrigated, `false` otherwise": ""
20
20
  }
21
+ LOOKUPS = {
22
+ "waterRegime": "irrigated"
23
+ }
21
24
  MODEL_KEY = 'irrigated'
22
25
 
23
26
 
24
27
  def run(impact: dict):
25
- value = is_irrigated(impact.get('cycle', {}))
28
+ value = is_irrigated(impact.get('cycle', {}), model=MODEL, model_key=MODEL_KEY)
26
29
  logger.debug('model=%s, key=%s, value=%s', MODEL, MODEL_KEY, value)
27
30
  return value
@@ -3,8 +3,8 @@ from hestia_earth.schema import EmissionMethodTier, TermTermType
3
3
  from hestia_earth.models.log import logRequirements, logShouldRun
4
4
  from hestia_earth.models.utils.constant import Units, get_atomic_conversion
5
5
  from hestia_earth.models.utils.completeness import _is_term_type_complete
6
- from hestia_earth.models.utils.emission import _new_emission
7
- from .utils import get_nh3_no3_nox_to_n, COEFF_NH3NOX_N2O, COEFF_NO3_N2O
6
+ from hestia_earth.models.utils.emission import _new_emission, get_nh3_no3_nox_to_n
7
+ from .utils import COEFF_NH3NOX_N2O, COEFF_NO3_N2O
8
8
  from . import MODEL
9
9
 
10
10
  REQUIREMENTS = {
@@ -43,7 +43,7 @@ def _run(no3: float, nh3: float, nox: float):
43
43
 
44
44
 
45
45
  def _should_run(cycle: dict):
46
- nh3_n, no3_n, nox_n = get_nh3_no3_nox_to_n(cycle, NH3_TERM_ID, NO3_TERM_ID, NOX_TERM_ID, allow_none=False)
46
+ nh3_n, no3_n, nox_n = get_nh3_no3_nox_to_n(cycle, NH3_TERM_ID, NO3_TERM_ID, NOX_TERM_ID)
47
47
  term_type_complete = _is_term_type_complete(cycle, TermTermType.CROPRESIDUE)
48
48
 
49
49
  logRequirements(cycle, model=MODEL, term=TERM_ID,
@@ -3,8 +3,8 @@ from hestia_earth.schema import EmissionMethodTier, TermTermType
3
3
  from hestia_earth.models.log import logRequirements, logShouldRun
4
4
  from hestia_earth.models.utils.constant import Units, get_atomic_conversion
5
5
  from hestia_earth.models.utils.completeness import _is_term_type_complete
6
- from hestia_earth.models.utils.emission import _new_emission
7
- from .utils import get_nh3_no3_nox_to_n, COEFF_NH3NOX_N2O, COEFF_NO3_N2O
6
+ from hestia_earth.models.utils.emission import _new_emission, get_nh3_no3_nox_to_n
7
+ from .utils import COEFF_NH3NOX_N2O, COEFF_NO3_N2O
8
8
  from . import MODEL
9
9
 
10
10
  REQUIREMENTS = {
@@ -43,7 +43,7 @@ def _run(no3_n: float, nh3_n: float, nox_n: float):
43
43
 
44
44
 
45
45
  def _should_run(cycle: dict):
46
- nh3_n, no3_n, nox_n = get_nh3_no3_nox_to_n(cycle, NH3_TERM_ID, NO3_TERM_ID, NOX_TERM_ID, allow_none=False)
46
+ nh3_n, no3_n, nox_n = get_nh3_no3_nox_to_n(cycle, NH3_TERM_ID, NO3_TERM_ID, NOX_TERM_ID)
47
47
  term_type_complete = _is_term_type_complete(cycle, TermTermType.EXCRETA)
48
48
 
49
49
  logRequirements(cycle, model=MODEL, term=TERM_ID,
@@ -1,11 +1,12 @@
1
1
  from hestia_earth.schema import EmissionMethodTier
2
2
 
3
3
  from hestia_earth.models.log import debugValues, logRequirements, logShouldRun
4
+ from hestia_earth.models.utils import sum_values
4
5
  from hestia_earth.models.utils.constant import Units, get_atomic_conversion
5
6
  from hestia_earth.models.utils.completeness import _is_term_type_complete
6
7
  from hestia_earth.models.utils.cycle import get_inorganic_fertiliser_N_total
7
- from hestia_earth.models.utils.emission import _new_emission
8
- from .utils import get_nh3_no3_nox_to_n, COEFF_NH3NOX_N2O, COEFF_NO3_N2O
8
+ from hestia_earth.models.utils.emission import _new_emission, get_nh3_no3_nox_to_n
9
+ from .utils import COEFF_NH3NOX_N2O, COEFF_NO3_N2O
9
10
  from . import MODEL
10
11
 
11
12
  REQUIREMENTS = {
@@ -54,9 +55,9 @@ def _run(cycle: dict, N_total: float):
54
55
  nh3_n=nh3_n,
55
56
  nox_n=nox_n)
56
57
  value = COEFF_NH3NOX_N2O * (
57
- N_total * 0.1 if nox_n == 0 or nh3_n == 0 else nh3_n + nox_n
58
+ sum_values([nh3_n, nox_n]) or N_total * 0.2
58
59
  ) + COEFF_NO3_N2O * (
59
- N_total * 0.3 if no3_n == 0 else no3_n
60
+ no3_n or N_total * 0.3
60
61
  )
61
62
  return [_emission(value * get_atomic_conversion(Units.KG_N2O, Units.TO_N))]
62
63
 
@@ -1,11 +1,12 @@
1
1
  from hestia_earth.schema import EmissionMethodTier
2
2
 
3
3
  from hestia_earth.models.log import debugValues, logRequirements, logShouldRun
4
+ from hestia_earth.models.utils import sum_values
4
5
  from hestia_earth.models.utils.constant import Units, get_atomic_conversion
5
6
  from hestia_earth.models.utils.completeness import _is_term_type_complete
6
7
  from hestia_earth.models.utils.cycle import get_organic_fertiliser_N_total
7
- from hestia_earth.models.utils.emission import _new_emission
8
- from .utils import get_nh3_no3_nox_to_n, COEFF_NH3NOX_N2O, COEFF_NO3_N2O
8
+ from hestia_earth.models.utils.emission import _new_emission, get_nh3_no3_nox_to_n
9
+ from .utils import COEFF_NH3NOX_N2O, COEFF_NO3_N2O
9
10
  from . import MODEL
10
11
 
11
12
  REQUIREMENTS = {
@@ -53,9 +54,9 @@ def _run(cycle: dict, N_total: float):
53
54
  nh3_n=nh3_n,
54
55
  nox_n=nox_n)
55
56
  value = COEFF_NH3NOX_N2O * (
56
- N_total * 0.2 if nox_n == 0 or nh3_n == 0 else nh3_n + nox_n
57
+ sum_values([nh3_n, nox_n]) or N_total * 0.2
57
58
  ) + COEFF_NO3_N2O * (
58
- N_total * 0.3 if no3_n == 0 else no3_n
59
+ no3_n or N_total * 0.3
59
60
  )
60
61
  return [_emission(value * get_atomic_conversion(Units.KG_N2O, Units.TO_N))]
61
62
 
@@ -1,25 +1,9 @@
1
1
  from hestia_earth.utils.model import find_primary_product
2
2
 
3
- from hestia_earth.models.utils.constant import Units, get_atomic_conversion
4
- from hestia_earth.models.utils.blank_node import find_terms_value
5
-
6
3
  COEFF_NH3NOX_N2O = 0.01
7
4
  COEFF_NO3_N2O = 0.0075
8
5
 
9
6
 
10
- def get_nh3_no3_nox_to_n(cycle: dict, nh3_term_id: str, no3_term_id: str, nox_term_id: str, allow_none: bool = True):
11
- default_value = 0 if allow_none else None
12
-
13
- nh3 = find_terms_value(cycle.get('emissions', []), nh3_term_id, default=default_value)
14
- nh3 = None if nh3 is None else nh3 / get_atomic_conversion(Units.KG_NH3, Units.TO_N)
15
- no3 = find_terms_value(cycle.get('emissions', []), no3_term_id, default=default_value)
16
- no3 = None if no3 is None else no3 / get_atomic_conversion(Units.KG_NO3, Units.TO_N)
17
- nox = find_terms_value(cycle.get('emissions', []), nox_term_id, default=default_value)
18
- nox = None if nox is None else nox / get_atomic_conversion(Units.KG_NOX, Units.TO_N)
19
-
20
- return (nh3, no3, nox)
21
-
22
-
23
7
  def get_N_N2O_excreta_coeff_from_primary_product(cycle: dict):
24
8
  product = find_primary_product(cycle)
25
9
  term = product.get('term', {}) if product else {}
@@ -62,13 +62,16 @@ def _emission(value: float, min: float, max: float, sd: float, aggregated: bool
62
62
  return emission
63
63
 
64
64
 
65
- def _run(cycle: dict, N_total: float, ecoClimateZone: str = None, flooded_rice: bool = False):
65
+ def _run(cycle: dict, N_total: float, flooded_rice: bool = False):
66
+ ecoClimateZone = get_ecoClimateZone(cycle)
67
+
66
68
  converted_N_total = N_total * get_atomic_conversion(Units.KG_N2O, Units.TO_N)
67
69
  factors, aggregated = get_N2O_factors(
68
70
  TERM_ID, cycle, TermTermType.CROPRESIDUE, ecoClimateZone=ecoClimateZone, flooded_rice=flooded_rice
69
71
  )
70
72
 
71
73
  debugValues(cycle, model=MODEL, term=TERM_ID,
74
+ ecoClimateZone=ecoClimateZone,
72
75
  factors_used=log_as_table(factors),
73
76
  aggregated=aggregated)
74
77
 
@@ -82,21 +85,19 @@ def _run(cycle: dict, N_total: float, ecoClimateZone: str = None, flooded_rice:
82
85
  def _should_run(cycle: dict):
83
86
  term_type_complete = _is_term_type_complete(cycle, TermTermType.CROPRESIDUE)
84
87
  N_crop_residue = get_crop_residue_decomposition_N_total(cycle)
85
- ecoClimateZone = get_ecoClimateZone(cycle)
86
88
 
87
89
  flooded_rice = has_flooded_rice(cycle.get('products', []))
88
90
 
89
91
  logRequirements(cycle, model=MODEL, term=TERM_ID,
90
92
  term_type_cropResidue_complete=term_type_complete,
91
93
  N_crop_residue=N_crop_residue,
92
- has_flooded_rice=flooded_rice,
93
- ecoClimateZone=ecoClimateZone)
94
+ has_flooded_rice=flooded_rice)
94
95
 
95
96
  should_run = all([N_crop_residue is not None, term_type_complete])
96
97
  logShouldRun(cycle, MODEL, TERM_ID, should_run, methodTier=TIER)
97
- return should_run, N_crop_residue, ecoClimateZone, flooded_rice
98
+ return should_run, N_crop_residue, flooded_rice
98
99
 
99
100
 
100
101
  def run(cycle: dict):
101
- should_run, N_total, ecoClimateZone, flooded_rice = _should_run(cycle)
102
- return _run(cycle, N_total, ecoClimateZone, flooded_rice) if should_run else []
102
+ should_run, N_total, flooded_rice = _should_run(cycle)
103
+ return _run(cycle, N_total, flooded_rice) if should_run else []
@@ -0,0 +1,100 @@
1
+ from hestia_earth.schema import EmissionMethodTier, EmissionStatsDefinition, TermTermType
2
+
3
+ from hestia_earth.models.log import debugValues, logRequirements, logShouldRun, log_as_table
4
+ from hestia_earth.models.utils.constant import Units, get_atomic_conversion
5
+ from hestia_earth.models.utils.completeness import _is_term_type_complete
6
+ from hestia_earth.models.utils.emission import _new_emission, get_nh3_no3_nox_to_n
7
+ from hestia_earth.models.utils.cycle import get_ecoClimateZone
8
+ from .utils import ecoClimate_factors, EF4_FACTORS, EF5_FACTORS
9
+ from . import MODEL
10
+
11
+ REQUIREMENTS = {
12
+ "Cycle": {
13
+ "completeness.cropResidue": "True",
14
+ "emissions": [
15
+ {"@type": "Emission", "value": "", "term.@id": "no3ToGroundwaterCropResidueDecomposition"},
16
+ {"@type": "Emission", "value": "", "term.@id": "nh3ToAirCropResidueDecomposition"},
17
+ {"@type": "Emission", "value": "", "term.@id": "noxToAirCropResidueDecomposition"}
18
+ ],
19
+ "optional": {
20
+ "site": {
21
+ "@type": "Site",
22
+ "measurements": [
23
+ {"@type": "Measurement", "value": "", "term.@id": "ecoClimateZone"}
24
+ ]
25
+ }
26
+ }
27
+ }
28
+ }
29
+ RETURNS = {
30
+ "Emission": [{
31
+ "value": "",
32
+ "methodTier": "tier 1"
33
+ }]
34
+ }
35
+ TERM_ID = 'n2OToAirCropResidueDecompositionIndirect'
36
+ TIER = EmissionMethodTier.TIER_1.value
37
+ NO3_TERM_ID = 'no3ToGroundwaterCropResidueDecomposition'
38
+ NH3_TERM_ID = 'nh3ToAirCropResidueDecomposition'
39
+ NOX_TERM_ID = 'noxToAirCropResidueDecomposition'
40
+
41
+
42
+ def _emission(value: float, min: float, max: float, aggregated: bool = False):
43
+ emission = _new_emission(TERM_ID, MODEL)
44
+ emission['value'] = [value]
45
+ emission['min'] = [min]
46
+ emission['max'] = [max]
47
+ emission['methodTier'] = TIER
48
+ emission['statsDefinition'] = EmissionStatsDefinition.MODELLED.value
49
+ emission['methodModelDescription'] = 'Aggregated version' if aggregated else 'Disaggregated version'
50
+ return emission
51
+
52
+
53
+ def _run(cycle: dict, no3: float, nh3: float, nox: float):
54
+ ecoClimateZone = get_ecoClimateZone(cycle)
55
+
56
+ ef4, aggregated = ecoClimate_factors(EF4_FACTORS, ecoClimateZone=ecoClimateZone)
57
+ ef5 = EF5_FACTORS.get('default')
58
+
59
+ debugValues(cycle, model=MODEL, term=TERM_ID,
60
+ ecoClimateZone=ecoClimateZone,
61
+ ef4_factors_used=log_as_table(ef4),
62
+ ef5_factors_used=log_as_table(ef5),
63
+ aggregated=aggregated)
64
+
65
+ value = sum([
66
+ no3 * ef5['value'],
67
+ nh3 * ef4['value'],
68
+ nox * ef4['value']
69
+ ]) * get_atomic_conversion(Units.KG_N2O, Units.TO_N)
70
+ min = sum([
71
+ no3 * ef5['min'],
72
+ nh3 * ef4['min'],
73
+ nox * ef4['min']
74
+ ]) * get_atomic_conversion(Units.KG_N2O, Units.TO_N)
75
+ max = sum([
76
+ no3 * ef5['max'],
77
+ nh3 * ef4['max'],
78
+ nox * ef4['max']
79
+ ]) * get_atomic_conversion(Units.KG_N2O, Units.TO_N)
80
+ return [_emission(value, min, max, aggregated=aggregated)]
81
+
82
+
83
+ def _should_run(cycle: dict):
84
+ nh3_n, no3_n, nox_n = get_nh3_no3_nox_to_n(cycle, NH3_TERM_ID, NO3_TERM_ID, NOX_TERM_ID)
85
+ term_type_complete = _is_term_type_complete(cycle, TermTermType.CROPRESIDUE)
86
+
87
+ logRequirements(cycle, model=MODEL, term=TERM_ID,
88
+ no3_n=no3_n,
89
+ nh3_n=nh3_n,
90
+ nox_n=nox_n,
91
+ term_type_cropResidue_complete=term_type_complete)
92
+
93
+ should_run = all([no3_n is not None, nh3_n is not None, nox_n is not None, term_type_complete])
94
+ logShouldRun(cycle, MODEL, TERM_ID, should_run, methodTier=TIER)
95
+ return should_run, no3_n, nh3_n, nox_n
96
+
97
+
98
+ def run(cycle: dict):
99
+ should_run, no3, nh3, nox = _should_run(cycle)
100
+ return _run(cycle, no3, nh3, nox) if should_run else []
@@ -0,0 +1,100 @@
1
+ from hestia_earth.schema import EmissionMethodTier, EmissionStatsDefinition, TermTermType
2
+
3
+ from hestia_earth.models.log import debugValues, logRequirements, logShouldRun, log_as_table
4
+ from hestia_earth.models.utils.constant import Units, get_atomic_conversion
5
+ from hestia_earth.models.utils.completeness import _is_term_type_complete
6
+ from hestia_earth.models.utils.emission import _new_emission, get_nh3_no3_nox_to_n
7
+ from hestia_earth.models.utils.cycle import get_ecoClimateZone
8
+ from .utils import ecoClimate_factors, EF4_FACTORS, EF5_FACTORS
9
+ from . import MODEL
10
+
11
+ REQUIREMENTS = {
12
+ "Cycle": {
13
+ "completeness.excreta": "True",
14
+ "emissions": [
15
+ {"@type": "Emission", "value": "", "term.@id": "no3ToGroundwaterExcreta"},
16
+ {"@type": "Emission", "value": "", "term.@id": "nh3ToAirExcreta"},
17
+ {"@type": "Emission", "value": "", "term.@id": "noxToAirExcreta"}
18
+ ],
19
+ "optional": {
20
+ "site": {
21
+ "@type": "Site",
22
+ "measurements": [
23
+ {"@type": "Measurement", "value": "", "term.@id": "ecoClimateZone"}
24
+ ]
25
+ }
26
+ }
27
+ }
28
+ }
29
+ RETURNS = {
30
+ "Emission": [{
31
+ "value": "",
32
+ "methodTier": "tier 1"
33
+ }]
34
+ }
35
+ TERM_ID = 'n2OToAirExcretaIndirect'
36
+ TIER = EmissionMethodTier.TIER_1.value
37
+ NO3_TERM_ID = 'no3ToGroundwaterExcreta'
38
+ NH3_TERM_ID = 'nh3ToAirExcreta'
39
+ NOX_TERM_ID = 'noxToAirExcreta'
40
+
41
+
42
+ def _emission(value: float, min: float, max: float, aggregated: bool = False):
43
+ emission = _new_emission(TERM_ID, MODEL)
44
+ emission['value'] = [value]
45
+ emission['min'] = [min]
46
+ emission['max'] = [max]
47
+ emission['methodTier'] = TIER
48
+ emission['statsDefinition'] = EmissionStatsDefinition.MODELLED.value
49
+ emission['methodModelDescription'] = 'Aggregated version' if aggregated else 'Disaggregated version'
50
+ return emission
51
+
52
+
53
+ def _run(cycle: dict, no3: float, nh3: float, nox: float):
54
+ ecoClimateZone = get_ecoClimateZone(cycle)
55
+
56
+ ef4, aggregated = ecoClimate_factors(EF4_FACTORS, ecoClimateZone=ecoClimateZone)
57
+ ef5 = EF5_FACTORS.get('default')
58
+
59
+ debugValues(cycle, model=MODEL, term=TERM_ID,
60
+ ecoClimateZone=ecoClimateZone,
61
+ ef4_factors_used=log_as_table(ef4),
62
+ ef5_factors_used=log_as_table(ef5),
63
+ aggregated=aggregated)
64
+
65
+ value = sum([
66
+ no3 * ef5['value'],
67
+ nh3 * ef4['value'],
68
+ nox * ef4['value']
69
+ ]) * get_atomic_conversion(Units.KG_N2O, Units.TO_N)
70
+ min = sum([
71
+ no3 * ef5['min'],
72
+ nh3 * ef4['min'],
73
+ nox * ef4['min']
74
+ ]) * get_atomic_conversion(Units.KG_N2O, Units.TO_N)
75
+ max = sum([
76
+ no3 * ef5['max'],
77
+ nh3 * ef4['max'],
78
+ nox * ef4['max']
79
+ ]) * get_atomic_conversion(Units.KG_N2O, Units.TO_N)
80
+ return [_emission(value, min, max, aggregated=aggregated)]
81
+
82
+
83
+ def _should_run(cycle: dict):
84
+ nh3_n, no3_n, nox_n = get_nh3_no3_nox_to_n(cycle, NH3_TERM_ID, NO3_TERM_ID, NOX_TERM_ID)
85
+ term_type_complete = _is_term_type_complete(cycle, TermTermType.EXCRETA)
86
+
87
+ logRequirements(cycle, model=MODEL, term=TERM_ID,
88
+ no3_n=no3_n,
89
+ nh3_n=nh3_n,
90
+ nox_n=nox_n,
91
+ term_type_cropResidue_complete=term_type_complete)
92
+
93
+ should_run = all([no3_n is not None, nh3_n is not None, nox_n is not None, term_type_complete])
94
+ logShouldRun(cycle, MODEL, TERM_ID, should_run, methodTier=TIER)
95
+ return should_run, no3_n, nh3_n, nox_n
96
+
97
+
98
+ def run(cycle: dict):
99
+ should_run, no3, nh3, nox = _should_run(cycle)
100
+ return _run(cycle, no3, nh3, nox) if should_run else []
@@ -1,107 +1,100 @@
1
- from hestia_earth.schema import EmissionMethodTier, EmissionStatsDefinition, TermTermType
1
+ from hestia_earth.schema import EmissionMethodTier, EmissionStatsDefinition
2
2
 
3
- from hestia_earth.models.log import debugValues, logRequirements, logShouldRun
3
+ from hestia_earth.models.log import debugValues, logRequirements, logShouldRun, log_as_table
4
4
  from hestia_earth.models.utils.constant import Units, get_atomic_conversion
5
5
  from hestia_earth.models.utils.completeness import _is_term_type_complete
6
- from hestia_earth.models.utils.cycle import get_inorganic_fertiliser_N_total, get_ecoClimateZone
7
- from hestia_earth.models.utils.emission import _new_emission
8
- from .utils import get_nh3_no3_nox_to_n, COEFF_NO3_N2O, COEFF_N_NH3NOX_inorganic, get_FracNH3NOx_N2O, get_FracLEACH_H
6
+ from hestia_earth.models.utils.emission import _new_emission, get_nh3_no3_nox_to_n
7
+ from hestia_earth.models.utils.cycle import get_ecoClimateZone
8
+ from .utils import ecoClimate_factors, EF4_FACTORS, EF5_FACTORS
9
9
  from . import MODEL
10
10
 
11
11
  REQUIREMENTS = {
12
12
  "Cycle": {
13
13
  "completeness.fertiliser": "True",
14
- "completeness.water": "True",
15
- "inputs": [{
16
- "@type": "Input",
17
- "value": "",
18
- "term.units": ["kg", "kg N"],
19
- "term.termType": "inorganicFertiliser",
20
- "optional": {
21
- "properties": [{"@type": "Property", "value": "", "term.@id": "nitrogenContent"}]
22
- }
23
- }],
24
14
  "emissions": [
25
15
  {"@type": "Emission", "value": "", "term.@id": "no3ToGroundwaterInorganicFertiliser"},
26
16
  {"@type": "Emission", "value": "", "term.@id": "nh3ToAirInorganicFertiliser"},
27
17
  {"@type": "Emission", "value": "", "term.@id": "noxToAirInorganicFertiliser"}
28
18
  ],
29
- "site": {
30
- "@type": "Site",
31
- "measurements": [{"@type": "Measurement", "value": "", "term.@id": "ecoClimateZone"}]
32
- },
33
19
  "optional": {
34
- "practices": [{"@type": "Practice", "value": "", "term.termType": "waterRegime"}]
20
+ "site": {
21
+ "@type": "Site",
22
+ "measurements": [
23
+ {"@type": "Measurement", "value": "", "term.@id": "ecoClimateZone"}
24
+ ]
25
+ }
35
26
  }
36
27
  }
37
28
  }
38
29
  RETURNS = {
39
30
  "Emission": [{
40
31
  "value": "",
41
- "sd": "",
42
- "min": "",
43
- "max": "",
44
- "methodTier": "tier 1",
45
- "statsDefinition": "modelled"
32
+ "methodTier": "tier 1"
46
33
  }]
47
34
  }
48
-
49
35
  TERM_ID = 'n2OToAirInorganicFertiliserIndirect'
36
+ TIER = EmissionMethodTier.TIER_1.value
50
37
  NO3_TERM_ID = 'no3ToGroundwaterInorganicFertiliser'
51
38
  NH3_TERM_ID = 'nh3ToAirInorganicFertiliser'
52
39
  NOX_TERM_ID = 'noxToAirInorganicFertiliser'
53
- TIER = EmissionMethodTier.TIER_1.value
54
40
 
55
41
 
56
- def _emission(value: float, sd: float, min: float, max: float):
42
+ def _emission(value: float, min: float, max: float, aggregated: bool = False):
57
43
  emission = _new_emission(TERM_ID, MODEL)
58
44
  emission['value'] = [value]
59
- emission['sd'] = [sd]
60
45
  emission['min'] = [min]
61
46
  emission['max'] = [max]
62
47
  emission['methodTier'] = TIER
63
48
  emission['statsDefinition'] = EmissionStatsDefinition.MODELLED.value
49
+ emission['methodModelDescription'] = 'Aggregated version' if aggregated else 'Disaggregated version'
64
50
  return emission
65
51
 
66
52
 
67
- def _run(cycle: dict):
68
- N_total = get_inorganic_fertiliser_N_total(cycle)
69
- nh3_n, no3_n, nox_n = get_nh3_no3_nox_to_n(cycle, NH3_TERM_ID, NO3_TERM_ID, NOX_TERM_ID)
53
+ def _run(cycle: dict, no3: float, nh3: float, nox: float):
54
+ ecoClimateZone = get_ecoClimateZone(cycle)
55
+
56
+ ef4, aggregated = ecoClimate_factors(EF4_FACTORS, ecoClimateZone=ecoClimateZone)
57
+ ef5 = EF5_FACTORS.get('default')
58
+
70
59
  debugValues(cycle, model=MODEL, term=TERM_ID,
71
- no3_n=no3_n,
72
- nh3_n=nh3_n,
73
- nox_n=nox_n)
74
- value, min_val, max_val, std = [
75
- get_FracNH3NOx_N2O(cycle, TERM_ID)[x] * (
76
- N_total * COEFF_N_NH3NOX_inorganic[x] if nox_n == 0 or nh3_n == 0 else nh3_n + nox_n
77
- ) +
78
- COEFF_NO3_N2O[x] * (
79
- N_total * get_FracLEACH_H(cycle, TERM_ID)[x] if no3_n == 0 else no3_n
80
- ) for x in range(4)
81
- ]
82
- return [_emission(
83
- value * get_atomic_conversion(Units.KG_N2O, Units.TO_N),
84
- std * get_atomic_conversion(Units.KG_N2O, Units.TO_N),
85
- min_val * get_atomic_conversion(Units.KG_N2O, Units.TO_N),
86
- max_val * get_atomic_conversion(Units.KG_N2O, Units.TO_N)
87
- )]
60
+ ecoClimateZone=ecoClimateZone,
61
+ ef4_factors_used=log_as_table(ef4),
62
+ ef5_factors_used=log_as_table(ef5),
63
+ aggregated=aggregated)
64
+
65
+ value = sum([
66
+ no3 * ef5['value'],
67
+ nh3 * ef4['value'],
68
+ nox * ef4['value']
69
+ ]) * get_atomic_conversion(Units.KG_N2O, Units.TO_N)
70
+ min = sum([
71
+ no3 * ef5['min'],
72
+ nh3 * ef4['min'],
73
+ nox * ef4['min']
74
+ ]) * get_atomic_conversion(Units.KG_N2O, Units.TO_N)
75
+ max = sum([
76
+ no3 * ef5['max'],
77
+ nh3 * ef4['max'],
78
+ nox * ef4['max']
79
+ ]) * get_atomic_conversion(Units.KG_N2O, Units.TO_N)
80
+ return [_emission(value, min, max, aggregated=aggregated)]
88
81
 
89
82
 
90
83
  def _should_run(cycle: dict):
91
- N_inorganic_fertiliser = get_inorganic_fertiliser_N_total(cycle)
92
- ecoClimateZone = get_ecoClimateZone(cycle)
93
- fertiliser_complete = _is_term_type_complete(cycle, 'fertiliser')
94
- water_complete = _is_term_type_complete(cycle, TermTermType.WATER)
84
+ nh3_n, no3_n, nox_n = get_nh3_no3_nox_to_n(cycle, NH3_TERM_ID, NO3_TERM_ID, NOX_TERM_ID)
85
+ term_type_complete = _is_term_type_complete(cycle, 'fertiliser')
95
86
 
96
87
  logRequirements(cycle, model=MODEL, term=TERM_ID,
97
- N_inorganic_fertiliser=N_inorganic_fertiliser,
98
- ecoClimateZone=ecoClimateZone,
99
- term_type_fertiliser_complete=fertiliser_complete,
100
- term_type_water_complete=water_complete)
88
+ no3_n=no3_n,
89
+ nh3_n=nh3_n,
90
+ nox_n=nox_n,
91
+ term_type_cropResidue_complete=term_type_complete)
101
92
 
102
- should_run = all([N_inorganic_fertiliser is not None, ecoClimateZone, fertiliser_complete, water_complete])
103
- logShouldRun(cycle, MODEL, TERM_ID, should_run)
104
- return should_run
93
+ should_run = all([no3_n is not None, nh3_n is not None, nox_n is not None, term_type_complete])
94
+ logShouldRun(cycle, MODEL, TERM_ID, should_run, methodTier=TIER)
95
+ return should_run, no3_n, nh3_n, nox_n
105
96
 
106
97
 
107
- def run(cycle: dict): return _run(cycle) if _should_run(cycle) else []
98
+ def run(cycle: dict):
99
+ should_run, no3, nh3, nox = _should_run(cycle)
100
+ return _run(cycle, no3, nh3, nox) if should_run else []