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();
        }
        //---------------------------------------------------------------------------------------------------------
        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 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();
             }
        }