Repository: NREL/bifacial_radiance (language: python)

Issue:
WArning if Radiance path not correct/existing
Is there a way to test if Radiance paths / Raypath etc has been set correctly? So that instead of getting emails with "this doesn't work" when bifacial_radiance loads or something it ill warn the user




Discussion / hints:
Yes, do you mean to confirm that RADIANCE functions are correctly on the PATH?   I was thinking about putting a check in the makeOct function (which is the first time we really require RADIANCE executables) to check that RAYPATH is set correctly.  Maybe just do a quick trial dummy test using XFORM or something small to make sure that it's behaving as expected.  Then give a useful warning like 'Hey, you need to set up RADIANCE correctly' if it fails?

Fix patch:
diff --git a/bifacial_radiance/main.py b/bifacial_radiance/main.py
index 4088d6b6..20a5be82 100644
--- a/bifacial_radiance/main.py
+++ b/bifacial_radiance/main.py
@@ -302,7 +302,24 @@ def _subhourlydatatoGencumskyformat(gencumskydata, label='right'):
     if (gencumskydata.index.year[-1] == gencumskydata.index.year[-2]+1) and len(gencumskydata)>8760:
         gencumskydata = gencumskydata[:-1]
     return gencumskydata
-    # end _subhourlydatatoGencumskyformat        
+    # end _subhourlydatatoGencumskyformat   
+
+def _checkRaypath():
+    # Ensure that os.environ['RAYPATH'] exists and contains current directory '.'     
+    if os.name == 'nt':
+        splitter = ';'
+    else:
+        splitter = ':'
+    try:
+        raypath = os.getenv('RAYPATH', default=None)
+        if not raypath:
+            raise KeyError()
+        raysplit = raypath.split(splitter)
+        if not '.' in raysplit:
+            os.environ['RAYPATH'] = splitter.join(filter(None, raysplit + ['.'+splitter]))
+    except (KeyError, AttributeError, TypeError):
+        raise Exception('No RAYPATH set for RADIANCE.  Please check your RADIANCE installation.')
+        
     
 
 class RadianceObj:
@@ -361,6 +378,7 @@ def __init__(self, name=None, path=None, hpc=False):
         
         now = datetime.datetime.now()
         self.nowstr = str(now.date())+'_'+str(now.hour)+str(now.minute)+str(now.second)
+        _checkRaypath()       # make sure we have RADIANCE path set up correctly
 
         # DEFAULTS
 
diff --git a/docs/sphinx/source/whatsnew/pending.rst b/docs/sphinx/source/whatsnew/pending.rst
new file mode 100644
index 00000000..a086decc
--- /dev/null
+++ b/docs/sphinx/source/whatsnew/pending.rst
@@ -0,0 +1,28 @@
+.. _whatsnew_0440:
+
+v0.4.4 (XX / XX / 2024)
+------------------------
+Bugfix Release  ...
+
+
+API Changes
+~~~~~~~~~~~~
+* 
+
+Enhancements
+~~~~~~~~~~~~
+* Conduct an automated check for proper radiance RAYPATH setting (:issue:`525`)
+
+
+Bug fixes
+~~~~~~~~~
+* 
+
+Documentation
+~~~~~~~~~~~~~~
+* 
+
+Contributors
+~~~~~~~~~~~~
+* Silvana Ayala (:ghuser:`shirubana`)
+* Chris Deline (:ghuser:`cdeline`)


Test patch:
diff --git a/tests/test_bifacial_radiance.py b/tests/test_bifacial_radiance.py
index 7df11da3..2bad7492 100644
--- a/tests/test_bifacial_radiance.py
+++ b/tests/test_bifacial_radiance.py
@@ -575,4 +575,18 @@ def test_customTrackerAngles():
     trackerdict = demo.set1axis(azimuth=90, useMeasuredTrackerAngle=True)
     assert trackerdict[-20]['count'] == 3440
     trackerdict = demo.set1axis(azimuth=90, useMeasuredTrackerAngle=False)
-    assert trackerdict[-20]['count'] == 37
\ No newline at end of file
+    assert trackerdict[-20]['count'] == 37
+    
+def test_raypath():
+    # test errors and raypath updates
+    import re
+    raypath0 = os.getenv('RAYPATH', default=None)
+    
+    os.environ['RAYPATH'] = ''
+    with pytest.raises(Exception):
+        bifacial_radiance.main._checkRaypath()
+    os.environ['RAYPATH'] = 'test'
+    bifacial_radiance.main._checkRaypath()
+    assert '.' in re.split(':|;', os.environ['RAYPATH'])
+    
+    os.environ['RAYPATH'] = raypath0    
\ No newline at end of file