% program to calculate the end time of the growing mode
clear all;format long g;

% select station
stationname='Hyytiala';

% Neccessary input files for the analysis
% 1. dmps daily files (sum-files)
% 2. Formation analysis daily files (MNC-files)
% 3. Classification of events daily files (CLS-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)];

dmpsdataday1=[];dmpsdataday2=[];dmpsdataday3=[];MNCdata=[];sizes1=[];sizes2=[];sizes3=[];
n=1;

%doing EOG calculations for each day
for i=1:period(end)-period(1)+1
     %loading MNC data to be able to plot the 'eventtime'-lines
        if exist([stationname,datestr(period(i),'YY'),datestr(period(i),'mm'),datestr(period(i),'dd'),'.MNC'])>0;
            tempdata=[];
            tempdata=[i load([stationname,datestr(period(i),'YY'),datestr(period(i),'mm'),datestr(period(i),'dd'),'.MNC'])];
            MNCdata=[MNCdata; tempdata];
            disp(['Loading data from : ',datestr(period(i),'yymmdd')]);
        end
end

for i=1:period(end)-period(1)+1
    % checking if it is an event day
    disp(['EOG for: ',datestr(period(i),'yymmdd')]);
    if EOG_eventday(stationname,period(i))
        
        %loaddata-del
        %loading eventday dmps data
        if exist([stationname,datestr(period(i),'YY'),datestr(period(i),'mm'),datestr(period(i),'dd'),'.sum'])>0;
            tempdata=[];
            tempdata=load([stationname,datestr(period(i),'YY'),datestr(period(i),'mm'),datestr(period(i),'dd'),'.sum']);
            varkoll=tempdata; %min koll för att se hur datum och tid fungerar
            tempdata(:,1)=(tempdata(:,1)-floor(tempdata(:,1)))+period(i);  %to transform the date and time to Matlab time
            
            sizes1=tempdata(1,3:end);   %ändrat från orginal, problem den 21 juni 2004 med för många kolumner i sum-fil jämfört med sizes
                                        %storlekarna för de andra dagarna ligger i sizes2 och sizes3
                                        
            tempdata = tempdata(2:end,:);   %utelämnar första raden som är storlekarna
            dmpsdataday1= tempdata;
        end 
        
        %loading the two days after eventday
        %load eventday + 1
        if exist([stationname,datestr(period(i)+1,'YY'),datestr(period(i)+1,'mm'),datestr(period(i)+1,'dd'),'.sum'])>0;
            tempdata=[];
            tempdata=load([stationname,datestr(period(i)+1,'YY'),datestr(period(i)+1,'mm'),datestr(period(i)+1,'dd'),'.sum']);
            tempdata(:,1)=(tempdata(:,1)-floor(tempdata(:,1)))+period(i)+1;
            sizes2=tempdata(1,3:end);
            tempdata=tempdata(2:end,:); %sizes tas inte för de andra dagarna än eventday, ändra?
            dmpsdataday2= tempdata;
        end
        %load eventday + 2
        if exist([stationname,datestr(period(i)+2,'YY'),datestr(period(i)+2,'mm'),datestr(period(i)+2,'dd'),'.sum'])>0;
            tempdata=[];
            tempdata=load([stationname,datestr(period(i)+2,'YY'),datestr(period(i)+2,'mm'),datestr(period(i)+2,'dd'),'.sum']);
            tempdata(:,1)=(tempdata(:,1)-floor(tempdata(:,1)))+period(i)+2;
            sizes3=tempdata(1,3:end);
            tempdata= tempdata(2:end,:);
            dmpsdataday3= tempdata;
        end
        % plotting the size distributions
        EOG_plotdistributions
        % if data is bad or missing it might not be possible to select end of
        % growth rate time, which could be chosen in the following menu:
        interrupt=menu('Press','Select end of growth rate time','Not possible to select end of growth rate time');
        if interrupt==2
            interrupt=true;
        else
            interrupt=false;
        end
        % Selecting the end time of the growing mode (unless input is interrupted)
        if interrupt
            time(n)=-999.99;
        else
            [time(n),Y]=ginput(1);
        end
        % checking if something is wrong with the input
        if isempty(time(n))
            time(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
        time(n)=-999.99;
        n=n+1;
    end
    % saving the end of growth rate time selection
    timetemp=time(n-1);
    save([stationname,datestr(period(i),'YY'),datestr(period(i),'mm'),datestr(period(i),'dd'),'.EOG'],'timetemp','-double','-ascii');
end
