details about N & P balance written to output file

This commit is contained in:
2024-02-12 15:43:00 +01:00
parent e4087adc77
commit 5d02370f56
3 changed files with 146 additions and 33 deletions

View File

@@ -71,7 +71,7 @@ namespace FarmmapsKPI
StreamWriter sw;
string KPIItemCsv = Path.GetFileNameWithoutExtension(fnKPIinput) + "_Items.csv";
string KPIItemPathCsv = Path.Combine(downloadFolder, KPIItemCsv);
List<string> headerList = new List<string> { "parentName", "cropfieldcode","area_ha", "cropTypeCode", "cropTypeName", "KPIid", "KPIvariable", "KPIvalue", "KPIunit", "KPItargetvalue", "KPIthresholdValue",
List<string> headerList = new List<string> { "parentName", "cropfieldcode", "area_ha", "cropTypeCode", "cropTypeName", "KPIid", "KPIvariable", "KPIvalue", "KPIunit", "KPItargetvalue", "KPIthresholdValue",
"mbp_productCode","mbp_productName","mbp_quantity","mbp_unitCode","mbp_date","mbp_KPIvariable","mbp_KPIvalue"};
//Create a new csv file. Means if existing then overwritten !!!
sw = new StreamWriter(KPIItemPathCsv);
@@ -88,7 +88,7 @@ namespace FarmmapsKPI
TimeSpan tsTotalEstimated;
for (int i = 0; i < fieldsInputs.Count; i++)
//for (int i = 7; i < 8; i++)
//for (int i = 3; i < 4; i++)
{
watch.Restart();
input = fieldsInputs[i];
@@ -362,7 +362,7 @@ namespace FarmmapsKPI
//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 = 7; //if we know we should be getting 7 KPI items (A1,B1,B2,C1,D1,F1,F2)
targetKPIitemsCount = 7; //if we know we should be getting 7 KPI items (A1,B1,B2,C1,D1,F1,F2) //8 if also E1
while (trycnt == 0 || (KPIItems.Count < targetKPIitemsCount & trycnt < maxtries))
{
_logger.LogInformation($"Call nr {trycnt + 1}");
@@ -396,7 +396,7 @@ namespace FarmmapsKPI
{
if (kpio.id != kpioPrevious.id)
{
if (kpio.id != "E1")
if (kpio.id == "B1")
{
//Make a new dataList = new line to be written
dataList = new List<string> { };
@@ -414,8 +414,69 @@ namespace FarmmapsKPI
dataList.Add(kpio.thresholdValue);
//Write the datalist to a line to the streamwrieter sw for the output csv file
sw.WriteLine(string.Join(",", dataList));
KPIelementsOfBalance kPIelementsOfBalance = kpio.data.values;
foreach (string elementName in kpio.B1elements)
{
// get B1element from the element called values
dataList = new List<string> { };
string elementValue = (string)kPIelementsOfBalance.GetType().GetProperty(elementName).GetValue(kPIelementsOfBalance, null);
dataList.Add(kpio.parentName);
dataList.Add(cropfieldItem.Code);
dataList.Add(kpio.data.area);
dataList.Add(kpio.data.cropTypeCode);
dataList.Add(kpio.data.cropTypeName);
dataList.Add(kpio.id);
dataList.Add(elementName); // specific output variable name for B1 element
dataList.Add(elementValue); // specific output value name for B1 element
dataList.Add(kpio.unit);
dataList.Add("");
dataList.Add("");
//Write the datalist to a line to the streamwrieter sw for the output csv file
sw.WriteLine(string.Join(",", dataList));
}
}
else
else if (kpio.id == "B2")
{
//Make a new dataList = new line to be written
dataList = new List<string> { };
//Fill the datalist with this kpi
dataList.Add(kpio.parentName);
dataList.Add(cropfieldItem.Code);
dataList.Add(kpio.data.area);
dataList.Add(kpio.data.cropTypeCode);
dataList.Add(kpio.data.cropTypeName);
dataList.Add(kpio.id);
dataList.Add(kpio.quantity); // in KPI output quantity is what we call KPIvariable in headerlist of csv file
dataList.Add(kpio.value);
dataList.Add(kpio.unit);
dataList.Add(kpio.targetValue);
dataList.Add(kpio.thresholdValue);
//Write the datalist to a line to the streamwrieter sw for the output csv file
sw.WriteLine(string.Join(",", dataList));
KPIelementsOfBalance kPIelementsOfBalance = kpio.data.values;
foreach (string elementName in kpio.B2elements)
{
// get B1element from the element called values
dataList = new List<string> { };
string elementValue = (string)kPIelementsOfBalance.GetType().GetProperty(elementName).GetValue(kPIelementsOfBalance, null);
dataList.Add(kpio.parentName);
dataList.Add(cropfieldItem.Code);
dataList.Add(kpio.data.area);
dataList.Add(kpio.data.cropTypeCode);
dataList.Add(kpio.data.cropTypeName);
dataList.Add(kpio.id);
dataList.Add(elementName); // specific output variable name for B2 element
dataList.Add(elementValue); // specific output value name for B2 element
dataList.Add(kpio.unit);
dataList.Add("");
dataList.Add("");
//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")
{
//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)
@@ -449,39 +510,59 @@ namespace FarmmapsKPI
}
}
}
else
{
//Make a new dataList = new line to be written
dataList = new List<string> { };
//Fill the datalist with this kpi
dataList.Add(kpio.parentName);
dataList.Add(cropfieldItem.Code);
dataList.Add(kpio.data.area);
dataList.Add(kpio.data.cropTypeCode);
dataList.Add(kpio.data.cropTypeName);
dataList.Add(kpio.id);
dataList.Add(kpio.quantity); // in KPI output quantity is what we call KPIvariable in headerlist of csv file
dataList.Add(kpio.value);
dataList.Add(kpio.unit);
dataList.Add(kpio.targetValue);
dataList.Add(kpio.thresholdValue);
//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
double totalNapplied = 0.0;
double operationNapplied;
for (int i = 0; i < crpOperationItemCodes.Count; i++)
{
codeOperation = crpOperationItemCodes[i];
crpOperationItem = await _farmmapsApiService.GetItemAsync(codeOperation);
dynamic data = JObject.Parse(crpOperationItem.Data.ToString(Formatting.None));
if (data.n != null)
{
operationNapplied = crpOperationItem.Data.GetValue("n").ToObject<double>();
totalNapplied = totalNapplied + operationNapplied;
}
}
//Also add totalNapplied to the csv
dataList = new List<string> { };
dataList.Add(kpioPrevious.parentName);
dataList.Add(cropfieldItem.Code);
try { dataList.Add(kpioPrevious.data.area); } catch { dataList.Add(""); };
try { dataList.Add(kpioPrevious.data.cropTypeCode); } catch { dataList.Add(""); };
try { dataList.Add(kpioPrevious.data.cropTypeName); } catch { dataList.Add(""); };
dataList.Add("");
dataList.Add("totalNapplied");
dataList.Add(totalNapplied.ToString());
dataList.Add("kg/ha");
dataList.Add("");
dataList.Add("");
sw.WriteLine(string.Join(",", dataList));
////Total N applied from input
//double totalNapplied = 0.0;
//double operationNapplied;
//for (int i = 0; i < crpOperationItemCodes.Count; i++)
//{
// codeOperation = crpOperationItemCodes[i];
// crpOperationItem = await _farmmapsApiService.GetItemAsync(codeOperation);
// dynamic data = JObject.Parse(crpOperationItem.Data.ToString(Formatting.None));
// if (data.n != null)
// {
// operationNapplied = crpOperationItem.Data.GetValue("n").ToObject<double>();
// totalNapplied = totalNapplied + operationNapplied;
// }
//}
////Also add totalNapplied to the csv
//dataList = new List<string> { };
//dataList.Add(kpioPrevious.parentName);
//dataList.Add(cropfieldItem.Code);
//try { dataList.Add(kpioPrevious.data.area); } catch { dataList.Add(""); };
//try { dataList.Add(kpioPrevious.data.cropTypeCode); } catch { dataList.Add(""); };
//try { dataList.Add(kpioPrevious.data.cropTypeName); } catch { dataList.Add(""); };
//dataList.Add("");
//dataList.Add("totalNapplied");
//dataList.Add(totalNapplied.ToString());
//dataList.Add("kg/ha");
//dataList.Add("");
//dataList.Add("");
//sw.WriteLine(string.Join(",", dataList));
//Clean up. Only newly created fields
//Look up instruction in Swagger / api / v1 / items /{ code}