Skip to content

Commit

Permalink
Add animal example to template
Browse files Browse the repository at this point in the history
  • Loading branch information
holos-aafc committed Aug 21, 2024
1 parent d62394c commit 19901ff
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 17 deletions.
8 changes: 4 additions & 4 deletions H.CLI.Test/FilesAndDirectoryAccessors/DirectoryHandlerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void TestPrioritizeComponentDirectoryPaths()
@"C:\Holos\HolosCommandLineInterface\Farms\HolosExampleFarm1\Fields",
};

var prioritizedComponentPathList = directoryHandler.prioritizeDirectoryKeys(componentPathList);
var prioritizedComponentPathList = directoryHandler.PrioritizeDirectoryKeys(componentPathList);
Assert.AreEqual(prioritizedComponentPathList[0] == @"C:\Holos\HolosCommandLineInterface\Farms\HolosExampleFarm1\Shelterbelts"
||
prioritizedComponentPathList[0] == @"C:\Holos\HolosCommandLineInterface\Farms\HolosExampleFarm1\Fields", true);
Expand All @@ -98,7 +98,7 @@ public void TestGetListOfFarmsWithNoCLArguments()
Directory.CreateDirectory(testFarmFolder + testFarm);
Directory.CreateDirectory(testFarmFolder + testFarm2);

var returnedListOfFarms = directoryHandler.getListOfFarms(testFarmFolder, argValues, testFarm, null);
var returnedListOfFarms = directoryHandler.GetListOfFarms(testFarmFolder, argValues, testFarm, null);
Assert.AreEqual(returnedListOfFarms[0], testFarmFolder + testFarm);
Assert.AreEqual(returnedListOfFarms[1], testFarmFolder + testFarm2);

Expand All @@ -120,7 +120,7 @@ public void TestGetListOfFarmsWithInputFile()
Directory.CreateDirectory(testFarmFolder);
Directory.CreateDirectory(testFarmFolder + testFarm);

var returnedListOfFarms = directoryHandler.getListOfFarms(testFarmFolder, argValues, testFarmFolder + testFarm, null);
var returnedListOfFarms = directoryHandler.GetListOfFarms(testFarmFolder, argValues, testFarmFolder + testFarm, null);
Assert.AreEqual(returnedListOfFarms[0], testFarmFolder + testFarm);

Directory.Delete(testFarmFolder, true); // Delete after test
Expand Down Expand Up @@ -148,7 +148,7 @@ public void TestGetListOfFarmsWithInputFolder()
generatedFarmFolders.Add(testFarmFolder + testFarm2);
generatedFarmFolders.Add(testFarmFolder + testFarm3);

var returnedListOfFarms = directoryHandler.getListOfFarms(testFarmFolder, argValues, "", generatedFarmFolders);
var returnedListOfFarms = directoryHandler.GetListOfFarms(testFarmFolder, argValues, "", generatedFarmFolders);

Assert.IsNotNull(returnedListOfFarms);
Assert.AreEqual(returnedListOfFarms[1], testFarmFolder + testFarm2);
Expand Down
4 changes: 2 additions & 2 deletions H.CLI/FileAndDirectoryAccessors/DataInputHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public Farm ProcessDataInputFiles(string farmDirectoryPath)
var farm = _farmHelper.Create();

var componentDirectoryList = Directory.GetDirectories(farmDirectoryPath).ToList();
_directoryHandler.checkForInvalidComponentDirectoryNames(componentDirectoryList, farmDirectoryPath);
_directoryHandler.CheckForInvalidComponentDirectoryNames(componentDirectoryList, farmDirectoryPath);

//Fields and Shelterbelts are prioritized. Priority is set in the Directory Keys Class by number weight
componentDirectoryList = _directoryHandler.prioritizeDirectoryKeys(componentDirectoryList);
componentDirectoryList = _directoryHandler.PrioritizeDirectoryKeys(componentDirectoryList);
foreach (var componentDirectoryPath in componentDirectoryList)
{
var componentCategoryType = Path.GetFileName(componentDirectoryPath);
Expand Down
10 changes: 4 additions & 6 deletions H.CLI/FileAndDirectoryAccessors/DirectoryHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public DirectoryHandler() { }
/// </summary>
public void InitializeDirectoriesAndFilesForComponents(string farmDirectoryPath)
{

if (!Directory.Exists(farmDirectoryPath))
{
Console.BackgroundColor = ConsoleColor.DarkRed;
Expand All @@ -44,12 +43,11 @@ public void InitializeDirectoriesAndFilesForComponents(string farmDirectoryPath)

ValidateComponentDirectories(farmDirectoryPath);
var componentDirectories = Directory.GetDirectories(farmDirectoryPath).ToList();
var validComponentDirectories = prioritizeDirectoryKeys(componentDirectories);
var validComponentDirectories = PrioritizeDirectoryKeys(componentDirectories);
_templateFileHandler.validateTemplateFiles(validComponentDirectories);

}

public void checkForInvalidComponentDirectoryNames(List<string> componentDirectoryPathsInAFarm, string farmDirectoryPath)
public void CheckForInvalidComponentDirectoryNames(List<string> componentDirectoryPathsInAFarm, string farmDirectoryPath)
{
var invalidDirectories = componentDirectoryPathsInAFarm.Where(x => !_directoryKeys.directoryWeights.Select(y => y.Key.ToUpper()).Contains(Path.GetFileName(x).ToUpper())).ToList();
Console.ForegroundColor = ConsoleColor.Yellow;
Expand Down Expand Up @@ -81,7 +79,7 @@ public void checkForInvalidComponentDirectoryNames(List<string> componentDirecto
/// Swines, if the user selects their HousingType to be pasture, they will potentially need to be refer to one of their
/// created Field Components, which needs to be processed first in order to use it in that Swine component.
/// </summary>
public List<string> prioritizeDirectoryKeys(List<string> componentDirectoryPathsInAFarm)
public List<string> PrioritizeDirectoryKeys(List<string> componentDirectoryPathsInAFarm)
{
var validDirectories = componentDirectoryPathsInAFarm.Where(x => _directoryKeys.directoryWeights.Select(y => y.Key.ToUpper()).Contains(Path.GetFileName(x).ToUpper())).ToList();
return validDirectories.OrderByDescending(x => _directoryKeys.directoryWeights.ContainsKey(Path.GetFileName(x))).ToList();
Expand Down Expand Up @@ -249,7 +247,7 @@ public void GetUsersFarmsPath(string[] args)

}

public string[] getListOfFarms(string farmsFolderPath, CLIArguments argvalues, string pathToExportedFarm, List<string> generatedFarmFolders)
public string[] GetListOfFarms(string farmsFolderPath, CLIArguments argvalues, string pathToExportedFarm, List<string> generatedFarmFolders)
{
if (!argvalues.IsFileNameFound && !argvalues.IsFolderNameFound)
{
Expand Down
2 changes: 1 addition & 1 deletion H.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static void Main(string[] args)
var processorHandler = new ProcessorHandler();

// Get The Directories in the "Farms" folder
var listOfFarmPaths = directoryHandler.getListOfFarms(farmsFolderPath, argValues, exportedFarmsHandler.pathToExportedFarm, generatedFarmFolders);
var listOfFarmPaths = directoryHandler.GetListOfFarms(farmsFolderPath, argValues, exportedFarmsHandler.pathToExportedFarm, generatedFarmFolders);

// Set up the geographic data provider only once to speed up processing.
var geographicDataProvider = new GeographicDataProvider();
Expand Down
1 change: 0 additions & 1 deletion H.CLI/Results/ComponentResultsProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,6 @@ public void WriteGHGAndCO2EmissionsForEachFarmByMonth(ApplicationData applicatio
// Farm name
stringBuilder.Append(CLILanguageConstants.Delimiter);

// Component category
stringBuilder.Append(CLILanguageConstants.Delimiter);

// Component name
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -183,5 +183,6 @@ void InitializeDefaultEmissionFactors(
void InitializeVolatileSolids(Farm farm);
List<DietAdditiveType> GetValidDietAdditiveTypes();
void InitializeTotals(ManagementPeriod managementPeriod);
void InitializeFarm(Farm farm);
}
}
5 changes: 5 additions & 0 deletions H.Core/Services/Initialization/InitializationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,11 @@ public void InitializeTotals(ManagementPeriod managementPeriod)
_animalInitializationService.InitializeTotals(managementPeriod);
}

public void InitializeFarm(Farm farm)
{
_animalInitializationService.InitializeFarm(farm);
}

#endregion
}
}
4 changes: 4 additions & 0 deletions H.Template/H.Template.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\H.CLI\H.CLI.csproj">
<Project>{7A9469D6-B913-49DB-B85F-319FD195C212}</Project>
<Name>H.CLI</Name>
</ProjectReference>
<ProjectReference Include="..\H.Core\H.Core.csproj">
<Project>{08D833EF-D06E-42F1-B548-89E985DFC26B}</Project>
<Name>H.Core</Name>
Expand Down
136 changes: 133 additions & 3 deletions H.Template/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,70 +4,199 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using H.CLI.FileAndDirectoryAccessors;
using H.CLI.Results;
using H.Core;
using H.Core.Calculators.Carbon;
using H.Core.Calculators.Climate;
using H.Core.Calculators.Infrastructure;
using H.Core.Calculators.Nitrogen;
using H.Core.Enumerations;
using H.Core.Models;
using H.Core.Models.Animals;
using H.Core.Models.Animals.Beef;
using H.Core.Models.Animals.Dairy;
using H.Core.Models.LandManagement.Fields;
using H.Core.Providers;
using H.Core.Providers.Animals.Table_69;
using H.Core.Providers.Climate;
using H.Core.Providers.Feed;
using H.Core.Services;
using H.Core.Services.Animals;
using H.Core.Services.Initialization;
using H.Core.Services.Initialization.Animals;
using H.Core.Services.Initialization.Climate;
using H.Core.Services.Initialization.Crops;
using H.Core.Services.Initialization.Geography;
using H.Core.Services.LandManagement;
using H.Infrastructure;
using Prism.Events;

namespace H.Template
{
internal class Program
{
#region Fields

private static readonly Storage _storage;
private static readonly ApplicationData _applicationData;

private static readonly IClimateInitializationService _climateInitializationService;
private static readonly IGeographyInitializationService _geographyInitializationService;
private static readonly ICropInitializationService _cropInitializationService;
private static readonly IAnimalService _animalService;
private static readonly ICBMSoilCarbonCalculator _icbmsoilCarbonCalculator;
private static readonly IPCCTier2SoilCarbonCalculator _ipccTier2SoilCarbonCalculator;
private static readonly N2OEmissionFactorCalculator _n2OEmissionFactorCalculator;
private static readonly IInitializationService _initializationService;
private static readonly FieldResultsService _fieldResultsService;
private static readonly IClimateProvider _climateProvider;
private static readonly ISlcClimateProvider _sliClimateProvider;
private static readonly IDietProvider _dietProvider;
private static readonly ComponentResultsProcessor _componentResultsProcessor;
private static readonly ITimePeriodHelper _timePeriodHelper;
private static readonly IFarmResultsService _farmResultsService;
private static readonly IEventAggregator _eventAggregator;
private static readonly IADCalculator _adCalculator;
private static readonly IManureService _manureService;
private static readonly IAnimalInitializationService _animalInitializationService;

#endregion

static Program()
{
_storage = new Storage();
_applicationData = new ApplicationData();
_storage.ApplicationData = _applicationData;

_adCalculator = new ADCalculator();
_manureService = new ManureService();
_timePeriodHelper = new TimePeriodHelper();
_eventAggregator = new EventAggregator();
_climateInitializationService = new ClimateInitializationService();
_geographyInitializationService = new GeographyInitializationService();
_cropInitializationService = new CropInitializationService();
_sliClimateProvider = new SlcClimateDataProvider();
_initializationService = new InitializationService();
_dietProvider = new DietProvider();
_climateProvider = new ClimateProvider(_sliClimateProvider);
_n2OEmissionFactorCalculator = new N2OEmissionFactorCalculator(_climateProvider);
_icbmsoilCarbonCalculator = new ICBMSoilCarbonCalculator(_climateProvider, _n2OEmissionFactorCalculator);
_ipccTier2SoilCarbonCalculator = new IPCCTier2SoilCarbonCalculator(_climateProvider, _n2OEmissionFactorCalculator);
_fieldResultsService = new FieldResultsService(_icbmsoilCarbonCalculator, _ipccTier2SoilCarbonCalculator, _n2OEmissionFactorCalculator, _initializationService);
_animalService = new AnimalResultsService();
_componentResultsProcessor = new ComponentResultsProcessor(_storage, _timePeriodHelper, _fieldResultsService);
_farmResultsService = new FarmResultsService(_eventAggregator, _fieldResultsService, _adCalculator, _manureService, _animalService);
_animalInitializationService = new AnimalInitializationService();
}

static void Main(string[] args)
{
Run();
//CroppingExample();
AnimalExample();
}

static void AnimalExample()
{
// An object to hold settings that can be referenced from all farms created by the user
var globalSettings = new GlobalSettings();

// Create a farm
var farm = new Farm();
farm.Name = "Template_Test_Farm_Animals";

// Add this farm to the system
_applicationData.Farms.Clear();
_applicationData.Farms.Add(farm);

/*
* All farms need to have their location set.
*/

// Place the farm within an SLC polygon (St-Julien, QC)
farm.PolygonId = 482019;

// Specify coordinates
farm.Latitude = 46.005;
farm.Longitude = -71.520;

/*
* Set climate data according to location
*/

_climateInitializationService.InitializeClimate(farm);

/*
* Set geographic data (soil properties, etc.) according to location
*/

_geographyInitializationService.InitializeGeography(farm);

/*
* Choose components to add to the farm
*/

var beefCattleFinishingComponent = new FinishingComponent();
beefCattleFinishingComponent.Name = "My Quebec beef cattle operation";
farm.Components.Add(beefCattleFinishingComponent);

// All animal components must have at least one group of animals
var groupOfHeifers = new AnimalGroup();
groupOfHeifers.Name = "Group of 10 heifers";
groupOfHeifers.GroupType = AnimalType.BeefFinishingHeifer;

// Associate this group with the animal component
beefCattleFinishingComponent.Groups.Add(groupOfHeifers);

// All animal groups must have at least one management period. To estimate emissions for a period of time, the start weight, end weight and duration of the period should be defined.
var managementPeriod = new ManagementPeriod();
managementPeriod.Name = "Summer management";
managementPeriod.AnimalType = AnimalType.BeefFinishingHeifer;
managementPeriod.NumberOfAnimals = 10;
managementPeriod.StartWeight = 100;
managementPeriod.EndWeight = 200;
managementPeriod.Start = new DateTime(2024, 1, 1);
managementPeriod.Duration = TimeSpan.FromDays(30);

// Choose a diet for the animals - all management periods must have diet defined
var availableDiets = _dietProvider.GetDiets().Where(x => x.AnimalType == AnimalType.BeefFinisher);
var diet = availableDiets.First();
managementPeriod.SelectedDiet = diet;

// Associate this management period with the animal group
groupOfHeifers.ManagementPeriods.Add(managementPeriod);

// Initialize defaults
_animalInitializationService.InitializeFarm(farm);

// We are done configuring our farm and are ready to calculate results
var results = _farmResultsService.CalculateFarmEmissionResults(farm);

// Use results...
foreach (var animalComponentEmissionsResults in results.AnimalComponentEmissionsResults)
{
foreach (var groupEmissions in animalComponentEmissionsResults.EmissionResultsForAllAnimalGroupsInComponent)
{
foreach (var groupEmissionsGroupEmissionsByMonth in groupEmissions.GroupEmissionsByMonths)
{
Console.WriteLine($"Enteric methane emissions for 10 heifers in {groupEmissionsGroupEmissionsByMonth.MonthString} is {groupEmissionsGroupEmissionsByMonth.MonthlyEntericMethaneEmission} kg CH4");
}
}
}
}

static void Run()
static void CroppingExample()
{
// An object to hold settings that can be referenced from all farms created by the user
var globalSettings = new GlobalSettings();

// Create a farm
var farm = new Farm();
farm.Name = "Template_Test_Farm";
farm.Name = "Template_Test_Farm_Cropping";

// Add this farm to the system
_applicationData.Farms.Clear();
_applicationData.Farms.Add(farm);

/*
* All farms need to have their location set.
Expand Down Expand Up @@ -99,6 +228,7 @@ static void Run()
// Add one field component. Choose a start year and an end year with at least a couple of decades in between
var fieldComponent = new FieldSystemComponent
{
Name = "My wheat and barley field",
StartYear = 1985,
EndYear = 2020
};
Expand Down

0 comments on commit 19901ff

Please sign in to comment.