function data = repan_data(varargin)

% close all

print_eps = 0;
plot_all = 0;

%% Read data
data.nc_filename = 'Wind_Salti.nc';

data.time_string = nc_varget(data.nc_filename,'time');
data.wind1 = nc_varget(data.nc_filename,'Wind1');
data.wind_dir = nc_varget(data.nc_filename,'Wind_dir');
% data.Salti(:,1) = nc_varget(data.nc_filename,'Salti1');
% data.Salti(:,2) = nc_varget(data.nc_filename,'Salti2');
% data.Salti(:,3) = nc_varget(data.nc_filename,'Salti3');
% data.Salti(:,4) = nc_varget(data.nc_filename,'Salti4');
% data.Salti(:,5) = nc_varget(data.nc_filename,'Salti5');

data.Salti(:,1) = nc_varget(data.nc_filename,'Salti1V');
data.Salti(:,2) = nc_varget(data.nc_filename,'Salti2V');
data.Salti(:,3) = nc_varget(data.nc_filename,'Salti3V');
data.Salti(:,4) = nc_varget(data.nc_filename,'Salti4V');
data.Salti(:,5) = nc_varget(data.nc_filename,'Salti5V');

% add year and convert to number
data.time_string(:,17:21)= repmat('2010"',length(data.time_string),1);
data.time_num = datenum(data.time_string,'"dd/mm HH:MM:SS yyyy"');

% detrend analog signals
data = detrend_analogue(data);

%% All data

if 1 || plot_all
    %times
    time_begin = datenum('8-Dec-2010 15:00:00');
    time_end = datenum('10-Dec-2010 12:30:00');
    
    % saltiphone locations
    Salti(1,:) = [68261.844 446551.988 3.235];
    Salti(2,:) = [68260.174 446553.215 3.173];
    Salti(3,:) = [68260.672 446552.832 3.195];
    Salti(4,:) = [68259.559 446553.567 3.167];
    Salti(5,:) = [68261.137 446552.491 3.209];
    
    plot_meting(Salti,time_begin,time_end,data);  
end

if 0
    data2 = resample_from_5sec_to_1min(data);
    plot_meting(time_begin,time_end,data2);
end

%% Meting I
if 0 || plot_all
    % times
    time_begin = datenum('8-Dec-2010 15:00:00');
    time_end = datenum('8-Dec-2010 21:30:00');
    
    % saltiphone locations
    Salti(1,:) = [68261.844 446551.988 3.235];
    Salti(2,:) = [68260.174 446553.215 3.173];
    Salti(3,:) = [68260.672 446552.832 3.195];
    Salti(4,:) = [68259.559 446553.567 3.167];
    Salti(5,:) = [68261.137 446552.491 3.209];
    
    plot_meting(Salti,time_begin,time_end,data);
    
    % printing pictures
    if print_eps
        print('-depsc',['setup_rose_m1.eps'])
        close
        print('-depsc',['salti_comp_m1.eps'])
        close
        print('-depsc',['data_m1.eps'])
        close
    end
    
end
%% Meting II
if 0 || plot_all
    % times
    time_begin = datenum('8-Dec-2010 22:10:00');
    time_end = datenum('8-Dec-2010 23:40:00');
    
    % Saltiphone location
    Salti(1,:) = [68261.910 446552.053 3.245];
    Salti(2,:) = [68222.309	446584.622 1.128];
    Salti(3,:) = [68235.115	446576.623 1.672];
    Salti(4,:) = [68248.474	446566.050 2.596];
    Salti(5,:) = [68204.868	446599.308 0.570];
    
    plot_meting(Salti,time_begin,time_end,data);
    
    % printing pictures
    if print_eps
        print('-depsc',['setup_rose_m2.eps'])
        close
        print('-depsc',['salti_comp_m2.eps'])
        close
        print('-depsc',['data_m2.eps'])
        close
    end
    
end

%% Meting III
if 0 || plot_all
    % times
    time_begin = datenum('9-Dec-2010 00:00:00');
    time_end = datenum('9-Dec-2010 9:30:00');
    
    % Saltiphone location
    Salti(1,:) = [68261.936 446552.178 3.240];
    Salti(2,:) = [68262.591 446552.708 3.241];
    Salti(3,:) = [68263.319 446553.193 3.243];
    Salti(4,:) = [68264.187 446553.707 3.234];
    Salti(5,:) = [68261.267 446551.554 3.251];
    
    plot_meting(Salti,time_begin,time_end,data);
    
    % printing pictures
    if print_eps
        print('-depsc',['setup_rose_m3.eps'])
        close
        print('-depsc',['salti_comp_m3.eps'])
        close
        print('-depsc',['data_m3.eps'])
        close
    end
    
end

%% Meting IV
if 0 || plot_all
    % times
    time_begin = datenum('9-Dec-2010 10:00:00');
    time_end = datenum('9-Dec-2010 11:30:00');
    
    % Saltiphone location
    Salti(1,:) = [68254.659 446539.049 3.410];
    Salti(2,:) = [68234.647 446552.586 2.589];
    Salti(3,:) = [68220.805 446564.756 1.553];
    Salti(4,:) = [68274.643 446517.166 3.824];
    Salti(5,:) = [68261.266 446551.450 3.243];
    
    plot_meting(Salti,time_begin,time_end,data);
    
    % printing pictures
    if print_eps
        print('-depsc',['setup_rose_m4.eps'])
        close
        print('-depsc',['salti_comp_m4.eps'])
        close
        print('-depsc',['data_m4.eps'])
        close
    end
    
end

%% Meting IVb
if 0 || plot_all
    % times
    time_begin = datenum('9-Dec-2010 11:30:00');
    time_end = datenum('9-Dec-2010 15:20:00');
    
    % Saltiphone location
    Salti(1,:) = [68254.659 446539.049 3.410];
    Salti(2,:) = [68234.647 446552.586 2.589];
    Salti(3,:) = [68220.805 446564.756 1.553];
    Salti(4,:) = [68274.643 446517.166 3.824];
    Salti(5,:) = [68227.556 446559.284 1.944];
    
    plot_meting(Salti,time_begin,time_end,data);
    
    % printing pictures
    if print_eps
        print('-depsc',['setup_rose_m4b.eps'])
        close
        print('-depsc',['salti_comp_m4b.eps'])
        close
        print('-depsc',['data_m4b.eps'])
        close
    end
end

%% Meting V
if 0 || plot_all
    % times
    time_begin = datenum('9-Dec-2010 16:00:00');
    time_end = datenum('10-Dec-2010 01:30:00');
    
    % Saltiphone location
    Salti(1,:) = [68254.640 446539.120 3.519];
    Salti(2,:) = [68248.963 446543.295 3.363];
    Salti(3,:) = [68265.234	446528.967 3.747];
    Salti(4,:) = [68274.822 446517.235 3.942];
    Salti(5,:) = [68270.394	446522.540 3.836];
    
    plot_meting(Salti,time_begin,time_end,data);
    
    % printing pictures
    if print_eps
        print('-depsc',['setup_rose_m5.eps'])
        close
        print('-depsc',['salti_comp_m5.eps'])
        close
        print('-depsc',['data_m5.eps'])
        close
    end
end
%%


function data = detrend_analogue(data)
    time_begin = datenum('8-Dec-2010 15:00:00');
    time_end = datenum('10-Dec-2010 12:30:00');
    time_index = data.time_num>time_begin & data.time_num<time_end;
wind_line = round((0:0.25:20)*10)/10;
min_count = ones(length(wind_line),5);
for i=1:length(wind_line)
    count_line = data.Salti(((data.wind1 == wind_line(i)) & time_index),:);
    if ~isempty(count_line)
        min_count(i,:) = min(count_line);
    else
        min_count(i,:) = nan;     
    end
end
ref = mean(min_count(1:35,:));
data.Salti = data.Salti - repmat(ref,length(data.Salti),1);
function plot_meting(Salti,time_begin,time_end,data)
%%
time_index = data.time_num>time_begin & data.time_num<time_end;

% round salti y axis up to a 1000 inerval
salti_axis_limits = [0 (ceil(max(max(data.Salti))/100))*100];
salti_axis_limits = [0 800];

figure
subplot(3,1,1)
plot(data.time_num(time_index),data.wind1(time_index),'.')
ylim([0 13])
datetick
ylabel('Wind speed (#1) [m/s]')

if floor(time_end) - floor(time_begin)> 0
    title([datestr(time_begin,'dd') ' - ' datestr(time_end,'dd mmm yyyy')])
else
    title(datestr(time_end,'dd mmm yyyy'))
end

subplot(3,1,2)
plot(data.time_num(time_index),data.wind_dir(time_index),'.')
ylim([0 360])
ylabel('Wind direction [deg]')
datetick
subplot(3,1,3)
plot(data.time_num(time_index),data.Salti(time_index,1),'.')
ylabel('Saltiphone (#1) [counts]')
xlabel('Time')
ylim(salti_axis_limits)
datetick
set(gcf,'PaperPosition',[0.634517 6.34517 15 15])

% Add total
total(1) = sum(data.Salti(time_index,1));
total(2) = sum(data.Salti(time_index,2));
total(3) = sum(data.Salti(time_index,3));
total(4) = sum(data.Salti(time_index,4));
total(5) = sum(data.Salti(time_index,5));

% Add max, min and mean count values grouped per wind speed
wind_line = round((0:0.25:20)*10)/10;
max_count = ones(length(wind_line),5);
min_count = ones(length(wind_line),5);
mean_count = ones(length(wind_line),5);
for i=1:length(wind_line)
    count_line = data.Salti(((data.wind1 == wind_line(i)) & time_index) ,:);
    if ~isempty(count_line)
        max_count(i,:) = max(count_line);
        min_count(i,:) = min(count_line);
        mean_count(i,:) = mean(count_line);
    else
        max_count(i,:) = nan;
        min_count(i,:) = nan;
        mean_count(i,:) = nan;
        
    end
end
figure
for i = 1:5
    subplot(3,2,i)
    plot(data.wind1(time_index),data.Salti(time_index,i),'.')
    hold all
    plot(wind_line,max_count(:,i),'linewidth',3)
    plot(wind_line,mean_count(:,i),'linewidth',3)
    plot(wind_line,min_count(:,i),'linewidth',3)
    xlabel('Wind speed [m/s]')
    ylabel(['S #' num2str(i) ' [counts]'])
    xlim([0 12])
    ylim(salti_axis_limits)
end
subplot(3,2,6)
bar(total)
xlabel('Saltiphone #')
ylabel('total counts')
hline(mean(total))
set(gcf,'PaperPosition',[0.634517 6.34517 15 15])

% make extra plot
if 1
    figure
    plot(data.wind1(time_index),data.Salti(time_index,1),'m.')
    hold all
    plot(wind_line,max_count(:,1),'linewidth',3)
    plot(wind_line,mean_count(:,1),'linewidth',3)
    plot(wind_line,min_count(:,1),'linewidth',3)
    xlabel('Wind speed [m/s]')
    ylabel(['S #' num2str(1) ' [mV]'])
    xlim([0 12])
    ylim(salti_axis_limits)
    set(gcf,'PaperPosition',[0.634517 6.34517 10 10])
    
end

%% Make a little map of the area
figure
subplot(1,2,1)
hold all
for i = 1:5
    plot(Salti(i,1),Salti(i,2),'rs');
    text(Salti(i,1)+5,Salti(i,2)+5,['Salti ' num2str(i)]);
end
%%% Reference line (app at the position of the fence)
Refline=[69133 447456 ; 67973 446152]; %NE to SW
angle_refline=-180 + atand( (Refline(2,2)-Refline(1,2)) / (Refline(2,1)-Refline(1,1)) );
plot(Refline(:,1), Refline(:,2),'--k')
plot(Refline(:,1)-[200 200]', Refline(:,2)-[0 0]','--b')
load surveylines.mat
for j=16
    plot(Surveylines{j,1}.xi,Surveylines{j,1}.yi,'k')
    % text(Surveylines{j,1}.xi(1),Surveylines{j,1}.yi(1)+10,num2str(Surveylines{j,1}.lineno))
end
xlabel('X [m RD]')
ylabel('Y [m RD]')
axis equal
xlim([68086 68478])
ylim([446424 446733])


subplot(1,2,2)
axis off
wind_rose(data.wind_dir(time_index),data.wind1(time_index),'dtype','meteo',...
    'di',[0 3 6 9 15],'cmap',gray,'ax',[gca 0.5 0.5 1],'quad',4);
set(gcf,'PaperPosition',[0.634517 6.34517 17 15])
%%
function data2 = resample_from_5sec_to_1min(data)
%% this function resamples the data from 5 seconds to one minute intervals.
disp('resampling data')
times_rounded = round(data.time_num*24*60)/60/24;
times_line = (round(data.time_num(1)*24*60):1:round(data.time_num(end)*24*60))/60/24;
data2.nc_filename = data.nc_filename;
data2.time_string = datestr(data.time_num,'"dd/mm HH:MM:SS   " ');

% allocate data2
data2.wind1 = zeros(length(times_line),1);
data2.wind_dir = zeros(length(times_line),1);
data2.Salti = zeros(length(times_line),5);
data2.SaltiV = zeros(length(times_line),5);

for i=1:length(times_line)
    index = times_rounded == times_line(i);
    data2.wind1(i,:) = mean(data.wind1(index));
    data2.wind_dir(i,:) = mean(data.wind_dir(index));
    data2.Salti(i,:) = sum(data.Salti(index,:));
    data2.SaltiV(i,:) = sum(data.SaltiV(index,:));
    
end
data2.time_num = times_line';

%round wind data towards old intervals
data2.wind1 = round((round(data2.wind1*4)/4)*10)/10;



%% Meting II

