Source code for xgc_analysis.read_bp_file

import adios2.stream
import numpy as np

[docs] def ReadBPFile(filename, variables=None, step_range=None): """ Reads data from an ADIOS2 BP file using the Stream API (ADIOS2 v2.10+). Parameters ---------- filename : str Path to the ADIOS2 BP file. variables : list of str, optional List of variable names to read. If None, all variables will be read. step_range : tuple of int, optional Tuple (start, end) indicating the range of steps to read. If None, only the last step is read. Returns ------- data : dict Dictionary with structure: {step_idx: {variable_name: np.ndarray}}. """ data = {} steps = [] step_counter = 0 # First pass to determine total number of steps if step_range is not given if step_range is None: with adios2.stream.Stream(filename, "r") as s: for _ in s: step_counter += 1 step_range = (step_counter - 1, step_counter) # Actual data reading with adios2.stream.Stream(filename, "r") as s: for step in s: current_step = step.current_step() if current_step < step_range[0]: continue if current_step >= step_range[1]: break # Get available variables and filter if necessary available_vars = step.available_variables() vars_to_read = variables if variables is not None else list(available_vars.keys()) step_data = {} for var in vars_to_read: if var in available_vars: step_data[var] = step.read(var) else: print(f"Warning: Variable '{var}' not found in step {current_step}") data[current_step] = step_data return data