Cycles I/O and plotting tools

generate_control_file(file_path, user_dict, *, simulation_dict=None)

Generate and write a Cycles control file.

Provide either direct values or callables that accept a simulation configuration and return a value in user_dict. The parameter names should be in lowercase and correspond to the fields in Cycles simulation control files. If a field is not provided, it will be filled with a default value. If a field's value is a callable, it will be called with the simulation_dict to resolve its value.

The following fields are required in user_dict:

  • simulation_start_year
  • simulation_end_year
  • rotation_size
  • operation_file
  • soil_file
  • weather_file

The default values for other fields are:

  • crop_file: GenericCrops.crop
  • reinit_file: N/A
  • soil_layers: inferred from the soil file (if not provided)
  • co2_level: -999
  • use_reinitialization: 0
  • adjusted_yields: 0
  • hydrology_option: 1
  • automatic_nitrogen: 0
  • automatic_phosphorus: 0
  • automatic_sulfur: 0

All output control fields default to 0.

Parameters:
  • file_path (str | Path) –

    Destination control file path.

  • user_dict (dict[str, Any] | ControlConfig) –

    Values or callables for control fields.

  • simulation_dict (dict[str, Any] | None, default: None ) –

    Optional simulation row for callable resolution.

Returns:
  • ControlConfig –

    The generated control configuration.

read_control_file(file_path)

Parse a Cycles control file into a ControlConfig dataclass instance.

Parameters:
  • file_path (str | Path) –

    Path to a Cycles control file path.

Returns:
  • ControlConfig –

    Control configuration.

convert_obsolete_crop_files(crop_fns, *, planted_crops=None)

Convert obsolete crop files to the current Cycles crop-file format.

Parameters:
  • crop_fns (dict[str | Path, str | Path]) –

    Mapping from obsolete crop-file paths to destination paths.

  • planted_crops (set[str] | None, default: None ) –

    Optional set of crop names to include. If omitted, all crops in each source file are converted.

convert_obsolete_operation_files(operation_fns, crop_fn, input_dir)

Convert obsolete operation files and write their shared resources.

The conversion creates current-format operation files and writes the referenced tillage tools and fertilizers to input_dir.

Parameters:
  • operation_fns (dict[str, str]) –

    Mapping from obsolete operation-file paths to destination paths.

  • crop_fn (str | Path) –

    Path to the obsolete crop file referenced by the operations.

  • input_dir (str | Path) –

    Directory in which to write tillage_tools.txt and fertilizers.txt.

Returns:
  • set[str] –

    The names of crops referenced by planting operations.

convert_obsolete_soil_files(soil_fns, *, keep_profile=True)

Convert obsolete soil files to the current Cycles soil-file format.

Parameters:
  • soil_fns (dict) –

    Mapping from obsolete soil-file paths to destination paths.

  • keep_profile (bool, default: True ) –

    If true, preserve the source layer structure; otherwise, use the default Cycles soil profile.

read_crop_file(file_path)

Read a Cycles crop file into named Crop dataclass instances.

Parameters:
  • file_path (str | Path) –

    Path to a Cycles crop file.

Returns:
  • dict[str, Crop] –

    A dictionary mapping crop names to their structured crop definitions.

generate_crop_file(file_path, crops)

Write a Cycles crop file from user-provided values.

Parameters:
  • file_path (str | Path) –

    Destination crop file path.

  • crops (dict[str, Crop]) –

    Dictionary of crop names and their corresponding Crop dataclass instances.

generate_nudge_file(file_path, user_dict, *, calibration_dict=None)

Write a Cycles nudge file from user-provided values.

Provide either direct values or callables that accept a calibration configuration and return a value in user_dict. The parameter names should be in lowercase and correspond to the fields in Cycles nudge (calibration) files. If a field is not provided, it will be filled with a default value. If a field's value is a callable, it will be called with the calibration_dict to resolve its value.

The default values for all calibration multipliers are 1.0, and the default values for kd_no3 and kd_nh4 are 0.0 and 5.6, respectively.

Parameters:
  • file_path (str | Path) –

    Destination nudge file path.

  • user_dict (dict[str, Any]) –

    Values or callables for nudge parameters.

  • calibration_dict (dict[str, Any] | None, default: None ) –

    Optional simulation row for callable resolution.

read_operation_file(file_path)

Parse a Cycles operation file into operation objects.

Parameters:
  • file_path (str | Path) –

    Path to a Cycles operation file.

Returns:
  • list[Operation] –

    A list of operation dataclass instances.

generate_operation_file(file_path, operations, *, desc='')

Write a Cycles operation file from structured operation records.

The optional description is written as the first line when provided. Each operation is serialized with to preserve Cycles field naming and relative day-of-year formatting.

Parameters:
  • file_path (Path | str) –

    Destination operation file path.

  • operations (list[Operation]) –

    Ordered operation records to serialize.

  • desc (str, default: '' ) –

    Optional header or description line to place at the top of the file.

read_output(file_path, output_type)

Read one Cycles output file and associated unit strings.

Parameters:
  • file_path (str | Path) –

    Directory containing output CSV files.

  • output_type (str) –

    Output file type, such as harvest.

Returns:
  • tuple[DataFrame, dict[str, str]] –

    Parsed DataFrame and a column-to-unit mapping.

generate_soil_file(file_path, profile, *, layers=DEFAULT_PROFILE, parameters=MAPPABLE_PARAMETERS, soil_depth=None, desc='', slope=None, curve_number=None, hsg='')

Generate a Cycles-formatted soil file from a list of soil layers.

The input profile is first mapped to the target layering scheme, then rendered to Cycles soil-file format and written to file_path.

Parameters:
  • file_path (str | Path) –

    Output soil file path.

  • profile (list[SoilLayer]) –

    Soil profile layers in depth order.

  • layers (list[SoilLayer], default: DEFAULT_PROFILE ) –

    Target layer structure to map onto. The DEFAULT_PROFILE has 12 layers from 0-2 m depth.

  • parameters (list[str], default: MAPPABLE_PARAMETERS ) –

    Soil parameters to map from measured profile. If not provided, the minimum set of parameters required for Cycles simulation will be mapped, including: clay, sand, soc, bulk_density, coarse_fragments, pH.

  • soil_depth (float | None, default: None ) –

    Optional maximum depth (m) to include in mapping.

  • desc (str, default: '' ) –

    Optional description/comment line written at file top.

  • slope (float | None, default: None ) –

    Optional slope value written to the header.

  • curve_number (float | None, default: None ) –

    Optional explicit curve number value.

  • hsg (str, default: '' ) –

    Optional hydrologic soil group used to infer curve number.

Returns:
  • list[SoilLayer] –

    The mapped soil layers written to the output file.

Raises:
  • ValueError –

    If both curve_number and hsg are provided.

read_soil_file(file_path)

Read a Cycles soil file into structured objects.

Parameters:
  • file_path (str | Path) –

    Soil file path to read.

Returns:
  • tuple[list[SoilLayer], dict] –

    A tuple containing: - List of parsed SoilLayer objects. - Header metadata dictionary with keys such as curve_number and slope.

read_weather_file(file_path, *, start_year=-9999, end_year=9999, subdaily=False)

Read weather file records and filter by year range.

Parameters:
  • file_path (str | Path) –

    Weather file path.

  • start_year (int, default: -9999 ) –

    Inclusive first year to keep.

  • end_year (int, default: 9999 ) –

    Inclusive last year to keep.

  • subdaily (bool, default: False ) –

    If True, parse hourly weather schema.

Returns:
  • DataFrame –

    Weather data indexed by datetime.

generate_reinit_file(out_path, in_path, doy)

Write a re-initialization file from Cycles output for all years at a selected day-of-year.

Parameters:
  • out_path (str | Path) –

    Destination re-initialization file path.

  • in_path (str | Path) –

    Simulation output directory that contains Cycles output files.

  • doy (int) –

    Day-of-year extracted from re-initialization.