in KPIApplication add bofek to cropfield, because KPI E1 (milieubelastingspunten) only calculated if soil is known

master
Pepijn van Oort 2023-11-21 14:03:14 +01:00
parent 3f66676914
commit d616fd3dfc
1 changed files with 31 additions and 5 deletions

View File

@ -42,7 +42,7 @@ namespace FarmmapsKPI
KPIInput input;
string fnKPIinput;
Console.WriteLine("Type name of input json file. Example: KPIinput.json (in same directory as FarmmapsKPI.exe) or C:/temp/KPIinputChemieTmp.json");
Console.WriteLine("Type name of input json file. Example: KPIinput.json (in same directory as FarmmapsKPI.exe) or for example like this: C:/temp/KPIinputChemieTmp.json");
fnKPIinput = Console.ReadLine();
if (string.IsNullOrEmpty(fnKPIinput))
{
@ -112,6 +112,8 @@ namespace FarmmapsKPI
private async Task Process(List<UserRoot> roots, KPIInput input, StreamWriter sw)
{
List<Item> cropfieldChildren;
List<Item> crprecChildren;
KPIOutput kpio;
KPIOutput kpioPrevious = new KPIOutput(); //creates a new empty
@ -207,6 +209,19 @@ namespace FarmmapsKPI
string strJarea = JsonConvert.SerializeObject(new { area = area_ha });
JObject Jarea = JObject.Parse(strJarea);
//Retreiving BOFEK. A cropfield has 1 soil
//Have a look at the cropfieldChildren before and after running this task, see one child (i.e. soil) has been added)
cropfieldChildren = await _farmmapsApiService.GetItemChildrenAsync(cropfieldItem.Code);
_logger.LogInformation("Get BOFEK for field");
Item bofekItem = await _generalService.RunBofekTask(cropfieldItem);
if (bofekItem == null)
{
_logger.LogError("Something went wrong while obtaining the BOFEK data");
return;
}
cropfieldChildren = await _farmmapsApiService.GetItemChildrenAsync(cropfieldItem.Code);
// A cropfield has 1 crop recording and the crop recording has 0:many operations
//So first at the crop recording
Item crprecItem;
@ -306,14 +321,25 @@ namespace FarmmapsKPI
// Inspect the children and grandchildren. If all is well, cropfield will have:
// one crprec, with 0-many operations as children. And the Data of an operation will have specification of how much fertilizer / cropprotection agent was applied
// one edicrop.characteristic (with yield in the data)
var cropfieldChildren = await _farmmapsApiService.GetItemChildrenAsync(cropfieldItem.Code);
var crprecChildren = await _farmmapsApiService.GetItemChildrenAsync(crprecItem.Code);
cropfieldChildren = await _farmmapsApiService.GetItemChildrenAsync(cropfieldItem.Code);
crprecChildren = await _farmmapsApiService.GetItemChildrenAsync(crprecItem.Code);
//Now get the KPIs for this cropfield, mounted with operations & cropyield
// Get KPI data for saving it in a file, here the generalsedrvice is called to get the KPI data
_logger.LogInformation($"GetKpiItemsForCropField('{cropfieldItem.Code}')");
KPIItems = await _generalService.GetKpiItemsForCropField(cropfieldItem, 60);
_logger.LogInformation($"Found {KPIItems.Count} KPI items");
//Pesticide KPI's D1 and E1 are retreived from API's of CTBG and CLM and may take a bit longer to retrieve
int targetKPIitemsCount = 8; //if we know we should be getting 8
int maxtries = 5; // but don't keep on trying forever; there is a maximum number of tries
int trycnt = 0;
_logger.LogInformation($"Firing calls GetKpiItemsForCropField() until we have {targetKPIitemsCount}, but don't keep firing forever, stop after {maxtries} calls");
while (KPIItems.Count < targetKPIitemsCount & trycnt < maxtries) {
KPIItems = await _generalService.GetKpiItemsForCropField(cropfieldItem, 30);
_logger.LogInformation($"Found {KPIItems.Count} KPI items");
trycnt ++;
}
if (KPIItems.Count < targetKPIitemsCount) {
_logger.LogWarning($"Found {KPIItems.Count} KPIItems while you were aiming for {targetKPIitemsCount} KPIItems");
}
//Download KPI's into a json output file for this specific cropfield (with unique cropfieldItem.Code)
string KPIItemPathJson = Path.Combine(downloadFolder, $"KPIItems_{cropfieldItem.Code}.json");