Beispiel #1
0
        public NOrganicMineralizations GetNMineralization(FarmManure farmManure, int locationid)
        {
            NOrganicMineralizations nOrganicMineralizations = new NOrganicMineralizations();

            NitrogenMineralization myNMineralization = _sd.GetNMineralization(farmManure.NMinerizationId, locationid);

            nOrganicMineralizations.OrganicN_FirstYear = myNMineralization.FirstYearValue;
            nOrganicMineralizations.OrganicN_LongTerm  = myNMineralization.LongTermValue;

            return(nOrganicMineralizations);
        }
Beispiel #2
0
        private void ReCalculateManure(int id)
        {
            Utility.CalculateNutrients calculateNutrients      = new CalculateNutrients(_env, _ud, _sd);
            NOrganicMineralizations    nOrganicMineralizations = new NOrganicMineralizations();

            List <Field> flds = _ud.GetFields();

            foreach (var fld in flds)
            {
                List <NutrientManure> mans = _ud.GetFieldNutrientsManures(fld.fieldName);

                foreach (var nm in mans)
                {
                    if (id.ToString() == nm.manureId)
                    {
                        int    regionid = _ud.FarmDetails().farmRegion.Value;
                        Region region   = _sd.GetRegion(regionid);
                        nOrganicMineralizations = calculateNutrients.GetNMineralization(Convert.ToInt16(nm.manureId), region.locationid);

                        string avail = (nOrganicMineralizations.OrganicN_FirstYear * 100).ToString("###");

                        string nh4 = (calculateNutrients.GetAmmoniaRetention(Convert.ToInt16(nm.manureId), Convert.ToInt16(nm.applicationId)) * 100).ToString("###");

                        NutrientInputs nutrientInputs = new NutrientInputs();

                        calculateNutrients.manure                          = nm.manureId;
                        calculateNutrients.applicationSeason               = nm.applicationId;
                        calculateNutrients.applicationRate                 = Convert.ToDecimal(nm.rate);
                        calculateNutrients.applicationRateUnits            = nm.unitId;
                        calculateNutrients.ammoniaNRetentionPct            = Convert.ToDecimal(nh4);
                        calculateNutrients.firstYearOrganicNAvailablityPct = Convert.ToDecimal(avail);

                        calculateNutrients.GetNutrientInputs(nutrientInputs);

                        nm.yrN    = nutrientInputs.N_FirstYear;
                        nm.yrP2o5 = nutrientInputs.P2O5_FirstYear;
                        nm.yrK2o  = nutrientInputs.K2O_FirstYear;
                        nm.ltN    = nutrientInputs.N_LongTerm;
                        nm.ltP2o5 = nutrientInputs.P2O5_LongTerm;
                        nm.ltK2o  = nutrientInputs.K2O_LongTerm;

                        _ud.UpdateFieldNutrientsManure(fld.fieldName, nm);
                    }
                }
            }
        }
Beispiel #3
0
        public async Task <string> RenderFields()
        {
            Utility.CalculateNutrients calculateNutrients      = new CalculateNutrients(_env, _ud, _sd);
            NOrganicMineralizations    nOrganicMineralizations = new NOrganicMineralizations();

            Utility.SoilTestConversions     stc = new SoilTestConversions(_ud, _sd);
            CalculateCropRequirementRemoval calculateCropRequirementRemoval = new CalculateCropRequirementRemoval(_ud, _sd);

            ReportFieldsViewModel rvm = new ReportFieldsViewModel();

            rvm.fields     = new List <ReportFieldsField>();
            rvm.year       = _ud.FarmDetails().year;
            rvm.methodName = string.IsNullOrEmpty(_ud.FarmDetails().testingMethod) ? "not selected" : _sd.GetSoilTestMethod(_ud.FarmDetails().testingMethod);
            rvm.prevHdg    = _sd.GetUserPrompt("ncreditlabel");

            List <Field> fldList = _ud.GetFields();

            foreach (var f in fldList)
            {
                ReportFieldsField rf = new ReportFieldsField();
                rf.fieldArea      = f.area.ToString();
                rf.fieldName      = f.fieldName;
                rf.fieldComment   = f.comment;
                rf.soiltest       = new ReportFieldSoilTest();
                rf.crops          = new List <ReportFieldCrop>();
                rf.otherNutrients = new List <ReportFieldOtherNutrient>();
                rf.footnotes      = new List <ReportFieldFootnote>();

                if (f.soilTest != null)
                {
                    rf.soiltest.sampleDate = f.soilTest.sampleDate.ToString("MMM yyyy");
                    rf.soiltest.dispNO3H   = f.soilTest.valNO3H.ToString() + " ppm";
                    rf.soiltest.dispP      = f.soilTest.ValP.ToString() + " ppm (" + _sd.SoilTestRating("phosphorous", stc.GetConvertedSTP(f.soilTest)) + ")";
                    rf.soiltest.dispK      = f.soilTest.valK.ToString() + " ppm (" + _sd.SoilTestRating("potassium", stc.GetConvertedSTK(f.soilTest)) + ")";
                    rf.soiltest.dispPH     = f.soilTest.valPH.ToString();
                }

                rf.nutrients = new List <ReportFieldNutrient>();
                if (f.crops != null)
                {
                    foreach (var c in f.crops)
                    {
                        ReportFieldCrop fc = new ReportFieldCrop();

                        fc.cropname = string.IsNullOrEmpty(c.cropOther) ? _sd.GetCrop(Convert.ToInt32(c.cropId)).cropname : c.cropOther;
                        if (c.coverCropHarvested.HasValue)
                        {
                            fc.cropname = c.coverCropHarvested.Value ? fc.cropname + "(harvested)" : fc.cropname;
                        }
                        if (c.prevCropId > 0)
                        {
                            fc.previousCrop = _sd.GetPrevCropType(c.prevCropId).name;
                        }

                        if (_sd.GetCropType(_sd.GetCrop(Convert.ToInt32(c.cropId)).croptypeid).crudeproteinrequired)
                        {
                            if (c.crudeProtien.Value.ToString("#.#") != calculateCropRequirementRemoval.GetCrudeProtienByCropId(Convert.ToInt32(c.cropId)).ToString("#.#"))
                            {
                                ReportFieldFootnote rff = new ReportFieldFootnote();
                                rff.id      = rf.footnotes.Count() + 1;
                                rff.message = "Crude protein adjusted to " + c.crudeProtien.Value.ToString("#.#") + "%";
                                fc.footnote = rff.id.ToString();
                                rf.footnotes.Add(rff);
                            }
                        }

                        if (_sd.GetCropType(_sd.GetCrop(Convert.ToInt32(c.cropId)).croptypeid).modifynitrogen)
                        {
                            // check for standard
                            CropRequirementRemoval cropRequirementRemoval = new CropRequirementRemoval();

                            calculateCropRequirementRemoval.cropid = Convert.ToInt16(c.cropId);
                            calculateCropRequirementRemoval.yield  = Convert.ToDecimal(c.yield);
                            if (c.crudeProtien == (decimal?)null)
                            {
                                calculateCropRequirementRemoval.crudeProtien = null;
                            }
                            else
                            {
                                calculateCropRequirementRemoval.crudeProtien = Convert.ToDecimal(c.crudeProtien);
                            }
                            calculateCropRequirementRemoval.coverCropHarvested = c.coverCropHarvested;
                            calculateCropRequirementRemoval.fieldName          = f.fieldName;
                            string nCredit = c.prevCropId != 0 ? _sd.GetPrevCropType(Convert.ToInt32(c.prevCropId)).nCreditImperial.ToString() : "0";

                            if (!string.IsNullOrEmpty(nCredit))
                            {
                                calculateCropRequirementRemoval.nCredit = Convert.ToInt16(nCredit);
                            }

                            cropRequirementRemoval = calculateCropRequirementRemoval.GetCropRequirementRemoval();

                            string stdNAmt = cropRequirementRemoval.N_Requirement.ToString();

                            if (c.reqN.ToString() != cropRequirementRemoval.N_Requirement.ToString())
                            {
                                ReportFieldFootnote rff = new ReportFieldFootnote();
                                rff.id      = rf.footnotes.Count() + 1;
                                rff.message = "Crop required nitrogen adjusted to " + c.reqN.ToString();
                                fc.footnote = rff.id.ToString();
                                rf.footnotes.Add(rff);
                            }
                        }

                        fc.yield = c.yield;
                        fc.reqN  = -c.reqN;
                        fc.reqP  = -c.reqP2o5;
                        fc.reqK  = -c.reqK2o;
                        fc.remN  = -c.remN;
                        fc.remP  = -c.remP2o5;
                        fc.remK  = -c.remK2o;

                        rf.reqN = rf.reqN + fc.reqN;
                        rf.reqP = rf.reqP + fc.reqP;
                        rf.reqK = rf.reqK + fc.reqK;
                        rf.remN = rf.remN + fc.remN;
                        rf.remP = rf.remP + fc.remP;
                        rf.remK = rf.remK + fc.remK;

                        rf.fieldCrops = rf.fieldCrops + fc.cropname + " ";

                        rf.crops.Add(fc);
                    }
                }
                if (f.nutrients != null)
                {
                    if (f.nutrients.nutrientManures != null)
                    {
                        foreach (var m in f.nutrients.nutrientManures)
                        {
                            FarmManure          manure = _ud.GetFarmManure(Convert.ToInt32(m.manureId));
                            ReportFieldNutrient rfn    = new ReportFieldNutrient();

                            rfn.nutrientName        = manure.name;
                            rfn.nutrientAmount      = String.Format((m.rate) % 1 == 0 ? "{0:#,##0}" : "{0:#,##0.00}", (m.rate));
                            rfn.nutrientSeason      = _sd.GetApplication(m.applicationId.ToString()).season;
                            rfn.nutrientApplication = _sd.GetApplication(m.applicationId.ToString()).application_method;
                            rfn.nutrientUnit        = _sd.GetUnit(m.unitId).name;
                            rfn.reqN = m.yrN;
                            rfn.reqP = m.yrP2o5;
                            rfn.reqK = m.yrK2o;
                            rfn.remN = m.ltN;
                            rfn.remP = m.ltP2o5;
                            rfn.remK = m.ltK2o;
                            rf.nutrients.Add(rfn);

                            rf.reqN = rf.reqN + rfn.reqN;
                            rf.reqP = rf.reqP + rfn.reqP;
                            rf.reqK = rf.reqK + rfn.reqK;
                            rf.remN = rf.remN + rfn.remN;
                            rf.remP = rf.remP + rfn.remP;
                            rf.remK = rf.remK + rfn.remK;

                            int    regionid = _ud.FarmDetails().farmRegion.Value;
                            Region region   = _sd.GetRegion(regionid);
                            nOrganicMineralizations = calculateNutrients.GetNMineralization(Convert.ToInt32(m.manureId), region.locationid);

                            string footNote = "";

                            if (m.nAvail != nOrganicMineralizations.OrganicN_FirstYear * 100)
                            {
                                footNote = "1st Yr Organic N Availability adjusted to " + m.nAvail.ToString("###") + "%";
                            }
                            if (m.nh4Retention != (calculateNutrients.GetAmmoniaRetention(Convert.ToInt16(m.manureId), Convert.ToInt16(m.applicationId)) * 100))
                            {
                                footNote = string.IsNullOrEmpty(footNote) ? "" : footNote + ", ";
                                footNote = footNote + "Ammonium-N Retention adjusted to " + m.nh4Retention.ToString("###") + "%";
                            }
                            if (!string.IsNullOrEmpty(footNote))
                            {
                                ReportFieldFootnote rff = new ReportFieldFootnote();
                                rff.id       = rf.footnotes.Count() + 1;
                                rff.message  = footNote;
                                rfn.footnote = rff.id.ToString();
                                rf.footnotes.Add(rff);
                            }
                        }
                    }
                    if (f.nutrients.nutrientFertilizers != null)
                    {
                        foreach (var ft in f.nutrients.nutrientFertilizers)
                        {
                            string fertilizerName    = string.Empty;
                            ReportFieldNutrient rfn  = new ReportFieldNutrient();
                            FertilizerType      ftyp = _sd.GetFertilizerType(ft.fertilizerTypeId.ToString());

                            if (ftyp.custom)
                            {
                                fertilizerName = ftyp.dry_liquid == "dry" ? "Custom (Dry) " : "Custom (Liquid) ";
                                fertilizerName = fertilizerName + ft.customN.ToString() + "-" + ft.customP2o5.ToString() + "-" + ft.customK2o.ToString();
                                rfn.reqN       = ft.fertN;
                                rfn.reqP       = ft.fertP2o5;
                                rfn.reqK       = ft.fertK2o;
                                rfn.remN       = ft.fertN;
                                rfn.remP       = ft.fertP2o5;
                                rfn.remK       = ft.fertK2o;
                            }
                            else
                            {
                                Fertilizer ff = _sd.GetFertilizer(ft.fertilizerId.ToString());
                                fertilizerName = ff.name;
                                rfn.reqN       = ff.nitrogen;
                                rfn.reqP       = ff.phosphorous;
                                rfn.reqK       = ff.potassium;
                                rfn.remN       = ff.nitrogen;
                                rfn.remP       = ff.phosphorous;
                                rfn.remK       = ff.potassium;
                            }

                            rfn.nutrientName        = fertilizerName;
                            rfn.nutrientApplication = ft.applMethodId > 0 ? _sd.GetFertilizerMethod(ft.applMethodId.ToString()).name : "";
                            rfn.nutrientUnit        = _sd.GetFertilizerUnit(ft.applUnitId).name;

                            rfn.nutrientAmount = String.Format((ft.applRate) % 1 == 0 ? "{0:#,##0}" : "{0:#,##0.00}", (ft.applRate));
                            rf.nutrients.Add(rfn);

                            rf.reqN = rf.reqN + rfn.reqN;
                            rf.reqP = rf.reqP + rfn.reqP;
                            rf.reqK = rf.reqK + rfn.reqK;
                            rf.remN = rf.remN + rfn.remN;
                            rf.remP = rf.remP + rfn.remP;
                            rf.remK = rf.remK + rfn.remK;

                            string footNote = "";

                            if (ftyp.dry_liquid == "liquid")
                            {
                                if (!ftyp.custom)
                                {
                                    if (ft.liquidDensity.ToString("#.##") != _sd.GetLiquidFertilizerDensity(ft.fertilizerId, ft.liquidDensityUnitId).value.ToString("#.##"))
                                    {
                                        footNote = "Liquid density adjusted to " + ft.liquidDensity.ToString("#.##");
                                    }
                                    if (!string.IsNullOrEmpty(footNote))
                                    {
                                        ReportFieldFootnote rff = new ReportFieldFootnote();
                                        rff.id       = rf.footnotes.Count() + 1;
                                        rff.message  = footNote;
                                        rfn.footnote = rff.id.ToString();
                                        rf.footnotes.Add(rff);
                                    }
                                }
                            }
                        }
                    }
                    if (f.nutrients.nutrientOthers != null)
                    {
                        foreach (var o in f.nutrients.nutrientOthers)
                        {
                            ReportFieldOtherNutrient fon = new ReportFieldOtherNutrient();
                            fon.otherName = o.description;
                            fon.reqN      = o.ltN;
                            fon.reqP      = o.ltP2o5;
                            fon.reqK      = o.ltK;
                            fon.remN      = o.yrN;
                            fon.remP      = o.yrP2o5;
                            fon.remK      = o.yrK;
                            rf.otherNutrients.Add(fon);

                            rf.reqN = rf.reqN + fon.reqN;
                            rf.reqP = rf.reqP + fon.reqP;
                            rf.reqK = rf.reqK + fon.reqK;
                            rf.remN = rf.remN + fon.remN;
                            rf.remP = rf.remP + fon.remP;
                            rf.remK = rf.remK + fon.remK;
                        }
                    }
                }

                if (rf.nutrients.Count() == 0)
                {
                    ReportFieldNutrient rfn = new ReportFieldNutrient();
                    rfn.nutrientName   = "None planned";
                    rfn.nutrientAmount = "";
                    rf.nutrients.Add(rfn);
                }
                ChemicalBalanceMessage cbm = new ChemicalBalanceMessage(_ud, _sd);

                var    request = HttpContext.Request;
                string scheme  = request.Scheme;
                string host    = request.Host.ToString();
                string imgLoc  = scheme + "://" + host + "/images/{0}.svg";

                rf.alertMsgs = cbm.DetermineBalanceMessages(f.fieldName);

                if (rf.alertMsgs.FirstOrDefault(r => r.Chemical == "AgrN") != null)
                {
                    rf.alertN    = true;
                    rf.iconAgriN = string.Format(imgLoc, rf.alertMsgs.FirstOrDefault(r => r.Chemical == "AgrN").Icon);
                }
                if (rf.alertMsgs.FirstOrDefault(r => r.Chemical == "AgrP2O5") != null)
                {
                    rf.alertP    = true;
                    rf.iconAgriP = string.Format(imgLoc, rf.alertMsgs.FirstOrDefault(r => r.Chemical == "AgrP2O5").Icon);
                }
                if (rf.alertMsgs.FirstOrDefault(r => r.Chemical == "AgrK2O") != null)
                {
                    rf.alertK    = true;
                    rf.iconAgriK = string.Format(imgLoc, rf.alertMsgs.FirstOrDefault(r => r.Chemical == "AgrK2O").Icon);
                }
                if (rf.alertMsgs.FirstOrDefault(r => r.Chemical == "CropN") != null)
                {
                    rf.alertN    = true;
                    rf.iconCropN = string.Format(imgLoc, rf.alertMsgs.FirstOrDefault(r => r.Chemical == "CropN").Icon);
                }
                if (rf.alertMsgs.FirstOrDefault(r => r.Chemical == "CropP2O5") != null)
                {
                    rf.alertP    = true;
                    rf.iconCropP = string.Format(imgLoc, rf.alertMsgs.FirstOrDefault(r => r.Chemical == "CropP2O5").Icon);
                }
                if (rf.alertMsgs.FirstOrDefault(r => r.Chemical == "CropK2O") != null)
                {
                    rf.alertK    = true;
                    rf.iconCropK = string.Format(imgLoc, rf.alertMsgs.FirstOrDefault(r => r.Chemical == "CropK2O").Icon);
                }

                //replace icon type with actual icon url for screen processing
                foreach (var i in rf.alertMsgs)
                {
                    i.Icon = string.Format(imgLoc, i.Icon);
                }

                rvm.fields.Add(rf);
            }

            var result = await _viewRenderService.RenderToStringAsync("~/Views/Report/ReportFields.cshtml", rvm);

            return(result);
        }
Beispiel #4
0
        public NutrientInputs GetNutrientInputs(
            FarmManure farmManure,
            Region region,
            decimal applicationRate,
            string applicationRateUnits,
            decimal ammoniaNRetentionPct,
            decimal firstYearOrganicNAvailablityPct)
        {
            var nutrientInputs = new NutrientInputs();
            var _cf            = _sd.GetConversionFactor();

            decimal potassiumAvailabilityFirstYear   = _cf.PotassiumAvailabilityFirstYear;
            decimal potassiumAvailabilityLongTerm    = _cf.PotassiumAvailabilityLongTerm;
            decimal potassiumKtoK2Oconversion        = _cf.PotassiumKtoK2OConversion;
            decimal phosphorousAvailabilityFirstYear = _cf.PhosphorousAvailabilityFirstYear;
            decimal phosphorousAvailabilityLongTerm  = _cf.PhosphorousAvailabilityLongTerm;
            decimal phosphorousPtoP2O5Kconversion    = _cf.PhosphorousPtoP2O5Conversion;
            decimal lbPerTonConversion = _cf.PoundPerTonConversion;
            decimal tenThousand        = 10000;

            // get conversion factor for selected units to lb/ac
            Unit    myunit     = _sd.GetUnit(applicationRateUnits);
            decimal conversion = myunit.ConversionlbTon;

            // for solid manures specified in cubic yards per ac, convert application rate to tons/ac
            if (myunit.Id == 6 && farmManure.SolidLiquid.ToUpper() == "SOLID")
            {
                Manure manure = _sd.GetManure(farmManure.ManureId);
                applicationRate = applicationRate * manure.CubicYardConversion;
            }

            // get potassium first year
            nutrientInputs.K2O_FirstYear = Convert.ToInt32(decimal.Multiply(applicationRate, farmManure.Potassium)
                                                           * lbPerTonConversion
                                                           * potassiumKtoK2Oconversion
                                                           * potassiumAvailabilityFirstYear
                                                           * conversion);

            // get potassium long term
            nutrientInputs.K2O_LongTerm = Convert.ToInt32(decimal.Multiply(applicationRate, farmManure.Potassium)
                                                          * lbPerTonConversion
                                                          * potassiumKtoK2Oconversion
                                                          * potassiumAvailabilityLongTerm
                                                          * conversion);

            // get phosphorous first year
            nutrientInputs.P2O5_FirstYear = Convert.ToInt32(decimal.Multiply(applicationRate, farmManure.Phosphorous)
                                                            * lbPerTonConversion
                                                            * phosphorousPtoP2O5Kconversion
                                                            * phosphorousAvailabilityFirstYear
                                                            * conversion);

            // get phosphorous long term
            nutrientInputs.P2O5_LongTerm = Convert.ToInt32(decimal.Multiply(applicationRate, farmManure.Phosphorous)
                                                           * lbPerTonConversion
                                                           * phosphorousPtoP2O5Kconversion
                                                           * phosphorousAvailabilityLongTerm
                                                           * conversion);

            // get N values
            // Organic N% = Total N% - NH4-N ppm / 10,000
            decimal organicN = farmManure.Nitrogen - Convert.ToDecimal(farmManure.Ammonia) / tenThousand;

            NOrganicMineralizations nOrganicMineralizations = new NOrganicMineralizations();

            nOrganicMineralizations = GetNMineralization(farmManure, region.LocationId);
            nOrganicMineralizations.OrganicN_FirstYear = firstYearOrganicNAvailablityPct / 100; // get data from screen

            //decimal ammoniaRetention = GetAmmoniaRetention(mymanure.id, Convert.ToInt32(applicationSeason));
            decimal ammoniaRetention = ammoniaNRetentionPct / 100; // get data from screen

            // N 1st year lb/ton = [NH4-N ppm/10,000 * NH4 retention + NO3-N/10,000 + Organic N %  * 1st yr Mineralization] * 20

            decimal a = decimal.Divide(farmManure.Ammonia, tenThousand) * ammoniaRetention;

            decimal b1 = decimal.Multiply(organicN, nOrganicMineralizations.OrganicN_FirstYear);
            //E07US20
            decimal c1          = a + b1 + Convert.ToDecimal(farmManure.Nitrate) / tenThousand;
            decimal N_Firstyear = decimal.Multiply(c1, lbPerTonConversion);

            nutrientInputs.N_FirstYear = Convert.ToInt32(applicationRate * N_Firstyear * conversion);

            // same for long term
            decimal b2 = decimal.Multiply(organicN, nOrganicMineralizations.OrganicN_LongTerm);
            //E07US20
            decimal c2         = a + b2 + Convert.ToDecimal(farmManure.Nitrate) / tenThousand;
            decimal N_LongTerm = decimal.Multiply(c2, lbPerTonConversion);

            nutrientInputs.N_LongTerm = Convert.ToInt32(applicationRate * N_LongTerm * conversion);

            return(nutrientInputs);
        }