Primitive Cruise Expansion

This is a basic cruise expansion demonstration creating a weighted average of diameter class records for all groups with no filtering unlike the original.


<?php

  require_once("FPSOpen.php");

  echo "\nFSD FPSExpander\n\n";

  $SQL = "SELECT VEG_LBL, SUM(AREA_GIS) AS AREA FROM ADMIN WHERE FLAG > 0 AND MSMT_YR > 0 GROUP BY VEG_LBL;";

  foreach($dbh->query($SQL, PDO::FETCH_ASSOC) as $row) {
    $VegLbl[$row['VEG_LBL']] = $row['AREA'];
  }

  foreach($VegLbl as $Lbl => $Area) {

    $SQL = "SELECT
      DBHCLS.SPECIES as Species,
      DBHCLS.GRP as Grp,
      ADMIN.RPT_YR AS Rpt_YR ,
      Sum(IIf([DBHCLS].[Age]>0,[DBHCLS].[Age],Null)*[ADMIN].[AREA_GIS])/Sum([ADMIN].[AREA_GIS]) AS Age,
      Sum([DBHCLS].[TREES]*[ADMIN].[AREA_GIS])/{$Area} AS Trees,
      Sum([DBHCLS].[DBH]*[ADMIN].[AREA_GIS])/Sum([ADMIN].[AREA_GIS]) AS DBH,
      Sum([DBHCLS].[BASAL]*[ADMIN].[AREA_GIS])/{$Area} AS Basal,
      Sum([DBHCLS].[Height]*[ADMIN].[AREA_GIS])/Sum([ADMIN].[AREA_GIS]) AS Height,
      Sum([DBHCLS].[BOLEHT]*[ADMIN].[AREA_GIS])/Sum([ADMIN].[AREA_GIS]) AS BoleHT,
      Sum([DBHCLS].[TAPER]*[ADMIN].[AREA_GIS])/Sum([ADMIN].[AREA_GIS]) AS Taper,
      Sum([DBHCLS].[CROWN]*[ADMIN].[AREA_GIS])/Sum([ADMIN].[AREA_GIS]) AS Crown,
      Round(Sum([DBHCLS].[Vigor]*[ADMIN].[AREA_GIS])/Sum([ADMIN].[AREA_GIS]),1) AS Vigor,
      Sum([DBHCLS].[CubicTot]*[ADMIN].[AREA_GIS])/{$Area} AS CubicTot,
      Sum([DBHCLS].[CubicGrs]*[ADMIN].[AREA_GIS])/{$Area} AS CubicGrs,
      Sum([DBHCLS].[CubicNet]*[ADMIN].[AREA_GIS])/{$Area} AS CubicNet,
      Sum([DBHCLS].[BoardGrs]*[ADMIN].[AREA_GIS])/{$Area} AS BoardGrs,
      Sum([DBHCLS].[BoardNet]*[ADMIN].[AREA_GIS])/{$Area} AS BoardNet,
      Sum([DBHCLS].[ValueGrs]*[ADMIN].[AREA_GIS])/{$Area} AS ValueGrs,
      Sum([DBHCLS].[DEFECT_B]*[ADMIN].[AREA_GIS])/Sum([ADMIN].[AREA_GIS]) AS Defect_B,
      Sum([DBHCLS].[DEFECT_M]*[ADMIN].[AREA_GIS])/Sum([ADMIN].[AREA_GIS]) AS Defect_M,
      Sum([DBHCLS].[DEFECT_T]*[ADMIN].[AREA_GIS])/Sum([ADMIN].[AREA_GIS]) AS Defect_T,
      Round([DBHCLS].[DBH]) AS DBHCls,
      Sum([DBHCLS].[Cords]*[ADMIN].[AREA_GIS])/{$Area} AS Cords,
      Sum([DBHCLS].[StemDry]*[ADMIN].[AREA_GIS])/{$Area} AS StemDry,
      Sum([DBHCLS].[StemWet]*[ADMIN].[AREA_GIS])/{$Area} AS StemWet,
      Sum([DBHCLS].[BoleDry]*[ADMIN].[AREA_GIS])/{$Area} AS BoleDry,
      Sum([DBHCLS].[BoleWet]*[ADMIN].[AREA_GIS])/{$Area} AS BoleWet,
      Sum([DBHCLS].[BarkDry]*[ADMIN].[AREA_GIS])/{$Area} AS BarkDry,
      Sum([DBHCLS].[CrwnDry]*[ADMIN].[AREA_GIS])/{$Area} AS CrwnDry,
      Sum([DBHCLS].[RootDry]*[ADMIN].[AREA_GIS])/{$Area} AS RootDry,
      Sum([DBHCLS].[CarbTree]*[ADMIN].[AREA_GIS])/{$Area} AS CarbTree,
      Sum([DBHCLS].[CarbBole]*[ADMIN].[AREA_GIS])/{$Area} AS CarbBole,
      Sum([DBHCLS].[CO2Tree]*[ADMIN].[AREA_GIS])/{$Area} AS CO2Tree,
      Sum([DBHCLS].[CO2Bole]*[ADMIN].[AREA_GIS])/{$Area} AS CO2Bole
    FROM
      ADMIN INNER JOIN DBHCLS ON (ADMIN.RPT_YR = DBHCLS.RPT_YR) AND (ADMIN.STD_ID = DBHCLS.STD_ID)
    WHERE
      ADMIN.FLAG>0 AND ADMIN.Veg_Lbl = '{$Lbl}' AND ADMIN.MSMT_YR > 0
    GROUP BY
      DBHCLS.SPECIES,
      DBHCLS.GRP,
      Round([DBHCLS].[DBH]),
      ADMIN.RPT_YR;";

    $DCLS = null;

    foreach($dbh->query($SQL, PDO::FETCH_ASSOC) as $row) {
      $DCLS[] = $row;
    }

    echo "The FSD Penguin has averaged {$Lbl} to expand...\n\n";

    $SQL = "SELECT STD_ID FROM ADMIN WHERE FLAG > 0 AND MSMT_YR = 0 AND VEG_LBL = '$Lbl';";

    echo "The following stands have been expanded:\n";
    $Cnt = 0;

    foreach($dbh->query($SQL, PDO::FETCH_ASSOC) as $row) {

      $PT = 0;

      $dbh->query('DELETE * FROM DBHCLS WHERE STD_ID = ' . $row['STD_ID']);
      $dbh->query('DELETE * FROM STAND WHERE STD_ID = ' . $row['STD_ID']);
      $dbh->query('INSERT INTO STAND (STD_ID, RPT_YR) VALUES (' . $row['STD_ID'] . ',' . $DCLS[0]['Rpt_YR'] . ')');
      $dbh->query("UPDATE ADMIN SET RPT_YR = {$DCLS[0]['Rpt_YR']} WHERE STD_ID = {$row['STD_ID']}");

      foreach($DCLS as $Rec) {

        $PT++;
        $Fields = null;
        $Values = null;
        $Fields[] = 'STD_ID';
        $Values[] = $row['STD_ID'];
        $Fields[] = 'PlotTree';
        $Values[] = $PT;

        foreach($Rec as $Field => $Value) {
          $Fields[] = $Field;
          if(in_array($Field,array('Species','Grp'))) {
            $Values[] = "'$Value'";
          } else {
            if($Value == '') { $Values[] = 0; } else { $Values[] = $Value;}
          }
        }
        $SQLInsert = 'INSERT INTO DBHCLS (`' . implode('`,`', $Fields) . '`) VALUES (' . implode(',', $Values) . ');';
        $dbh->query($SQLInsert);
      }
      $Cnt++;
      echo $Lbl . "\t -> \t " . $row['STD_ID'] . "\t Stand Counter:$Cnt \t DBHCLS Recs Appended:$PT \n";
    }
  }

  $dbh = null;

?>