2020-04-08 18:23:22 +00:00
|
|
|
using System;
|
2020-04-08 19:43:03 +00:00
|
|
|
using System.Collections.Generic;
|
2020-04-08 18:23:22 +00:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using FarmmapsApi;
|
2020-04-08 19:43:03 +00:00
|
|
|
using FarmmapsApi.Models;
|
2020-04-08 18:23:22 +00:00
|
|
|
using FarmmapsApi.Services;
|
2020-04-08 19:43:03 +00:00
|
|
|
using FarmmapsNbs.Models;
|
2020-04-08 18:23:22 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2020-04-08 19:43:03 +00:00
|
|
|
using Newtonsoft.Json;
|
2020-04-08 18:23:22 +00:00
|
|
|
|
2020-04-08 18:39:38 +00:00
|
|
|
namespace FarmmapsNbs
|
2020-04-08 18:23:22 +00:00
|
|
|
{
|
|
|
|
public class NbsApplication : IApplication
|
|
|
|
{
|
|
|
|
private const string DownloadFolder = "Downloads";
|
|
|
|
|
|
|
|
private readonly ILogger<NbsApplication> _logger;
|
|
|
|
private readonly FarmmapsApiService _farmmapsApiService;
|
|
|
|
private readonly NitrogenService _nitrogenService;
|
|
|
|
private readonly GeneralService _generalService;
|
|
|
|
|
|
|
|
public NbsApplication(ILogger<NbsApplication> logger, FarmmapsApiService farmmapsApiService,
|
|
|
|
GeneralService generalService, NitrogenService nitrogenService)
|
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
_farmmapsApiService = farmmapsApiService;
|
|
|
|
_generalService = generalService;
|
|
|
|
_nitrogenService = nitrogenService;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task RunAsync()
|
|
|
|
{
|
2020-04-08 19:43:03 +00:00
|
|
|
var nitrogenInputJson = File.ReadAllText("NitrogenInput.json");
|
|
|
|
List<NitrogenInput> nitrogenInputs = JsonConvert.DeserializeObject<List<NitrogenInput>>(nitrogenInputJson);
|
|
|
|
|
|
|
|
if (!Directory.Exists(DownloadFolder))
|
|
|
|
Directory.CreateDirectory(DownloadFolder);
|
2020-04-08 18:23:22 +00:00
|
|
|
|
2020-04-08 19:43:03 +00:00
|
|
|
// !! this call is needed the first time an api is called with a fresh clientid and secret !!
|
|
|
|
await _farmmapsApiService.GetCurrentUserCodeAsync();
|
|
|
|
var roots = await _farmmapsApiService.GetCurrentUserRootsAsync();
|
2020-04-08 18:23:22 +00:00
|
|
|
|
2020-04-08 19:43:03 +00:00
|
|
|
foreach (var input in nitrogenInputs)
|
|
|
|
{
|
|
|
|
try
|
2020-04-08 18:23:22 +00:00
|
|
|
{
|
2020-04-08 19:43:03 +00:00
|
|
|
await Process(roots, input);
|
2020-04-08 18:23:22 +00:00
|
|
|
}
|
2020-04-08 19:43:03 +00:00
|
|
|
catch (Exception ex)
|
2020-04-08 18:23:22 +00:00
|
|
|
{
|
2020-04-08 19:43:03 +00:00
|
|
|
_logger.LogError(ex.Message);
|
2020-04-08 18:23:22 +00:00
|
|
|
}
|
2020-04-08 19:43:03 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-08 18:23:22 +00:00
|
|
|
|
2020-04-08 19:43:03 +00:00
|
|
|
private async Task Process(List<UserRoot> roots, NitrogenInput input)
|
|
|
|
{
|
|
|
|
var plantingDate = input.PlantingDate;
|
|
|
|
var measurementDate = input.MeasurementDate;
|
2020-04-08 18:23:22 +00:00
|
|
|
|
2020-04-08 19:43:03 +00:00
|
|
|
var uploadedRoot = roots.SingleOrDefault(r => r.Name == "Uploaded");
|
|
|
|
if (uploadedRoot == null)
|
|
|
|
{
|
|
|
|
_logger.LogError("Could not find a needed root item");
|
|
|
|
return;
|
|
|
|
}
|
2020-04-08 18:23:22 +00:00
|
|
|
|
2020-04-08 19:43:03 +00:00
|
|
|
var myDriveRoot = roots.SingleOrDefault(r => r.Name == "My drive");
|
|
|
|
if (myDriveRoot == null)
|
|
|
|
{
|
|
|
|
_logger.LogError("Could not find a needed root item");
|
|
|
|
return;
|
|
|
|
}
|
2020-04-08 18:23:22 +00:00
|
|
|
|
2020-04-08 19:43:03 +00:00
|
|
|
var cropfieldItem = await _generalService.CreateCropfieldItemAsync(myDriveRoot.Code,
|
|
|
|
$"VRA NBS cropfield {input.OutputFileName}", DateTime.Now.Year, input.GeometryJson.ToString(Formatting.None));
|
2020-04-08 18:23:22 +00:00
|
|
|
|
2020-04-08 19:43:03 +00:00
|
|
|
var dataPath = Path.Combine("Data", input.IsariaZipFile);
|
|
|
|
var isariaShapeItem =
|
|
|
|
await _generalService.UploadZipWithShapeAsync(uploadedRoot, dataPath, Path.GetFileNameWithoutExtension(input.IsariaZipFile));
|
|
|
|
if (isariaShapeItem == null)
|
|
|
|
{
|
|
|
|
_logger.LogError("Could not find isaria shape item");
|
|
|
|
return;
|
2020-04-08 18:23:22 +00:00
|
|
|
}
|
2020-04-08 19:43:03 +00:00
|
|
|
|
|
|
|
_logger.LogInformation($"Converting shape to geotiff");
|
|
|
|
var isariaGeotiffItem = await _generalService.ShapeToGeotiff(isariaShapeItem);
|
|
|
|
if (isariaGeotiffItem == null)
|
2020-04-08 18:23:22 +00:00
|
|
|
{
|
2020-04-08 19:43:03 +00:00
|
|
|
_logger.LogError("Something went wrong with isaria shape to geotiff transformation");
|
|
|
|
return;
|
2020-04-08 18:23:22 +00:00
|
|
|
}
|
2020-04-08 19:43:03 +00:00
|
|
|
|
|
|
|
_logger.LogInformation($"Calculating targetN with targetYield: {input.TargetYield}");
|
|
|
|
var targetNItem = await _nitrogenService.CreateTargetNItem(cropfieldItem);
|
|
|
|
var targetN = await _nitrogenService.CalculateTargetN(cropfieldItem, targetNItem, plantingDate,
|
|
|
|
measurementDate, input.PotatoPurposeType, "irmi", input.TargetYield);
|
|
|
|
_logger.LogInformation($"TargetN: {targetN}");
|
|
|
|
|
|
|
|
_logger.LogInformation("Calculating uptake map");
|
|
|
|
var uptakeMapItem =
|
|
|
|
await _nitrogenService.CalculateUptakeMap(cropfieldItem, isariaGeotiffItem, plantingDate,
|
|
|
|
measurementDate);
|
|
|
|
|
|
|
|
_logger.LogInformation("Downloading uptake map");
|
|
|
|
await _farmmapsApiService.DownloadItemAsync(uptakeMapItem.Code,
|
|
|
|
Path.Combine(DownloadFolder, $"{uptakeMapItem.Name}.zip"));
|
|
|
|
|
|
|
|
_logger.LogInformation("Calculating appliance map");
|
|
|
|
var applianceMapItem =
|
|
|
|
await _nitrogenService.CalculateApplicationMap(cropfieldItem, isariaGeotiffItem, plantingDate,
|
|
|
|
measurementDate,
|
|
|
|
"irmi", targetN);
|
|
|
|
|
|
|
|
_logger.LogInformation("Downloading appliance map");
|
|
|
|
await _farmmapsApiService.DownloadItemAsync(applianceMapItem.Code,
|
|
|
|
Path.Combine(DownloadFolder, $"{applianceMapItem.Name}.zip"));
|
2020-04-08 18:23:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|