forked from FarmMaps/FarmMapsApiClient
farmmapsKPI receives minimum necessary input and writes relevant KPI output
This commit is contained in:
@@ -46,21 +46,45 @@ namespace FarmmapsKPI
|
||||
await _farmmapsApiService.GetCurrentUserCodeAsync();
|
||||
var roots = await _farmmapsApiService.GetCurrentUserRootsAsync();
|
||||
|
||||
//Where to write the output
|
||||
string downloadFolder = fieldsInputs[0].DownloadFolder;
|
||||
if (string.IsNullOrEmpty(downloadFolder))
|
||||
{
|
||||
downloadFolder = "Downloads";
|
||||
}
|
||||
if (!Directory.Exists(downloadFolder))
|
||||
Directory.CreateDirectory(downloadFolder);
|
||||
|
||||
//Write the same info to a single csv file. Note this means existing file will be overwritten!
|
||||
StreamWriter sw; string KPIItemPathCsv = Path.Combine(downloadFolder, "KPIItems.csv");
|
||||
List<string> headerList = new List<string> { "parentName", "area_ha", "cropTypeCode", "cropTypeName", "KPIid", "KPIvariable", "KPIvalue", "KPIunit", "KPItargetvalue", "KPIthresholdValue" };
|
||||
//Create a new csv file. Means if existing then overwritten !!!
|
||||
sw = new StreamWriter(KPIItemPathCsv);
|
||||
sw.WriteLine(string.Join(",", headerList));
|
||||
|
||||
//Now loop through the list of cropfields in KPIinput.json and get the KPI's for each cropfield
|
||||
foreach (var input in fieldsInputs)
|
||||
{
|
||||
try
|
||||
{
|
||||
await Process(roots, input);
|
||||
await Process(roots, input, sw);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
//Close the csv file, write message to screen
|
||||
sw.Close();
|
||||
_logger.LogInformation($"Done! Written all KPI for all fields in 'KPIinput.json' to output file '{KPIItemPathCsv}'");
|
||||
}
|
||||
|
||||
private async Task Process(List<UserRoot> roots, KPIInput input)
|
||||
private async Task Process(List<UserRoot> roots, KPIInput input, StreamWriter sw)
|
||||
{
|
||||
KPIOutput kpio;
|
||||
KPIOutput kpioPrevious = null;
|
||||
|
||||
string downloadFolder = input.DownloadFolder;
|
||||
if (string.IsNullOrEmpty(downloadFolder)) {
|
||||
downloadFolder = "Downloads";
|
||||
@@ -69,20 +93,13 @@ namespace FarmmapsKPI
|
||||
Directory.CreateDirectory(downloadFolder);
|
||||
|
||||
// !!specify if you are using an already created cropfield:
|
||||
bool useCreatedCropfield = input.UseCreatedCropfield;
|
||||
bool useCreatedCropRecording = input.UseCreatedCropRecording;
|
||||
bool useCreatedOperations = input.UseCreatedOperation;
|
||||
bool useCreatedCropfieldCharacteristic = input.UseCreatedCropfieldCharacteristic;
|
||||
var cropYear = input.CropYear;
|
||||
var fieldName = input.fieldName;
|
||||
//bool storeSatelliteStatistics = input.StoreSatelliteStatisticsSingleImage;
|
||||
//bool storeSatelliteStatisticsCropYear = input.StoreSatelliteStatisticsCropYear;
|
||||
//List<string> SatelliteBands = new List<string>(1) { input.SatelliteBand };
|
||||
//string headerLineStats = $"FieldName,satelliteDate,satelliteBand,max,min,mean,mode,median,stddev,minPlus,curtosis,maxMinus,skewness,variance,populationCount,variationCoefficient,confidenceIntervalLow, confidenceIntervalHigh,confidenceIntervalErrorMargin" + Environment.NewLine;
|
||||
|
||||
bool useExistingCropfieldWithChildren = input.UseExistingCropfieldWithChildren;
|
||||
int cropYear = input.CropYear;
|
||||
string fieldName = input.fieldName;
|
||||
string fieldGeom = input.GeometryJson.ToString(Formatting.None);
|
||||
|
||||
//Settings
|
||||
string settingsfile = $"Settings_{fieldName}.json";
|
||||
|
||||
LoadSettings(settingsfile);
|
||||
|
||||
var uploadedRoot = roots.SingleOrDefault(r => r.Name == "USER_IN");
|
||||
@@ -101,14 +118,14 @@ namespace FarmmapsKPI
|
||||
|
||||
// Use already created cropfield or create new one, added a Data input, with field specific data for the KPI calculation
|
||||
Item cropfieldItem;
|
||||
//1 useCreatedCropfield = false -> create new
|
||||
//2 useCreatedCropfield = true && CropfieldItemCode = "" or absent -> read from settings json
|
||||
//3 useCreatedCropfield = true && CropfieldItemCode like "deb48a74c5b54299bb852f17288010e9" in KPIinput -> use this one
|
||||
if (useCreatedCropfield == false || string.IsNullOrEmpty(_settings.CropfieldItemCode))
|
||||
//1 useExistingCropfieldWithChildren = false -> create new
|
||||
//2 useExistingCropfieldWithChildren = true && input.CropfieldItemCode = "" or absent -> read from settings json
|
||||
//3 useExistingCropfieldWithChildren = true && input.CropfieldItemCode like "deb48a74c5b54299bb852f17288010e9" in KPIinput -> use this one
|
||||
if (useExistingCropfieldWithChildren == false)
|
||||
{
|
||||
_logger.LogInformation("Creating cropfield, writting to settings file");
|
||||
cropfieldItem = await _generalService.CreateCropfieldItemAsync(myDriveRoot.Code,
|
||||
$"{input.OutputFileName}", cropYear, input.GeometryJson.ToString(Formatting.None), input.DataCropfield.ToString(Formatting.None));
|
||||
$"{fieldName}", cropYear, input.GeometryJson.ToString(Formatting.None), input.DataCropfield.ToString(Formatting.None));
|
||||
_settings.CropfieldItemCode = cropfieldItem.Code;
|
||||
SaveSettings(settingsfile);
|
||||
}
|
||||
@@ -121,15 +138,16 @@ namespace FarmmapsKPI
|
||||
{
|
||||
_logger.LogInformation("reading CropfieldItemCode from KPIinput.json");
|
||||
cropfieldItem = await _farmmapsApiService.GetItemAsync(input.CropfieldItemCode);
|
||||
SaveSettings(settingsfile);
|
||||
}
|
||||
|
||||
|
||||
// Use already created croprecording or create new one, added a Data input, with field specific data for the KPI calculation
|
||||
Item crprecItem;
|
||||
//1 useCreatedCropRecording = false -> create new
|
||||
//2 useCreatedCropRecording = true && CropRecordingItemCode = "" or absent -> read from settings json
|
||||
//3 useCreatedCropRecording = true && CropRecordingItemCode like "deb48a74c5b54299bb852f17288010e9" in KPIinput -> use this one
|
||||
if (useCreatedCropRecording == false || string.IsNullOrEmpty(_settings.CropRecordingItemCode))
|
||||
//1 useExistingCropfieldWithChildren = false -> create new
|
||||
//2 useExistingCropfieldWithChildren = true && input.CropRecordingItemCode = "" or absent -> read from settings json
|
||||
//3 useExistingCropfieldWithChildren = true && input.CropRecordingItemCode like "deb48a74c5b54299bb852f17288010e9" in KPIinput -> use this one
|
||||
if (useExistingCropfieldWithChildren == false)
|
||||
{
|
||||
_logger.LogInformation("RunCropRecordingTask ...");
|
||||
crprecItem = await _generalService.RunCropRecordingTask(cropfieldItem);
|
||||
@@ -145,29 +163,55 @@ namespace FarmmapsKPI
|
||||
{
|
||||
_logger.LogInformation("reading CropRecordingItemCode from KPIinput.json");
|
||||
crprecItem = await _farmmapsApiService.GetItemAsync(input.CropRecordingItemCode);
|
||||
SaveSettings(settingsfile);
|
||||
}
|
||||
|
||||
// Use already created operation or create new one, added a Data input, with field specific data for the KPI calculation
|
||||
// Use already created operations or create new one,s added a Data input, with field specific data for the KPI calculation
|
||||
List<Item> crpOperationItems = new List<Item> { };
|
||||
List<string> crpOperationItemCodes = new List<string> { };
|
||||
Item crpOperationItem;
|
||||
//1 useCreatedOperation = false -> create new
|
||||
//2 useCreatedOperation = true && OperationItemCode = "" or absent -> read from settings json
|
||||
//3 useCreatedOperation = true && OperationItemCode like "deb48a74c5b54299bb852f17288010e9" in KPIinput -> use this one
|
||||
if (useCreatedOperations == false || string.IsNullOrEmpty(_settings.OperationItemCode))
|
||||
string dataOperation;
|
||||
string codeOperation;
|
||||
//1 useExistingCropfieldWithChildren = false -> create new
|
||||
//2 useExistingCropfieldWithChildren = true && input.OperationItemCode = "" or absent -> read from settings json
|
||||
//3 useExistingCropfieldWithChildren = true && input.OperationItemCode like "deb48a74c5b54299bb852f17288010e9" in KPIinput -> use this one
|
||||
if (useExistingCropfieldWithChildren == false)
|
||||
{
|
||||
_logger.LogInformation("CreateOperationItemAsync ...");
|
||||
crpOperationItem = await _generalService.CreateOperationItemAsync(crprecItem.Code, cropYear, input.GeometryJson.ToString(Formatting.None), input.DataOperation.ToString(Formatting.None));
|
||||
_settings.OperationItemCode = crpOperationItem.Code;
|
||||
for (int i = 0; i < input.DataOperations.Length; i++)
|
||||
{
|
||||
dataOperation = input.DataOperations[i].ToString(Formatting.None);
|
||||
dynamic data = JObject.Parse(dataOperation);
|
||||
_logger.LogInformation($"CreateOperationItemAsync ... for operation {i}: '{data.name}', {data.n} kg N/ha, on date '{data.from}'");
|
||||
crpOperationItem = await _generalService.CreateOperationItemAsync(crprecItem.Code, cropYear, fieldGeom, dataOperation);
|
||||
crpOperationItems.Add(crpOperationItem);
|
||||
crpOperationItemCodes.Add(crpOperationItem.Code);
|
||||
}
|
||||
_settings.OperationItemCodes = crpOperationItemCodes.ToArray();
|
||||
SaveSettings(settingsfile);
|
||||
}
|
||||
else if (string.IsNullOrEmpty(input.CropfieldItemCode))
|
||||
{
|
||||
_logger.LogInformation("reading OperationItemCode from settings file");
|
||||
crpOperationItem = await _farmmapsApiService.GetItemAsync(_settings.OperationItemCode);
|
||||
for (int i = 0; i < _settings.OperationItemCodes.Length; i++)
|
||||
{
|
||||
codeOperation = _settings.OperationItemCodes[i];
|
||||
crpOperationItem = await _farmmapsApiService.GetItemAsync(codeOperation);
|
||||
crpOperationItems.Add(crpOperationItem);
|
||||
crpOperationItemCodes.Add(crpOperationItem.Code);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogInformation("reading OperationItemCode from KPIinput.json");
|
||||
crpOperationItem = await _farmmapsApiService.GetItemAsync(input.OperationItemCode);
|
||||
_logger.LogInformation("reading OperationItemCodes from KPIinput.json");
|
||||
for (int i = 0; i < input.OperationItemCodes.Length; i++)
|
||||
{
|
||||
codeOperation = input.OperationItemCodes[i];
|
||||
crpOperationItem = await _farmmapsApiService.GetItemAsync(codeOperation);
|
||||
crpOperationItems.Add(crpOperationItem);
|
||||
crpOperationItemCodes.Add(crpOperationItem.Code);
|
||||
}
|
||||
_settings.OperationItemCodes = crpOperationItemCodes.ToArray();
|
||||
SaveSettings(settingsfile);
|
||||
}
|
||||
|
||||
// The cropfieldCharacteristicItem is used to enter crop yields
|
||||
@@ -175,17 +219,17 @@ namespace FarmmapsKPI
|
||||
// Nutrient balance.
|
||||
// Use already created cropfieldCharacteristicItem or create new one, added a Data input, with field specific data for the KPI calculation
|
||||
Item cropfieldCharacteristicItem;
|
||||
//1 useCreatedCropfieldCharacteristic = false -> create new
|
||||
//2 useCreatedCropfieldCharacteristic = true && CropfieldCharacteristicItemCode = "" or absent -> read from settings json
|
||||
//3 useCreatedCropfieldCharacteristic = true && CropfieldCharacteristicItemCode like "deb48a74c5b54299bb852f17288010e9" in KPIinput -> use this one
|
||||
if (useCreatedCropfieldCharacteristic == false)
|
||||
//1 useExistingCropfieldWithChildren = false -> create new
|
||||
//2 useExistingCropfieldWithChildren = true && input.CropfieldCharacteristicItemCode = "" or absent -> read from settings json
|
||||
//3 useExistingCropfieldWithChildren = true && input.CropfieldCharacteristicItemCode like "deb48a74c5b54299bb852f17288010e9" in KPIinput -> use this one
|
||||
if (useExistingCropfieldWithChildren == false)
|
||||
{
|
||||
_logger.LogInformation("CreateCropfieldCharacteristicItemAsync ...");
|
||||
cropfieldCharacteristicItem = await _generalService.CreateCropfieldCharacteristicItemAsync(cropfieldItem.Code, cropYear, input.GeometryJson.ToString(Formatting.None), input.DataCropfieldCharacteristic.ToString(Formatting.None));
|
||||
cropfieldCharacteristicItem = await _generalService.CreateCropfieldCharacteristicItemAsync(cropfieldItem.Code, cropYear, fieldGeom, input.DataCropfieldCharacteristic.ToString(Formatting.None));
|
||||
_settings.CropfieldCharacteristicItemCode = cropfieldCharacteristicItem.Code;
|
||||
SaveSettings(settingsfile);
|
||||
}
|
||||
else if (string.IsNullOrEmpty(input.CropfieldCharacteristicItemCode))
|
||||
else if (string.IsNullOrEmpty(input.CropfieldItemCode))
|
||||
{
|
||||
_logger.LogInformation("reading OperationItemCode from settings file");
|
||||
cropfieldCharacteristicItem = await _farmmapsApiService.GetItemAsync(_settings.CropfieldCharacteristicItemCode);
|
||||
@@ -194,38 +238,77 @@ namespace FarmmapsKPI
|
||||
{
|
||||
_logger.LogInformation("reading CropfieldCharacteristicItemCode from KPIinput.json");
|
||||
cropfieldCharacteristicItem = await _farmmapsApiService.GetItemAsync(input.CropfieldCharacteristicItemCode);
|
||||
SaveSettings(settingsfile);
|
||||
}
|
||||
|
||||
// Inspect the children. If all is well, cropfield will have one crprec and one edicrop.characteristic
|
||||
// And the crprec will have 0-many operations as children
|
||||
// And the Data of an operation will have specification of how much fertilizer was applied
|
||||
// And crprec can have multiple operations
|
||||
// Note existing cropfields and croprecordings keep existing properties you added in previous runs
|
||||
// (unless you deleted them)
|
||||
// So ech time you run with "UseCreatedCropfield": true & "UseCreatedCropRecording": false -> a new recording will be added to the existing cropfield
|
||||
// So ech time you run with "UseCreatedCropfield": true & "useCreatedCropfieldCharacteristic": false -> a new CropfieldCharacteristic will be added to the existing cropfield
|
||||
// So ech time you run with "UseCreatedCropRecording": true & "UseCreatedOperation": false -> a new operation will be added to the existing crop recording
|
||||
// Inspect the children and grandchildren. If all is well, cropfield will have:
|
||||
// one crprec, with 0-many operations as children. And the Data of an operation will have specification of how much fertilizer was applied
|
||||
// one edicrop.characteristic (with yield in the data)
|
||||
var cropfieldChildren = await _farmmapsApiService.GetItemChildrenAsync(cropfieldItem.Code);
|
||||
var crprecChildren = await _farmmapsApiService.GetItemChildrenAsync(crprecItem.Code);
|
||||
|
||||
//Now get the KPIs for this cropfield, mounted with operations & cropyield
|
||||
// Get KPI data for saving it in a file, here the generalsedrvice is called to get the KPI data
|
||||
_logger.LogInformation($"GetKpiItemsForCropField('{cropfieldItem.Code}')");
|
||||
var KPIItem = await _generalService.GetKpiItemsForCropField(cropfieldItem);
|
||||
List<Item> KPIItems = await _generalService.GetKpiItemsForCropField(cropfieldItem);
|
||||
_logger.LogInformation($"Found {KPIItems.Count} KPI items");
|
||||
|
||||
//Download KPI's into a json output file for this specific cropfield (with unique cropfieldItem.Code)
|
||||
var KPIItemPath = Path.Combine(downloadFolder, $"KPIItems_{cropfieldItem.Code}.json");
|
||||
_logger.LogInformation($"Found {KPIItem.Count} KPI items");
|
||||
string KPIItemPathJson = Path.Combine(downloadFolder, $"KPIItems_{cropfieldItem.Code}.json");
|
||||
var count = 0;
|
||||
await Task.Delay(50);
|
||||
foreach (var item in KPIItem)
|
||||
foreach (Item item in KPIItems)
|
||||
{
|
||||
Console.WriteLine($"KPI item #{count}: {item.Name}");
|
||||
File.AppendAllText(KPIItemPath, item.Data + Environment.NewLine);
|
||||
//Console.WriteLine($"KPI item #{count}: {item.Name}");
|
||||
File.AppendAllText(KPIItemPathJson, item.Data + Environment.NewLine);
|
||||
count++;
|
||||
}
|
||||
_logger.LogInformation($"Downloaded file {KPIItemPath}");
|
||||
_logger.LogInformation($"Downloaded file {KPIItemPathJson}");
|
||||
|
||||
//Write to the csv file that collects all KPI's for all the crop fields
|
||||
List<string> dataList;
|
||||
foreach (Item item in KPIItems)
|
||||
{
|
||||
dataList = new List<string> { };
|
||||
kpio = JsonConvert.DeserializeObject<KPIOutput>(item.Data.ToString());
|
||||
//Seems sometimes duplicate KPI items are returned. So check that here and only write if this kpio is different from previous
|
||||
if (kpio != kpioPrevious)
|
||||
{
|
||||
dataList.Add(kpio.parentName);
|
||||
dataList.Add(kpio.data.area);
|
||||
dataList.Add(kpio.data.cropTypeCode);
|
||||
dataList.Add(kpio.data.cropTypeName);
|
||||
dataList.Add(kpio.id);
|
||||
dataList.Add(kpio.quantity);
|
||||
dataList.Add(kpio.value);
|
||||
dataList.Add(kpio.unit);
|
||||
dataList.Add(kpio.targetValue);
|
||||
dataList.Add(kpio.thresholdValue);
|
||||
sw.WriteLine(string.Join(",", dataList));
|
||||
}
|
||||
kpioPrevious = kpio;
|
||||
}
|
||||
|
||||
//Clean up. Only newly created fields
|
||||
//Look up instruction in Swagger / api / v1 / items /{ code}
|
||||
if (useExistingCropfieldWithChildren == false && input.DeleteNewlyCreatedAfterCalc == true)
|
||||
{
|
||||
// Tested and found that if I delete the parent (cropfieldItem) then
|
||||
// automatically deletes children (crprecItem, cropfieldCharacteristicItem, KPIcontainer)
|
||||
// and automatically deletes grand children (crprecItem: operations, KPIcontainer: KPI items)
|
||||
await _farmmapsApiService.DeleteItemAsync(cropfieldItem.Code);
|
||||
//Do we need to wait here for the task to be completed? Or does it continue in the background?
|
||||
//await Task.Delay(60000); // wait 60 secs for task to be completed
|
||||
//Before the GetItem you will see cropfieldItem, crprecItem etc exist (with data).
|
||||
//After the GetItem these cropfieldItem, crprecItem etc are null which means they have been succesfully deleted.
|
||||
//// Check if below works, i.e. is item really deleted?
|
||||
//cropfieldItem = await _farmmapsApiService.GetItemAsync(cropfieldItem.Code);
|
||||
//// Check if croprecording has also been deleted. If not also delete it
|
||||
//crprecItem = await _farmmapsApiService.GetItemAsync(crprecItem.Code);
|
||||
//// Check if 1 operation has also been deleted. If not also delete it
|
||||
//crpOperationItem = await _farmmapsApiService.GetItemAsync(crpOperationItemCodes[0]);
|
||||
//// Check if 1 cropfieldCharacteristicItem has also been deleted. If not also delete it
|
||||
//cropfieldCharacteristicItem = await _farmmapsApiService.GetItemAsync(cropfieldCharacteristicItem.Code);
|
||||
}
|
||||
}
|
||||
|
||||
// Functions to save previously created cropfields
|
||||
|
||||
Reference in New Issue
Block a user