Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature floor mitigation (to be merged into master towards end of the year) #565

Draft
wants to merge 16 commits into
base: floor_mitigation
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Agrammon Change Log

- extend REST inputTemplate for json

- feature branch floor_mitigation, 2022-06-09, [email protected]

- pigs:
- floor mitigation has effect on both indoors and outdoors
- Slurry_Label_Open has 100% flow into outdoors

- pre, 2022-06-09, [email protected]

Expand Down
7 changes: 3 additions & 4 deletions lib/Agrammon/OutputFormatter/CSV.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ sub output-as-csv(
) is export {
return (gather for sorted-kv($outputs.get-outputs-hash) -> $module, $_ {
my $prefix = "$simulation-name;$dataset-id" if $simulation-name;
my $filters = $include-filters ?? 'total' !! '';
when Hash {
for sorted-kv($_) -> $output, $value {
next unless $model.should-print($module, $output, @print-set);

my $filters = '';
my $raw-value = $value;
my $var = $output;
my $unit = $model.output-unit($module, $output, $language);
Expand All @@ -42,7 +42,6 @@ sub output-as-csv(
}
when Array {
for sorted-kv($_) -> $instance-id, %instance-outputs {
my $filters = '';
for sorted-kv(%instance-outputs) -> $fq-name, %values {
my $q-name = module-with-instance($module, $instance-id, $fq-name);
for sorted-kv(%values) -> $output, $value {
Expand Down Expand Up @@ -82,8 +81,8 @@ sub add-filters(Agrammon::Outputs::FilterGroupCollection $collection,
for (@filters || ( :value('Uncategorized') ) ) {
my @data = (
|($prefix if $prefix),
$q-name,
$fq-name,
$q-name, # module
$fq-name, # variable
.<value>,
flat-value($raw-value) // '',
$unit
Expand Down
48 changes: 40 additions & 8 deletions lib/Agrammon/OutputFormatter/JSON.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ sub output-as-json(
Bool :$all-filters = False,
Bool :$short = False
) is export {
return get-data($model, $outputs, $include-filters, @print-set, $short, $language);
return get-data($model, $outputs, $include-filters, @print-set, $short, $language, :merge-filters);
}

sub output-for-gui(Agrammon::Model $model,
Expand All @@ -28,18 +28,24 @@ sub output-for-gui(Agrammon::Model $model,
);
}

sub get-data($model, $outputs, $include-filters, @print-set, $short, $language?) {
sub get-data($model, $outputs, $include-filters, @print-set, $short, $language?, :$merge-filters) {
my @records;
my $last-order = -1;
for sorted-kv($outputs.get-outputs-hash) -> $module, $_ {
when Hash {
for sorted-kv($_) -> $output, $raw-value {
my $var = $module ~ '::' ~ $output;
my $order = $model.output-labels($module, $output)<sort> || $last-order;
push @records, make-record($module, $output, $model, $raw-value, $var, $order, $short, :$language, :@print-set);
my $record = make-record($module, $output, $model, $raw-value, $var, $order, $short, :$language, :@print-set);
push @records, $record;
if $include-filters {
if $raw-value ~~ Agrammon::Outputs::FilterGroupCollection && $raw-value.has-filters {
add-filters(@records, $module, $output, $model, $raw-value, $var, $order, $short, :@print-set);
if $merge-filters {
merge-filters($record, $module, $output, $model, $raw-value, $var, $order, $short, $language, :@print-set);
}
else {
add-filters(@records, $module, $output, $model, $raw-value, $var, $order, $short, :@print-set);
}
}
}
$last-order = $order;
Expand All @@ -52,10 +58,16 @@ sub get-data($model, $outputs, $include-filters, @print-set, $short, $language?)
for sorted-kv(%values) -> $output, $raw-value {
my $order = $model.output-labels($fq-name, $output)<sort> || $last-order;
my $var = $q-name ~ '::' ~ $output;
push @records, make-record($fq-name, $output, $model, $raw-value, $var, $order, $short, $instance-id, :$language, :@print-set);
my $record = make-record($fq-name, $output, $model, $raw-value, $var, $order, $short, $instance-id, :$language, :@print-set);
push @records, $record;
if $include-filters {
if $raw-value ~~ Agrammon::Outputs::FilterGroupCollection && $raw-value.has-filters {
add-filters(@records, $fq-name, $output, $model, $raw-value, $var, $order, $short, :@print-set);
if $merge-filters {
merge-filters($record, $fq-name, $output, $model, $raw-value, $var, $order, $short, $language, :@print-set);
}
else {
add-filters(@records, $fq-name, $output, $model, $raw-value, $var, $order, $short, :@print-set);
}
}
}
$last-order = $order;
Expand Down Expand Up @@ -108,12 +120,32 @@ sub make-record($fq-name, $output, $model, $raw-value, $var, $order, $short, $in
}

sub add-filters(@records, $fq-name, $output, $model,
Agrammon::Outputs::FilterGroupCollection $collection,
$var, $order, $sort, :@print-set) {
Agrammon::Outputs::FilterGroupCollection $collection,
$var, $order, $sort, :@print-set) {
for $collection.results-by-filter-group {
my %keyFilters := .key;
my @filters = translate-filter-keys($model, %keyFilters).map: -> $trans { %( label => $trans.key, enum => $trans.value ) };
my $value := .value;
push @records, make-record($fq-name, $output, $model, $value, $var, $order, $sort, :@print-set, :@filters);
}
}

sub merge-filters($record, $fq-name, $output, $model,
Agrammon::Outputs::FilterGroupCollection $collection,
$var, $order, $sort, $language?, :@print-set) {
for $collection.results-by-filter-group {
my %keyFilters := .key;
my @filters = translate-filter-keys($model, %keyFilters).map: -> $trans { %( label => $trans.key, enum => $trans.value ) };
my $value := .value;
# TODO: make this an option
# next unless $value;
my $filter-record = make-record($fq-name, $output, $model, $value, $var, $order, $sort, :@print-set, :@filters);
push $record<values>, %( :label($filter-record<filters>[0]<enum>{$language}), :value($filter-record<fullValue>));
}
push $record<values>, %( :label($record<label>:delete), :value($record<fullValue>:delete));
$record<order>:delete;
$record<format>:delete;
$record<value>:delete;
$record<filters>:delete;
return $record;
}
2 changes: 2 additions & 0 deletions lib/Agrammon/OutputFormatter/Text.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ sub add-filters(@module-lines, Agrammon::Outputs::FilterGroupCollection $collect
for @results {
my %filters := .key;
my $value := .value;
# TODO: make this an option
# next unless $value;
my @filters = %filters.map: { .key ~ '=' ~ .value };
for (@filters || '(Uncategorized)').kv -> $idx, $filter-id {
my $padding = ' ' x $longest-filter - $filter-id.chars;
Expand Down
5 changes: 4 additions & 1 deletion lib/Agrammon/Web/APIRoutes.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ sub rest-api-routes (Str $schema, Agrammon::Web::Service $ws) is export {
request-body 'multipart/form-data' => -> (
:$simulation!, :$dataset!, :$inputs!, :$technical='',
:$model = 'version6', :$variants = 'Base', :$language = 'de',
:$print-only = '', :$include-filters = 'false', :$all-filters = 'false'
:$print-only = '',
:$compact-output = 'true',
:$include-filters = 'false', :$all-filters = 'false'
) {
my $type = $inputs.content-type;
if not ($type eq 'application/json' or $type eq 'text/csv') {
Expand All @@ -103,6 +105,7 @@ sub rest-api-routes (Str $schema, Agrammon::Web::Service $ws) is export {
~$simulation, ~$dataset, $input-data, ~$type,
:model-version(~$model), :variants(~$variants), :technical-file(~$technical),
:language(~$language), :format($accept), :print-only(~$print-only),
:$compact-output,
:include-filters($include-filters eq 'true'), :all-filters($all-filters eq 'true')
);
if $accept eq 'application/json' {
Expand Down
6 changes: 4 additions & 2 deletions lib/Agrammon/Web/Service.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ class Agrammon::Web::Service {
Str $simulation-name, Str $dataset-name, $input-data, InputFormats $type,
:$model-version, :$variants, :$technical-file,
:$language = 'de', OutputFormats :$format!, :$print-only,
:$include-filters = False, :$all-filters = False
:$include-filters = False, :$all-filters = False, :$compact-output
) {
my $data-source = do given $type {
when 'text/csv' { Agrammon::DataSource::CSV.new }
Expand Down Expand Up @@ -329,6 +329,7 @@ class Agrammon::Web::Service {
my $outputs = $!model.run(:$input, :%technical);

my @print-set = ($print-only).split(',') if $print-only;
my $short = $compact-output eq 'true';
my $result;
given $format {
when 'text/csv' {
Expand All @@ -340,7 +341,8 @@ class Agrammon::Web::Service {
}
when 'application/json' {
$result = output-as-json(
$!model, $outputs, $language, @print-set, $include-filters, :$all-filters
$!model, $outputs, $language, @print-set, $include-filters, :$all-filters,
:$short
);
}
when 'text/plain' {
Expand Down
7 changes: 4 additions & 3 deletions share/Models/version6/Livestock/Pig/Housing.nhd
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ taxonomy = Livestock::Pig::Housing
my $ef = Val(er_housing, Housing::Type) *
Val(c_free_factor_housing, Housing::CFreeFactor) *
Val(c_area, Housing::Type) *
(1 - Val(red_housing_floor, Housing::MitigationOptions)) *
(1 - Val(red_housing_floor_indoor, Housing::MitigationOptions)) *
(1 - Val(red_housing_air, Housing::MitigationOptions));
if ( $ef > 1 ) {
writeLog({
Expand All @@ -97,7 +97,7 @@ taxonomy = Livestock::Pig::Housing
Out(ef_housing_indoor_before_air_scrubber) *
(1 - Val(red_air_scrubber, Housing::AirScrubber));

+ef_housing_grazing
+ef_housing_outdoor
print = Pig
++units
en = -
Expand All @@ -108,6 +108,7 @@ taxonomy = Livestock::Pig::Housing
++formula
my $ef = Val(er_housing, Housing::Type) *
Val(c_free_factor_housing, Housing::CFreeFactor) *
(1 - Val(red_housing_floor_outdoor, Housing::MitigationOptions)) *
Val(c_area, Housing::Type);
if ( $ef > 1 ) {
writeLog({
Expand All @@ -131,7 +132,7 @@ taxonomy = Livestock::Pig::Housing
# indoor part
Val(share_indoor, Housing::Type) * Out(ef_housing_indoor) +
# grazing part
(1 - Val(share_indoor, Housing::Type)) * Out(ef_housing_grazing);
(1 - Val(share_indoor, Housing::Type)) * Out(ef_housing_outdoor);

+nh3_nhousing
print = Pig
Expand Down
19 changes: 16 additions & 3 deletions share/Models/version6/Livestock/Pig/Housing/MitigationOptions.nhd
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,25 @@ UNECE 2007. Guidance document on control techniques for preventing and abating e

*** output ***

+red_housing_floor
+red_housing_floor_indoor
print = Pig
++units
en = -
++description
Reduction factor for the emission due to UNECE housing systems tasks for fully and partly slatted floors.
Reduction factor for the emission due to floor related mitigation strategies - part indoor.
++formula
given ( In(mitigation_housing_floor) ) {
when 'none' {
0;
}
}

+red_housing_floor_outdoor
print = Pig
++units
en = -
++description
Reduction factor for the emission due to floor related mitigation strategies - part outdoor.
++formula
given ( In(mitigation_housing_floor) ) {
when 'none' {
Expand All @@ -109,7 +122,7 @@ UNECE 2007. Guidance document on control techniques for preventing and abating e
++units
en = -
++description
Reduction factor for the emission due to the use of housing system adaptations.
Reduction factor for the emission due to the use of housing system adaptations (only indoor).
++formula
if (In(mitigation_housing_air) eq 'low_impuls_air_supply'){
return( Tech(red_low_impuls_air_supply));
Expand Down
15 changes: 10 additions & 5 deletions share/Models/version6/Livestock/Pig/Housing/Type.nhd
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,16 @@ Selects the emission rate and other correciton factors for the specific housing
++description
Factor for considering grazing part.
++formula
if ( Out(housing_type) eq 'Slurry_Label' or Out(housing_type) eq 'Slurry_Label_Open' ) {
return 0.5;
}
else {
return 1;
given ( Out(housing_type) ) {
when 'Slurry_Label' {
return 0.5;
}
when 'Slurry_Label_Open' {
return 0;
}
default {
return 1;
}
}


Expand Down
2 changes: 1 addition & 1 deletion share/Models/version6/Total.nhd
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ taxonomy = Total

+nh3_nanimalproduction
format= %.0f
print = SummaryLivestock,ResultsLivestock,LivestockNH3
print = SummaryLivestock,ResultsLivestock,LivestockNH3,TotalNH3
++labels
sort = 099
en = Total
Expand Down
6 changes: 6 additions & 0 deletions share/agrammon-rest.openapi
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,12 @@ components:
enum:
- 'true'
- 'false'
compact-output:
description: compact instead of full output
type: string
enum:
- 'true'
- 'false'
Error:
required:
- error
Expand Down
2 changes: 1 addition & 1 deletion t/apiroutes.t
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ my $fake-store = mocked(Agrammon::Web::Service,
get-outputs-for-rest => -> Str $simulation-name, Str $dataset-name, Str $csv-data, $type,
:$model-version, :$variants, :$technical-file,
:$language, :$format, :$print-only,
:$include-filters, :$all-filters {
:$include-filters, :$all-filters, :$compact-output {
"Test results"
},
}
Expand Down
Loading