Skip to content

Commit 4a38b0e

Browse files
committed
Substitute occurrences of pvlib.__file__ or similar where appropiate
1 parent 3a91cd7 commit 4a38b0e

File tree

12 files changed

+37
-63
lines changed

12 files changed

+37
-63
lines changed

docs/examples/adr-pvarray/plot_simulate_system.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@
1212
Author: Anton Driesse
1313
"""
1414

15-
import os
1615
import pandas as pd
1716
import matplotlib.pyplot as plt
1817

1918
import pvlib
20-
from pvlib import iotools, location
19+
from pvlib import iotools, location, tools
2120
from pvlib.irradiance import get_total_irradiance
2221
from pvlib.pvarray import pvefficiency_adr
2322

@@ -26,10 +25,9 @@
2625
# Read a TMY3 file containing weather data and select needed columns
2726
#
2827

29-
PVLIB_DIR = pvlib.__path__[0]
30-
DATA_FILE = os.path.join(PVLIB_DIR, 'data', '723170TYA.CSV')
28+
tmy3_filepath = tools.get_test_dataset_path('723170TYA.CSV')
3129

32-
tmy, metadata = iotools.read_tmy3(DATA_FILE, coerce_year=1990,
30+
tmy, metadata = iotools.read_tmy3(tmy3_filepath, coerce_year=1990,
3331
map_variables=True)
3432

3533
df = pd.DataFrame({'ghi': tmy['ghi'], 'dhi': tmy['dhi'], 'dni': tmy['dni'],

docs/examples/irradiance-decomposition/plot_diffuse_fraction.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,18 @@
1414
# GHI into the diffuse and direct components. The separate components are
1515
# needed to estimate the total irradiance on a tilted surface.
1616

17-
import pathlib
1817
from matplotlib import pyplot as plt
1918
import pandas as pd
2019
from pvlib.iotools import read_tmy3
2120
from pvlib.solarposition import get_solarposition
22-
from pvlib import irradiance
23-
import pvlib
21+
from pvlib import irradiance, tools
2422

2523
# For this example we use the Greensboro, North Carolina, TMY3 file which is
2624
# in the pvlib data directory. TMY3 are made from the median months from years
2725
# of data measured from 1990 to 2010. Therefore we change the timestamps to a
2826
# common year, 1990.
29-
DATA_DIR = pathlib.Path(pvlib.__file__).parent / 'data'
30-
greensboro, metadata = read_tmy3(DATA_DIR / '723170TYA.CSV', coerce_year=1990,
27+
tmy3_filepath = tools.get_test_dataset_path('723170TYA.CSV')
28+
greensboro, metadata = read_tmy3(tmy3_filepath, coerce_year=1990,
3129
map_variables=True)
3230

3331
# Many of the diffuse fraction estimation methods require the "true" zenith, so

docs/examples/irradiance-transposition/plot_seasonal_tilt.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@
1212
# to use a custom Mount class to use the Seasonal Tilt strategy
1313
# with :py:class:`~pvlib.modelchain.ModelChain`.
1414

15-
import pvlib
16-
from pvlib import pvsystem, location, modelchain, iotools
15+
from pvlib import pvsystem, location, modelchain, iotools, tools
1716
from pvlib.temperature import TEMPERATURE_MODEL_PARAMETERS
1817
import pandas as pd
19-
import pathlib
2018
import matplotlib.pyplot as plt
2119
from dataclasses import dataclass
2220

@@ -43,8 +41,8 @@ def get_orientation(self, solar_zenith, solar_azimuth):
4341
# First let's grab some weather data and make sure our mount produces tilts
4442
# like we expect:
4543

46-
DATA_DIR = pathlib.Path(pvlib.__file__).parent / 'data'
47-
tmy, metadata = iotools.read_tmy3(DATA_DIR / '723170TYA.CSV', coerce_year=1990,
44+
tmy3_filepath = tools.get_test_dataset_path('723170TYA.CSV')
45+
tmy, metadata = iotools.read_tmy3(tmy3_filepath, coerce_year=1990,
4846
map_variables=True)
4947
# shift from TMY3 right-labeled index to left-labeled index:
5048
tmy.index = tmy.index - pd.Timedelta(hours=1)

docs/examples/irradiance-transposition/plot_transposition_gain.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,19 @@
1919
# insolation is calculated for each strategy to show how orientation affects
2020
# seasonal irradiance collection.
2121

22-
import pvlib
2322
from pvlib import location
2423
from pvlib import irradiance
2524
from pvlib import tracking
25+
from pvlib import tools
2626
from pvlib.iotools import read_tmy3
2727
import pandas as pd
2828
from matplotlib import pyplot as plt
29-
import pathlib
3029

31-
# get full path to the data directory
32-
DATA_DIR = pathlib.Path(pvlib.__file__).parent / 'data'
30+
# get full path to the example file
31+
tmy3_filepath = tools.get_test_dataset_path('723170TYA.CSV')
3332

3433
# get TMY3 dataset
35-
tmy, metadata = read_tmy3(DATA_DIR / '723170TYA.CSV', coerce_year=1990,
36-
map_variables=True)
34+
tmy, metadata = read_tmy3(tmy3_filepath, coerce_year=1990, map_variables=True)
3735
# TMY3 datasets are right-labeled (AKA "end of interval") which means the last
3836
# interval of Dec 31, 23:00 to Jan 1 00:00 is labeled Jan 1 00:00. When rolling
3937
# up hourly irradiance to monthly insolation, a spurious January value is

docs/examples/soiling/plot_fig3A_hsu_soiling_example.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@
2121
# PM2.5 and PM10 data come from the EPA. First, let's read in the
2222
# weather data and run the HSU soiling model:
2323

24-
import pathlib
2524
from matplotlib import pyplot as plt
2625
from pvlib import soiling
2726
import pvlib
2827
import pandas as pd
2928

30-
# get full path to the data directory
31-
DATA_DIR = pathlib.Path(pvlib.__file__).parent / 'data'
29+
# get full path to the example file
30+
soiling_hsu_filepath = \
31+
pvlib.tools.get_test_dataset_path('soiling_hsu_example_inputs.csv')
3232

3333
# read rainfall, PM2.5, and PM10 data from file
34-
imperial_county = pd.read_csv(DATA_DIR / 'soiling_hsu_example_inputs.csv',
35-
index_col=0, parse_dates=True)
34+
imperial_county = pd.read_csv(soiling_hsu_filepath, index_col=0,
35+
parse_dates=True)
3636
rainfall = imperial_county['rain']
3737
depo_veloc = {'2_5': 0.0009, '10': 0.004} # default values from [1] (m/s)
3838
rain_accum_period = pd.Timedelta('1h') # default

docs/sphinx/source/user_guide/clearsky.rst

+4-12
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ We'll need these imports for the examples below.
3030

3131
.. ipython::
3232

33-
In [1]: import os
34-
3533
In [1]: import itertools
3634

3735
In [1]: import matplotlib.pyplot as plt
@@ -40,7 +38,7 @@ We'll need these imports for the examples below.
4038

4139
In [1]: import pvlib
4240

43-
In [1]: from pvlib import clearsky, atmosphere, solarposition
41+
In [1]: from pvlib import clearsky, atmosphere, solarposition, tools
4442

4543
In [1]: from pvlib.location import Location
4644

@@ -130,13 +128,9 @@ the year. You could run it in a loop to create plots for all months.
130128

131129
In [1]: import calendar
132130

133-
In [1]: import os
134-
135131
In [1]: import h5py
136132

137-
In [1]: pvlib_path = os.path.dirname(os.path.abspath(pvlib.clearsky.__file__))
138-
139-
In [1]: filepath = os.path.join(pvlib_path, 'data', 'LinkeTurbidities.h5')
133+
In [1]: filepath = tools.get_test_dataset_path('LinkeTurbidities.h5')
140134

141135
In [1]: def plot_turbidity_map(month, vmin=1, vmax=100):
142136
...: plt.figure();
@@ -210,13 +204,11 @@ wavelengths [Bir80]_, and is implemented in
210204

211205
.. ipython::
212206

213-
In [1]: pvlib_data = os.path.join(os.path.dirname(pvlib.__file__), 'data')
214-
215207
In [1]: mbars = 100 # conversion factor from mbars to Pa
216208

217-
In [1]: tmy_file = os.path.join(pvlib_data, '703165TY.csv') # TMY file
209+
In [1]: tmy_file_path = tools.get_test_dataset_path('703165TY.csv') # TMY file
218210

219-
In [1]: tmy_data, tmy_header = read_tmy3(tmy_file, coerce_year=1999, map_variables=True)
211+
In [1]: tmy_data, tmy_header = read_tmy3(tmy_file_path, coerce_year=1999, map_variables=True)
220212

221213
In [1]: tl_historic = clearsky.lookup_linke_turbidity(time=tmy_data.index,
222214
...: latitude=tmy_header['latitude'], longitude=tmy_header['longitude'])

pvlib/clearsky.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
to calculate clear sky GHI, DNI, and DHI.
44
"""
55

6-
import os
76
from collections import OrderedDict
87
import calendar
98

@@ -14,7 +13,7 @@
1413
import h5py
1514

1615
from pvlib import atmosphere, tools
17-
from pvlib.tools import _degrees_to_index
16+
from pvlib.tools import _degrees_to_index, get_test_dataset_path
1817

1918

2019
def ineichen(apparent_zenith, airmass_absolute, linke_turbidity,
@@ -189,8 +188,7 @@ def lookup_linke_turbidity(time, latitude, longitude, filepath=None,
189188
# 1st column: 179.9583 W, 2nd column: 179.875 W
190189

191190
if filepath is None:
192-
pvlib_path = os.path.dirname(os.path.abspath(__file__))
193-
filepath = os.path.join(pvlib_path, 'data', 'LinkeTurbidities.h5')
191+
filepath = get_test_dataset_path('LinkeTurbidities.h5')
194192

195193
latitude_index = _degrees_to_index(latitude, coordinate='latitude')
196194
longitude_index = _degrees_to_index(longitude, coordinate='longitude')

pvlib/location.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
# Will Holmgren, University of Arizona, 2014-2016.
66

7-
import os
87
import datetime
98

109
import pandas as pd
1110
import pytz
1211
import h5py
1312

1413
from pvlib import solarposition, clearsky, atmosphere, irradiance
15-
from pvlib.tools import _degrees_to_index
14+
from pvlib.tools import _degrees_to_index, get_test_dataset_path
15+
1616

1717
class Location:
1818
"""
@@ -426,8 +426,7 @@ def lookup_altitude(latitude, longitude):
426426
427427
"""
428428

429-
pvlib_path = os.path.dirname(os.path.abspath(__file__))
430-
filepath = os.path.join(pvlib_path, 'data', 'Altitude.h5')
429+
filepath = get_test_dataset_path('Altitude.h5')
431430

432431
latitude_index = _degrees_to_index(latitude, coordinate='latitude')
433432
longitude_index = _degrees_to_index(longitude, coordinate='longitude')

pvlib/pvsystem.py

+6-12
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import functools
88
import io
99
import itertools
10-
import os
1110
from urllib.request import urlopen
1211
import numpy as np
1312
from scipy import constants
@@ -20,7 +19,7 @@
2019

2120
from pvlib import (atmosphere, iam, inverter, irradiance,
2221
singlediode as _singlediode, temperature)
23-
from pvlib.tools import _build_kwargs, _build_args
22+
from pvlib.tools import _build_kwargs, _build_args, get_test_dataset_path
2423

2524

2625
# a dict of required parameter names for each DC power model
@@ -2371,24 +2370,19 @@ def retrieve_sam(name=None, path=None):
23712370

23722371
if name is not None:
23732372
name = name.lower()
2374-
data_path = os.path.join(
2375-
os.path.dirname(os.path.abspath(__file__)), 'data')
23762373
if name == 'cecmod':
2377-
csvdata = os.path.join(
2378-
data_path, 'sam-library-cec-modules-2019-03-05.csv')
2374+
csvdata = 'sam-library-cec-modules-2019-03-05.csv'
23792375
elif name == 'sandiamod':
2380-
csvdata = os.path.join(
2381-
data_path, 'sam-library-sandia-modules-2015-6-30.csv')
2376+
csvdata = 'sam-library-sandia-modules-2015-6-30.csv'
23822377
elif name == 'adrinverter':
2383-
csvdata = os.path.join(
2384-
data_path, 'adr-library-cec-inverters-2019-03-05.csv')
2378+
csvdata = 'adr-library-cec-inverters-2019-03-05.csv'
23852379
elif name in ['cecinverter', 'sandiainverter']:
23862380
# Allowing either, to provide for old code,
23872381
# while aligning with current expectations
2388-
csvdata = os.path.join(
2389-
data_path, 'sam-library-cec-inverters-2019-03-05.csv')
2382+
csvdata = 'sam-library-cec-inverters-2019-03-05.csv'
23902383
else:
23912384
raise ValueError(f'invalid name {name}')
2385+
csvdata = get_test_dataset_path(csvdata)
23922386
elif path is not None:
23932387
if path.startswith('http'):
23942388
response = urlopen(path)

pvlib/spectrum/mismatch.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import numpy as np
77
import pandas as pd
88
from scipy.interpolate import interp1d
9-
import os
109

1110

1211
def get_example_spectral_response(wavelength=None):
@@ -117,10 +116,9 @@ def get_am15g(wavelength=None):
117116
'''
118117
# Contributed by Anton Driesse (@adriesse), PV Performance Labs. Aug. 2022
119118

120-
pvlib_path = pvlib.__path__[0]
121-
filepath = os.path.join(pvlib_path, 'data', 'astm_g173_am15g.csv')
119+
am15g_file = pvlib.tools.get_test_dataset_path('astm_g173_am15g.csv')
122120

123-
am15g = pd.read_csv(filepath, index_col=0).squeeze()
121+
am15g = pd.read_csv(am15g_file, index_col=0).squeeze()
124122

125123
if wavelength is not None:
126124
interpolator = interp1d(am15g.index, am15g,

pvlib/tests/test_tools.py

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ def test_degrees_to_index_1():
9898
with pytest.raises(IndexError): # invalid value for coordinate argument
9999
tools._degrees_to_index(degrees=22.0, coordinate='width')
100100

101+
101102
@pytest.mark.parametrize('location', [tuple(), ('data',)])
102103
def test_get_test_dataset_path_passes(location):
103104
expected_dataset = '723170TYA.CSV'

pvlib/tools.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ def get_test_dataset_path(dataset, location='data'):
491491
location : str or PurePath, default 'data'
492492
PVLIB subfolder where dataset can be found.
493493
This value can be:
494-
494+
495495
+-------------------------+------------------------------+
496496
| ``location`` | Description |
497497
+=========================+==============================+

0 commit comments

Comments
 (0)