Skip to content

Commit

Permalink
AER-2858 added more information for conversion warnings (#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
BertScholten authored May 30, 2024
1 parent ae5b42e commit 2d5165f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,18 @@ public EmissionSource convert(final T source) throws AeriusException {
private FarmAnimalHousingEmissionSource convertToAnimalHousing(final T source) throws AeriusException {
final FarmAnimalHousingEmissionSource animalHousingSource = new FarmAnimalHousingEmissionSource();
for (final IsGmlProperty<IsGmlFarmLodging> lodging : source.getFarmLodgings()) {
animalHousingSource.getSubSources().add(getAnimalHousing(lodging.getProperty(), source.getId()));
animalHousingSource.getSubSources().add(getAnimalHousing(lodging.getProperty(), source));
}
animalHousingSource.setEstablished(source.getEstablished());
return animalHousingSource;
}

private FarmAnimalHousing getAnimalHousing(final IsGmlFarmLodging lodging, final String sourceId) throws AeriusException {
private FarmAnimalHousing getAnimalHousing(final IsGmlFarmLodging lodging, final T source) throws AeriusException {
final FarmAnimalHousing returnAnimalHousing;
if (lodging instanceof final IsGmlCustomFarmLodging customLodging) {
returnAnimalHousing = convertCustom(customLodging, sourceId);
returnAnimalHousing = convertCustom(customLodging, source);
} else if (lodging instanceof final IsGmlStandardFarmLodging standardLodging) {
returnAnimalHousing = convertStandard(standardLodging, sourceId);
returnAnimalHousing = convertStandard(standardLodging, source);
} else {
LOG.error("Don't know how to treat lodging type: {}", lodging.getClass());
throw new AeriusException(ImaerExceptionReason.INTERNAL_ERROR);
Expand All @@ -91,27 +91,27 @@ private FarmAnimalHousing getAnimalHousing(final IsGmlFarmLodging lodging, final
return returnAnimalHousing;
}

private CustomFarmAnimalHousing convertCustom(final IsGmlCustomFarmLodging customLodging, final String sourceId) throws AeriusException {
private CustomFarmAnimalHousing convertCustom(final IsGmlCustomFarmLodging customLodging, final T source) throws AeriusException {
final CustomFarmAnimalHousing customEmissions = new CustomFarmAnimalHousing();
customEmissions.setAnimalTypeCode(customLodging.getAnimalCode() == null ? UNKNOWN_ANIMAL_TYPE_CODE : customLodging.getAnimalCode());
customEmissions.setDescription(customLodging.getDescription());
customEmissions.setFarmEmissionFactorType(determineEmissionFactorType(customLodging.getEmissionFactorType(), sourceId));
customEmissions.setFarmEmissionFactorType(determineEmissionFactorType(customLodging.getEmissionFactorType(), source.getId()));
for (final IsGmlProperty<IsGmlEmission> emissionProperty : customLodging.getEmissionFactors()) {
final IsGmlEmission emission = emissionProperty.getProperty();
customEmissions.getEmissionFactors().put(emission.getSubstance(), emission.getValue());
}
return customEmissions;
}

private FarmAnimalHousing convertStandard(final IsGmlStandardFarmLodging standardLodging, final String sourceId) {
private FarmAnimalHousing convertStandard(final IsGmlStandardFarmLodging standardLodging, final T source) {
final FarmAnimalHousing converted;
final String oldCode = standardLodging.getCode();

final FarmLodgingConversion conversion = getConversionData().determineFarmLodgingConversion(oldCode);
if (conversion == null) {
converted = convertStandardWithoutConversion(standardLodging, sourceId);
converted = convertStandardWithoutConversion(standardLodging, source);
} else {
converted = convertStandardWithConversion(conversion, standardLodging, sourceId);
converted = convertStandardWithConversion(conversion, standardLodging, source);
}
converted.setNumberOfAnimals(standardLodging.getNumberOfAnimals());
converted.setNumberOfDays(standardLodging.getNumberOfDays());
Expand All @@ -120,7 +120,7 @@ private FarmAnimalHousing convertStandard(final IsGmlStandardFarmLodging standar
}

private CustomFarmAnimalHousing convertStandardWithoutConversion(
final IsGmlStandardFarmLodging standardLodging, final String sourceId) {
final IsGmlStandardFarmLodging standardLodging, final T source) {
final CustomFarmAnimalHousing customEmissions = new CustomFarmAnimalHousing();
// Not sure if this'll stick: relies on custom animal housing using the old AnimalType codes.
customEmissions.setAnimalTypeCode(standardLodging.getCode() == null || standardLodging.getCode().length() <= 1
Expand All @@ -130,12 +130,12 @@ private CustomFarmAnimalHousing convertStandardWithoutConversion(
customEmissions.setFarmEmissionFactorType(FarmEmissionFactorType.PER_ANIMAL_PER_YEAR);
customEmissions.getEmissionFactors().put(Substance.NH3, 0.0);
// Warn the user that this source has been converted to custom animal housing.
addWarningForSource(ImaerExceptionReason.GML_CONVERTED_LODGING_TO_CUSTOM, sourceId, standardLodging);
addWarningForSource(ImaerExceptionReason.GML_CONVERTED_LODGING_TO_CUSTOM, source, standardLodging);
return customEmissions;
}

private StandardFarmAnimalHousing convertStandardWithConversion(final FarmLodgingConversion conversion,
final IsGmlStandardFarmLodging standardLodging, final String sourceId) {
final IsGmlStandardFarmLodging standardLodging, final T source) {
final StandardFarmAnimalHousing standardEmissions = new StandardFarmAnimalHousing();
standardEmissions.setAnimalTypeCode(conversion.getAnimalTypeCode());
standardEmissions.setAnimalHousingCode(conversion.getAnimalHousingCode());
Expand All @@ -147,16 +147,21 @@ private StandardFarmAnimalHousing convertStandardWithConversion(final FarmLodgin
if (!standardLodging.getLodgingSystems().isEmpty() || !standardLodging.getFodderMeasures().isEmpty()) {
// We can't convert additional systems: no data available to do so.
// Instead, add a specific warning that the user has to check additional systems for this source.
addWarningForSource(ImaerExceptionReason.GML_CONVERTED_LODGING_WITH_SYSTEMS, sourceId, standardLodging);
addWarningForSource(ImaerExceptionReason.GML_CONVERTED_LODGING_WITH_SYSTEMS, source, standardLodging);
} else if (!conversion.getAnimalHousingCode().equals(standardLodging.getCode())) {
// Warn the user that this source has been converted.
addWarningForSource(ImaerExceptionReason.GML_CONVERTED_LODGING, sourceId, standardLodging);
addWarningForSource(ImaerExceptionReason.GML_CONVERTED_LODGING, source, standardLodging);
}
return standardEmissions;
}

private void addWarningForSource(final ImaerExceptionReason reason, final String sourceId, final IsGmlStandardFarmLodging standardLodging) {
getConversionData().getWarnings().add(new AeriusException(reason, sourceId, standardLodging.getCode()));
private void addWarningForSource(final ImaerExceptionReason reason, final T source, final IsGmlStandardFarmLodging standardLodging) {
getConversionData().getWarnings().add(new AeriusException(reason, source.getId(), safe(source.getLabel()),
standardLodging.getCode(), safe(standardLodging.getLodgingSystemDefinitionCode())));
}

private String safe(final String value) {
return value == null ? "" : value;
}

public FarmLodgingEmissionSource convertLegacy(final T source) throws AeriusException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ void testGMLUnknownPasMeasure() throws IOException {
assertResult("fout_5216_unknown_pas_measure", "GML Unknown PAS measure", ImaerExceptionReason.GML_CONVERTED_LODGING_WITH_SYSTEMS,
e -> {
assertEquals("ES.1", e.getArgs()[0], "Id");
assertEquals("A1.1", e.getArgs()[1], "Code of lodging");
assertEquals("Bron 1", e.getArgs()[1], "Label");
assertEquals("A1.1", e.getArgs()[2], "Code of lodging");
assertEquals("GL_BB93.06.009", e.getArgs()[3], "System definition code of lodging");
});
}

Expand All @@ -162,8 +164,10 @@ void testGMLUnsupportedLodingMeasure() throws IOException {
// With animal housing, lodgings with systems are just converted with special warning.
assertResult("fout_5217_unsupported_lodging_measure", "GML Unsupported loding measure", ImaerExceptionReason.GML_CONVERTED_LODGING_WITH_SYSTEMS,
e -> {
assertEquals("ES.1", e.getArgs()[0], "Label");
assertEquals("A1.1", e.getArgs()[1], "Code of lodging");
assertEquals("ES.1", e.getArgs()[0], "ID");
assertEquals("Bron 1", e.getArgs()[1], "Label");
assertEquals("A1.1", e.getArgs()[2], "Code of lodging");
assertEquals("GL_BB93.06.009", e.getArgs()[3], "System definition code of lodging");
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,15 +524,19 @@ public enum ImaerExceptionReason implements Reason {
* GML contained a farm lodging that was converted to standard animal housing.
*
* @param 0 the id of the object that was converted.
* @param 1 the code of the lodging that was converted.
* @param 1 the label of the object that was converted.
* @param 2 the code of the lodging that was converted.
* @param 3 the system definition code of the lodging that was converted.
*/
GML_CONVERTED_LODGING(5263),
/**
* GML contained a farm lodging that was converted to standard animal housing.
* In this case, the lodging contained additional or reductive systems or fodder measures, which were not converted.
*
* @param 0 the id of the object that was converted.
* @param 1 the code of the lodging that was converted.
* @param 1 the label of the object that was converted.
* @param 2 the code of the lodging that was converted.
* @param 3 the system definition code of the lodging that was converted.
*/
GML_CONVERTED_LODGING_WITH_SYSTEMS(5264),
/**
Expand All @@ -541,7 +545,9 @@ public enum ImaerExceptionReason implements Reason {
* A custom animal housing has been created instead, with 0 emission factors.
*
* @param 0 the id of the object that was converted.
* @param 1 the code of the lodging that could not be converted.
* @param 1 the label of the object that was converted.
* @param 2 the code of the lodging that was converted.
* @param 3 the system definition code of the lodging that was converted.
*/
GML_CONVERTED_LODGING_TO_CUSTOM(5265),

Expand Down

0 comments on commit 2d5165f

Please sign in to comment.