Skip to content

Commit

Permalink
Fix #295 overwrite existing ground material
Browse files Browse the repository at this point in the history
RadianceObj.addMaterial() now overwrites existing material with new instance based on matching name and type.
If previous instance preferred, set parameter
>>> rewrite=False
  • Loading branch information
mcbrown042 committed Jan 8, 2021
1 parent 7ec0fd0 commit b563a0a
Showing 1 changed file with 50 additions and 14 deletions.
64 changes: 50 additions & 14 deletions bifacial_radiance/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,25 +286,61 @@ 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))

'''
found = reWriteMaterial(matfile, materialtype, material, comment, rewrite)
if (found and rewrite) or (not found):
# append --
'''
file = open(matfile, 'r', newline='')
buffer = []
for line in file:
buffer.append(line)
file.close()

# 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
# -- CHANGE --
# figure out newline error
with open(matfile, 'w', newline='') as ict:
for i in buffer[0:pre]:
ict.write(i)
for i in buffer[loc+4:]:
ict.write(str(i)+'\n')
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

0 comments on commit b563a0a

Please sign in to comment.