lsst.afw  14.0-35-g29d3a87a7
refraction.py
Go to the documentation of this file.
1 from __future__ import absolute_import, division, print_function
2 #
3 # LSST Data Management System
4 # Copyright 2008-2018 LSST/AURA.
5 #
6 # This product includes software developed by the
7 # LSST Project (http://www.lsst.org/).
8 #
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the LSST License Statement and
20 # the GNU General Public License along with this program. If not,
21 # see <http://www.lsstcorp.org/LegalNotices/>.
22 #
23 
24 from astropy import units
25 from astropy.units import cds
26 import numpy as np
27 
28 from lsst.afw.geom import Angle
29 from lsst.afw.coord import Weather
30 
31 __all__ = ["refraction", "differentialRefraction"]
32 
33 # The differential refractive index is the difference of the refractive index from 1.,
34 # multiplied by 1E8 to simplify the notation and equations.
35 deltaRefractScale = 1.0E8
36 
37 
38 def refraction(wavelength, elevation, observatory, weather=None):
39  """Calculate overall refraction under atmospheric and observing conditions.
40 
41  The calculation is taken from Stone 1996
42  "An Accurate Method for Computing Atmospheric Refraction"
43  Parameters
44  ----------
45  wavelength : `float`
46  wavelength is in nm (valid for 230.2 < wavelength < 2058.6)
47  elevation : `lsst.afw.geom.Angle`
48  Elevation of the observation, as an Angle.
49  observatory : `lsst.afw.coord.Observatory`
50  Class containing the longitude, latitude,
51  and altitude of the observatory.
52  weather : `lsst.afw.coord.Weather`, optional
53  Class containing the measured temperature, pressure, and humidity
54  at the observatory during an observation
55  If omitted, typical conditions for the observatory's elevation will be calculated.
56 
57  Returns
58  -------
59  `lsst.afw.geom.Angle`
60  The angular refraction for light of the given wavelength,
61  under the given observing conditions.
62  """
63  if wavelength < 230.2:
64  raise ValueError("Refraction calculation is valid for wavelengths between 230.2 and 2058.6 nm.")
65  if wavelength > 2058.6:
66  raise ValueError("Refraction calculation is valid for wavelengths between 230.2 and 2058.6 nm.")
67  latitude = observatory.getLatitude()
68  altitude = observatory.getElevation()
69  if weather is None:
70  weather = defaultWeather(altitude*units.meter)
71  reducedN = deltaN(wavelength, weather)/deltaRefractScale
72  temperature = extractTemperature(weather, useKelvin=True)
73  atmosScaleheightRatio = 4.5908E-6*temperature/units.Kelvin
74 
75  # Account for oblate Earth
76  # This replicates equation 10 of Stone 1996
77  relativeGravity = (1. + 0.005302*np.sin(latitude.asRadians())**2. -
78  0.00000583*np.sin(2.*latitude.asRadians())**2. - 0.000000315*altitude)
79 
80  # Calculate the tangent of the zenith angle.
81  tanZ = np.tan(np.pi/2. - elevation.asRadians())
82  atmosTerm1 = reducedN*relativeGravity*(1. - atmosScaleheightRatio)
83  atmosTerm2 = reducedN*relativeGravity*(atmosScaleheightRatio - reducedN/2.)
84  result = Angle(atmosTerm1*tanZ + atmosTerm2*tanZ**3.)
85  return result
86 
87 
88 def differentialRefraction(wavelength, wavelengthRef, elevation, observatory, weather=None):
89  """Calculate the differential refraction between two wavelengths.
90 
91  Parameters
92  ----------
93  wavelength : `float`
94  wavelength is in nm (valid for 230.2 < wavelength < 2058.6)
95  wavelengthRef : `float`
96  Reference wavelength, typically the effective wavelength of a filter.
97  elevation : `lsst.afw.geom.Angle`
98  Elevation of the observation, as an Angle.
99  observatory : `lsst.afw.coord.Observatory`
100  Class containing the longitude, latitude,
101  and altitude of the observatory.
102  weather : `lsst.afw.coord.Weather`, optional
103  Class containing the measured temperature, pressure, and humidity
104  at the observatory during an observation
105  If omitted, typical conditions for the observatory's elevation will be calculated.
106 
107  Returns
108  -------
109  `lsst.afw.geom.Angle`
110  The refraction at `wavelength` - the refraction at `wavelengthRef`.
111  """
112  refractionStart = refraction(wavelength, elevation, observatory, weather=weather)
113  refractionEnd = refraction(wavelengthRef, elevation, observatory, weather=weather)
114  return refractionStart - refractionEnd
115 
116 
117 def deltaN(wavelength, weather):
118  """Calculate the differential refractive index of air.
119 
120  The differential refractive index is the difference of
121  the refractive index from 1., multiplied by 1E8 to simplify
122  the notation and equations. Calculated as (n_air - 1)*10^8
123 
124  This replicates equation 14 of Stone 1996
125 
126  Parameters
127  ----------
128  wavelength : `float`
129  wavelength is in nanometers
130  weather : `lsst.afw.coord.Weather`
131  Class containing the measured temperature, pressure, and humidity
132  at the observatory during an observation
133 
134  Returns
135  -------
136  `float`
137  The difference of the refractive index of air from 1.,
138  calculated as (n_air - 1)*10^8
139  """
140  waveNum = 1E3/wavelength # want wave number in units 1/micron
141  dryAirTerm = 2371.34 + (683939.7/(130. - waveNum**2.)) + (4547.3/(38.9 - waveNum**2.))
142  wetAirTerm = 6487.31 + 58.058*waveNum**2. - 0.71150*waveNum**4. + 0.08851*waveNum**6.
143  return (dryAirTerm*densityFactorDry(weather) +
144  wetAirTerm*densityFactorWater(weather))
145 
146 
147 def densityFactorDry(weather):
148  """Calculate dry air pressure term to refractive index calculation.
149 
150  This replicates equation 15 of Stone 1996
151 
152  Parameters
153  ----------
154  weather : `lsst.afw.coord.Weather`
155  Class containing the measured temperature, pressure, and humidity
156  at the observatory during an observation
157 
158  Returns
159  -------
160  `float`
161  Returns the relative density of dry air
162  at the given pressure and temperature.
163  """
164  temperature = extractTemperature(weather, useKelvin=True)
165  waterVaporPressure = humidityToPressure(weather)
166  airPressure = weather.getAirPressure()*units.pascal
167  dryPressure = airPressure - waterVaporPressure
168  eqn = (dryPressure/cds.mbar)*(57.90E-8 - 9.3250E-4*units.Kelvin/temperature +
169  0.25844*units.Kelvin**2/temperature**2.)
170  densityFactor = (1. + eqn)*(dryPressure/cds.mbar)/(temperature/units.Kelvin)
171  return densityFactor
172 
173 
174 def densityFactorWater(weather):
175  """Calculate water vapor pressure term to refractive index calculation.
176 
177  This replicates equation 16 of Stone 1996
178 
179  Parameters
180  ----------
181  weather : `lsst.afw.coord.Weather`
182  Class containing the measured temperature, pressure, and humidity
183  at the observatory during an observation
184 
185  Returns
186  -------
187  `float`
188  Returns the relative density of water vapor
189  at the given pressure and temperature.
190  """
191  temperature = extractTemperature(weather, useKelvin=True)
192  waterVaporPressure = humidityToPressure(weather)
193  densityEqn1 = (-2.37321E-3 + 2.23366*units.Kelvin/temperature -
194  710.792*units.Kelvin**2/temperature**2. +
195  7.75141E-4*units.Kelvin**3/temperature**3.)
196  densityEqn2 = (waterVaporPressure/cds.mbar)*(1. + 3.7E-4*waterVaporPressure/cds.mbar)
197  relativeDensity = waterVaporPressure*units.Kelvin/(temperature*cds.mbar)
198  densityFactor = (1 + densityEqn2*densityEqn1)*relativeDensity
199 
200  return densityFactor
201 
202 
203 def humidityToPressure(weather):
204  """Convert humidity and temperature to water vapor pressure.
205 
206  This replicates equations 18 & 20 of Stone 1996
207 
208  Parameters
209  ----------
210  weather : `lsst.afw.coord.Weather`
211  Class containing the measured temperature, pressure, and humidity
212  at the observatory during an observation
213 
214  Returns
215  -------
216  `float`
217  The water vapor pressure in millibar
218  calculated from the given humidity and temperature.
219  """
220  humidity = weather.getHumidity()
221  x = np.log(humidity/100.0)
222  temperature = extractTemperature(weather)
223  temperatureEqn1 = (temperature + 238.3*units.Celsius)*x + 17.2694*temperature
224  temperatureEqn2 = (temperature + 238.3*units.Celsius)*(17.2694 - x) - 17.2694*temperature
225  dewPoint = 238.3*temperatureEqn1/temperatureEqn2
226  waterVaporPressure = (4.50874 + 0.341724*dewPoint + 0.0106778*dewPoint**2 + 0.184889E-3*dewPoint**3 +
227  0.238294E-5*dewPoint**4 + 0.203447E-7*dewPoint**5)*133.32239*units.pascal
228 
229  return waterVaporPressure
230 
231 
232 def extractTemperature(weather, useKelvin=False):
233  """Thin wrapper to return the measured temperature from an observation.
234 
235  Parameters
236  ----------
237  weather : `lsst.afw.coord.Weather`
238  Class containing the measured temperature, pressure, and humidity
239  at the observatory during an observation
240  useKelvin : bool, optional
241  Set to True to return the temperature in Kelvin instead of Celsius
242  This is needed because Astropy can't easily convert
243  between Kelvin and Celsius.
244 
245  Returns
246  -------
247  `astropy.units.quantity.Quantity`
248  The temperature in Celsius, unless `useKelvin` is set.
249  """
250  temperature = weather.getAirTemperature()*units.Celsius
251  if useKelvin:
252  temperature = temperature.to(units.Kelvin, equivalencies=units.temperature())
253  return temperature
254 
255 
256 def defaultWeather(altitude):
257  """Set default local weather conditions if they are missing.
258 
259  Parameters
260  ----------
261  weather : `lsst.afw.coord.Weather`
262  Class containing the measured temperature, pressure, and humidity
263  at the observatory during an observation
264  altitude : `astropy.units.quantity.Quantity`
265  The altitude of the observatory, in meters.
266 
267  Returns
268  -------
269  `lsst.afw.coord.Weather`
270  Updated Weather class with any `nan` values replaced by defaults.
271  """
272  if isinstance(altitude, units.quantity.Quantity):
273  altitude2 = altitude
274  else:
275  altitude2 = altitude*units.meter
276  p0 = 101325.*units.pascal # sea level air pressure
277  g = 9.80665*units.meter/units.second**2 # typical gravitational acceleration at sea level
278  R0 = 8.31447*units.Joule/(units.mol*units.Kelvin) # gas constant
279  T0 = 19.*units.Celsius # Typical sea-level temperature
280  lapseRate = -6.5*units.Celsius/units.km # Typical rate of change of temperature with altitude
281  M = 0.0289644*units.kg/units.mol # molar mass of dry air
282 
283  temperature = T0 + lapseRate*altitude2
284  temperatureK = temperature.to(units.Kelvin, equivalencies=units.temperature())
285  pressure = p0*np.exp(-(g*M*altitude2)/(R0*temperatureK))
286  humidity = 40. # Typical humidity at many observatory sites.
287  weather = Weather((temperature/units.Celsius).value, (pressure/units.pascal).value, humidity)
288  return weather
def densityFactorWater(weather)
Definition: refraction.py:174
def humidityToPressure(weather)
Definition: refraction.py:203
def defaultWeather(altitude)
Definition: refraction.py:256
def refraction(wavelength, elevation, observatory, weather=None)
Definition: refraction.py:38
def densityFactorDry(weather)
Definition: refraction.py:147
A class representing an angle.
Definition: Angle.h:102
Basic weather information sufficient for a simple model for air mass or refraction.
Definition: Weather.h:38
def differentialRefraction(wavelength, wavelengthRef, elevation, observatory, weather=None)
Definition: refraction.py:88
def deltaN(wavelength, weather)
Definition: refraction.py:117
def extractTemperature(weather, useKelvin=False)
Definition: refraction.py:232