Source code for pyiecwind.template

"""OpenFAST-style input formatting and template generation."""

from __future__ import annotations

from pathlib import Path

from .models import CASE_ROW_COMMENTS, CASE_TYPE_ORDER, DEFAULT_TEMPLATE_FILENAME, IECParameters
from .parsing import _group_conditions_by_type

__all__ = ["format_openfast_input", "default_template_text", "write_template"]


[docs] def format_openfast_input(params: IECParameters) -> str: """Render parameters as an OpenFAST-style input file. The output round-trips: feeding it back through :func:`~pyiecwind.parse_input_file` reproduces the same conditions and scalars. Parameters ---------- params : IECParameters The parameters to render. Length and speed fields are shown in the parameters' own unit system (``si_unit``). Returns ------- str The formatted input-file text, including the grouped ``Cases`` section. """ lc = params.len_convert value_width = 14 key_width = 12 def row(value: str, key: str, comment: str) -> str: return f"{value:<{value_width}} {key:<{key_width}} - {comment}" def section(title: str) -> list[str]: return [f"! {title}", f"! {'-' * len(title)}"] def case_row(case_type: str, enabled: str, options: str, comment: str) -> str: return f"{case_type:<14} {enabled:<5} {options:<24} - {comment}" grouped_conditions = _group_conditions_by_type(params.conditions) rows = [ "! ------- pyIECWind Input File -----------------------------------------------", "! Generated by pyIECWind in an OpenFAST-style table layout.", "! format: openfast-table-v1", "! Supported IEC standard scope: IEC 61400-1 Edition 1 and Edition 3 options", "! Each active data row follows: value key - comment", "", *section("General"), "", row(str(params.si_unit), "si_unit", "True for SI (m, m/s), False for English (ft, ft/s)"), row(f"{params.t1:.3f}", "t1", "transient start time [s]"), row(f"{params.wtc:d}", "wtc", "wind turbine class (1, 2, or 3)"), row(params.catg.upper(), "catg", "turbulence category (A, B, or C)"), row(f"{params.slope_deg:.3f}", "slope_deg", "inflow inclination angle [deg]"), row(f"{params.iec_edition:d}", "iec_edition", "IEC 61400-1 edition for alpha (1 or 3)"), "", *section("Turbine"), "", row(f"{params.hh * lc:.3f}", "hh", f"hub height [{params.len_unit}]"), row(f"{params.dia * lc:.3f}", "dia", f"rotor diameter [{params.len_unit}]"), "", *section("Operating Speeds"), "", row(f"{params.vin * lc:.3f}", "vin", f"cut-in wind speed [{params.spd_unit}]"), row(f"{params.vrated * lc:.3f}", "vrated", f"rated wind speed [{params.spd_unit}]"), row(f"{params.vout * lc:.3f}", "vout", f"cut-out wind speed [{params.spd_unit}]"), "", *section("Cases"), "", "! Format: case_type use_case options_array - allowed options and notes", "! ECD: Extreme Coherent Gust with Direction Change", "! EWS: Extreme Wind Shear", "! EOG: Extreme Operating Gust", "! EDC: Extreme Direction Change", "! NWP: Normal Wind Profile", "! EWM: Extreme Wind Model", "! Set use_case to True to generate the listed options, False to skip the row, or None as a placeholder.", "! For NWP, the listed speeds are always interpreted in m/s.", "", ] for case_type in CASE_TYPE_ORDER: options = grouped_conditions[case_type] if options: options_text = "[" + ", ".join(options) + "]" enabled = "True" elif case_type == "EWM": options_text = "[50]" enabled = "False" elif case_type == "NWP": options_text = "[10.0]" enabled = "False" else: options_text = "[None]" enabled = "False" rows.append(case_row(case_type, enabled, options_text, CASE_ROW_COMMENTS[case_type])) return "\n".join(rows) + "\n"
def default_template_text() -> str: """Return the text of a commented example input file with sensible defaults.""" return format_openfast_input( IECParameters( si_unit=True, t1=40.0, wtc=2, catg="B", slope_deg=0.0, iec_edition=3, hh=80.0, dia=80.0, vin=4.0, vrated=10.0, vout=24.0, conditions=( "ECD+R", "EWSV+12.0", "EOGR+2.0", "EDC+R", "NWP23.7", "EWM50", ), ) )
[docs] def write_template(dest: str | Path = DEFAULT_TEMPLATE_FILENAME) -> Path: """Write a commented example input file to ``dest``. Parameters ---------- dest : str or pathlib.Path, optional Destination path. Defaults to ``pyiecwind_template.ipt``. Returns ------- pathlib.Path The path that was written. """ path = Path(dest) path.write_text(default_template_text(), encoding="utf-8") return path