Skip to content

Commit 18de41d

Browse files
committed
fix namespace clashes
* modeule name solar_utils.py was shadowing package solar_utils/__init__.py so changed it to core.py * also change solar_utils.solar_utils to solar_utils.core in solar_utils/__init__.py where it imports solposAM and spectrl2 and in setup.py where it's used to import __version__ and __name__ from solar_utils/__init__.py * change solar_utils_excpetions to just exceptions * fixed solposAM the exported DLL function was shadowing solposAM() the Python function so made it _solposAM with preceding underscore * ditto for spectrl2 * gitignore venv/ and spyderproject files * remove logging from setup.py * don't create extral LDFLAGS constant, just call make_ldflags() inside link_shared_lib() call for extra_preargs * add if clause to remove libs if clean in sys.argv, then don't run tests * add release name to package __init__.py * combine imports from solar_utils.exceptions in core * change arg x to err_code in _int2bits() to make pylint happy :) * change code to err_code everywhere since code is a builtin Python function * change atmosphericConditions to atomspheric_conditions everywhere, oops! this will break pvsimlife :( * in tests, change rel_diff lambda to constant REL_DIFF * get rid of intermediate values in spectrl2 tests * line up spaces, change fp to filepath and do what pylint likes :|
1 parent dc63461 commit 18de41d

File tree

6 files changed

+71
-76
lines changed

6 files changed

+71
-76
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# build
2+
venv/
23
build/
34
dist/
45
solar_utils.egg-info/
@@ -21,6 +22,7 @@ solar_utils.egg-info/
2122
.pydevproject
2223
SolarUtils.sublime-workspace
2324
SolarUtils.sublime-project
25+
.spyderproject
2426

2527
# sphinx
2628
docs/_build/

setup.py

+14-15
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,9 @@
1111
sys.exit('setuptools was not detected - please install setuptools and pip')
1212
from solar_utils import __version__ as VERSION, __name__ as NAME
1313
from solar_utils.tests import test_cdlls
14-
import logging
1514

16-
logging.basicConfig(level=logging.DEBUG)
17-
LOGGER = logging.getLogger(__name__)
1815
# set platform constants
19-
CCFLAGS, RPATH, INSTALL_NAME, LDFLAGS = None, None, None, None
16+
CCFLAGS, RPATH, INSTALL_NAME = None, None, None
2017
PLATFORM = sys.platform
2118
if PLATFORM == 'win32':
2219
SRC_DIR = 'win32'
@@ -71,8 +68,14 @@ def make_ldflags(lib_name, rpath=RPATH, install_name=INSTALL_NAME):
7168
os.path.exists(os.path.join(LIB_DIR, SPECTRL2_LIB_FILE))
7269
])
7370

74-
# build libraries if they don't exist
75-
if not LIB_FILES_EXIST:
71+
# run clean or build libraries if they don't exist
72+
if 'clean' in sys.argv:
73+
try:
74+
os.remove(os.path.join(LIB_DIR, SOLPOSAM_LIB_FILE))
75+
os.remove(os.path.join(LIB_DIR, SPECTRL2_LIB_FILE))
76+
except OSError as err:
77+
sys.stderr.write('%s\n' % err)
78+
elif not LIB_FILES_EXIST:
7679
# clean build directory
7780
if os.path.exists(BUILD_DIR):
7881
shutil.rmtree(BUILD_DIR) # delete entire directory tree
@@ -84,26 +87,22 @@ def make_ldflags(lib_name, rpath=RPATH, install_name=INSTALL_NAME):
8487
OBJS = CC.compile([SOLPOS, SOLPOSAM], output_dir=BUILD_DIR,
8588
extra_preargs=CCFLAGS)
8689
# link objects and make shared library in build directory
87-
LDFLAGS = make_ldflags(SOLPOSAM_LIB) # make linker option flags for compiler
88-
LOGGER.debug('%s LDFLAGS: %r', SPECTRL2_LIB, LDFLAGS)
8990
CC.link_shared_lib(OBJS, SOLPOSAM_LIB, output_dir=BUILD_DIR,
90-
extra_preargs=LDFLAGS)
91+
extra_preargs=make_ldflags(SOLPOSAM_LIB))
9192
# compile spectrl2 objects into build directory
9293
OBJS = CC.compile([SPECTRL2, SPECTRL2_2, SOLPOS], output_dir=BUILD_DIR,
9394
extra_preargs=CCFLAGS)
9495
CC.set_libraries([SOLPOSAM_LIB]) # set linked libraries
9596
CC.set_library_dirs([BUILD_DIR]) # set library directories
9697
# link objects and make shared library in build directory
97-
LDFLAGS = make_ldflags(SPECTRL2_LIB) # make linker option flags for compiler
98-
LOGGER.debug('%s LDFLAGS: %r', SPECTRL2_LIB, LDFLAGS)
9998
CC.link_shared_lib(OBJS, SPECTRL2_LIB, output_dir=BUILD_DIR,
100-
extra_preargs=LDFLAGS)
99+
extra_preargs=make_ldflags(SPECTRL2_LIB))
101100
# copy files from build to library folder
102101
shutil.copy(os.path.join(BUILD_DIR, SOLPOSAM_LIB_FILE), LIB_DIR)
103102
shutil.copy(os.path.join(BUILD_DIR, SPECTRL2_LIB_FILE), LIB_DIR)
104-
# test libraries
105-
test_cdlls.test_solposAM()
106-
test_cdlls.test_spectrl2()
103+
# test libraries
104+
test_cdlls.test_solposAM()
105+
test_cdlls.test_spectrl2()
107106

108107
setup(name=NAME,
109108
version=VERSION,

solar_utils/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
Do Not Distribute
88
"""
99

10-
from solar_utils import solposAM, spectrl2
10+
from solar_utils.core import solposAM, spectrl2
1111

1212
__version__ = '0.1.1'
13+
__release__ = 'Aerial Acrobats'
1314
__author__ = 'Mark Mikofski'
1415
__all__ = ['solposAM', 'spectrl2']

solar_utils/solar_utils.py solar_utils/core.py

+35-37
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
import math
1919
import os
2020
import sys
21-
22-
from solar_utils_exceptions import SOLPOS_Error
23-
from solar_utils_exceptions import SPECTRL2_Error
21+
from solar_utils.exceptions import SOLPOS_Error, SPECTRL2_Error
2422

2523
_DIRNAME = os.path.dirname(__file__)
2624
if sys.platform == 'win32':
@@ -35,19 +33,18 @@
3533
PLATFORM = 'darwin'
3634
SOLPOSAM = 'libsolposAM.dylib'
3735
SPECTRL2 = 'libspectrl2.dylib'
38-
3936
SOLPOSAMDLL = os.path.join(_DIRNAME, PLATFORM, SOLPOSAM)
4037
SPECTRL2DLL = os.path.join(_DIRNAME, PLATFORM, SPECTRL2)
4138

4239

43-
def _int2bits(x):
40+
def _int2bits(err_code):
4441
"""
4542
Convert integer to bits.
4643
4744
:param x: integer to convert
4845
:returns: log(x, 2)
4946
"""
50-
return int(math.log(x, 2))
47+
return int(math.log(err_code, 2))
5148

5249

5350
def solposAM(location, datetime, weather):
@@ -63,11 +60,11 @@ def solposAM(location, datetime, weather):
6360
:type weather: list of floats
6461
:returns: angles, airmass
6562
:rtype: float
66-
:raises: :exc:`~solar_utils.solar_utils_exceptions.SOLPOS_Error`
63+
:raises: :exc:`~solar_utils.exceptions.SOLPOS_Error`
6764
"""
6865
# load the DLL
69-
solposAMdll = ctypes.cdll.LoadLibrary(SOLPOSAMDLL)
70-
solposAM = solposAMdll.solposAM
66+
solposAM_dll = ctypes.cdll.LoadLibrary(SOLPOSAMDLL)
67+
_solposAM = solposAM_dll.solposAM
7168
# cast Python types as ctypes
7269
_location = (ctypes.c_float * 3)(*location)
7370
_datetime = (ctypes.c_int * 6)(*datetime)
@@ -79,14 +76,14 @@ def solposAM(location, datetime, weather):
7976
orientation = (ctypes.c_float * 2)()
8077
shadowband = (ctypes.c_float * 3)()
8178
# call DLL
82-
code = solposAM(_location, _datetime, _weather, angles, airmass, settings,
83-
orientation, shadowband)
79+
err_code = _solposAM(_location, _datetime, _weather, angles, airmass,
80+
settings, orientation, shadowband)
8481
# return results if successful, otherwise raise SOLPOS_Error
85-
if code == 0:
82+
if err_code == 0:
8683
return angles, airmass
8784
else:
88-
# convert code to bits
89-
_code = _int2bits(code)
85+
# convert err_code to bits
86+
_code = _int2bits(err_code)
9087
data = {'location': location,
9188
'datetime': datetime,
9289
'weather': weather,
@@ -99,7 +96,7 @@ def solposAM(location, datetime, weather):
9996

10097

10198
def spectrl2(units, location, datetime, weather, orientation,
102-
atmosphericConditions, albedo):
99+
atmospheric_conditions, albedo):
103100
"""
104101
Calculate solar spectrum by calling functions exported by
105102
:data:`SPECTRL2DLL`.
@@ -114,14 +111,14 @@ def spectrl2(units, location, datetime, weather, orientation,
114111
:type weather: list of floats
115112
:param orientation: tilt and aspect [degrees]
116113
:type orientation: list of floats
117-
:param atmosphericConditions: alpha, assym, ozone, tau500 and watvap
118-
:type atmosphericConditions: list of floats
114+
:param atmospheric_conditions: alpha, assym, ozone, tau500 and watvap
115+
:type atmospheric_conditions: list of floats
119116
:param albedo: 6 wavelengths and 6 reflectivities
120117
:type albedo: list of lists of floats
121118
:returns: specdif, specdir, specetr, specglo and specx
122119
:rtype: float
123-
:raises: :exc:`~solar_utils.solar_utils_exceptions.SPECTRL2_Error`,
124-
:exc:`~solar_utils.solar_utils_exceptions.SOLPOS_Error`
120+
:raises: :exc:`~solar_utils.exceptions.SPECTRL2_Error`,
121+
:exc:`~solar_utils.exceptions.SOLPOS_Error`
125122
126123
.. seealso::
127124
:func:`solposAM`
@@ -133,23 +130,22 @@ def spectrl2(units, location, datetime, weather, orientation,
133130
>>> datetime = [1999, 7, 22, 9, 45, 37]
134131
>>> weather = [1006.0, 27.0]
135132
>>> orientation = [33.65, 135.0]
136-
>>> atmosphericConditions = [1.14, 0.65, -1.0, 0.2, 1.36]
133+
>>> atmospheric_conditions = [1.14, 0.65, -1.0, 0.2, 1.36]
137134
>>> albedo = [0.3, 0.7, 0.8, 1.3, 2.5, 4.0] + ([0.2] * 6)
138135
>>> (specdif, specdir, specetr, specglo,
139136
specx) = spectrl2(units, location, datetime, weather, orientation,
140-
atmosphericConditions, albedo)
137+
atmospheric_conditions, albedo)
141138
"""
142-
# requires 'solpos.dll'
143139
# load the DLL
144-
ctypes.cdll.LoadLibrary(SOLPOSAMDLL)
145-
spectrl2DLL = ctypes.cdll.LoadLibrary(SPECTRL2DLL)
146-
spectrl2 = spectrl2DLL.spectrl2
140+
ctypes.cdll.LoadLibrary(SOLPOSAMDLL) # requires 'solpos.dll'
141+
spectrl2_dll = ctypes.cdll.LoadLibrary(SPECTRL2DLL)
142+
_spectrl2 = spectrl2_dll.spectrl2
147143
# cast Python types as ctypes
148144
_location = (ctypes.c_float * 3)(*location)
149145
_datetime = (ctypes.c_int * 6)(*datetime)
150146
_weather = (ctypes.c_float * 2)(*weather)
151147
_orientation = (ctypes.c_float * 2)(*orientation)
152-
_atmosphericConditions = (ctypes.c_float * 5)(*atmosphericConditions)
148+
_atmospheric_conditions = (ctypes.c_float * 5)(*atmospheric_conditions)
153149
_albedo = (ctypes.c_float * 12)(*albedo)
154150
# allocate space for results
155151
specdif = (ctypes.c_float * 122)()
@@ -162,21 +158,23 @@ def spectrl2(units, location, datetime, weather, orientation,
162158
settings = (ctypes.c_int * 2)()
163159
shadowband = (ctypes.c_float * 3)()
164160
# call DLL
165-
code = spectrl2(units, _location, _datetime, _weather, _orientation,
166-
_atmosphericConditions, _albedo, specdif, specdir, specetr,
167-
specglo, specx, angles, airmass, settings, shadowband)
161+
err_code = _spectrl2(
162+
units, _location, _datetime, _weather, _orientation,
163+
_atmospheric_conditions, _albedo, specdif, specdir, specetr, specglo,
164+
specx, angles, airmass, settings, shadowband
165+
)
168166
# return results if successful, otherwise raise exception
169-
if code == 0:
167+
if err_code == 0:
170168
return specdif, specdir, specetr, specglo, specx
171-
elif code < 0:
169+
elif err_code < 0:
172170
data = {'units': units,
173-
'tau500': atmosphericConditions[3],
174-
'watvap': atmosphericConditions[4],
175-
'assym': atmosphericConditions[1]}
176-
raise SPECTRL2_Error(code, data)
171+
'tau500': atmospheric_conditions[3],
172+
'watvap': atmospheric_conditions[4],
173+
'assym': atmospheric_conditions[1]}
174+
raise SPECTRL2_Error(err_code, data)
177175
else:
178-
# convert code to bits
179-
_code = _int2bits(code)
176+
# convert err_code to bits
177+
_code = _int2bits(err_code)
180178
data = {'location': location,
181179
'datetime': datetime,
182180
'weather': weather,
File renamed without changes.

solar_utils/tests/test_cdlls.py

+18-23
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
from nose.tools import ok_
2121

2222
from solar_utils import *
23-
from solar_utils.solar_utils_exceptions import SOLPOS_Error, SPECTRL2_Error
23+
from solar_utils.exceptions import SOLPOS_Error, SPECTRL2_Error
2424

2525
_DIRNAME = os.path.dirname(__file__)
2626
TOL = 1E-3
2727

28-
reldiff = lambda (x, x0): np.abs(x - x0) / x0
28+
RELDIFF = lambda (x, x0): np.abs(x - x0) / x0
2929

3030

3131
def test_solposAM():
@@ -41,10 +41,10 @@ def test_solposAM():
4141
# call test function
4242
angles, airmass = solposAM(location, datetime, weather)
4343
# test OUTPUTS are within TOL
44-
ok_(reldiff((angles[0], test_data['angles'][0])) < TOL)
45-
ok_(reldiff((angles[1], test_data['angles'][1])) < TOL)
46-
ok_(reldiff((airmass[0], test_data['airmass'][0])) < TOL)
47-
ok_(reldiff((airmass[1], test_data['airmass'][1])) < TOL)
44+
ok_(RELDIFF((angles[0], test_data['angles'][0])) < TOL)
45+
ok_(RELDIFF((angles[1], test_data['angles'][1])) < TOL)
46+
ok_(RELDIFF((airmass[0], test_data['airmass'][0])) < TOL)
47+
ok_(RELDIFF((airmass[1], test_data['airmass'][1])) < TOL)
4848
# test errors - YEAR
4949
datetime = [2051, 6, 5, 12, 31, 0]
5050
try:
@@ -89,53 +89,48 @@ def test_spectrl2():
8989
"""
9090
# test data
9191
test_data_jsonfile = 'test_spectrl2_data.json'
92-
with open(os.path.join(_DIRNAME, test_data_jsonfile), 'r') as fp:
93-
test_data = json.load(fp)
92+
with open(os.path.join(_DIRNAME, test_data_jsonfile), 'r') as filepath:
93+
test_data = json.load(filepath)
9494
units = 1
9595
location = [33.65, -84.43, -5.0]
9696
datetime = [1999, 7, 22, 9, 45, 37]
9797
weather = [1006.0, 27.0]
9898
orientation = [33.65, 135.0]
99-
atmosphericConditions = [1.14, 0.65, -1.0, 0.2, 1.36]
99+
atmospheric_conditions = [1.14, 0.65, -1.0, 0.2, 1.36]
100100
albedo = [0.3, 0.7, 0.8, 1.3, 2.5, 4.0] + ([0.2] * 6)
101101
# call test function
102102
(specdif, specdir, specetr, specglo,
103103
specx) = spectrl2(units, location, datetime, weather, orientation,
104-
atmosphericConditions, albedo)
104+
atmospheric_conditions, albedo)
105105
# convert OUTPUT to numpy arrays
106106
_specdif = np.ctypeslib.as_array(specdif) # use ctypeslib
107107
_specdir = np.ctypeslib.as_array(specdir)
108108
_specetr = np.ctypeslib.as_array(specetr)
109109
_specglo = np.ctypeslib.as_array(specglo)
110110
_specx = np.ctypeslib.as_array(specx)
111111
# test OUTPUT are within TOL
112-
delta_specdif = reldiff((_specdif, test_data['specdif']))
113-
assert np.all(delta_specdif < TOL)
114-
delta_specdir = reldiff((_specdir, test_data['specdir']))
115-
assert np.all(delta_specdir < TOL)
116-
delta_specetr = reldiff((_specetr, test_data['specetr']))
117-
assert np.all(delta_specetr < TOL)
118-
delta_specglo = reldiff((_specglo, test_data['specglo']))
119-
assert np.all(delta_specglo < TOL)
120-
delta_specx = reldiff((_specx, test_data['specx']))
121-
assert np.all(delta_specx < TOL)
112+
assert np.all(RELDIFF((_specdif, test_data['specdif'])) < TOL)
113+
assert np.all(RELDIFF((_specdir, test_data['specdir'])) < TOL)
114+
assert np.all(RELDIFF((_specetr, test_data['specetr'])) < TOL)
115+
assert np.all(RELDIFF((_specglo, test_data['specglo'])) < TOL)
116+
assert np.all(RELDIFF((_specx, test_data['specx'])) < TOL)
122117
# raise a SPECTRL2_Error - UNITS
123118
try:
124119
spectrl2(3, location, datetime, weather, orientation,
125-
atmosphericConditions, albedo)
120+
atmospheric_conditions, albedo)
126121
except SPECTRL2_Error as err:
127122
assert err.args[0] == -1
128123
# raise a SPECTRL2_Error - TAU500
129124
try:
130125
spectrl2(units, location, datetime, weather, orientation,
131-
[-1.0] * 5, albedo)
126+
[-1.0] * 5, albedo)
132127
except SPECTRL2_Error as err:
133128
assert err.args[0] == -2
134129
# now raise a SOLPOS_Error - YEAR
135130
datetime = [2051, 6, 5, 12, 31, 0]
136131
try:
137132
spectrl2(units, location, datetime, weather, orientation,
138-
atmosphericConditions, albedo)
133+
atmospheric_conditions, albedo)
139134
except SOLPOS_Error as err:
140135
assert err.args[0] == 'S_YEAR_ERROR'
141136

0 commit comments

Comments
 (0)