details about N & P balance written to output file

This commit is contained in:
tamara 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; StreamWriter sw;
string KPIItemCsv = Path.GetFileNameWithoutExtension(fnKPIinput) + "_Items.csv"; string KPIItemCsv = Path.GetFileNameWithoutExtension(fnKPIinput) + "_Items.csv";
string KPIItemPathCsv = Path.Combine(downloadFolder, KPIItemCsv); 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"}; "mbp_productCode","mbp_productName","mbp_quantity","mbp_unitCode","mbp_date","mbp_KPIvariable","mbp_KPIvalue"};
//Create a new csv file. Means if existing then overwritten !!! //Create a new csv file. Means if existing then overwritten !!!
sw = new StreamWriter(KPIItemPathCsv); sw = new StreamWriter(KPIItemPathCsv);
@ -88,7 +88,7 @@ namespace FarmmapsKPI
TimeSpan tsTotalEstimated; TimeSpan tsTotalEstimated;
for (int i = 0; i < fieldsInputs.Count; i++) for (int i = 0; i < fieldsInputs.Count; i++)
//for (int i = 7; i < 8; i++) //for (int i = 3; i < 4; i++)
{ {
watch.Restart(); watch.Restart();
input = fieldsInputs[i]; input = fieldsInputs[i];
@ -362,7 +362,7 @@ namespace FarmmapsKPI
//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 //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)) while (trycnt == 0 || (KPIItems.Count < targetKPIitemsCount & trycnt < maxtries))
{ {
_logger.LogInformation($"Call nr {trycnt + 1}"); _logger.LogInformation($"Call nr {trycnt + 1}");
@ -396,7 +396,7 @@ namespace FarmmapsKPI
{ {
if (kpio.id != kpioPrevious.id) if (kpio.id != kpioPrevious.id)
{ {
if (kpio.id != "E1") if (kpio.id == "B1")
{ {
//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> { };
@ -414,8 +414,69 @@ namespace FarmmapsKPI
dataList.Add(kpio.thresholdValue); dataList.Add(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));
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 //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 (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; kpioPrevious = kpio;
} }
//Total N applied ////Total N applied from input
double totalNapplied = 0.0; //double totalNapplied = 0.0;
double operationNapplied; //double operationNapplied;
for (int i = 0; i < crpOperationItemCodes.Count; i++) //for (int i = 0; i < crpOperationItemCodes.Count; i++)
{ //{
codeOperation = crpOperationItemCodes[i]; // codeOperation = crpOperationItemCodes[i];
crpOperationItem = await _farmmapsApiService.GetItemAsync(codeOperation); // crpOperationItem = await _farmmapsApiService.GetItemAsync(codeOperation);
dynamic data = JObject.Parse(crpOperationItem.Data.ToString(Formatting.None)); // dynamic data = JObject.Parse(crpOperationItem.Data.ToString(Formatting.None));
if (data.n != null) // if (data.n != null)
{ // {
operationNapplied = crpOperationItem.Data.GetValue("n").ToObject<double>(); // operationNapplied = crpOperationItem.Data.GetValue("n").ToObject<double>();
totalNapplied = totalNapplied + operationNapplied; // totalNapplied = totalNapplied + operationNapplied;
} // }
} //}
//Also add totalNapplied to the csv ////Also add totalNapplied to the csv
dataList = new List<string> { }; //dataList = new List<string> { };
dataList.Add(kpioPrevious.parentName); //dataList.Add(kpioPrevious.parentName);
dataList.Add(cropfieldItem.Code); //dataList.Add(cropfieldItem.Code);
try { dataList.Add(kpioPrevious.data.area); } catch { dataList.Add(""); }; //try { dataList.Add(kpioPrevious.data.area); } catch { dataList.Add(""); };
try { dataList.Add(kpioPrevious.data.cropTypeCode); } catch { dataList.Add(""); }; //try { dataList.Add(kpioPrevious.data.cropTypeCode); } catch { dataList.Add(""); };
try { dataList.Add(kpioPrevious.data.cropTypeName); } catch { dataList.Add(""); }; //try { dataList.Add(kpioPrevious.data.cropTypeName); } catch { dataList.Add(""); };
dataList.Add(""); //dataList.Add("");
dataList.Add("totalNapplied"); //dataList.Add("totalNapplied");
dataList.Add(totalNapplied.ToString()); //dataList.Add(totalNapplied.ToString());
dataList.Add("kg/ha"); //dataList.Add("kg/ha");
dataList.Add(""); //dataList.Add("");
dataList.Add(""); //dataList.Add("");
sw.WriteLine(string.Join(",", dataList)); //sw.WriteLine(string.Join(",", dataList));
//Clean up. Only newly created fields //Clean up. Only newly created fields
//Look up instruction in Swagger / api / v1 / items /{ code} //Look up instruction in Swagger / api / v1 / items /{ code}

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
namespace FarmmapsKPI.Models namespace FarmmapsKPI.Models
@ -15,12 +16,21 @@ namespace FarmmapsKPI.Models
public string parentName { get; set; } public string parentName { get; set; }
public string targetValue { get; set; } public string targetValue { get; set; }
public string thresholdValue { get; set; } public string thresholdValue { get; set; }
public List<string> B1elements;
public List<string> B2elements;
public KPIOutput()
{
this.B1elements = new List<string>() { "disposal", "nFixationNKgHa", "nDepositionNKgHa","nFertilizerNKgHa","sowPlantingNKgHa" }; //TODO Tamara: rename disposal to nHarvestedNKgHa
this.B2elements = new List<string>() { "disposal", "pFertillizerNKgHa", "sowPlantingNKgHa" }; //TODO Tamara: rename from pFertilizerNKgHa to pFertilizerPKgHa; rename disposal to pHarvestedPKgHa
}
} }
public class KPIOutputData public class KPIOutputData
{ {
public string area { get; set; } public string area { get; set; }
public string cropTypeCode { get; set; } public string cropTypeCode { get; set; }
public string cropTypeName { get; set; } public string cropTypeName { get; set; }
public KPIelementsOfBalance values { get; set; }
public KPIenvironmentMeasureData[] environmentMeasureData { get; set; } public KPIenvironmentMeasureData[] environmentMeasureData { get; set; }
} }
@ -35,4 +45,15 @@ namespace FarmmapsKPI.Models
public string aquaticLife { get; set; } public string aquaticLife { get; set; }
public string groundWater { get; set; } public string groundWater { get; set; }
} }
public class KPIelementsOfBalance
{
public string disposal { get; set; }
public string nFixationNKgHa { get; set; }
public string nDepositionNKgHa { get; set; }
public string nFertilizerNKgHa { get; set; }
public string sowPlantingNKgHa { get; set; }
public string pFertillizerNKgHa { get; set; } //TODO Tamara: delete this one after renaming
public string pFertilizerPKgHa { get; set; }
public string sowPlantingPKgHa { get; set; }
}
} }

View File

@ -48,3 +48,14 @@ dataOperations: "status": "3":
"DataCropfieldCharacteristic": { "DataCropfieldCharacteristic": {
"code": "860619", //PO20231004: so what does this code mean? Can we see the code list somewhere? NOT YET TESTED "code": "860619", //PO20231004: so what does this code mean? Can we see the code list somewhere? NOT YET TESTED
KPIelementsOfBalance: Tamara
a. define elements for C1 organic matter supply
b. for nitrogen B1 rename disposal to nHarvestedNKgHa
c. for phosporous B2 rename disposal to pHarvestedPKgHa
d. for phosporous B2 rename from pFertilizerNKgHa to pFertilizerPKgHa
e. idem sowPlantingNKgHa
f. add straw for N & P in output
g. add 2 examples in KPIinput.json: straw yes/no removed. Bijv. nStrawLeftInField EN nStrawRemoved
h. update KPIdefinitions.csv
MBP (E1): Pepijn: in same way as for B1 & B2