From 5d02370f560a47c8073ccde5f9fb0dd4b3f34901 Mon Sep 17 00:00:00 2001 From: tamara Date: Mon, 12 Feb 2024 15:43:00 +0100 Subject: [PATCH] details about N & P balance written to output file --- FarmmapsKPI/KPIApplication.cs | 147 +++++++++++++++++++++++++------- FarmmapsKPI/Models/KPIoutput.cs | 21 +++++ FarmmapsKPI/TODO.txt | 11 +++ 3 files changed, 146 insertions(+), 33 deletions(-) diff --git a/FarmmapsKPI/KPIApplication.cs b/FarmmapsKPI/KPIApplication.cs index cbd4991..0d6e514 100644 --- a/FarmmapsKPI/KPIApplication.cs +++ b/FarmmapsKPI/KPIApplication.cs @@ -71,7 +71,7 @@ namespace FarmmapsKPI StreamWriter sw; string KPIItemCsv = Path.GetFileNameWithoutExtension(fnKPIinput) + "_Items.csv"; string KPIItemPathCsv = Path.Combine(downloadFolder, KPIItemCsv); - List headerList = new List { "parentName", "cropfieldcode","area_ha", "cropTypeCode", "cropTypeName", "KPIid", "KPIvariable", "KPIvalue", "KPIunit", "KPItargetvalue", "KPIthresholdValue", + List headerList = new List { "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 { }; @@ -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 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 { }; + //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 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 { }; + //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(); - totalNapplied = totalNapplied + operationNapplied; - } - } - //Also add totalNapplied to the csv - dataList = new List { }; - 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(); + // totalNapplied = totalNapplied + operationNapplied; + // } + //} + ////Also add totalNapplied to the csv + //dataList = new List { }; + //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} diff --git a/FarmmapsKPI/Models/KPIoutput.cs b/FarmmapsKPI/Models/KPIoutput.cs index 2a64b40..5073eda 100644 --- a/FarmmapsKPI/Models/KPIoutput.cs +++ b/FarmmapsKPI/Models/KPIoutput.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using Newtonsoft.Json.Linq; namespace FarmmapsKPI.Models @@ -15,12 +16,21 @@ namespace FarmmapsKPI.Models public string parentName { get; set; } public string targetValue { get; set; } public string thresholdValue { get; set; } + + public List B1elements; + public List B2elements; + public KPIOutput() + { + this.B1elements = new List() { "disposal", "nFixationNKgHa", "nDepositionNKgHa","nFertilizerNKgHa","sowPlantingNKgHa" }; //TODO Tamara: rename disposal to nHarvestedNKgHa + this.B2elements = new List() { "disposal", "pFertillizerNKgHa", "sowPlantingNKgHa" }; //TODO Tamara: rename from pFertilizerNKgHa to pFertilizerPKgHa; rename disposal to pHarvestedPKgHa + } } public class KPIOutputData { public string area { get; set; } public string cropTypeCode { get; set; } public string cropTypeName { get; set; } + public KPIelementsOfBalance values { get; set; } public KPIenvironmentMeasureData[] environmentMeasureData { get; set; } } @@ -35,4 +45,15 @@ namespace FarmmapsKPI.Models public string aquaticLife { 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; } + } } \ No newline at end of file diff --git a/FarmmapsKPI/TODO.txt b/FarmmapsKPI/TODO.txt index a23ff9d..9419da2 100644 --- a/FarmmapsKPI/TODO.txt +++ b/FarmmapsKPI/TODO.txt @@ -48,3 +48,14 @@ dataOperations: "status": "3": "DataCropfieldCharacteristic": { "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