;{CR10X}
;
;*********************************************
;*             TABLE 1 PROGRAM               *
;*  controls taking of manual sample and     *
;*  checks encoders and calculates upencoder,*
;*  dnencoder, upaccum, dnaccum, and         *
;*  calculates rain total                    *
;*********************************************
*Table 1 Program
  01: 10        Execution Interval (seconds)

;*********************************************
;* Instr.1 stores program version number     *
;* in the format y/mm/dd. Please update      *
;* version number if changes are made to     *
;* this program. Check website               *
;* www.http://www.pastures.org/chandlersys/  *
;* for current version number to ensure you  *
;* are updating the most recent version.     *
;*********************************************

1:  Z=F (P30)
 1: 30821    F
 2: 00       Exponent of 10
 3: 71       Z Loc [ PgVersion ]

;count rain pulses
2:  Pulse (P3)
 1: 1        Reps
 2: 1        Pulse Input Channel
 3: 2        Switch Closure, All Counts
 4: 1        Loc [ Precip_mm ]
 5: 0.254    Mult
 6: 0.0      Offset

;add pulses rain_acc to get total rain
3:  Z=X+Y (P33)
 1: 1        X Loc [ Precip_mm ]
 2: 66       Y Loc [ Rain_acc  ]
 3: 66       Z Loc [ Rain_acc  ]

;check manual switch pulses
4:  Pulse (P3)
 1: 1        Reps
 2: 2        Pulse Channel 2
 3: 2        Switch Closure, All Counts
 4: 40       Loc [ MANSAMPLE ]
 5: 1.0      Mult
 6: 0.0      Offset

;if manual switch pressed 3 or more times, take samples
5:  If (X<=>F) (P89)
 1: 40       X Loc [ MANSAMPLE ]
 2: 3        >=
 3: 3        F
 4: 30       Then Do

     ;allow samples to be taken when subroutine2 called
     6:  Do (P86)
      1: 11       Set Flag 1 High

     ;reset number of samples counter
     7:  Z=F (P30)
      1: -1       F
      2: 00       Exponent of 10
      3: 57       Z Loc [ N_of_samp ]

     ;flag3 low means bottle not full so samples can be taken
     8:  Do (P86)
      1: 23       Set Flag 3 Low

9:  End (P95)

;**************************************************************************
;* take samples and reset accumulated flow and counter every 6 hrs if     *
;* in 6hr independant sampling mode (flag 4), and not in 6hr dependant    *
;* sampling mode (flag 5)                                                 *
;**************************************************************************
;**************************************************************************
;* Using PC208W to remotely set user flags :                              *
;* 1. open PC208W                                                         *
;* 2. click on 'Connect' icon                                             *
;* 3. select appropriate CR10 from 'station list' window                  *
;* 4. click on 'terminal' tab (located on the bottom left)                *
;* 5. click on 'connect' (located on the bottom right)                    *
;* 6. type 7h and press enter                                             *
;* 7. type '*6' and press enter                                           *
;* 8. type 'D'                                                            *
;* -- display will show 0000.0000 which represents the 8 user flags (1-8) *
;* -- numbered from left to right                                         *
;* 9. type '4' to toggle flag 4 high                                      *
;* -- display will show 0001.0000                                         *
;* -- typing '4' again will toggle flag 4 low                             *
;* -- typing any number from 1-8 will toggle the corresponding flag       *
;* 10. click disconnect                                                   *
;**************************************************************************
;**************************************************************************
;* go to http://www.pastures.org/chandler/design/flag_descriptions.htm    *                                                                         *
;*  for a description of this programs user flags                         *
;*                                                                        *
;* go to http://www.pastures.org/chandler/design/how_to_set_flags.htm     *
;*  for more instructions on how to set flags with pc208w                 *
;*                                                                        *
;**************************************************************************

;if in 6hr independant sampling mode...
10:  If Flag/Port (P91)
 1: 14       Do if Flag 4 is High
 2: 30       Then Do

          ;and not in 6hr dependant sampling mode...
          11:  If Flag/Port (P91)
           1: 25       Do if Flag 5 is Low
           2: 30       Then Do

               ;and if 0 min into 6 hr interval then...
               12:  If time is (P92)
                1: 0000     Minutes (Seconds --) into a
                2: 360      Interval (same units as above)
                3: 30       Then Do

                         ;allow samples to be taken when subroutine2 called
                         13:  Do (P86)
                          1: 11       Set Flag 1 High

                         ;reset accumulated flow
                         14:  Z=F (P30)
                          1: 0.0      F
                          2: 00       Exponent of 10
                          3: 3        Z Loc [  ACC_qh1   ]

                         ;reset counter
                         15:  Z=F (P30)
                          1: 0.0      F
                          2: 00       Exponent of 10
                          3: 4        Z Loc [ COUNTER   ]

               ;end if time...
               16:  End (P95)

     ;end if flag/port 5...
     17:  End (P95)

;end if flag/port 4...
18:  End (P95)


;flag1 must be high, flag3 low to take samples
19:  If Flag/Port (P91)
 1: 11       Do if Flag 1 is High
 2: 30       Then Do

     20:  If Flag/Port (P91)
      1: 23       Do if Flag 3 is Low
      2: 30       Then Do

          ;Subroutine 2 activates pumps, timers
          21:  Do (P86)
           1: 2        Call Subroutine 2

     22:  End (P95)

23:  Else (P94)

     ;read sensors every 1 minute
     24:  If time is (P92)
      1: 0        Minutes (Seconds --) into a
      2: 1        Interval (same units as above)
      3: 30       Then Do

          ;Subroutine 79 sets offsets for bottom of culvert and staff gauges
          25:  Do (P86)
           1: 79       Call Subroutine 79


          ;get measurement from upencoder
          26:  SDI-12 Recorder (P105)
           1: 0        SDI-12 Address
           2: 0        Start Measurement (aM0!)
           3: 7        Port
           4: 6        Loc [ upencoder ]
           5: 1.0      Mult
           6: 0        Offset

          ;add offset to value measured by upencoder
          27:  Z=X+Y (P33)
           1: 6        X Loc [ upencoder ]
           2: 67       Y Loc [ HWOFFSET  ]
           3: 6        Z Loc [ upencoder ]

          ;if upencoder reading is < 35 ft then...
          28:  If (X<=>F) (P89)
           1: 6        X Loc [ upencoder ]
           2: 4        <
           3: 35       F
           4: 30       Then Do

               ;...add reading to upaccum
               29:  Z=X+Y (P33)
                1: 6        X Loc [ upencoder ]
                2: 7        Y Loc [ upaccum   ]
                3: 7        Z Loc [ upaccum   ]

               ;increase upcount by 1
               30:  Z=Z+1 (P32)
                1: 8        Z Loc [ upcount   ]

          31:  End (P95)

          ;get measurement from dnencoder
          32:  SDI-12 Recorder (P105)
           1: 0        SDI-12 Address
           2: 0        Start Measurement (aM0!)
           3: 8        Port
           4: 9        Loc [ dnencoder ]
           5: 1.0      Mult
           6: 0        Offset

          ;add offset to value measured by dnencoder
          33:  Z=X+Y (P33)
           1: 9        X Loc [ dnencoder ]
           2: 68       Y Loc [ TWOFFSET  ]
           3: 9        Z Loc [ dnencoder ]


          ;if dnencoder reading is < 35 ft then...
          34:  If (X<=>F) (P89)
           1: 9        X Loc [ dnencoder ]
           2: 4        <
           3: 35       F
           4: 30       Then Do

               ;...add reading to dnaccum
               35:  Z=X+Y (P33)
                1: 9        X Loc [ dnencoder ]
                2: 10       Y Loc [ dnaccum   ]
                3: 10       Z Loc [ dnaccum   ]

               ;increase dncount by 1
               36:  Z=Z+1 (P32)
                1: 11       Z Loc [ dncount   ]

          37:  End (P95)

     38:  End (P95)

39:  End (P95)

;********************************************
;*               TABLE 2 PROGRAM            *
;*  calls subroutine1 to calc flow and      *
;*  accum flow. Sets threshold, resets      *
;*  accum flow if >= threshold1, sets flag1 *
;*  high to allow samples to be taken       *
;********************************************
*Table 2 Program
  02: 1200      Execution Interval (seconds)

;calc avg HWE,TWE,FLOW20MIN(avg.flow), ACC_qh1(accumulated flow)
1:  Do (P86)
 1: 1        Call Subroutine 1

;loads/sets variable values and constants
2:  Do (P86)
1: 9        Call Subroutine 9

;store time
3:  Do (P86)
 1: 7        Call Subroutine 7

;************HERE***********************************************************************
;ACCUMULATED FLOW THRESHOLD IN CUBIC FT/S                                              *
; max bottle vol = 19ml, vol per sample = 150 ml, 1 week between manual retrieval,     *
; [7days * 24h * 60min] / 127(max sample capacity) = 80min                             *
;    F = [80(MIN) * 60(sec) * flow_peak] / 1200 sec (table 2 execution interval)       *
;    To change threshold modify flow_peak (set in subroutine 9, instr. 111 parameter 5 *
;***************************************************************************************
4:  Z=X*F (P37)
 1: 53       X Loc [ Flow_Peak ]
 2: 4        F
 3: 2        Z Loc [ Q_KT      ]

;checks CR10 battery voltage
5:  Batt Voltage (P10)
 1: 5        Loc [ BATVOL    ]

;allow output
6:  Do (P86)
 1: 10       Set Output Flag High

;designate final storage area
7:  Set Active Storage Area (P80)
 1: 1        Final Storage Area 1
 2: 80       Array ID

;output year
8:  Real Time (P77)
 1: 1000     Year (midnight = 0000)

;output julian day
9:  Sample (P70)
 1: 1        Reps
 2: 16       Loc [ Julian_2  ]

;output time
10:  Real Time (P77)
 1: 0010     Hour/Minute (midnight = 0000)

;high resolution = +- 99999 in final storage area
11:  Resolution (P78)
 1: 1        High Resolution

;output time
12:  Sample (P70)
 1: 1        Reps
 2: 24       Loc [ dec_hrmin ]

;output CR10 battery voltage
13:  Sample (P70)
 1: 1        Reps
 2: 5        Loc [ BATVOL    ]

;output accumulated rain
14:  Sample (P70)
 1: 1        Reps
 2: 66       Loc [ Rain_acc  ]

;output number of samples taken
15:  Sample (P70)
 1: 1        Reps
 2: 57       Loc [ N_of_samp ]

; output total offset of hw (upoffset + upsgoff)
;   upoffset = offset of encoder referenced to sea level
;   dnsgoff = offset of encoder referenced to pipe botom
16:  Sample (P70)
 1: 1        Reps
 2: 67       Loc [ HWOFFSET  ]

;output total offset of tw (dnoffset + dnsgoff)
17:  Sample (P70)
 1: 1        Reps
 2: 68       Loc [ TWOFFSET  ]

;output headwater
18:  Sample (P70)
 1: 1        Reps
 2: 12       Loc [ HWE       ]

;output tailwater
19:  Sample (P70)
 1: 1        Reps
 2: 13       Loc [ TWE       ]

;output average flow for 20 minutes
20:  Sample (P70)
 1: 1        Reps
 2: 14       Loc [ FLOW20MIN ]

;outputs accumulated flow
21:  Sample (P70)
 1: 1        Reps
 2: 3        Loc [ ACC_qh1   ]

;output program version number
22:  Sample (P70)
 1: 1        Reps
 2: 71       Loc [ PgVersion ]

;reset rain accumulation to 0
23:  Z=F (P30)
 1: 0.0      F
 2: 00       Exponent of 10
 3: 66       Z Loc [ Rain_acc  ]

;****************************************************************************
;* take samples and reset accumulated flow and counter if not in  6hr       *
;* independant sampling mode (flag 4), and if not in 6hr dependant sampling *
;* mode (flag 5) and accumulated flow >= to flow threshold                  *
;****************************************************************************

     ;if not in 6 hr independant sampling mode and...
     24:  If Flag/Port (P91)
      1: 24       Do if Flag 4 is Low
      2: 30       Then Do

          ;if not in 6 hr dependant sampling mode and...
          25:  If Flag/Port (P91)
           1: 25       Do if Flag 5 is Low
           2: 30       Then Do

               ;if accumulated flow is >= accumulated flow threshold then...
               26:  If (X<=>Y) (P88)
                1: 3        X Loc [ ACC_qh1   ]
                2: 3        >=
                3: 2        Y Loc [ Q_KT      ]
                4: 30       Then Do

                    ;set high so table1 will call subroutine2 and take a sample
                    27:  Do (P86)
                     1: 11       Set Flag 1 High

                    ;reset accumulated flow
                    28:  Z=F (P30)
                     1: 0.0      F
                     2: 00       Exponent of 10
                     3: 3        Z Loc [ ACC_qh1   ]

                    ;reset counter
                    29:  Z=F (P30)
                     1: 0.0      F
                     2: 00       Exponent of 10
                     3: 4        Z Loc [ COUNTER   ]

               ;end if accumulated flow...
               30:  End (P95)

     ;end if flag/port 5...
     31:  End (P95)

;end if flag/port 4...
32:  End (P95)



;************************************************************************
;* take samples and reset accumulated flow and counter every 6 hrs if   *
;* in 6hr dependant sampling mode (flag 5), and not in 6hr independant  *
;* sampling mode (flag 4) and if HWE and TWE >= 28.9                    *
;************************************************************************

;if in 6 hr dependant sampling mode and...
33:  If Flag/Port (P91)
 1: 15       Do if Flag 5 is High
 2: 30       Then Do

     ;if not in 6 hr independant sampling mode and...
     34:  If Flag/Port (P91)
      1: 24       Do if Flag 4 is Low
      2: 30       Then Do

               ;if 0 min into 6 hr interval and...
               35:  If time is (P92)
                1: 0000     Minutes (Seconds --) into a
                2: 360      Interval (same units as above)
                3: 30       Then Do

                    ;if TWE >= 28.9 and...
                    36:  If (X<=>F) (P89)
                     1: 13       X Loc [ TWE       ]
                     2: 3        >=
                     3: 28.9     F
                     4: 30       Then Do

                         ;if HWE >= 28.9...
                         37:  If (X<=>F) (P89)
                          1: 12       X Loc [ HWE       ]
                          2: 3        >=
                          3: 28.9     F
                          4: 30       Then Do

                              ;then allow output
                              38:  Do (P86)
                               1: 11       Set Flag 1 High

                              ;reset accumulated flow
                              39:  Z=F (P30)
                               1: 0.0      F
                               2: 00       Exponent of 10
                               3: 3        Z Loc [ ACC_qh1   ]

                              ;reset counter
                              40:  Z=F (P30)
                               1: 0.0      F
                               2: 00       Exponent of 10
                               3: 4        Z Loc [ COUNTER   ]

                         ;end if hwe...
                         41:  End (P95)

                    ;end if twe...
                    42:  End (P95)

               ;end if time is...
               43:  End (P95)

          ;end if flag/port 4...
          44:  End (P95)

     ;end if flag/port 5...
     45:  End (P95)


*Table 3 Subroutines

;**************************************************
;*                SUBROUTINE 1                    *
;*  calcs avg. HWE and TWE.  calcs avg. flow,     *
;*  accumul. flow, checks if TWE high enough to   *
;*  allow flow to occur                           *
;**************************************************
1:  Beginning of Subroutine (P85)
 1: 1        Subroutine 1

     ;checks for valid upcount encoder measurement
     ; if upcount = 0 (invalid) then set HWE to 0
     2:  If (X<=>F) (P89)
      1: 8        X Loc [ upcount   ]
      2: 1        =
      3: 0        F
      4: 30       Then Do

          3:  Z=F (P30)
           1: 0        F
           2: 00       Exponent of 10
           3: 12       Z Loc [ HWE       ]

     4:  Else (P94)

          ;checks for valid dncount encoder measurement
          ;if dncount = 0 (invalid) then set TWE to 0
          5:  If (X<=>F) (P89)
           1: 11       X Loc [ dncount   ]
           2: 1        =
           3: 0        F
           4: 30       Then Do

               6:  Z=F (P30)
                1: 0        F
                2: 00       Exponent of 10
                3: 13       Z Loc [ TWE       ]

          ;if valid encoder measurements
          7:  Else (P94)

               ;HWE = avg headwater flow accumulation
               8:  Z=X/Y (P38)
                1: 7        X Loc [ upaccum   ]
                2: 8        Y Loc [ upcount   ]
                3: 12       Z Loc [ HWE       ]

               ;reset upaccum
               9:  Z=F (P30)
                1: 0.0      F
                2: 00       Exponent of 10
                3: 7        Z Loc [ upaccum   ]

               ;reset upcount
               10:  Z=F (P30)
                1: 0.0      F
                2: 00       Exponent of 10
                3: 8        Z Loc [ upcount   ]

               ;TWE = avg tailwater flow accumulation
               11:  Z=X/Y (P38)
                1: 10       X Loc [ dnaccum   ]
                2: 11       Y Loc [ dncount   ]
                3: 13       Z Loc [ TWE       ]

               ;reset dnaccum
               12:  Z=F (P30)
                1: 0.0      F
                2: 00       Exponent of 10
                3: 10       Z Loc [ dnaccum   ]

               ;reset dncount
               13:  Z=F (P30)
                1: 0.0      F
                2: 00       Exponent of 10
                3: 11       Z Loc [ dncount   ]

;****************************************************************************************
;If TWE >= F(physical terrain parameter), there is flow, calculate flow

               14:  If (X<=>F) (P89)
                1: 13       X Loc [ TWE       ]
                2: 3        >=
                3: 28.9     F
                4: 30       Then Do

;*****************************************************************************************

                    ;copy diameter of culvert(Dia_1) to (W_Dia) location
                    15:  Z=X (P31)
                     1: 49       X Loc [ Dia_1     ]
                     2: 26       Z Loc [ W_Dia     ]

                    ;calculates flow based on avg HWE and avg TWE, outputs to Working_Q
                    16:  Do (P86)
                     1: 3        Call Subroutine 3

                    ;copy Working_Q to FLOW20MIN
                    17:  Z=X (P31)
                     1: 27       X Loc [ Working_Q ]
                     2: 14       Z Loc [ FLOW20MIN ]

                    ;adds avg flow for last 20min to accumulated flow
                    18:  Z=X+Y (P33)
                     1: 3        X Loc [ ACC_qh1   ]
                     2: 14       Y Loc [ FLOW20MIN ]
                     3: 3        Z Loc [ ACC_qh1   ]

                    ;increase counter by 1
                    19:  Z=Z+1 (P32)
                     1: 4        Z Loc [ COUNTER   ]

               ;if twe < (physical layout of terrain)(28.9)
               ;  then no flow, reset FLOW20MIN
               20:  Else (P94)

                    21:  Z=F (P30)
                     1: 0.0      F
                     2: 00       Exponent of 10
                     3: 14       Z Loc [ FLOW20MIN ]

               22:  End (P95)

          23:  End (P95)

     24:  End (P95)

25:  End (P95)

;*******************************************************
;*                   SUBROUTINE 2                      *
;*   takes upstream and downstream samples             *
;*   downstream samples taken first, followed by       *
;*   upstream                                          *
;*******************************************************
26:  Beginning of Subroutine (P85)
 1: 2        Subroutine 2

     ;when subroutine2 is called flag2 should be low to take downstream samples first
     27:  If Flag/Port (P91)
      1: 22       Do if Flag 2 is Low
      2: 30       Then Do

          ;if 0 minutes into 8 minute interval then...
          28:  If time is (P92)
           1: 0        Minutes (Seconds --) into a
           2: 8        Interval (same units as above)
           3: 30       Then Do

               ;...set port2 high to start downstream bilge pump
               29:  Do (P86)
                1: 42       Set Port 2 High

          30:  Else (P94)

               ;if downstream bilge pump is running and...
               31:  If Flag/Port (P91)
                1: 42       Do if Port 2 is High
                2: 30       Then Do

                    ;...if 4min into 8min interval...
                    32:  If time is (P92)
                     1: 4        Minutes (Seconds --) into a
                     2: 8        Interval (same units as above)
                     3: 30       Then Do

                         ;...then start small downstream pump
                         33:  Do (P86)
                          1: 44       Set Port 4 High

                    34:  Else (P94)

                         ;if 6min into 8min interval...
                         35:  If time is (P92)
                          1: 6        Minutes (Seconds --) into a
                          2: 8        Interval (same units as above)
                          3: 30       Then Do

                              ;stop downstream bilge pump
                              36:  Do (P86)
                               1: 52       Set Port 2 Low

                         37:  End (P95)

                    38:  End (P95)

               39:  Else (P94)

                    ;if downstream bilge pump is not running and small dnstream pump is, and...
                    40:  If Flag/Port (P91)
                     1: 44       Do if Port 4 is High
                     2: 30       Then Do

                         ;...if 7min into 8min interval...
                         41:  If time is (P92)
                          1: 7        Minutes (Seconds --) into a
                          2: 8        Interval (same units as above)
                          3: 30       Then Do

                              ;...then stop small dnstream pump and...
                              42:  Do (P86)
                               1: 54       Set Port 4 Low

                              ;...set flag for upstream samples to be taken
                              43:  Do (P86)
                               1: 12       Set Flag 2 High

                         44:  End (P95)

                    45:  End (P95)

               46:  End (P95)

          47:  End (P95)

     ;if flag2 high allow start of upstream sampling (set high in instr 44)
     48:  Else (P94)

          ;if 0min into 8min interval...
          49:  If time is (P92)
           1: 0        Minutes (Seconds --) into a
           2: 8        Interval (same units as above)
           3: 30       Then Do

               ;...then activate upstream bilge pump
               50:  Do (P86)
                1: 41       Set Port 1 High

          51:  Else (P94)

               ;if upstream bilge pump running and...
               52:  If Flag/Port (P91)
                1: 41       Do if Port 1 is High
                2: 30       Then Do

                    ;...4min into 8min interval...
                    53:  If time is (P92)
                     1: 4        Minutes (Seconds --) into a
                     2: 8        Interval (same units as above)
                     3: 30       Then Do

                         ;then activate small upstream pump
                         54:  Do (P86)
                          1: 43       Set Port 3 High

                    55:  Else (P94)

                         ;if 6min into 8min interval...
                         56:  If time is (P92)
                          1: 6        Minutes (Seconds --) into a
                          2: 8        Interval (same units as above)
                          3: 30       Then Do

                              ;...then stop upstream bilge pump
                              57:  Do (P86)
                               1: 51       Set Port 1 Low

                         58:  End (P95)

                    59:  End (P95)

               60:  Else (P94)

                    ;if upstream bilge not running and small upstream pump is, and...
                    61:  If Flag/Port (P91)
                     1: 43       Do if Port 3 is High
                     2: 30       Then Do

                         ;...if 7min into 8min interval...
                         62:  If time is (P92)
                          1: 7        Minutes (Seconds --) into a
                          2: 8        Interval (same units as above)
                          3: 30       Then Do

                              ;...then stop small upstream pump and...
                              63:  Do (P86)
                               1: 53       Set Port 3 Low

                              ;...reset control to large bilge pumps for next cycle of sampling and...
                              64:  Do (P86)
                               1: 22       Set Flag 2 Low

                              ;...initialize to do not sample mode and...
                              65:  Do (P86)
                               1: 21       Set Flag 1 Low

                              ;...increment number of samples taken counter and...
                              66:  Z=Z+1 (P32)
                               1: 57       Z Loc [ N_of_samp ]

                              ;...if number of samples >= 120, halt sampling (bottle almost full)
                              67:  If (X<=>F) (P89)
                               1: 57       X Loc [ N_of_samp ]
                               2: 3        >=
                               3: 120      F
                               4: 13       Set Flag 3 High

                         68:  End (P95)

                    69:  End (P95)

               70:  End (P95)

          71:  End (P95)

     72:  End (P95)

73:  End (P95)

;**********************************************************
;*                   SUBROUTINE 3                         *
;*    Calculates flow based on HWE and TWE and stores it  *
;*    in Working_Q                                        *
;**********************************************************
74:  Beginning of Subroutine (P85)
 1: 3        Subroutine 3

     ;if HWE = TWE (no flow)
     75:  If (X<=>Y) (P88)
      1: 12       X Loc [ HWE       ]
      2: 1        =
      3: 13       Y Loc [ TWE       ]
      4: 30       Then Do

          ;...set Working_Q to 0
          76:  Z=F (P30)
           1: 0.0      F
           2: 00       Exponent of 10
           3: 27       Z Loc [ Working_Q ]

     77:  End (P95)

     ;if HWE < TWE (backflow)...
     78:  If (X<=>Y) (P88)
      1: 12       X Loc [ HWE       ]
      2: 4        <
      3: 13       Y Loc [ TWE       ]
      4: 30       Then Do

          ;...set Working_Q to 0
          79:  Z=F (P30)
           1: 0        F
           2: 00       Exponent of 10
           3: 27       Z Loc [ Working_Q ]

     ;else if hwe > twe then flow so...
     80:  Else (P94)

          ;instr. 82-85 calculate total area and perimeter of culvert
          81:  Z=X*F (P37)
           1: 26       X Loc [ W_Dia     ]
           2: 0.5      F
           3: 28       Z Loc [ Radius    ]

          82:  Z=X^Y (P47)
           1: 28       X Loc [ Radius    ]
           2: 45       Y Loc [ Exp_2     ]
           3: 29       Z Loc [ Radius_Sq ]

          83:  Z=X*Y (P36)
           1: 42       X Loc [ Pi        ]
           2: 26       Y Loc [ W_Dia     ]
           3: 30       Z Loc [ Crcmfrnce ]

          84:  Z=X*Y (P36)
           1: 42       X Loc [ Pi        ]
           2: 29       Y Loc [ Radius_Sq ]
           3: 31       Z Loc [ Tot_Area  ]

          ;instr 86 and 87 set values for full pipe formula
          85:  Z=X (P31)
           1: 44       X Loc [ K         ]
           2: 32       Z Loc [ KE        ]

          86:  Z=X*F (P37)
           1: 26       X Loc [ W_Dia     ]
           2: 1        F
           3: 33       Z Loc [ Crit_Dpth ]

          ;HWE(called Head_in for formula) = HWE - HWELEV
          ;   HWELEV = elevation of bottom of HW pipe, set in subroutine79
          87:  Z=X-Y (P35)
           1: 12       X Loc [ HWE       ]
           2: 64       Y Loc [ HWELEV    ]
           3: 34       Z Loc [ Head_In   ]

          ;TWE(called Head_out for formula) = TWE - TWELEV
          ;   TWELEV = elevation of bottom of TW pipe, set in subroutine79
          88:  Z=X-Y (P35)
           1: 13       X Loc [ TWE       ]
           2: 65       Y Loc [ TWELEV    ]
           3: 35       Z Loc [ Head_Out  ]

          ;instr.90 - 102 calculates flow with full pipe formula
          89:  Z=X-Y (P35)
           1: 12       X Loc [ HWE       ]
           2: 13       Y Loc [ TWE       ]
           3: 36       Z Loc [ Head      ]

          90:  Z=X*F (P37)
           1: 26       X Loc [ W_Dia     ]
           2: 0.25     F
           3: 37       Z Loc [ Hyd_Rad   ]

          91:  Z=X^Y (P47)
           1: 43       X Loc [ Manning   ]
           2: 45       Y Loc [ Exp_2     ]
           3: 38       Z Loc [ Man_Sq    ]

          92:  Z=X*F (P37)
           1: 38       X Loc [ Man_Sq    ]
           2: 29.1     F
           3: 39       Z Loc [ KF        ]

          93:  Z=X*Y (P36)
           1: 39       X Loc [ KF        ]
           2: 52       Y Loc [ Length_1  ]
           3: 39       Z Loc [ KF        ]

          94:  Z=X^Y (P47)
           1: 37       X Loc [ Hyd_Rad   ]
           2: 48       Y Loc [ Exp_13333 ]
           3: 37       Z Loc [ Hyd_Rad   ]

          95:  Z=X/Y (P38)
           1: 39       X Loc [ KF        ]
           2: 37       Y Loc [ Hyd_Rad   ]
           3: 39       Z Loc [ KF        ]

          96:  Z=X+Y (P33)
           1: 32       X Loc [ KE        ]
           2: 39       Y Loc [ KF        ]
           3: 27       Z Loc [ Working_Q ]

          97:  Z=X+F (P34)
           1: 27       X Loc [ Working_Q ]
           2: 1        F
           3: 27       Z Loc [ Working_Q ]

          98:  Z=X/Y (P38)
           1: 36       X Loc [ Head      ]
           2: 27       Y Loc [ Working_Q ]
           3: 27       Z Loc [ Working_Q ]

          99:  Z=X*F (P37)
           1: 27       X Loc [ Working_Q ]
           2: 64.4     F
           3: 27       Z Loc [ Working_Q ]

          100:  Z=SQRT(X) (P39)
           1: 27       X Loc [ Working_Q ]
           2: 27       Z Loc [ Working_Q ]

          101:  Z=X*Y (P36)
           1: 27       X Loc [ Working_Q ]
           2: 31       Y Loc [ Tot_Area  ]
           3: 27       Z Loc [ Working_Q ]

          ;if Head_Out (TW) < Diameter (Crit_Dpth) there is flow...
          102:  If (X<=>Y) (P88)
           1: 35       X Loc [ Head_Out  ]
           2: 4        <
           3: 33       Y Loc [ Crit_Dpth ]
           4: 30       Then Do

               ;...then calculate hfraction and...
               103:  Z=X/Y (P38)
                1: 35       X Loc [ Head_Out  ]
                2: 33       Y Loc [ Crit_Dpth ]
                3: 69       Z Loc [ hfraction ]

               ;...calculate qfraction and...
               104:  Polynomial (P55)
                1: 1        Reps
                2: 69       X Loc [ hfraction ]
                3: 70       F(X) Loc [ qfraction ]
                4: 0.0043   C0
                5: -0.2857  C1
                6: 4.7431   C2
                7: -7.5127  C3
                8: 8.3483   C4
                9: -4.2713  C5

               ;...add qfraction to Working_Q to get final Working_Q value
               105:  Z=X*Y (P36)
                1: 27       X Loc [ Working_Q ]
                2: 70       Y Loc [ qfraction ]
                3: 27       Z Loc [ Working_Q ]

          106:  End (P95)

     107:  End (P95)

108:  End (P95)

;******************************************
;*           SUBROUTINE 9                 *
;*  loads variable values and constants   *
;******************************************
109:  Beginning of Subroutine (P85)
 1: 9        Subroutine 9

     ;loads 8 consecutive input locations
     110:  Bulk Load (P65)
      1: 2        F ;               ;Dia_1
      2: 2        F ;               ;Dia_2
      3: 1.5      F ;               ;B_Elev
      4: 38       F ;               ;Length_1
      5: 7        F ;               ;Flow_Peak
      6: 0        F ;               ;Dia_6
      7: 0        F ;               ;Dia_7
      8: 0        F ;               ;Dia_8
      9: 49       Loc [ Dia_1     ] ;starting point

     ;loads 8 consecutive input locations
     111:  Bulk Load (P65)
      1: 1.49     F ;               ;Const1_49
      2: 3.1415   F ;               ;Pi
      3: .024     F ;               ;Manning
      4: .7       F ;               ;K
      5: 2        F ;               ;Exp_2
      6: .5       F ;               ;Exp_05
      7: .6667    F ;               ;Exp_06667
      8: 1.3333   F ;               ;Exp_13333
      9: 41       Loc [ Const1_49 ] ;starting point

112:  End (P95)

;******************************************
;*            SUBROUTINE 7                *
;* stores year, day, hr, min, and seconds *
;******************************************
113:  Beginning of Subroutine (P85)
 1: 7        Subroutine 7

     114:  Time (P18)
      1: 3        Store Year,Day,Hr,Min,Sec in 5 consecutive locations
      2: 0        Mod/By
      3: 15       Loc [ Julian_1  ]

     115:  Z=F (P30)
      1: 24       F
      2: 0        Exponent of 10
      3: 20       Z Loc [ hour_24   ]

     116:  Z=F (P30)
      1: 1440     F
      2: 0        Exponent of 10
      3: 21       Z Loc [ min_1440  ]

     117:  Z=X/Y (P38)
      1: 17       X Loc [ Julian_3  ]
      2: 20       Y Loc [ hour_24   ]
      3: 22       Z Loc [ dec_hr    ]

     118:  Z=X/Y (P38)
      1: 18       X Loc [ Julian_4  ]
      2: 21       Y Loc [ min_1440  ]
      3: 23       Z Loc [ dec_min   ]

     119:  Z=X+Y (P33)
      1: 22       X Loc [ dec_hr    ]
      2: 23       Y Loc [ dec_min   ]
      3: 24       Z Loc [ dec_hrmin ]

     120:  Z=X+Y (P33)
      1: 16       X Loc [ Julian_2  ]
      2: 24       Y Loc [ dec_hrmin ]
      3: 25       Z Loc [ dec_jul   ]

121:  End (P95)

;****************************************
;*             SUBROUTINE 79            *
;* sets upstream and downstream offsets *
;****************************************
122:  Beginning of Subroutine (P85)
 1: 79       Subroutine 79

     ;loads 8 consecutive input locations
     123:  Bulk Load (P65)
      1: 28       F  ;              ;upoffset(upstream culvert offset)
      2: 2.61     F ;               ;upsgoff(upstream staff gauge offset)
      3: 0        F ;               ;zero
      4: 0        F ;               ;zero
      5: 28.01    F ;               ;dnoffset(downstream culvert offset)
      6: 2.44     F ;               ;dnsgoff(downstream staff gauge offset
      7: 28       F ;               ;hwelev(elevation of bottom of upstream pipe)
      8: 28.1     F ;               ;twelev(elevation of bottom of downstream pipe)
      9: 58       Loc [ UPOFFSET  ] ;starting point

     ;upoffset + upsgoff = hwoffset
     124:  Z=X+Y (P33)
      1: 58       X Loc [ UPOFFSET  ]
      2: 59       Y Loc [ UPSGOFF   ]
      3: 67       Z Loc [ HWOFFSET  ]


     ;dnoffset + dnsgoff = twoffset
     125:  Z=X+Y (P33)
      1: 62       X Loc [ DNOFFSET  ]
      2: 63       Y Loc [ DNSGOFF   ]
      3: 68       Z Loc [ TWOFFSET  ]

126:  End (P95)

End Program

-Input Locations-
1 Precip_mm 1 1 1
2 Q_KT      1 1 1
3 ACC_qh1   1 3 4
4 COUNTER   1 0 4
5 BATVOL    1 1 1
6 upencoder 1 2 0
7 upaccum   1 2 2
8 upcount   1 2 2
9 dnencoder 1 2 0
10 dnaccum   1 2 2
11 dncount   1 2 2
12 HWE       1 6 2
13 TWE       1 7 2
14 FLOW20MIN 1 2 2
15 Julian_1  5 0 1
16 Julian_2  9 2 1
17 Julian_3  9 1 1
18 Julian_4  9 1 1
19 Julian_5  17 0 1
20 hour_24   1 1 1
21 min_1440  1 1 1
22 dec_hr    1 1 1
23 dec_min   1 1 1
24 dec_hrmin 1 2 1
25 dec_jul   1 0 1
26 W_Dia     1 4 1
27 Working_Q 1 7 9
28 Radius    1 1 1
29 Radius_Sq 1 1 1
30 Crcmfrnce 1 0 1
31 Tot_Area  1 1 1
32 KE        1 1 1
33 Crit_Dpth 1 2 1
34 Head_In   1 0 1
35 Head_Out  1 2 1
36 Head      1 1 1
37 Hyd_Rad   1 2 2
38 Man_Sq    1 1 1
39 KF        1 3 3
40 MANSAMPLE 1 1 1
41 Const1_49 5 0 1
42 Pi        9 2 1
43 Manning   9 1 1
44 K         9 1 1
45 Exp_2     9 2 1
46 Exp_05    9 0 1
47 Exp_06667 9 0 1
48 Exp_13333 17 1 1
49 Dia_1     5 1 1
50 Dia_2     9 0 1
51 B_Elev    9 0 1
52 Length_1  9 1 1
53 Flow_Peak 9 1 1
54 Dia_6     9 0 1
55 Dia_7     9 0 1
56 Dia_8     17 0 1
57 N_of_samp 1 2 2
58 UPOFFSET  5 1 1
59 UPSGOFF   9 1 1
60 UPOFFSE_3 9 0 1
61 UPOFFSE_4 9 0 1
62 DNOFFSET  9 1 1
63 DNSGOFF   9 1 1
64 HWELEV    9 1 1
65 TWELEV    17 1 1
66 Rain_acc  1 2 2
67 HWOFFSET  1 1 1
68 TWOFFSET  1 1 1
69 hfraction 1 1 1
70 qfraction 1 1 1
71 PgVersion 1 1 1
-Program Security-
1280
0000
0000
-Mode 4-
-Final Storage Area 2-
0
-CR10X ID-
0
-CR10X Power Up-
3
