% Plott histogrammer av snødybde, snøtetthet og SWE for Rybekken og Sylsjødammen
% INPUT: .txt-filer med observasjoner fra Rybekken og Sylsjødammen med snødybde,
% snøtetthet og SWE i henholdsvis andre, tredje og fjerde kolonne
clc, clear all, close all

% Last inn data
ry = load('rybekken.txt');
sy = load('sylsjodammen.txt');

ry_depth = ry(:,2);
ry_density = ry(:,3);
ry_SWE = ry(:,4);
sy_depth = sy(:,2);
sy_density = sy(:,3);
sy_SWE = sy(:,4);

% Bestem minste og største verdier
ry_depth_min = min(ry_depth);
ry_depth_max = max(ry_depth);
ry_density_min = min(ry_density);
ry_density_max = max(ry_density);
ry_SWE_min = min(ry_SWE);
ry_SWE_max = max(ry_SWE);
sy_depth_min = min(sy_depth);
sy_depth_max = max(sy_depth);
sy_density_min = min(sy_density);
sy_density_max = max(sy_density);
sy_SWE_min = min(ry_SWE);
sy_SWE_max = max(sy_SWE);

% Definer delintervaller
n = 16; % Antall delintervaller
ry_depth_dx = (ry_depth_max - ry_depth_min)/n;
ry_density_dx = (ry_density_max - ry_density_min)/n;
ry_SWE_dx = (ry_SWE_max - ry_SWE_min)/n;
sy_depth_dx = (sy_depth_max - sy_depth_min)/n;
sy_density_dx = (sy_density_max - sy_density_min)/n;
sy_SWE_dx = (sy_SWE_max - sy_SWE_min)/n;

ry_depth_x = ry_depth_min:ry_depth_dx:ry_depth_max;
ry_density_x = ry_density_min:ry_density_dx:ry_density_max;
ry_SWE_x = ry_SWE_min:ry_SWE_dx:ry_SWE_max;
sy_depth_x = sy_depth_min:sy_depth_dx:sy_depth_max;
sy_density_x = sy_density_min:sy_density_dx:sy_density_max;
sy_SWE_x = sy_SWE_min:sy_SWE_dx:sy_SWE_max;

% Tell antall observasjoner i delintervallene
ry_depth_count = hist(ry_depth,ry_depth_x);
ry_density_count = hist(ry_density,ry_density_x);
ry_SWE_count = hist(ry_SWE,ry_SWE_x);
sy_depth_count = hist(sy_depth,sy_depth_x);
sy_density_count = hist(sy_density,sy_density_x);
sy_SWE_count = hist(sy_SWE,sy_SWE_x);

% Beregn arealet av søylene i histogrammet (for standardisering)
ry_depth_area = ry_depth_dx*sum(ry_depth_count);
ry_density_area = ry_density_dx*sum(ry_density_count);
ry_SWE_area = ry_SWE_dx*sum(ry_SWE_count);
sy_depth_area = sy_depth_dx*sum(sy_depth_count);
sy_density_area = sy_density_dx*sum(sy_density_count);
sy_SWE_area = sy_SWE_dx*sum(sy_SWE_count);

% Plott dataene
figure(1), set(gcf,'Color','White'); %Opprett figur, hvit bakgrunnsfarge

subplot(3,2,1), hold on % Velg subplott
title('Snødybde, Rybekken','FontWeight','bold')
bar(ry_depth_x,ry_depth_count/ry_depth_area,1) % Søylediagram hvor bredden av søylene er 1, dvs. de er helt inntil hverandre
box on % Ramme rundt diagrammet
shading faceted % marker kanten av søylene
alpha(0.6) % Juster gjennomsiktigheten av søylene
xlabel('{Dybde (cm)}')
ylabel('Relativ frekvens')
xlim([0 300])
hold off

subplot(3,2,3), hold on
title('Snøtetthet, Rybekken','FontWeight','bold')
bar(ry_density_x,ry_density_count/ry_density_area,1)
box on
shading faceted
alpha(0.6)
xlabel('{Tetthet (g/cm^3)}')
ylabel('Relativ frekvens')
xlim([0.1 0.7])
hold off

subplot(3,2,2), hold on
title('Snødybde, Sylsjødammen','FontWeight','bold')
bar(sy_depth_x,sy_depth_count/sy_depth_area,1)
box on
shading faceted
alpha(0.6)
xlabel('{Dybde (cm)}')
ylabel('Relativ frekvens')
xlim([0 300])
hold off

subplot(3,2,4), hold on
title('Snøtetthet, Sylsjødammen','FontWeight','bold')
bar(sy_density_x,sy_density_count/sy_density_area,1)
box on
shading faceted
alpha(0.6)
xlabel('{Tetthet (g/cm^3)}')
ylabel('Relativ frekvens')
xlim([0.1 0.7])
hold off

subplot(3,2,5), hold on
title('SWE, Rybekken','FontWeight','bold')
bar(ry_SWE_x,ry_SWE_count/ry_SWE_area,1)
box on
shading faceted
alpha(0.6)
ylabel('Relativ frekvens')
xlabel('{SWE (cm)}')
xlim([0 140])
hold off

subplot(3,2,6), hold on
title('SWE, Sylsjødammen','FontWeight','bold')
bar(sy_SWE_x,sy_SWE_count/sy_SWE_area,1)
box on
shading faceted
alpha(0.6)
xlabel('{SWE (cm)}')
ylabel('Relativ frekvens')
xlim([0 140])
hold off
