diff --git a/FarmmapsKPI/KPIApplication.cs b/FarmmapsKPI/KPIApplication.cs index 18e33a2..d55fc98 100644 --- a/FarmmapsKPI/KPIApplication.cs +++ b/FarmmapsKPI/KPIApplication.cs @@ -89,7 +89,7 @@ namespace FarmmapsKPI TimeSpan tsTotalEstimated; //Per default just 1 run per field. For debugging check if when we run multiple times do we get (should be) always the same output? - int nrun = 5; + int nrun = 1; for (int run = 1; run <= nrun; run++) { for (int i = 0; i < fieldsInputs.Count; i++) //for (int i = 3; i < 4; i++) // for testing @@ -223,16 +223,16 @@ namespace FarmmapsKPI // We need that because for operations, you need to provide the area on which the operation was applied // And if we put that to the crop area, then we neatly get everything on a per ha basis _logger.LogInformation($"Getting polygon area (ha))"); - List KPIItems = await _generalService.GetKpiItemsForCropField(cropfieldItem, 3); + List KPIItemsArea = await _generalService.GetKpiItemsForCropField(cropfieldItem, 3); trycnt = 1; - targetKPIitemsCount = 3; // here for the area we need at least 3, but not more than that - while (KPIItems.Count < targetKPIitemsCount & trycnt < maxtries) + targetKPIitemsCount = 3; // here for the area we need at least 3, but not more than that + while (KPIItemsArea.Count < targetKPIitemsCount & trycnt < maxtries) { - KPIItems = await _generalService.GetKpiItemsForCropField(cropfieldItem,3); - _logger.LogInformation($"Found {KPIItems.Count} KPI items"); + KPIItemsArea = await _generalService.GetKpiItemsForCropField(cropfieldItem,3); + _logger.LogInformation($"Found {KPIItemsArea.Count} KPI items"); trycnt++; } - kpio = JsonConvert.DeserializeObject(KPIItems[0].Data.ToString()); + kpio = JsonConvert.DeserializeObject(KPIItemsArea[0].Data.ToString()); string area_ha = kpio.data.area; // turn the area into a JObject for later merging with operation data; string strJarea = JsonConvert.SerializeObject(new { area = area_ha }); @@ -383,10 +383,8 @@ namespace FarmmapsKPI //throw new Exception(String.Format($"run {run}: totalNferiliserInput != totalNferiliserCropfield?!")); } - //Now get the KPIs for this cropfield, mounted with operations & cropyield - //Note sometimes the KPIItems.Count is already for some crazy reason greater than or equal to targetKPIitemsCount - //But that would have strange results, since that was from above before adding the crop recordings. We want to do at least one new call -> 'while (trycnt == 0 || ' + List KPIItems = await _generalService.GetKpiItemsForCropField(cropfieldItem, 3); _logger.LogInformation($"GetKpiItemsForCropField('{cropfieldItem.Code}')"); //Pesticide KPI's D1 and E1 are retreived from API's of CTBG and CLM and may take a bit longer to retrieve targetKPIitemsCount = 8; //if we know we should be getting 8 KPI items (A1,B1,B2,C1,D1,E1,F1,F2) @@ -394,17 +392,16 @@ namespace FarmmapsKPI bool boolAquaticLife = false; _logger.LogInformation($"Firing calls GetKpiItemsForCropField() until we have {targetKPIitemsCount} KPIitems, but don't keep firing forever, stop after {maxtries} calls"); _logger.LogInformation($"Before we start:"); - _logger.LogInformation($"* KPIItems.Count = {KPIItems.Count}"); + //_logger.LogInformation($"* KPIItems.Count = {KPIItems.Count}"); _logger.LogInformation($"* trycnt = {trycnt}"); _logger.LogInformation($"* boolAquaticLife = {boolAquaticLife}"); //additional criterion for while loop: check if it really contains the E1 mbp elements. //while (trycnt == 0 || ((KPIItems.Count < targetKPIitemsCount || boolAquaticLife == false) & trycnt < maxtries)) - //normal while loop targetKPIitemsCount = 8; //if we know we should be getting 8 KPI items (A1,B1,B2,C1,D1,E1,F1,F2) while (trycnt == 0 || (KPIItems.Count < targetKPIitemsCount & trycnt < maxtries)) { _logger.LogInformation($"Call nr {trycnt + 1}"); - KPIItems = await _generalService.GetKpiItemsForCropField(cropfieldItem, 3); //number after comma is how many seconds per try + KPIItems = await _generalService.GetKpiItemsForCropField(cropfieldItem, 30); //number after comma is how many seconds per try, should be between 30 seconds and 5 minutes _logger.LogInformation($"Found {KPIItems.Count} KPI items"); //boolAquaticLife = GetBoolAquaticLife(KPIItems); trycnt ++; @@ -425,23 +422,65 @@ namespace FarmmapsKPI _logger.LogInformation($"Downloaded file {KPIItemPathJson}"); _logger.LogInformation($""); + + //Remove duplicate KPI items + List KPIItemsClean = new List(); + Item itemi; + string id, idNext; + double value, valueNext; + value = 0; + id = ""; + for (int i = 0; i < KPIItems.Count; i++) + { + itemi = KPIItems[i]; + idNext = JsonConvert.DeserializeObject(itemi.Data.ToString()).id; + valueNext = Convert.ToDouble(JsonConvert.DeserializeObject(itemi.Data.ToString()).value); //hoe doe ik dit voor een getal? + if (idNext != null) + { + if (id != idNext) + { + KPIItemsClean.Add(itemi); + } + else + { + if (valueNext >= value) + { + //Remove the previous element from this list with same id but wrong value (valueNext > value) or duplicate value (valueNext == value). Presumes list is always sorted by kpiid, e.g. we may havce " + KPIItemsClean.RemoveAt(i - 1); + KPIItemsClean.Add(itemi); + } + else + { + //Previous element was correct and already added so do nothing here + } + } + id = idNext; + value = valueNext; + } + } + + ////Order again from A zo Z. + //List KPIItemsCleanSorted = new List(); + //for (int i = KPIItemsClean.Count - 1; i >= 0; i--) + //{ + // KPIItemsCleanSorted.Add(KPIItemsClean[i]); + //} + //Write to the csv file that collects all KPI's for all the crop fields List dataList; - foreach (Item item in KPIItems) - { + foreach (Item item in KPIItemsClean) + { kpio = JsonConvert.DeserializeObject(item.Data.ToString()); if (kpio.id != null) { - if (kpio.id != kpioPrevious.id) - { - KPIelementsOfBalance kPIelementsOfBalance = kpio.data.values; - if (kpio.id == "A1") //TtD - { - //Make a new dataList = new line to be written - //Fill the datalist with this kpi - dataList = new List + KPIelementsOfBalance kPIelementsOfBalance = kpio.data.values; + if (kpio.id == "A1") //TtD + { + //Make a new dataList = new line to be written + //Fill the datalist with this kpi + dataList = new List { - run.ToString(), + run.ToString(), kpio.parentName, cropfieldItem.Code, kpio.data.area, @@ -454,14 +493,14 @@ namespace FarmmapsKPI kpio.targetValue, kpio.thresholdValue }; - //Write the datalist to a line to the streamwrieter sw for the output csv file - sw.WriteLine(string.Join(",", dataList)); - foreach (string elementName in kpio.A1elements) - { - // get A1element from the element called values - // TtD 20240311: note elements of A1 are in the data structure elements of kpio.data and not as below for B1, B2, etc as kpio.data.values - string elementValue = (string)kpio.data.GetType().GetProperty(elementName).GetValue(kpio.data, null); - dataList = new List + //Write the datalist to a line to the streamwrieter sw for the output csv file + sw.WriteLine(string.Join(",", dataList)); + foreach (string elementName in kpio.A1elements) + { + // get A1element from the element called values + // TtD 20240311: note elements of A1 are in the data structure elements of kpio.data and not as below for B1, B2, etc as kpio.data.values + string elementValue = (string)kpio.data.GetType().GetProperty(elementName).GetValue(kpio.data, null); + dataList = new List { run.ToString(), kpio.parentName, @@ -476,15 +515,15 @@ namespace FarmmapsKPI "", "" }; - //Write the datalist to a line to the streamwrieter sw for the output csv file - sw.WriteLine(string.Join(",", dataList)); - } + //Write the datalist to a line to the streamwrieter sw for the output csv file + sw.WriteLine(string.Join(",", dataList)); } - else if (kpio.id == "B1") - { - //Make a new dataList = new line to be written - //Fill the datalist with this kpi - dataList = new List + } + else if (kpio.id == "B1") + { + //Make a new dataList = new line to be written + //Fill the datalist with this kpi + dataList = new List { run.ToString(), kpio.parentName, @@ -498,15 +537,15 @@ namespace FarmmapsKPI kpio.unit, kpio.targetValue, kpio.thresholdValue - }; - //Write the datalist to a line to the streamwrieter sw for the output csv file - sw.WriteLine(string.Join(",", dataList)); + }; + //Write the datalist to a line to the streamwrieter sw for the output csv file + sw.WriteLine(string.Join(",", dataList)); - foreach (string elementName in kpio.B1elements) - { - // get B1element from the element called values - string elementValue = (string)kPIelementsOfBalance.GetType().GetProperty(elementName).GetValue(kPIelementsOfBalance, null); - dataList = new List + foreach (string elementName in kpio.B1elements) + { + // get B1element from the element called values + string elementValue = (string)kPIelementsOfBalance.GetType().GetProperty(elementName).GetValue(kPIelementsOfBalance, null); + dataList = new List { run.ToString(), kpio.parentName, @@ -521,25 +560,25 @@ namespace FarmmapsKPI "", "" }; - //Write the datalist to a line to the streamwrieter sw for the output csv file - sw.WriteLine(string.Join(",", dataList)); + //Write the datalist to a line to the streamwrieter sw for the output csv file + sw.WriteLine(string.Join(",", dataList)); - //Check if totalNferiliserInput is equal to what comes out of the KPI calculation - //PO20240515: Hier gaat het mis!!! - if (elementName == "nFertilizerNKgHa") + //Check if totalNferiliserInput is equal to what comes out of the KPI calculation + //PO20240515: Hier gaat het mis!!! + if (elementName == "nFertilizerNKgHa") + { + if (totalNferiliserInput != Convert.ToDouble(elementValue)) { - if (totalNferiliserInput != Convert.ToDouble(elementValue)) - { - _logger.LogWarning(String.Format($"3. run {run}: totalNferiliserInput != nFertilizerNKgHa")); - //throw new Exception(String.Format($"run {run}: totalNferiliserInput != nFertilizerNKgHa")); - } + _logger.LogWarning(String.Format($"3. run {run}: totalNferiliserInput != nFertilizerNKgHa")); + //throw new Exception(String.Format($"run {run}: totalNferiliserInput != nFertilizerNKgHa")); } } } - else if (kpio.id == "B2") - { - //Make a new dataList = new line to be written - dataList = new List + } + else if (kpio.id == "B2") + { + //Make a new dataList = new line to be written + dataList = new List { //Fill the datalist with this kpi run.ToString(), @@ -555,14 +594,14 @@ namespace FarmmapsKPI kpio.targetValue, kpio.thresholdValue }; - //Write the datalist to a line to the streamwrieter sw for the output csv file - sw.WriteLine(string.Join(",", dataList)); + //Write the datalist to a line to the streamwrieter sw for the output csv file + sw.WriteLine(string.Join(",", dataList)); - foreach (string elementName in kpio.B2elements) - { - // get B1element from the element called values - string elementValue = (string)kPIelementsOfBalance.GetType().GetProperty(elementName).GetValue(kPIelementsOfBalance, null); - dataList = new List + foreach (string elementName in kpio.B2elements) + { + // get B1element from the element called values + string elementValue = (string)kPIelementsOfBalance.GetType().GetProperty(elementName).GetValue(kPIelementsOfBalance, null); + dataList = new List { run.ToString(), kpio.parentName, @@ -577,15 +616,15 @@ namespace FarmmapsKPI "", "" }; - //Write the datalist to a line to the streamwrieter sw for the output csv file - sw.WriteLine(string.Join(",", dataList)); - } + //Write the datalist to a line to the streamwrieter sw for the output csv file + sw.WriteLine(string.Join(",", dataList)); } + } - else if (kpio.id == "C1") //TtD - { - //Make a new dataList = new line to be written - dataList = new List + else if (kpio.id == "C1") //TtD + { + //Make a new dataList = new line to be written + dataList = new List { run.ToString(), kpio.parentName, @@ -600,14 +639,14 @@ namespace FarmmapsKPI kpio.targetValue, kpio.thresholdValue }; - //Write the datalist to a line to the streamwrieter sw for the output csv file - sw.WriteLine(string.Join(",", dataList)); + //Write the datalist to a line to the streamwrieter sw for the output csv file + sw.WriteLine(string.Join(",", dataList)); - foreach (string elementName in kpio.C1elements) - { - // get C1element from the element called values - string elementValue = (string)kPIelementsOfBalance.GetType().GetProperty(elementName).GetValue(kPIelementsOfBalance, null); - dataList = new List + foreach (string elementName in kpio.C1elements) + { + // get C1element from the element called values + string elementValue = (string)kPIelementsOfBalance.GetType().GetProperty(elementName).GetValue(kPIelementsOfBalance, null); + dataList = new List { run.ToString(), kpio.parentName, @@ -622,21 +661,21 @@ namespace FarmmapsKPI "", "" }; - //Write the datalist to a line to the streamwrieter sw for the output csv file - sw.WriteLine(string.Join(",", dataList)); - } + //Write the datalist to a line to the streamwrieter sw for the output csv file + sw.WriteLine(string.Join(",", dataList)); } + } - else if (kpio.id == "E1") + else if (kpio.id == "E1") + { + //for E1, environmentMeasureData is an array of elements with 1 per pesticide. Inside each element there are multiple mbp_KPIvariables to be written + foreach (KPIenvironmentMeasureData e in kpio.data.environmentMeasureData) { - //for E1, environmentMeasureData is an array of elements with 1 per pesticide. Inside each element there are multiple mbp_KPIvariables to be written - foreach (KPIenvironmentMeasureData e in kpio.data.environmentMeasureData) + foreach (string mbp_KPIvariable in mbp_KPIvariables) { - foreach (string mbp_KPIvariable in mbp_KPIvariables) - { - if (mbp_KPIvariable == "aquaticLife") { mbp_KPIvalue = e.aquaticLife; } else if (mbp_KPIvariable == "groundWater") { mbp_KPIvalue = e.groundWater; } else if (mbp_KPIvariable == "soilLife") { mbp_KPIvalue = e.soilLife; } else { mbp_KPIvalue = ""; } - //Make a new dataList = new line to be written - dataList = new List + if (mbp_KPIvariable == "aquaticLife") { mbp_KPIvalue = e.aquaticLife; } else if (mbp_KPIvariable == "groundWater") { mbp_KPIvalue = e.groundWater; } else if (mbp_KPIvariable == "soilLife") { mbp_KPIvalue = e.soilLife; } else { mbp_KPIvalue = ""; } + //Make a new dataList = new line to be written + dataList = new List { run.ToString(), kpio.parentName, @@ -658,15 +697,15 @@ namespace FarmmapsKPI mbp_KPIvariable, mbp_KPIvalue }; - //Write the datalist to a line to the streamwrieter sw for the output csv file - sw.WriteLine(string.Join(",", dataList)); - } + //Write the datalist to a line to the streamwrieter sw for the output csv file + sw.WriteLine(string.Join(",", dataList)); } } - else - { - //Any other KPI, example A1 or D1, with just 1 record to write - dataList = new List + } + else + { + //Any other KPI, example A1 or D1, with just 1 record to write + dataList = new List { run.ToString(), kpio.parentName, @@ -681,13 +720,10 @@ namespace FarmmapsKPI kpio.targetValue, kpio.thresholdValue }; - //Write the datalist to a line to the streamwrieter sw for the output csv file - sw.WriteLine(string.Join(",", dataList)); - } - + //Write the datalist to a line to the streamwrieter sw for the output csv file + sw.WriteLine(string.Join(",", dataList)); } } - kpioPrevious = kpio; } ////Total N applied from input