!-------------------------------------- LICENCE BEGIN ------------------------------------
!Environment Canada - Atmospheric Science and Technology License/Disclaimer, 
!                     version 3; Last Modified: May 7, 2008.
!This is free but copyrighted software; you can use/redistribute/modify it under the terms 
!of the Environment Canada - Atmospheric Science and Technology License/Disclaimer 
!version 3 or (at your option) any later version that should be found at: 
!http://collaboration.cmc.ec.gc.ca/science/rpn.comm/license.html 
!
!This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
!without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
!See the above mentioned License/Disclaimer for more details.
!You should have received a copy of the License/Disclaimer along with this software; 
!if not, you can write to: EC-RPN COMM Group, 2121 TransCanada, suite 500, Dorval (Quebec), 
!CANADA, H9P 1J3; or send e-mail to service.rpn@ec.gc.ca
!-------------------------------------- LICENCE END --------------------------------------
***S/P CALCCOVF
**
*
#include "phy_macros_f.h"

      SUBROUTINE CALCCOVF( E, ESIZ, F, FSIZ, KOUNT, NI ) 1
*
#include "impnone.cdk"
*
      INTEGER NI, KOUNT
      INTEGER ESIZ, FSIZ
      REAL    E(FSIZ), F(FSIZ)
*
*
*Author
*       Aude Lemonsu (Oct 2004)
*
*Object
*       Calculation of covf
*
*Revision
* 001      A. Lemonsu (Dec 2005) Change urban parameters
*
*Arguments
*
*       - Input/Ouput -
* F        field for permanent physics variables
*       - Input -
* FSIZ     dimension of F
* KOUNT    current timestep number
* NI       horizontal slice dimension
*
*
#include "nclassvg.cdk"
#include "tebcst.cdk"
*
**
*
#include "options.cdk"
#include "phybus.cdk"
*
*
*                           the geophysical fields determined from
*                           vegetation are done so using the following
*                           classification:
*
*     Class       Vegetation type
*     =====       ===============
*       1         (salt) water
*       2         ice
*       3         inland lake
*       4         evergreen needleleaf trees
*       5         evergreen broadleaf trees
*       6         deciduous needleleaf trees
*       7         deciduous broadleaf trees
*       8         tropical broadleaf trees
*       9         drought deciduous trees
*       10        evergreen broadleaf shrub
*       11        deciduous shrubs
*       12        thorn shrubs
*       13        short grass and forbs
*       14        long grass
*       15        crops
*       16        rice
*       17        sugar
*       18        maize
*       19        cotton
*       20        irrigated crops
*       21        urban
*       22        tundra
*       23        swamp
*       24        desert
*       25        mixed shrubs
*       26        mixed wood forests
*
*                           the geophysical fields determined from
*                           urban covers are done so using the following
*                           classification:
*
*     Class       Urban type      
*     =====       ==========
*       1         High buildings
*       2         Mid-high buildings 
*       3         Low buildings      
*       4         Very low buildings        
*       5         Industrial areas   
*       6         Sparse buildings 
*       7         Roads and parkings
*       8         Road borders   
*       9         High-density suburbs
*       10        Mid-density suburbs
*       11        Low-density suburbs
*       12        Mix built and nature
*
*
*
*
*********************************************************************
*                              FRACTIONS 
*********************************************************************
*
*     Fraction of built-up areas by urban class
      REAL town(NCLASSURB)
*
      DATA town/   
     1             0.95, 0.90, 0.80, 0.70, 0.70, 
     1             0.40, 0.85, 0.70, 0.44, 0.27, 
     1             0.18, 0.25                  /
*
*     Fraction of trees by urban class
      REAL frac_tree(NCLASSURB)
*
      DATA frac_tree/
     1             0.02, 0.05, 0.05, 0.10, 0.00,
     1             0.30, 0.05, 0.15, 0.28, 0.36,
     1             0.47, 0.37                  / 
*
*     Fraction of grassland by urban class
      REAL frac_gras(NCLASSURB)
*
      DATA frac_gras/
     1             0.03, 0.05, 0.05, 0.20, 0.00,
     1             0.30, 0.10, 0.15, 0.28, 0.37,
     1             0.35, 0.38                  /
*
*     Fraction of bare soil by urban class
      REAL frac_soil(NCLASSURB)
*
      DATA frac_soil/
     1             0.00, 0.00, 0.00, 0.00, 0.30,
     1             0.00, 0.00, 0.00, 0.00, 0.00,
     1             0.00, 0.00                  /
*
*     Dynamic roughness length for natural classes
      REAL z0veg(NCLASS)
* 
      DATA z0veg/
     1             0.001, 0.001, 0.001, 1.5 , 3.5 ,  
     1             1.0  ,  2.0 ,  3.0 , 0.8 , 0.05, 
     1             0.15 ,  0.15,  0.02, 0.08, 0.08, 
     1             0.08 ,  0.35,  0.25, 0.1 , 0.08, 
     1             1.35 ,  0.01,  0.05, 0.05, 1.5 , 
     1             0.05                       /
*
*********************************************************************
*
*
      INTEGER I, J
      REAL    sumcovf, sumurb
      REAL    lnz0
*
*
*************************************************************************
*                VEGETATION AND NATURAL SOIL FRACTIONS                
*************************************************************************
*
*       The urbanized classes contain one fraction 
*       of natural soils and vegetation
*
*       Classes                      Built-up    Grass       Tree        Bare soil 
*                                    fraction    fraction    fraction    fraction
*       --------------------------------------------------------------------------
*       1  High buildings            95%         03%         02%         00%
*       2  Mid-high buildings        90%         05%         05%         00%
*       3  Low buildings             90%         05%         05%         00%
*       4  Very low buildings        85%         10%         05%         00%
*       5  Industrial areas          85%         00%         00%         15%
*       6  Sparse buildings          40%         30%         30%         00%
*       7  Roads and parkings        98%         02%         02%         00%
*       8  Road borders              70%         15%         15%         00%
*       9  High-density suburbs      44%         28%         28%         00%
*       10 Mid-density suburbs       27%         37%         36%         00%
*       11 Low-density suburbs       18%         35%         47%         00%
*       12 Mix built and nature      25%         38%         37%         00%
*
*
        if (ischmurb .EQ. 1) then
*
         do i=0,ni-1
*
           f(urban+i) = 0.
           sumurb     = 0.
           sumcovf    = 0.
           lnz0       = 0.
*
*        Calculation of urban fraction and built-up fraction
           do j=1,nclassurb
            sumurb                    = sumurb     + e(urbfen+(j-1)*ni+i)
            f(urban+i)                = f(urban+i) + e(urbfen+(j-1)*ni+i)
     1                                               *town(j)
            f(urbf +(j-1)*ni+i)       = e(urbfen+(j-1)*ni+i)
            f(covf+(nclass+j-1)*ni+i) = e(urbfen+(j-1)*ni+i)*town(j)
           end do 
*
*        Calculation of covf(1-26) according to the urban fraction 
           do j=1,nclass
            f(covf+(j-1)*ni+i)        = e(vegfen+(j-1)*ni+i)*(1-sumurb)
           end do
*
*        Calculation of covf according to the vegetation part of the urban classes
           do j=1,nclassurb
            f(covf+i+ni*(class_tree-1)) = f(covf+i+ni*(class_tree-1)) 
     1                                  + e(urbfen+i+ni*(j-1))*frac_tree(j)
            f(covf+i+ni*(class_gras-1)) = f(covf+i+ni*(class_gras-1)) 
     1                                  + e(urbfen+i+ni*(j-1))*frac_gras(j)
            f(covf+i+ni*(class_soil-1)) = f(covf+i+ni*(class_soil-1)) 
     1                                  + e(urbfen+i+ni*(j-1))*frac_soil(j)
           end do
*
*        Calculation of sumcovf
           do j=1,nclass
            sumcovf = sumcovf + f(covf+(j-1)*ni+i)
           end do
*
*        Removal of Class 21
           if (f(covf+i+ni*(class_urb-1)) .gt. 0. .and. 
     1         f(covf+i+ni*(class_urb-1)) .lt. sumcovf) then
            do j=1,nclass
             if (j .ne. class_urb) then
*             covf calculation
              f(covf+(j-1)*ni+i) = f(covf+(j-1)*ni+i)*sumcovf
     1                             /(sumcovf-f(covf+i+ni*(class_urb-1)))
*             dynamical roughness calculation
              lnz0 = lnz0 + f(covf+(j-1)*ni+i)*ALOG(z0veg(j))/sumcovf
             endif
            end do
            f(covf+i+ni*(class_urb-1)) = 0.
            e(z0en+i) = EXP(lnz0)
           endif
*
*        Limitation of urban
         if (f(urban+i)<1.E-3) f(urban+i)=0.
*
         end do
*
*
        else if (ischmurb .EQ. 0) then
*
         do i=0,ni-1
*         Non-urban classes : covf = vegfen
          do j=1,nclass
           f(covf+(j-1)*ni+i) = e(vegfen+(j-1)*ni+i)
          end do
*         Urban classes : covf = 0
          do j=nclass+1,nclass+nclassurb
           f(covf+(j-1)*ni+i) = 0.
          end do
*         Town fraction=0
          f(urban+i) = 0.
         enddo
*
        endif
*
*
      RETURN
      END