} // end ClearCalculatedTables public void MakePopulationIDs(List <SampleGroupDO> sgList, List <TreeDefaultValueDO> tdvList) { CPbusinessLayer bslyr = new CPbusinessLayer(); bslyr.DAL = DAL; bslyr.fileName = fileName; // Load ID info into tables List <LCDDO> lcdList = new List <LCDDO>(); List <POPDO> popList = new List <POPDO>(); List <PRODO> proList = new List <PRODO>(); List <TreeDO> tList = bslyr.getTrees(); // need to check Contract Species in TDV table before doing unique // reset to a blank if it is null -- September 2016 List <TreeDefaultValueDO> treeDefaults = bslyr.getTreeDefaults(); foreach (TreeDefaultValueDO tdv in treeDefaults) { if (tdv.ContractSpecies == null) { tdv.ContractSpecies = " "; } else if (tdv.ContractSpecies == "") { tdv.ContractSpecies = " "; } } // end foreach bslyr.SaveTreeDefaults(treeDefaults); foreach (SampleGroupDO sgd in sgList) { // Load LCD population IDs // Need unique species, livedead and grade from Tree table // not sure about the count table -- need IDs from there? 11/2012 List <TreeDO> distinctSpecies = bslyr.GetDistinctSpecies((long)sgd.SampleGroup_CN); foreach (TreeDO t in distinctSpecies) { LCDDO lcd = new LCDDO(); lcd.CutLeave = sgd.CutLeave; lcd.Stratum = sgd.Stratum.Code; lcd.SampleGroup = sgd.Code; lcd.PrimaryProduct = sgd.PrimaryProduct; lcd.SecondaryProduct = sgd.SecondaryProduct; lcd.UOM = sgd.UOM; lcd.Species = t.Species; lcd.LiveDead = t.LiveDead; if (t.Grade == null) { lcd.TreeGrade = ""; } else { lcd.TreeGrade = t.Grade; } lcd.STM = t.STM; // per K.Cormier, because a null value in contract species causes // causes problems, it is being dropped from the population ID for LCD // Howeverm it will be stored as null but not used as part of the ID // September 2016 // Found a slicker way to address this -- default contract species to a blank // however, this would be in the TreeDefaultValue table and only affects // new cruises. CP still needs to set CS in TDV to blank (see above). lcd.ContractSpecies = t.TreeDefaultValue.ContractSpecies; //if (t.TreeDefaultValue.ContractSpecies != null) // lcd.ContractSpecies = t.TreeDefaultValue.ContractSpecies; //else lcd.ContractSpecies = ""; if (t.Stratum.YieldComponent != null) { lcd.Yield = t.Stratum.YieldComponent; } else { lcd.Yield = ""; } //} // endif nthRow lcdList.Add(lcd); } // end foreach loop on species // Load POP population IDs POPDO pop = new POPDO(); // Don't need unique species for this group pop.CutLeave = sgd.CutLeave; pop.Stratum = sgd.Stratum.Code; pop.SampleGroup = sgd.Code; pop.PrimaryProduct = sgd.PrimaryProduct; pop.SecondaryProduct = sgd.SecondaryProduct; pop.UOM = sgd.UOM; // check for sure-to-measure trees for this group // Add non-sure-to-measure first and then STM pop.STM = "N"; popList.Add(pop); List <TreeDO> justSTM = tList.FindAll( delegate(TreeDO td) { return(sgd.CutLeave == td.SampleGroup.CutLeave && sgd.Stratum.Code == td.Stratum.Code && sgd.Code == td.SampleGroup.Code && sgd.PrimaryProduct == td.SampleGroup.PrimaryProduct && sgd.SecondaryProduct == td.SampleGroup.SecondaryProduct && sgd.UOM == td.SampleGroup.UOM && td.STM == "Y"); }); if (justSTM.Count > 0) { POPDO popSTM = new POPDO(); popSTM.CutLeave = sgd.CutLeave; popSTM.Stratum = sgd.Stratum.Code; popSTM.SampleGroup = sgd.Code; popSTM.PrimaryProduct = sgd.PrimaryProduct; popSTM.SecondaryProduct = sgd.SecondaryProduct; popSTM.UOM = sgd.UOM; popSTM.STM = "Y"; popList.Add(popSTM); } // endif // Load PRO population IDs // These need cutting unit numbers -- from Cutting Unit List <CuttingUnitStratumDO> strataUnits = bslyr.getCuttingUnitStratum((long)sgd.Stratum_CN); foreach (CuttingUnitStratumDO cudo in strataUnits) { PRODO pro = new PRODO(); pro.CutLeave = sgd.CutLeave; pro.Stratum = sgd.Stratum.Code; pro.CuttingUnit = cudo.CuttingUnit.Code; pro.SampleGroup = sgd.Code; pro.PrimaryProduct = sgd.PrimaryProduct; pro.SecondaryProduct = sgd.SecondaryProduct; pro.UOM = sgd.UOM; // check for sure-to-measure trees for this group // Add non-sure-to-measure first and then STM pro.STM = "N"; proList.Add(pro); justSTM = tList.FindAll( delegate(TreeDO td) { return(sgd.CutLeave == td.SampleGroup.CutLeave && sgd.Stratum.Code == td.Stratum.Code && cudo.CuttingUnit.Code == td.CuttingUnit.Code && sgd.Code == td.SampleGroup.Code && sgd.PrimaryProduct == td.SampleGroup.PrimaryProduct && sgd.SecondaryProduct == td.SampleGroup.SecondaryProduct && sgd.UOM == td.SampleGroup.UOM && td.STM == "Y"); }); if (justSTM.Count > 0) { PRODO proSTM = new PRODO(); proSTM.CutLeave = sgd.CutLeave; proSTM.Stratum = sgd.Stratum.Code; proSTM.CuttingUnit = cudo.CuttingUnit.Code; proSTM.SampleGroup = sgd.Code; proSTM.PrimaryProduct = sgd.PrimaryProduct; proSTM.SecondaryProduct = sgd.SecondaryProduct; proSTM.UOM = sgd.UOM; proSTM.STM = "Y"; proList.Add(proSTM); } // endif } // end foreach loop on strataUnits } // end foreach loop on SampleGroup bslyr.SaveLCD(lcdList); bslyr.SavePOP(popList); bslyr.SavePRO(proList); return; } // end MakePopulationIDs
private void on_GO(object sender, EventArgs e) { processingStatus.Text = "READY TO BEGIN? Click GO."; Cursor.Current = Cursors.WaitCursor; // perform edit checks -- processingStatus.Text = "Edit checking the data. Please wait."; processingStatus.Refresh(); // calls edit check routines /* string outputFileName; * // check for errors from FScruiser before running edit checks * // generate an error report * // June 2013 * List<ErrorLogDO> fscList = bslyr.getErrorMessages("E", "FScruiser"); * if (fscList.Count > 0) * { * ErrorReport eRpt = new ErrorReport(); * eRpt.fileName = fileName; * eRpt.bslyr.fileName = bslyr.fileName; * eRpt.bslyr.DAL = bslyr.DAL; * outputFileName = eRpt.PrintFScruiserErrors(fscList); * string outputMessage = "ERRORS FROM FSCRUISER FOUND!\nCorrect data and rerun\nOutput file is:" + outputFileName; * MessageBox.Show(outputMessage, "ERRORS", MessageBoxButtons.OK, MessageBoxIcon.Stop); * // request made to open error report in preview -- May 2015 * PrintPreview pp = new PrintPreview(); * pp.fileName = outputFileName; * pp.setupDialog(); * pp.ShowDialog(); * Environment.Exit(0); * } // endif report needed * * // clear out error log table for just CruiseProcessing before performing checks * bslyr.DeleteErrorMessages(); * * EditChecks eChecks = new EditChecks(); * eChecks.fileName = fileName; * eChecks.bslyr.fileName = bslyr.fileName; * eChecks.bslyr.DAL = bslyr.DAL; * * int errors = eChecks.TableEditChecks(); * if (errors == -1) * { * // no measured trees detected in the cruise. critical errpor stops the program. * Close(); * return; * } // endif * errors = eChecks.MethodChecks(); * if (errors == -1) * { * // empty stratum detected and user wants to quit * Close(); * return; * } // endif * // just check the ErrorLog table for entries * List<ErrorLogDO> errList = bslyr.getErrorMessages("E", "CruiseProcessing"); * if (errList.Count > 0) * { * ErrorReport er = new ErrorReport(); * er.fileName = fileName; * er.bslyr.fileName = fileName; * er.bslyr.DAL = bslyr.DAL; * outputFileName = er.PrintErrorReport(errList); * string outputMessage = "ERRORS FOUND!\nCorrect data and rerun\nOutput file is:" + outputFileName; * MessageBox.Show(outputMessage, "ERRORS", MessageBoxButtons.OK, MessageBoxIcon.Stop); * // request made to open error report in preview -- May 2015 * PrintPreview pp = new PrintPreview(); * pp.fileName = outputFileName; * pp.setupDialog(); * pp.ShowDialog(); * Environment.Exit(0); * } // endif report needed * moved to EditCheck routine*/ // Show editCheck message -- edit checks complete EditChecks eChecks = new EditChecks(); eChecks.fileName = fileName; eChecks.DAL = DAL; int err = eChecks.CheckErrors(); if (err < 0) { Close(); return; } editCheck.Enabled = true; // next show preparation of data processingStatus.Text = "Preparing data for processing."; processingStatus.Refresh(); // before making IDs, need to check for blank or null secondary products in sample groups // if blank, default to 02 for every region but 6 where it will be 08 instead // put a warning message in the error log table indicating the secondary product was set to a default // June 2013 List <SaleDO> saleList = new List <SaleDO>(); saleList = DAL.From <SaleDO>().Read().ToList(); string currRegion = saleList[0].Region; //string currRegion = bslyr.getRegion(); DefaultSecondaryProduct(currRegion); CalculateTreeValues calcTreeVal = new CalculateTreeValues(); CalculatedValues calcVal = new CalculatedValues(); // retrieve lists needed and sets up population IDs calcVal.fileName = fileName; calcVal.DAL = DAL; // List<SampleGroupDO> sgList = bslyr.getSampleGroups(); // List<TreeDefaultValueDO> tdvList = bslyr.getTreeDefaults(); // List<CountTreeDO> ctList = bslyr.getCountTrees(); // List<PlotDO> pList = bslyr.getPlots(); // calcVal.ClearCalculatedTables(); // calcVal.MakePopulationIDs(sgList, tdvList); calcVal.CalcValues(); CPbusinessLayer bslyr = new CPbusinessLayer(); bslyr.DAL = DAL; // now need some other tables to start summing values List <LCDDO> lcdList = bslyr.getLCD(); List <POPDO> popList = bslyr.getPOP(); List <PRODO> proList = bslyr.getPRO(); List <StratumDO> sList = bslyr.getStratum(); List <SampleGroupDO> sgList = bslyr.getSampleGroups(); List <TreeDefaultValueDO> tdvList = bslyr.getTreeDefaults(); List <CountTreeDO> ctList = bslyr.getCountTrees(); List <PlotDO> pList = bslyr.getPlots(); List <TreeDO> tList = bslyr.getTrees(); calcTreeVal.fileName = fileName; calcTreeVal.bslyr.fileName = fileName; calcTreeVal.bslyr.DAL = bslyr.DAL; // show preparation of data is complete prepareCheck.Enabled = true; // now loop through strata and show status message updating for each stratum StringBuilder sb = new StringBuilder(); foreach (StratumDO sdo in sList) { // update status message for next stratum sb.Clear(); sb.Append("Calculating stratum "); sb.Append(sdo.Code); processingStatus.Text = sb.ToString(); processingStatus.Refresh(); // Sum counts and KPI for LCD table List <PlotDO> justPlots = PlotMethods.GetStrata(pList, sdo.Code); // need cut and leave trees for this List <LCDDO> justCurrentLCD = LCDmethods.GetStratum(lcdList, sdo.Code); calcVal.SumTreeCountsLCD(sdo.Code, ctList, justPlots, justCurrentLCD, sdo.Method, lcdList); // Sum counts and KPI for POP table List <POPDO> justCurrentPOP = POPmethods.GetStratumData(popList, sdo.Code, ""); calcVal.SumTreeCountsPOP(sdo.Code, ctList, justPlots, justCurrentPOP, sdo.Method, popList); // Sum counts and KPI for PRO table List <PRODO> justCurrentPRO = PROmethods.GetCutTrees(proList, sdo.Code, "", "", 0); calcVal.SumTreeCountsPRO(sdo.Code, ctList, justPlots, justCurrentPRO, sdo.Method, proList); // Calculate expansion factor calcVal.CalcExpFac(sdo, justPlots, justCurrentPOP); // Calculate volumes calcTreeVal.ProcessTrees(sdo.Code, sdo.Method, (long)sdo.Stratum_CN); // Update 3P tally if (sdo.Method == "3P") { List <LCDDO> LCDstratum = LCDmethods.GetStratumGroupedBy(fileName, sdo.Code, bslyr); Update3Ptally(fileName, ctList, justCurrentLCD, tList, LCDstratum); // Save bslyr.SaveLCD(justCurrentLCD); } // endif method is 3P // Update expansion factors for methods 3PPNT, F3P, and P3P if (sdo.Method == "3PPNT" || sdo.Method == "F3P" || sdo.Method == "P3P") { List <TreeDO> justCurrentStratum = tList.FindAll( delegate(TreeDO td) { return(td.Stratum.Code == sdo.Code); }); List <TreeCalculatedValuesDO> tcvList = bslyr.getTreeCalculatedValues((int)sdo.Stratum_CN); UpdateExpansionFactors(justCurrentStratum, tcvList); // Save update bslyr.SaveTrees(justCurrentStratum); } // endif on method // Sum data for the LCD, POP and PRO table SumAll Sml = new SumAll(); Sml.fileName = fileName; Sml.bslyr.fileName = bslyr.fileName; Sml.bslyr.DAL = bslyr.DAL; Sml.SumAllValues(sdo.Code, sdo.Method, (int)sdo.Stratum_CN, sList, pList, justCurrentLCD, justCurrentPOP, justCurrentPRO); // Update STR tally after expansion factors are summed if (sdo.Method == "STR") { UpdateSTRtally(fileName, sdo.Code, justCurrentLCD, ctList, lcdList); // save bslyr.SaveLCD(lcdList); } // endif method is STR } // end foreach stratum // show volume calculation is finished volumeCheck.Enabled = true; processingStatus.Text = "Processing is DONE"; processingStatus.Refresh(); System.Threading.Thread.Sleep(5000); Cursor.Current = this.Cursor; Close(); return; } // end on_GO
} // end MakePopulationIDs // NOTE: Converted files - sum of KPI for 3P and S3P is the total of the SumKPI from the Count table // plus the sum of measured KPI from the tree table. No longer need to sum KPI from Count Tree and Tree // April 2013 // methods to sum trees and KPI for individual methods public void SumTreeCountsLCD(string currST, List <CountTreeDO> ctList, List <PlotDO> justPlots, List <LCDDO> justCurrentLCD, string currMethod, List <LCDDO> lcdList) { CPbusinessLayer bslyr = new CPbusinessLayer(); bslyr.DAL = DAL; bslyr.fileName = fileName; string currSG = "*"; string prevSP = "*"; // Sums trees and counts from either the CountTree table or tree count records foreach (LCDDO lcd in justCurrentLCD) { firstSum = 0.0; talliedSum = 0.0; totalMeasuredKPI = 0.0; totalKPI = 0.0; // find measured trees for current LCD group List <TreeDO> lcdTrees = bslyr.getLCDtrees(lcd, "M"); // measured trees is just a count of the trees for current LCD group lcd.MeasuredTrees = lcdTrees.Count(); // now need count trees from tree count records List <TreeDO> lcdCntTrees = bslyr.getLCDtrees(lcd, "C"); // Sum all tree counts for first stage and total tallied // see note in POP section on 100% method if (currMethod == "STR" || currMethod == "100") { firstSum = lcdTrees.Count(); } else if (currMethod != "3P") { firstSum = lcdTrees.Sum(tdo => tdo.TreeCount); firstSum += lcdCntTrees.Sum(tdo => tdo.TreeCount); } if (lcd.STM == "Y") { firstSum += lcd.MeasuredTrees; //talliedSum += lcdTrees.Sum(tdo => tdo.TreeCount); talliedSum += lcd.MeasuredTrees; } else { //if (currMethod == "3P" || currMethod == "100") if (currMethod == "100") { talliedSum += lcd.MeasuredTrees; } else { talliedSum = lcdTrees.Sum(tdo => tdo.TreeCount); talliedSum += lcdCntTrees.Sum(tdo => tdo.TreeCount); } // endif on current method } // endif // insurance trees go into just the tallied tree count List <TreeDO> lcdInsurance = bslyr.getLCDtrees(lcd, "I"); talliedSum += lcdInsurance.Sum(tdo => tdo.TreeCount); // Complete totals based on method -- KPIs too List <CountTreeDO> lcdCountCounts = new List <CountTreeDO>(); switch (currMethod) { case "STR": currSG = "*"; prevSP = "*"; foreach (CountTreeDO ctd in ctList) { if (lcd.Stratum == ctd.SampleGroup.Stratum.Code && lcd.SampleGroup == ctd.SampleGroup.Code) { if (currSG != lcd.SampleGroup && prevSP != lcd.Species && (ctd.TreeDefaultValue_CN == 0 || ctd.TreeDefaultValue_CN == null)) { // multiple species for this sample group so avoid double counting talliedSum += ctd.TreeCount; currSG = lcd.SampleGroup; prevSP = lcd.Species; } else if (currSG == lcd.SampleGroup && prevSP != lcd.Species && (ctd.TreeDefaultValue_CN == 0 || ctd.TreeDefaultValue_CN == null)) { // do not add tree count prevSP = lcd.Species; } else if (currSG != lcd.SampleGroup && prevSP != lcd.Species && (ctd.TreeDefaultValue_CN > 0 || ctd.TreeDefaultValue_CN == null)) { // sample group probably doesn't have multiple species talliedSum += ctd.TreeCount; } else if (currSG == lcd.SampleGroup && prevSP == lcd.Species && (ctd.TreeDefaultValue_CN > 0 || ctd.TreeDefaultValue_CN == null)) { talliedSum += ctd.TreeCount; } // endif } // endif } // end foreach count tree break; case "3P": case "S3P": lcdCountCounts = ctList.FindAll( delegate(CountTreeDO ctd) { return(lcd.CutLeave == ctd.SampleGroup.CutLeave && lcd.Stratum == ctd.SampleGroup.Stratum.Code && lcd.SampleGroup == ctd.SampleGroup.Code && lcd.Species == ctd.TreeDefaultValue.Species && lcd.PrimaryProduct == ctd.SampleGroup.PrimaryProduct && lcd.SecondaryProduct == ctd.SampleGroup.SecondaryProduct && lcd.UOM == ctd.SampleGroup.UOM && lcd.LiveDead == ctd.TreeDefaultValue.LiveDead && lcd.Yield == ctd.SampleGroup.Stratum.YieldComponent && // per K.Cormier, dropping contract species from LCD ID //lcd.ContractSpecies == ctd.TreeDefaultValue.ContractSpecies && lcd.TreeGrade == ctd.TreeDefaultValue.TreeGrade); }); if (lcd.STM == "N") { firstSum += lcd.MeasuredTrees; talliedSum += lcdCountCounts.Sum(ctd => ctd.TreeCount); totalKPI += lcdCountCounts.Sum(ctd => ctd.SumKPI); } // Sum measured KPI from trees totalMeasuredKPI = lcdTrees.Sum(tdo => tdo.KPI); // for S3P and 3P, sum of KPI is total of count and measured KPI from the trees == November 2013 // even though KPI on an individual may be zero for one or the other // however, countTree has sum of count and measured KPI for 3P so adding measured and count // from trees will double count for 3P ONLY -- December 2013 if (currMethod == "S3P") { totalKPI += lcdCntTrees.Sum(lc => lc.KPI); totalKPI += lcdTrees.Sum(lt => lt.KPI); } // endif on method break; case "F3P": case "P3P": // Sum measured KPI from trees totalMeasuredKPI = lcdTrees.Sum(tdo => tdo.KPI); totalKPI = lcdCntTrees.Sum(tdo => tdo.KPI); totalKPI += totalMeasuredKPI; break; case "3PPNT": // this will probably no longer work on legacy data until // the conversion program handles this change if (lcdTrees.Count > 0) { // means there are measured trees so this was a measured plot // KPI in the plot table is measured totalMeasuredKPI = justPlots.Sum(pd => pd.KPI); // plus add to total KPI totalKPI = justPlots.Sum(pd => pd.KPI); } else { // no measured trees means this is a count plot // just add plot KPI to total KPI totalKPI += justPlots.Sum(pd => pd.KPI); } // endif // this piece of code will no longer work -- September 2013 //totalMeasuredKPI = lcdTrees.Sum(tdo => tdo.KPI); //totalKPI += lcdCountCounts.Sum(ctd => ctd.SumKPI); // Add plot KPI to total KPI //totalKPI += justPlots.Sum(pd => pd.KPI); break; } // end switch on method lcd.FirstStageTrees = firstSum; lcd.TalliedTrees = talliedSum; lcd.SumKPI = totalKPI; lcd.SumMeasuredKPI = totalMeasuredKPI; } // end foreach loop // Save list before continuing bslyr.SaveLCD(lcdList); return; } // end SumTreeCountsLCD