diff --git a/CarbonCalculation.cs b/CarbonCalculation.cs index c48f6c1..343cfc8 100644 --- a/CarbonCalculation.cs +++ b/CarbonCalculation.cs @@ -146,30 +146,46 @@ namespace FarmMapsAPI.Carbon { foreach (var cropYear in crops.Crops) { - var cropMapping = mappingCropFarmmapsMiterraRothC.FirstOrDefault(x => x.CropCode == cropYear.CropCode); - if (cropMapping != null) + if (!string.IsNullOrEmpty(cropYear.MiterraCropName)) { - var orgManure = new List(); - if (cropYear.OrganicManures != null) + mappedCropData.Add(new HistoricalCropData { - foreach (var manure in cropYear.OrganicManures) - { - orgManure.Add(new OrganicManureType { Type = manure.Type, Quantity = manure.Quantity }); - } - } - var crop = mappedCropData.SingleOrDefault(x => x.Year == crops.Year); - if (crop == null) - { - mappedCropData.Add(new HistoricalCropData { Year = crops.Year, Crops = new List { new OrgManureApplied { Crop = cropMapping.MiterraRothCCrop, OrganicManures = orgManure, CropYield = cropYear.CropYield } } }); - } - else - { - crop.Crops.Add(new OrgManureApplied { Crop = cropMapping.MiterraRothCCrop, OrganicManures = orgManure, CropYield = cropYear.CropYield, CropRes = cropYear.CropRes }); - } + Year = crops.Year, + Crops = new List { + new OrgManureApplied { + Crop = cropYear.MiterraCropName, + OrganicManures = + cropYear.OrganicManures.Select(x => new OrganicManureType { Type = x.Type, Quantity = x.Quantity }).ToList(), + CropYield = cropYear.CropYield } } + }); } else { - mappedCropData.Add(new HistoricalCropData { Year = crops.Year, Crops = new List { new OrgManureApplied { Crop = cropYear.CropCode, OrganicManures = null, CropYield = cropYear.CropYield } } }); + var cropMapping = mappingCropFarmmapsMiterraRothC.FirstOrDefault(x => x.CropCode == cropYear.CropCode); + if (cropMapping != null) + { + var orgManure = new List(); + if (cropYear.OrganicManures != null) + { + foreach (var manure in cropYear.OrganicManures) + { + orgManure.Add(new OrganicManureType { Type = manure.Type, Quantity = manure.Quantity }); + } + } + var crop = mappedCropData.SingleOrDefault(x => x.Year == crops.Year); + if (crop == null) + { + mappedCropData.Add(new HistoricalCropData { Year = crops.Year, Crops = new List { new OrgManureApplied { Crop = cropMapping.MiterraRothCCrop, OrganicManures = orgManure, CropYield = cropYear.CropYield } } }); + } + else + { + crop.Crops.Add(new OrgManureApplied { Crop = cropMapping.MiterraRothCCrop, OrganicManures = orgManure, CropYield = cropYear.CropYield, CropRes = cropYear.CropRes }); + } + } + else + { + mappedCropData.Add(new HistoricalCropData { Year = crops.Year, Crops = new List { new OrgManureApplied { Crop = cropYear.CropCode, OrganicManures = null, CropYield = cropYear.CropYield } } }); + } } } @@ -1493,10 +1509,6 @@ namespace FarmMapsAPI.Carbon sumCarbonGreenManure += item.Value.SingleOrDefault(x => x.Key == "CarbonGreenManure")?.Value ?? 0; sumCarbonCompost += item.Value.SingleOrDefault(x => x.Key == "CarbonCompost")?.Value ?? 0; } - sumCarbonManure = sumCarbonManure / 0.54; - sumCarbonResidues = sumCarbonResidues / 0.54; - sumCarbonGreenManure = sumCarbonGreenManure / 0.54; - sumCarbonCompost = sumCarbonCompost / 0.54; soc_Summary[(fieldLoop, yLoop)].Add(new ColumnValue("SOC_CarbonManure", sumCarbonManure)); soc_Summary[(fieldLoop, yLoop)].Add(new ColumnValue("SOC_CarbonResidues", sumCarbonResidues)); soc_Summary[(fieldLoop, yLoop)].Add(new ColumnValue("SOC_CarbonGreenManure", sumCarbonGreenManure)); @@ -1518,7 +1530,7 @@ namespace FarmMapsAPI.Carbon var crpField = cropFieldSummary.Single(x => x.Name == item.Key.Item1); var summary = crpField.Summary.Single(x => x.Year == item.Key.Item2); - summary.Balance = sum / 0.54; + summary.Balance = sum; } foreach (var item in socIni) @@ -1639,13 +1651,14 @@ namespace FarmMapsAPI.Carbon foreach (var item in soc_Summary.Where(x=>x.Key.Item2 == farm.Key.Item2)) { + var cropfieldArea = item.Value.SingleOrDefault(x => x.Key == "Total_crop_area")?.Value ?? 0; sumCarbonMan += item.Value.SingleOrDefault(x => x.Key == "SOC_CarbonManure")?.Value ?? 0; sumCarbonRes += item.Value.SingleOrDefault(x => x.Key == "SOC_CarbonResidues")?.Value ?? 0; sumCarbonGreenMan += item.Value.SingleOrDefault(x => x.Key == "SOC_CarbonGreenManure")?.Value ?? 0; sumCarbonCom += item.Value.SingleOrDefault(x => x.Key == "SOC_CarbonCompost")?.Value ?? 0; sumEmision += item.Value.SingleOrDefault(x => x.Key == "Emision")?.Value ?? 0; - sumBal += item.Value.SingleOrDefault(x => x.Key == "Balance")?.Value / 0.54 ?? 0; + sumBal += item.Value.SingleOrDefault(x => x.Key == "Balance")?.Value * cropfieldArea ?? 0; sumIni += item.Value.SingleOrDefault(x => x.Key == "Initial")?.Value ?? 0; sumInicc += item.Value.SingleOrDefault(x => x.Key == "IniCcontent")?.Value ?? 0; } @@ -1708,7 +1721,7 @@ namespace FarmMapsAPI.Carbon var soilPropertiesField = new Dictionary(); foreach (var field in farmInput.CropFields) { - soilPropertiesField.Add(field.Name, new SoilPropertyType { OM_Cont = field.SoilProperty.OM_Const.Value, Clay_Content = field.SoilProperty.Clay_Content.Value, OM_Corr = field.SoilProperty.OM_Const.Value }); + soilPropertiesField.Add(field.Name, new SoilPropertyType { OM_Cont = field.SoilProperty.OM_Const.Value, Clay_Content = field.SoilProperty.Clay_Content.Value, OM_Corr = field.SoilProperty.OM_Const.Value <= 20 ? field.SoilProperty.OM_Const.Value : 0 }); } return soilPropertiesField; diff --git a/Models/FarmMapsCarbonRequest.cs b/Models/FarmMapsCarbonRequest.cs index c2d8ab4..3cf7517 100644 --- a/Models/FarmMapsCarbonRequest.cs +++ b/Models/FarmMapsCarbonRequest.cs @@ -55,6 +55,7 @@ namespace FarmMapsAPI.Carbon.Models { [Required] public string CropCode { get; set; } + public string MiterraCropName { get; set; } public List OrganicManures { get; set; } public int? CropYield { get; set; } public bool CropRes { get; set; }