Skip to content

Commit

Permalink
fix(postprocess): Add command to postprocess radiation
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswmackey committed Jul 7, 2021
1 parent 1f6204a commit fb1a873
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 23 deletions.
44 changes: 44 additions & 0 deletions honeybee_radiance/cli/postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,50 @@ def average_matrix_rows(input_matrix, output):
input_file.close()


@post_process.command('cumulative-radiation')
@click.argument(
'average-irradiance', type=click.Path(exists=True, file_okay=True, resolve_path=True)
)
@click.argument(
'wea', type=click.Path(exists=True, file_okay=True, resolve_path=True)
)
@click.option(
'--timestep', type=int, default=1, help='The timestep of the Wea file, which '
'is used to to compute cumulative radiation over the time period of the Wea.'
)
@click.option(
'--output', '-o', help='Optional path to output file to output the name of the newly'
' created matrix. By default the list will be printed out to stdout',
type=click.File('w'), default='-')
def cumulative_radiation(average_irradiance, wea, timestep, output):
"""Postprocess average irradiance (W/m2) into cumulative radiation (kWh/m2).
\b
Args:
average_irradiance: A single-column matrix of average irradiance values.
This input matrix must be in ASCII format.
wea: The .wea file that was used in the irradiance simulation. This
will be used to determine the duration of the analysis for computing
cumulative radiation.
"""
try:
# parse the Wea and the average_irradiance matrix
conversion = Wea.count_timesteps(wea) / (timestep * 1000)
first_line, input_file = remove_header(average_irradiance)
# calculate the value for the first line
output.write('%s\n' % (float(first_line) * conversion))
# write rest of the lines
for line in input_file:
output.write('%s\n' % (float(line) * conversion))
except Exception:
_logger.exception('Failed to compute cumulative radiation.')
sys.exit(1)
else:
sys.exit(0)
finally:
input_file.close()


@post_process.command('annual-irradiance')
@click.argument(
'folder',
Expand Down
41 changes: 19 additions & 22 deletions honeybee_radiance/cli/sunpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def sunpath():
'--timestep', default=1, type=int, show_default=True,
help='An optional integer to set the number of time steps per hour. Default is 1'
' for one value per hour.')
@click.option('--leap-year', is_flag=True, help='Dates are for a leap year.')
@click.option('--leap-year', is_flag=True, help='Flag for whether to use a leap year.')
@click.option('--folder', default='.', help='Output folder.')
@click.option('--name', default='sunpath', help='File name.')
@click.option(
Expand Down Expand Up @@ -108,7 +108,12 @@ def sunpath_from_location(
@click.option(
'--north', default=0, type=float, show_default=True,
help='Angle to north (0-360). 90 is west and 270 is east')
@click.option('--leap-year', is_flag=True, help='dates are for a leap year.')
@click.option(
'--timestep', default=1, type=int, show_default=True,
help='An integer to set the number of time steps per hour in the wea. Default is 1'
' for one value per hour.')
@click.option(
'--leap-year', is_flag=True, help='Flag for whether input wea is for a leap year.')
@click.option('--folder', default='.', help='Output folder.')
@click.option('--name', default='sunpath', help='File name.')
@click.option(
Expand All @@ -118,7 +123,8 @@ def sunpath_from_location(
@click.option(
'--reverse-vectors', is_flag=True,
help='Reverse sun vectors to go from ground to sky.')
def sunpath_from_wea(wea, north, folder, name, log_file, leap_year, reverse_vectors):
def sunpath_from_wea(wea, north, folder, name, log_file, timestep, leap_year,
reverse_vectors):
"""Generate a climate-based sunpath from a Wea file.
This command also generates a mod file which includes all the modifiers in sunpath.
Expand All @@ -130,7 +136,7 @@ def sunpath_from_wea(wea, north, folder, name, log_file, leap_year, reverse_vect
wea: Path to a wea file.
"""
try:
wea = Wea.from_file(wea)
wea = Wea.from_file(wea, timestep=timestep, is_leap_year=leap_year)
sp = Sunpath(wea.location, north)
hoys = wea.hoys
sp_files = sp.to_file(
Expand Down Expand Up @@ -240,7 +246,8 @@ def sunpath_from_wea_rad(wea, north, folder, name, visible, log_file, dry_run):
'--timestep', default=1, type=int, show_default=True,
help='An optional integer to set the number of time steps per hour. Default is 1'
' for one value per hour.')
@click.option('--leap-year', is_flag=True, help='dates are for a leap year.')
@click.option(
'--leap-year', is_flag=True, help='Flag for whether input epw is for a leap year.')
@click.option('--folder', default='.', help='Output folder.')
@click.option('--name', default='sunpath', help='File name.', type=str)
@click.option(
Expand All @@ -252,7 +259,7 @@ def sunpath_from_wea_rad(wea, north, folder, name, visible, log_file, dry_run):
help='Reverse sun vectors to go from ground to sky.')
def sunpath_from_epw(
epw, north, folder, name, log_file, start_date, start_time, end_date, end_time,
timestep, leap_year, reverse_vectors):
timestep, leap_year, reverse_vectors):
"""Generate a climate-based sunpath from an epw weather file.
This command also generates a mod file which includes all the modifiers in sunpath.
Expand Down Expand Up @@ -287,32 +294,22 @@ def sunpath_from_epw(

@sunpath.command('parse-hours')
@click.argument('suns', type=click.File(mode='r'))
@click.option(
'--leap-year/--full-year', is_flag=True, help='A flag to switch between a normal '
'year and a leap year. The default is a normal year.', default=False,
show_default=True
)
@click.option(
'--timestep', type=click.INT, default=1, show_default=True,
help='An integer value to set the timestep of the input hours. If timestep is set '
'to 1 the time will be offset by -0.5 to align with the start of the hour. For '
'other timesteps the hour will not be adjusted.'
)
@click.option('--timestep', default=1, type=int, show_default=True,
help='This input is not used and is deprecated.')
@click.option('--leap-year', is_flag=True,
help='This input is not used and is deprecated.')
@click.option('--folder', default='.', help='Output folder.')
@click.option('--name', default='hours.txt', help='Output file name.')
def parse_hours_from_suns(suns, folder, name, timestep, leap_year):
def parse_hours_from_suns(suns, timestep, leap_year, folder, name):
"""Parse hours of the year from a suns modifier file generated by Radiance's
gendaymtx.
suns: Path to a suns modifiers file.
"""
offset = -0.5 if timestep == 1 else 0
try:
hours = []
for line in suns:
hours.append(
DateTime.from_moy(int(line.split('solar')[1]), leap_year).hoy + offset
)
hours.append(int(line.split('solar')[1]) / 60)
# write the new file to hoys
with open(os.path.join(folder, name), 'w') as hf:
for h in hours:
Expand Down
2 changes: 1 addition & 1 deletion tests/cli/sunpath_cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,6 @@ def test_sunpath_hours():
]

with open(os.path.join(folder, name)) as inf:
values = [float(i) for i in inf]
values = [int(float(i)) for i in inf]

assert values == expected_results

0 comments on commit fb1a873

Please sign in to comment.