Skip to content

Commit

Permalink
Set plotting resolution from workflow config
Browse files Browse the repository at this point in the history
  • Loading branch information
jfrost-mo committed Aug 20, 2024
1 parent 531cf4c commit de3f283
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 13 deletions.
2 changes: 2 additions & 0 deletions cset-workflow/app/run_cset_recipe/bin/run-cset-recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def parallel():
f"--input-dir={data_directory()}",
f"--output-dir={output_directory()}",
f"--style-file={os.getenv('COLORBAR_FILE', '')}",
f"--plot-resolution={os.getenv('PLOT_RESOLUTION', '')}",
"--parallel-only",
),
check=True,
Expand Down Expand Up @@ -188,6 +189,7 @@ def collate():
f"--recipe={recipe_file()}",
f"--output-dir={output_directory()}",
f"--style-file={os.getenv('COLORBAR_FILE', '')}",
f"--plot-resolution={os.getenv('PLOT_RESOLUTION', '')}",
"--collate-only",
),
check=True,
Expand Down
10 changes: 9 additions & 1 deletion src/CSET/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ def main():
parser_bake.add_argument(
"-s", "--style-file", type=Path, help="colour bar definition to use"
)
parser_bake.add_argument(
"--plot-resolution", type=int, help="plotting resolution in dpi"
)
parser_bake.set_defaults(func=_bake_command)

parser_graph = subparsers.add_parser("graph", help="visualise a recipe file")
Expand Down Expand Up @@ -207,10 +210,15 @@ def _bake_command(args, unparsed_args):
args.output_dir,
recipe_variables,
args.style_file,
args.plot_resolution,
)
if not args.parallel_only:
execute_recipe_collate(
args.recipe, args.output_dir, recipe_variables, args.style_file
args.recipe,
args.output_dir,
recipe_variables,
args.style_file,
args.plot_resolution,
)


Expand Down
29 changes: 25 additions & 4 deletions src/CSET/operators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,14 @@ def _step_parser(step: dict, step_input: any) -> str:
return operator(**kwargs)


def _run_steps(recipe, steps, step_input, output_directory: Path, style_file: Path):
def _run_steps(
recipe,
steps,
step_input,
output_directory: Path,
style_file: Path,
plot_resolution: int,
) -> None:
"""Execute the steps in a recipe."""
original_working_directory = Path.cwd()
os.chdir(output_directory)
Expand All @@ -159,6 +166,8 @@ def _run_steps(recipe, steps, step_input, output_directory: Path, style_file: Pa
# Create metadata file used by some steps.
if style_file:
recipe["style_file_path"] = str(style_file)
if plot_resolution:
recipe["plot_resolution"] = plot_resolution
_write_metadata(recipe)
# Execute the recipe.
for step in steps:
Expand All @@ -174,6 +183,7 @@ def execute_recipe_parallel(
output_directory: Path,
recipe_variables: dict = None,
style_file: Path = None,
plot_resolution: int = None,
) -> None:
"""Parse and executes the parallel steps from a recipe file.
Expand All @@ -188,8 +198,12 @@ def execute_recipe_parallel(
input.
output_directory: Path
Pathlike indicating desired location of output.
recipe_variables: dict
recipe_variables: dict, optional
Dictionary of variables for the recipe.
style_file: Path, optional
Path to a style file.
plot_resolution: int, optional
Resolution of plots in dpi.
Raises
------
Expand All @@ -213,14 +227,15 @@ def execute_recipe_parallel(
logging.error("Output directory is a file. %s", output_directory)
raise err
steps = recipe["parallel"]
_run_steps(recipe, steps, step_input, output_directory, style_file)
_run_steps(recipe, steps, step_input, output_directory, style_file, plot_resolution)


def execute_recipe_collate(
recipe_yaml: Union[Path, str],
output_directory: Path,
recipe_variables: dict = None,
style_file: Path = None,
plot_resolution: int = None,
) -> None:
"""Parse and execute the collation steps from a recipe file.
Expand All @@ -234,6 +249,10 @@ def execute_recipe_collate(
Pathlike indicating desired location of output. Must already exist.
recipe_variables: dict
Dictionary of variables for the recipe.
style_file: Path, optional
Path to a style file.
plot_resolution: int, optional
Resolution of plots in dpi.
Raises
------
Expand All @@ -249,4 +268,6 @@ def execute_recipe_collate(
recipe = parse_recipe(recipe_yaml, recipe_variables)
# If collate doesn't exist treat it as having no steps.
steps = recipe.get("collate", [])
_run_steps(recipe, steps, output_directory, output_directory, style_file)
_run_steps(
recipe, steps, output_directory, output_directory, style_file, plot_resolution
)
24 changes: 16 additions & 8 deletions src/CSET/operators/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@
from CSET._common import get_recipe_metadata, render_file, slugify
from CSET.operators._utils import get_cube_yxcoordname, is_transect

#####################
# Runtime constants #
#####################

# Resolution of rasterised plots in pixels per inch.
PLOT_RESOLUTION = get_recipe_metadata().get("plot_resolution", 100)


############################
# Private helper functions #
############################
Expand Down Expand Up @@ -248,7 +256,7 @@ def _plot_and_save_contour_plot(
cbar.set_label(label=f"{cube.name()} ({cube.units})", size=20)

# Save plot.
fig.savefig(filename, bbox_inches="tight", dpi=100)
fig.savefig(filename, bbox_inches="tight", dpi=PLOT_RESOLUTION)
logging.info("Saved contour plot to %s", filename)
plt.close(fig)

Expand Down Expand Up @@ -311,7 +319,7 @@ def _plot_and_save_postage_stamp_contour_plot(
# Overall figure title.
fig.suptitle(title)

fig.savefig(filename, bbox_inches="tight", dpi=100)
fig.savefig(filename, bbox_inches="tight", dpi=PLOT_RESOLUTION)
logging.info("Saved contour postage stamp plot to %s", filename)
plt.close(fig)

Expand Down Expand Up @@ -347,7 +355,7 @@ def _plot_and_save_line_series(
ax.autoscale()

# Save plot.
fig.savefig(filename, bbox_inches="tight", dpi=100)
fig.savefig(filename, bbox_inches="tight", dpi=PLOT_RESOLUTION)
logging.info("Saved line plot to %s", filename)
plt.close(fig)

Expand Down Expand Up @@ -436,7 +444,7 @@ def _plot_and_save_vertical_line_series(
ax.autoscale()

# Save plot.
fig.savefig(filename, bbox_inches="tight", dpi=100)
fig.savefig(filename, bbox_inches="tight", dpi=PLOT_RESOLUTION)
logging.info("Saved line plot to %s", filename)
plt.close(fig)

Expand Down Expand Up @@ -492,7 +500,7 @@ def _plot_and_save_scatter_plot(
ax.autoscale()

# Save plot.
fig.savefig(filename, bbox_inches="tight", dpi=100)
fig.savefig(filename, bbox_inches="tight", dpi=PLOT_RESOLUTION)
logging.info("Saved scatter plot to %s", filename)
plt.close(fig)

Expand Down Expand Up @@ -551,7 +559,7 @@ def _plot_and_save_histogram_series(
)

# Save plot.
fig.savefig(filename, bbox_inches="tight", dpi=100)
fig.savefig(filename, bbox_inches="tight", dpi=PLOT_RESOLUTION)
logging.info("Saved line plot to %s", filename)
plt.close(fig)

Expand Down Expand Up @@ -620,7 +628,7 @@ def _plot_and_save_postage_stamp_histogram_series(
# Overall figure title.
fig.suptitle(title)

fig.savefig(filename, bbox_inches="tight", dpi=100)
fig.savefig(filename, bbox_inches="tight", dpi=PLOT_RESOLUTION)
logging.info("Saved histogram postage stamp plot to %s", filename)
plt.close(fig)

Expand Down Expand Up @@ -658,7 +666,7 @@ def _plot_and_save_postage_stamps_in_single_plot_histogram_series(
ax.legend()

# Save the figure to a file
plt.savefig(filename, bbox_inches="tight", dpi=100)
plt.savefig(filename, bbox_inches="tight", dpi=PLOT_RESOLUTION)

# Close the figure
plt.close(fig)
Expand Down

0 comments on commit de3f283

Please # to comment.