-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #689 from bcgov/nmp-654-fertigation-calculations
[FEAT][NMP-654] Implement Fertigation calculations
- Loading branch information
Showing
9 changed files
with
562 additions
and
167 deletions.
There are no files selected for viewing
121 changes: 121 additions & 0 deletions
121
app/Agri.CalculateService/CalculateFertigationNutrients.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
|
||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Agri.Data; | ||
using Agri.Models.Calculate; | ||
using Agri.Models.Configuration; | ||
using Newtonsoft.Json; | ||
|
||
namespace Agri.CalculateService | ||
{ | ||
public interface ICalculateFertigationNutrients | ||
{ | ||
FertilizerNutrients GetFertilizerNutrients(int FertilizerId, string fertilizerType, decimal applicationRate, int applicationRateUnits, decimal density, int densityUnits, decimal userN, decimal userP2o5, decimal userK2o, bool customFertilizer); | ||
} | ||
|
||
public class CalculateFertigationNutrients : ICalculateFertigationNutrients | ||
{ | ||
private readonly IAgriConfigurationRepository _sd; | ||
private readonly Fertigation _fd; | ||
|
||
public CalculateFertigationNutrients( IAgriConfigurationRepository sd) | ||
{ | ||
_sd = sd; | ||
_fd = GetFertigationData(); | ||
} | ||
|
||
// This processing detemines the N P K values in lb per acre for the fertilizer select and appricatio rate/density etc. | ||
public FertilizerNutrients GetFertilizerNutrients( | ||
int FertilizerId, | ||
string fertilizerType, | ||
decimal applicationRate, | ||
int applicationRateUnits, | ||
decimal density, | ||
int densityUnits, | ||
decimal userN, | ||
decimal userP2o5, | ||
decimal userK2o, | ||
bool customFertilizer) | ||
{ | ||
var fn = new FertilizerNutrients(); | ||
|
||
// get the fertilizer N P K % values from fertlizer look up | ||
// for dry fertilizers | ||
// N (lb/ac) = Application rate converted to lb/ac * N % | ||
// P and K same as above | ||
// for wet fertilizers | ||
// N (lb/ac) = Application rate converted to lb/ac * N% * Density converted to lb / imp gallons | ||
// | ||
|
||
decimal applicationRateConversion = 0; | ||
decimal densityInSelectedUnit = 0; | ||
decimal densityUnitConversion = 0; | ||
|
||
Fertilizer _fertilizer = getFertilizer(FertilizerId); | ||
|
||
if (customFertilizer && fertilizerType == "dry" || !customFertilizer && _fertilizer.DryLiquid == "dry") | ||
{ | ||
densityUnitConversion = 1; | ||
switch (applicationRateUnits) | ||
{ | ||
case 1: // application rate in lb/ac no conversion required | ||
applicationRateConversion = 1; | ||
break; | ||
|
||
case 2: // application rate in kg/ha, convert to lb/ac | ||
ConversionFactor _cf = _sd.GetConversionFactor(); | ||
applicationRateConversion = _cf.KilogramPerHectareToPoundPerAcreConversion; | ||
break; | ||
|
||
case 7: // application rate in lb/100 ft squared, convert to lb/ac | ||
ConversionFactor _cf1 = _sd.GetConversionFactor(); | ||
applicationRateConversion = _cf1.PoundPer1000FtSquaredToPoundPerAcreConversion; | ||
break; | ||
} | ||
} | ||
else //use liquid fertilizer | ||
{ | ||
FertilizerUnit _fU = _sd.GetFertilizerUnit(applicationRateUnits); | ||
applicationRateConversion = _fU.ConversionToImperialGallonsPerAcre; | ||
//if (customFertilizer) | ||
densityInSelectedUnit = density; | ||
// else | ||
// { | ||
// LiquidFertilizerDensity _lfd = GetLiquidFertilizerDensity(FertilizerId, densityUnits); | ||
// densityInSelectedUnit = _lfd.Value; | ||
// } | ||
DensityUnit _du = _sd.GetDensityUnit(densityUnits); | ||
densityUnitConversion = _du.ConvFactor * densityInSelectedUnit; | ||
} | ||
|
||
fn.fertilizer_N = applicationRate * decimal.Divide(userN, 100) * applicationRateConversion * densityUnitConversion; | ||
fn.fertilizer_P2O5 = applicationRate * decimal.Divide(userP2o5, 100) * applicationRateConversion * densityUnitConversion; | ||
fn.fertilizer_K2O = applicationRate * decimal.Divide(userK2o, 100) * applicationRateConversion * densityUnitConversion; | ||
|
||
return fn; | ||
} | ||
|
||
private Fertilizer getFertilizer(int id){ | ||
//return _fd.Fertilizers.Single(fert => fert.Id == id); | ||
|
||
|
||
List<Fertilizer> fertilizers = _fd.Fertilizers; | ||
foreach (var fert in fertilizers){ | ||
if(fert.Id == id){ | ||
return fert; | ||
} | ||
} | ||
return _fd.Fertilizers[0]; | ||
|
||
|
||
} | ||
|
||
|
||
private Fertigation GetFertigationData() | ||
{ | ||
var filePath = "../../../Agri.Data/SeedData/FertigationData.json"; | ||
var jsonData = System.IO.File.ReadAllText(filePath); | ||
return JsonConvert.DeserializeObject<Fertigation>(jsonData); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace Agri.Models.Configuration | ||
{ | ||
public class Scheduling: SelectOption | ||
{ | ||
} | ||
} |
Oops, something went wrong.