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