module yomcmemtypes

! This module contains all variables needed to run CMEM
! organized as
!
! general fields  (cmem_surface type)
! and subtypes:
!      soil variables  (cmem_soils type)
!      vegetation variables (cmem_veget type)
!      atmosphere variables (cmem_atmos type)
!
!
! Description :
!
! 1.- Module containing CMEM I/O datafields (cmem_surface type)
!
! ftair : Air temperature at screen level (2m) (K)
! ftskin : skin temperature
! ftveg : Canopy temperature (K)
! ftskin : Skin Temperature (K)
! ftsurf : Surface Temperature 0-10cm (K)
! ftl2 : Soil Temperature layer 2 at 10-35cm (K)
! ftdeep : Deep soil Temperature at 35-100cm (K)
!
! fwc : water content of surface layer 1 [m3/m3]
! fwc2 : water content of layer 2 [m3/m3]
! fwc3 : water content of layer 3 [m3/m3]
!
! ftb_soil  surface brightness temperature (K)
! ftb_toa  TOA brightness temperature (K)
! fsurf_emis : global surface emissivity [-]
!
! fsand [%]
! fclay [%]
! frho_b : bulk density  [kg/m3]
! fp : porosity [%]
! fWP : Wilting point [%]
! falpha : alpha for WANG dielectric model
!
! ftau_atm
! ftb_ad [K]
! ftb_au [K]
!
! fsnowd : snow depth, water equivalent (m)
! frsnow : snow density (kg/m3)
! fLSM(:)   
! fTVL(:)    
! fTVH(:)    
! ftfrac(:,:)  
! fwater : water fraction (0-1)
! mask(:) : point on which TB is going to be computed (1), or masked (0)
!  fs_laiL : global LAI for Low vegetation of ecoclimap
!  fs_laiH : global LAI for High vegetation of ecoclimap, NOT USED
!
! fwc_veg : vegetation water content for low (1) and high vegetation (2) [kg/m3]
! fh : effective roughness height [cm]
! fb : b parameter (Jackson)
! fsal_wat : salinity of water  (psu = ppt(weight) )
! ftauN: vegetation optical thickness at Nadir
!
! Control variables :
!  N : number of data cells in field
!  JJ : current field cell of computation
!  JCMEMTILE : current tile
! for ascci input/outputs : doy,ndate,ntime,nbpt,NFCASCMAX
!

! 2.- Variables containing the soil emissivity model for a single cell (cmem_soil type)
!
! Soil variables :
!   tb_soil : soil brightness temperature (K), Index 1 = hor. pol., 2 = ver. pol.
!   tsoil : temperature of each soil layer (K)
!   tsoildeep : temperature of deep soil layer (10-35cm?) (K)
!   tc : soil temperature in Celsius (C)
!   zsoil :  thickness of the soil layers (cm)
!   z1: depth of center of layer 1 (m)
!   z2: depth of center of layer 2 (m)
!   z3: depth of center of layer 3 (m)
!   wc1 :  volumetric soil water content of surface (cm3/cm3)
!   wcsoil :  volumetric soil water content of profile (cm3/cm3)
!   wc :  volumetric soil water content of layer (cm3/cm3)
!   t_eff : effective soil temperature (K), Index 1 = hor. pol., 2 = ver. pol.
!   surf_emis : surface emissivity  (-), Index 1 = hor. pol., 2 = ver. pol.
!   r_s : reflectivity of smooth soil surface (-), index 1 = h-pol., 2 = v-pol.
!   r_r : reflectivity of rough soil surface (-),  index 1 = h-pol., 2 = v-pol.
!   tsd : thermal sampling depth (cm), Index 1 = hor. pol., 2 = ver. pol.
!
! Snow variables :
!   XMV : snow moisture [cm3/cm3]
!
! Soil Texture Parameters:
!   p : porosity of dry soil (cm3/cm3)
!   rho_b : soil bulk density (g/cm3) ~1.3,  f(soiltexture) 
!   rho_s : soil specific density (g/cm3), ATBD 2.664
!   sand  : sand fraction (% of dry weight of soil)
!   clay  : clay fraction (% of dry weight of soil)
!   wp : wilting point (cm3/cm3)
!   alpha : fitting parameter (Wang and Schmugge, 1980)
!
!   sal_soil : soil water salinity (psu = ppt(weight) ) , ATBD 0, (~0.65)
!   sal_sea : sea water salinity (psu = ppt(weight) ) , lmeb 32.5, range 6-60psu
!   sal_wat : water salinity (psu = ppt(weight) )   
!   Q : Cross polarization parameter [-]
!   Nrv : exponent in surface roughness expression
!   Nrh
!
! parameters for effective temperature :
!   w0 : teff wign parameter (f(texture)) avignon (0.3) smosrex (0.33) ATBD (0.3)
!   bw : teff wign parameter (f(texture)) avignon (0.3) smosrex (0.63) ATBD (0.3)
!   eps0 : teff holm parameter (f(texture))  smosrex (0.08) 
!   bh : teff holm parameter (f(texture)) smosrex (0.87) 
!            
! Dielectric Properties :
!   eps : Dielectric constant of the soil layer
!   eps_soil : Dielectric constant for each soil layer
!   eps_winf : dielectric constant at infinite frequency (Stogryn 1971) 
!       also called: high frequency dielectric constant (4.9 Lane and Saxton )
!   eps_0 : permittivity of free space (Klein and Swift 1977) [Farads/meter] 
!   ea : dielectric constant of air
!   er : dielectric constant of rock
!  ef :  dielectric constant of frozen soil (Hallikainen et al. 1984, 1985)
!
! Control variables :
!   layer : current soil layer of computation

!3.- Variables containing the variables for the vegetation model for a single cell
!
!   t_veg   : vegetation temperature (K)
!   wc_veg  : vegetation water content (kg/m2)
!   sal_vw  : salinity of vegetation water (psu = ppt (weight))
!   bj       : b-parameter Jackson (tauN=bj*wc_veg)
!   w_eff   : single scattering albedo of vegetation (index 1.h-pol, 2.v-pol)
!             ATBD (0,0) ,  C-band (0.06)
!   w_effL   : single scattering albedo of LOW vegetation 
!   w_effH   : single scattering albedo of HIGH vegetation 
!   a_geo   : vegetation structure coefficient (index 1.h-pol, 2.v-pol)
!   a_geoL   : LOW vegetation structure coefficient (index 1.h-pol, 2.v-pol)
!   a_geoH   : HIGH vegetation structure coefficient (index 1.h-pol, 2.v-pol)
!   tau_veg : effective vegetation opacity (index 1.h-pol, 2.v-pol)
!   tb_veg  : microwave emission from vegetation (K) (index 1.h-pol, 2.v-pol)
! tth, ttv  : empirical parameter to account for incidence angle in tau at horiz. and vertical pol.
!   eps_vw  : dielectric constant of vegetation water

!4. Variables needed for the atmosphere and vegetation for a single cell
!
! Atmosphere variables :
!   tb_toa :: top-of-atmosphere brightness temperature (K)
!   tb_tov :: top-of-atmosphere brightness temperature (K)
!   tb_au :: upward atmospheric radiation
!   tb_ad :: downward atmospheric radiation
!   tau_atm :: optical thickness of atmosphere (zenith opacity / costheta)
! tair : 2m air temperature (K)
! spres : surface pressure (Pa)
! Z :  Average altitude above sea level for the pixel (km)
!   twv          scaling factor for water vapor absorption
!                  (1.0 = normal absorption)
!   to2          scaling factor for oxygen absorption
!                  (1.0 = normal absorption)
!  r_atm : density of air kg/m3
!  z_atm : observation layer height (m)
!  t_atm : observation layer temperature (K)
!  tc_atm : observation layer temperature (C)
!  p_atm : observation layer pressure (Pa)
!  rh_atm : observation layer relative humidity (%)
!  ah : observation layer absolute humidity (kg/m3)

! t_sky : cosmic background radiation (K)
! rw : gas constant for water vapor (J/(kg*K))
!---------------------------------------------------------------------------
 
!---------------------------------------------------------------------------------
      
use parkind1, only: jpim, jprb
use yomcmempar

implicit none

save

type cmem_soils
  real(kind = jprb) :: tb_soil(2) 
  real(kind = jprb), pointer :: tsoil(:) 
  real(kind = jprb) :: tsoildeep
  real(kind = jprb) :: tc 
  real(kind = jprb), pointer :: zsoil(:)
  real(kind = jprb) :: z1 
  real(kind = jprb) :: z2 
  real(kind = jprb) :: z3 
  real(kind = jprb) :: wc1 
  real(kind = jprb), pointer  :: wcsoil(:) 
  real(kind = jprb) :: wc 
  real(kind = jprb) :: t_eff(2)  
  real(kind = jprb) :: surf_emis(2)
  real(kind = jprb) :: r_r(2)
  real(kind = jprb) :: r_s(2)
  real(kind = jprb) :: tsd(2)
  real(kind = jprb) :: XMV
  real(kind = jprb) :: p
  real(kind = jprb) :: rho_b
  real(kind = jprb) :: rho_s
  real(kind = jprb) :: sand
  real(kind = jprb) :: clay
  real(kind = jprb) :: wp
  real(kind = jprb) :: alpha
  real(kind = jprb) :: sal_soil
  real(kind = jprb) :: sal_sea
  real(kind = jprb) :: sal_wat
  real(kind = jprb) :: Nrv
  real(kind = jprb) :: Nrh
  real(kind = jprb) :: hrmodel
  real(kind = jprb) :: w0
  real(kind = jprb) :: bw 
  real(kind = jprb) :: eps0
  real(kind = jprb) :: bh
  real(kind = jprb) :: eps_winf
  real(kind = jprb) :: eps_0
  complex(kind = jprb) :: eps
  complex(kind = jprb), pointer :: eps_soil(:)
  complex(kind = jprb) :: ea
  complex(kind = jprb) :: er
  complex(kind = jprb) :: ef
  integer(kind = jpim) :: JLAYER
end type cmem_soils


type cmem_veget
  real(kind = jprb) :: t_veg
  real(kind = jprb) :: wc_veg
  real(kind = jprb) :: sal_vw
  real(kind = jprb) :: bj
  real(kind = jprb) :: w_eff(2)
  real(kind = jprb) :: w_effL(2)
  real(kind = jprb) :: w_effH(2)
  real(kind = jprb) :: a_geo(2)
  real(kind = jprb) :: a_geoL(2)
  real(kind = jprb) :: a_geoH(2)
  real(kind = jprb) :: tau_veg(2)
  real(kind = jprb) :: tauN
  real(kind = jprb) :: ttv
  real(kind = jprb) :: tth
  real(kind = jprb) :: tb_veg(2)
  complex(kind = jprb) :: eps_vw
end type cmem_veget

type cmem_atmos
  real(kind = jprb) :: tb_toa(2)
  real(kind = jprb) :: tb_tov(2)
  real(kind = jprb) :: tb_au
  real(kind = jprb) :: tb_ad
  real(kind = jprb) :: tau_atm
  real(kind = jprb) :: tair
  real(kind = jprb) :: spres
  real(kind = jprb) :: Z
  real(kind = jprb) :: twv
  real(kind = jprb) :: to2
  real(kind = jprb), pointer  :: fZ (:) 
  real(kind = jprb), pointer  :: fs_tatm (:,:) 
  real(kind = jprb), pointer  :: fs_spres (:)
  real(kind = jprb), pointer  :: fs_R (:,:) 
  real(kind = jprb), pointer  :: r_atm (:)
  real(kind = jprb), pointer  :: z_atm (:)
  real(kind = jprb), pointer  :: p_atm (:)
  real(kind = jprb), pointer  :: t_atm (:)
  real(kind = jprb) :: tc_atm
  real(kind = jprb), pointer  :: rh_atm (:)
  real(kind = jprb), pointer  :: ah (:)
  real(kind = jprb) :: t_sky
  real(kind = jprb) :: rw
end type cmem_atmos



type cmem_fields

  real(kind = jprb),pointer :: ftair(:) 
  real(kind = jprb),pointer :: ftveg(:) 
  real(kind = jprb),pointer :: ftskin(:) 
  real(kind = jprb),pointer :: ftsurf(:) 
  real(kind = jprb),pointer :: ftl2(:) 
  real(kind = jprb),pointer :: ftdeep(:) 
  real(kind = jprb),pointer :: fwc(:)    
  real(kind = jprb),pointer :: fwc2(:)  
  real(kind = jprb),pointer :: fwc3(:)  
  real(kind = jprb),pointer :: ftb_soil(:,:)      
  real(kind = jprb),pointer :: ftb_toa(:,:)       
  real(kind = jprb),pointer :: fsurf_emis(:,:)    
  real(kind = jprb),pointer :: fsand(:)    
  real(kind = jprb),pointer :: fclay(:)    
  real(kind = jprb),pointer :: frho_b(:)   
  real(kind = jprb),pointer :: fp(:)   
  real(kind = jprb),pointer :: fWP(:)  
  real(kind = jprb),pointer :: falpha(:)  
  real(kind = jprb),pointer :: fteffC(:,:)  
  real(kind = jprb),pointer :: ftau_atm(:)  
  real(kind = jprb),pointer :: ftb_au(:)  
  real(kind = jprb),pointer :: ftb_ad(:)  
  real(kind = jprb),pointer :: fsnowd(:) 
  real(kind = jprb),pointer :: frsnow(:) 
  real(kind = jprb),pointer :: fLSM(:)   
  integer(kind=jpim),pointer :: fTVL(:)    
  integer(kind=jpim),pointer :: fTVH(:)    
  integer(kind=jpim),pointer :: mask(:)    
  real(kind = jprb),pointer :: ftfrac(:,:)  
  real(kind = jprb),pointer :: fwater(:)    
  real(kind = jprb),pointer :: fs_laiL(:)  
  !real(kind = jprb),pointer :: fs_laiH(:)  
  real(kind = jprb),pointer :: fNrh_L(:)
  real(kind = jprb),pointer :: fNrh_H(:)
  real(kind = jprb),pointer :: fNrv_L(:)
  real(kind = jprb),pointer :: fNrv_H(:)
  real(kind = jprb),pointer :: ftau_veg(:,:)  
  real(kind = jprb),pointer :: ftth(:,:)
  real(kind = jprb),pointer :: fttv(:,:)
  real(kind = jprb),pointer :: fw_effL(:,:)
  real(kind = jprb),pointer :: fw_effH(:,:)
  real(kind = jprb),pointer :: fhrmodel(:,:)
  real(kind = jprb),pointer :: fwc_veg(:,:) 
  real(kind = jprb),pointer :: fh(:) 
  real(kind = jprb),pointer :: fb(:,:) 
  real(kind = jprb),pointer :: ftauN(:,:) 
  real(kind = jprb),pointer :: fsal_wat(:) 
  integer(kind=jpim) :: N
  integer(kind=jpim) :: JJ
  integer(kind=jpim) :: JCMEMTILE
  character(len=80)  :: clname
     type(cmem_soils):: soils
     type(cmem_veget):: veget
     type(cmem_atmos):: atmos
end type cmem_fields


end module yomcmemtypes
