/*
 * SUMMARY:      CutBankGeometry.c - Calculates effects of cut banks
 * USAGE:        Part of DHSVM
 *
 * AUTHOR:       Mark Wigmosta
 * ORG:          Batelle Pacific Northwest Laboratories
 * E-MAIL:       ms_wigmosta@pnl.gov
 * ORIG-DATE:    Jul-96
 * Last change: Wed Feb  3 23:04:53 1999 by Bart Nijssen <nijssen@u.washington.edu>
 * DESCRIPTION:  This subroutine calculates corrections to adjust for the
 *               effects of road cut-banks and channels in grid cells.  It
 *               also add precip and updates the upper zone soil moisture.
 *               If the water table is below the road / channel bed precip is
 *               added to the coresponding zone. 
 * DESCRIP-END.
 * FUNCTIONS:    CutBankGeometry()
 * COMMENTS:     
 */

#ifndef lint
static char vcid[] = "$Id: CutBankGeometry.c,v 1.2 2001/03/31 00:10:21 waichler Exp $";
#endif /* lint */

#include <assert.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include "settings.h"
#include "soilmoisture.h"

/*****************************************************************************
  Function name: CutBankGeometry()

  Purpose      : This subroutine calculates corrections to adjust for the
                 effects of road cut-banks and channels in grid cells.  It
                 also add precip and updates the upper zone soil
                 moisture.  If the water table is below the road /
                 channel bed precip is added to the coresponding zone.
	
  Required     :
    int i            - Number of the soil layer being processed.
    float RootDepth  - Depth of the soil layer being processed (m)
    float TopZone    - Distance from the ground surface to top of zone i (m) 
    float BankHeight - Distance from ground surface to channel bed or bottom
                       of road-cut (m) 
    float Width      - Width of channel or road surface (m)
    float DX         - Grid cell width (m)

  Returns      : void

  Modifies     :
    float *PercWidth - Width of the bottom of zone i, for perc (m/m)
    float *Adjust    - Corrects for loss of soil storage due to channel/
                       road-cut.  Multiplied with RootDepth to give the zone
                       thickness for use in calculating soil moisture
    int *CutBankZone - Number of the soil layer containing the bottom of the
                       cut-bank.  Used in UnsaturatedFlow to check for
                       surface runoff.  If BankHeight = 0.; CutBankZone =
                       NO_CUT 

  Comments     :
    Schematic: 
    
    <-----------------------------------DX---------------------------------->
    |====================|     -                     |======================|
    |      ^             |     |                     |   |                  |
    |      TopZone[0]    |<----|-----Width---------->|   |- RootDepth[0]    |
    |                    |     |                     |   |                  |
    |<--PercWidth*DX---->|     |                     |<--|-PercWidth*DX---->|
    |                    |     |-BankHeight          |   V                  |
    |====================|     |                     |======================|
    |       ^            |     |                     |   |                  |
    |       TopZone[1]   |     |                     |   |-RootDepth[1]     |
    |                    |     V                     |   |                  |
    |                    |---------------------------|   |                  |
    |                                                    |                  |
    |                             CutBankZone            V                  |
    |=======================================================================|
    |                                                                       |
    |                                                                       |
    |=======================================================================|    

    Note: if the cut bank is cause by a road instead of a channel, then the 
          schematic would look as follows:

          |=================|
          |                 |			  
          |=================|			  
          |                 |_______________
          |                                 |
          |=================================|
          |                                 |
          |=================================|
*****************************************************************************/
void CutBankGeometry(int i, float RootDepth, float TopZone,float BankHeight,  
                     float Width, float DX, float *PercWidth, float *Adjust,
                     int *CutBankZone) 
{
  *PercWidth = 1.0;
  *Adjust = 1.0;
  
  if (BankHeight > 0.0) {
    if (BankHeight <= TopZone) {   
      /* below cut depth - full width */
      *PercWidth = 1.0;
      *Adjust = 1.0;
    }
    else{
      if (BankHeight <= (TopZone + RootDepth)) {  
        /* cut depth in this zone partial width*/
        *PercWidth = 1.0;
        *Adjust = 1.0 - (Width * (BankHeight - TopZone) / (RootDepth*DX));
        *CutBankZone = i;	
      }
      else {	 
        /* above cut depth - less than full width  */
        assert(DX >= Width);
        *PercWidth = (DX - Width) / DX;
        *Adjust = *PercWidth;
      }
    }
  } 
}
