removed incorrect duplicate KPIs

master
ttenden 2024-05-27 13:52:15 +02:00
parent fcf6b2af51
commit 6d6b5b1b7a
1 changed files with 142 additions and 106 deletions

View File

@ -89,7 +89,7 @@ namespace FarmmapsKPI
TimeSpan tsTotalEstimated; 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? //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 run = 1; run <= nrun; run++) {
for (int i = 0; i < fieldsInputs.Count; i++) for (int i = 0; i < fieldsInputs.Count; i++)
//for (int i = 3; i < 4; i++) // for testing //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 // 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 // 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))"); _logger.LogInformation($"Getting polygon area (ha))");
List<Item> KPIItems = await _generalService.GetKpiItemsForCropField(cropfieldItem, 3); List<Item> KPIItemsArea = await _generalService.GetKpiItemsForCropField(cropfieldItem, 3);
trycnt = 1; trycnt = 1;
targetKPIitemsCount = 3; // here for the area we need at least 3, but not more than that targetKPIitemsCount = 3; // here for the area we need at least 3, but not more than that
while (KPIItems.Count < targetKPIitemsCount & trycnt < maxtries) while (KPIItemsArea.Count < targetKPIitemsCount & trycnt < maxtries)
{ {
KPIItems = await _generalService.GetKpiItemsForCropField(cropfieldItem,3); KPIItemsArea = await _generalService.GetKpiItemsForCropField(cropfieldItem,3);
_logger.LogInformation($"Found {KPIItems.Count} KPI items"); _logger.LogInformation($"Found {KPIItemsArea.Count} KPI items");
trycnt++; trycnt++;
} }
kpio = JsonConvert.DeserializeObject<KPIOutput>(KPIItems[0].Data.ToString()); kpio = JsonConvert.DeserializeObject<KPIOutput>(KPIItemsArea[0].Data.ToString());
string area_ha = kpio.data.area; string area_ha = kpio.data.area;
// turn the area into a JObject for later merging with operation data; // turn the area into a JObject for later merging with operation data;
string strJarea = JsonConvert.SerializeObject(new { area = area_ha }); string strJarea = JsonConvert.SerializeObject(new { area = area_ha });
@ -383,10 +383,8 @@ namespace FarmmapsKPI
//throw new Exception(String.Format($"run {run}: totalNferiliserInput != totalNferiliserCropfield?!")); //throw new Exception(String.Format($"run {run}: totalNferiliserInput != totalNferiliserCropfield?!"));
} }
//Now get the KPIs for this cropfield, mounted with operations & cropyield //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 List<Item> KPIItems = await _generalService.GetKpiItemsForCropField(cropfieldItem, 3);
//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 || '
_logger.LogInformation($"GetKpiItemsForCropField('{cropfieldItem.Code}')"); _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 //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) 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; 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($"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($"Before we start:");
_logger.LogInformation($"* KPIItems.Count = {KPIItems.Count}"); //_logger.LogInformation($"* KPIItems.Count = {KPIItems.Count}");
_logger.LogInformation($"* trycnt = {trycnt}"); _logger.LogInformation($"* trycnt = {trycnt}");
_logger.LogInformation($"* boolAquaticLife = {boolAquaticLife}"); _logger.LogInformation($"* boolAquaticLife = {boolAquaticLife}");
//additional criterion for while loop: check if it really contains the E1 mbp elements. //additional criterion for while loop: check if it really contains the E1 mbp elements.
//while (trycnt == 0 || ((KPIItems.Count < targetKPIitemsCount || boolAquaticLife == false) & trycnt < maxtries)) //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) 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)) while (trycnt == 0 || (KPIItems.Count < targetKPIitemsCount & trycnt < maxtries))
{ {
_logger.LogInformation($"Call nr {trycnt + 1}"); _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"); _logger.LogInformation($"Found {KPIItems.Count} KPI items");
//boolAquaticLife = GetBoolAquaticLife(KPIItems); //boolAquaticLife = GetBoolAquaticLife(KPIItems);
trycnt ++; trycnt ++;
@ -425,23 +422,65 @@ namespace FarmmapsKPI
_logger.LogInformation($"Downloaded file {KPIItemPathJson}"); _logger.LogInformation($"Downloaded file {KPIItemPathJson}");
_logger.LogInformation($""); _logger.LogInformation($"");
//Remove duplicate KPI items
List<Item> KPIItemsClean = new List<Item>();
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<KPIOutput>(itemi.Data.ToString()).id;
valueNext = Convert.ToDouble(JsonConvert.DeserializeObject<KPIOutput>(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<Item> KPIItemsCleanSorted = new List<Item>();
//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 //Write to the csv file that collects all KPI's for all the crop fields
List<string> dataList; List<string> dataList;
foreach (Item item in KPIItems) foreach (Item item in KPIItemsClean)
{ {
kpio = JsonConvert.DeserializeObject<KPIOutput>(item.Data.ToString()); kpio = JsonConvert.DeserializeObject<KPIOutput>(item.Data.ToString());
if (kpio.id != null) if (kpio.id != null)
{ {
if (kpio.id != kpioPrevious.id) KPIelementsOfBalance kPIelementsOfBalance = kpio.data.values;
{ if (kpio.id == "A1") //TtD
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
//Make a new dataList = new line to be written dataList = new List<string>
//Fill the datalist with this kpi
dataList = new List<string>
{ {
run.ToString(), run.ToString(),
kpio.parentName, kpio.parentName,
cropfieldItem.Code, cropfieldItem.Code,
kpio.data.area, kpio.data.area,
@ -454,14 +493,14 @@ namespace FarmmapsKPI
kpio.targetValue, kpio.targetValue,
kpio.thresholdValue kpio.thresholdValue
}; };
//Write the datalist to a line to the streamwrieter sw for the output csv file //Write the datalist to a line to the streamwrieter sw for the output csv file
sw.WriteLine(string.Join(",", dataList)); sw.WriteLine(string.Join(",", dataList));
foreach (string elementName in kpio.A1elements) foreach (string elementName in kpio.A1elements)
{ {
// get A1element from the element called values // 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 // 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); string elementValue = (string)kpio.data.GetType().GetProperty(elementName).GetValue(kpio.data, null);
dataList = new List<string> dataList = new List<string>
{ {
run.ToString(), run.ToString(),
kpio.parentName, kpio.parentName,
@ -476,15 +515,15 @@ namespace FarmmapsKPI
"", "",
"" ""
}; };
//Write the datalist to a line to the streamwrieter sw for the output csv file //Write the datalist to a line to the streamwrieter sw for the output csv file
sw.WriteLine(string.Join(",", dataList)); sw.WriteLine(string.Join(",", dataList));
}
} }
else if (kpio.id == "B1") }
{ else if (kpio.id == "B1")
//Make a new dataList = new line to be written {
//Fill the datalist with this kpi //Make a new dataList = new line to be written
dataList = new List<string> //Fill the datalist with this kpi
dataList = new List<string>
{ {
run.ToString(), run.ToString(),
kpio.parentName, kpio.parentName,
@ -498,15 +537,15 @@ namespace FarmmapsKPI
kpio.unit, kpio.unit,
kpio.targetValue, kpio.targetValue,
kpio.thresholdValue kpio.thresholdValue
}; };
//Write the datalist to a line to the streamwrieter sw for the output csv file //Write the datalist to a line to the streamwrieter sw for the output csv file
sw.WriteLine(string.Join(",", dataList)); sw.WriteLine(string.Join(",", dataList));
foreach (string elementName in kpio.B1elements) foreach (string elementName in kpio.B1elements)
{ {
// get B1element from the element called values // get B1element from the element called values
string elementValue = (string)kPIelementsOfBalance.GetType().GetProperty(elementName).GetValue(kPIelementsOfBalance, null); string elementValue = (string)kPIelementsOfBalance.GetType().GetProperty(elementName).GetValue(kPIelementsOfBalance, null);
dataList = new List<string> dataList = new List<string>
{ {
run.ToString(), run.ToString(),
kpio.parentName, kpio.parentName,
@ -521,25 +560,25 @@ namespace FarmmapsKPI
"", "",
"" ""
}; };
//Write the datalist to a line to the streamwrieter sw for the output csv file //Write the datalist to a line to the streamwrieter sw for the output csv file
sw.WriteLine(string.Join(",", dataList)); sw.WriteLine(string.Join(",", dataList));
//Check if totalNferiliserInput is equal to what comes out of the KPI calculation //Check if totalNferiliserInput is equal to what comes out of the KPI calculation
//PO20240515: Hier gaat het mis!!! //PO20240515: Hier gaat het mis!!!
if (elementName == "nFertilizerNKgHa") 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") }
{ else if (kpio.id == "B2")
//Make a new dataList = new line to be written {
dataList = new List<string> //Make a new dataList = new line to be written
dataList = new List<string>
{ {
//Fill the datalist with this kpi //Fill the datalist with this kpi
run.ToString(), run.ToString(),
@ -555,14 +594,14 @@ namespace FarmmapsKPI
kpio.targetValue, kpio.targetValue,
kpio.thresholdValue kpio.thresholdValue
}; };
//Write the datalist to a line to the streamwrieter sw for the output csv file //Write the datalist to a line to the streamwrieter sw for the output csv file
sw.WriteLine(string.Join(",", dataList)); sw.WriteLine(string.Join(",", dataList));
foreach (string elementName in kpio.B2elements) foreach (string elementName in kpio.B2elements)
{ {
// get B1element from the element called values // get B1element from the element called values
string elementValue = (string)kPIelementsOfBalance.GetType().GetProperty(elementName).GetValue(kPIelementsOfBalance, null); string elementValue = (string)kPIelementsOfBalance.GetType().GetProperty(elementName).GetValue(kPIelementsOfBalance, null);
dataList = new List<string> dataList = new List<string>
{ {
run.ToString(), run.ToString(),
kpio.parentName, kpio.parentName,
@ -577,15 +616,15 @@ namespace FarmmapsKPI
"", "",
"" ""
}; };
//Write the datalist to a line to the streamwrieter sw for the output csv file //Write the datalist to a line to the streamwrieter sw for the output csv file
sw.WriteLine(string.Join(",", dataList)); sw.WriteLine(string.Join(",", dataList));
}
} }
}
else if (kpio.id == "C1") //TtD else if (kpio.id == "C1") //TtD
{ {
//Make a new dataList = new line to be written //Make a new dataList = new line to be written
dataList = new List<string> dataList = new List<string>
{ {
run.ToString(), run.ToString(),
kpio.parentName, kpio.parentName,
@ -600,14 +639,14 @@ namespace FarmmapsKPI
kpio.targetValue, kpio.targetValue,
kpio.thresholdValue kpio.thresholdValue
}; };
//Write the datalist to a line to the streamwrieter sw for the output csv file //Write the datalist to a line to the streamwrieter sw for the output csv file
sw.WriteLine(string.Join(",", dataList)); sw.WriteLine(string.Join(",", dataList));
foreach (string elementName in kpio.C1elements) foreach (string elementName in kpio.C1elements)
{ {
// get C1element from the element called values // get C1element from the element called values
string elementValue = (string)kPIelementsOfBalance.GetType().GetProperty(elementName).GetValue(kPIelementsOfBalance, null); string elementValue = (string)kPIelementsOfBalance.GetType().GetProperty(elementName).GetValue(kPIelementsOfBalance, null);
dataList = new List<string> dataList = new List<string>
{ {
run.ToString(), run.ToString(),
kpio.parentName, kpio.parentName,
@ -622,21 +661,21 @@ namespace FarmmapsKPI
"", "",
"" ""
}; };
//Write the datalist to a line to the streamwrieter sw for the output csv file //Write the datalist to a line to the streamwrieter sw for the output csv file
sw.WriteLine(string.Join(",", dataList)); 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 (string mbp_KPIvariable in mbp_KPIvariables)
foreach (KPIenvironmentMeasureData e in kpio.data.environmentMeasureData)
{ {
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
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 = ""; } dataList = new List<string>
//Make a new dataList = new line to be written
dataList = new List<string>
{ {
run.ToString(), run.ToString(),
kpio.parentName, kpio.parentName,
@ -658,15 +697,15 @@ namespace FarmmapsKPI
mbp_KPIvariable, mbp_KPIvariable,
mbp_KPIvalue mbp_KPIvalue
}; };
//Write the datalist to a line to the streamwrieter sw for the output csv file //Write the datalist to a line to the streamwrieter sw for the output csv file
sw.WriteLine(string.Join(",", dataList)); sw.WriteLine(string.Join(",", dataList));
}
} }
} }
else }
{ else
//Any other KPI, example A1 or D1, with just 1 record to write {
dataList = new List<string> //Any other KPI, example A1 or D1, with just 1 record to write
dataList = new List<string>
{ {
run.ToString(), run.ToString(),
kpio.parentName, kpio.parentName,
@ -681,13 +720,10 @@ namespace FarmmapsKPI
kpio.targetValue, kpio.targetValue,
kpio.thresholdValue kpio.thresholdValue
}; };
//Write the datalist to a line to the streamwrieter sw for the output csv file //Write the datalist to a line to the streamwrieter sw for the output csv file
sw.WriteLine(string.Join(",", dataList)); sw.WriteLine(string.Join(",", dataList));
}
} }
} }
kpioPrevious = kpio;
} }
////Total N applied from input ////Total N applied from input