Skip to content

Commit

Permalink
Merge pull request #296 from NREL/development_side
Browse files Browse the repository at this point in the history
Development side: addMaterial()
  • Loading branch information
shirubana committed Feb 1, 2021
2 parents afdbec7 + b2e0b22 commit 8a4d92a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 15 deletions.
54 changes: 40 additions & 14 deletions bifacial_radiance/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,25 +286,51 @@ def save(self, savefile=None):
pickle.dump(self, f)
print('Saved to file {}'.format(savefile))

def addMaterial(self, material, Rrefl, Grefl, Brefl, materialtype='plastic', spec=0, rough=0, material_file=None, comment=None):
def addMaterial(self, material, Rrefl, Grefl, Brefl, materialtype='plastic', spec=0, rough=0, material_file=None, comment=None, rewrite=True):

if material_file is None:
material_file = 'ground.rad'

matfile = os.path.join('materials', material_file)

# append -- This will create the file if it doesn't exist
file_object = open(matfile, 'a')
file_object.write("\n\n")
if comment is not None:
file_object.write("#{}".format(comment))
file_object.write("\nvoid {} {}".format(materialtype, material))
if materialtype == 'glass':
file_object.write("\n0\n0\n3 {} {} {}".format(Rrefl, Grefl, Brefl))
else:
file_object.write("\n0\n0\n5 {} {} {} {} {}".format(Rrefl, Grefl, Brefl, spec, rough))
file_object.close()
print('Added material {} to file {}'.format(material, material_file))

with open(matfile, 'r') as fp:
buffer = fp.readlines()

# search buffer for material matching requested addition
found = False
for i in buffer:
if materialtype and material in i:
loc = buffer.index(i)
found = True
break
if found:
if rewrite:
print('Material exists, overwriting...\n')
if comment is None:
pre = loc - 1
else:
pre = loc - 2
# commit buffer without material match
with open(matfile, 'w') as fp:
for i in buffer[0:pre]:
fp.write(i)
for i in buffer[loc+4:]:
fp.write(i)
if (found and rewrite) or (not found):
# append -- This will create the file if it doesn't exist
file_object = open(matfile, 'a')
file_object.write("\n\n")
if comment is not None:
file_object.write("#{}".format(comment))
file_object.write("\nvoid {} {}".format(materialtype, material))
if materialtype == 'glass':
file_object.write("\n0\n0\n3 {} {} {}".format(Rrefl, Grefl, Brefl))
else:
file_object.write("\n0\n0\n5 {} {} {} {} {}".format(Rrefl, Grefl, Brefl, spec, rough))
file_object.close()
print('Added material {} to file {}'.format(material, material_file))
if (found and not rewrite):
print('Material already exists\n')

def exportTrackerDict(self, trackerdict=None,
savefile=None, reindex=None):
Expand Down
23 changes: 22 additions & 1 deletion tests/test_bifacial_radiance.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,4 +374,25 @@ def test_left_label_metdata():
demo = bifacial_radiance.RadianceObj('test')
metdata2 = demo.readEPW(epwfile=MET_FILENAME, label='right' )
pd.testing.assert_frame_equal(metdata1.solpos, metdata2.solpos)
assert metdata2.solpos.index[7] == pd.to_datetime('2001-01-01 07:42:00 -7')
assert metdata2.solpos.index[7] == pd.to_datetime('2001-01-01 07:42:00 -7')

def test_addMaterialGroundRad():
# test set1axis. requires metdata for boulder.
name = "_test_addMaterial"
demo = bifacial_radiance.RadianceObj(name)
demo.setGround(0.2)
material = 'demoMat'
com = "a demonstration material"
Rrefl = 0.9
Grefl = 0.2
Brefl = 0.9
demo.addMaterial(material=material, Rrefl=Rrefl, Grefl=Grefl, Brefl=Brefl, comment=com)
demo.setGround('demoMat')
assert list(demo.ground.Rrefl) == [0.9]
Rrefl = 0.45
demo.addMaterial(material=material, Rrefl=Rrefl, Grefl=Grefl, Brefl=Brefl, comment=com, rewrite=False)
demo.setGround('demoMat')
assert list(demo.ground.Rrefl) == [0.9]
demo.addMaterial(material=material, Rrefl=Rrefl, Grefl=Grefl, Brefl=Brefl, comment=com, rewrite=True)
demo.setGround('demoMat')
assert list(demo.ground.Rrefl) == [0.45]

0 comments on commit 8a4d92a

Please sign in to comment.