Simulation of Atmospheric Refraction

Thursday, November 22, 2018 - 21:50 | Author: wabis | Topics: Simulation, Interactive, Physics | Comments(10)
The interactive Refraction Simulator on this page renders scenes as seen on atmospheric conditions you can provide. It is an accurate simulation of Atmospheric Refraction for both Globe Earth and Flat Earth. The simulation predicts what we can expect to observe in reality.

Link: http://walter.bislins.ch/RefractionSim

Please visit Mick West's excellent Demonstration of Refraction on Metabunk and try his Refraction Simulator too.

The Refraction Simulation App

The Demos behind the black buttons are based on the excellent website about An Introduction to Mirages by Andrew T. Young. Please follow the links below the buttons shown as "more infos" to go to the specific page on his website where he explains the phenomena in detail (sometimes you have to press F5 to see the contents).

no scnene description
  • Rendering
  • Baro Observer
  • Baro Target
  • Graphs
  • Targets
  • Save/Restore

How the Simulation works

The images of the simulation are composed of pixels. Each pixel has at least one assosiated light ray comming from the scene and carrying a color value from the surface of at least one object.

How the Ray Tracer works

But the simulation works backwards. It calculates for each image pixel at least one light ray into the scene and checks where this ray hits the Targets. This principle as called a Ray Tracer.

Each light ray is divided into many Ray Segments. The start point and direction of the first Ray Segment is determined by the location of the image pixel with respect to the observer.

Then depending on the position and direction of the Ray Segment the Refraction Coefficient and from that the bending of the Ray Segment is calculated as described at How Custom Refraction is derived. From this bending the end position and direction of the Ray Segment can be calculated. This gives the start point of the next Ray Segment and so on.

Each Ray Segment is checked against all Targets of the scene. If the Ray Segment intersects a Target, the exact intersection point is calculated and which target coordinates are hit by the Ray Segment. The target coordinates are used to determine the color of the target at this coordinates. If the target is opaque, the calculation of the light ray ends here and the image pixel color is defined. If the Target is transparent, the color is stored for later and the calculation of the light ray continues until the next opaque Target or the Horizon is hit. Then all stored colors are combined and define the image pixel color of this light ray. It is also possible to assign multiple light rays to one pixel to get smoother edges in the image (antialiasing). This is accomplished with the Obersampling setting.

Because the simulation makes use of curved Ray Segments, only very few segments are needed, compared to simulations that use a straight Ray Segment approach, to achieve the same accuracy. This allows to simulate even Astronomical Refraction with very long light rays in a short time, see the Sunset Demo.

The simulation is written entirely in JavaScript and uses the HTML canvas element to display the rendered graphics. The Source Code of the simulation App alone, excluding the code of the modules for the graphic output, control panels ect., consists of about 5800 lines.

How Custom Refraction is derived

The bending of each light ray is dependent on the Refraction Curve along the path of the light ray. The simulation splits each light ray into a number of segments and calculates the bending of each segment and joins the segments together to a single light ray.

The curvature of a Light Ray Segment depends on the Refraction Coefficient at the location of the Ray Segment. The Refraction Coefficient depends on the atmospheric conditions at the location of the Ray Segment and the vertical direction of the light ray segment.

The atmospheric conditions are derived from the inputs in the Barometric Settings panel. You can specify two different barometric parameter sets for two different locations, eg. for the location of the observer via Baro Measure Dist at the Baro Observer panel and the location of the farthest Target or the Horizon at the Baro Target panel. If you don't specify barometric data for the second panel, the parameters from the first panel are used along the whole light path. If you specify separate target parameters, the values between are interpolated and for distances beyond that the target parameters are kept fixed.

You can enter Pressure, Pressure Altitude and some pairs of (altitude, temperature). The simulation contains a default barometric model as defined by the Standard Atmosphere. The entered parameters modify this model accordingly:

  1. The simulation calculates a smooth Temperature Curve through the entered parameter values and blends the curve slowly into the curve of the Standard Atmosphere where no values are specified. You are limited to enter values to an altitude of 9 km. Above that the Standard Atmosphere is used. This is not a big limitation, because Refraction is nearly zero at altitudes above anyway.
  2. A Temperature Gradient Curve is derived from the calculated Temperature curve.
  3. A Pressure Curve is derived from the user supplied Pressure and the Standard Atmosphere.
  4. A Refraction Curve is derived from the Pressure, Temperature and Temperature Gradient curves.

You can display the Temperature Curve, Temperature Gradient Curve and Refraction Curve by selecting Display = Baro.

The simulation can now calculate the Refraction Coefficient for any point in the atmosphere between observer and Horizon. The Refraction Coefficient, together with the vertical angle of the Ray Segment at a certain position and altitude, determine the curvature of the Ray Segment.

To calculate the Refraction Coefficient from the barometric data and the vertical angle of the Ray Segment the following equation is used:

k = 503 \cdot { P \over T^2 } \cdot ( 0.0343 + \mathrm{d} T / \mathrm{d} h ) \cdot \cos( \theta )
k ' =' 'Refraction Coefficient of a light ray segment at a certain location between observer and target, depending on the altitude and the distance from the observer
P ' =' 'atmospheric pressure at that location in mBar
T ' =' 'temperature at that location in Kelvin
\mathrm{d} T / \mathrm{d} h ' =' 'temperature gradient at that location in °C/m
\theta ' =' 'vertical angle of the Ray Segment at that location measured from the horizontal

Note: The constants in the equation above are slightly different in different parts of the world due to different mean atmospheric conditions. Their visual differences are minute but must be taken into account on precise survey measurements over long distances.

The curvature of the Ray Segment is then calculated by the following equation:

c_\mathrm{R} = { 1 \over R_\mathrm{R} } = { k \over R_\mathrm{E} }
c_\mathrm{R} ' =' 'curvature of the light Ray Segment
R_\mathrm{R} ' =' 'radius of the light ray segment
R_\mathrm{E} ' =' 'radius of the earth
k ' =' 'Refraction Coefficient as calculated with (1)

Note: The same equations are used for Globe and Flat Earth, because the physics of Refraction is not dependent on the shape of the earth. The curvature or flatness of the air gradient is taken into account by the simulator. The equations do NOT assume a globe earth! The radius of the earth RE in the equation is due to the way the Refraction Coefficient k is defined. k could be defined as the curvature of the light ray without scaling it with the readius of the earth. You can modify (1) accordingly so that you can calculate the curvature of the light ray directly without any reference to the radius of the earth:

c_\mathrm{R} = { 1 \over R_\mathrm{R} } = 7{.}895 \times 10^{-5} \cdot { P \over T^2 } \cdot ( 0.0343 + \mathrm{d} T / \mathrm{d} h ) \cdot \cos( \theta )
c_\mathrm{R} ' =' 'curvature of the light Ray Segment
R_\mathrm{R} ' =' 'radius of the light ray segment
P ' =' 'atmospheric pressure in mBar
T ' =' 'temperature in Kelvin
\mathrm{d} T / \mathrm{d} h ' =' 'temperature gradient in °C/m
\theta ' =' 'vertical angle of the Ray Segment measured from the horizontal

URL Parameters

The App can be called with some URL parameters to set a specific stored App State:

On the Save/Restore Panel click Get App URL to get an URL from the current state of the App. Copy this URL into any Web-Form (e.g. YouTube comment). Clicking the link will call this page with the saved state.

  • To start a certain Demo with the URL, append
    &demo=DemoName to the page URL, where DemoName is one of the following names:
Intro, Std2km, Std5km, Std10km, Looming, Towering, Stooping, InteriorMirage, SuperiorMirage, ChicagoStrongLooming, ChicagoSuperiorMirage, Sunset, FECurved, GodRays
  • To open a specific Panel, append
    &tab=TabName, where TabNam is one of the following names:
Rendering, BaroObserver, BaroTarget, Graphs, Targets, SaveRestore

The parameters may be combined in one URL, eg. &demo=Sunset&tab=Targets

Specialist Terms


A Scene is composed of a list of Targets. A Target can be an Image or a Pattern.

Each Target can be positioned and rotated individually.

A Target can be sized as needed. This is important if you use images that show a certain scene like a skyline.

A Target can have Boundaries/Limits or it can be infinetely extended in any direction. If a Target is an image without boundary limits, the image is repeated.

Targets can be made partially transparent. Simulated Light Rays are sent through the scene until they hit an opaque Target or exceed the Horizon limit.

To create a new Target, open the Targets panel and click on New. A copy of an internal Target Template is created. You can also create a Copy of the current selected Target by clicking Clone and then change some parameters. Or you can create one of the Preset Targets.

Note: Targets are treated differently in the Model Flat Earth and Globe Earth. On the Flat Earth model the targets are always flat planes. On the Globe Model the same targets are wraped around the globe, so targets that are not entirely vertical are curved. This allows to create targets like water images that are flat on the Flat Earth display but curved on the Globe Earth display.

Note: For some scenes the order of the Targets may be important. You can move the Targets in the sequence with the buttons Move Up and Move Down.


Because each simulated Light Ray is dividet into many Ray Segments, a limit must be defined so that the calculation is stopped if no opaque Target is hit. This limit is called the Horizon.

A color gradient can be specified for the Horizon, so that a light ray hitting the horizon has a defined color assigned to it. The specified Horizon Distance limits how far a light ray has to be calculated.

Ray Segment

In the simulation each light ray is dividet into many Ray Segments. For each Ray Segment the Ray Curvature is calculated separately from the Refraction Coefficient at the position of the Ray Segment, see How Custom Refraction is derived.

Ray Curvature

The Ray Curvature is the bending of a simulated Ray Segment. The Ray Curvature is derived from the Refraction Coefficient and the vertical angle of the Ray Segment at its position.

Refraction Coefficient

The Refraction Coefficient is a measure of the Ray Curvature of a light ray with respect to the radius of the earth. The Refraction Coefficient is defined as:

k = { R_\mathrm{E} \over R_\mathrm{R} }
k ' =' 'Refraction Coefficient
R_\mathrm{E} ' =' '6371 km = radius of the earth
R_\mathrm{R} ' =' 'radius of a light ray segment

A value of k = 0 means the Ray Segment is straight. A value of k = 1 means the Ray Segment has the same curvature radius as the earth. Ray segments with negative Coefficients are bent upwards.

The Refraction Coefficient can be calculated from atmospheric properties such as Pressure, Temperature and Temperature Gradient at any position of a light ray segment. It also depends on the vertical angle of the ray segment, see equation (1).

The Refraction Coefficient in reality is slightly different at each location of a light ray from the observer to the Target. In practical applications like survey a mean Refraction Coefficient may be used as an approximation. The simulation calculates the individual Refraction Coefficient for each Ray Segment along the whole way from the observer to a target.

Refraction Curve

The Refraction Curve describes the Refraction Coefficient as a function of altitude, so each altitude is a certain Refraction Coefficient assigned. The Refraction Curve is derived from the Barometric Settings, see How Custom Refraction is derived.

The simulation calculates two Refraction Curves, one for the observer location and one for a Target location the user can specify. For all locations between observer and target the Refraction Coefficient is derived by interpolating the Refraction Coefficient from this two curves according to the altitude and location of a Ray Segment. If no Barometric Settings are defined for a target location, the observer Refraction Curve is used everywhere.

Pressure Curve

The Pressure changes with altitude. Such a change is described by a Pressure Curve, which gives the Pressure as a function of altitude.

The simulation uses the Standard Atmosphere as the basis for the Pressure Curve but modifies it according to the user specified Pressure entered in the Barometric Settings.

Temperature Curve

The temperature changes with altitude. Such a change is described by a Temperature Curve, which gives the Temperature as a function of altitude.

The simulation uses some discrete user specified temperature values to calculate a smooth Temperature Curve through these points. The calculated curve blends into the curve as defined by the Standard Atmosphere above the highest specified Temperature point.

Temperature Gradient

The Temperature Gradient describes the change in Temperature at each altitude. Mathematically it is the Derivative of the Temperature Curve with respect to altitude.

The simulation calculates a Temperature Gradient Curve from the Temperature Curve.

Temperature Gradient Curve

The Temperature Gradient Curve is a mathematically curve that connects all Temperature Gradient values for a specific altitude and is derived from the Temperature Curve. The Temperature Gradient Curve is used in the calculation of the Refraction Curve.

Standard Atmosphere

The Standard Atmosphere defines curves for Temperature, Pressure, air Density and Temperature Gradient for each altitude up to about 85 km. These curves are derived from mean values and used as a reference in many applications, eg. in aviation.

The simulation contains the Standard Atmosphere model and uses it for regions the user does not provide atmospheric data data.


1Nicolas 11/28/2018 | 09:17

Great work!

Nevertheless, dispite maybe it would be antiscientific, but it would be very curious and even funny to see the picture with flat earth and negative refraction with its radius equal to the radius of spherical earth

2wabiswalter@bislins.ch (Walter Bislin, Author of this Page) 11/28/2018 | 20:03

Nocolas, already implemented!

Press the button FE Curved above the displays. To see the correspondig Rays bending upwards select Display = Rays below the right display.

To get the same observations on the Flat Earth due to refraction there must be an impossible Temperature Gradient of −15°C/100m every day!

3Nicolas 11/30/2018 | 04:42

It's super great! The same picture on the Flat Earth really impossible due to the properties of atmosphere and it's gasses. But there is vacuum and maybe there is aether. And it could be more dense closer to Earth surface. And lightspeed could be faster in this denser aether. A littlebit. And produce very stable negative refraction. Hypothetically.

4Nicolas 12/7/2018 | 13:12

I really don't believe that you've surrendered to the problem of measuring of terrestrial refraction. Moreover, you was very close to the truth. Some correction of method and formulas,maybe... I guess, one middle point only needed.

5wabiswalter@bislins.ch (Walter Bislin, Author of this Page) 12/7/2018 | 23:02

Nicolas, if you know a method to align at least 3 measuring rods exactly parallel at a distance of about some 100 m or more each, let me know. The only way I can think of is to measure the angle to a fix star at each location at the same latitude at the same time. But this only will work if you believe that stars are very far away, much more than what flat earther believe.

We have instruments that measure refraction directly using 2 lasers with different wave lengths which are reflected from a target. But this method is nothing a layman can use. This instruments are cutting edge technology and have to be extremely precise, because the difference in the light paths are extremely small. This instruments are very expensive.

But anyway, we have multiple ways to measure refraction and correct the measurements accordingly. And we always get a drop with distance as expected by the WGS84 globe model.

Nowadayas we use directly GPS to acquire 3D coordinates of any point on earth or in space. Jesse drove over the Causeway bridge with his car equipped with GPS recorders. He then plotted the data. And guess what, the bridge curves with a radius corresponding to the accepted radius of the earth. This experiment will be repeated with much more details this winter.

6Nicolas 12/8/2018 | 09:53

Oh, I've just mistaken too with your method, with what I wanted to change with it. You are right.
But I have another method. It measures change of declination of vertical line (or not exactly vertical)due to the distance of measuring. It is just modified method of measuring the distances between top and bottom points of bridge vertical supports. I propose to use mountain slope. Distance between two points depends from refraction, but difference doesn't so much. (Really, this method is not so simple. For example, if to measure distances to low and upper point from lower point, the measurement will show declining due to refraction, not for declining itself)
Really the fixed star is the most precise method, of course

7Nicolas 12/8/2018 | 23:50

And there is more simple method to detect the declining of axes: using vertical declination measured between two points in both directions, and geometric properties of parallel lines crossed by third line, but refraction will affect the result. Meanwhile the first method doesn't depend of refraction.

8Nicolas 12/9/2018 | 09:44

If to talk about Flat Earth and our beliefs, my model of Flat Earth ( really I'm not a flatearther, but I've built the model of Flat Earth, or rather flat part of huge jupiterlike planet, on my view, the only possible, if to be flat). According to my model, all terrestrial bodies ( the Sun and Moon too) that we see, or don't exist at all, or situated in absolutely other places (but in the same configuration ). And there is Dome above Earth, that doesn't rotate, that wasn't built, but had grown itself long time ago, that is a living organism, and it has a holographic function and other functions. And there is no any conspiracy, spacecrafts work, and show us real photos, but Dome works too. And we may rely on stars that Dome shows, but it is only our belief, that it is shown in that way, that we need.

9NickMet 2/14/2019 | 18:11

Hi, I guess I found an error in refr calc.

Water texture does not go to true horizon level, check several times.

10wabiswalter@bislins.ch (Walter Bislin, Author of this Page) 2/14/2019 | 23:24

Nick, thank you for your feedback.

It's not an error but something I can improve. You have to insert a value at Horizon Dist other than auto on the Rendering panel, because auto sets the horizon too near if the target is not behind the curvature of the globe. The rendering of the ground stops at the horizon sphere, which is in your case too near.

The horizon in the simulator is a sphere with radius Horizon Dist. If a light ray hits this sphere the ray tracer stops there, even if the real globe or FE horizon is much farther away. This limit is there to speed up the calculation, because it limits the number of light segments that have to be calculated. So depending on the scene you are trying to create, you have to manually enter a suitable horizon distance depending on the observer height. Try 50 to 100 km. This should do it.

I will improve the calculation of the auto horizon to set it some factor behind the globe horizon.

Your Comment to this Article
Email optional; wird nicht angezeigt
  • Name is displayed on your Comment.
  • Email is only for the Admin, will not be displayed.
  • You can edit or delete your Comment for some Time.
  • You can use Comment Formatings, eg Codes, Formulas...
  • External Links and Pictures will not be displayed, until enabled by the Admin.
Weitere Infos zur Seite
Erzeugt Thursday, November 22, 2018
von wabis
Zum Seitenanfang
Geändert Thursday, February 7, 2019
von wabis