master #16
| @@ -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<Item> KPIItems = await _generalService.GetKpiItemsForCropField(cropfieldItem, 3); | ||||
|             List<Item> 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) | ||||
|             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<KPIOutput>(KPIItems[0].Data.ToString()); | ||||
|             kpio = JsonConvert.DeserializeObject<KPIOutput>(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<Item> 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,14 +422,56 @@ namespace FarmmapsKPI | ||||
|             _logger.LogInformation($"Downloaded file {KPIItemPathJson}"); | ||||
|             _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 | ||||
|             List<string> dataList;  | ||||
|             foreach (Item item in KPIItems) | ||||
|             foreach (Item item in KPIItemsClean) | ||||
|                 { | ||||
|                 kpio = JsonConvert.DeserializeObject<KPIOutput>(item.Data.ToString()); | ||||
|                 if (kpio.id != null) | ||||
|                 { | ||||
|                     if (kpio.id != kpioPrevious.id) | ||||
|                 { | ||||
|                     KPIelementsOfBalance kPIelementsOfBalance = kpio.data.values; | ||||
|                     if (kpio.id == "A1")    //TtD | ||||
| @@ -684,11 +723,8 @@ namespace FarmmapsKPI | ||||
|                         //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 | ||||
|             //double totalNapplied = 0.0; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user