Skip to content

Commit 646aa5f

Browse files
committed
refactor refract
1 parent 5b74221 commit 646aa5f

File tree

1 file changed

+21
-25
lines changed

1 file changed

+21
-25
lines changed

pvlib/solarposition.py

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -342,40 +342,36 @@ def ephemeris(time, location, pressure=101325, temperature=12):
342342
SunAz = np.degrees(np.arctan2(- 1 * np.sin(HrAngleR), np.cos(LatR) *
343343
(np.tan(DecR)) - np.sin(LatR)*(np.cos(HrAngleR))))
344344
SunAz[SunAz < 0] += 360
345-
# potential error in the following line
346-
SunEl = np.degrees(np.arcsin((np.cos(LatR) * (np.cos(DecR)) *
347-
(np.cos(HrAngleR)) + np.sin(LatR)*(np.sin(DecR)))))
348-
345+
346+
SunEl = np.degrees(np.arcsin(
347+
np.cos(LatR) * np.cos(DecR) * np.cos(HrAngleR) +
348+
np.sin(LatR) * np.sin(DecR) ))
349+
349350
SolarTime = (180 + HrAngle) / 15.
350351

351352
# Calculate refraction correction
352-
# replace with conditional array assignment
353-
Refract = []
354-
for Elevation in SunEl:
355-
TanEl = np.tan(np.radians(Elevation))
356-
if Elevation > 5 and Elevation <= 85:
357-
Refract.append((58.1 / float(TanEl) - 0.07 / float(TanEl ** 3) +
358-
8.6e-05 / float(TanEl ** 5)))
359-
elif Elevation > -0.575 and Elevation <= 5:
360-
Refract.append((Elevation * ((- 518.2 + Elevation *
361-
((103.4 + Elevation * ((- 12.79 + Elevation *
362-
(0.711))))))) + 1735))
363-
elif Elevation > -1 and Elevation <= -0.575:
364-
Refract.append(- 20.774 / float(TanEl))
365-
else:
366-
Refract.append(0)
367-
368-
Refract = (np.array(Refract) * ((283 / (273. + temperature))) *
369-
pressure / 101325. / 3600.)
370-
371-
SunZen = 90 - SunEl
353+
Elevation = SunEl
354+
TanEl = pd.Series(np.tan(np.radians(Elevation)), index=time_utc)
355+
Refract = pd.Series(0, index=time_utc)
356+
357+
Refract[(Elevation > 5) & (Elevation <= 85)] = (
358+
58.1/TanEl - 0.07/(TanEl**3) + 8.6e-05/(TanEl**5))
359+
360+
Refract[(Elevation > -0.575) & (Elevation <= 5)] = ( Elevation *
361+
(-518.2 + Elevation*(103.4 + Elevation*(-12.79 + Elevation*0.711))) +
362+
1735 )
363+
364+
Refract[(Elevation > -1) & (Elevation <= -0.575)] = -20.774 / TanEl
365+
366+
Refract *= (283/(273. + temperature)) * (pressure/101325.) / 3600.
372367

373368
ApparentSunEl = SunEl + Refract
374369

370+
# make output DataFrame
375371
DFOut = pd.DataFrame(index=time_utc).tz_convert(location.tz)
376372
DFOut['elevation'] = SunEl
377373
DFOut['azimuth'] = SunAz
378-
DFOut['zenith'] = SunZen
374+
DFOut['zenith'] = 90 - SunEl
379375
DFOut['apparent_elevation'] = ApparentSunEl
380376
DFOut['apparent_zenith'] = 90 - ApparentSunEl
381377
DFOut['solar_time'] = SolarTime

0 commit comments

Comments
 (0)