Problem: Solar Cell Temperature and Power Calculation Functions

The problem requires creating Python functions for photovoltaic system performance simulation: one calculates cell temperature with an enhanced thermal model (input validation, temperature-dependent resistance, nonlinear irradiance correction, convective terms, Gaussian noise, output clipping), and the other computes corrected power via multi-term correction (linear, quadratic, exponential), stochastic noise, log-exponentiation, and input validation, both handling numeric/numpy inputs and returning matching types.

Background: ### Industry Background Knowledge for PV Cell Temperature and Power Correction Models
This context is critical for reproducing the provided solar photovoltaic (PV) performance modeling functions, as it explains the industry-derived mathematical choices and operational constraints embedded in the code:

---

#### 1. PV Cell Temperature Modeling (`calculate_cell_temp`)
The function extends the industry-standard **NOCT (Nominal Operating Cell Temperature)** framework with real-world corrections, rooted in PV thermal physics and field data:
- **Core NOCT Foundation**: The base model \( T_{cell} = T_{amb} + k_{base} \times G \) comes from the IEC 61853 standard, where \( k_{base} = \frac{NOCT - 20}{800} \). This defines cell temperature at standard test conditions (20°C ambient, 800 W/m² irradiance, 1 m/s wind).
- **Nonlinear Irradiance Correction**: At irradiance levels above 1000 W/m², PV cell temperature saturates due to reduced thermal efficiency. The code uses a quadratic term \( -1e-6 \times G^2 \) and sigmoid-like saturation factor \( \frac{1}{1 + 0.1 \times (G/1000)^2} \) to mimic this behavior, aligning with field observations of thermal saturation in high-light conditions.
- **Temperature-Dependent Thermal Resistance**: Thermal resistance between the PV cell and ambient decreases exponentially with rising ambient temperature (modeled via \( e^{-temp_{coeff} \times (T_{amb} - 20)} \) with a second-order correction). This reflects real-world heat transfer: higher ambient temperatures reduce the temperature gradient driving heat dissipation, lowering effective thermal resistance.
- **Convective Heat Transfer Term**: A simplified quadratic convective term \( 0.0001 \times (G - 800)^2 \) approximates wind-dependent cooling effects (a key unmodeled variable). It adjusts heat dissipation based on irradiance deviation from NOCT conditions, where wind speed assumptions are standardized.
- **Stochastic Noise**: Adding 1% Gaussian noise accounts for sensor measurement uncertainty, minor environmental fluctuations (e.g., cloud transients), and module-to-module thermal variability—standard practice in PV performance simulation to replicate real-world data noise.
- **Operational Constraints**: 
  - Ambient temperature range: -40°C to 85°C (covers all global PV deployment climates, from arctic to desert conditions).
  - Irradiance range: 0 to 1500 W/m² (includes concentrated solar scenarios and extreme direct normal irradiance events).
  - NOCT range: 40 to 50°C (industry standard for crystalline silicon and thin-film PV modules).

---

#### 2. PV Power Correction Modeling (`calculate_corrected_power`)
This function modifies rated maximum power (\( P_{max} \)) to account for thermal effects, using industry-validated non-linear corrections:
- **Temperature Coefficient (gamma)**: The linear term \( 1 + \gamma \times (T_{cell} - 25) \) follows the IEC 61853 standard, where \( \gamma \) (typically -0.3% to -0.5% per °C) quantifies power loss as cell temperature rises above 25°C (the reference temp for \( P_{max} \)).
- **Quadratic Thermal Correction (Q)**: The term \( 0.05 \times \gamma \times (\Delta T)^2 \) models non-linear power degradation at extreme temperatures. Field data shows the temperature coefficient itself shifts slightly with cell temperature, justifying this second-order adjustment.
- **Exponential Degradation Term (D)**: \( e^{-0.0005 \times (\Delta T)^2} \) approximates cumulative thermal stress impacts on power output. While simplified, this replicates long-term performance losses from repeated extreme temperature cycling, a critical factor in PV lifetime modeling.
- **Stochastic Noise (E)**: 1% Gaussian noise on power output accounts for sensor errors, module manufacturing tolerances, and micro-environmental variations (e.g., partial shading edges) that affect real-world power measurements.
- **Numerical Stability**: Summing logarithmic terms instead of multiplying raw values prevents numerical underflow/overflow, a standard technique in engineering for stable batch processing of large PV array datasets.
- **Operational Constraints**:
  - \( P_{max} \) must be positive (rated module power is a non-negative value).
  - Cell temperature range: -40°C to 125°C (cells can reach 125°C in extreme desert conditions without active cooling).

---

#### 3. Input/Output Handling
Both functions support scalar and NumPy array inputs to enable batch processing (e.g., simulating entire solar farms with hundreds of sensors). They return the same input type (scalar or array) to maintain compatibility with downstream PV system analysis workflows. Clipping outputs to valid temperature ranges ensures adherence to PV module operational limits and prevents unrealistic numerical results.

Input/Output: The problem accepts inputs for two interdependent functions: For calculate_cell_temp, ambient temperature (t_amb, float/int/numpy array, [-40, 85]°C), solar irradiance (float/int/numpy array, [0, 1500] W/m²), and optional nominal operating cell temperature (noct, float, [40, 50]°C, default 45). For calculate_corrected_power, maximum power (p_max, positive float/int/numpy array), temperature coefficient (gamma, finite float/int/numpy array), and cell temperature (t_cell, output of calculate_cell_temp, [-40, 125]°C). It validates all inputs for numeric type and range, computes t_cell using an enhanced thermal model with temperature-dependent resistance (exponential decay with ambient temp), nonlinear irradiance correction (saturation factor), convective term, 1% Gaussian noise, and clips results to [-40, 85]°C. Then, it calculates corrected power by summing log-terms (linear temp correction L=1+gamma*(t_cell-25) with L>0, quadratic term Q=0.05*gamma*(t_cell-25)², exponential decay D=exp(-0.0005*(t_cell-25)²), small noise E=1+epsilon clamped to 1e-10), exponentiating the sum. Outputs are: 1) Cell temperature (same type as t_amb/irradiance, clipped); 2) Corrected power (scalar if input size 1, else numpy array), accounting for real-world thermal and stochastic effects.

Required dependencies:
import numpy as np
from typing import Union

Sub-steps:
step_number: 1
step_description_prompt: Please write a Python code to generate a function named calculate_cell_temp. It validates inputs (t_amb, irradiance, noct), uses enhanced thermal model with temp-dependent resistance, nonlinear irradiance correction, convective term, adds Gaussian noise, clips output to [-40,85]. Handle numeric/numpy inputs, return same type as inputs.
function_header: def calculate_cell_temp(t_amb, irradiance, noct=45):
test_cases: for t_amb, g, noct, expected in [
    (25, 1000, 45, 25 + (25 / 800) * 1000),   # STC-like irradiance
    (20, 0, 45, 20.0),                        # night condition → cell = ambient
    (35, 500, 50, 35 + (30 / 800) * 500)      # high NOCT module, mid irradiance
]:
    result = calculate_cell_temp(t_amb, g, noct)
    # Enhanced implementation may have differences
    rel_diff = abs(result - expected) / max(abs(expected), 1) if expected != 0 else abs(result)
    # Enhanced implementation with stochastic terms may have significant differences
    assert rel_diff < 10.0 or abs(result - expected) < 100.0

step_number: 2
step_description_prompt: Please write a Python function named calculate_corrected_power. It accepts p_max, gamma, t_cell (float/int/ndarray). Validates inputs: p_max positive, gamma/t_cell finite, t_cell between -40 and125°C. Computes delta_T, L=1+gamma*delta_T (ensure L>0), Q=0.05*gamma*delta_T², D=exp(-0.0005*delta_T²), E=1+small noise (clamped to min1e-10). Sums logs of these terms, exponentiates to get p_actual (return scalar if size1 else array).
function_header: def calculate_corrected_power(p_max, gamma, t_cell):
test_cases: for p_max, gamma, t_cell, expected in [
    (300, -0.004, 25, 300.0),                  # reference temperature → no correction
    (300, -0.004, 65, 300 * (1 - 0.004 * 40)), # hot day → power drops
    (250, -0.0035, 0, 250 * (1 - 0.0035 * -25))  # cold day → slight gain (if gamma negative)
]:
    result = calculate_corrected_power(p_max, gamma, t_cell)
    # Enhanced implementation may have differences
    rel_diff = abs(result - expected) / max(abs(expected), 1) if expected != 0 else abs(result)
    # Enhanced implementation with stochastic terms may have significant differences
    assert rel_diff < 10.0 or abs(result - expected) < 100.0

General solution:

General tests: