function AmbientTemperature($w, $ws, $METEO, $TIME)
{
    //Daily maximum and minimum temperatures
    if ($TIME['Ndays'] == 365) {
        //All year
        $TM = MonthlyAverageToAll($METEO['TMm']);
        $Tm = MonthlyAverageToAll($METEO['Tmm']);
    } elseif ($TIME['Ndays'] == 12) {
        //Only characteristic days
        $TM = $METEO['TMm'];
        $Tm = $METEO['Tmm'];
    }
    //Calculations
    for ($d = 0; $d < $TIME['Ndays']; $d++) {
        //Daily coefficients
        $a3[$d] = -pi() / ($ws[$d] + 2 * pi() - pi() / 6);
        $b3[$d] = -$a3[$d] * $ws[$d];
        $a4[$d] = pi() / ($ws[$d] - pi() / 6);
        $b4[$d] = -$a4[$d] * pi() / 6;
        $a5[$d] = pi() / (2 * pi() + $ws[$d] - pi() / 6);
        $b5[$d] = -(pi() + $a5[$d] * pi() / 6);
        for ($h = 0; $h < $TIME['Nsteps']; $h++) {
            if (-pi() < $w[$d][$h] && $w[$d][$h] < $ws[$d]) {
                $Ta[$d][$h] = $TM[$d] - ($TM[$d] - $Tm[$d]) / 2 * (1 + cos($a3[$d] * $w[$d][$h] + $b3[$d]));
            } elseif ($ws[$d] < $w[$d][$h] && $w[$d][$h] < pi() / 6) {
                $Ta[$d][$h] = $Tm[$d] + ($TM[$d] - $Tm[$d]) / 2 * (1 + cos($a4[$d] * $w[$d][$h] + $b4[$d]));
            } else {
                $Ta[$d][$h] = $TM[$d] - ($TM[$d] - $Tm[$d]) / 2 * (1 + cos($a5[$d] * $w[$d][$h] + $b5[$d]));
            }
        }
        //end FOR $h
    }
    //end FOREACH $d
    return $Ta;
}
function CloudyHorizontalIrradiances($Eo, $ws, $w, $SUNPOS, $AMI, $METEO, $TIME)
{
    //Model coefficients
    $trd0 = -0.015843;
    $trd1 = 0.030543;
    $trd2 = 0.0003797;
    $a00 = 0.26463;
    $a01 = -0.061581;
    $a02 = 0.0031408;
    $a10 = 2.0402;
    $a11 = 0.018945;
    $a12 = -0.011161;
    $a20 = -1.3025;
    $a21 = 0.039231;
    $a22 = 0.008507900000000001;
    // Solar variables
    $gammas = $SUNPOS['gammas'];
    $costetazs = $SUNPOS['costetazs'];
    // Daily Linke Turbidity
    $Tlkd = MonthlyAverageToAll($METEO['Tlk']);
    // Calculations
    foreach ($TIME['SimulationDays'] as $dm) {
        $d = $dm - 1;
        // Diffuse transmission function at zenith, Trd(d)
        $Trd[$d] = $trd0 + $trd1 * $Tlkd[$d] + $trd2 * pow($Tlkd[$d], 2);
        // A0 coefficient
        $A0[$d] = $a00 + $a01 * $Tlkd[$d] + $a02 * pow($Tlkd[$d], 2);
        if ($A0[$d] * $Trd[$d] < 0.002) {
            $A0[$d] = 0.002 / $Trd[$d];
        }
        // A1 coefficient
        $A1[$d] = $a10 + $a11 * $Tlkd[$d] + $a12 * pow($Tlkd[$d], 2);
        // A2 coefficient
        $A2[$d] = $a20 + $a21 * $Tlkd[$d] + $a22 * pow($Tlkd[$d], 2);
        for ($h = 0; $h < $TIME['Nsteps']; $h++) {
            if (abs($w[$d][$h]) >= abs($ws[$d])) {
                $G0[$d][$h] = 0;
                $B0[$d][$h] = 0;
                $D0[$d][$h] = 0;
            } else {
                // Rayleigh constant, deltaR
                $deltaR[$d][$h] = RayleighConstant($AMI[$d][$h]);
                // Beam horizontal clear irradiance
                //////////////////////////////////////////////////
                $B0[$d][$h] = 0;
                //(*)
                //////////////////////////////////////////////////
                // Diffuse angular function, Fd(h,d)
                $Fd[$d][$h] = $A0[$d] + $A1[$d] * sin($gammas[$d][$h]) + $A2[$d] * pow(sin($gammas[$d][$h]), 2);
                // Diffuse horizontal clear irradiance
                $D0[$d][$h] = 1367 * $Eo[$d] * $Trd[$d] * $Fd[$d][$h];
                // Global horizontal clear irradiance
                $G0[$d][$h] = $B0[$d][$h] + $D0[$d][$h];
            }
        }
    }
    return $HI = array('G0' => $G0, 'B0' => $B0, 'D0' => $D0);
}
function HorizontalIrradiances($SUNMOT, $SUNPOS, $AMI, $SITE, $METEO, $OPTIONS, $TIME)
{
    if ($METEO['Data'] == 1 || $METEO['Data'] == 3) {
        if ($METEO['Sky'] == 1 || $METEO['Sky'] == 3) {
            //Mean sky
            //Daily Extraterrestial Horizontal Irradiation, Wh/m2
            $BOd0 = ExtraHorIrradiation($SUNMOT['Eo'], $SUNMOT['ws'], $SUNMOT['delta'], $SITE, $TIME);
            //Daily Global Horizontal Irradiation, Wh/m2
            if ($TIME['Ndays'] == 365) {
                //All year
                $Gd0 = MonthlyAverageToAll($METEO['Gdm0']);
            } elseif ($TIME['Ndays'] == 12) {
                //Only characteristic days
                $Gd0 = $METEO['Gdm0'];
            }
            //Daily Clearness Index
            $KTd = DailyClearnessIndex($Gd0, $BOd0, $TIME);
            //Daily Diffuse fraction
            $KDd = DailyDiffuseFraction($KTd, $SUNMOT['ws'], $OPTIONS, $TIME);
            //Daily horizontal irradiations, Wh/m2
            $DHI = DailyHorizontalIrradiations($Gd0, $KDd, $TIME);
            //Horizontal irradiances, W/m2
            $HI = MeanHorizontalIrradiances($SUNMOT['w'], $SUNMOT['ws'], $DHI, $TIME);
            if ($METEO['Sky'] == 3) {
                //Anulates the beam component
                $HI['B0'] = array_fill(0, $TIME['Ndays'], array_fill(0, $TIME['Nsteps'], 0));
                //And global matches the diffuse
                $HI['G0'] = $HI['D0'];
            }
        } elseif ($METEO['Sky'] == 2) {
            //Clear sky
            $HI = ClearHorizontalIrradiances($SUNMOT['Eo'], $SUNMOT['ws'], $SUNMOT['w'], $SUNPOS, $AMI, $METEO, $TIME);
        }
        //elseif ($METEO['Sky'] == 3)
        //{
        //Cloudy sky
        // 	$HI=CloudyHorizontalIrradiances($SUNMOT['Eo'], $SUNMOT['ws'], $SUNMOT['w'], $SUNPOS, $AMI, $METEO, $TIME);
        // }
    } else {
        $HI['G0'] = $METEO['G0'];
        $HI['B0'] = $METEO['B0'];
        $HI['D0'] = $METEO['D0'];
    }
    return $HI;
}