From 08d9f94f2b0862ab25bd651cf0183b1d333e136d Mon Sep 17 00:00:00 2001 From: Mark van der Wal Date: Wed, 8 Apr 2020 22:17:39 +0200 Subject: [PATCH] saves targetn information to a json file. --- FarmmapsNbs/Models/TargetNData.cs | 9 +++++++++ FarmmapsNbs/NbsApplication.cs | 20 +++++++++++++++----- FarmmapsNbs/NitrogenInput.json | 27 +++++++++++++++++++++++++++ FarmmapsNbs/NitrogenService.cs | 10 ++++------ 4 files changed, 55 insertions(+), 11 deletions(-) create mode 100644 FarmmapsNbs/Models/TargetNData.cs diff --git a/FarmmapsNbs/Models/TargetNData.cs b/FarmmapsNbs/Models/TargetNData.cs new file mode 100644 index 0000000..d4aaea9 --- /dev/null +++ b/FarmmapsNbs/Models/TargetNData.cs @@ -0,0 +1,9 @@ +namespace FarmmapsNbs.Models +{ + public class TargetNData + { + public double TSum { get; set; } + public int TargetYield { get; set; } + public double TargetN { get; set; } + } +} \ No newline at end of file diff --git a/FarmmapsNbs/NbsApplication.cs b/FarmmapsNbs/NbsApplication.cs index 9d20dc6..e621bf4 100644 --- a/FarmmapsNbs/NbsApplication.cs +++ b/FarmmapsNbs/NbsApplication.cs @@ -96,9 +96,19 @@ namespace FarmmapsNbs _logger.LogInformation($"Calculating targetN with targetYield: {input.TargetYield}"); var targetNItem = await _nitrogenService.CreateTargetNItem(cropfieldItem); - var targetN = await _nitrogenService.CalculateTargetN(cropfieldItem, targetNItem, plantingDate, + var targetNData = await _nitrogenService.CalculateTargetN(cropfieldItem, targetNItem, plantingDate, measurementDate, input.PotatoPurposeType, "irmi", input.TargetYield); - _logger.LogInformation($"TargetN: {targetN}"); + + if (targetNData == null) + { + _logger.LogError("Something went wrong with TargetN calculation"); + return; + } + + _logger.LogInformation($"TargetN: {targetNData.TargetN}"); + + var targetNDataPath = Path.Combine(DownloadFolder, $"{input.OutputFileName}.targetn.json"); + await File.WriteAllTextAsync(targetNDataPath, JsonConvert.SerializeObject(targetNData, Formatting.Indented)); _logger.LogInformation("Calculating uptake map"); var uptakeMapItem = @@ -107,17 +117,17 @@ namespace FarmmapsNbs _logger.LogInformation("Downloading uptake map"); await _farmmapsApiService.DownloadItemAsync(uptakeMapItem.Code, - Path.Combine(DownloadFolder, $"{uptakeMapItem.Name}.zip")); + Path.Combine(DownloadFolder, $"{input.OutputFileName}.uptake.zip")); _logger.LogInformation("Calculating appliance map"); var applianceMapItem = await _nitrogenService.CalculateApplicationMap(cropfieldItem, isariaGeotiffItem, plantingDate, measurementDate, - "irmi", targetN); + "irmi", targetNData.TargetN); _logger.LogInformation("Downloading appliance map"); await _farmmapsApiService.DownloadItemAsync(applianceMapItem.Code, - Path.Combine(DownloadFolder, $"{applianceMapItem.Name}.zip")); + Path.Combine(DownloadFolder, $"{input.OutputFileName}.application.zip")); } } } \ No newline at end of file diff --git a/FarmmapsNbs/NitrogenInput.json b/FarmmapsNbs/NitrogenInput.json index f272013..add2203 100644 --- a/FarmmapsNbs/NitrogenInput.json +++ b/FarmmapsNbs/NitrogenInput.json @@ -7,5 +7,32 @@ "potatoPurposeType": "consumption", "targetYield": 45, "geometryJson": { "type": "Polygon", "coordinates": [ [ [ 3.40843828875524, 50.638966444680605 ], [ 3.408953272886064, 50.639197789621612 ], [ 3.409242951459603, 50.639469958681836 ], [ 3.409328782148028, 50.639612846807708 ], [ 3.409457528180712, 50.639789755314411 ], [ 3.409639918393741, 50.640014292074966 ], [ 3.409833037442765, 50.640211611372706 ], [ 3.410069071836049, 50.640395321698435 ], [ 3.410380208081761, 50.640572227259661 ], [ 3.410605513638958, 50.640715112034222 ], [ 3.411925160474145, 50.641177783561204 ], [ 3.411935889310142, 50.640728720085136 ], [ 3.412590348309737, 50.63948356709389 ], [ 3.413244807309242, 50.638224772339846 ], [ 3.413400375432099, 50.637901562841307 ], [ 3.413539850300779, 50.637449065809889 ], [ 3.413475477284437, 50.637418445552932 ], [ 3.40999396998362, 50.637449065810451 ], [ 3.409940325803365, 50.638102293212661 ], [ 3.409575545377398, 50.638483338338325 ], [ 3.409060561246574, 50.638707881340494 ], [ 3.40843828875524, 50.638966444680605 ] ] ] } + }, + { + "isariaZipFile": "Scan_1_20190605.zip", + "outputFileName": "vranbs2", + "plantingDate": "2019-04-18", + "measurementDate": "2019-06-05", + "potatoPurposeType": "starch", + "targetYield": 45, + "geometryJson": { "type": "Polygon", "coordinates": [ [ [ 3.40843828875524, 50.638966444680605 ], [ 3.408953272886064, 50.639197789621612 ], [ 3.409242951459603, 50.639469958681836 ], [ 3.409328782148028, 50.639612846807708 ], [ 3.409457528180712, 50.639789755314411 ], [ 3.409639918393741, 50.640014292074966 ], [ 3.409833037442765, 50.640211611372706 ], [ 3.410069071836049, 50.640395321698435 ], [ 3.410380208081761, 50.640572227259661 ], [ 3.410605513638958, 50.640715112034222 ], [ 3.411925160474145, 50.641177783561204 ], [ 3.411935889310142, 50.640728720085136 ], [ 3.412590348309737, 50.63948356709389 ], [ 3.413244807309242, 50.638224772339846 ], [ 3.413400375432099, 50.637901562841307 ], [ 3.413539850300779, 50.637449065809889 ], [ 3.413475477284437, 50.637418445552932 ], [ 3.40999396998362, 50.637449065810451 ], [ 3.409940325803365, 50.638102293212661 ], [ 3.409575545377398, 50.638483338338325 ], [ 3.409060561246574, 50.638707881340494 ], [ 3.40843828875524, 50.638966444680605 ] ] ] } + }, + { + "isariaZipFile": "Scan_1_20190605.zip", + "outputFileName": "vranbs3", + "plantingDate": "2019-04-18", + "measurementDate": "2019-06-20", + "potatoPurposeType": "starch", + "targetYield": 45, + "geometryJson": { "type": "Polygon", "coordinates": [ [ [ 3.40843828875524, 50.638966444680605 ], [ 3.408953272886064, 50.639197789621612 ], [ 3.409242951459603, 50.639469958681836 ], [ 3.409328782148028, 50.639612846807708 ], [ 3.409457528180712, 50.639789755314411 ], [ 3.409639918393741, 50.640014292074966 ], [ 3.409833037442765, 50.640211611372706 ], [ 3.410069071836049, 50.640395321698435 ], [ 3.410380208081761, 50.640572227259661 ], [ 3.410605513638958, 50.640715112034222 ], [ 3.411925160474145, 50.641177783561204 ], [ 3.411935889310142, 50.640728720085136 ], [ 3.412590348309737, 50.63948356709389 ], [ 3.413244807309242, 50.638224772339846 ], [ 3.413400375432099, 50.637901562841307 ], [ 3.413539850300779, 50.637449065809889 ], [ 3.413475477284437, 50.637418445552932 ], [ 3.40999396998362, 50.637449065810451 ], [ 3.409940325803365, 50.638102293212661 ], [ 3.409575545377398, 50.638483338338325 ], [ 3.409060561246574, 50.638707881340494 ], [ 3.40843828875524, 50.638966444680605 ] ] ] } + }, + { + "isariaZipFile": "Scan_1_20190605.zip", + "outputFileName": "vranbs4", + "plantingDate": "2019-04-18", + "measurementDate": "2019-07-03", + "potatoPurposeType": "starch", + "targetYield": 45, + "geometryJson": { "type": "Polygon", "coordinates": [ [ [ 3.40843828875524, 50.638966444680605 ], [ 3.408953272886064, 50.639197789621612 ], [ 3.409242951459603, 50.639469958681836 ], [ 3.409328782148028, 50.639612846807708 ], [ 3.409457528180712, 50.639789755314411 ], [ 3.409639918393741, 50.640014292074966 ], [ 3.409833037442765, 50.640211611372706 ], [ 3.410069071836049, 50.640395321698435 ], [ 3.410380208081761, 50.640572227259661 ], [ 3.410605513638958, 50.640715112034222 ], [ 3.411925160474145, 50.641177783561204 ], [ 3.411935889310142, 50.640728720085136 ], [ 3.412590348309737, 50.63948356709389 ], [ 3.413244807309242, 50.638224772339846 ], [ 3.413400375432099, 50.637901562841307 ], [ 3.413539850300779, 50.637449065809889 ], [ 3.413475477284437, 50.637418445552932 ], [ 3.40999396998362, 50.637449065810451 ], [ 3.409940325803365, 50.638102293212661 ], [ 3.409575545377398, 50.638483338338325 ], [ 3.409060561246574, 50.638707881340494 ], [ 3.40843828875524, 50.638966444680605 ] ] ] } } ] \ No newline at end of file diff --git a/FarmmapsNbs/NitrogenService.cs b/FarmmapsNbs/NitrogenService.cs index ce90d39..bc230f2 100644 --- a/FarmmapsNbs/NitrogenService.cs +++ b/FarmmapsNbs/NitrogenService.cs @@ -3,6 +3,7 @@ using System.Globalization; using System.Threading.Tasks; using FarmmapsApi.Models; using FarmmapsApi.Services; +using FarmmapsNbs.Models; using Microsoft.Extensions.Logging; using static FarmmapsApi.Extensions; using static FarmmapsApiSamples.Constants; @@ -42,7 +43,7 @@ namespace FarmmapsNbs /// The date the crop is planted /// The date the measurements are taken /// The TargetN - public async Task CalculateTargetN(Item cropfieldItem, Item targetNItem, DateTime plantingDate, + public async Task CalculateTargetN(Item cropfieldItem, Item targetNItem, DateTime plantingDate, DateTime measurementDate, string inputType, string purposeType, int targetYield) { var nbsTargetNRequest = new TaskRequest {TaskType = VRANBS_TASK}; @@ -66,14 +67,11 @@ namespace FarmmapsNbs if(itemTask.State == ItemTaskState.Error) { _logger.LogError($"Something went wrong with task execution: {itemTask.Message}"); - return 0; + return null; } var item = await _farmmapsApiService.GetItemAsync(targetNItem.Code); - if (item.Data.ContainsKey("TargetN")) - return item.Data.Value("TargetN"); - - return 0; + return item.Data.ToObject(); } ///