public void calc100(StratumStatsDO curStrStats, SampleGroupStatsDO curSgStats, bool useDefault, float treePerAcre, float volumePerAcre)
        {
            double sgCV = 100;
             double sgError = 0;

             calcStats cStat = new calcStats();
             if (!useDefault)
             {
            sgCV = cStat.getCV(treeVolumes, treeVolumes2, treeCount);
            curSgStats.CV_Def = 0;
            curSgStats.CV2_Def = 0;
             }
             else
             {
            curSgStats.CV_Def = 1;
            curSgStats.CV2_Def = 1;
             }
             curSgStats.TreesPerAcre = treePerAcre;
             curSgStats.VolumePerAcre = volumePerAcre;
             curSgStats.TreesPerPlot = 0;
             curSgStats.AverageHeight = 0;
             curSgStats.ReconPlots = plotCount;
             curSgStats.ReconTrees = treeCount;

             //copy calculated stuff
             curSgStats.CV1 = (float)sgCV;
             curSgStats.SampleSize1 = (long)(treePerAcre * totalAcres);
             curSgStats.TPA_Def = (long)(treePerAcre * totalAcres);
             curSgStats.VPA_Def = (long)(volumePerAcre * totalAcres);
             curSgStats.SgError = (float)sgError;

             curSgStats.Save();
        }
Exemplo n.º 2
0
		public void SetValues(SampleGroupStatsDO obj)
		{
			if(obj == null) { return; }
			Code = obj.Code;
			SgSet = obj.SgSet;
			Description = obj.Description;
			CutLeave = obj.CutLeave;
			UOM = obj.UOM;
			PrimaryProduct = obj.PrimaryProduct;
			SecondaryProduct = obj.SecondaryProduct;
			DefaultLiveDead = obj.DefaultLiveDead;
			SgError = obj.SgError;
			SampleSize1 = obj.SampleSize1;
			SampleSize2 = obj.SampleSize2;
			CV1 = obj.CV1;
			CV2 = obj.CV2;
			TreesPerAcre = obj.TreesPerAcre;
			VolumePerAcre = obj.VolumePerAcre;
			TreesPerPlot = obj.TreesPerPlot;
			AverageHeight = obj.AverageHeight;
			SamplingFrequency = obj.SamplingFrequency;
			InsuranceFrequency = obj.InsuranceFrequency;
			KZ = obj.KZ;
			BigBAF = obj.BigBAF;
			BigFIX = obj.BigFIX;
			MinDbh = obj.MinDbh;
			MaxDbh = obj.MaxDbh;
			CV_Def = obj.CV_Def;
			CV2_Def = obj.CV2_Def;
			TPA_Def = obj.TPA_Def;
			VPA_Def = obj.VPA_Def;
			ReconPlots = obj.ReconPlots;
			ReconTrees = obj.ReconTrees;
		}
Exemplo n.º 3
0
		public SampleGroupStatsDO(SampleGroupStatsDO obj) : this()
		{
		    SetValues(obj);
		}
Exemplo n.º 4
0
        private void bindingSourceSgStats_CurrentChanged(object sender, EventArgs e)
        {
            // display the TDV selected items
             if (currentSgStats != null) currentSgStats.Save();

             currentSgStats = bindingSourceSgStats.Current as SampleGroupStatsDO;
             //plotSampleSizeCheck();
        }
        //---------------------------------------------------------------------------------------------------------
        private int getSampleGroupStats(StratumDO currentStratum, StratumStatsDO currentStratumStats, float totalAcres)
        {
            int n1, n2, measTrees, sumKpi, talliedTrees, freq, kz, insTrees;
             double st1x, st1x2, st2x, st2x2, cv1, cv2, sampErr, sumNetVol;
             float treesPerAcre;
             double comberr2 = 0;
             double totalVolume = 0;
             // for each sample group
             sampleGroups = new List<SampleGroupDO>(cdDAL.Read<SampleGroupDO>("SampleGroup", "Where Stratum_CN = ?", currentStratum.Stratum_CN));

             foreach (SampleGroupDO sg in sampleGroups)
             {
            // create samplegroupstats
            currentSgStats = new SampleGroupStatsDO(cdDAL);
            //set foriegn key
            currentSgStats.StratumStats = currentStratumStats;
            currentSgStats.SgSet = 1;
            currentSgStats.Code = sg.Code;
            currentSgStats.CutLeave = sg.CutLeave;
            currentSgStats.UOM = sg.UOM;
            currentSgStats.PrimaryProduct = sg.PrimaryProduct;
            currentSgStats.SecondaryProduct = sg.SecondaryProduct;
            currentSgStats.DefaultLiveDead = sg.DefaultLiveDead;
            currentSgStats.Description = sg.Description;
            // get POP data
            selectedPOP = cdDAL.ReadSingleRow<POPDO>("POP", "WHERE Stratum = ? AND SampleGroup = ?", currentStratum.Code, sg.Code);

            // calculate statistics (based on method)
            if (selectedPOP == null)
            {
               MessageBox.Show("Cruise Not Processed. Please Process Cruise Before Continuing.", "Warning");
               return(-1);
            }
            n1 = Convert.ToInt32(selectedPOP.StageOneSamples);
            n2 = Convert.ToInt32(selectedPOP.StageTwoSamples);
            measTrees = Convert.ToInt32(selectedPOP.MeasuredTrees);
            talliedTrees = Convert.ToInt32(selectedPOP.TalliedTrees);
            sumKpi = Convert.ToInt32(selectedPOP.SumKPI);
            st1x = selectedPOP.Stg1NetXPP;
            st1x2 = selectedPOP.Stg1NetXsqrdPP;
            st2x = selectedPOP.Stg2NetXPP;
            st2x2 = selectedPOP.Stg2NetXsqrdPP;
            // trees per plot
            // find CVs
            cv1 = statClass.getCV(st1x, st1x2, n1);
            if (stage == 12 || stage == 22)
               cv2 = statClass.getCV(st2x, st2x2, n2);
            else
               cv2 = 0;
            // find errors stage 11=tree,single 12=tree,2 stage 21=plot,single 22=plot,2 stage
            if (stage == 11 || stage == 21)
               sampErr = statClass.getSampleError(cv1, n1, 0);
            else if (stage == 12 || stage == 22)
               sampErr = statClass.getTwoStageError(cv1, cv2, n1, n2);
            else
               sampErr = 0;
            currentSgStats.CV1 = Convert.ToSingle(cv1);
            currentSgStats.CV2 = Convert.ToSingle(cv2);
            currentSgStats.SampleSize1 = n1;
            currentSgStats.SampleSize2 = n2;
            if (stage == 11 || stage == 10)
            {
               currentSgStats.ReconTrees = n1;
               currentSgStats.ReconPlots = 0;
            }
            else if (stage == 12)
            {
               currentSgStats.ReconPlots = n1;
               currentSgStats.ReconTrees = n2;
            }
            else if (stage == 21 || stage == 22)
            {
               currentSgStats.ReconTrees = measTrees;
               currentSgStats.ReconPlots = n1;
            }
            // calculate frequency

            currentSgStats.SgError = Convert.ToSingle(sampErr);

            // get LCD data
            selectedLCD = cdDAL.Read<LCDDO>("LCD", "WHERE Stratum = ? AND SampleGroup = ?", currentStratum.Code, sg.Code);
            sumExpFac = 0;
            sumNetVol = 0;
            //foreach (SampleGroupDO sg in Owner.histSampleGroup)
            foreach (LCDDO lcd in selectedLCD)
            {
               // sum volume
               double expFac = lcd.SumExpanFactor;
               sumExpFac += expFac;
               // sum trees
               double netVol = lcd.SumNCUFT;
               sumNetVol += netVol;
            }
            comberr2 += (sampErr * sumNetVol) * (sampErr * sumNetVol);
            totalVolume += sumNetVol;
            // find volume/acre and trees/acre
            if (stage < 20)
            {
               treesPerAcre = Convert.ToSingle(Math.Round((sumExpFac / totalAcres), 2));
               currentSgStats.TreesPerAcre = treesPerAcre;
               currentSgStats.VolumePerAcre = Convert.ToSingle(Math.Round((sumNetVol / totalAcres), 2));
            }
            else
            {
               treesPerAcre = Convert.ToSingle(Math.Round((sumExpFac), 2));
               currentSgStats.TreesPerAcre = treesPerAcre;
               currentSgStats.VolumePerAcre = Convert.ToSingle(Math.Round((sumNetVol), 2));
               if (stage == 21)
                  currentSgStats.TreesPerPlot = Convert.ToSingle(Math.Round((Convert.ToSingle((float)measTrees / (float)n1)), 1));
               else
                  currentSgStats.TreesPerPlot = Convert.ToSingle(Math.Round((Convert.ToSingle((float)talliedTrees / (float)n1)), 1));
            }
            currentSgStats.TPA_Def = (long)(treesPerAcre * totalAcres);
            // find frequency/KZ/BigBAF values
            if ((stage == 11 || stage == 12 || stage == 22) && measTrees > 0)
            {
               freq = Convert.ToInt32((talliedTrees / measTrees));
               kz = Convert.ToInt32((sumKpi / measTrees));

               if (currentStratum.Method == "S3P")
               {
                  currentSgStats.SamplingFrequency = Convert.ToInt32((talliedTrees / n1));
                  currentSgStats.KZ = kz;
               }
               else
               {
                  currentSgStats.SamplingFrequency = freq;
                  currentSgStats.KZ = kz;
               }
            }
            // find insurance trees
            if (stage == 11 || stage == 12)
               insTrees = getInsuranceTrees(sg);
            else
               insTrees = 0;
            currentSgStats.InsuranceFrequency = insTrees;
            // save samplegroupstats row
            currentSgStats.Save();
            // loop through TDV information
            sg.TreeDefaultValues.Populate();
            foreach (TreeDefaultValueDO tdv in sg.TreeDefaultValues)
            {
                currentSgStats.TreeDefaultValueStats.Add(tdv);
            }

            currentSgStats.Save();
            currentSgStats.TreeDefaultValueStats.Save();

             }
             return (0);
        }
        public void GoToStrataPage(int pageID)
        {
            // If from UnitPage - get new currentStratumStats
               mySale.DefaultUOM = sUOM;

               //currentStratumStats.Save();
               //setUsed(currentStratumStats.Stratum_CN);

               myTreeDefaultList = new List<TreeDefaultValueDO>();
               cdTreeDefaults.Clear();

               // save the current stratum
               currentStratumStats.Save();
               setUsed(currentStratumStats.Stratum_CN);

               cdSgStats = new BindingList<SampleGroupStatsDO>(cdDAL.Read<SampleGroupStatsDO>("SampleGroupStats", "Where StratumStats_CN = ? AND SgSet = ?", currentStratumStats.StratumStats_CN, currentStratumStats.SgSet));
               // setup Tree Default Values by CuttingUnits
               fillTreeDefaultList();
               //getTreeDefaultSql();

               strataPage.bindingSourceSgStats.DataSource = cdSgStats;

               strataPage.bindingSourceTDV.DataSource = cdTreeDefaults;

               strataPage.textBoxSGset.Text = currentStratumStats.SgSet.ToString();
               strataPage.textBoxSGsetDescr.Text = currentStratumStats.SgSetDescription;
               strataPage.textBoxStratum.Text = currentStratumStats.Code;
               // check for no sample groups
               if (cdSgStats.Count <= 0)
               {
              // add a blank sample group
              currentSgStats = new SampleGroupStatsDO(cdDAL);

              currentSgStats.SgSet = 1; ;
              currentSgStats.Code = " ";
              currentSgStats.PrimaryProduct = "01";
              currentSgStats.SecondaryProduct = "02";
              currentSgStats.DefaultLiveDead = "L";
              currentSgStats.CutLeave = "C";
              currentSgStats.UOM = sUOM;

              currentSgStats.StratumStats = currentStratumStats;

              cdSgStats.Add(currentSgStats);
              strataPage.product = "01";
              strataPage.setTreeDefaultValue("01");
               }
               else
               {
               // find product for first sample group
              strataPage.product = cdSgStats[0].PrimaryProduct;
              strataPage.setTreeDefaultValue(strataPage.product);

               }

               pageHost1.Display(strataPage);
        }
        public int getReconData(StratumStatsDO curStrStats, SampleGroupStatsDO curSgStats, DAL rDAL, DAL fsDAL, List<PlotDO> myPlots, List<TreeDO> myTree, List<LogDO> myLogs, long? sampleGroupCN, bool first, int measHit)
        {
            TreeDO fsTree;
             LogDO fsLog;
             long? plotCN = 0;
             var myTreeList = myTree;
             var myLogList = myLogs;
             int treeCnt = 1;

             curStrStats.Stratum.CuttingUnits.Populate();
             curSgStats.TreeDefaultValueStats.Populate();
             // loop through design units
             foreach (CuttingUnitDO curUnit in curStrStats.Stratum.CuttingUnits)
             {
            // get number of plots for stratum and cutting unit
            var myPlotList = (from plt in myPlots
                              where plt.CuttingUnit_CN == curUnit.CuttingUnit_CN
                              select plt).ToList();
            // loop through plots
            foreach (PlotDO curPlot in myPlotList)
            {
               // if first time for stratum, save plots
               plotCN = savePlots(curPlot, curStrStats.Stratum_CN, first);

               foreach (TreeDefaultValueDO curTdv in curSgStats.TreeDefaultValueStats)
               {
                  if (curSgStats.MinDbh > 0 && curSgStats.MaxDbh > 0)
                  {
                     double maxDbh = curSgStats.MaxDbh + 0.0499;
                     double minDbh = curSgStats.MinDbh - 0.0500;
                     myTreeList = (from tcv in myTree
                                       where tcv.SampleGroup.PrimaryProduct == curSgStats.PrimaryProduct
                                       && tcv.Plot_CN == curPlot.Plot_CN
                                       && tcv.Species == curTdv.Species
                                       && tcv.LiveDead == curTdv.LiveDead
                                       && tcv.DBH >= minDbh
                                       && tcv.DBH <= maxDbh
                                       select tcv).ToList();
                  }
                  else
                  {
                     myTreeList = (from tcv in myTree
                                       where tcv.SampleGroup.PrimaryProduct == curSgStats.PrimaryProduct
                                       && tcv.Plot_CN == curPlot.Plot_CN
                                       && tcv.Species == curTdv.Species
                                       && tcv.LiveDead == curTdv.LiveDead
                                       select tcv).ToList();
                  }

                  foreach (TreeDO rTree in myTreeList)
                  {
                     fsTree = new TreeDO(fsDAL);
                     fsTree.Stratum_CN = thisStrCN;
                     fsTree.TreeDefaultValue_CN = curTdv.TreeDefaultValue_CN;
                     fsTree.SampleGroup_CN = sampleGroupCN;
                     fsTree.Plot_CN = plotCN;
                     fsTree.CuttingUnit_CN = curUnit.CuttingUnit_CN;
                     fsTree.TreeNumber = rTree.TreeNumber;
                     fsTree.Species = rTree.Species;
                     if (curStrStats.Method == "PCM" || curStrStats.Method == "FCM")
                     {
                        if (radioButton1.Checked)
                        {
                           // use frequency
                           // check hit
                           if (treeCnt == measHit)
                           {
                              fsTree.CountOrMeasure = "M";
                              measHit += (int)curSgStats.SamplingFrequency;
                           }
                           else
                              fsTree.CountOrMeasure = "C";

                        }
                        else if (radioButton2.Checked)
                        {
                           fsTree.CountOrMeasure = "C";
                        }
                        else
                           fsTree.CountOrMeasure = "M";
                     }
                     else
                        fsTree.CountOrMeasure = rTree.CountOrMeasure;
                     treeCnt++;
                     fsTree.TreeCount = 1;
                     fsTree.SeenDefectPrimary = rTree.SeenDefectPrimary;
                     fsTree.SeenDefectSecondary = rTree.SeenDefectSecondary;
                     fsTree.RecoverablePrimary = rTree.RecoverablePrimary;
                     fsTree.Initials = rTree.Initials;
                     fsTree.LiveDead = rTree.LiveDead;
                     fsTree.Grade = rTree.Grade;
                     fsTree.HeightToFirstLiveLimb = rTree.HeightToFirstLiveLimb;
                     fsTree.DBH = rTree.DBH;
                     fsTree.DRC = rTree.DRC;
                     fsTree.TotalHeight = rTree.TotalHeight;
                     fsTree.MerchHeightPrimary = rTree.MerchHeightPrimary;
                     fsTree.MerchHeightSecondary = rTree.MerchHeightSecondary;
                     fsTree.FormClass = rTree.FormClass;
                     fsTree.UpperStemHeight = rTree.UpperStemHeight;
                     fsTree.TopDIBPrimary = rTree.TopDIBPrimary;
                     fsTree.TopDIBSecondary = rTree.TopDIBSecondary;
                     //fsTree.CreatedBy = rTree.CreatedBy;
                     //fsTree.CreatedDate = rTree.CreatedDate;

                     fsTree.Save();
                     // save logs
                     myLogList = (from lcv in myLogs
                                  where lcv.Tree_CN == rTree.Tree_CN
                                  select lcv).ToList();

                     foreach (LogDO rLog in myLogList)
                     {
                        fsLog = new LogDO(fsDAL);
                        fsLog.Tree_CN = rLog.Tree_CN;
                        fsLog.LogNumber = rLog.LogNumber;
                        fsLog.Grade = rLog.Grade;
                        fsLog.SeenDefect = rLog.SeenDefect;
                        fsLog.PercentRecoverable = rLog.PercentRecoverable;
                        fsLog.Length = rLog.Length;
                        fsLog.ExportGrade = rLog.ExportGrade;
                        fsLog.SmallEndDiameter = rLog.SmallEndDiameter;
                        fsLog.LargeEndDiameter = rLog.LargeEndDiameter;
                        fsLog.GrossBoardFoot = rLog.GrossBoardFoot;
                        fsLog.NetBoardFoot = rLog.NetBoardFoot;
                        fsLog.GrossCubicFoot = rLog.GrossCubicFoot;
                        fsLog.NetCubicFoot = rLog.NetCubicFoot;
                        fsLog.DIBClass = rLog.DIBClass;
                        fsLog.BarkThickness = rLog.BarkThickness;
                        fsLog.Save();
                    }
                  }
               }
            }
             }
             return (0);
        }
        public long? getReconStratum(SampleGroupStatsDO curSgStats, long? UnitCN, string method)
        {
            // find strata assigned with Unit Code where Method = FIX

             List<StratumDO> rStratum = new List<StratumDO>(rDAL.Read<StratumDO>("Stratum", "WHERE CuttingUnit_CN = ? AND Stratum.Method = ?", UnitCN, method));
             //List<StratumDO> rStratum = new List<StratumDO>(rDAL.Read<StratumDO>("Stratum", "JOIN CuttingUnitStratum JOIN CuttingUnit WHERE Stratum.Stratum_CN = CuttingUnitStratum.Stratum_CN AND CuttingUnitStratum.CuttingUnit_CN = CuttingUnit.CuttingUnit_CN AND CuttingUnit.Code = ? AND Stratum.Method = ?", curUnit.Code, method));
             // find stratum with correct method (If multiple, ask for correct to use)
             if (rStratum.Count() > 0)
             {
            if (rStratum.Count() > 1)
            {
               // check sample group for curSgStats product code
               List<StratumDO> chkStratum = new List<StratumDO>(rDAL.Read<StratumDO>("Stratum", "Join SampleGroup WHERE Stratum.Stratum_CN = SampleGroup.Stratum_CN AND Stratum.CuttingUnit_CN = ? AND Stratum.Method = ? AND SampleGroup.PrimaryProduct = ?", UnitCN, method, curSgStats.PrimaryProduct));

               //List<StratumDO> chkStratum = new List<StratumDO>(rDAL.Read<StratumDO>("Stratum", "JOIN CuttingUnitStratum JOIN CuttingUnit JOIN SampleGroup WHERE Stratum.Stratum_CN = CuttingUnitStratum.Stratum_CN AND CuttingUnitStratum.CuttingUnit_CN = CuttingUnit.CuttingUnit_CN AND Stratum.Stratum_CN = SampleGroup.Stratum_CN AND CuttingUnit.Code = ? AND Stratum.Method = ? AND SampleGroup.PrimaryProduct = ?", curUnit.Code, method, curSgStats.PrimaryProduct));
               // if only one stratum, return as a single row
               if (chkStratum.Count() == 1)
               {
                  if (method == "FIX")
                     curSgPlotSize = chkStratum[0].FixedPlotSize;
                  else if (method == "PNT")
                     curSgPlotSize = chkStratum[0].BasalAreaFactor;
                  return (chkStratum[0].Stratum_CN);
               }
               else
               {
                  if (method == "FIX")
                     curSgPlotSize = 0;
                  else if (method == "PNT")
                     curSgPlotSize = 0;

                  // if two or more strata, check species codes (?)
                  // user selects the stratum to use
                  // save the stratum_cn to use for rest of design stratum
               }
            }
            else
            {
               // return rStratum as single row
               if (method == "FIX")
                  curSgPlotSize = rStratum[0].FixedPlotSize;
               else if (method == "PNT")
                  curSgPlotSize = rStratum[0].BasalAreaFactor;
               return (rStratum[0].Stratum_CN);
            }
             }
             return (null);
        }
        //********************                                                                      getSgReconData
        public int getSgReconData(StratumStatsDO curStrStats, SampleGroupStatsDO curSgStats, string method)
        {
            CuttingUnitDO rUnit;
             SampleGroupDO rSampleGroup;
             long? myStratumCN = 0;
             //long?[] stratumArray = new long?[10];

             //get search variables
             if (!reconExists)
            return (1);
             double vBar;
             int unitCnt = 0;
             treeHeights = 0;
             plotCount = 0;
             plotVolume = 0;
             plotVolume2 = 0;
             treePlot = 0;
             treePlot2 = 0;
             vExpFac = 0;
             sumExpFac = 0;
             treeCount = 0;
             treeVolumes = 0;
             treeVolumes2 = 0;
             vBarSum = 0;
             vBarSum2 = 0;
             vBarPlot2 = 0;
             //         currentTreeList.Clear();
             curSgStats.TreeDefaultValueStats.Populate();
             // loop through design units
             foreach (CuttingUnitDO curUnit in curStrStats.Stratum.CuttingUnits)
             {
            rUnit = rDAL.ReadSingleRow<CuttingUnitDO>("CuttingUnit", "Where Code = ?", curUnit.Code);
            if (rUnit != null)
            {
               unitCnt++;
               rUnit.Strata.Populate();
               int cnt = 0;
               foreach (StratumDO myStratum in rUnit.Strata)
               {
                  if (myStratum.Method == method)
                  {
                     // check sample group product code
                     rSampleGroup = rDAL.ReadSingleRow<SampleGroupDO>("SampleGroup", "Where Stratum_CN = ? AND PrimaryProduct = ?", myStratum.Stratum_CN, curSgStats.PrimaryProduct);
                     if (rSampleGroup != null)
                     {
                        myStratumCN = myStratum.Stratum_CN;
                        if (method == "FIX")
                           curSgPlotSize = myStratum.FixedPlotSize;
                        else if (method == "PNT")
                           curSgPlotSize = myStratum.BasalAreaFactor;
                        cnt++;
                     }
                  }
               }
               if (cnt == 0)
               {
                  return (1);
               }
               else if (cnt > 1)
               {
                  // loop through stratumArray

                  // popup to select correct stratum
               }
               //            else
               //              myStratumCN = stratumArray[0];

               // get number of plots for stratum and cutting unit
               var myPlotList = (from plt in myPlots
                                 where plt.CuttingUnit_CN == rUnit.CuttingUnit_CN
                                 && plt.Stratum_CN == myStratumCN
                                 select plt).ToList();
               //List<PlotDO> myPlots = new List<PlotDO>(rDAL.Read<PlotDO>("Plot", "WHERE Plot.CuttingUnit_CN = ? AND Plot.Stratum_CN = ?", rUnit.CuttingUnit_CN, myStratumCN));
               plotCount += myPlotList.Count();
               // loop through plots
               foreach (PlotDO curPlot in myPlotList)
               {
                  pltVol = 0;
                  vBarPlot = 0;
                  int plotTreeCount = 0;
                  vExpFac = 0;

                  foreach (TreeDefaultValueDO curTdv in curSgStats.TreeDefaultValueStats)
                  {
                     double maxDbh = 200;
                     double minDbh = 0;
                     if (curSgStats.MinDbh > 0 && curSgStats.MaxDbh > 0)
                     {
                        maxDbh = curSgStats.MaxDbh + 0.0499;
                        minDbh = curSgStats.MinDbh - 0.0500;
                     }
                        var myTreeList = (from tcv in rTreeCalcJoin
                                      where tcv.Tree.SampleGroup.PrimaryProduct == curSgStats.PrimaryProduct
                                      && tcv.Tree.Plot_CN == curPlot.Plot_CN
                                      && tcv.Tree.Species == curTdv.Species
                                      && tcv.Tree.LiveDead == curTdv.LiveDead
                                      && tcv.Tree.DBH >= minDbh
                                      && tcv.Tree.DBH <= maxDbh
                                      select tcv).ToList();

                     // load Unit, Plot, Tree, DBH, THT, Volume into list
                     //check MinDbh and MaxDbh
                     foreach (TreeCalculatedValuesDO myTree in myTreeList)
                     {
                        plotTreeCount++;
                        treeHeights += myTree.Tree.TotalHeight;

                        if (myTree.Tree.DBH > 0 && method == "PNT")
                        {
                           if (curSgStats.UOM == "01")
                              vBar = myTree.NetBDFTPP / (0.005454 * myTree.Tree.DBH * myTree.Tree.DBH);
                           else
                              vBar = myTree.NetCUFTPP / (0.005454 * myTree.Tree.DBH * myTree.Tree.DBH);

                           vBarSum += vBar;
                           vBarSum2 += (vBar * vBar);
                           vBarPlot += vBar;

                           vExpFac += curSgPlotSize / (0.005454 * myTree.Tree.DBH * myTree.Tree.DBH);
                        }

                        if (curSgStats.UOM == "01")
                        {
                           treeVolumes += myTree.NetBDFTPP;
                           treeVolumes2 += myTree.NetBDFTPP * myTree.NetBDFTPP;
                           pltVol += myTree.NetBDFTPP;
                        }
                        else
                        {
                           treeVolumes += myTree.NetCUFTPP;
                           treeVolumes2 += myTree.NetCUFTPP * myTree.NetCUFTPP;
                           pltVol += myTree.NetCUFTPP;
                        }
                     }
                  }
                  // save plot level information
                  treeCount += plotTreeCount;
                  treePlot += plotTreeCount;
                  treePlot2 += plotTreeCount * plotTreeCount;
                  plotVolume += pltVol;
                  plotVolume2 += pltVol * pltVol;
                  vBarPlot2 += vBarPlot * vBarPlot;
                  sumExpFac += vExpFac;

               }

               // !!!check for different fixed plot sizes across units!!!
               if (sgPlotSize == 0) sgPlotSize = curSgPlotSize;
               if (sgPlotSize != curSgPlotSize)
               {
                  // cannot calculate statistics
                  if (method == "FIX")
                  {
                     useDefaultFix = true;
                     useDefaultTree = true;
                  }
                  else if (method == "PNT")
                  {
                     useDefaultPnt = true;
                  }
               }
            }
             }
             if(unitCnt == 0) return (1);

             return (0);
        }
 public void getPntSgStats(StratumStatsDO curStrStats, SampleGroupStatsDO curSgStats, bool useDefault)
 {
     //sgStats = new SampleGroupDO(cdDAL);
      float treePerAcre = 1;
      float volumePerAcre = 1;
      float treePerPlot = 1;
      if (!useDefault)
      {
     // calculate TPA
     treePerAcre = (float)(sumExpFac / plotCount);
     // calculate VPA
     pntFac = curSgPlotSize / plotCount;
     volumePerAcre = (float) (vBarSum * pntFac);
     // calculate TPP
     treePerPlot = (float)((float)treeCount / (float)plotCount);
     if (treePerPlot < .1 && treePerPlot > 0) treePerPlot = (float)(0.1);
      }
      // calculate statistics
      calculateStats(curStrStats, curSgStats, useDefault, treePerAcre, volumePerAcre, treePerPlot, "PNT");
      calculateStats(curStrStats, curSgStats, useDefault, treePerAcre, volumePerAcre, treePerPlot, "P3P");
      calculateStats(curStrStats, curSgStats, useDefault, treePerAcre, volumePerAcre, treePerPlot, "PCM");
      calculateStats(curStrStats, curSgStats, useDefault, treePerAcre, volumePerAcre, treePerPlot, "3PPNT");
 }
 //*********                                                                           setupPopulations
 public void getFixSgStats(StratumStatsDO curStrStats, SampleGroupStatsDO curSgStats, bool useDefault)
 {
     //sgStats = new SampleGroupDO(cdDAL);
      float treePerAcre = 1;
      float volumePerAcre = 1;
      float treePerPlot = 1;
      if (!useDefault)
      {
     // calculate Expansion Factor
     //expFac = curSgPlotSize / plotCount;
     // calculate TPA
       //          float xtreePerAcre = treeCount * expFac;
     pntFac = curSgPlotSize / plotCount;
     treePerAcre = (float) (pntFac * treeCount);
     // calculate VPA
     //            float xvolumePerAcre = plotVolume * expFac;
     volumePerAcre = (float)(plotVolume * pntFac);
     // calculate TPP
     //treePerPlot = (float) (treeCount / plotCount);
     treePerPlot = (float)((float)treeCount / (float)plotCount);
     if (treePerPlot < .1 && treePerPlot > 0) treePerPlot = (float)(0.1);
      }
      // calculate statistics
      calc100(curStrStats, curSgStats, useDefault, treePerAcre, volumePerAcre);
      calculateStats(curStrStats, curSgStats, useDefault, treePerAcre, volumePerAcre, treePerPlot, "FIX");
      calculateStats(curStrStats, curSgStats, useDefault, treePerAcre, volumePerAcre, treePerPlot, "F3P");
      calculateStats(curStrStats, curSgStats, useDefault, treePerAcre, volumePerAcre, treePerPlot, "FCM");
      calculateStats(curStrStats, curSgStats, useDefault, treePerAcre, volumePerAcre, treePerPlot, "STR");
      calculateStats(curStrStats, curSgStats, useDefault, treePerAcre, volumePerAcre, treePerPlot, "3P");
      calculateStats(curStrStats, curSgStats, useDefault, treePerAcre, volumePerAcre, treePerPlot, "S3P");
 }
        public void calculateStats(StratumStatsDO curStrStats, SampleGroupStatsDO curSgStats, bool useDefault, float treePerAcre, float volumePerAcre, float treePerPlot, String method)
        {
            double sgCV = 100;
             double sgCV2 = 0;
             double sgError = 40;
             int n = 0;
             int n2 = 0;
             int KZ = 0;
             int Freq = 0;

             calcStats cStat = new calcStats();
             if (!useDefault)
             {
            if (method == "FIX")
            {
               sgCV = cStat.getPntCV(plotVolume, plotVolume2, pntFac, plotCount);
               sgError = cStat.getSampleError(sgCV, plotCount, 0);
               n = plotCount;
               n2 = treePlot;
            }
            else if (method == "PNT")
            {
               sgCV = cStat.getPntCV(vBarSum, vBarPlot2, pntFac, plotCount);
               sgError = cStat.getSampleError(sgCV, plotCount, 0);
               n = plotCount;
               n2 = treePlot;
            }
            else if (method == "F3P")
            {
               sgCV = cStat.getPntCV(plotVolume, plotVolume2, pntFac, plotCount);
               sgCV2 = 30;
               sgError = cStat.getTwoStageError(sgCV, sgCV2, plotCount, treePlot);
               n = plotCount;
               n2 = treePlot;
               KZ = 1;
            }
            else if (method == "P3P" || method == "3PPNT")
            {
               sgCV = cStat.getPntCV(vBarSum, vBarPlot2, pntFac, plotCount);
               sgCV2 = 30;
               sgError = cStat.getTwoStageError(sgCV, sgCV2, plotCount, treePlot);
               n = plotCount;
               n2 = treePlot;
               if (method == "3PPNT") n2 = plotCount;
               KZ = 1;
            }
            else if (method == "FCM")
            {
               sgCV = cStat.getCV(treePlot, treePlot2, plotCount);
               sgCV2 = cStat.getCV(treeVolumes, treeVolumes2, treePlot);
               sgError = cStat.getTwoStageError(sgCV, sgCV2, plotCount, treePlot);
               n = plotCount;
               n2 = treePlot;
               Freq = 1;
            }
            else if (method == "PCM")
            {
               sgCV = cStat.getCV(treePlot, treePlot2, plotCount);
               sgCV2 = cStat.getCV(vBarSum, vBarSum2, treePlot);
               sgError = cStat.getTwoStageError(sgCV, sgCV2, plotCount, treePlot);
               n = plotCount;
               n2 = treePlot;
               Freq = 1;
            }
            else if (method == "STR")
            {
               sgCV = cStat.getCV(treeVolumes, treeVolumes2, treeCount);
               sgError = cStat.getSampleError(sgCV, treeCount, 0);
               n = treePlot;
               if (treePlot > 0)
                  Freq = (int)Math.Floor((treePerAcre * totalAcres) / treeCount);
               else
                  Freq = 0;
            }
            else if (method == "3P")
            {
               sgCV = 35;
               sgError = cStat.getSampleError(sgCV, treeCount, 0);
               n = treePlot;
               if (treeVolumes > 0)
                  KZ = (int)Math.Floor((volumePerAcre * totalAcres) / treeVolumes);
               else
                  KZ = 0;
            }
            else if (method == "S3P")
            {
               sgCV = cStat.getCV(treeVolumes, treeVolumes2, treeCount);
               sgCV2 = 35;
               sgError = cStat.getTwoStageError(sgCV, sgCV2, treeCount, treeCount);
               if (treeVolumes > 0)
                  KZ = (int)Math.Floor((volumePerAcre * totalAcres) / treeVolumes);
               else
                  KZ = 0;
               Freq = 1;
            }
             }
             else
             {
            if (method == "FCM" || method == "PCM")
            {
               sgCV2 = 100;
               cStat.getTwoStageSampleSize(sgCV, sgCV2, sgError);
               n = (int)cStat.sampleSize1;
               n2 = (int)cStat.sampleSize2;
            }
            else if (method == "F3P" || method == "P3P" || method == "S3P" || method == "3PPNT")
            {
               sgCV2 = 35;
               cStat.getTwoStageSampleSize(sgCV, sgCV2, sgError);
               n = (int)cStat.sampleSize1;
               n2 = (int)cStat.sampleSize2;
            }
            else
            {
               n = cStat.getSampleSize(sgError, sgCV);
            }
             }

             // Find the strataStats_CN
             long? strStatsCN = GetStratumStatsCN(curStrStats, method);
             // Create SampleGroupStats record with StratumStats CN and save stats
             SampleGroupStatsDO newSgStats = new SampleGroupStatsDO(cdDAL);
             //copy standard info
             newSgStats.StratumStats_CN = strStatsCN;
             newSgStats.Code = curSgStats.Code;
             newSgStats.Description = curSgStats.Description;
             newSgStats.SgSet = curSgStats.SgSet;
             newSgStats.CutLeave = curSgStats.CutLeave;
             newSgStats.UOM = curSgStats.UOM;
             newSgStats.PrimaryProduct = curSgStats.PrimaryProduct;
             newSgStats.SecondaryProduct = curSgStats.SecondaryProduct;
             newSgStats.DefaultLiveDead = curSgStats.DefaultLiveDead;
             newSgStats.MinDbh = curSgStats.MinDbh;
             newSgStats.MaxDbh = curSgStats.MaxDbh;
             // general stuff
             newSgStats.TreesPerAcre = treePerAcre;
             newSgStats.VolumePerAcre = volumePerAcre;
             newSgStats.TreesPerPlot = treePerPlot;
             newSgStats.TPA_Def = (long)(treePerAcre * totalAcres);
             newSgStats.VPA_Def = (long)(volumePerAcre * totalAcres);
             if (treeCount > 0)
            newSgStats.AverageHeight = (float)(treeHeights / treeCount);
             else
            newSgStats.AverageHeight = 0;
             newSgStats.KZ = KZ;
             newSgStats.BigBAF = 0;
             newSgStats.BigFIX = 0;
             newSgStats.SamplingFrequency = Freq;
             newSgStats.InsuranceFrequency = 0;
             //copy calculated stuff
             newSgStats.CV1 = (float)sgCV;
             newSgStats.CV2 = (float)sgCV2;
             newSgStats.SampleSize1 = n;
             newSgStats.SampleSize2 = n2;
             newSgStats.SgError = (float)sgError;
             if (useDefault)
             {
            newSgStats.CV_Def = 1;
            newSgStats.CV2_Def = 1;
             }
             else
             {
            newSgStats.CV_Def = 0;
            newSgStats.CV2_Def = 0;
             }
             newSgStats.ReconPlots = plotCount;
             newSgStats.ReconTrees = treeCount;

             //selectSgStats.Add(newSgStats);
             newSgStats.Save();
             //long? sgcn = newSgStats.SampleGroupStats_CN;
             curSgStats.TreeDefaultValueStats.Populate();
             foreach (TreeDefaultValueDO myTDV in curSgStats.TreeDefaultValueStats)
             {
            SampleGroupStatsTreeDefaultValueDO mySgTDV = new SampleGroupStatsTreeDefaultValueDO(cdDAL);
            mySgTDV.TreeDefaultValue_CN = myTDV.TreeDefaultValue_CN;
            mySgTDV.SampleGroupStats = newSgStats;
            mySgTDV.Save();
             }
        }
        //*******************************
        private void getSgErr(int stage, SampleGroupStatsDO _currentSgStats)
        {
            double sgCV = _currentSgStats.CV1;
             long n = _currentSgStats.SampleSize1;
             calcStats cStat = new calcStats();
             // check for plot cruise, if yes, all SampleSize1 is changed and errors recomputed
             if (stage == 11)
             {
            float tpa = _currentSgStats.TreesPerAcre;
            long N = (long)Math.Ceiling(tpa * totAcres);

            _currentSgStats.SgError = Convert.ToSingle(Math.Round(cStat.getSampleError(sgCV, n, 0, N), 2));
             }
             else if (stage == 21)
             {
            _currentSgStats.SgError = Convert.ToSingle(Math.Round(cStat.getSampleError(sgCV, n, 0), 2));
             }
             else if (stage == 12 || stage == 22)
             {
            _currentSgStats.SgError = Convert.ToSingle(Math.Round(cStat.getTwoStageError(sgCV, currentSgStats.CV2, n, currentSgStats.SampleSize2), 2));
             }
        }
        public int getReconData(StratumStatsDO curStrStats, SampleGroupStatsDO curSgStats, DAL rDAL, DAL fsDAL, List<PlotDO> myPlots, List<TreeDO> myTree, long? sampleGroupCN, bool first)
        {
            List<LogDO> rLog = new List<LogDO>();
             TreeDO fsTree;
             LogDO fsLog;
             long? plotCN = 0;
             var myTreeList = myTree;

             curStrStats.Stratum.CuttingUnits.Populate();
             curSgStats.TreeDefaultValueStats.Populate();
             // loop through design units
             foreach (CuttingUnitDO curUnit in curStrStats.Stratum.CuttingUnits)
             {
            // get number of plots for stratum and cutting unit
            var myPlotList = (from plt in myPlots
                              where plt.CuttingUnit_CN == curUnit.CuttingUnit_CN
                              select plt).ToList();
            // loop through plots
            foreach (PlotDO curPlot in myPlotList)
            {
               // if first time for stratum, save plots
               plotCN = savePlots(curPlot, curStrStats.Stratum_CN, first);

               foreach (TreeDefaultValueDO curTdv in curSgStats.TreeDefaultValueStats)
               {
                  if (curSgStats.MinDbh > 0 && curSgStats.MaxDbh > 0)
                  {
                     double maxDbh = curSgStats.MaxDbh + 0.0499;
                     double minDbh = curSgStats.MinDbh - 0.0500;
                     myTreeList = (from tcv in myTree
                                       where tcv.SampleGroup.PrimaryProduct == curSgStats.PrimaryProduct
                                       && tcv.Plot_CN == curPlot.Plot_CN
                                       && tcv.Species == curTdv.Species
                                       && tcv.LiveDead == curTdv.LiveDead
                                       && tcv.DBH >= minDbh
                                       && tcv.DBH <= maxDbh
                                       select tcv).ToList();
                  }
                  else
                  {
                     myTreeList = (from tcv in myTree
                                       where tcv.SampleGroup.PrimaryProduct == curSgStats.PrimaryProduct
                                       && tcv.Plot_CN == curPlot.Plot_CN
                                       && tcv.Species == curTdv.Species
                                       && tcv.LiveDead == curTdv.LiveDead
                                       select tcv).ToList();
                  }

                  foreach (TreeDO rTree in myTreeList)
                  {
                     fsTree = new TreeDO(fsDAL);
                     fsTree.Stratum_CN = curStrStats.Stratum_CN;
                     fsTree.TreeDefaultValue_CN = curTdv.TreeDefaultValue_CN;
                     fsTree.SampleGroup_CN = sampleGroupCN;
                     fsTree.Plot_CN = plotCN;
                     fsTree.CuttingUnit_CN = curUnit.CuttingUnit_CN;
                     fsTree.TreeNumber = rTree.TreeNumber;
                     fsTree.Species = rTree.Species;
                     fsTree.CountOrMeasure = rTree.CountOrMeasure;
                     fsTree.SeenDefectPrimary = rTree.SeenDefectPrimary;
                     fsTree.SeenDefectSecondary = rTree.SeenDefectSecondary;
                     fsTree.RecoverablePrimary = rTree.RecoverablePrimary;
                     fsTree.Initials = rTree.Initials;
                     fsTree.LiveDead = rTree.LiveDead;
                     fsTree.Grade = rTree.Grade;
                     fsTree.HeightToFirstLiveLimb = rTree.HeightToFirstLiveLimb;
                     fsTree.DBH = rTree.DBH;
                     fsTree.DRC = rTree.DRC;
                     fsTree.TotalHeight = rTree.TotalHeight;
                     fsTree.MerchHeightPrimary = rTree.MerchHeightPrimary;
                     fsTree.MerchHeightSecondary = rTree.MerchHeightSecondary;
                     fsTree.FormClass = rTree.FormClass;
                     fsTree.UpperStemHeight = rTree.UpperStemHeight;
                     fsTree.TopDIBPrimary = rTree.TopDIBPrimary;
                     fsTree.TopDIBSecondary = rTree.TopDIBSecondary;
                     fsTree.CreatedBy = rTree.CreatedBy;
                     fsTree.CreatedDate = rTree.CreatedDate;

                     fsTree.Save();
                     // save logs
                  }
               }
            }
             }
             return (0);
        }