# RiverWare_Ruleset 4.6.6 Patch
# Created 10:02 December 21, 2005
# 
RULESET
AGENDA_ORDER DESCENDING;
DESCRIPTION "";
PRECISION   8;
BEGIN
  POLICY_GROUP   "DETERMINE YEAR HYDROGRAPHS ON JAN 1";
  DESCRIPTION    "";
  ACTIVE         TRUE;
  BEGIN
    RULE                 "NPS_q from hydrograph";
    DESCRIPTION          "RULE SETS NPS_Q EQUAL TO USER PROVIDED INPUT HYDROGRAPH, IF PROVIDING ANYTHING BEYOND 300 CFS
enter zero in column MxMPRel_NPS of FlowFlags table for minimum nps demand of 300 or any other number to limited the release that NPS can draw thru Morrow Pt.:
  suggest number like 999999 if no limit.";
    ACTIVE               TRUE;
    RULE_EXEC_CONSTRAINT @"Current Timestep" == @"24:00:00 January 1, Current Year";
    BEGIN
      FOREACH (DATETIME date IN @"Current Timestep" TO @"24:00:00 December 31, Current Year") DO
            $ "RulesData.NPS_q" [date] := IF ( $ "RulesData.FlowFlags" [0.00000000, "MxMPRel_NPS"] == 0.00000000 ["cfs"] )
   THEN
      300.00000000 ["cfs"]
   ELSE
      $ "RulesData.NPS_hydrograph" [date]
   ENDIF;
      ENDFOREACH;
    END;
    RULE                 "FWS_q from hydrograph";
    DESCRIPTION          "RULE SET FWS_Q EQUAL TO HYDROGRAPH IF PROVIDING ANYTHING BEYOND 0 CFS
enter zero in column MxMPRel_FWS of FlowFlags table for minimum fws demand of 0 or any other number to limited the release that FWS can draw thru Morrow Pt.:
  suggest number like 999999 if no limit.";
    ACTIVE               TRUE;
    RULE_EXEC_CONSTRAINT @"Current Timestep" == @"24:00:00 January 1, Current Year";
    BEGIN
      FOREACH (DATETIME date IN @"Current Timestep" TO @"24:00:00 December 31, Current Year") DO
            $ "RulesData.FWS_q_in_BC" [date] := IF ( $ "RulesData.FlowFlags" [0.00000000, "MxMPRel_FWS"] == 0.00000000 ["cfs"] )
   THEN
      0.00000000 ["cfs"]
   ELSE
      $ "RulesData.FWS_Hydrograph_in_BC" [date]
   ENDIF;
      ENDFOREACH;
    END;
    RULE                 "Maximum NPS_q";
    DESCRIPTION          "RULE LIMIT AMOUNT RELEASES FOR NPS THRU MORROW PT
enter zero in columnMXMPRel_NPS of FlowFlags table for minimum nps demand of 300 or any other number to limited the release that NPS can draw thru Morrow Pt.:
  suggest number like 999999 if no limit.";
    ACTIVE               TRUE;
    RULE_EXEC_CONSTRAINT @"Current Timestep" == @"24:00:00 January 1, Current Year";
    BEGIN
      FOREACH (DATETIME date IN @"Current Timestep" TO @"24:00:00 December 31, Current Year") DO
            $ "RulesData.NPS_max_q" [date] := IF ( $ "RulesData.FlowFlags" [0.00000000, "MxMPRel_NPS"] == 0.00000000 ["cfs"] )
   THEN
      300.00000000 ["cfs"]
   ELSE
      "Min"( $ "RulesData.NPS_q" [date], $ "RulesData.FlowFlags" [0.00000000, "MxMPRel_NPS"] + $ "MPtoCRY.Inflow2" [date] - $ "GunnisonTunnel.Diversion" [date] )
   ENDIF;
      ENDFOREACH;
    END;
    RULE                 "Maximum FWS_q";
    DESCRIPTION          "RULE LIMIT AMOUNT RELEASES FOR FWS THRU MORROW PT
enter zero in columnMXMPRel_FWS of FlowFlags table for minimum fws demand of 0 or any other number to limited the release that FWS can draw thru Morrow Pt.:
  suggest number like 999999 if no limit.";
    ACTIVE               TRUE;
    RULE_EXEC_CONSTRAINT @"Current Timestep" == @"24:00:00 January 1, Current Year";
    BEGIN
      FOREACH (DATETIME date IN @"Current Timestep" TO @"24:00:00 December 31, Current Year") DO
            $ "RulesData.FWS_max_q" [date] := IF ( $ "RulesData.FlowFlags" [0.00000000, "MxMPRel_FWS"] == 0.00000000 ["cfs"] )
   THEN
      0.00000000 ["cfs"]
   ELSE
      "Min"( $ "RulesData.FWS_q_in_BC" [date], $ "RulesData.FlowFlags" [0.00000000, "MxMPRel_FWS"] + $ "MPtoCRY.Inflow2" [date] - $ "GunnisonTunnel.Diversion" [date] )
   ENDIF;
      ENDFOREACH;
    END;
    RULE                 "Trout hydrograph all forecast periods";
    DESCRIPTION          "Build the minimum daily trout demand time series in the canyon for current timestep to end of forecast period from a list.  This daily hydrograph is used to forecast the amount of water needed in the canyon for the trout criteria.";
    ACTIVE               TRUE;
    RULE_EXEC_CONSTRAINT @"Current Timestep" IN { @"24:00:00 January 1, Current Year" , @"24:00:00 April 1, Current Year" , @"24:00:00 April 15, Current Year" , @"24:00:00 May 1, Current Year" , @"24:00:00 May 15, Current Year" , @"24:00:00 June 15, Current Year" , @"24:00:00 August 1, Current Year" , @"24:00:00 October 15, Current Year" , @"24:00:00 November 1, Current Year" , @"24:00:00 November 15, Current Year" };
    BEGIN
      PRINT "Build DateAndValue list"(  );
      FOREACH (LIST DateAndValue IN "Build DateAndValue list"(  )) DO
            $ "RulesData.Trout_q" [GET DATETIME @INDEX 0.00000000 FROM DateAndValue] := GET NUMERIC @INDEX 1.00000000 FROM DateAndValue;
      ENDFOREACH;
    END;
    RULE                 "BM Diversion Hydrograph";
    DESCRIPTION          "Create daily diversion demand hydrograph for forecasting pump demands from Blue Mesa.  At this time it is a constant demand.";
    ACTIVE               TRUE;
    RULE_EXEC_CONSTRAINT @"Current Timestep" == @"24:00:00 January 1, Current Year";
    BEGIN
      FOREACH (DATETIME date IN @"Current Timestep" TO @"24:00:00 December 31, Current Year") DO
            $ "BM_Diversion.Diversion Request" [date] := $ "DIVERSION_DATA.BMPumpDiversion" [0.00000000, "FlowRequest"];
      ENDFOREACH;
    END;
  END;
  POLICY_GROUP   "DETERMINE OPERATING PARAMETERS";
  DESCRIPTION    "";
  ACTIVE         TRUE;
  BEGIN
    RULE                 "Compute Prev 3-day averages";
    DESCRIPTION          "";
    ACTIVE               TRUE;
    RULE_EXEC_CONSTRAINT TRUE;
    BEGIN
      $ "3DayAvg.BMUnregInflow" [] := IF ( @"Current Timestep" > @"Start Timestep + 2 Timesteps" )
   THEN
      "SumSlot"( $ "RESERVOIR_DATA.BLUE_MESA_DLY_UNREG_INFLOW", @"Current Timestep - 3 Timesteps", @"Previous Timestep" ) / 3.00000000
   ELSE
      $ "RESERVOIR_DATA.BLUE_MESA_DLY_UNREG_INFLOW" []
   ENDIF;
      $ "3DayAvg.MP_CR_SideInflow" [] := IF ( @"Current Timestep" > @"Start Timestep + 2 Timesteps" )
   THEN
      ( "SumSlot"( $ "BMtoMP.Inflow2", @"Current Timestep - 3 Timesteps", @"Previous Timestep" ) + "SumSlot"( $ "MPtoCRY.Inflow2", @"Current Timestep - 3 Timesteps", @"Previous Timestep" ) ) / 3.00000000
   ELSE
      $ "BMtoMP.Inflow2" [] + $ "MPtoCRY.Inflow2" []
   ENDIF;
      $ "3DayAvg.GunnisonTunnel" [] := IF ( @"Current Timestep" > @"Start Timestep + 2 Timesteps" )
   THEN
      "SumSlot"( $ "GunnisonTunnel.Diversion", @"Current Timestep - 3 Timesteps", @"Previous Timestep" ) / 3.00000000
   ELSE
      $ "GunnisonTunnel.Diversion" []
   ENDIF;
      $ "3DayAvg.GT_to_RPCC" [] := IF ( @"Current Timestep" > @"Start Timestep + 2 Timesteps" )
   THEN
      ( "SumSlot"( $ "GunnisonAtRedlands.Inflow", @"Current Timestep - 3 Timesteps", @"Previous Timestep" ) - "SumSlot"( $ "Crystal_to_GunnisonTunnel.Outflow", @"Current Timestep - 3 Timesteps", @"Previous Timestep" ) ) / 3.00000000
   ELSE
      $ "GunnisonUncompaghre.Inflow2" [] + $ "GTtoDelta.Local Inflow" [] + $ "DeltaToWhitewater.Local Inflow" []
   ENDIF;
    END;
    RULE                 "Estimated Crystal Bypass - Spring Volume";
    DESCRIPTION          "Estimated the amount of Blue Mesa release that will result in a bypass at Crystal powerplant during Apr-Jun 
assuming BM has a july target of 810,000 af.
";
    ACTIVE               TRUE;
    RULE_EXEC_CONSTRAINT "GetMonth"( @"Current Timestep" ) >= 4.00000000 AND "GetMonth"( @"Current Timestep" ) <= 6.00000000;
    BEGIN
      $ "RulesDataOut.EstCrystalBypassVolume" [] := IF ( "GetDayOfMonth"( @"Current Timestep" ) IN { 1.00000000 ["day"] , 8.00000000 ["day"] , 15.00000000 ["day"] , 22.00000000 ["day"] } )
   THEN
      "BMVolumetoRelease"(  ) - "BMDiverRemainingDmd"(  ) - "Max"( $ "Crystal.Max Turbine Q" [0.00000000, 1.00000000] * ( @"24:00:00 August 1, Current Year" - @"Current Timestep" ) - "BMtoCRRemainingInflow"(  ), 0.00000000 ["acre-ft"] )
   ELSE
      $ "RulesDataOut.EstCrystalBypassVolume" [@"Previous Timestep"]
   ENDIF;
    END;
    RULE                 "Additional q to meet flow blw RPC fish ladder 1day lag";
    DESCRIPTION          "RulesData.FlowFlags [0,TargetBelowRPC]    TargetBelowRPC must be 0 or 100 cfs for this to work correctly ( I think).  
Rule releases water for fish ladder in 25 cfs increments up to 100 cfs.  apr-sep
";
    ACTIVE               FALSE;
    RULE_EXEC_CONSTRAINT TRUE;
    BEGIN
      $ "RulesData.Q_blw_RPC_adj" [] := IF ( @"Current Timestep" == @"Start Timestep" OR ( @"Current Timestep" == @"Start Timestep + 1 Timesteps" OR @"Current Timestep" == @"Start Timestep + 2 Timesteps" ) )
   THEN
      0.00000000 ["cfs"]
   ELSE
      IF ( "GetMonth"( @"Current Timestep" ) <= 3.00000000 OR "GetMonth"( @"Current Timestep" ) >= 10.00000000 OR $ "RulesData.FlowFlags" [0.00000000, "TargetBelowRPC"] == 0.00000000 ["cfs"] )
      THEN
         0.00000000 ["cfs"]
      ELSE
         IF ( ( ( $ "GunnisonAtRedlands.Outflow" [@"Current Timestep - 2 Timesteps"] - $ "RulesData.Q_blw_RPC_adj" [@"Current Timestep - 3 Timesteps"] ) + ( $ "GunnisonAtRedlands.Outflow" [@"Previous Timestep"] - $ "RulesData.Q_blw_RPC_adj" [@"Current Timestep - 2 Timesteps"] ) ) / 2.00000000 >= 100.00000000 ["cfs"] )
         THEN
            0.00000000 ["cfs"]
         ELSE
            IF ( ( ( $ "GunnisonAtRedlands.Outflow" [@"Current Timestep - 2 Timesteps"] - $ "RulesData.Q_blw_RPC_adj" [@"Current Timestep - 3 Timesteps"] ) + ( $ "GunnisonAtRedlands.Outflow" [@"Previous Timestep"] - $ "RulesData.Q_blw_RPC_adj" [@"Current Timestep - 2 Timesteps"] ) ) / 2.00000000 >= 75.00000000 ["cfs"] )
            THEN
               25.00000000 ["cfs"]
            ELSE
               IF ( ( ( $ "GunnisonAtRedlands.Outflow" [@"Current Timestep - 2 Timesteps"] - $ "RulesData.Q_blw_RPC_adj" [@"Current Timestep - 3 Timesteps"] ) + ( $ "GunnisonAtRedlands.Outflow" [@"Previous Timestep"] - $ "RulesData.Q_blw_RPC_adj" [@"Current Timestep - 2 Timesteps"] ) ) / 2.00000000 >= 50.00000000 ["cfs"] )
               THEN
                  50.00000000 ["cfs"]
               ELSE
                  IF ( ( ( $ "GunnisonAtRedlands.Outflow" [@"Current Timestep - 2 Timesteps"] - $ "RulesData.Q_blw_RPC_adj" [@"Current Timestep - 3 Timesteps"] ) + ( $ "GunnisonAtRedlands.Outflow" [@"Previous Timestep"] - $ "RulesData.Q_blw_RPC_adj" [@"Current Timestep - 2 Timesteps"] ) ) / 2.00000000 >= 25.00000000 ["cfs"] )
                  THEN
                     75.00000000 ["cfs"]
                  ELSE
                     IF ( ( ( $ "GunnisonAtRedlands.Outflow" [@"Current Timestep - 2 Timesteps"] - $ "RulesData.Q_blw_RPC_adj" [@"Current Timestep - 3 Timesteps"] ) + ( $ "GunnisonAtRedlands.Outflow" [@"Previous Timestep"] - $ "RulesData.Q_blw_RPC_adj" [@"Current Timestep - 2 Timesteps"] ) ) / 2.00000000 >= 0.00000000 ["cfs"] )
                     THEN
                        100.00000000 ["cfs"]
                     ELSE
                        100.00000000 ["cfs"]
                     ENDIF
                  ENDIF
               ENDIF
            ENDIF
         ENDIF
      ENDIF
   ENDIF;
    END;
    RULE                 "Additional q to meet flow blw RPC fish ladder 2day lag";
    DESCRIPTION          "RulesData.FlowFlags [0,TargetBelowRPC]    TargetBelowRPC must be 0 or 100 cfs for this to work correctly ( I think).  
Rule releases water for fish ladder in 25 cfs increments up to 100 cfs.  apr-sep
";
    ACTIVE               TRUE;
    RULE_EXEC_CONSTRAINT TRUE;
    BEGIN
      $ "RulesData.Q_blw_RPC_adj" [] := IF ( @"Current Timestep" == @"Start Timestep" OR ( @"Current Timestep" == @"Start Timestep + 1 Timesteps" OR @"Current Timestep" == @"Start Timestep + 2 Timesteps" ) )
   THEN
      0.00000000 ["cfs"]
   ELSE
      IF ( "GetMonth"( @"Current Timestep" ) <= 3.00000000 OR "GetMonth"( @"Current Timestep" ) >= 10.00000000 OR $ "RulesData.FlowFlags" [0.00000000, "TargetBelowRPC"] == 0.00000000 ["cfs"] )
      THEN
         0.00000000 ["cfs"]
      ELSE
         IF ( ( ( $ "GunnisonAtRedlands.Outflow" [@"Current Timestep - 2 Timesteps"] - $ "RulesData.Q_blw_RPC_adj" [@"Current Timestep - 4 Timesteps"] ) + ( $ "GunnisonAtRedlands.Outflow" [@"Previous Timestep"] - $ "RulesData.Q_blw_RPC_adj" [@"Current Timestep - 3 Timesteps"] ) ) / 2.00000000 >= 100.00000000 ["cfs"] )
         THEN
            0.00000000 ["cfs"]
         ELSE
            IF ( ( ( $ "GunnisonAtRedlands.Outflow" [@"Current Timestep - 2 Timesteps"] - $ "RulesData.Q_blw_RPC_adj" [@"Current Timestep - 4 Timesteps"] ) + ( $ "GunnisonAtRedlands.Outflow" [@"Previous Timestep"] - $ "RulesData.Q_blw_RPC_adj" [@"Current Timestep - 3 Timesteps"] ) ) / 2.00000000 >= 75.00000000 ["cfs"] )
            THEN
               25.00000000 ["cfs"]
            ELSE
               IF ( ( ( $ "GunnisonAtRedlands.Outflow" [@"Current Timestep - 2 Timesteps"] - $ "RulesData.Q_blw_RPC_adj" [@"Current Timestep - 4 Timesteps"] ) + ( $ "GunnisonAtRedlands.Outflow" [@"Previous Timestep"] - $ "RulesData.Q_blw_RPC_adj" [@"Current Timestep - 3 Timesteps"] ) ) / 2.00000000 >= 50.00000000 ["cfs"] )
               THEN
                  50.00000000 ["cfs"]
               ELSE
                  IF ( ( ( $ "GunnisonAtRedlands.Outflow" [@"Current Timestep - 2 Timesteps"] - $ "RulesData.Q_blw_RPC_adj" [@"Current Timestep - 4 Timesteps"] ) + ( $ "GunnisonAtRedlands.Outflow" [@"Previous Timestep"] - $ "RulesData.Q_blw_RPC_adj" [@"Current Timestep - 3 Timesteps"] ) ) / 2.00000000 >= 25.00000000 ["cfs"] )
                  THEN
                     75.00000000 ["cfs"]
                  ELSE
                     IF ( ( ( $ "GunnisonAtRedlands.Outflow" [@"Current Timestep - 2 Timesteps"] - $ "RulesData.Q_blw_RPC_adj" [@"Current Timestep - 4 Timesteps"] ) + ( $ "GunnisonAtRedlands.Outflow" [@"Previous Timestep"] - $ "RulesData.Q_blw_RPC_adj" [@"Current Timestep - 3 Timesteps"] ) ) / 2.00000000 >= 0.00000000 ["cfs"] )
                     THEN
                        100.00000000 ["cfs"]
                     ELSE
                        100.00000000 ["cfs"]
                     ENDIF
                  ENDIF
               ENDIF
            ENDIF
         ENDIF
      ENDIF
   ENDIF;
    END;
    RULE                 "Additional q to meet flow below RPC300 WW1050";
    DESCRIPTION          "RulesData.FlowFlags [0,TargetBelowRPC]    TargetBelowRPC must be 0 or 300 cfs for this to work correctly ( I think).  
Rule which set RPC avail for diversion use hardwired values for GunnisonAtRedlands which must change is anything but 0 or 300 is goal below RPC.";
    ACTIVE               FALSE;
    RULE_EXEC_CONSTRAINT TRUE;
    BEGIN
      $ "RulesData.Q_blw_RPC_adj" [] := IF ( @"Current Timestep" == @"Start Timestep" OR ( @"Current Timestep" == @"Start Timestep + 1 Timesteps" OR @"Current Timestep" == @"Start Timestep + 2 Timesteps" ) )
   THEN
      0.00000000 ["cfs"]
   ELSE
      IF ( $ "RulesData.FWS_Rcmd_Q_WW" [] <= 1049.00000000 ["cfs"] OR $ "RulesData.FlowFlags" [0.00000000, "TargetBelowRPC"] == 0.00000000 ["cfs"] )
      THEN
         0.00000000 ["cfs"]
      ELSE
         IF ( ( ( $ "GunnisonAtRedlands.Outflow" [@"Current Timestep - 2 Timesteps"] - $ "RulesData.Q_blw_RPC_adj" [@"Current Timestep - 3 Timesteps"] ) + ( $ "GunnisonAtRedlands.Outflow" [@"Previous Timestep"] - $ "RulesData.Q_blw_RPC_adj" [@"Current Timestep - 2 Timesteps"] ) ) / 2.00000000 >= 275.00000000 ["cfs"] )
         THEN
            0.00000000 ["cfs"]
         ELSE
            IF ( ( ( $ "GunnisonAtRedlands.Outflow" [@"Current Timestep - 2 Timesteps"] - $ "RulesData.Q_blw_RPC_adj" [@"Current Timestep - 3 Timesteps"] ) + ( $ "GunnisonAtRedlands.Outflow" [@"Previous Timestep"] - $ "RulesData.Q_blw_RPC_adj" [@"Current Timestep - 2 Timesteps"] ) ) / 2.00000000 >= 225.00000000 ["cfs"] )
            THEN
               50.00000000 ["cfs"]
            ELSE
               IF ( ( ( $ "GunnisonAtRedlands.Outflow" [@"Current Timestep - 2 Timesteps"] - $ "RulesData.Q_blw_RPC_adj" [@"Current Timestep - 3 Timesteps"] ) + ( $ "GunnisonAtRedlands.Outflow" [@"Previous Timestep"] - $ "RulesData.Q_blw_RPC_adj" [@"Current Timestep - 2 Timesteps"] ) ) / 2.00000000 >= 175.00000000 ["cfs"] )
               THEN
                  100.00000000 ["cfs"]
               ELSE
                  IF ( ( ( $ "GunnisonAtRedlands.Outflow" [@"Current Timestep - 2 Timesteps"] - $ "RulesData.Q_blw_RPC_adj" [@"Current Timestep - 3 Timesteps"] ) + ( $ "GunnisonAtRedlands.Outflow" [@"Previous Timestep"] - $ "RulesData.Q_blw_RPC_adj" [@"Current Timestep - 2 Timesteps"] ) ) / 2.00000000 >= 125.00000000 ["cfs"] )
                  THEN
                     150.00000000 ["cfs"]
                  ELSE
                     IF ( ( ( $ "GunnisonAtRedlands.Outflow" [@"Current Timestep - 2 Timesteps"] - $ "RulesData.Q_blw_RPC_adj" [@"Current Timestep - 3 Timesteps"] ) + ( $ "GunnisonAtRedlands.Outflow" [@"Previous Timestep"] - $ "RulesData.Q_blw_RPC_adj" [@"Current Timestep - 2 Timesteps"] ) ) / 2.00000000 >= 75.00000000 ["cfs"] )
                     THEN
                        200.00000000 ["cfs"]
                     ELSE
                        IF ( ( ( $ "GunnisonAtRedlands.Outflow" [@"Current Timestep - 2 Timesteps"] - $ "RulesData.Q_blw_RPC_adj" [@"Current Timestep - 3 Timesteps"] ) + ( $ "GunnisonAtRedlands.Outflow" [@"Previous Timestep"] - $ "RulesData.Q_blw_RPC_adj" [@"Current Timestep - 2 Timesteps"] ) ) / 2.00000000 >= 25.00000000 ["cfs"] )
                        THEN
                           250.00000000 ["cfs"]
                        ELSE
                           300.00000000 ["cfs"]
                        ENDIF
                     ENDIF
                  ENDIF
               ENDIF
            ENDIF
         ENDIF
      ENDIF
   ENDIF;
    END;
    RULE                 "Compute max BM flood content";
    DESCRIPTION          "Determine the maximum allowable BM content using the Flood Control Diagram";
    ACTIVE               TRUE;
    RULE_EXEC_CONSTRAINT @"Current Timestep" >= @"24:00:00 April 1, Current Year" AND @"Current Timestep" <= @"24:00:00 July 15, Current Year";
    BEGIN
      $ "RulesDataOut.BMMaxFldCtrlCont" [] := "TableInterpolation"( $ "FloodControlTables.BMFldCtrlContvsInflow", 0.00000000, "GetDayOfYear"( @"Current Timestep" ) - "GetDayOfYear"( @"24:00:00 March 1, Current Year" ), "Max"( 0.00000000, "RowWithMaxBMFloodContent"(  ) ), @"Current Timestep" );
    END;
    RULE                 "Est Fall Crystal Target Rel if Jun or Jul: Jun less";
    DESCRIPTION          "Increases operational release during June and July if estimated operation release during the fall season exceeds current computed operation release.  This rule prevents reducing releases in june and july to fill BM and then going to Full powerplant operation  during late summer and fall to meet the end of december target.
";
    ACTIVE               TRUE;
    RULE_EXEC_CONSTRAINT TRUE;
    BEGIN
      $ "RulesData.EstFallCryTargetOperRelease" [] := IF ( @"Current Timestep" >= @"24:00:00 June 15, Current Year" AND @"Current Timestep" < @"24:00:00 July 31, Current Year" )
   THEN
      IF ( "GetDayOfMonth"( @"Current Timestep" ) IN { 1.00000000 ["day"] , 8.00000000 ["day"] , 15.00000000 ["day"] , 22.00000000 ["day"] } )
      THEN
         IF ( "GetMonth"( @"Current Timestep" ) == 6.00000000 )
         THEN
            "EarlyEstFallOpRelCFS"(  ) * 0.75000000
         ELSE
            "EarlyEstFallOpRelCFS"(  )
         ENDIF
      ELSE
         $ "RulesData.EstFallCryTargetOperRelease" [@"Previous Timestep"]
      ENDIF
   ELSE
      0.00000000 ["cfs"]
   ENDIF;
    END;
  END;
  POLICY_GROUP   "OPERATING RULES";
  DESCRIPTION    "";
  ACTIVE         TRUE;
  BEGIN
    RULE                 "Blue Mesa Bypass for RPCC";
    DESCRIPTION          "Sets BM minimum release to prevent storing water when the Redlands Diversion of 750 is not met.
";
    ACTIVE               TRUE;
    RULE_EXEC_CONSTRAINT TRUE;
    BEGIN
      $ "RulesData.BMBypassForRPCC" [] := IF ( @"Current Timestep" > @"Start Timestep + 2 Timesteps" )
   THEN
      "Max"( "Min"( 750.00000000 ["cfs"] - $ "3DayAvg.GT_to_RPCC" [] - "Max"( $ "3DayAvg.MP_CR_SideInflow" [] - $ "3DayAvg.GunnisonTunnel" [], 0.00000000 ["cfs"] ), $ "3DayAvg.BMUnregInflow" [] - "Max"( $ "3DayAvg.GunnisonTunnel" [] - $ "3DayAvg.MP_CR_SideInflow" [], 0.00000000 ["cfs"] ) ), 0.00000000 ["cfs"] )
   ELSE
      0.00000000 ["cfs"]
   ENDIF;
    END;
    RULE                 "BlueMesaNearlyFullIncreaseRelease";
    DESCRIPTION          "If Blue Mesa above 7505 feet , check to see if short term forecast or the rate of rise requires additional release from Blue Mesa.";
    ACTIVE               TRUE;
    RULE_EXEC_CONSTRAINT TRUE;
    BEGIN
      $ "BlueMesa.Outflow" [] := IF ( $ "BlueMesa.Pool Elevation" [@"Previous Timestep"] >= $ "ReservoirData.MaxElevation" [2.00000000, 0.00000000] - 14.40000000 ["ft"] )
   THEN
      "Max"( "BMMaxElevRelease"(  ), "Max"( 0.00000000 ["cfs"], "Min"( "GetMaxOutflowGivenInflow"( % "BlueMesa", $ "BlueMesa.Inflow" [], @"Current Timestep" ), "Max"( "Max"( "BMReleaseByRateofRise"(  ), "BMShortTermRel"(  ) ), "Max"( $ "RulesData.BMFloodCntrlRelease" [], "Max"( $ "RulesData.BlueMesaTargetRelease" [], $ "RulesData.BMBypassForRPCC" [] ) ) ) + $ "RulesData.Q_blw_RPC_adj" [] - "DeltaFldAdj"(  ) ) ) )
   ENDIF;
    END;
    RULE                 "Limit BM by Cry PP in Jan-Mar";
    DESCRIPTION          "Prevents releases at Blue Mesa which would result in bypass of Crystal powerplant Jan-Mar";
    ACTIVE               TRUE;
    RULE_EXEC_CONSTRAINT TRUE;
    BEGIN
      $ "BlueMesa.Outflow" [] := IF ( "GetMonth"( @"Current Timestep" ) <= 3.00000000 )
   THEN
      IF ( $ "Crystal.Outflow" [] > $ "Crystal.Max Turbine Q" [0.00000000, 1.00000000] )
      THEN
         $ "Crystal.Max Turbine Q" [0.00000000, 1.00000000] - ( $ "BMtoMP.Inflow2" [] + $ "MPtoCRY.Inflow2" [] )
      ENDIF
   ENDIF;
    END;
    RULE                 "Setting Crystal operation target release";
    DESCRIPTION          "Determine operational release base on current conditions and projected demands.  Operation release is a projection of water which needs to be release in excess of demands: downstream senior water rights, canyon minimums, etc using forecasts and BM end of forecast period targets.";
    ACTIVE               TRUE;
    RULE_EXEC_CONSTRAINT TRUE;
    BEGIN
      $ "RulesData.CrystalTargetOperRelease" [] := IF ( "GetDayOfMonth"( @"Current Timestep" ) IN { 1.00000000 ["day"] , 8.00000000 ["day"] , 15.00000000 ["day"] , 22.00000000 ["day"] } )
   THEN
      "Max"( "CROperReleaseinCFS"(  ), $ "RulesData.EstFallCryTargetOperRelease" [] )
   ELSE
      $ "RulesData.CrystalTargetOperRelease" [@"Previous Timestep"]
   ENDIF;
    END;
    RULE                 "Setting Blue Mesa target release";
    DESCRIPTION          "Set Blue Mesa target release based on current day downstream demand and using ramping for the canyon, unless flood adjustment is needed.
If flood adjustment needed, set Blue Mesa release to tunnel + yesterday canyon flow - side inflow to MP and CR.";
    ACTIVE               TRUE;
    RULE_EXEC_CONSTRAINT TRUE;
    BEGIN
      $ "RulesData.BlueMesaTargetRelease" [] := IF ( "DeltaFldAdj"(  ) < 10.00000000 ["cfs"] )
   THEN
      "Max"( "Min"( "CanyonMaxSafety&Trout"(  ), "Max"( "CanyonMinSafety&Trout"(  ), "Max"( "Max"( $ "RulesData.SpringPeakRelease" [], "Max"( $ "RulesData.FWS_max_q" [], $ "RulesData.NPS_max_q" [] ) ), $ "RulesData.CrystalTargetOperRelease" [] + "Max"( 300.00000000 ["cfs"], "CanyonMinTroutTarget"(  ) ) ) ) ) + $ "Crystal_to_GunnisonTunnel.Diversion" [] - $ "BMtoMP.Inflow2" [] - $ "MPtoCRY.Inflow2" [], 0.00000000 ["cfs"] )
   ELSE
      $ "Crystal_to_GunnisonTunnel.Outflow" [@"Previous Timestep"] + $ "Crystal_to_GunnisonTunnel.Diversion" [] - ( $ "BMtoMP.Inflow2" [] + $ "MPtoCRY.Inflow2" [] )
   ENDIF;
    END;
    RULE                 "Blue Mesa Flood Control Release";
    DESCRIPTION          "Set  a BM release based on Flood control diagram if BM in violation by more than 1000 af.  Allowable ramping rates can be exceed by 5% if necessary.
";
    ACTIVE               TRUE;
    RULE_EXEC_CONSTRAINT TRUE;
    BEGIN
      $ "RulesData.BMFloodCntrlRelease" [] := IF ( @"Current Timestep" >= @"24:00:00 April 2, Current Year" AND @"Current Timestep" <= @"24:00:00 July 15, Current Year" )
   THEN
      IF ( $ "BlueMesa.Storage" [@"Previous Timestep"] > $ "RulesDataOut.BMMaxFldCtrlCont" [@"Previous Timestep"] + 1000.00000000 ["acre-feet"] )
      THEN
         "Min"( "Max"( $ "Crystal_to_GunnisonTunnel.Diversion" [] + "CanyonMaxSafety&Trout"(  ) * 1.05000000 - $ "BMtoMP.Inflow2" [] - $ "MPtoCRY.Inflow2" [], 0.00000000 ["cfs"] ), $ "BlueMesa.Inflow" [] * 1.15000000 )
      ELSE
         0.00000000 ["cfs"]
      ENDIF
   ELSE
      0.00000000 ["cfs"]
   ENDIF;
    END;
    RULE                 "BM general release rule";
    DESCRIPTION          "Resets Blue Mesa releases based on previous target release with consideration for flooding at Delta and minimum flows below Redlands Power Canal.
";
    ACTIVE               TRUE;
    RULE_EXEC_CONSTRAINT TRUE;
    BEGIN
      $ "BlueMesa.Outflow" [] := "Max"( 0.00000000 ["cfs"], "Min"( "GetMaxOutflowGivenInflow"( % "BlueMesa", $ "BlueMesa.Inflow" [], @"Current Timestep" ), "Max"( $ "RulesData.BMFloodCntrlRelease" [], "Max"( $ "RulesData.BlueMesaTargetRelease" [], $ "RulesData.BMBypassForRPCC" [] ) ) + $ "RulesData.Q_blw_RPC_adj" [] - "DeltaFldAdj"(  ) ) );
    END;
    RULE                 "MPRelease=inflows";
    DESCRIPTION          "Set Morrow Point release equal to Inflow - yesterdays Morrow Point evaporation.  This will keep Morrow Point at constaint elevation.";
    ACTIVE               TRUE;
    RULE_EXEC_CONSTRAINT TRUE;
    BEGIN
      $ "MorrowPoint.Outflow" [] := $ "MorrowPoint.Inflow" [] - "VolumeToFlow"( $ "MorrowPoint.Evaporation" [@"Previous Timestep"], @"Current Timestep" );
    END;
    RULE                 "CRRelease=inflows w/check";
    DESCRIPTION          "Set Crystal release equal to Crystal inflow unless Reservoir is in a spill situation.  If spill situation, set release to maximum possible using Crystal inflow ( inflow cannot equal outflow because of surcharge storage).";
    ACTIVE               FALSE;
    RULE_EXEC_CONSTRAINT TRUE;
    BEGIN
      $ "Crystal.Outflow" [] := IF ( $ "Crystal.Inflow" [] >= "GetMaxOutflowGivenInflow"( % "Crystal", $ "Crystal.Inflow" [], @"Current Timestep" ) OR $ "Crystal.Pool Elevation" [@"Previous Timestep"] >= $ "ReservoirData.MaxElevation" [1.00000000, 0.00000000] )
   THEN
      "GetMaxOutflowGivenInflow"( % "Crystal", $ "Crystal.Inflow" [], @"Current Timestep" )
   ELSE
      $ "Crystal.Inflow" []
   ENDIF;
    END;
    RULE                 "CRRelease=inflows w/check + 10% vol > 16700";
    DESCRIPTION          "Set Crystal release equal to Crystal inflow unless Reservoir is in a spill situation.  If spill situation, set release to maximum possible using Crystal inflow ( inflow cannot equal outflow because of surcharge storage).";
    ACTIVE               TRUE;
    RULE_EXEC_CONSTRAINT TRUE;
    BEGIN
      $ "Crystal.Outflow" [] := IF ( $ "Crystal.Inflow" [] >= "GetMaxOutflowGivenInflow"( % "Crystal", $ "Crystal.Inflow" [], @"Current Timestep" ) OR $ "Crystal.Pool Elevation" [@"Previous Timestep"] >= $ "ReservoirData.MaxElevation" [1.00000000, 0.00000000] )
   THEN
      "GetMaxOutflowGivenInflow"( % "Crystal", $ "Crystal.Inflow" [], @"Current Timestep" )
   ELSE
      IF ( $ "Crystal.Pool Elevation" [@"Previous Timestep"] >= 6752.00000000 ["ft"] )
      THEN
         "Min"( $ "Crystal.Inflow" [] + 50.00000000 ["cfs"], "GetMaxOutflowGivenInflow"( % "Crystal", $ "Crystal.Inflow" [], @"Current Timestep" ) )
      ELSE
         $ "Crystal.Inflow" []
      ENDIF
   ENDIF;
    END;
    RULE                 "Redlands diversion request";
    DESCRIPTION          "Compute Redlands diversion based using physical limitations.";
    ACTIVE               FALSE;
    RULE_EXEC_CONSTRAINT TRUE;
    BEGIN
      $ "RedlandsPowerCanal.Diversion Request" [] := 850.00000000 ["cfs"];
    END;
    RULE                 "Redlands diversion request physical limit";
    DESCRIPTION          "Compute Redlands diversion based using physical limitations.";
    ACTIVE               FALSE;
    RULE_EXEC_CONSTRAINT TRUE;
    BEGIN
      $ "RedlandsPowerCanal.Diversion Request" [] := "Min"( 825.00000000 ["cfs"], "Max"( $ "GunnisonAtRedlands.Inflow" [] * 0.10800000 ["cfs"] / 1.00000000 ["cfs"] + 640.00000000 ["cfs"], 740.00000000 ["cfs"] ) );
    END;
    RULE                 "Water Available to Redland Power Canal";
    DESCRIPTION          "Limit water available to Redland based on FERC permit.";
    ACTIVE               TRUE;
    RULE_EXEC_CONSTRAINT TRUE;
    BEGIN
      $ "RedlandsPowerCanal.Available For Diversion" [] := IF ( $ "GunnisonAtRedlands.Inflow" [] <= 750.00000000 ["cfs"] )
   THEN
      $ "GunnisonAtRedlands.Inflow" [] - $ "RulesData.Q_blw_RPC_adj" [@"Previous Timestep"]
   ELSE
      IF ( $ "GunnisonAtRedlands.Inflow" [] >= 1050.00000000 ["cfs"] )
      THEN
         $ "GunnisonAtRedlands.Inflow" [] - 300.00000000 ["cfs"]
      ELSE
         "Min"( 750.00000000 ["cfs"], $ "GunnisonAtRedlands.Inflow" [] - $ "RulesData.Q_blw_RPC_adj" [@"Previous Timestep"] )
      ENDIF
   ENDIF;
    END;
  END;
  POLICY_GROUP   "Write function value or misc slots";
  DESCRIPTION    "";
  ACTIVE         FALSE;
  BEGIN
    RULE                 "List of functions BM";
    DESCRIPTION          "";
    ACTIVE               FALSE;
    RULE_EXEC_CONSTRAINT TRUE;
    BEGIN
      $ "RulesDataOut.BM releases" [@"Current Timestep", 0.00000000] := $ "BlueMesa.Outflow" [];
      $ "RulesDataOut.BM releases" [@"Current Timestep", 1.00000000] := "BMShortTermRel"(  );
      $ "RulesDataOut.BM releases" [@"Current Timestep", 2.00000000] := "BMReleaseByRateofRise"(  );
      $ "RulesDataOut.BM releases" [@"Current Timestep", 3.00000000] := "BMShortTermRel"(  );
    END;
    RULE                 "mis func & slots";
    DESCRIPTION          "";
    ACTIVE               TRUE;
    RULE_EXEC_CONSTRAINT TRUE;
    BEGIN
      $ "RulesDataOut.short term vol" [] := "BMShortTermVol"(  );
      $ "RulesDataOut.Temp_slot for cfs" [] := "CROperReleaseinCFS"(  );
      $ "RulesDataOut.BM volume to release af" [] := "BMVolumetoRelease"(  );
      $ "RulesDataOut.Series 0 af" [] := "BMStorageTarget"(  );
      $ "RulesDataOut.Series 1 af" [] := "BMOperVolumetoRelease"(  );
    END;
  END;
  POLICY_GROUP   "NO ACTION SPRING PEAK";
  DESCRIPTION    "";
  ACTIVE         TRUE;
  BEGIN
    RULE                 "Spring Peak Release in Canyon";
    DESCRIPTION          "creates a spring peak release if  crystal bypasses are expected.  Activated by identifying dates for releases to occur.
probably don't need ramping check in this function, it will be controlled when actual release made
";
    ACTIVE               TRUE;
    RULE_EXEC_CONSTRAINT TRUE;
    BEGIN
      $ "RulesData.SpringPeakRelease" [] := IF ( @"Current Timestep" < "SpringPeakReleaseStart"(  ) OR @"Current Timestep" >= "SpringPeakReleaseStop"(  ) )
   THEN
      0.00000000 ["cfs"]
   ELSE
      IF ( $ "RulesDataOut.EstCrystalBypassVolume" ["SpringPeakForecastDate"(  )] <= $ "FlowCriteria.NoActionPeak" [1.00000000, 0.00000000] )
      THEN
         0.00000000 ["cfs"]
      ELSE
         IF ( $ "RulesDataOut.EstCrystalBypassVolume" ["SpringPeakForecastDate"(  )] <= $ "FlowCriteria.NoActionPeak" [2.00000000, 0.00000000] )
         THEN
            1000.00000000 ["cfs"] + $ "Crystal.Max Turbine Q" [0.00000000, 1.00000000] - $ "Crystal_to_GunnisonTunnel.Diversion" []
         ELSE
            IF ( $ "RulesDataOut.EstCrystalBypassVolume" ["SpringPeakForecastDate"(  )] <= $ "FlowCriteria.NoActionPeak" [3.00000000, 0.00000000] )
            THEN
               2500.00000000 ["cfs"] + $ "Crystal.Max Turbine Q" [0.00000000, 1.00000000] - $ "Crystal_to_GunnisonTunnel.Diversion" []
            ELSE
               2000.00000000 ["cfs"] + $ "Crystal.Max Turbine Q" [0.00000000, 1.00000000] - $ "Crystal_to_GunnisonTunnel.Diversion" []
            ENDIF
         ENDIF
      ENDIF
   ENDIF;
    END;
  END;
  POLICY_GROUP   "PEAK CR or MP LIMITED; Q & DAYS VARIABLE";
  DESCRIPTION    "";
  ACTIVE         FALSE;
  BEGIN
    RULE                 "Spring Peak Release in Canyon";
    DESCRIPTION          "creates a spring peak release if  crystal bypasses are expected.  Activated by identifying dates for releases to occur.";
    ACTIVE               FALSE;
    RULE_EXEC_CONSTRAINT TRUE;
    BEGIN
      $ "RulesData.SpringPeakRelease" [] := IF ( @"Current Timestep" < "SpringPeakReleaseStart"(  ) OR @"Current Timestep" >= "SpringPeakReleaseEnd"(  ) )
   THEN
      0.00000000 ["cfs"]
   ELSE
      IF ( $ "RulesDataOut.EstCrystalBypassVolume" [@"24:00:00 May 1, Current Year"] <= 0.00000000 ["acre-feet"] OR $ "RulesDataOut.EstCrystalBypassVolume" [@"24:00:00 May 1, Current Year"] > $ "FlowCriteria.SprPeakSurplusRange" [3.00000000, 0.00000000] )
      THEN
         0.00000000 ["cfs"]
      ELSE
         IF ( $ "RulesDataOut.EstCrystalBypassVolume" [@"24:00:00 May 1, Current Year"] <= $ "FlowCriteria.SprPeakSurplusRange" [0.00000000, 0.00000000] )
         THEN
            "Min"( "CanyonMaxSafety&Trout"(  ), 4000.00000000 ["cfs"] - $ "Crystal_to_GunnisonTunnel.Diversion" [] )
         ELSE
            IF ( $ "RulesDataOut.EstCrystalBypassVolume" [@"24:00:00 May 1, Current Year"] <= $ "FlowCriteria.SprPeakSurplusRange" [1.00000000, 0.00000000] )
            THEN
               "Min"( "CanyonMaxSafety&Trout"(  ), 5000.00000000 ["cfs"] - $ "Crystal_to_GunnisonTunnel.Diversion" [] + $ "MPtoCRY.Inflow2" [] )
            ELSE
               "Min"( "CanyonMaxSafety&Trout"(  ), 6500.00000000 ["cfs"] - $ "Crystal_to_GunnisonTunnel.Diversion" [] + $ "MPtoCRY.Inflow2" [] )
            ENDIF
         ENDIF
      ENDIF
   ENDIF;
    END;
  END;
  UTILITY_GROUP "Utility Group1";
  DESCRIPTION   "";
  ACTIVE        TRUE;
  BEGIN
    FUNCTION       "BMStorageTarget" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "Determing Blue Mesa target capacity at end of forecast period.";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      IF ( "GetMonth"( @"Current Timestep" ) < 4.00000000 )
   THEN
      "TableInterpolation"( $ "RulesData.BMMar31StorageTarget", 0.00000000, $ "MiscTable.LookupCol" [], $ "FORECAST_TABLES.BMJanMar4castedAJInflow" [@"Current Timestep", "Get4castYr"(  )], @"Current Timestep" ) + 92.00000000 ["day"] * $ "DIVERSION_DATA.BMPumpDiversion" [0.00000000, 0.00000000]
   ELSE
      IF ( "GetMonth"( @"Current Timestep" ) < 8.00000000 )
      THEN
         810000.00000000 ["acre-ft"]
      ELSE
         580000.00000000 ["acre-ft"]
      ENDIF
   ENDIF;
    END;
    FUNCTION       "TPStorageAdj" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "Expected storage or release of storage from current date until end of forecast period.  Used to change BM unregated forecasts to regulated forecast.";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      IF ( "GetMonth"( @"Current Timestep" ) < 4.00000000 )
   THEN
      $ "RESERVOIR_DATA.TAYLOR_PARK_CONTENTS" [@"24:00:00 March 31, Current Year"] - $ "RESERVOIR_DATA.TAYLOR_PARK_CONTENTS" [@"Previous Timestep"]
   ELSE
      IF ( "GetMonth"( @"Current Timestep" ) < 8.00000000 )
      THEN
         $ "RESERVOIR_DATA.TAYLOR_PARK_CONTENTS" [@"24:00:00 July 31, Current Year"] - $ "RESERVOIR_DATA.TAYLOR_PARK_CONTENTS" [@"Previous Timestep"]
      ELSE
         $ "RESERVOIR_DATA.TAYLOR_PARK_CONTENTS" [@"24:00:00 December 31, Current Year"] - $ "RESERVOIR_DATA.TAYLOR_PARK_CONTENTS" [@"Previous Timestep"]
      ENDIF
   ENDIF;
    END;
    FUNCTION       "BMVolumetoRelease" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "Estimated total volume to release ( includes forecasted inflows) by end of forecast period to achieve end of forecast BM target content.";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      "Max"( 0.00000000 ["acre-ft"], "BMRemainingInflow"(  ) - "BMStorageTarget"(  ) - "TPStorageAdj"(  ) + $ "BlueMesa.Storage" [@"Previous Timestep"] );
    END;
    FUNCTION       "BMCurrent4cast" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      $ "FORECAST_TABLES.BM4castedUnregInflow" [@"Current Timestep", "Get4castYr"(  )];
    END;
    FUNCTION       "Get4castYr" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      $ "FORECAST_TABLES.RunNo" ["GetYear"( @"Current Timestep" ) - 1975.00000000, "GetRunIndex"(  ) - 1.00000000];
    END;
    FUNCTION       "BMRemainingInflow" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "Remaining unreg BM inflow adjusted for tendency to over forecast Apr, May, & Jun.  This is based on looking at forecast over last ~26 years vs actual inflow";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      "BMCurrent4cast"(  ) - "BMAccumUnregInflow"(  );
    END;
    FUNCTION       "BMAccumUnregInflow" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      IF ( "GetMonth"( @"Current Timestep" ) < 4.00000000 )
   THEN
      IF ( @"Current Timestep" == @"24:00:00 January 1, Current Year" )
      THEN
         0.00000000 ["acre-ft"]
      ELSE
         "SumFlowsToVolume"( $ "RESERVOIR_DATA.BLUE_MESA_DLY_UNREG_INFLOW", @"24:00:00 January 1, Current Year", @"Previous Timestep" )
      ENDIF
   ELSE
      IF ( "GetMonth"( @"Current Timestep" ) < 8.00000000 )
      THEN
         IF ( @"Current Timestep" == @"24:00:00 April 1, Current Year" )
         THEN
            0.00000000 ["acre-ft"]
         ELSE
            "SumFlowsToVolume"( $ "RESERVOIR_DATA.BLUE_MESA_DLY_UNREG_INFLOW", @"24:00:00 April 1, Current Year", @"Previous Timestep" )
         ENDIF
      ELSE
         IF ( @"Current Timestep" == @"24:00:00 August 1, Current Year" )
         THEN
            0.00000000 ["acre-ft"]
         ELSE
            "SumFlowsToVolume"( $ "RESERVOIR_DATA.BLUE_MESA_DLY_UNREG_INFLOW", @"24:00:00 August 1, Current Year", @"Previous Timestep" )
         ENDIF
      ENDIF
   ENDIF;
    END;
    FUNCTION       "BMtoCRCurrent4cast" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      IF ( "GetMonth"( @"Current Timestep" ) < 4.00000000 )
   THEN
      ( "SumFlowsToVolume"( $ "BMtoMP.Inflow2", @"24:00:00 January 1, Current Year", @"24:00:00 March 31, Current Year" ) + "SumFlowsToVolume"( $ "MPtoCRY.Inflow2", @"24:00:00 January 1, Current Year", @"24:00:00 March 31, Current Year" ) ) * "BMCurrentFrcstErr"(  )
   ELSE
      IF ( "GetMonth"( @"Current Timestep" ) < 8.00000000 )
      THEN
         ( "SumFlowsToVolume"( $ "BMtoMP.Inflow2", @"24:00:00 April 1, Current Year", @"24:00:00 July 31, Current Year" ) + "SumFlowsToVolume"( $ "MPtoCRY.Inflow2", @"24:00:00 April 1, Current Year", @"24:00:00 July 31, Current Year" ) ) * "BMCurrentFrcstErr"(  )
      ELSE
         ( "SumFlowsToVolume"( $ "BMtoMP.Inflow2", @"24:00:00 August 1, Current Year", @"24:00:00 December 31, Current Year" ) + "SumFlowsToVolume"( $ "MPtoCRY.Inflow2", @"24:00:00 August 1, Current Year", @"24:00:00 December 31, Current Year" ) ) * "BMCurrentFrcstErr"(  )
      ENDIF
   ENDIF;
    END;
    FUNCTION       "BMtoCRRemainingInflow" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "Apply BM forecast error to side inflows of Morrow Pt and Crystal to create forecasted side inflow component.";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      IF ( "GetMonth"( @"Current Timestep" ) < 4.00000000 )
   THEN
      "BMtoCRCurrent4cast"(  ) - "BMtoCRAccumInflow"(  )
   ELSE
      IF ( "GetMonth"( @"Current Timestep" ) < 8.00000000 )
      THEN
         "BMtoCRCurrent4cast"(  ) - "BMtoCRAccumInflow"(  )
      ELSE
         "BMtoCRCurrent4cast"(  ) - "BMtoCRAccumInflow"(  )
      ENDIF
   ENDIF;
    END;
    FUNCTION       "BMtoCRAccumInflow" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      IF ( "GetMonth"( @"Current Timestep" ) < 4.00000000 )
   THEN
      IF ( @"Current Timestep" == @"24:00:00 January 1, Current Year" )
      THEN
         0.00000000 ["acre-ft"]
      ELSE
         ( "SumFlowsToVolume"( $ "BMtoMP.Inflow2", @"24:00:00 January 1, Current Year", @"Previous Timestep" ) + "SumFlowsToVolume"( $ "MPtoCRY.Inflow2", @"24:00:00 January 1, Current Year", @"Previous Timestep" ) )
      ENDIF
   ELSE
      IF ( "GetMonth"( @"Current Timestep" ) < 8.00000000 )
      THEN
         IF ( @"Current Timestep" == @"24:00:00 April 1, Current Year" )
         THEN
            0.00000000 ["acre-ft"]
         ELSE
            ( "SumFlowsToVolume"( $ "BMtoMP.Inflow2", @"24:00:00 April 1, Current Year", @"Previous Timestep" ) + "SumFlowsToVolume"( $ "MPtoCRY.Inflow2", @"24:00:00 April 1, Current Year", @"Previous Timestep" ) )
         ENDIF
      ELSE
         IF ( @"Current Timestep" == @"24:00:00 August 1, Current Year" )
         THEN
            0.00000000 ["acre-ft"]
         ELSE
            ( "SumFlowsToVolume"( $ "BMtoMP.Inflow2", @"24:00:00 August 1, Current Year", @"Previous Timestep" ) + "SumFlowsToVolume"( $ "MPtoCRY.Inflow2", @"24:00:00 August 1, Current Year", @"Previous Timestep" ) )
         ENDIF
      ENDIF
   ENDIF;
    END;
    FUNCTION       "BMCurrentFrcstErr" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "Divides current BM forecast by Actual Inflow to arrive at current error.";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      IF ( "GetMonth"( @"Current Timestep" ) < 4.00000000 )
   THEN
      "BMCurrent4cast"(  ) / "SumFlowsToVolume"( $ "RESERVOIR_DATA.BLUE_MESA_DLY_UNREG_INFLOW", @"24:00:00 January 1, Current Year", @"24:00:00 March 31, Current Year" )
   ELSE
      IF ( "GetMonth"( @"Current Timestep" ) < 8.00000000 )
      THEN
         "BMCurrent4cast"(  ) / "SumFlowsToVolume"( $ "RESERVOIR_DATA.BLUE_MESA_DLY_UNREG_INFLOW", @"24:00:00 April 1, Current Year", @"24:00:00 July 31, Current Year" )
      ELSE
         "BMCurrent4cast"(  ) / "SumFlowsToVolume"( $ "RESERVOIR_DATA.BLUE_MESA_DLY_UNREG_INFLOW", @"24:00:00 August 1, Current Year", @"24:00:00 December 31, Current Year" )
      ENDIF
   ENDIF;
    END;
    FUNCTION       "TunnelRemainingDmd" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "Remaining tunnel demand for current timestep thru end of forecast period.";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      IF ( "GetMonth"( @"Current Timestep" ) < 4.00000000 )
   THEN
      "Max"( "TunnelDmd4cast"(  ) - "TunnelAccumDiv"(  ), 0.00000000 ["acre-ft"] )
   ELSE
      IF ( "GetMonth"( @"Current Timestep" ) < 8.00000000 )
      THEN
         "Max"( "TunnelDmd4cast"(  ) - "TunnelAccumDiv"(  ), 0.00000000 ["acre-ft"] )
      ELSE
         "Max"( "TunnelDmd4cast"(  ) - "TunnelAccumDiv"(  ), 0.00000000 ["acre-ft"] )
      ENDIF
   ENDIF;
    END;
    FUNCTION       "TunnelAccumDiv" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      IF ( "GetMonth"( @"Current Timestep" ) < 4.00000000 )
   THEN
      IF ( @"Current Timestep" == @"24:00:00 January 1, Current Year" )
      THEN
         0.00000000 ["acre-ft"]
      ELSE
         "SumFlowsToVolume"( $ "GunnisonTunnel.Diversion", @"24:00:00 January 1, Current Year", @"Previous Timestep" )
      ENDIF
   ELSE
      IF ( "GetMonth"( @"Current Timestep" ) < 8.00000000 )
      THEN
         IF ( @"Current Timestep" == @"24:00:00 April 1, Current Year" )
         THEN
            0.00000000 ["acre-ft"]
         ELSE
            "SumFlowsToVolume"( $ "GunnisonTunnel.Diversion", @"24:00:00 April 1, Current Year", @"Previous Timestep" )
         ENDIF
      ELSE
         IF ( @"Current Timestep" == @"24:00:00 August 1, Current Year" )
         THEN
            0.00000000 ["acre-ft"]
         ELSE
            "SumFlowsToVolume"( $ "GunnisonTunnel.Diversion", @"24:00:00 August 1, Current Year", @"Previous Timestep" )
         ENDIF
      ENDIF
   ENDIF;
    END;
    FUNCTION       "TunnelDmd4cast" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      $ "FORECAST_TABLES.GT4casted" [@"Current Timestep", "GetYear"( @"Current Timestep" )];
    END;
    FUNCTION       "BMDiverRemainingDmd" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "Remaining BM pump demand for current timestep thru end of forecast period.";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      IF ( "GetMonth"( @"Current Timestep" ) < 4.00000000 )
   THEN
      $ "DIVERSION_DATA.BMPumpDiversion" [0.00000000, "FlowRequest"] * ( @"24:00:00 April 1, Current Year" - @"Current Timestep" )
   ELSE
      IF ( "GetMonth"( @"Current Timestep" ) < 8.00000000 )
      THEN
         $ "DIVERSION_DATA.BMPumpDiversion" [0.00000000, "FlowRequest"] * ( @"24:00:00 August 1, Current Year" - @"Current Timestep" )
      ELSE
         $ "DIVERSION_DATA.BMPumpDiversion" [0.00000000, "FlowRequest"] * ( @"24:00:00 January 1, Next Year" - @"Current Timestep" )
      ENDIF
   ENDIF;
    END;
    FUNCTION       "CanyonRemainingMinDmd" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "Remaining canyon demand for current timestep thru forecast period, based on estimated minimum trout flows, min. NPS flows, and min. FWS flows.";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      IF ( "GetMonth"( @"Current Timestep" ) < 4.00000000 )
   THEN
      FOR ( DATETIME date IN @"Current Timestep" TO @"24:00:00 March 31, Current Year" ) WITH NUMERIC total = 0.00000000 ["acre-ft"] DO
         "FlowToVolume"( "Max"( "Max"( $ "RulesData.NPS_q" [date], $ "RulesData.FWS_q_in_BC" [date] ), $ "RulesData.Trout_q" [date] ), @"Current Timestep" ) + total
      ENDFOR
   ELSE
      IF ( "GetMonth"( @"Current Timestep" ) < 8.00000000 )
      THEN
         FOR ( DATETIME date IN @"Current Timestep" TO @"24:00:00 July 31, Current Year" ) WITH NUMERIC total = 0.00000000 ["acre-ft"] DO
            "FlowToVolume"( "Max"( "Max"( $ "RulesData.NPS_q" [date], $ "RulesData.FWS_q_in_BC" [date] ), $ "RulesData.Trout_q" [date] ), @"Current Timestep" ) + total
         ENDFOR
      ELSE
         FOR ( DATETIME date IN @"Current Timestep" TO @"24:00:00 December 31, Current Year" ) WITH NUMERIC total = 0.00000000 ["acre-ft"] DO
            "FlowToVolume"( "Max"( "Max"( $ "RulesData.NPS_q" [date], $ "RulesData.FWS_q_in_BC" [date] ), $ "RulesData.Trout_q" [date] ), @"Current Timestep" ) + total
         ENDFOR
      ENDIF
   ENDIF;
    END;
    FUNCTION       "JulyPowerWater" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "Hold block of water for early summer power approx (pp max-300-950 cfs)*2*45 da   It will be releases 
as operation component after jun 15 if available
";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      IF ( @"Current Timestep" >= @"24:00:00 April 1, Current Year" AND @"Current Timestep" < @"24:00:00 June 15, Current Year" )
   THEN
      $ "Crystal.Max Turbine Q" [0.00000000, 1.00000000] * 45.00000000 ["day"]
   ELSE
      0.00000000 ["acre-feet"]
   ENDIF;
    END;
    FUNCTION       "BMOperVolumetoRelease" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "Estimated volume of water to release from BM not need for downstream demands.";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      "BMVolumetoRelease"(  ) - "BMDiverRemainingDmd"(  ) - "Max"( "CanyonRemainingMinDmd"(  ) + "TunnelRemainingDmd"(  ) - "BMtoCRRemainingInflow"(  ) + "JulyPowerWater"(  ), 0.00000000 ["acre-ft"] );
    END;
    FUNCTION       "CROperVolumetoRelease" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "Estimated volume of water to release at Crystal that is not need for downstream demands.";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      "Max"( "BMOperVolumetoRelease"(  ) + "Max"( "BMtoCRRemainingInflow"(  ) - "CanyonRemainingMinDmd"(  ) - "TunnelRemainingDmd"(  ) - "JulyPowerWater"(  ), 0.00000000 ["acre-ft"] ), 0.00000000 ["acre-ft"] );
    END;
    FUNCTION       "CROperReleaseinCFS" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "Constant rate at which to release CROperVolumetoRelease until recomputed in 1 week.  Has a percent adjustment to 
hold releases down during spawn.  Could also be used to prevent over reaction to hight or low forecast early in forecast period.
Also adjusts release if bypasses expected in Aug-Oct and powerplant capacity is expected to be available in Nov-Dec.";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      IF ( "GetMonth"( @"Current Timestep" ) < 8.00000000 )
   THEN
      "CROperReleaseUnadj_inCFS"(  ) * $ "RulesData.OpVolAdj" ["GetMonthAsString"( @"Current Timestep" ), "WeekOfMonth"(  )]
   ELSE
      IF ( "GetMonth"( @"Current Timestep" ) < 11.00000000 )
      THEN
         ( "CROperReleaseUnadj_inCFS"(  ) - "CRAugOctOperQ_adjust"(  ) ) * $ "RulesData.OpVolAdj" ["GetMonthAsString"( @"Current Timestep" ), "WeekOfMonth"(  )]
      ELSE
         "CROperReleaseUnadj_inCFS"(  ) * $ "RulesData.OpVolAdj" ["GetMonthAsString"( @"Current Timestep" ), "WeekOfMonth"(  )]
      ENDIF
   ENDIF;
    END;
    FUNCTION       "CROperReleaseUnadj_inCFS" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "Constant rate at which to release CROperVolumetoRelease";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      IF ( "GetMonth"( @"Current Timestep" ) < 4.00000000 )
   THEN
      "VolumeToFlow"( "CROperVolumetoRelease"(  ) / ( "GetJulianDate"( @"24:00:00 March 31, Current Year" ) - "GetJulianDate"( @"Previous Timestep" ) ), @"Current Timestep" )
   ELSE
      IF ( "GetMonth"( @"Current Timestep" ) < 8.00000000 )
      THEN
         "VolumeToFlow"( "CROperVolumetoRelease"(  ) / ( "GetJulianDate"( @"24:00:00 July 31, Current Year" ) - "GetJulianDate"( @"Previous Timestep" ) ), @"Current Timestep" )
      ELSE
         "VolumeToFlow"( "CROperVolumetoRelease"(  ) / ( "GetJulianDate"( @"24:00:00 December 31, Current Year" ) - "GetJulianDate"( @"Previous Timestep" ) ), @"Current Timestep" )
      ENDIF
   ENDIF;
    END;
    FUNCTION       "WeekOfMonth" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      IF ( "GetDayOfMonth"( @"Current Timestep" ) <= 7.00000000 ["day"] )
   THEN
      1.00000000
   ELSE
      IF ( "GetDayOfMonth"( @"Current Timestep" ) <= 14.00000000 ["day"] )
      THEN
         2.00000000
      ELSE
         IF ( "GetDayOfMonth"( @"Current Timestep" ) <= 21.00000000 ["day"] )
         THEN
            3.00000000
         ELSE
            4.00000000
         ENDIF
      ENDIF
   ENDIF;
    END;
    FUNCTION       "BMMaxElevRelease" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "Release necessary to prevent BM from exceeding maximum allowable elevation.";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      "Max"( $ "BlueMesa.Inflow" [] - "VolumeToFlow"( 829780.00000000 ["acre-feet"] - $ "BlueMesa.Storage" [@"Previous Timestep"], @"Current Timestep" ), 0.00000000 ["cfs"] );
    END;
    FUNCTION       "BMReleaseByRateofRise" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "increase BM releases  if rate of fill is less than 20 days.  The shorter the day to fill the more release are 
increased (300*15/day to fill)";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      IF ( "BMDaystoFill"(  ) > 0.00000000 ["day"] AND "BMDaystoFill"(  ) < 20.00000000 ["day"] )
   THEN
      IF ( $ "BlueMesa.Pool Elevation" [@"Previous Timestep"] < 7514.40000000 ["feet"] )
      THEN
         "Min"( "Min"( 1500.00000000 ["cfs"], $ "BlueMesa.Outflow" [@"Previous Timestep"] ) + 300.00000000 ["cfs"] * ( 15.00000000 ["day"] / "BMDaystoFill"(  ) ), "CanyonMaxSafety&Trout"(  ) + $ "Crystal_to_GunnisonTunnel.Diversion" [] - $ "BMtoMP.Inflow2" [] - $ "MPtoCRY.Inflow2" [] )
      ELSE
         "Min"( "Min"( 3500.00000000 ["cfs"], $ "BlueMesa.Outflow" [@"Previous Timestep"] ) + 300.00000000 ["cfs"] * ( 15.00000000 ["day"] / "BMDaystoFill"(  ) ), "CanyonMaxSafety&Trout"(  ) + $ "Crystal_to_GunnisonTunnel.Diversion" [] - $ "BMtoMP.Inflow2" [] - $ "MPtoCRY.Inflow2" [] )
      ENDIF
   ELSE
      0.00000000 ["cfs"]
   ENDIF;
    END;
    FUNCTION       "BMDaystoFill" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "Day till BM full based on last 2 days rate of rise.";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      IF ( $ "BlueMesa.Pool Elevation" [@"Previous Timestep"] > $ "BlueMesa.Pool Elevation" [@"Current Timestep - 3 Timesteps"] )
   THEN
      ( 7519.40000000 ["feet"] - $ "BlueMesa.Pool Elevation" [@"Previous Timestep"] ) / ( ( $ "BlueMesa.Pool Elevation" [@"Previous Timestep"] - $ "BlueMesa.Pool Elevation" [@"Current Timestep - 3 Timesteps"] ) / 2.00000000 ["day"] )
   ELSE
      0.00000000 ["day"]
   ENDIF;
    END;
    FUNCTION       "BMShortTermRel" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "Release needed to prevent BM from exceeding capacity of 820000af in the next x days using BMShortTermVolume
";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      WITH NUMERIC no_days = $ "RulesData.ShortTermForecast" [0.00000000, 0.00000000] DO
      "Min"( "Min"( ( "BMShortTermVol"(  ) - 820000.00000000 ["acre-feet"] + $ "BlueMesa.Storage" [@"Previous Timestep"] ) / no_days, 6000.00000000 ["cfs"] ), "Max"( $ "GunnisonTunnel.Diversion" [] + "CanyonMaxSafety&Trout"(  ) - $ "BMtoMP.Inflow2" [] - $ "MPtoCRY.Inflow2" [], 0.00000000 ["cfs"] ) )
   ENDWITH;
    END;
    FUNCTION       "RowWithMaxBMFloodContent" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      "TableInterpolation"( $ "FloodControlTables.RowBMFldCtrl", 0.00000000, 1.00000000, "BMRemainingInflow"(  ), @"Current Timestep" );
    END;
    FUNCTION       "BMShortTermVol" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "Estimated inflow over the next x days based on the previous 6 days inflow.";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      WITH NUMERIC no_days = $ "RulesData.ShortTermForecast" [0.00000000, 0.00000000] DO
      $ "BlueMesa.Inflow" [] * no_days + no_days * ( no_days + 1.00000000 ["day"] ) / 2.00000000 ["day"] * ( ( "SumSlot"( $ "BlueMesa.Inflow", @"Current Timestep - 2 Day", @"Current Timestep" ) - "SumSlot"( $ "BlueMesa.Inflow", @"Current Timestep - 5 Day", @"Current Timestep - 3 Day" ) ) / 9.00000000 )
   ENDWITH;
    END;
    FUNCTION       "EarlyEstFallOpVol" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "Estimated operational volume to be released at Crystal during the fall months based current BM content and
average conditions during the fall.";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      "Max"( $ "BlueMesa.Storage" [@"Previous Timestep"] + 199000.00000000 ["acre-ft"] - 580000.00000000 ["acre-ft"] + 44000.00000000 ["acre-ft"] - 140000.00000000 ["acre-ft"] - 90000.00000000 ["acre-ft"], 0.00000000 ["acre-ft"] );
    END;
    FUNCTION       "EarlyEstFallOpRelCFS" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "Average rate at which to release Early Estimated Fall operation volume.";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      "Max"( "EarlyEstFallOpVol"(  ) / ( @"24:00:00 December 31, Current Year" - @"Current Timestep" ), "Min"( $ "Crystal.Max Turbine Q" [0.00000000, 1.00000000] - 300.00000000 ["cfs"] - $ "GunnisonTunnel.Diversion" [], "EarlyEstFallOpVol"(  ) / ( @"24:00:00 September 30, Current Year" - @"Current Timestep" ) ) );
    END;
    FUNCTION       "DeltaFldAdj" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "Amount to reduce BM release if flow at Delta exceeds 14000 cfs.";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      IF ( @"Current Timestep" == @"Start Timestep" )
   THEN
      0.00000000 ["cfs"]
   ELSE
      IF ( $ "GTtoDelta.Outflow" [@"Previous Timestep"] <= 14000.00000000 ["cfs"] )
      THEN
         0.00000000 ["cfs"]
      ELSE
         IF ( $ "GTtoDelta.Outflow" [@"Previous Timestep"] <= 15000.00000000 ["cfs"] )
         THEN
            "Min"( ( $ "GTtoDelta.Outflow" [@"Previous Timestep"] - 14000.00000000 ["cfs"] ) / $ "GTtoDelta.Outflow" [@"Previous Timestep"] * $ "Crystal_to_GunnisonTunnel.Outflow" [@"Previous Timestep"], "Max"( 400.00000000 ["cfs"], 0.15000000 * $ "Crystal_to_GunnisonTunnel.Outflow" [@"Previous Timestep"] ) )
         ELSE
            "Min"( "Min"( $ "Crystal_to_GunnisonTunnel.Outflow" [@"Previous Timestep"] - 300.00000000 ["cfs"], $ "GTtoDelta.Outflow" [@"Previous Timestep"] - 14500.00000000 ["cfs"] ), "Max"( 400.00000000 ["cfs"], 0.15000000 * $ "Crystal_to_GunnisonTunnel.Outflow" [@"Previous Timestep"] ) )
         ENDIF
      ENDIF
   ENDIF;
    END;
    FUNCTION       "SpringPeakReleaseStart" (  )
    RETURN_TYPE    DATETIME;
    SCALE_UNITS    "";
    DESCRIPTION    "";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      @"24:00:00 April 30, Current Year" + $ "FlowCriteria.SprPeakDates" [0.00000000, 0.00000000];
    END;
    FUNCTION       "SpringPeakReleaseStop_last" (  )
    RETURN_TYPE    DATETIME;
    SCALE_UNITS    "";
    DESCRIPTION    "";
    ACTIVE         FALSE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      IF ( @"Current Timestep" >= @"24:00:00 June 1, Current Year" AND $ "RulesData.SpringPeakRelease" [@"24:00:00 May 31, Current Year"] >= 3000.00000000 ["cfs"] AND "MaxItem"( "GetSlotVals"( $ "RulesData.SpringPeakRelease", @"24:00:00 May 15, Current Year", @"24:00:00 May 31, Current Year" ) ) >= 0.00000000 ["cfs"] )
   THEN
      @"24:00:00 April 30, Current Year" + "Min"( 13.00000000 ["day"] + $ "FlowCriteria.SprPeakDates" [0.00000000, 0.00000000] + "TableInterpolation"( $ "FlowCriteria.NoActionPeak", 0.00000000, 1.00000000, $ "RulesDataOut.EstCrystalBypassVolume" [@"24:00:00 June 1, Current Year"], @"Current Timestep" ), $ "FlowCriteria.SprPeakDates" [1.00000000, 0.00000000] )
   ELSE
      IF ( @"Current Timestep" >= @"24:00:00 May 22, Current Year" AND $ "RulesData.SpringPeakRelease" [@"24:00:00 May 21, Current Year"] >= 3000.00000000 ["cfs"] AND "MaxItem"( "GetSlotVals"( $ "RulesData.SpringPeakRelease", @"24:00:00 May 15, Current Year", @"24:00:00 May 21, Current Year" ) ) >= 0.00000000 ["cfs"] )
      THEN
         @"24:00:00 April 30, Current Year" + "Min"( 6.00000000 ["day"] + $ "FlowCriteria.SprPeakDates" [0.00000000, 0.00000000] + "TableInterpolation"( $ "FlowCriteria.NoActionPeak", 0.00000000, 1.00000000, $ "RulesDataOut.EstCrystalBypassVolume" [@"24:00:00 May 22, Current Year"], @"Current Timestep" ), $ "FlowCriteria.SprPeakDates" [1.00000000, 0.00000000] )
      ELSE
         @"24:00:00 April 30, Current Year" + $ "FlowCriteria.SprPeakDates" [0.00000000, 0.00000000] + "TableInterpolation"( $ "FlowCriteria.NoActionPeak", 0.00000000, 1.00000000, $ "RulesDataOut.EstCrystalBypassVolume" [@"24:00:00 May 15, Current Year"], @"Current Timestep" )
      ENDIF
   ENDIF;
    END;
    FUNCTION       "SpringPeakReleaseStop9-10" (  )
    RETURN_TYPE    DATETIME;
    SCALE_UNITS    "";
    DESCRIPTION    "";
    ACTIVE         FALSE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      IF ( @"Current Timestep" >= @"24:00:00 June 1, Current Year" )
   THEN
      IF ( "SpringPeakPrevStop"(  ) > @"24:00:00 June 1, Current Year" OR "MaxItem"( "GetSlotVals"( $ "RulesData.SpringPeakRelease", @"24:00:00 May 15, Current Year", @"24:00:00 May 31, Current Year" ) ) > 0.00000000 ["cfs"] )
      THEN
         "DateMin"( @"24:00:00 May 31, Current Year" + "TableInterpolation"( $ "FlowCriteria.NoActionPeak", 0.00000000, 1.00000000, $ "RulesDataOut.EstCrystalBypassVolume" [@"24:00:00 June 1, Current Year"], @"Current Timestep" ), @"24:00:00 April 30, Current Year" + $ "FlowCriteria.SprPeakDates" [1.00000000, 0.00000000] )
      ELSE
         @"Current Timestep"
      ENDIF
   ELSE
      IF ( @"Current Timestep" >= @"24:00:00 May 22, Current Year" )
      THEN
         IF ( "SpringPeakPrevStop"(  ) > @"24:00:00 May 22, Current Year" OR "MaxItem"( "GetSlotVals"( $ "RulesData.SpringPeakRelease", @"24:00:00 May 15, Current Year", @"24:00:00 May 21, Current Year" ) ) > 0.00000000 ["cfs"] )
         THEN
            "DateMin"( @"24:00:00 May 21, Current Year" + "TableInterpolation"( $ "FlowCriteria.NoActionPeak", 0.00000000, 1.00000000, $ "RulesDataOut.EstCrystalBypassVolume" [@"24:00:00 May 22, Current Year"], @"Current Timestep" ), @"24:00:00 April 30, Current Year" + $ "FlowCriteria.SprPeakDates" [1.00000000, 0.00000000] )
         ELSE
            @"Current Timestep"
         ENDIF
      ELSE
         @"24:00:00 April 30, Current Year" + $ "FlowCriteria.SprPeakDates" [0.00000000, 0.00000000] + "TableInterpolation"( $ "FlowCriteria.NoActionPeak", 0.00000000, 1.00000000, $ "RulesDataOut.EstCrystalBypassVolume" [@"24:00:00 May 15, Current Year"], @"Current Timestep" )
      ENDIF
   ENDIF;
    END;
    FUNCTION       "SpringPeakReleaseStop" (  )
    RETURN_TYPE    DATETIME;
    SCALE_UNITS    "";
    DESCRIPTION    "";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      IF ( @"Current Timestep" < "SpringPeakReleaseStart"(  ) )
   THEN
      "SpringPeakReleaseStart"(  )
   ELSE
      IF ( @"Current Timestep" < @"24:00:00 May 22, Current Year" )
      THEN
         @"24:00:00 April 30, Current Year" + $ "FlowCriteria.SprPeakDates" [0.00000000, 0.00000000] + "TableInterpolation"( $ "FlowCriteria.NoActionPeak", 0.00000000, 1.00000000, $ "RulesDataOut.EstCrystalBypassVolume" [@"24:00:00 May 15, Current Year"], @"Current Timestep" )
      ELSE
         IF ( @"Current Timestep" < @"24:00:00 June 1, Current Year" )
         THEN
            IF ( "SpringPeakPrevStop"(  ) >= @"24:00:00 May 22, Current Year" OR "MaxItem"( "GetSlotVals"( $ "RulesData.SpringPeakRelease", @"24:00:00 May 15, Current Year", @"24:00:00 May 21, Current Year" ) ) == 0.00000000 ["cfs"] )
            THEN
               "DateMin"( @"24:00:00 May 21, Current Year" + "TableInterpolation"( $ "FlowCriteria.NoActionPeak", 0.00000000, 1.00000000, $ "RulesDataOut.EstCrystalBypassVolume" [@"24:00:00 May 22, Current Year"], @"Current Timestep" ), @"24:00:00 April 30, Current Year" + $ "FlowCriteria.SprPeakDates" [1.00000000, 0.00000000] )
            ELSE
               @"Current Timestep"
            ENDIF
         ELSE
            IF ( @"Current Timestep" < @"24:00:00 June 8, Current Year" )
            THEN
               IF ( "SpringPeakPrevStop"(  ) >= @"24:00:00 June 1, Current Year" OR "MaxItem"( "GetSlotVals"( $ "RulesData.SpringPeakRelease", @"24:00:00 May 15, Current Year", @"24:00:00 May 31, Current Year" ) ) == 0.00000000 ["cfs"] )
               THEN
                  "DateMin"( @"24:00:00 May 31, Current Year" + "TableInterpolation"( $ "FlowCriteria.NoActionPeak", 0.00000000, 1.00000000, $ "RulesDataOut.EstCrystalBypassVolume" [@"24:00:00 June 1, Current Year"], @"Current Timestep" ), @"24:00:00 April 30, Current Year" + $ "FlowCriteria.SprPeakDates" [1.00000000, 0.00000000] )
               ELSE
                  @"Current Timestep"
               ENDIF
            ELSE
               @"Current Timestep"
            ENDIF
         ENDIF
      ENDIF
   ENDIF;
    END;
    FUNCTION       "SpringPeakPrevStop" (  )
    RETURN_TYPE    DATETIME;
    SCALE_UNITS    "";
    DESCRIPTION    "";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  FALSE;
    POST_EXEC_DIAG FALSE;
    BEGIN
      IF ( @"Current Timestep" >= @"24:00:00 June 1, Current Year" )
   THEN
      "DateMin"( @"24:00:00 May 21, Current Year" + "TableInterpolation"( $ "FlowCriteria.NoActionPeak", 0.00000000, 1.00000000, $ "RulesDataOut.EstCrystalBypassVolume" [@"24:00:00 May 22, Current Year"], @"Current Timestep" ), @"24:00:00 April 30, Current Year" + $ "FlowCriteria.SprPeakDates" [0.00000000, 0.00000000] + $ "FlowCriteria.SprPeakDates" [1.00000000, 0.00000000] )
   ELSE
      IF ( @"Current Timestep" >= @"24:00:00 May 22, Current Year" )
      THEN
         @"24:00:00 April 30, Current Year" + $ "FlowCriteria.SprPeakDates" [0.00000000, 0.00000000] + "TableInterpolation"( $ "FlowCriteria.NoActionPeak", 0.00000000, 1.00000000, $ "RulesDataOut.EstCrystalBypassVolume" [@"24:00:00 May 15, Current Year"], @"Current Timestep" )
      ELSE
         @"24:00:00 April 30, Current Year" + $ "FlowCriteria.SprPeakDates" [0.00000000, 0.00000000]
      ENDIF
   ENDIF;
    END;
    FUNCTION       "SpringPeakForecastDate" (  )
    RETURN_TYPE    DATETIME;
    SCALE_UNITS    "";
    DESCRIPTION    "";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      IF ( @"Current Timestep" >= @"24:00:00 June 1, Current Year" )
   THEN
      @"24:00:00 June 1, Current Year"
   ELSE
      IF ( @"Current Timestep" >= @"24:00:00 May 22, Current Year" )
      THEN
         @"24:00:00 May 22, Current Year"
      ELSE
         IF ( @"Current Timestep" >= @"24:00:00 May 15, Current Year" )
         THEN
            @"24:00:00 May 15, Current Year"
         ELSE
            @"Current Timestep"
         ENDIF
      ENDIF
   ENDIF;
    END;
    FUNCTION       "SpringPeakReleaseStop1" (  )
    RETURN_TYPE    DATETIME;
    SCALE_UNITS    "";
    DESCRIPTION    "";
    ACTIVE         FALSE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      IF ( @"Current Timestep" < @"24:00:00 May 22, Current Year" )
   THEN
      @"24:00:00 April 30, Current Year" + $ "FlowCriteria.SprPeakDates" [0.00000000, 0.00000000] + "TableInterpolation"( $ "FlowCriteria.NoActionPeak", 0.00000000, 1.00000000, $ "RulesDataOut.EstCrystalBypassVolume" [@"24:00:00 May 15, Current Year"], @"Current Timestep" )
   ELSE
      IF ( @"Current Timestep" < @"24:00:00 June 1, Current Year" AND $ "RulesData.SpringPeakRelease" [@"24:00:00 May 21, Current Year"] >= 3000.00000000 ["cfs"] AND "MaxItem"( "GetSlotVals"( $ "RulesData.SpringPeakRelease", @"24:00:00 May 15, Current Year", @"24:00:00 May 21, Current Year" ) ) >= 0.00000000 ["cfs"] )
      THEN
         @"24:00:00 April 30, Current Year" + 6.00000000 ["day"] + $ "FlowCriteria.SprPeakDates" [0.00000000, 0.00000000] + "TableInterpolation"( $ "FlowCriteria.NoActionPeak", 0.00000000, 1.00000000, $ "RulesDataOut.EstCrystalBypassVolume" [@"24:00:00 May 22, Current Year"], @"Current Timestep" )
      ELSE
         IF ( @"Current Timestep" < @"24:00:00 June 5, Current Year" AND $ "RulesData.SpringPeakRelease" [@"24:00:00 May 31, Current Year"] >= 3000.00000000 ["cfs"] AND "MaxItem"( "GetSlotVals"( $ "RulesData.SpringPeakRelease", @"24:00:00 May 15, Current Year", @"24:00:00 May 31, Current Year" ) ) >= 0.00000000 ["cfs"] )
         THEN
            @"24:00:00 April 30, Current Year" + 13.00000000 ["day"] + $ "FlowCriteria.SprPeakDates" [0.00000000, 0.00000000] + "TableInterpolation"( $ "FlowCriteria.NoActionPeak", 0.00000000, 1.00000000, $ "RulesDataOut.EstCrystalBypassVolume" [@"24:00:00 June 1, Current Year"], @"Current Timestep" )
         ELSE
            @"Current Timestep"
         ENDIF
      ENDIF
   ENDIF;
    END;
    FUNCTION       "SpringPeakReleaseEnd" (  )
    RETURN_TYPE    DATETIME;
    SCALE_UNITS    "";
    DESCRIPTION    "";
    ACTIVE         FALSE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      @"24:00:00 April 30, Current Year" + $ "FlowCriteria.SprPeakDates" [1.00000000, 0.00000000];
    END;
    FUNCTION       "GTPrev5DayAvgDiv" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      "SumSlot"( $ "GunnisonTunnel.Diversion", @"Current Timestep - 5 Timesteps", @"Previous Timestep" ) / 5.00000000;
    END;
    FUNCTION       "CRAugOctOperQ_adjust" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "If bypass for computed operatial release is anticipated to result in crystal bypass in Aug-oct, shift this water to Nov and Dec if capacity is 
expected to be available.";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      IF ( "GetMonth"( @"Current Timestep" ) == 8.00000000 )
   THEN
      IF ( "CROperReleaseUnadj_inCFS"(  ) > $ "Crystal.Max Turbine Q" [0.00000000, 1.00000000] - $ "RulesData.TroutCriteria" ["Trout", "MinFlow"] - "GTPrev5DayAvgDiv"(  ) AND "CROperReleaseUnadj_inCFS"(  ) < $ "Crystal.Max Turbine Q" [0.00000000, 1.00000000] - $ "RulesData.TroutCriteria" ["Trout", "MinFlow"] )
      THEN
         "Min"( "EstAugCryBypVol"(  ), "EstNovDecCryPP_RemainingVol"(  ) ) / ( @"24:00:00 September 1, Current Year" - @"Current Timestep" )
      ELSE
         0.00000000 ["cfs"]
      ENDIF
   ELSE
      IF ( "GetMonth"( @"Current Timestep" ) == 9.00000000 )
      THEN
         IF ( "CROperReleaseUnadj_inCFS"(  ) > $ "Crystal.Max Turbine Q" [0.00000000, 1.00000000] - $ "RulesData.TroutCriteria" ["Trout", "MinFlow"] - "GTPrev5DayAvgDiv"(  ) AND "CROperReleaseUnadj_inCFS"(  ) < $ "Crystal.Max Turbine Q" [0.00000000, 1.00000000] - $ "RulesData.TroutCriteria" ["Trout", "MinFlow"] )
         THEN
            "Min"( "EstSeptCryBypVol"(  ), "EstNovDecCryPP_RemainingVol"(  ) ) / ( @"24:00:00 October 1, Current Year" - @"Current Timestep" )
         ELSE
            0.00000000 ["cfs"]
         ENDIF
      ELSE
         IF ( "CROperReleaseUnadj_inCFS"(  ) > $ "Crystal.Max Turbine Q" [0.00000000, 1.00000000] - $ "RulesData.TroutCriteria" ["Trout", "MinFlow"] - "GTPrev5DayAvgDiv"(  ) AND "CROperReleaseUnadj_inCFS"(  ) < $ "Crystal.Max Turbine Q" [0.00000000, 1.00000000] - $ "RulesData.TroutCriteria" ["Trout", "MinFlow"] )
         THEN
            "Min"( "EstOctCryBypVol"(  ), "EstNovDecCryPP_RemainingVol"(  ) ) / ( @"24:00:00 November 1, Current Year" - @"Current Timestep" )
         ELSE
            0.00000000 ["cfs"]
         ENDIF
      ENDIF
   ENDIF;
    END;
    FUNCTION       "EstAugCryBypVol" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "Estimate of Bypass volume which will occur in Aug if Tunnel diverts 900, minimum canyon is 300, and powerplant max";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      ( "CROperReleaseUnadj_inCFS"(  ) - ( $ "Crystal.Max Turbine Q" [0.00000000, 1.00000000] - $ "RulesData.TroutCriteria" ["Trout", "MinFlow"] - "GTPrev5DayAvgDiv"(  ) ) ) * ( @"24:00:00 September 1, Current Year" - @"Current Timestep" );
    END;
    FUNCTION       "EstSeptCryBypVol" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "Estimate of Cry Bypass volume which will occure in Sept if Tunnel diverts 650, min canyon is 300, and powerplant max";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      ( "CROperReleaseUnadj_inCFS"(  ) - ( $ "Crystal.Max Turbine Q" [0.00000000, 1.00000000] - $ "RulesData.TroutCriteria" ["Trout", "MinFlow"] - "GTPrev5DayAvgDiv"(  ) ) ) * ( @"24:00:00 October 1, Current Year" - @"Current Timestep" );
    END;
    FUNCTION       "EstNovDecCryPP_RemainingVol" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "Estimates remaining volume of water which could be utilized in CR pp running at max -50 cfs in Nov & Dec with the current computed CROperRel.";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      ( $ "Crystal.Max Turbine Q" [0.00000000, 1.00000000] - 50.00000000 ["cfs"] - 300.00000000 ["cfs"] - "CROperReleaseUnadj_inCFS"(  ) ) * 61.00000000 ["day"];
    END;
    FUNCTION       "EstOctCryBypVol" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "Est of Bypass Oct bypas volume which will occur it Tunnel diversts 500, minimum canyon is 300, and 
max pp
";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      ( "CROperReleaseUnadj_inCFS"(  ) - ( $ "Crystal.Max Turbine Q" [0.00000000, 1.00000000] - $ "RulesData.TroutCriteria" ["Trout", "MinFlow"] - "GTPrev5DayAvgDiv"(  ) ) ) * ( @"24:00:00 November 1, Current Year" - @"Current Timestep" );
    END;
  END;
  UTILITY_GROUP "CANYON W/ Ramping and Trout";
  DESCRIPTION   "";
  ACTIVE        TRUE;
  BEGIN
    FUNCTION       "CanyonMinTroutTarget" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "Minimum target flow in Black canyon based on trout.  If ideal minimum flow indicates bypass, target is set 
prevent bypass at Crystal.";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      "Min"( "CanyonMinTroutTarget_ideal"(  ), $ "Crystal.Max Turbine Q" [0.00000000, 1.00000000] - $ "Crystal_to_GunnisonTunnel.Diversion" [] );
    END;
    FUNCTION       "CanyonMinTroutTarget_ideal" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "Set minimum canyon target based on the controlling trout species.";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      "Max"( "CanyonMinBrownTarget_ideal"(  ), "CanyonMinRainbowTarget_ideal"(  ) );
    END;
    FUNCTION       "CanyonMinBrownTarget_ideal" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "Determines minimum canyon flow for the current day based on Brown Trout.";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      IF ( @"Current Timestep" >= @"24:00:00 October 15, Current Year" AND @"Current Timestep" <= @"24:00:00 October 31, Current Year" )
   THEN
      $ "RulesData.TroutCriteria" ["Trout", "MinFlow"]
   ELSE
      IF ( @"Current Timestep" >= @"24:00:00 November 1, Current Year" AND @"Current Timestep" <= @"24:00:00 November 14, Current Year" )
      THEN
         "Max"( "Avg_Q_DuringBrownSpawnOct15Oct31"(  ) * $ "RulesData.TroutCriteria" ["Trout", "SpawnPercent"], $ "RulesData.TroutCriteria" ["Trout", "MinFlow"] )
      ELSE
         IF ( @"Current Timestep" >= @"24:00:00 November 15, Current Year" AND @"Current Timestep" <= @"24:00:00 December 31, Current Year" )
         THEN
            "Max"( "Min"( "Avg_Q_DuringBrownSpawnOct15Oct31"(  ), "Avg_Q_DuringBrownSpawnNov1Nov14"(  ) ) * $ "RulesData.TroutCriteria" ["Trout", "IncubationPercent"], $ "RulesData.TroutCriteria" ["Trout", "MinFlow"] )
         ELSE
            IF ( @"Current Timestep" >= @"24:00:00 January 1, Current Year" AND @"Current Timestep" <= @"24:00:00 April 14, Current Year" )
            THEN
               IF ( "GetYear"( @"Current Timestep" ) == 1975.00000000 )
               THEN
                  $ "RulesData.TroutCriteria" ["Trout", "MinFlow"]
               ELSE
                  "Max"( "Min"( "Avg_Q_DuringBrownSpawnOct15Oct31_Prev_Yr"(  ), "Avg_Q_DuringBrownSpawnNov1Nov14_Prev_Yr"(  ) ) * $ "RulesData.TroutCriteria" ["Trout", "IncubationPercent"], $ "RulesData.TroutCriteria" ["Trout", "MinFlow"] )
               ENDIF
            ELSE
               $ "RulesData.TroutCriteria" ["Trout", "MinFlow"]
            ENDIF
         ENDIF
      ENDIF
   ENDIF;
    END;
    FUNCTION       "CanyonMinRainbowTarget_ideal" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "Determines minimum canyon flow for the current day based on Rainbow trout.";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      IF ( @"Current Timestep" >= @"24:00:00 April 1, Current Year" AND @"Current Timestep" <= @"24:00:00 April 14, Current Year" )
   THEN
      $ "RulesData.TroutCriteria" ["Trout", "MinFlow"]
   ELSE
      IF ( @"Current Timestep" >= @"24:00:00 April 15, Current Year" AND @"Current Timestep" <= @"24:00:00 April 30, Current Year" )
      THEN
         "Avg_Q_DuringRainbowSpawnApr1Apr14"(  ) * $ "RulesData.TroutCriteria" ["Trout", "SpawnPercent"]
      ELSE
         IF ( @"Current Timestep" >= @"24:00:00 May 1, Current Year" AND @"Current Timestep" <= @"24:00:00 May 14, Current Year" )
         THEN
            "Max"( "Min"( "Avg_Q_DuringRainbowSpawnApr1Apr14"(  ), "Avg_Q_DuringRainbowSpawnApr15Apr30"(  ) ) * $ "RulesData.TroutCriteria" ["Trout", "SpawnPercent"], $ "RulesData.TroutCriteria" ["Trout", "MinFlow"] )
         ELSE
            IF ( @"Current Timestep" >= @"24:00:00 May 15, Current Year" AND @"Current Timestep" <= @"24:00:00 June 14, Current Year" )
            THEN
               "Max"( "Min"( "Min"( "Avg_Q_DuringRainbowSpawnApr1Apr14"(  ), "Avg_Q_DuringRainbowSpawnApr15Apr30"(  ) ), "Avg_Q_DuringRainbowSpawnMay1May14"(  ) ) * $ "RulesData.TroutCriteria" ["Trout", "IncubationPercent"], $ "RulesData.TroutCriteria" ["Trout", "MinFlow"] )
            ELSE
               $ "RulesData.TroutCriteria" ["Trout", "MinFlow"]
            ENDIF
         ENDIF
      ENDIF
   ENDIF;
    END;
    FUNCTION       "Avg_Q_DuringBrownSpawnOct15Oct31" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      "SumSlot"( $ "Crystal_to_GunnisonTunnel.Outflow", @"24:00:00 October 15, Current Year", @"24:00:00 October 31, Current Year" ) / ( "GetJulianDate"( @"24:00:00 October 31, Current Year" ) - "GetJulianDate"( @"24:00:00 October 14, Current Year" ) );
    END;
    FUNCTION       "Avg_Q_DuringBrownSpawnOct15Oct31_Prev_Yr" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      "SumSlot"( $ "Crystal_to_GunnisonTunnel.Outflow", @"24:00:00 October 15, Previous Year", @"24:00:00 October 31, Previous Year" ) / ( "GetJulianDate"( @"24:00:00 October 31, Previous Year" ) - "GetJulianDate"( @"24:00:00 October 14, Previous Year" ) );
    END;
    FUNCTION       "Avg_Q_DuringBrownSpawnNov1Nov14" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      "SumSlot"( $ "Crystal_to_GunnisonTunnel.Outflow", @"24:00:00 November 1, Current Year", @"24:00:00 November 14, Current Year" ) / ( "GetJulianDate"( @"24:00:00 November 14, Current Year" ) - "GetJulianDate"( @"24:00:00 October 31, Current Year" ) );
    END;
    FUNCTION       "Avg_Q_DuringBrownSpawnNov1Nov14_Prev_Yr" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      "SumSlot"( $ "Crystal_to_GunnisonTunnel.Outflow", @"24:00:00 November 1, Previous Year", @"24:00:00 November 14, Previous Year" ) / ( "GetJulianDate"( @"24:00:00 November 14, Previous Year" ) - "GetJulianDate"( @"24:00:00 October 31, Previous Year" ) );
    END;
    FUNCTION       "Avg_Q_DuringRainbowSpawnApr1Apr14" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      "SumSlot"( $ "Crystal_to_GunnisonTunnel.Outflow", @"24:00:00 April 1, Current Year", @"24:00:00 April 14, Current Year" ) / ( "GetJulianDate"( @"24:00:00 April 14, Current Year" ) - "GetJulianDate"( @"24:00:00 March 31, Current Year" ) );
    END;
    FUNCTION       "Avg_Q_DuringRainbowSpawnApr15Apr30" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      "SumSlot"( $ "Crystal_to_GunnisonTunnel.Outflow", @"24:00:00 April 15, Current Year", @"24:00:00 April 30, Current Year" ) / ( "GetJulianDate"( @"24:00:00 April 30, Current Year" ) - "GetJulianDate"( @"24:00:00 April 14, Current Year" ) );
    END;
    FUNCTION       "Avg_Q_DuringRainbowSpawnApr1Apr30" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "";
    ACTIVE         FALSE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      "SumSlot"( $ "Crystal_to_GunnisonTunnel.Outflow", @"24:00:00 April 1, Current Year", @"24:00:00 April 30, Current Year" ) / ( "GetJulianDate"( @"24:00:00 April 30, Current Year" ) - "GetJulianDate"( @"24:00:00 March 31, Current Year" ) );
    END;
    FUNCTION       "Avg_Q_DuringRainbowSpawnMay1May14" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      "SumSlot"( $ "Crystal_to_GunnisonTunnel.Outflow", @"24:00:00 May 1, Current Year", @"24:00:00 May 14, Current Year" ) / ( "GetJulianDate"( @"24:00:00 May 14, Current Year" ) - "GetJulianDate"( @"24:00:00 April 30, Current Year" ) );
    END;
    FUNCTION       "CanyonMinSafety&Trout" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "Minumum flow for current day in canyon based on ramping rates for trout and safety.";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      "Max"( "Min"( $ "Crystal_to_GunnisonTunnel.Outflow" [@"Previous Timestep"] * 0.85000000, $ "Crystal_to_GunnisonTunnel.Outflow" [@"Previous Timestep"] - 400.00000000 ["cfs"] ), "CanyonMinTroutTarget"(  ) );
    END;
    FUNCTION       "CanyonMaxSafety&Trout" (  )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "Maximum flow for current day in canyon based on ramping rates for trout and safety.";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      "Max"( $ "Crystal_to_GunnisonTunnel.Outflow" [@"Previous Timestep"] * 1.15000000, $ "Crystal_to_GunnisonTunnel.Outflow" [@"Previous Timestep"] + 500.00000000 ["cfs"] );
    END;
    FUNCTION       "Build DateAndValue list" (  )
    RETURN_TYPE    LIST;
    SCALE_UNITS    "";
    DESCRIPTION    "Build a list containing estimated minimum flows desired by trout for current day thru end of forecast period.";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      IF ( @"Current Timestep" == @"24:00:00 January 1, Current Year" )
   THEN
      FOR ( DATETIME date IN @"Current Timestep" TO @"24:00:00 March 31, Current Year" ) WITH LIST list = {  } DO
         IF ( date == @"Start Timestep" )
         THEN
            INSERT { date , $ "RulesData.TroutCriteria" ["Trout", "MinFlow"] } INTO list
         ELSE
            IF ( @"Current Timestep" == @"24:00:00 January 1, Current Year" )
            THEN
               INSERT { date , $ "RulesData.Trout_q" [@"24:00:00 December 31, Previous Year"] } INTO list
            ELSE
               INSERT { date , "ComputeValueFromDate&YesterdayQ"( date, GET NUMERIC @INDEX 1.00000000 FROM GET LIST @INDEX 0.00000000 FROM list ) } INTO list
            ENDIF
         ENDIF
      ENDFOR
   ELSE
      IF ( @"Current Timestep" == @"24:00:00 April 1, Current Year" )
      THEN
         FOR ( DATETIME date IN @"Current Timestep" TO @"24:00:00 July 31, Current Year" ) WITH LIST list = {  } DO
            IF ( date == @"24:00:00 April 1, Current Year" )
            THEN
               INSERT { date , "Max"( $ "RulesData.Trout_q" [@"24:00:00 March 31, Current Year"], $ "RulesData.TroutCriteria" ["Trout", "MinFlow"] ) } INTO list
            ELSE
               IF ( date == @"24:00:00 April 15, Current Year" )
               THEN
                  INSERT { date , "Max"( $ "RulesData.Trout_q" [@"24:00:00 March 31, Current Year"] * $ "RulesData.TroutCriteria" ["Trout", "SpawnPercent"], $ "RulesData.TroutCriteria" ["Trout", "MinFlow"] ) } INTO list
               ELSE
                  IF ( date == @"24:00:00 May 15, Current Year" )
                  THEN
                     INSERT { date , "Max"( $ "RulesData.Trout_q" [@"24:00:00 March 31, Current Year"] * $ "RulesData.TroutCriteria" ["Trout", "SpawnPercent"] * $ "RulesData.TroutCriteria" ["Trout", "IncubationPercent"], $ "RulesData.TroutCriteria" ["Trout", "MinFlow"] ) } INTO list
                  ELSE
                     IF ( date == @"24:00:00 June 15, Current Year" )
                     THEN
                        INSERT { date , $ "RulesData.TroutCriteria" ["Trout", "MinFlow"] } INTO list
                     ELSE
                        INSERT { date , "ComputeValueFromDate&YesterdayQ"( date, GET NUMERIC @INDEX 1.00000000 FROM GET LIST @INDEX 0.00000000 FROM list ) } INTO list
                     ENDIF
                  ENDIF
               ENDIF
            ENDIF
         ENDFOR
      ELSE
         IF ( @"Current Timestep" == @"24:00:00 April 15, Current Year" )
         THEN
            FOR ( DATETIME date IN @"Current Timestep" TO @"24:00:00 July 31, Current Year" ) WITH LIST list = {  } DO
               IF ( date == @"24:00:00 April 15, Current Year" )
               THEN
                  INSERT { date , "Max"( "Avg_Q_DuringRainbowSpawnApr1Apr14"(  ) * $ "RulesData.TroutCriteria" ["Trout", "SpawnPercent"], $ "RulesData.TroutCriteria" ["Trout", "MinFlow"] ) } INTO list
               ELSE
                  IF ( date == @"24:00:00 May 15, Current Year" )
                  THEN
                     INSERT { date , "Max"( "Avg_Q_DuringRainbowSpawnApr1Apr14"(  ) * $ "RulesData.TroutCriteria" ["Trout", "SpawnPercent"] * $ "RulesData.TroutCriteria" ["Trout", "IncubationPercent"], $ "RulesData.TroutCriteria" ["Trout", "MinFlow"] ) } INTO list
                  ELSE
                     IF ( date == @"24:00:00 June 15, Current Year" )
                     THEN
                        INSERT { date , $ "RulesData.TroutCriteria" ["Trout", "MinFlow"] } INTO list
                     ELSE
                        INSERT { date , "ComputeValueFromDate&YesterdayQ"( date, GET NUMERIC @INDEX 1.00000000 FROM GET LIST @INDEX 0.00000000 FROM list ) } INTO list
                     ENDIF
                  ENDIF
               ENDIF
            ENDFOR
         ELSE
            IF ( @"Current Timestep" == @"24:00:00 May 1, Current Year" )
            THEN
               FOR ( DATETIME date IN @"Current Timestep" TO @"24:00:00 July 31, Current Year" ) WITH LIST list = {  } DO
                  IF ( date == @"24:00:00 May 1, Current Year" )
                  THEN
                     INSERT { date , "Max"( "Min"( "Avg_Q_DuringRainbowSpawnApr1Apr14"(  ), "Avg_Q_DuringRainbowSpawnApr15Apr30"(  ) ), $ "RulesData.TroutCriteria" ["Trout", "MinFlow"] ) } INTO list
                  ELSE
                     IF ( date == @"24:00:00 May 15, Current Year" )
                     THEN
                        INSERT { date , "Max"( "Min"( "Avg_Q_DuringRainbowSpawnApr1Apr14"(  ), "Avg_Q_DuringRainbowSpawnApr15Apr30"(  ) ) * $ "RulesData.TroutCriteria" ["Trout", "IncubationPercent"], $ "RulesData.TroutCriteria" ["Trout", "MinFlow"] ) } INTO list
                     ELSE
                        IF ( date == @"24:00:00 June 15, Current Year" )
                        THEN
                           INSERT { date , $ "RulesData.TroutCriteria" ["Trout", "MinFlow"] } INTO list
                        ELSE
                           INSERT { date , "ComputeValueFromDate&YesterdayQ"( date, GET NUMERIC @INDEX 1.00000000 FROM GET LIST @INDEX 0.00000000 FROM list ) } INTO list
                        ENDIF
                     ENDIF
                  ENDIF
               ENDFOR
            ELSE
               IF ( @"Current Timestep" == @"24:00:00 May 15, Current Year" OR @"Current Timestep" == @"24:00:00 June 15, Current Year" )
               THEN
                  FOR ( DATETIME date IN @"Current Timestep" TO @"24:00:00 July 31, Current Year" ) WITH LIST list = {  } DO
                     IF ( date == @"24:00:00 May 15, Current Year" )
                     THEN
                        INSERT { date , "Max"( "Min"( "Min"( "Avg_Q_DuringRainbowSpawnApr1Apr14"(  ), "Avg_Q_DuringRainbowSpawnApr15Apr30"(  ) ), "Avg_Q_DuringRainbowSpawnMay1May14"(  ) ) * $ "RulesData.TroutCriteria" ["Trout", "IncubationPercent"], $ "RulesData.TroutCriteria" ["Trout", "MinFlow"] ) } INTO list
                     ELSE
                        IF ( date == @"24:00:00 June 15, Current Year" )
                        THEN
                           INSERT { date , $ "RulesData.TroutCriteria" ["Trout", "MinFlow"] } INTO list
                        ELSE
                           INSERT { date , "ComputeValueFromDate&YesterdayQ"( date, GET NUMERIC @INDEX 1.00000000 FROM GET LIST @INDEX 0.00000000 FROM list ) } INTO list
                        ENDIF
                     ENDIF
                  ENDFOR
               ELSE
                  IF ( @"Current Timestep" == @"24:00:00 August 1, Current Year" )
                  THEN
                     FOR ( DATETIME date IN @"Current Timestep" TO @"24:00:00 December 31, Current Year" ) WITH LIST list = {  } DO
                        IF ( date == @"24:00:00 August 1, Current Year" )
                        THEN
                           INSERT { date , $ "RulesData.TroutCriteria" ["Trout", "MinFlow"] } INTO list
                        ELSE
                           INSERT { date , "ComputeValueFromDate&YesterdayQ"( date, GET NUMERIC @INDEX 1.00000000 FROM GET LIST @INDEX 0.00000000 FROM list ) } INTO list
                        ENDIF
                     ENDFOR
                  ELSE
                     IF ( @"Current Timestep" == @"24:00:00 October 15, Current Year" )
                     THEN
                        FOR ( DATETIME date IN @"Current Timestep" TO @"24:00:00 December 31, Current Year" ) WITH LIST list = {  } DO
                           IF ( date == @"24:00:00 October 15, Current Year" )
                           THEN
                              INSERT { date , $ "RulesData.TroutCriteria" ["Trout", "MinFlow"] } INTO list
                           ELSE
                              INSERT { date , "ComputeValueFromDate&YesterdayQ"( date, GET NUMERIC @INDEX 1.00000000 FROM GET LIST @INDEX 0.00000000 FROM list ) } INTO list
                           ENDIF
                        ENDFOR
                     ELSE
                        IF ( @"Current Timestep" == @"24:00:00 November 1, Current Year" )
                        THEN
                           FOR ( DATETIME date IN @"Current Timestep" TO @"24:00:00 December 31, Current Year" ) WITH LIST list = {  } DO
                              IF ( date == @"24:00:00 November 1, Current Year" )
                              THEN
                                 INSERT { date , "Max"( "Avg_Q_DuringBrownSpawnOct15Oct31"(  ), $ "RulesData.TroutCriteria" ["Trout", "MinFlow"] ) } INTO list
                              ELSE
                                 IF ( date == @"24:00:00 November 15, Current Year" )
                                 THEN
                                    INSERT { date , "Max"( "Avg_Q_DuringBrownSpawnOct15Oct31"(  ) * $ "RulesData.TroutCriteria" ["Trout", "IncubationPercent"], $ "RulesData.TroutCriteria" ["Trout", "MinFlow"] ) } INTO list
                                 ELSE
                                    INSERT { date , "ComputeValueFromDate&YesterdayQ"( date, GET NUMERIC @INDEX 1.00000000 FROM GET LIST @INDEX 0.00000000 FROM list ) } INTO list
                                 ENDIF
                              ENDIF
                           ENDFOR
                        ELSE
                           FOR ( DATETIME date IN @"Current Timestep" TO @"24:00:00 December 31, Current Year" ) WITH LIST list = {  } DO
                              IF ( date == @"24:00:00 November 15, Current Year" )
                              THEN
                                 INSERT { date , "Max"( "Min"( "Avg_Q_DuringBrownSpawnOct15Oct31"(  ), "Avg_Q_DuringBrownSpawnNov1Nov14"(  ) ) * $ "RulesData.TroutCriteria" ["Trout", "IncubationPercent"], $ "RulesData.TroutCriteria" ["Trout", "MinFlow"] ) } INTO list
                              ELSE
                                 INSERT { date , "ComputeValueFromDate&YesterdayQ"( date, GET NUMERIC @INDEX 1.00000000 FROM GET LIST @INDEX 0.00000000 FROM list ) } INTO list
                              ENDIF
                           ENDFOR
                        ENDIF
                     ENDIF
                  ENDIF
               ENDIF
            ENDIF
         ENDIF
      ENDIF
   ENDIF;
    END;
    FUNCTION       "ComputeValueFromDate&YesterdayQ" ( DATETIME date, NUMERIC yesterdayQ )
    RETURN_TYPE    NUMERIC;
    SCALE_UNITS    "";
    DESCRIPTION    "";
    ACTIVE         TRUE;
    PRE_EXEC_DIAG  TRUE;
    POST_EXEC_DIAG TRUE;
    BEGIN
      yesterdayQ;
    END;
  END;
END