Skip to content

Commit

Permalink
Merge pull request #689 from bcgov/nmp-654-fertigation-calculations
Browse files Browse the repository at this point in the history
[FEAT][NMP-654] Implement Fertigation calculations
  • Loading branch information
dallascrichmond authored Sep 5, 2024
2 parents a309f08 + 0aba413 commit e21b8a2
Show file tree
Hide file tree
Showing 9 changed files with 562 additions and 167 deletions.
121 changes: 121 additions & 0 deletions app/Agri.CalculateService/CalculateFertigationNutrients.cs
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);
}
}
}
112 changes: 105 additions & 7 deletions app/Agri.Data/SeedData/FertigationData.json
Original file line number Diff line number Diff line change
Expand Up @@ -685,40 +685,49 @@
],
"ProductRateUnits": [
{
"Id": 1,
"Id": 5,
"Name": "US gallon/ac",
"StaticDataVersionId": 14
},
{
"Id": 2,
"Id": 3,
"Name": "L/ac",
"StaticDataVersionId": 14
},
{
"Id": 3,
"Id": 4,
"Name": "Imp. gallon/ac",
"StaticDataVersionId": 14
},
{
"Id": 6,
"Name": "L/ha",
"StaticDataVersionId": 14
}
],
"DensityUnits": [
{
"Id": 1,
"Name": "lb/US gallon",
"Name": "kg/US gallon",
"ConvFactor": 2.65,
"StaticDataVersionId": 14
},
{
"Id": 2,
"Name": "lb/Imp. Gallon",
"Name": "kg/L",
"ConvFactor": 10.02,
"StaticDataVersionId": 14
},
{
"Id": 3,
"Name": "kg/L",
"Name": "lb/imp. gallon",
"ConvFactor": 1,
"StaticDataVersionId": 14
},
{
"Id": 4,
"Name": "kg/US gallon",
"Name": "lb/US gallon",
"ConvFactor": 1.2,
"StaticDataVersionId": 14
}
],
Expand All @@ -739,4 +748,93 @@
"StaticDataVersionId": 14
}
]
,
"FertilizerUnits": [
{
"Id" : 1,
"Name" : "lb/ac",
"DryLiquid" : "dry",
"ConversionToImperialGallonsPerAcre" : 0.0,
"FarmRequiredNutrientsStdUnitsConversion" : 0.4535923700,
"FarmRequiredNutrientsStdUnitsAreaConversion" : 1.0,
"StaticDataVersionId" : 14
},
{
"Id" : 2,
"Name" : "kg/ha",
"DryLiquid" : "dry",
"ConversionToImperialGallonsPerAcre" : 0.0,
"FarmRequiredNutrientsStdUnitsConversion" : 1.0,
"FarmRequiredNutrientsStdUnitsAreaConversion" : 0.4046856422,
"StaticDataVersionId" : 14
},
{
"Id" : 3,
"Name" : "L/ac",
"DryLiquid" : "liquid",
"ConversionToImperialGallonsPerAcre" : 0.219969157,
"FarmRequiredNutrientsStdUnitsConversion" : 0.2641720523,
"FarmRequiredNutrientsStdUnitsAreaConversion" : 1.0,
"StaticDataVersionId" : 14
},
{
"Id" : 4,
"Name" : "imp. gallons/ac",
"DryLiquid" : "liquid",
"ConversionToImperialGallonsPerAcre" : 1.0,
"FarmRequiredNutrientsStdUnitsConversion" : 1.2009499255,
"FarmRequiredNutrientsStdUnitsAreaConversion" : 1.0,
"StaticDataVersionId" : 14
},
{
"Id" : 5,
"Name" : "US gallons/ac",
"DryLiquid" : "liquid",
"ConversionToImperialGallonsPerAcre" : 0.83267384,
"FarmRequiredNutrientsStdUnitsConversion" : 1.0,
"FarmRequiredNutrientsStdUnitsAreaConversion" : 1.0,
"StaticDataVersionId" : 14
},
{
"Id" : 6,
"Name" : "L/ha",
"DryLiquid" : "liquid",
"ConversionToImperialGallonsPerAcre" : 0.0890183597,
"FarmRequiredNutrientsStdUnitsConversion" : 0.2641720523,
"FarmRequiredNutrientsStdUnitsAreaConversion" : 0.4046856422,
"StaticDataVersionId" : 14
},
{
"Id" : 7,
"Name" : "lb/1000ft²",
"DryLiquid" : "dry",
"ConversionToImperialGallonsPerAcre" : 0.0,
"FarmRequiredNutrientsStdUnitsConversion" : 0.4535923700,
"FarmRequiredNutrientsStdUnitsAreaConversion" : 43.56000000001,
"StaticDataVersionId" : 14
}
],
"Schedules": [
{
"Id": 1,
"Name": "Monthly",
"StaticDataVersionId": 14
},
{
"Id": 2,
"Name": "Biweekly",
"StaticDataVersionId": 14
},
{
"Id": 3,
"Name": "Weekly",
"StaticDataVersionId": 14
},
{
"Id": 4,
"Name": "Daily",
"StaticDataVersionId": 14
}
]

}
9 changes: 8 additions & 1 deletion app/Agri.Models/Configuration/Fertigation.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace Agri.Models.Configuration
{
public class Fertigation
{
public Fertigation()
{
// LiquidFertilizerDensities = new List<LiquidFertilizerDensity>();

}

public List<Fertilizer> Fertilizers { get; set; }
Expand All @@ -18,6 +19,12 @@ public Fertigation()
public List<ProductRateUnit> ProductRateUnits { get; set; }
public List<DensityUnit> DensityUnits { get; set; }
public List<InjectionRateUnit> InjectionRateUnits { get; set; }
public List<FertilizerUnit> FertilizerUnit { get; set; }
public List<Scheduling> Schedules { get; set; }

public LiquidFertilizerDensity GetLiquidFertilizerDensity( int id, int densityUnitId){
return LiquidFertilizerDensities.Single(density => density.FertilizerId == id && density.DensityUnitId == densityUnitId);
}

}
}
9 changes: 9 additions & 0 deletions app/Agri.Models/Configuration/Scheduling.cs
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
{
}
}
Loading

0 comments on commit e21b8a2

Please sign in to comment.