//*************************************************************************
        public void getStratumStats(StratumStatsDO thisStrStats)
        {
            List<SampleGroupStatsDO> mySgStats;
             float totalVolumeAcre, totalVolume, wtCV1, wtCv2, volumeAcre;
             float cv1, cv2, wtErr, sgErr, treesAcre;
             long sampleSize1, sampleSize2;
             string _UOM = "";
             //loop through SampleGroupStats

             mySgStats = new List<SampleGroupStatsDO>(cdDAL.Read<SampleGroupStatsDO>("SampleGroupStats", "Where StratumStats_CN = ?", thisStrStats.StratumStats_CN));
             // loop through sample groups
             //totalVolumeAcre = getTotals(mySgStats);
             totalVolumeAcre = mySgStats.Sum(P => P.VolumePerAcre);
             totalVolume = totalVolumeAcre * thisStrStats.TotalAcres;
             treesAcre = 0;
             sampleSize1 = 0;
             sampleSize2 = 0;
             wtCV1 = 0;
             wtCv2 = 0;
             wtErr = 0;
             foreach (SampleGroupStatsDO thisSgStats in mySgStats)
             {
            //sum volumes, trees/acre, sample sizes
            treesAcre += thisSgStats.TreesPerAcre;
            if(stage > 20)
               sampleSize1 = thisSgStats.SampleSize1;
            else
               sampleSize1 += thisSgStats.SampleSize1;
            sampleSize2 += thisSgStats.SampleSize2;
            volumeAcre = thisSgStats.VolumePerAcre;
            cv1 = thisSgStats.CV1;
            cv2 = thisSgStats.CV2;
            sgErr = thisSgStats.SgError;
            //calculate weighted CVs
            if (totalVolumeAcre > 0)
            {
               wtCV1 += cv1 * (volumeAcre / totalVolumeAcre);
               wtCv2 += cv2 * (volumeAcre / totalVolumeAcre);
            }

            wtErr += (float)Math.Pow((sgErr * (volumeAcre * thisStrStats.TotalAcres)), 2);
            _UOM = thisSgStats.UOM;
             }
             //save calculated values
             if (totalVolume > 0)
            thisStrStats.StrError = (float)Math.Sqrt(wtErr) / totalVolume;
             else
            thisStrStats.StrError = 0;
             thisStrStats.SampleSize1 = sampleSize1;
             thisStrStats.SampleSize2 = sampleSize2;
             thisStrStats.WeightedCV1 = wtCV1;
             thisStrStats.WeightedCV2 = wtCv2;
             thisStrStats.TreesPerAcre = treesAcre;
             thisStrStats.VolumePerAcre = totalVolumeAcre;
             //Uom Check
             if(_UOM == "01")
            thisStrStats.TotalVolume = (float)(totalVolume / 1000.0);
             else
            thisStrStats.TotalVolume = (float)(totalVolume / 100.0);
             if (thisStrStats.Method == "FIX" || thisStrStats.Method == "FCM" || thisStrStats.Method == "F3P" || thisStrStats.Method == "PNT" || thisStrStats.Method == "PCM" || thisStrStats.Method == "P3P" || thisStrStats.Method == "3PPNT")
             {
            if (sampleSize1 > 0)
               thisStrStats.PlotSpacing = (int)Math.Floor(Math.Sqrt((thisStrStats.TotalAcres * 43560) / sampleSize1));
             }
             else
             {
            if (thisStrStats.TreesPerAcre > 0)
               thisStrStats.PlotSpacing = (int)Math.Floor(Math.Sqrt((43560) / thisStrStats.TreesPerAcre));
             }

             thisStrStats.Save();
        }
        public int getPopulations(StratumDO currentStratum)
        {
            // for each stratum
             stage = statClass.isTwoStage(currentStratum.Method);
             strStats = new StratumStatsDO(cdDAL);
             strStats.Stratum = currentStratum;
             strStats.Code = currentStratum.Code;
             strStats.Description = currentStratum.Description;
             strStats.SgSet = 1;
             strStats.SgSetDescription = "";

             float totalAcres = 0;
             currentStratum.CuttingUnits.Populate();

             foreach (CuttingUnitDO cu in currentStratum.CuttingUnits)
             {
            float acres = cu.Area;
            totalAcres += acres;
             }

             strStats.TotalAcres = totalAcres;
             strStats.Method = currentStratum.Method;
             strStats.BasalAreaFactor = currentStratum.BasalAreaFactor;
             strStats.FixedPlotSize = currentStratum.FixedPlotSize;
             strStats.Used = 1;
             strStats.Save();

             if (getSampleGroupStats(currentStratum, strStats, totalAcres) < 0)
            return(-1);

             getStratumStats(strStats);
             // create stratum sets;
             // create sample group sets
             // calculate stats
             return (0);
        }
 public long? GetStratumStatsCN(StratumStatsDO curStrStats, string method)
 {
     //find StratumStats_CN for population with Stratum_CN, Code, SgSet, method are the same
      StratumStatsDO thisStrStats = (cdDAL.ReadSingleRow<StratumStatsDO>("StratumStats", "WHERE Stratum_CN = ? AND SgSet = ? AND Method = ?", curStrStats.Stratum_CN, curStrStats.SgSet, method));
      //         currentStratumStats = (cdDAL.ReadSingleRow<StratumStatsDO>("StratumStats", "WHERE Stratum_CN = ? AND SgSet = 1", currentStratum.Stratum_CN));
      if (thisStrStats != null)
      {
     if(method == "FIX" || method == "FCM" || method == "F3P")
        thisStrStats.FixedPlotSize = curPlotFixSize;
     else if(method == "PNT" || method == "PCM" || method == "P3P" || method == "3PPNT")
        thisStrStats.BasalAreaFactor = curPlotPntSize;
     thisStrStats.TotalAcres = totalAcres;
     thisStrStats.Used = 0;
     thisStrStats.Save();
     return (thisStrStats.StratumStats_CN);
      }
      else
      {
     // add new record
     StratumStatsDO newStrStats = new StratumStatsDO(cdDAL);
     newStrStats.Stratum_CN = curStrStats.Stratum_CN;
     newStrStats.Code = curStrStats.Code;
     newStrStats.Description = curStrStats.Description;
     newStrStats.SgSet = curStrStats.SgSet;
     newStrStats.SgSetDescription = curStrStats.SgSetDescription;
     newStrStats.TotalAcres = totalAcres;
     newStrStats.Method = method;
     newStrStats.Used = 0;
     if (method == "FIX" || method == "FCM" || method == "F3P")
        newStrStats.FixedPlotSize = curPlotFixSize;
     else if (method == "PNT" || method == "PCM" || method == "P3P" || method == "3PPNT")
        newStrStats.BasalAreaFactor = curPlotPntSize;
     newStrStats.Save();
     // return StratumStats_CN
     return (newStrStats.StratumStats_CN);
      }
 }