From d616fd3dfcacfd0055d5054cbc3de58d31a8f565 Mon Sep 17 00:00:00 2001 From: Pepijn van Oort Date: Tue, 21 Nov 2023 14:03:14 +0100 Subject: [PATCH] in KPIApplication add bofek to cropfield, because KPI E1 (milieubelastingspunten) only calculated if soil is known --- FarmmapsKPI/KPIApplication.cs | 36 ++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/FarmmapsKPI/KPIApplication.cs b/FarmmapsKPI/KPIApplication.cs index d1a989c..dc3299d 100644 --- a/FarmmapsKPI/KPIApplication.cs +++ b/FarmmapsKPI/KPIApplication.cs @@ -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 roots, KPIInput input, StreamWriter sw) { + List cropfieldChildren; + List 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");