<?xml version="1.0" ?>
<!--
 This file is part of FlightGear, the free flight simulator
 http://www.flightgear.org/

 Copyright (C) 2012 Torsten Dreyer, Torsten (at) t3r _dot_ de

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License as
 published by the Free Software Foundation; either version 2 of the
 License, or (at your option) any later version.

 This program 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 GNU
 General Public License for more details.
-->
<!--
  Compute Atmospheric Light Shading related properties
-->
<PropertyList>

  <!-- (pi/2 - /sim/time/sun-angle-rad) / 0.017451 * 110000.0 -->
  <filter>
    <name>LocalWeather:terminator-relative-position</name>
    <input>
      <expression>
        <difference>
          <value>1.57079632675</value>
          <property>/sim/time/sun-angle-rad</property>
        </difference>
      </expression>
    </input>
    <output>/environment/terminator-relative-position-m</output>
    <type>gain</type>
    <gain>6303363.70409</gain> 
  </filter>

  <filter>
    <name>LocalWeather:eye-altitude</name>
    <input>
     <property>/position/altitude-ft</property>
    </input>
    <output>/sim/rendering/eye-altitude-m</output>
    <type>gain</type>
    <gain>0.30480</gain> 
  </filter>
 
  <!-- Properties used by the atmospheric scattering shader, when not being set directly 
       by Advanced Weather -->
       
  <filter>
    <!--  Ground visibility.  Same as lowest boundary layer.  -->
    <name>EnvironmentInterpolator:AtmosphericShader:ground-visibility-m</name>
    <type>gain</type>
    <enable>
      <condition>
        <not>
          <property>/nasal/local_weather/enabled</property>
          <value>true</value>
        </not>
      </condition>
    </enable>
    <input>/environment/config/boundary/entry[0]/visibility-m</input>
    <output>/environment/ground-visibility-m</output>
  </filter>

  <filter>
    <!--  Ground haze level - assumed to be up to the lowest cloud layer.  -->
    <name>EnvironmentInterpolator:AtmosphericShader:GroundHazeThickness</name>
    <type>gain</type>
    <enable>
      <condition>
        <not>
          <property>/nasal/local_weather/enabled</property>
          <value>true</value>
        </not>
      </condition>
    </enable>
    <input>
      <expression>
        <sum>
          <property>/environment/clouds/layer[0]/elevation-ft</property>
          <property>/environment/clouds/layer[0]/thickness-ft</property>
        </sum>
      </expression>
    </input>
    <gain>
      <!-- feet to meters -->
      <value>0.3048</value>
    </gain>
    <u_min>
      <value>0</value>
    </u_min>
    <u_max>
      <!-- haze maxes out at 6000ft.  Could probably do something using lapse rates instead -->
      <value>2000</value>
    </u_max> 
    <output>/environment/ground-haze-thickness-m</output>
  </filter>
  
  <!--  Build up the ground level scattering from 1 for clear skies to 0.6 for overcast -->
  <filter>
    <name>EnvironmentInterpolator:AtmosphericShader:GroundScattering:0:Clear</name>
    <type>gain</type>
    <enable>
      <condition>
        <and>
          <not>
            <property>/nasal/local_weather/enabled</property>
            <value>true</value>
          </not>
          <equals>
            <property>/environment/clouds/layer[0]/coverage</property>
            <value>clear</value>
          </equals>
        </and>
      </condition>
    </enable>
    <input>
      <value>1</value>
    </input>
    <output>/environment/surface/ground-scattering</output>
  </filter>
  
  <filter>
    <name>EnvironmentInterpolator:AtmosphericShader:GroundScattering:0:Few</name>
    <type>gain</type>
    <enable>
      <condition>
        <and>
          <not>
            <property>/nasal/local_weather/enabled</property>
            <value>true</value>
          </not>
          <equals>
            <property>/environment/clouds/layer[0]/coverage</property>
            <value>few</value>
          </equals>
        </and>
      </condition>
    </enable>
    <input>
      <value>0.9</value>
    </input>
    <output>/environment/surface/ground-scattering</output>
  </filter>
  
  <filter>
    <name>EnvironmentInterpolator:AtmosphericShader:GroundScattering:0:Scattered</name>
    <type>gain</type>
    <enable>
      <condition>
        <and>
          <not>
            <property>/nasal/local_weather/enabled</property>
            <value>true</value>
          </not>
          <equals>
            <property>/environment/clouds/layer[0]/coverage</property>
            <value>scattered</value>
          </equals>
        </and>
      </condition>
    </enable>
    <input>
      <value>0.8</value>
    </input>
    <output>/environment/surface/ground-scattering</output>
  </filter>
  
  <filter>
    <name>EnvironmentInterpolator:AtmosphericShader:GroundScattering:0:Broken</name>
    <type>gain</type>
    <enable>
      <condition>
        <and>
          <not>
            <property>/nasal/local_weather/enabled</property>
            <value>true</value>
          </not>
          <equals>
            <property>/environment/clouds/layer[0]/coverage</property>
            <value>broken</value>
          </equals>
        </and>
      </condition>
    </enable>
    <input>
      <value>0.7</value>
    </input>
    <output>/environment/surface/ground-scattering</output>
  </filter>
  
  <filter>
    <name>EnvironmentInterpolator:AtmosphericShader:GroundScattering:0:Overcast</name>
    <type>gain</type>
    <enable>
      <condition>
        <and>
          <not>
            <property>/nasal/local_weather/enabled</property>
            <value>true</value>
          </not>
          <equals>
            <property>/environment/clouds/layer[0]/coverage</property>
            <value>overcast</value>
          </equals>
        </and>
      </condition>
    </enable>
    <input>
      <value>0.6</value>
    </input>
    <output>/environment/surface/ground-scattering</output>
  </filter>
  
  <!-- TODO: Handle layers beyond the first -->
  
  <!-- Set the scattering at the aircraft level -->

  <filter>
    <name>EnvironmentInterpolator:AtmosphericShader:Scattering:AircraftAboveLayer</name>
    <type>gain</type>
    <enable>
      <condition>
        <and>
          <not>
            <property>/nasal/local_weather/enabled</property>
            <value>true</value>
          </not>
          <greater-than>
            <property>/position/altitude-ft</property>
            <expression>
              <sum>
                <property>/environment/clouds/layer[0]/elevation-ft</property>
                <property>/environment/clouds/layer[0]/thickness-ft</property>
              </sum>
            </expression>
          </greater-than>
        </and>
      </condition>
    </enable>
    <input>
      <value>1</value>
    </input>
    <output>/rendering/scene/scattering</output>
  </filter>

  <filter>
    <name>EnvironmentInterpolator:AtmosphericShader:Scattering:AircraftBelowLayer</name>
    <type>gain</type>
    <enable>
      <condition>
        <and>
          <not>
            <property>/nasal/local_weather/enabled</property>
            <value>true</value>
          </not>
          <less-than-equals>
            <property>/position/altitude-ft</property>
            <expression>
              <sum>
                <property>/environment/clouds/layer[0]/elevation-ft</property>
                <property>/environment/clouds/layer[0]/thickness-ft</property>
              </sum>
            </expression>
          </less-than-equals>
        </and>
      </condition>
    </enable>
    <input>/environment/surface/ground-scattering</input>
    <output>/rendering/scene/scattering</output>
  </filter>
  
  <!-- Clouds self-shading -->
  <filter>
    <name>EnvironmentInterpolator:AtmosphericShader:CloudSelfShading</name>
    <type>gain</type>
    <enable>
      <condition>
        <not>
          <property>/nasal/local_weather/enabled</property>
          <value>true</value>
        </not>
      </condition>
    </enable>
    <input>
      <!-- TODO : Make this sun-angle dependent -->
      <value>1.0</value>    
    </input>
    <output>/environment/cloud-self-shading</output>
  </filter>
  
  <!-- Skydome parameters - /sim/rendering/[mie|rayleigh|dome-density] -->
  <filter>
    <name>EnvironmentInterpolator:AtmosphericShader:Skydome:VisFactor</name>
    <type>gain</type>
    <enable>
      <condition>
        <not>
          <property>/nasal/local_weather/enabled</property>
          <value>true</value>
        </not>
      </condition>
    </enable>
    <input>
      <expression>
        <product>    
          <difference> 
            <property>/environment/visibility-m</property>
            <value>30000</value>
          </difference>
          <value>0.000011111</value>
        </product>
      </expression>
    </input>
    <u_min>0.0</u_min>
    <u_max>1.0</u_max>
    <output>/sim/rendering/skydome-vis-factor</output>
  </filter>
  
  <filter>
    <name>EnvironmentInterpolator:AtmosphericShader:Skydome:Rayleigh:Low</name>
    <type>gain</type>
    <enable>
      <condition>
        <and>
          <not>
            <property>/nasal/local_weather/enabled</property>
            <value>true</value>
          </not>
          <less-than>
            <property>/position/altitude-ft</property>
            <value>36000</value>
          </less-than>          
        </and>
      </condition>
    </enable>
    <input>
      <expression>
         <difference>
           <value>0.0003</value>
           <product>
             <property>/sim/rendering/skydome-vis-factor</property>
             <value>0.0001</value>
           </product>
         </difference>
      </expression>
    </input>
    <output>/sim/rendering/rayleigh-no-polution</output>
  </filter>
  
  <filter>
    <name>EnvironmentInterpolator:AtmosphericShader:Skydome:Rayleigh:Medium</name>
    <type>gain</type>
    <enable>
      <condition>
        <and>
          <not>
            <property>/nasal/local_weather/enabled</property>
            <value>true</value>
          </not>
          <greater-than>
            <property>/position/altitude-ft</property>
            <value>36000</value>
          </greater-than>          
          <less-than>
            <property>/position/altitude-ft</property>
            <value>85000</value>
          </less-than>          
        </and>
      </condition>
    </enable>
    <input>
      <expression>
        <difference>
          <difference>
            <value>0.0003</value>
            <product>
              <property>/sim/rendering/skydome-vis-factor</property>
              <value>0.0001</value>
            </product>
          </difference>
          <product>
            <difference>
              <property>/position/altitude-ft</property>
              <value>36000</value>
            </difference>
            <value>0.000000002</value>
          </product>
        </difference>
      </expression>
    </input>
    <output>/sim/rendering/rayleigh-no-polution</output>
  </filter>

  <filter>
    <name>EnvironmentInterpolator:AtmosphericShader:Skydome:Rayleigh:High</name>
    <type>gain</type>
    <enable>
      <condition>
        <and>
          <not>
            <property>/nasal/local_weather/enabled</property>
            <value>true</value>
          </not>
          <greater-than>
            <property>/position/altitude-ft</property>
            <value>85000</value>
          </greater-than>          
        </and>
      </condition>
    </enable>
    <input>
      <expression>
         <difference>
           <value>0.0002</value>
           <product>
             <property>/sim/rendering/skydome-vis-factor</property>
             <value>0.0001</value>
           </product>
         </difference>
      </expression>
    </input>
    <output>/sim/rendering/rayleigh-no-polution</output>
  </filter>
  
  <filter>
    <name>EnvironmentInterpolator:AtmosphericShader:Skydome:Rayleight:Polution:Low</name>
    <type>gain</type>
    <enable>
      <condition>
        <and>
          <not>
            <property>/nasal/local_weather/enabled</property>
            <value>true</value>
          </not>
          <less-than-equals>
            <property>/position/altitude-ft</property>
            <expression>
              <sum>
                <property>/environment/clouds/layer[0]/elevation-ft</property>
                <property>/environment/clouds/layer[0]/thickness-ft</property>
              </sum>
            </expression>
          </less-than-equals>
        </and>
      </condition>
    </enable>
    <input>
      <expression>
        <sum>
          <property>/sim/rendering/rayleigh-no-polution</property>
          <product>
            <property>/environment/air-pollution-norm</property>
            <value>0.0003</value>
          </product>
          <product>
            <property>/environment/air-pollution-norm</property>
            <value>0.0004</value>
            <difference>
              <value>1.0</value>
              <product>
                <div>
                  <property>/position/altitude-ft</property>
                  <sum>
                    <property>/environment/clouds/layer[0]/elevation-ft</property>
                    <property>/environment/clouds/layer[0]/thickness-ft</property>
                    <value>1.0</value><!-- to avoid divide by 0 exceptions -->
                  </sum>
                </div>
                <div>
                  <property>/position/altitude-ft</property>
                  <sum>
                    <property>/environment/clouds/layer[0]/elevation-ft</property>
                    <property>/environment/clouds/layer[0]/thickness-ft</property>
                    <value>1.0</value><!-- to avoid divide by 0 exceptions -->
                  </sum>
                </div>
              </product>
            </difference>
          </product>
        </sum>
      </expression>
    </input>
    <output>/sim/rendering/rayleigh</output>
  </filter>

  <filter>
    <name>EnvironmentInterpolator:AtmosphericShader:Skydome:Rayleight:Polution:High</name>
    <type>gain</type>
    <enable>
      <condition>
        <and>
          <not>
            <property>/nasal/local_weather/enabled</property>
            <value>true</value>
          </not>
          <greater-than>
            <property>/position/altitude-ft</property>
            <expression>
              <sum>
                <property>/environment/clouds/layer[0]/elevation-ft</property>
                <property>/environment/clouds/layer[0]/thickness-ft</property>
              </sum>
            </expression>
          </greater-than>
        </and>
      </condition>
    </enable>
    <input>
      <expression>
        <sum>
          <property>/sim/rendering/rayleigh-no-polution</property>
          <product>
            <property>/environment/air-pollution-norm</property>
            <value>0.0003</value>
          </product>
        </sum>
      </expression>
    </input>
    <output>/sim/rendering/rayleigh</output>
  </filter>
  
  <filter>
    <name>EnvironmentInterpolator:AtmosphericShader:Skydome:Mie:Low</name>
    <type>gain</type>
    <enable>
      <condition>
        <and>
          <not>
            <property>/nasal/local_weather/enabled</property>
            <value>true</value>
          </not>
          <less-than>
            <property>/position/altitude-ft</property>
            <value>36000</value>
          </less-than>          
        </and>
      </condition>
    </enable>
    <input>
      <expression>
         <difference>
           <value>0.005</value>
           <product>
             <property>/sim/rendering/skydome-vis-factor</property>
             <value>0.002</value>
          </product>
        </difference>
      </expression>
    </input>
    <output>/sim/rendering/mie</output>
  </filter>
  
  <filter>
    <name>EnvironmentInterpolator:AtmosphericShader:Skydome:Mie:Medium</name>
    <type>gain</type>
    <enable>
      <condition>
        <and>
          <not>
            <property>/nasal/local_weather/enabled</property>
            <value>true</value>
          </not>
          <greater-than>
            <property>/position/altitude-ft</property>
            <value>36000</value>
          </greater-than>          
          <less-than>
            <property>/position/altitude-ft</property>
            <value>85000</value>
          </less-than>          
        </and>
      </condition>
    </enable>
    <input>
      <expression>
        <difference>
           <difference>
             <value>0.005</value>
             <product>
               <property>/sim/rendering/skydome-vis-factor</property>
               <value>0.002</value>
            </product>
          </difference>
          <product>
            <difference>
              <property>/position/altitude-ft</property>
              <value>36000</value>
            </difference>
            <value>0.00000004</value>
          </product>
        </difference>
      </expression>
    </input>
    <output>/sim/rendering/mie</output>
  </filter>         

  <filter>
    <name>EnvironmentInterpolator:AtmosphericShader:Skydome:Mie:High</name>
    <type>gain</type>
    <enable>
      <condition>
        <and>
          <not>
            <property>/nasal/local_weather/enabled</property>
            <value>true</value>
          </not>
          <greater-than>
            <property>/position/altitude-ft</property>
            <value>85000</value>
          </greater-than>          
        </and>
      </condition>
    </enable>
    <input>
      <expression>
         <difference>
           <value>0.003</value>
           <product>
             <property>/sim/rendering/skydome-vis-factor</property>
             <value>0.002</value>
          </product>
        </difference>
      </expression>
    </input>
    <output>/sim/rendering/mie</output>
  </filter>
  
  <!-- Density -->

  <filter>
    <name>EnvironmentInterpolator:AtmosphericShader:Skydome:Density:Low</name>
    <type>gain</type>
    <enable>
      <condition>
        <and>
          <not>
            <property>/nasal/local_weather/enabled</property>
            <value>true</value>
          </not>
          <less-than-equals>
            <property>/position/altitude-ft</property>
            <expression>
              <sum>
                <property>/environment/clouds/layer[0]/elevation-ft</property>
                <property>/environment/clouds/layer[0]/thickness-ft</property>
              </sum>
            </expression>
          </less-than-equals>
        </and>
      </condition>
    </enable>
    <input>
      <expression>
        <sum>
          <value>0.3</value>
          <product>
            <property>/environment/air-pollution-norm</property>
            <value>0.05</value>
          </product>
          <product>
            <property>/environment/air-pollution-norm</property>
            <value>0.05</value>
            <difference>
              <value>1.0</value>
              <product>
                <div>
                  <property>/position/altitude-ft</property>
                  <sum>
                    <property>/environment/clouds/layer[0]/elevation-ft</property>
                    <property>/environment/clouds/layer[0]/thickness-ft</property>
                    <value>1.0</value><!-- to avoid divide by 0 exceptions -->
                  </sum>
                </div>
                <div>
                  <property>/position/altitude-ft</property>
                  <sum>
                    <property>/environment/clouds/layer[0]/elevation-ft</property>
                    <property>/environment/clouds/layer[0]/thickness-ft</property>
                    <value>1.0</value><!-- to avoid divide by 0 exceptions -->
                  </sum>
                </div>
              </product>
            </difference>
          </product>
        </sum>
      </expression>
    </input>
    <output>/sim/rendering/dome-density</output>
  </filter>

  <filter>
    <name>EnvironmentInterpolator:AtmosphericShader:Skydome:Density:High</name>
    <type>gain</type>
    <enable>
      <condition>
        <and>
          <not>
            <property>/nasal/local_weather/enabled</property>
            <value>true</value>
          </not>
          <greater-than>
            <property>/position/altitude-ft</property>
            <expression>
              <sum>
                <property>/environment/clouds/layer[0]/elevation-ft</property>
                <property>/environment/clouds/layer[0]/thickness-ft</property>
              </sum>
            </expression>
          </greater-than>
        </and>
      </condition>
    </enable>
    <input>
      <expression>
        <sum>
          <value>0.3</value>
          <product>
            <property>/environment/air-pollution-norm</property>
            <value>0.05</value>
          </product>
        </sum>
      </expression>
    </input>
    <output>/sim/rendering/dome-density</output>
  </filter>
  
  <!-- Mean elevation. No easy way to do this, so we'll just use the altitude of the nearest METAR station -->
  <filter>
    <name>EnvironmentInterpolator:AtmosphericShader:Skydome:MeanElevation</name>
    <type>gain</type>
    <enable>
      <condition>
        <not>
          <property>/nasal/local_weather/enabled</property>
          <value>true</value>
        </not>
      </condition>
    </enable>
    <input>
      <expression>
        <product>
          <property>/environment/metar[0]/station-elevation-ft</property>
          <value>0.3048</value>
        </product>
      </expression>
    </input>
    <output>/environment/mean-terrain-elevation-m</output>
  </filter>
  
  <!-- Tree "season" - used to determine whether to display the trees in summer or winter foliage -->
  <filter>
    <name>EnvironmentInterpolator:Trees:Season:Summer</name>
    <type>gain</type>
    <enable>
      <condition>
        <not>
          <or>
            <equals>
              <property>sim/startup/season</property>
              <value>winter</value>
            </equals>
            <greater-than>
              <property>/environment/season</property>
              <value>1.5</value>
            </greater-than>
          </or>
        </not>
      </condition>
    </enable>
    <input>
      <value>0.0</value>
    </input>
    <output>/environment/tree-season</output>
  </filter>


  <filter>
    <name>EnvironmentInterpolator:Trees:Season:Winter</name>
    <type>gain</type>
    <enable>
      <condition>
        <or>
          <equals>
            <property>sim/startup/season</property>
            <value>winter</value>
          </equals>
          <greater-than>
            <property>/environment/season</property>
            <value>1.5</value>
          </greater-than>
        </or>
      </condition>
    </enable>
    <input>
      <value>1.0</value>
    </input>
    <output>/environment/tree-season</output>
  </filter>
  

  <filter>
    <name>EnvironmentInterpolator:Precipitation:Illumination</name>
    <type>gain</type>
    <enable>
      <condition>
	<property>/environment/precipitation-control/detailed-precipitation</property>
      </condition>
     </enable>
     <input>
       <expression>
         <sum>
           <property>/rendering/scene/diffuse/red</property>
	   <value>0.3</value>
	   <product>
              <property>/sim/rendering/als-secondary-lights/use-searchlight</property>
	      <value>0.3</value>
           </product>
	   <product>
              <property>/sim/rendering/als-secondary-lights/use-landing-light</property>
	      <value>0.3</value>
           </product>
	   <product>
              <property>/sim/rendering/als-secondary-lights/use-alt-landing-light</property>
	      <value>0.3</value>
           </product>
         </sum>
       </expression>
     </input>
     <output>/environment/precipitation-control/illumination</output>
     <min>0.0</min>
     <max>1.0</max>
  </filter>


  <filter>
    <name>EnvironmentInterpolator:Precipitation:SurfaceWetness</name>
    <type>noise-spike</type>
    <max-rate-of-change>0.004</max-rate-of-change>
    <input>/environment/rain-norm</input>
    <output>/environment/surface/wetness</output>
    <min><property>/environment/surface/wetness-set</property></min>
    <max>1.0</max>
  </filter>

</PropertyList>