% calculate the t_start and t_end time of the formation event (MNC-files)
clear all;format long g;

% select station
stationname='Hyytiala'; % Write the name of your station

% Neccessary input files for the analysis
% 1. dmps daily, hourly averaged files (sum-files)
% 2. Classification of events daily files (CLS-files)

% output file
% 1. t_start and t_end in daily files (MNC-files)

% load all fitted data and dmps data for selected period (+2 days to allow for growth)
% select period
period=[datenum(2005,5,11,0,0,0):datenum(2005,5,14,0,0,0)]; % select period YYYY,M,D,0,0,0 until YYYY,M,D,0,0,0

% loading data
start_end_time_loaddata;

n=1;
for i=1:period(end)-period(1)+1
    % checking if it is an event day
    if start_end_time_eventday(stationname,period(i))
        % plotting the size distributions
        start_end_time_plotdistribution;
        % if data is bad or missing it might not be possible to select the times, which could be chosen in the following menu:
        interrupt=menu('Press','Select t_start and t_end time','Not possible to select these times');
        if interrupt==2
            interrupt=true;
        else
            interrupt=false;
        end
        % Selecting the end time of the growing mode (unless input is interrupted)
        if interrupt
            tstart(n)=-999.99;
            tend(n)=-999.99;
        else
            [ttimes,Y]=ginput(2);
            tstart(n)=ttimes(1,1);
            tend(n)=ttimes(2,1);
        end
        % checking if something is wrong with the input
        if isempty(tstart(n))
            tstart(n)=-999.99;
            tend(n)=-999.99;
        end
        % next time step
        n=n+1;
    % if it is not an event day the end of growth rate time will be set to
    % -999.99
    else
        tstart(n)=-999.99;
        tend(n)=-999.99;
        n=n+1;
    end
    % saving the end of growth rate time selection
    starttemp=tstart(n-1);
    endtemp=tend(n-1);
    % the vector should contain 7 columns (we don't care about remaining 4
    % columns), we only save tstart and tend in columns 5 and 7 resp.
    timetemp=[period(i) -999.99 -999.99 starttemp -999.99 endtemp -999.99];
    % saving the MNC data file
    save([stationname,datestr(period(i),'YY'),datestr(period(i),'mm'),datestr(period(i),'dd'),'.MNC'],'timetemp','-double','-ascii');
end