Skip to content

Commit e2c17c6

Browse files
committed
* set minimum index as 0 not 1 * adjust _linearly_scale so that is increments are 5 minutes or 1/12 of an arcdegree from the centers of neighboring indices * this fixes the out of bounds error for latitudes or longitudes at the limits of the range * move _linearly_scale to be right after lookup_linke_turbidity * add link to pdf on AOD and LT from MeteoTest * raise IndexError if outputmatrix is more than half an index outside of the limits Signed-off-by: Mark Mikofski <[email protected]>
1 parent 5ddd9b1 commit e2c17c6

File tree

2 files changed

+88
-11
lines changed

2 files changed

+88
-11
lines changed

pvlib/clearsky.py

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
from __future__ import division
77

8+
import logging
9+
810
import os
911
from collections import OrderedDict
1012

@@ -13,6 +15,9 @@
1315

1416
from pvlib import tools
1517

18+
LOGGER = logging.getLogger(__name__)
19+
LOGGER.setLevel(logging.DEBUG)
20+
1621

1722
def ineichen(apparent_zenith, airmass_absolute, linke_turbidity,
1823
altitude=0, dni_extra=1364.):
@@ -186,6 +191,12 @@ def lookup_linke_turbidity(time, latitude, longitude, filepath=None,
186191
# so divide the number from the file by 20 to get the
187192
# turbidity.
188193

194+
# The nodes of the grid are 5' (1/12=0.0833[arcdeg]) apart.
195+
# From Section 8 of Aerosol optical depth and Linke turbidity climatology
196+
# http://www.meteonorm.com/images/uploads/downloads/ieashc36_report_TL_AOD_climatologies.pdf
197+
# 1st row: 89.9583 S, 2nd row: 89.875 S
198+
# 1st column: 179.9583 W, 2nd column: 179.875 W
199+
189200
try:
190201
import scipy.io
191202
except ImportError:
@@ -201,10 +212,10 @@ def lookup_linke_turbidity(time, latitude, longitude, filepath=None,
201212
linke_turbidity_table = mat['LinkeTurbidity']
202213

203214
latitude_index = (
204-
np.around(_linearly_scale(latitude, 90, -90, 1, 2160))
215+
np.around(_linearly_scale(latitude, 90, -90, 0, 2160))
205216
.astype(np.int64))
206217
longitude_index = (
207-
np.around(_linearly_scale(longitude, -180, 180, 1, 4320))
218+
np.around(_linearly_scale(longitude, -180, 180, 0, 4320))
208219
.astype(np.int64))
209220

210221
g = linke_turbidity_table[latitude_index][longitude_index]
@@ -232,6 +243,33 @@ def lookup_linke_turbidity(time, latitude, longitude, filepath=None,
232243
return linke_turbidity
233244

234245

246+
def _linearly_scale(inputmatrix, inputmin, inputmax, outputmin, outputmax):
247+
"""
248+
Used by linke turbidity lookup function.
249+
"""
250+
inputrange = inputmax - inputmin
251+
outputrange = outputmax - outputmin
252+
delta = outputrange/inputrange # number of indices per input unit
253+
inputmin = inputmin + 1.0 / delta / 2.0 # shift index to center
254+
outputmax = outputmax - 1
255+
outputmatrix = (inputmatrix - inputmin) * delta + outputmin
256+
LOGGER.debug('outputmatrix: %g', outputmatrix)
257+
# TODO: allow wrapping for longitude
258+
err = IndexError('Input, %g, is out of range (%g, %g).' %
259+
(inputmatrix, inputmax - inputrange, inputmax))
260+
if outputmatrix > outputmax:
261+
if np.around(outputmatrix - outputmax, 1) <= 0.5:
262+
outputmatrix = outputmax
263+
else:
264+
raise err
265+
elif outputmatrix < outputmin:
266+
if np.around(outputmin - outputmatrix, 1) <= 0.5:
267+
outputmatrix = outputmin
268+
else:
269+
raise err
270+
return outputmatrix
271+
272+
235273
def haurwitz(apparent_zenith):
236274
'''
237275
Determine clear sky GHI from Haurwitz model.
@@ -281,15 +319,6 @@ def haurwitz(apparent_zenith):
281319
return df_out
282320

283321

284-
def _linearly_scale(inputmatrix, inputmin, inputmax, outputmin, outputmax):
285-
""" used by linke turbidity lookup function """
286-
287-
inputrange = inputmax - inputmin
288-
outputrange = outputmax - outputmin
289-
outputmatrix = (inputmatrix-inputmin) * outputrange/inputrange + outputmin
290-
return outputmatrix
291-
292-
293322
def simplified_solis(apparent_elevation, aod700=0.1, precipitable_water=1.,
294323
pressure=101325., dni_extra=1364.):
295324
"""

pvlib/test/test_clearsky.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from collections import OrderedDict
22

3+
import logging
4+
35
import numpy as np
46
import pandas as pd
57

@@ -14,6 +16,9 @@
1416

1517
from conftest import requires_scipy
1618

19+
LOGGER = logging.getLogger(__name__)
20+
LOGGER.setLevel(logging.DEBUG)
21+
1722

1823
def test_ineichen_series():
1924
tus = Location(32.2, -111, 'US/Arizona', 700)
@@ -440,3 +445,46 @@ def test_simplified_solis_nans_series():
440445
precipitable_water, pressure, dni_extra)
441446

442447
assert_frame_equal(expected, out)
448+
449+
450+
def test_linke_turbidity_corners():
451+
"""
452+
Test Linke turbidity corners out of bounds
453+
"""
454+
time = pd.DatetimeIndex('%d/1/2016' % (m + 1) for m in range(12))
455+
# Northwest
456+
assert np.allclose(
457+
clearsky.lookup_linke_turbidity(time, 90, -180, interp_turbidity=False),
458+
[1.9, 1.9, 1.9, 2.0, 2.05, 2.05, 2.1, 2.1, 2.0, 1.95, 1.9, 1.9])
459+
# Southwest
460+
assert np.allclose(
461+
clearsky.lookup_linke_turbidity(time, -90, -180, interp_turbidity=False),
462+
[1.35, 1.3, 1.45, 1.35, 1.35, 1.35, 1.35, 1.35, 1.35, 1.4, 1.4, 1.3])
463+
# Northeast
464+
assert np.allclose(
465+
clearsky.lookup_linke_turbidity(time, 90, 180, interp_turbidity=False),
466+
[1.9, 1.9, 1.9, 2.0, 2.05, 2.05, 2.1, 2.1, 2.0, 1.95, 1.9, 1.9])
467+
# Southeast
468+
assert np.allclose(
469+
clearsky.lookup_linke_turbidity(time, -90, 180, interp_turbidity=False),
470+
[1.35, 1.7, 1.35, 1.35, 1.35, 1.35, 1.35, 1.35, 1.35, 1.35, 1.35, 1.7])
471+
try:
472+
clearsky.lookup_linke_turbidity(time, 91, -122, interp_turbidity=False)
473+
except IndexError as err:
474+
LOGGER.exception(err)
475+
assert isinstance(err, IndexError)
476+
try:
477+
clearsky.lookup_linke_turbidity(time, 38.2, 181, interp_turbidity=False)
478+
except IndexError as err:
479+
LOGGER.exception(err)
480+
assert isinstance(err, IndexError)
481+
try:
482+
clearsky.lookup_linke_turbidity(time, -91, -122, interp_turbidity=False)
483+
except IndexError as err:
484+
LOGGER.exception(err)
485+
assert isinstance(err, IndexError)
486+
try:
487+
clearsky.lookup_linke_turbidity(time, 38.2, -181, interp_turbidity=False)
488+
except IndexError as err:
489+
LOGGER.exception(err)
490+
assert isinstance(err, IndexError)

0 commit comments

Comments
 (0)