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();
        }
예제 #2
0
        private void dataGridViewSgStats_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            long n, n2;
             double sgCV, sgCV2, sgErr;
             //string meth = currentStratumStats.Method;
             // check for sgError, SampleSize1, SampleSize2, CV1, CV2, TreesPerAcre, VolumePerAcre, TreesPerPlot, AverageHeight, Sampling Frequency, KZ
             calcStats cStats = new calcStats();
             // find stage 11=tree,single 12=tree,2 stage 21=plot,single 22=plot,2 stage
             int stage = cStats.isTwoStage(meth);

             sColEdit = this.dataGridViewSgStats.Columns[e.ColumnIndex].DataPropertyName;
             if (sColEdit == "SgError")
             {
            //string sgErr = dataGridViewSgStats["SgError", e.RowIndex].Value.ToString();
            sgErr = currentSgStats.SgError;
            sgCV = currentSgStats.CV1;
            // find errors stage 11=tree,single 12=tree,2 stage 21=plot,single 22=plot,2 stage
            if (stage == 11)
            {
               float tpa = currentSgStats.TreesPerAcre;
               long N = (long)Math.Ceiling(tpa* totAcres);

               if (sgErr == 0)
                  currentSgStats.SampleSize1 = N;
               else
                  currentSgStats.SampleSize1 = cStats.getSampleSize(sgErr, sgCV, N);
            }
            else if (stage == 21)
            {

               currentSgStats.SampleSize1 = cStats.getSampleSize(sgErr, sgCV);
               getN2();
            }
            else if (stage == 12 || stage == 22)
            {
               sgCV2 = currentSgStats.CV2;
               cStats.getTwoStageSampleSize(sgCV, sgCV2, sgErr);
               currentSgStats.SampleSize1 = cStats.sampleSize1;
               currentSgStats.SampleSize2 = cStats.sampleSize2;
               //call routine to loop through
               setFirstStagePlots(currentSgStats.SampleSize1);

            }
            if (cStats.isFreq)
               getFreq();
            if (cStats.isKz)
               getKZ();
             }
             else if (sColEdit == "SampleSize1")
             {
            if (stage == 11 || stage == 12)
            {
               getSgErr(stage);

               if (cStats.isFreq)
                  getFreq();
               if (cStats.isKz)
                  getKZ();
            }
            else if (stage == 21 || stage == 22)
            {
               // check for plot cruise, if yes, all SampleSize1 is changed and errors recomputed
               setFirstStagePlots(currentSgStats.SampleSize1);
               }
             }
             else if (sColEdit == "SampleSize2" || sColEdit == "CV2")
             {
            // calculate sgError
            getSgErr(stage);
            // change freq, KZ
            if (cStats.isFreq)
               getFreq();
            if (cStats.isKz)
               getKZ();
             }
             else if (sColEdit == "CV1")
             {
            // calculate sgError
            getSgErr(stage);
             }
             else if (sColEdit == "TreesPerAcre")
             {
            if (cStats.isFreq)
               getFreq();
            if (cStats.isKz)
               getKZ();

            getEstTree();
             }
             else if (sColEdit == "VolumePerAcre")
             {
            if (cStats.isKz)
               getKZ();

            getEstVol();
             }
             else if (sColEdit == "TreesPerPlot")
             {
            // calculate Frequency/KZ/BigBAF for PCM, P3P, FCM, F3P
            if (meth == "PNT" || meth == "FIX")
               getN2();
            if (cStats.isFreq)
               getFreq();
            if (cStats.isKz)
               getKZ();
             }
             else if (sColEdit == "AverageHeight")
             {
            float tpp = currentSgStats.TreesPerPlot;
            float avgHt = currentSgStats.AverageHeight;
            n = currentSgStats.SampleSize1;
            n2 = currentSgStats.SampleSize2;
            if (n2 > 0)
               currentSgStats.KZ = Convert.ToInt32((tpp * n * avgHt) / n2);
            else
               currentSgStats.KZ = 1;
             }
             else if (sColEdit == "SamplingFrequency")
             {
            // calculate sample size, sgError
            long sFreq = currentSgStats.SamplingFrequency;
            if (sFreq < 1) return;

            float tpa = currentSgStats.TreesPerAcre;
            float totAcres = currentStratumStats.TotalAcres;
            float tpp = currentSgStats.TreesPerPlot;
            if (meth == "STR" || meth == "S3P")
            {
               float totalTrees = tpa * totAcres;
               if (sFreq > totalTrees)
               {
                  currentSgStats.SampleSize1 = 1;
                  currentSgStats.SamplingFrequency = Convert.ToInt32(totalTrees);
               }
               else if (sFreq == 0)
                  currentSgStats.SampleSize1 = Convert.ToInt32(totalTrees);
               else
                  currentSgStats.SampleSize1 = Convert.ToInt32((totalTrees / sFreq));
            }
            else if (meth == "FCM" || meth == "PCM")
            {
               float totalTrees = Convert.ToInt32(tpp * currentSgStats.SampleSize1);
               if (sFreq > totalTrees)
               {
                  currentSgStats.SampleSize2 = 1;
                  currentSgStats.SamplingFrequency = Convert.ToInt32(totalTrees);
               }
               else if (sFreq == 0)
                  currentSgStats.SampleSize2 = Convert.ToInt32(totalTrees);
               else
                  currentSgStats.SampleSize2 = Convert.ToInt32(totalTrees / sFreq);
            }
            getSgErr(stage);
             }

             else if (sColEdit == "KZ")
             {
            // calculate sample size, sgError
            long kz = currentSgStats.KZ;
            if (kz < 1) return;

            float tpp = currentSgStats.TreesPerPlot;
            float vpa = currentSgStats.VolumePerAcre;
            float tpa = currentSgStats.TreesPerAcre;
            if (tpa <= 0) tpa = 1;
            n = currentSgStats.SampleSize1;

            if (meth == "3P")
            {
               float totalTrees = tpa * totAcres;
               float totalVol = vpa * totAcres;

               if (kz > totalVol)
               {
                  currentSgStats.SampleSize1 = 1;
                  currentSgStats.KZ = Convert.ToInt32(totalVol);
               }
               else if (kz == 0 || kz == 1)
                  currentSgStats.SampleSize1 = Convert.ToInt32(totalTrees);
               else
               {
                  currentSgStats.SampleSize1 = Convert.ToInt32((totalVol / kz));
               }
            }
            else if (meth == "S3P")
            {
               float totalVol = vpa / tpa * n;

               if (kz > totalVol)
               {
                  currentSgStats.SampleSize2 = 1;
                  currentSgStats.KZ = Convert.ToInt32(totalVol);
               }
               else if (kz == 0 || kz == 1)
                  currentSgStats.SampleSize2 = n;
               else
               {
                  n2 = Convert.ToInt32(totalVol / kz);
                  if (n2 > n)
                  {
                     currentSgStats.SampleSize2 = n;
                     if(n > 0)
                        currentSgStats.KZ = Convert.ToInt32(totalVol / n);
                     else
                        currentSgStats.KZ = 1;
                  }
                  else
                     currentSgStats.SampleSize2 = n2;
               }
            }
            else if (meth == "P3P")
            {
               float avgHt = currentSgStats.AverageHeight;
               currentSgStats.SampleSize2 = Convert.ToInt32((tpp * n * avgHt) / kz);
            }
            else if (meth == "F3P")
            {
               float totalTrees = tpp * n;
               float totalVol = (vpa / tpa) * totalTrees;

               if (kz > totalVol)
               {
                  currentSgStats.SampleSize2 = 1;
                  currentSgStats.KZ = Convert.ToInt32(totalVol);
               }
               else if (kz <= 1)
                  currentSgStats.SampleSize2 = Convert.ToInt32(totalTrees);
               else
               {

                  n2 = Convert.ToInt32(totalVol / kz);
                  if (n2 > totalTrees)
                  {
                     currentSgStats.SampleSize2 = Convert.ToInt32(totalTrees);
                     if (totalTrees > 0)
                        currentSgStats.KZ = Convert.ToInt32(totalVol / totalTrees);
                     else
                        currentSgStats.KZ = 1;
                  }
                  else
                     currentSgStats.SampleSize2 = n2;

               }
            }
            else if (meth == "3PPNT")
            {
               n2 = Convert.ToInt32((tpp * n * (vpa / tpa)) / kz);
               if (n2 > n)
               {
                  currentSgStats.SampleSize2 = n;
                  if (tpa > 0)
                     currentSgStats.KZ = Convert.ToInt32((tpp * n * (vpa / tpa)) / n);
                  else
                     currentSgStats.KZ = 1;
               }
               else
                  currentSgStats.SampleSize2 = n2;
            }
            getSgErr(stage);

             }
             else if (sColEdit == "BigBAF ")
             {
            // calcuate sample size2, sgError
             }

             getStratumStats();
             bindingSourceStratumStats.DataSource = cdStratumStats;
             bindingSourceSgStats.DataSource = cdSgStats;

             double saleError = getSaleError();
        }
예제 #3
0
        private void buttonOptimize_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
             calcStats cStat = new calcStats();
             long strSamp, sgSamp, sgSamp1, sgSamp2;
             double combWtCV, strCV, strCalcError, sgCV, sgCalcError, totVolume;
             // get sale volume
             double tVolume = cdStratumStats.Sum(P => P.TotalVolume);
             if (tVolume == 0)
             {
            MessageBox.Show("No volume. Cannot calculate sample sizes.", "Warning");
            return;
             }
             if (mySale.DefaultUOM == "01")
            totVolume = tVolume * 1000.0;
             else
            totVolume = tVolume * 100.0;
             // get weighted sale cv
             double saleCV = 0;
             long strSamp1 = 0;
             long strSamp2 = 0;
             double sumError = 0;
             double sumVolume = 0;
             double tStrVol;
             foreach (StratumStatsDO thisStrStats in cdStratumStats)
             {
            if (mySale.DefaultUOM == "01")
               tStrVol = thisStrStats.TotalVolume * 1000.0;
            else
               tStrVol = thisStrStats.TotalVolume * 100.0;

            int stage = cStat.isTwoStage(thisStrStats.Method);
            // single stage wted CV
            if (stage == 10 || stage == 11 || stage == 21)
            {
               saleCV += thisStrStats.WeightedCV1 * (tStrVol / totVolume);
            }
            // 2 stage wted CV
            else
            {
               combWtCV = (thisStrStats.WeightedCV1 + thisStrStats.WeightedCV2) / 2.0;
               saleCV += combWtCV * (tStrVol / totVolume);
            }
             }
             if (saleCV == 0)
             {
            MessageBox.Show("Cannot calculate weighted sale CV.", "Warning");
            return;
             }

             //float saleCV = cdStratumStats.Sum(P => ((P.WeightedCV1 * P.TotalVolume)/totVolume));
             double saleOptError = Convert.ToSingle(numericUpDown1.Value);
             // calculate sale level sample size
             long saleCalcSamples = cStat.getSampleSize(saleOptError, saleCV);
             // calculate sale error using t-value of 2
             double saleCalcError = cStat.getSampleError(saleCV, saleCalcSamples, 2.0);
             // correct sample size using correct t-value
             double saleSamples = cStat.checkTValueError(saleCV, saleCalcSamples, saleCalcError);

             // prorate to strata
             foreach (StratumStatsDO thisStrStats in cdStratumStats)
             {
            int stage = cStat.isTwoStage(thisStrStats.Method);
            long strSample1 = 0;
            long strSample2 = 0;
            double wtErr = 0;
            float acres = thisStrStats.TotalAcres;
            float stTpa = thisStrStats.TreesPerAcre;
            long stN = (long)Math.Ceiling(stTpa * acres);
            // single stage wted CV
            if (mySale.DefaultUOM == "01")
               tStrVol = thisStrStats.TotalVolume * 1000.0;
            else
               tStrVol = thisStrStats.TotalVolume * 100.0;

            if (stage == 10 || stage == 21)
            {
               strCV = thisStrStats.WeightedCV1 * (tStrVol / totVolume);
               strSamp = Convert.ToInt32((strCV / saleCV) * saleSamples);
               strCalcError = cStat.getSampleError(thisStrStats.WeightedCV1, strSamp, 2.0);
               // correct for t-value
               strSamp1 = cStat.checkTValueError(thisStrStats.WeightedCV1, strSamp, strCalcError);
            }
            else if (stage == 11)
            {
               strCV = thisStrStats.WeightedCV1 * (tStrVol / totVolume);
               strSamp = Convert.ToInt32((strCV / saleCV) * saleSamples);
               strCalcError = cStat.getSampleError(thisStrStats.WeightedCV1, strSamp, 2.0);
               // correct for t-value
               strSamp1 = cStat.checkTValueError(thisStrStats.WeightedCV1, strSamp, strCalcError, stN);
            }
            // 2 stage wted CV
            else
            {
               combWtCV = (thisStrStats.WeightedCV1 + thisStrStats.WeightedCV2) / 2.0;
               strCV = combWtCV * (tStrVol / totVolume);
               strSamp = Convert.ToInt32((strCV / saleCV) * saleSamples);
               strCalcError = cStat.getSampleError(combWtCV, strSamp, 2.0);
               cStat.getTwoStageSampleSize(thisStrStats.WeightedCV1, thisStrStats.WeightedCV2, strCalcError);
               // correct for t-value
               strSamp2 = cStat.sampleSize2;
               strSamp1 = cStat.checkTValueError2Stage(thisStrStats.WeightedCV1, thisStrStats.WeightedCV2, cStat.sampleSize1, cStat.sampleSize2, strCalcError);
            }
            List<SampleGroupStatsDO> mySgStats = new List<SampleGroupStatsDO>(cdDAL.Read<SampleGroupStatsDO>("SampleGroupStats", "Where StratumStats_CN = ?", thisStrStats.StratumStats_CN));
            long sgSamp2Stage1 = 0;
            long sgSamp2Stage2 = 0;
            foreach (SampleGroupStatsDO thisSgStats in mySgStats)
            {
               float sgTpa = thisSgStats.TreesPerAcre;
               long sgN = (long)Math.Ceiling(sgTpa * acres);
               // prorate to sample groups
               if (stage == 11)
               {
                  sgCV = thisSgStats.CV1 * (thisSgStats.VolumePerAcre / thisStrStats.VolumePerAcre);
                  sgSamp = Convert.ToInt32((sgCV / thisStrStats.WeightedCV1) * strSamp1);
                  sgCalcError = cStat.getSampleError(thisSgStats.CV1, sgSamp, 2.0);
                  // correct for t-value
                  sgSamp1 = cStat.checkTValueError(thisSgStats.CV1, sgSamp, sgCalcError, sgN);
                  if (sgSamp1 < 3) sgSamp1 = 3;
                  sgSamp2 = 0;
                  sgCalcError = cStat.getSampleError(thisSgStats.CV1, sgSamp1, 0, sgN);
               }
               else if (stage == 21)
               {
                  sgCV = thisSgStats.CV1 * (thisSgStats.VolumePerAcre / thisStrStats.VolumePerAcre);
                  sgSamp = Convert.ToInt32((sgCV / thisStrStats.WeightedCV1) * strSamp1);
                  sgCalcError = cStat.getSampleError(thisSgStats.CV1, sgSamp, 2.0);
                  // correct for t-value
                  sgSamp1 = cStat.checkTValueError(thisSgStats.CV1, sgSamp, sgCalcError);
                  if (sgSamp1 < 3) sgSamp1 = 3;
                  sgSamp2 = (long)(thisSgStats.TreesPerPlot * sgSamp1);

                  sgCalcError = cStat.getSampleError(thisSgStats.CV1, sgSamp1, 0);
               }
               else if (stage == 10)
               {
                  sgSamp1 = Convert.ToInt32(thisSgStats.TreesPerAcre * thisStrStats.TotalAcres);
                  sgSamp2 = 0;
                  sgCalcError = 0;
               }
               else
               {
                  sgCV = thisSgStats.CV2 * (thisSgStats.VolumePerAcre / thisStrStats.VolumePerAcre);
                  sgSamp2 = Convert.ToInt32((sgCV / thisStrStats.WeightedCV2) * strSamp2);
                  if (sgSamp2 < 3) sgSamp2 = 3;
                  sgCV = thisSgStats.CV1 * (thisSgStats.VolumePerAcre / thisStrStats.VolumePerAcre);
                  sgSamp1 = Convert.ToInt32((sgCV / thisStrStats.WeightedCV1) * strSamp1);
                  if (sgSamp1 < 3) sgSamp1 = 3;

                  if (stage == 12)
                  {
                     if (sgSamp2 > sgSamp1) sgSamp1 = sgSamp2;
                  }
                  else
                  {
                     if (sgSamp1 > sgSamp2Stage1) sgSamp2Stage1 = sgSamp1;
                  }

                  thisSgStats.SampleSize1 = sgSamp1;
                  thisSgStats.SampleSize2 = sgSamp2;
                  sgCalcError = cStat.getTwoStageError(thisSgStats.CV1, thisSgStats.CV2, sgSamp1, sgSamp2);
               }
               thisSgStats.SampleSize1 = sgSamp1;
               thisSgStats.SampleSize2 = sgSamp2;
               // update errors, frequency, KZ, BigBAF
               if (thisStrStats.Method == "STR")
               {
                  thisSgStats.SamplingFrequency = Convert.ToInt32((thisSgStats.TreesPerAcre * thisStrStats.TotalAcres) / sgSamp1);
                  strSample1 += sgSamp1;
               }
               else if (thisStrStats.Method == "S3P")
               {
                  thisSgStats.SamplingFrequency = Convert.ToInt32((thisSgStats.TreesPerAcre * thisStrStats.TotalAcres) / sgSamp1);
                  strSample1 += sgSamp1;
                  strSample2 += sgSamp2;
               }
               else if (thisStrStats.Method == "100")
               {
                  thisSgStats.SamplingFrequency = 1;
                  strSample1 += sgSamp1;
               }
               else if (thisStrStats.Method == "3P")
               {
                  thisSgStats.KZ = Convert.ToInt32((thisSgStats.VolumePerAcre * thisStrStats.TotalAcres) / sgSamp1);
                  strSample1 += sgSamp1;
               }
               else if (thisStrStats.Method == "S3P")
               {
                  if (thisSgStats.TreesPerAcre > 0)
                     thisSgStats.KZ = Convert.ToInt32(((thisSgStats.VolumePerAcre / thisSgStats.TreesPerAcre * sgSamp1) / sgSamp2));
                  else
                     thisSgStats.KZ = 1;
                  strSample1 += sgSamp1;
                  strSample2 += sgSamp2;
               }
               else if (thisStrStats.Method == "PNT" || thisStrStats.Method == "FIX")
               {
                  strSample1 = sgSamp1;
                  strSample2 = sgSamp2;

               }
               // calc sg error
               if(stage != 22)
               {
                  if (stage == 11)
                  {
                    thisSgStats.SgError = Convert.ToSingle(Math.Round(cStat.getSampleError(thisSgStats.CV1,sgSamp1, 0, sgN), 2));
                  }
                  else if (stage == 21)
                  {
                     thisSgStats.SgError = Convert.ToSingle(Math.Round(cStat.getSampleError(thisSgStats.CV1, sgSamp1, 0), 2));
                  }
                  else if (stage == 12 || stage == 22)
                  {
                     thisSgStats.SgError = Convert.ToSingle(Math.Round(cStat.getTwoStageError(thisSgStats.CV1, thisSgStats.CV2, sgSamp1, sgSamp2), 2));
                  }
                  // calc combined stratum error
                  wtErr += (double)Math.Pow((thisSgStats.SgError * (thisSgStats.VolumePerAcre * thisStrStats.TotalAcres)), 2);
               }
               thisSgStats.Save();
            }
            if (stage == 22)
            {
               //loop back through
               foreach (SampleGroupStatsDO thisSgStats in mySgStats)
               {
                  // set each sample size to sgSamp2Stage
                  strSample1 = sgSamp2Stage1;
                  sgSamp2Stage2 = thisSgStats.SampleSize2;
                  strSample2 += sgSamp2Stage2;
                  // set frequencies
                  if (thisStrStats.Method == "FCM")
                  {
                     thisSgStats.SamplingFrequency = Convert.ToInt32((thisSgStats.TreesPerPlot * sgSamp2Stage1) / sgSamp2Stage2);
                  }
                  else if (thisStrStats.Method == "PCM")
                  {
                     thisSgStats.SamplingFrequency = Convert.ToInt32((thisSgStats.TreesPerPlot * sgSamp2Stage1) / sgSamp2Stage2);
                     thisSgStats.BigBAF = Convert.ToSingle(thisSgStats.SamplingFrequency * thisStrStats.BasalAreaFactor);
                  }
                  else if (thisStrStats.Method == "P3P")
                  {
                     thisSgStats.KZ = Convert.ToInt32((thisSgStats.TreesPerPlot * sgSamp2Stage1 * thisSgStats.AverageHeight) / sgSamp2Stage2);
                  }
                  else if (thisStrStats.Method == "F3P" || thisStrStats.Method == "3PPNT")
                  {
                     if (thisSgStats.TreesPerAcre > 0)
                        thisSgStats.KZ = Convert.ToInt32((thisSgStats.TreesPerPlot * sgSamp2Stage1 * (thisSgStats.VolumePerAcre / thisSgStats.TreesPerAcre)) / sgSamp2Stage2);
                     else
                        thisSgStats.KZ = 1;
                  }
                  thisSgStats.SampleSize1 = sgSamp2Stage1;
                  thisSgStats.SgError = Convert.ToSingle(Math.Round(cStat.getTwoStageError(thisSgStats.CV1, thisSgStats.CV2, sgSamp2Stage1, sgSamp2Stage2), 2));
               // calc combined stratum error
                  thisSgStats.Save();
                  wtErr += (double)Math.Pow((thisSgStats.SgError * (thisSgStats.VolumePerAcre * thisStrStats.TotalAcres)), 2);
               }
            }

            // Update Stratum
            if(mySale.DefaultUOM == "01")
               thisStrStats.StrError = (float)(Math.Sqrt(wtErr) / (tStrVol));
            else
               thisStrStats.StrError = (float)(Math.Sqrt(wtErr) / (tStrVol));

            thisStrStats.SampleSize1 = strSample1;
            thisStrStats.SampleSize2 = strSample2;
            if (stage > 20)
            {
               thisStrStats.PlotSpacing = (int)Math.Floor(Math.Sqrt((thisStrStats.TotalAcres * 43560) / strSample1));
            }
            thisStrStats.Save();
            sumError += (double)Math.Pow((tStrVol * thisStrStats.StrError),2);
            sumVolume += tStrVol;
             }
             // Update Sale Error and Cost
             getSaleError();
             //double saleError = Math.Sqrt(sumError) / sumVolume;
             //textBoxError.Text = (Math.Round(saleError, 2)).ToString();
             //textBoxVolume.Text = (Math.Round(tVolume, 0)).ToString();

             Cursor.Current = this.Cursor;
             return;
        }
예제 #4
0
        private void setGridDisplay()
        {
            // select visible fields by method, update headers
             calcStats cStat = new calcStats();
             //string meth = currentStratumStats.Method;
             int stage = cStat.isTwoStage(meth);
             if (cStat.isKz)
            kZDataGridViewTextBoxColumn.Visible = true;
             else
            kZDataGridViewTextBoxColumn.Visible = false;

             if(cStat.isFreq)
            samplingFrequencyDataGridViewTextBoxColumn.Visible = true;
             else
            samplingFrequencyDataGridViewTextBoxColumn.Visible = false;

             if ((cStat.isFreq || cStat.isKz) && stage < 20)
            insuranceFrequencyDataGridViewTextBoxColumn.Visible = true;
             else
            insuranceFrequencyDataGridViewTextBoxColumn.Visible = false;

             if (stage == 12 || stage == 22)
             {
            cV2DataGridViewTextBoxColumn.Visible = true;
            sampleSize2DataGridViewTextBoxColumn1.Visible = true;
            sampleSize2DataGridViewTextBoxColumn1.ToolTipText = "Number of Measure Trees";
             }
             else
             {
            cV2DataGridViewTextBoxColumn.Visible = false;
            sampleSize2DataGridViewTextBoxColumn1.Visible = false;
             }

             if (stage == 21 || stage == 22)
             {
            treesPerPlotDataGridViewTextBoxColumn.Visible = true;
            ReconPlotsDataGridViewTextBoxColumn.Visible = true;
            sampleSize1DataGridViewTextBoxColumn1.ToolTipText = "Number of Plots";
             }
             else
             {
            treesPerPlotDataGridViewTextBoxColumn.Visible = false;
            ReconPlotsDataGridViewTextBoxColumn.Visible = false;
            sampleSize1DataGridViewTextBoxColumn1.ToolTipText = "Number of Trees";
             }

             if (meth == "P3P")
             {
            averageHeightDataGridViewTextBoxColumn.Visible = true;
             }
             else
             {
            averageHeightDataGridViewTextBoxColumn.Visible = false;
             }

             if (meth == "PCM")
             {
            bigBAFDataGridViewTextBoxColumn.Visible = true;
             }
             else
             {
            bigBAFDataGridViewTextBoxColumn.Visible = false;
             }

             if (meth == "3PPNT")
             {
            insuranceFrequencyDataGridViewTextBoxColumn.Visible = true;
            sampleSize2DataGridViewTextBoxColumn1.ToolTipText = "Number of Measure Plots";
             }

             // check for red fonts
             // loop through cdSgStats
            //         foreach (SampleGroupStatsDO thisSgStats in cdSgStats)
            //         {
            //            if (thisSgStats.CV_Def == 1)
            //               cV1DataGridViewTextBoxColumn.DefaultCellStyle.ForeColor = Color.Red;
            //            else
            //               cV1DataGridViewTextBoxColumn.DefaultCellStyle.ForeColor = Color.Black;

            //            if (thisSgStats.CV2_Def == 1)
            //               cV2DataGridViewTextBoxColumn.DefaultCellStyle.ForeColor = Color.Red;
            //            else
            //               cV2DataGridViewTextBoxColumn.DefaultCellStyle.ForeColor = Color.Black;
            //         }
        }
예제 #5
0
        //********************************************************
        private void setFirstStagePlots(long sampSize)
        {
            // loop through all sample groups
             long _freq;
             calcStats cStat = new calcStats();
             foreach (SampleGroupStatsDO thisSgStats in cdSgStats)
             {
            // change first stage sample size
            thisSgStats.SampleSize1 = sampSize;
            // update Freq, KZ, sgErr

            if (meth == "PNT" || meth == "FIX")
            {
               // update error
               thisSgStats.SgError = Convert.ToSingle(Math.Round(cStat.getSampleError(thisSgStats.CV1, sampSize, 0), 2));
               thisSgStats.SampleSize2 = (long)(sampSize * thisSgStats.TreesPerPlot);
            }
            else
            {
               long n2 = thisSgStats.SampleSize2;
               if (n2 < 1) return;
               float tpp = thisSgStats.TreesPerPlot;
               float vpa = thisSgStats.VolumePerAcre;
               float tpa = thisSgStats.TreesPerAcre;
               if (tpa <= 0) tpa = 1;

               if (meth == "PCM" || meth == "FCM")
               {
                  // update freq
                  _freq = thisSgStats.SamplingFrequency;
                  if (_freq == 1)
                  {
                     thisSgStats.SampleSize2 = Convert.ToInt32((tpp * sampSize));
                  }
                  else
                  {
                     thisSgStats.SamplingFrequency = Convert.ToInt32((tpp * sampSize) / n2);
                  }
                  if (meth == "PCM")
                     thisSgStats.BigBAF = Convert.ToSingle(thisSgStats.SamplingFrequency * currentStratumStats.BasalAreaFactor);
               }
               else if (meth == "F3P")
               {
                  if (tpa > 0)
                     thisSgStats.KZ = Convert.ToInt32((tpp * sampSize * (vpa / tpa)) / n2);
                  else
                     thisSgStats.KZ = 1;
               }
               else if (meth == "P3P")
               {
                  if (n2 > 0)
                     thisSgStats.KZ = Convert.ToInt32((tpp * sampSize * thisSgStats.AverageHeight) / n2);
                  else
                     thisSgStats.KZ = 1;
               }
               else if (meth == "3PPNT")
               {
                  if (tpa > 0)
                     thisSgStats.KZ = Convert.ToInt32((tpp * sampSize * (vpa / tpa)) / n2);
                  else
                     thisSgStats.KZ = 1;
               }
               // update err
               thisSgStats.SgError = Convert.ToSingle(Math.Round(cStat.getTwoStageError(thisSgStats.CV1, thisSgStats.CV2, sampSize, thisSgStats.SampleSize2), 2));
            }
            thisSgStats.Save();
             }
        }
예제 #6
0
        //*******************************
        private void getSgErr(int stage)
        {
            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));
             }
        }
        //*************************************************************************
        private void getSampleGroupStats()
        {
            calcStats statClass = new calcStats();
             stage = statClass.isTwoStage(Owner.currentStratum.Method);

             double comberr2 = 0;
             double totalVolume = 0;
             // for each sample group
             foreach (SampleGroupDO sg in Owner.histSampleGroup)
             {
            // create samplegroupstats
            Owner.currentSgStats = new SampleGroupStatsDO(Owner.cdDAL);
            //set foriegn key
            Owner.currentSgStats.StratumStats = Owner.currentStratumStats;
            Owner.currentSgStats.SgSet = 1;
            Owner.currentSgStats.Code = sg.Code;
            Owner.currentSgStats.CutLeave = sg.CutLeave;
            Owner.currentSgStats.UOM = sg.UOM;
            Owner.currentSgStats.PrimaryProduct = sg.PrimaryProduct;
            Owner.currentSgStats.SecondaryProduct = sg.SecondaryProduct;
            Owner.currentSgStats.DefaultLiveDead = sg.DefaultLiveDead;
            Owner.currentSgStats.Description = sg.Description;
            // get POP data
            //Owner.selectedPOP = new POPDO(Owner.hDAL);
            Owner.selectedPOP = Owner.hDAL.ReadSingleRow<POPDO>("POP", "WHERE Stratum = ? AND SampleGroup = ?", Owner.selectedStratum.Code, sg.Code);
            // calculate statistics (based on method)
            if (Owner.selectedPOP == null)
            {
               MessageBox.Show("Cruise Not Processed. Cannot Import Data.", "Warning");
               return;
            }
            n1 = Convert.ToInt32(Owner.selectedPOP.StageOneSamples);
            n2 = Convert.ToInt32(Owner.selectedPOP.StageTwoSamples);
            measTrees = Convert.ToInt32(Owner.selectedPOP.MeasuredTrees);
            talliedTrees = Convert.ToInt32(Owner.selectedPOP.TalliedTrees);
            sumKpi = Convert.ToInt32(Owner.selectedPOP.SumKPI);
            st1x = Owner.selectedPOP.Stg1NetXPP;
            st1x2 = Owner.selectedPOP.Stg1NetXsqrdPP;
            st2x = Owner.selectedPOP.Stg2NetXPP;
            st2x2 = Owner.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;
            Owner.currentSgStats.CV1 = Convert.ToSingle(cv1);
            Owner.currentSgStats.CV2 = Convert.ToSingle(cv2);
            Owner.currentSgStats.SampleSize1 = n1;
            Owner.currentSgStats.SampleSize2 = n2;
            // calculate frequency

            Owner.currentSgStats.SgError = Convert.ToSingle(sampErr);

            // get LCD data
            Owner.selectedLCD = Owner.hDAL.Read<LCDDO>("LCD", "WHERE Stratum = ? AND SampleGroup = ?", Owner.selectedStratum.Code, sg.Code);
            sumExpFac = 0;
            sumNetVol = 0;
            //foreach (SampleGroupDO sg in Owner.histSampleGroup)
            foreach (LCDDO lcd in Owner.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)
            {
               Owner.currentSgStats.TreesPerAcre = Convert.ToSingle(Math.Round((sumExpFac / totalAcres), 2));
               Owner.currentSgStats.VolumePerAcre = Convert.ToSingle(Math.Round((sumNetVol / totalAcres), 2));
            }
            else
            {
               Owner.currentSgStats.TreesPerAcre = Convert.ToSingle(Math.Round((sumExpFac), 2));
               Owner.currentSgStats.VolumePerAcre = Convert.ToSingle(Math.Round((sumNetVol), 2));
               if(stage == 21)
                  Owner.currentSgStats.TreesPerPlot = Convert.ToSingle(Math.Round((Convert.ToSingle((float)measTrees / (float)n1)), 1));
               else
                  Owner.currentSgStats.TreesPerPlot = Convert.ToSingle(Math.Round((Convert.ToSingle((float)talliedTrees / (float) n1)), 1));
            }
            // 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 (Owner.currentStratum.Method == "S3P")
               {
                  Owner.currentSgStats.SamplingFrequency = Convert.ToInt32((talliedTrees / n1));
                  Owner.currentSgStats.KZ = kz;
               }
               else
               {
                  Owner.currentSgStats.SamplingFrequency = freq;
                  Owner.currentSgStats.KZ = kz;
               }
            }

            // save samplegroupstats row
            Owner.currentSgStats.Save();
            // loop through TDV information
            sg.TreeDefaultValues.Populate();
            foreach (TreeDefaultValueDO tdv in sg.TreeDefaultValues)
            {
               // check with current TDV values
               Owner.currentTreeDefaults = Owner.cdDAL.ReadSingleRow<TreeDefaultValueDO>("TreeDefaultValue", "WHERE Species = ? AND PrimaryProduct = ? AND LiveDead = ?",tdv.Species,tdv.PrimaryProduct,tdv.LiveDead);
               if (Owner.currentTreeDefaults == null)
               {
                  // if null, add tdv to list then create link
                  TreeDefaultValueDO newTdv = new TreeDefaultValueDO(Owner.cdDAL);
                  newTdv.SetValues(tdv);
                  newTdv.Save();
                  Owner.currentSgStats.TreeDefaultValueStats.Add(newTdv);
               }
               else
               {
                  // if exists, create link
                  Owner.currentSgStats.TreeDefaultValueStats.Add(Owner.currentTreeDefaults);
               }
            }

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

             }
             // calculate stats for stratum
            // double stratumError = Math.Sqrt(comberr2) / totalVolume;
             // wted CVs
             //Owner.currentStratumStats.StrError = Convert.ToSingle(stratumError);

             //Owner.currentStratumStats.Save();
        }
        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 setGridDisplay()
        {
            // select visible fields by method, update headers
             calcStats cStat = new calcStats();
             //string meth = currentStratumStats.Method;
             int stage = cStat.isTwoStage(meth);
             if (cStat.isKz)
            kZDataGridViewTextBoxColumn.Visible = true;
             else
            kZDataGridViewTextBoxColumn.Visible = false;

             if (cStat.isFreq)
            samplingFrequencyDataGridViewTextBoxColumn.Visible = true;
             else
            samplingFrequencyDataGridViewTextBoxColumn.Visible = false;

             if ((cStat.isFreq || cStat.isKz) && stage < 20)
            insuranceFrequencyDataGridViewTextBoxColumn.Visible = true;
             else
            insuranceFrequencyDataGridViewTextBoxColumn.Visible = false;

             if (stage == 10 || stage == 11)
             {
            // second Stage columns
            cV2DataGridViewTextBoxColumn.Visible = false;
            sampleSize2DataGridViewTextBoxColumn1.Visible = false;
            // trees per plot
            treesPerPlotDataGridViewTextBoxColumn.Visible = false;

            // cruise data columns
            ReconPlotsDataGridViewTextBoxColumn.Visible = false;

            // sample size tooltips
            sampleSize1DataGridViewTextBoxColumn1.ToolTipText = "Number of Trees";

             }
             else if (stage == 12)
             {
            // second Stage columns
            cV2DataGridViewTextBoxColumn.Visible = true;
            sampleSize2DataGridViewTextBoxColumn1.Visible = true;
            // trees per plot
            treesPerPlotDataGridViewTextBoxColumn.Visible = false;
            // cruise data columns
            ReconPlotsDataGridViewTextBoxColumn.Visible = true;
            ReconPlotsDataGridViewTextBoxColumn.HeaderText = "3P Tree";
            ReconPlotsDataGridViewTextBoxColumn.ToolTipText = "Number of 1st Stage Samples from Cruise";
            // sample size tooltips
            sampleSize1DataGridViewTextBoxColumn1.ToolTipText = "Number of 3P Trees";
            sampleSize2DataGridViewTextBoxColumn1.ToolTipText = "Number of Measure Trees";
             }
             else if (stage == 21)
             {
            // second Stage columns
            cV2DataGridViewTextBoxColumn.Visible = false;
            sampleSize2DataGridViewTextBoxColumn1.Visible = false;
            // trees per plot
            treesPerPlotDataGridViewTextBoxColumn.Visible = true;
            // cruise data columns
            ReconPlotsDataGridViewTextBoxColumn.Visible = true;
            ReconPlotsDataGridViewTextBoxColumn.HeaderText = "Plots";
            ReconPlotsDataGridViewTextBoxColumn.ToolTipText = "Number of Plots from Cruise";
            // sample size tooltips
            sampleSize1DataGridViewTextBoxColumn1.ToolTipText = "Number of Plots";

             }
             else if (stage == 22)
             {
            // second Stage columns
            cV2DataGridViewTextBoxColumn.Visible = true;
            sampleSize2DataGridViewTextBoxColumn1.Visible = true;
            // trees per plot
            treesPerPlotDataGridViewTextBoxColumn.Visible = true;
            // cruise data columns
            ReconPlotsDataGridViewTextBoxColumn.Visible = true;
            ReconPlotsDataGridViewTextBoxColumn.HeaderText = "Plots";
            ReconPlotsDataGridViewTextBoxColumn.ToolTipText = "Number of Plots from Cruise";
            // sample size tooltips
            sampleSize1DataGridViewTextBoxColumn1.ToolTipText = "Number of Plots";
            sampleSize2DataGridViewTextBoxColumn1.ToolTipText = "Number of Measure Trees";
             }

             if (meth == "P3P")
             {
            averageHeightDataGridViewTextBoxColumn.Visible = true;
             }
             else
             {
            averageHeightDataGridViewTextBoxColumn.Visible = false;
             }

             if (meth == "PCM")
             {
            bigBAFDataGridViewTextBoxColumn.Visible = true;
             }
             else
             {
            bigBAFDataGridViewTextBoxColumn.Visible = false;
             }

             if (meth == "3PPNT")
             {
            insuranceFrequencyDataGridViewTextBoxColumn.Visible = true;
            sampleSize2DataGridViewTextBoxColumn1.ToolTipText = "Number of Measure Plots";
             }
        }
        //********************************************************
        private void setFirstStagePlots(long sampSize)
        {
            // loop through all sample groups
             long _freq;
             float totalTrees;
             calcStats cStat = new calcStats();
             foreach (SampleGroupStatsDO thisSgStats in cdSgStats)
             {
            // change first stage sample size
            thisSgStats.SampleSize1 = sampSize;
            // update Freq, KZ, sgErr

            if (meth == "PNT" || meth == "FIX")
            {
               // update error
               thisSgStats.SgError = Convert.ToSingle(Math.Round(cStat.getSampleError(thisSgStats.CV1, sampSize, 0), 2));
               thisSgStats.SampleSize2 = (long)(sampSize * thisSgStats.TreesPerPlot);
            }
            else
            {
               long n2 = thisSgStats.SampleSize2;
               if (n2 < 1) return;
               float tpp = thisSgStats.TreesPerPlot;
               float vpa = thisSgStats.VolumePerAcre;
               float tpa = thisSgStats.TreesPerAcre;
               if (tpa <= 0) tpa = 1;

               if (meth == "PCM" || meth == "FCM")
               {
                  // update freq
                  _freq = thisSgStats.SamplingFrequency;
                  if (_freq == 1)
                  {
                     thisSgStats.SampleSize2 = Convert.ToInt32((tpp * sampSize));
                  }
                  else if (_freq <= 0)
                  {
                     _freq = Convert.ToInt32((tpp * sampSize) / n2);
                     thisSgStats.SamplingFrequency = _freq;
                     thisSgStats.SampleSize2 = Convert.ToInt32((tpp * sampSize) / _freq);
                  }
                  else
                  {
                     thisSgStats.SampleSize2 = Convert.ToInt32((tpp * sampSize) / _freq);
                  }
               }
               else if (meth == "F3P")
               {
                  long _kz = thisSgStats.KZ;
                  totalTrees = tpp * sampSize;
                  float totalVol = (vpa / tpa) * totalTrees;

                  if (_kz > totalVol)
                  {
                     thisSgStats.SampleSize2 = 1;
                  }
                  else if (_kz <= 1)
                     thisSgStats.SampleSize2 = Convert.ToInt32(totalTrees);
                  else
                  {

                     n2 = Convert.ToInt32(totalVol / _kz);
                     if (n2 > totalTrees)
                     {
                        thisSgStats.SampleSize2 = Convert.ToInt32(totalTrees);
                     }
                     else
                        thisSgStats.SampleSize2 = n2;
                  }
               }
               else if (meth == "P3P")
               {
                  long _kz = thisSgStats.KZ;
                  float avgHt = thisSgStats.AverageHeight;
                  thisSgStats.SampleSize2 = Convert.ToInt32((tpp * sampSize * avgHt) / _kz);
               }
               else if (meth == "3PPNT")
               {
                  long _kz = thisSgStats.KZ;
                  n2 = Convert.ToInt32((tpp * sampSize * (vpa / tpa)) / _kz);
                  if (n2 > sampSize)
                     thisSgStats.SampleSize2 = sampSize;
                  else
                     thisSgStats.SampleSize2 = n2;
               }
               // update err
               thisSgStats.SgError = Convert.ToSingle(Math.Round(cStat.getTwoStageError(thisSgStats.CV1, thisSgStats.CV2, sampSize, thisSgStats.SampleSize2), 2));
            }
            thisSgStats.Save();
             }
        }
        private void resetSampleSizesForStratumToolStripMenuItem_Click(object sender, EventArgs e)
        {
            calcStats cStat = new calcStats();
             int stage = cStat.isTwoStage(meth);

             //loop through sample groups
             foreach (SampleGroupStatsDO thisSgStats in cdSgStats)
             {
            if (stage == 11)
            {
               thisSgStats.SampleSize1 = thisSgStats.ReconTrees;
            }
            else
            {
               thisSgStats.SampleSize1 = thisSgStats.ReconPlots;
               thisSgStats.SampleSize2 = thisSgStats.ReconTrees;
            }
            getSgErr(stage,thisSgStats);
             }

             // recalculate strata errors
        }
        private void resetSampleGroupSampleSizesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            calcStats cStat = new calcStats();
             //string meth = currentStratumStats.Method;
             int stage = cStat.isTwoStage(meth);
             if (stage == 11)
             {
            currentSgStats.SampleSize1 = currentSgStats.ReconTrees;
             }
             else
             {
            currentSgStats.SampleSize1 = currentSgStats.ReconPlots;
            currentSgStats.SampleSize2 = currentSgStats.ReconTrees;
             }
             if(stage > 20)
            setFirstStagePlots(currentSgStats.SampleSize1);

             getSgErr(stage,currentSgStats);

             // recalculate strata error
        }
        private void dataGridViewSgStats_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            long n, n2;
             double sgCV, sgCV2, sgErr;
             // check for sgError, SampleSize1, SampleSize2
             calcStats cStats = new calcStats();
             // find stage 11=tree,single 12=tree,2 stage 21=plot,single 22=plot,2 stage
             int stage = cStats.isTwoStage(meth);

             sColEdit = this.dataGridViewSgStats.Columns[e.ColumnIndex].DataPropertyName;
             if (sColEdit == "SgError")
             {
            //string sgErr = dataGridViewSgStats["SgError", e.RowIndex].Value.ToString();
            sgErr = currentSgStats.SgError;
            sgCV = currentSgStats.CV1;
            // find errors stage 11=tree,single 12=tree,2 stage 21=plot,single 22=plot,2 stage
            if (stage == 11)
            {
               float tpa = currentSgStats.TreesPerAcre;
               long N = (long)Math.Ceiling(tpa * totAcres);

               if (sgErr == 0)
                  currentSgStats.SampleSize1 = N;
               else
                  currentSgStats.SampleSize1 = cStats.getSampleSize(sgErr, sgCV, N);
            }
            else if (stage == 21)
            {

               currentSgStats.SampleSize1 = cStats.getSampleSize(sgErr, sgCV);
               setFirstStagePlots(currentSgStats.SampleSize1);
            }
            else if (stage == 12 || stage == 22)
            {
               sgCV2 = currentSgStats.CV2;
               cStats.getTwoStageSampleSize(sgCV, sgCV2, sgErr);
               currentSgStats.SampleSize1 = cStats.sampleSize1;
               currentSgStats.SampleSize2 = cStats.sampleSize2;
               //call routine to loop through
               setFirstStagePlots(currentSgStats.SampleSize1);

            }
             }

             else if (sColEdit == "SampleSize1")
             {
            if (stage == 11 || stage == 12)
            {
               getSgErr(stage, currentSgStats);

            }
            else if (stage == 21 || stage == 22)
            {
               // check for plot cruise, if yes, all SampleSize1 is changed and errors recomputed
               setFirstStagePlots(currentSgStats.SampleSize1);
            }
             }
             else if (sColEdit == "SampleSize2")
             {
            // calculate sgError
            getSgErr(stage,currentSgStats);
            // change freq, KZ
             }

             getStratumStats();
             bindingSourceStratumStats.DataSource = cdStratumStats;
             bindingSourceSgStats.DataSource = cdSgStats;

             double saleError = getSaleError();
        }