%% cleaning to start 
clear all
clc
%% Import the data
[~, ~, raw] = xlsread('C:\Users\imbaq\Downloads\Data\Building_2_power.xlsx','Hoja1','A2:B5209');

%% Create output variable
data = reshape([raw{:}],size(raw));

%% Allocate imported array to column variable names
P1W = data(:,1);
GHIWm21 = data(:,2);

%% Clear temporary variables
clearvars data raw;

%% Import the data
[~, ~, raw] = xlsread('C:\Users\imbaq\Downloads\Data\GHI_to_extrapolated.xlsx','366dias','A2:A7687');
raw(cellfun(@(x) ~isempty(x) && isnumeric(x) && isnan(x),raw)) = {''};

%% Replace non-numeric cells with NaN
R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),raw); % Find non-numeric cells
raw(R) = {NaN}; % Replace non-numeric cells

%% Create output variable
GHIIWm21 = reshape([raw{:}],size(raw));

%% Clear temporary variables
clearvars raw R;

%% data debug
a=max(GHIWm21)
b=min(GHIWm21)
cont=0
for i=b:a
    
[f c]=find(GHIWm21==i);
if isempty(f)==0
    cont=cont+1;
    potencia=mean(P1W(f,1));
    datos_limpios(cont,:)=[i potencia];
end
end
datos_limpios
x1=datos_limpios(:,1)
y1=datos_limpios(:,2)

%% curve fitting
[xData, yData] = prepareCurveData( x1, y1 );

% Set up fittype and options.
ft = fittype( 'exp2' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.Robust = 'LAR';
opts.StartPoint = [-1.36170923700675e-08 0.0144422902171821 0.388197683522063 -0.000472913681232424];

% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );

% Plot fit with data.
%hold on
subplot(3,1,1)
h1 = plot( fitresult, xData, yData );
lgd = legend(h1,'\alpha = 45°','R^2 = 0.980, RMSE = 0.024', 'Location', 'NorthEast','Orientation','Horizontal');
lgd.FontSize = 14;
% Label axes
xlabel ''
ylabel ''
title('Building 2','FontSize',17)
%% Fitresult
PotF1_building2=fitresult(GHIIWm21);

%% Save and export PotF1 to xls.file
xlswrite('Anual_model_data_Building2',PotF1_building2,'B2+45');
clc
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% end to first panel power
%% Import the data
[~, ~, raw] = xlsread('C:\Users\imbaq\Downloads\Data\Building_2_power.xlsx','Hoja2','A2:B5209');

%% Create output variable
data = reshape([raw{:}],size(raw));

%% Allocate imported array to column variable names
P2W = data(:,1);
GHIWm12 = data(:,2);

%% Clear temporary variables
clearvars data raw;

%% Import the data
[~, ~, raw] = xlsread('C:\Users\imbaq\Downloads\Data\GHI_to_extrapolated.xlsx','366dias','A2:A7687');
raw(cellfun(@(x) ~isempty(x) && isnumeric(x) && isnan(x),raw)) = {''};

%% Replace non-numeric cells with NaN
R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),raw); % Find non-numeric cells
raw(R) = {NaN}; % Replace non-numeric cells

%% Create output variable
GHIIWm22 = reshape([raw{:}],size(raw));

%% Clear temporary variables
clearvars raw R;

%% Debug data
a=max(GHIWm12)
b=min(GHIWm12)
cont=0
for i=b:a
    
[f c]=find(GHIWm12==i);
if isempty(f)==0
    cont=cont+1;
    potencia=mean(P2W(f,1));
    datos_limpios(cont,:)=[i potencia];
end
end
datos_limpios
x2=datos_limpios(:,1)
y2=datos_limpios(:,2)

%% curve fitting
[xData, yData] = prepareCurveData( x2, y2 );

% Set up fittype and options.
ft = fittype( 'sin2' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Algorithm = 'Trust-Region';
opts.Display = 'Off';
opts.Lower = [-Inf 0 -Inf -Inf 0 -Inf];
opts.Normalize = 'on';
opts.Robust = 'LAR';
opts.StartPoint = [0.451052730712573 0.895554546238274 1.42993602643652 0.144945785162333 1.79110909247655 -1.93085268574292];

% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.
subplot(3,1,2)
h2 = plot( fitresult, xData, yData );
lgd = legend(h2,'\alpha = -45°','R^2 = 0.838, RMSE = 0.082', 'Location', 'NorthEast','Orientation','Horizontal');
lgd.FontSize = 14;
% Label axes
xlabel ''
ylabel Power(W)
%% generate data
PotF2_building2=fitresult(GHIIWm22);

%% Save and export PotF1 to xls.file
xlswrite('Anual_model_data_Building2',PotF2_building2,'B2-45');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% end to firts panel power

%% cleaning all and starting to panel number three

clc
%% Import the data
[~, ~, raw] = xlsread('C:\Users\imbaq\Downloads\Data\Building_2_power.xlsx','Hoja3','A2:B5209');

%% Create output variable
data = reshape([raw{:}],size(raw));

%% Allocate imported array to column variable names
P3W = data(:,1);
GHIWm33 = data(:,2);

%% Clear temporary variables
clearvars data raw;

%% Import the data
[~, ~, raw] = xlsread('C:\Users\imbaq\Downloads\Data\GHI_to_extrapolated.xlsx','366dias','A2:A7687');
raw(cellfun(@(x) ~isempty(x) && isnumeric(x) && isnan(x),raw)) = {''};

%% Replace non-numeric cells with NaN
R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),raw); % Find non-numeric cells
raw(R) = {NaN}; % Replace non-numeric cells

%% Create output variable
GHIIWm23 = reshape([raw{:}],size(raw));

%% Clear temporary variables
clearvars raw R;

%% Debug data
a=max(GHIWm33)
b=min(GHIWm33)
cont=0
for i=b:a
    
[f c]=find(GHIWm33==i);
if isempty(f)==0
    cont=cont+1;
    potencia=mean(P3W(f,1));
    datos_limpios(cont,:)=[i potencia];
end
end
datos_limpios
x3=datos_limpios(:,1)
y3=datos_limpios(:,2)

%% curve fitting
[xData, yData] = prepareCurveData( x3, y3 );

% Set up fittype and options.
ft = fittype( 'sin2' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Algorithm = 'Trust-Region';
opts.Display = 'Off';
opts.Lower = [-Inf 0 -Inf -Inf 0 -Inf];
opts.Normalize = 'on';
opts.Robust = 'LAR';
opts.StartPoint = [0.448785465650084 0.895554546238274 1.40232689212382 0.153652538572134 1.79110909247655 -1.90172880724937];

% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.
subplot(3,1,3)
h3 = plot( fitresult, xData, yData );
lgd = legend(h3,'\alpha = -135°','R^2 = 0.928, RMSE = 0.044', 'Location', 'NorthEast','Orientation','Horizontal');
lgd.FontSize = 14;
% Label axes
xlabel GHI(W/m2)
ylabel ''
%% Fitresult
PotF3_building2=fitresult(GHIIWm23);

%% Save and export PotF1 to xls.file
xlswrite('Anual_model_data_Building2',PotF3_building2,'B2-135');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% end to third panel power
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%