%**************************************************************************
% CLUSTER project: Filelist creator                Autor: Christian Maszl * 
% Unit System = [SI]                  Mail: e0025754@student.tuwien.ac.at *
%**************************************************************************
%
% This script searches for Magnetometer - Datafiles in the datapath
% defined in configfile.my and store the filenames in an
% array. Afterwards the program
% curlB use this files for the calculation of Jav and div(B)av. The
% filenames have to be in the following shape:
%
% [yymmdd]0000[type].[satellite].[t1]-[t2].[coordsys].[timeformat].[igmasci]
% Example: 0203040000fn.01.0700-1019.gse.hr.igmasc
%
% These datafiles contains the measuring time, the components of the B 
% field and the position vector. It's important that the B field data 
% is spin (satellite) compensated!
%
% The valid files (variable: myfilelist) are stored in myfilelist.log
%
% The program also creates the file error.log (variable errorlog). In this 
% file will be missing and invalid files reported.
% *************************************************************************
% Use the binary Version of this program if possible. This version
% is no longer supported and maintained!
% *************************************************************************

% Variables definig the data type (ASCII Version only!)
coordsystem='gse'; timeformat='hr'; extension='igmasc';

% Read all files with the extension *.igmasc
filenam1=dir([datapath,'*.',extension]);
% Preallocate array
status      =cell (size(filenam1,1),4);
indexlist1  =zeros(size(filenam1,1),1);
invalidlist1=zeros(size(filenam1,1),1);
invalidlist2=zeros(1,1);
indexlist   =zeros(1,1);
datalist    =zeros(1,4);
% Check data for mode, coord.system, timeformat, datatype
for i=1:length(filenam1)
    status{i,1}=findstr(filenam1(i,1).name,mode);
    status{i,2}=findstr(filenam1(i,1).name,coordsystem);
    status{i,3}=findstr(filenam1(i,1).name,timeformat);
    status{i,4}=findstr(filenam1(i,1).name,extension);
    if (status{i,1}==11 & status{i,2}==27 & ...
        status{i,3}==31 & status{i,4}==34)
        indexlist(i,1)=i;
    else
        invalidlist1(i,1)=i;    
    end
end
% Find all nonzero elements of the lists
indexlist   =find(indexlist);
invalidlist1=find(invalidlist1);
% Preallocate arrays
filenam2 =cell(size(indexlist,1),8);
filenam3 =cell(size(indexlist,1),1);
% Write valid filenames into filenam2
for i=1:length(indexlist)
    % Valid lines of the input file list
    j=indexlist(i);
    % Extract date, mode, satellite, start time, stop time, coord. sys,
    % time unit and data format (ascii, binary)
    filenam2{i,1}=sscanf(filenam1(j,1).name,'%6c',1);
    filenam2{i,2}=sscanf(filenam1(j,1).name,'%*u%2c',1);
    filenam2{i,3}=sscanf(filenam1(j,1).name,'%*ufn.%2u',1);
    filenam2{i,4}=sscanf(filenam1(j,1).name,'%*ufn.%*u.%4u',1);
    filenam2{i,5}=sscanf(filenam1(j,1).name,'%*ufn.%*u.%*u-%4u',1);
    filenam2{i,6}=sscanf(filenam1(j,1).name,'%*ufn.%*u.%*u-%*u.%3c',1);
    filenam2{i,7}=sscanf(filenam1(j,1).name, ...
                  '%*ufn.%*u.%*u-%*u.%*3c.%2c',1);
    filenam2{i,8}=sscanf(filenam1(j,1).name, ...
                  '%*ufn.%*u.%*u-%*u.%*3c.%*2c.%6c',1);
    % Store complete filename
    filenam3{i,1}=filenam1(j,1).name;
end
% Sort the datafiles with respect to the date, the starttime and the
% satellite number
dateinfo=zeros(size(filenam2,1),6);
filenam4=cell (size(filenam2,1),8);
filenam5=cell (size(filenam2,1),1);
for i=1:size(filenam2,1)
    % Store year
    dateinfo(i,1)=str2double(sscanf(filenam2{i,1},'%2c',1));
    % Store month
    dateinfo(i,2)=str2double(sscanf(filenam2{i,1},'%*2c%2c',1));
    % Store day
    dateinfo(i,3)=str2double(sscanf(filenam2{i,1},'%*4c%2c',1));
    % Satellite number
    dateinfo(i,4)=filenam2{i,3};
    %Start time
    dateinfo(i,5)=filenam2{i,4};
    % Store index
    dateinfo(i,6)=i;
end
% Sort the rows according to the following rule:
% 1) year(1) -> 2) month(2) -> 3) day(3) -> 4) start time(5) 
%            -> 5) Satellite number(4)
dateinfo=sortrows(dateinfo,[1 2 3 5 4]);
% Write sorted data in filenam2 and filenam3
for i=1:size(dateinfo,1)
        filenam4{i,1}=filenam2{dateinfo(i,6),1};
        filenam4{i,2}=filenam2{dateinfo(i,6),2};
        filenam4{i,3}=filenam2{dateinfo(i,6),3};
        filenam4{i,4}=filenam2{dateinfo(i,6),4};
        filenam4{i,5}=filenam2{dateinfo(i,6),5};        
        filenam4{i,6}=filenam2{dateinfo(i,6),6};
        filenam4{i,7}=filenam2{dateinfo(i,6),7};
        filenam4{i,8}=filenam2{dateinfo(i,6),8};
        % Store complete filenames 
        filenam5{i,1}=filenam3{dateinfo(i,6),1};
end
filenam2=filenam4;
filenam3=filenam5;
clear filenam4 dateinfo;
% Check for the same date and start and end time 
lauf = 1;
indexlist2 =zeros(size(filenam2,1),1);
for i=1:size(filenam2,1)-3
    if (filenam2{i,1} == filenam2{i+1,1} & filenam2{i,1}==filenam2{i+2,1} & ...
        filenam2{i,1} == filenam2{i+3,1} & ...
        filenam2{i,4} == filenam2{i+1,4} & filenam2{i,4}==filenam2{i+2,4} & ...
        filenam2{i,4} == filenam2{i+3,4} & ...
        filenam2{i,5} == filenam2{i+1,5} & filenam2{i,5}==filenam2{i+2,5} & ...
        filenam2{i,5} == filenam2{i+3,5} )
        % Write match to row, no zero lines in datalist due to lauf
        datalist(lauf,:) =  [i i+1 i+2 i+3];   
        lauf=lauf+1;
    else
    end
end
% Create filelist for use in the main program
myfilelist=cell(lauf-1,4);
for i=1:lauf-1
        myfilelist{i,1}=filenam3{datalist(i,1),1};
        myfilelist{i,2}=filenam3{datalist(i,2),1};
        myfilelist{i,3}=filenam3{datalist(i,3),1};
        myfilelist{i,4}=filenam3{datalist(i,4),1};
end
% Compare filenam1(:,1).name with filenam3. Write nonmatching results to
% invalidlist2
lauf = 1;
X=size(datalist);
elements=X(1,1)*X(1,2);
datalist=reshape(datalist,elements,1);
foundone=0;
for i=1:size(filenam3,1)
    for j=1:length(datalist)
        if (i==datalist(j,1))
            foundone=1; 
        else
        end
    end
    if (foundone==0)
        invalidlist2(lauf,1)=i;
        lauf=lauf+1;
    else
    end
    foundone=0;
end
% Use invalidlist1 and 2 to create filelist.log
% Find maximum length of the log file + 2 lines for comments
l=size(invalidlist1,1)+size(invalidlist2,1)+2;
errorlog=cell(l,1);
errorlog{1,1}='Error in the file name. Files not included in the calculation!';
for i=1:size(invalidlist1,1)
    errorlog{i+1,1}=filenam1(invalidlist1(i),1).name;
end
errorlog{size(invalidlist1,1)+2,1}='Set of four datafiles not complete!';
%filenam3=cell2mat(filenam3);
if (invalidlist2(1)~=0)
    for i=1:size(invalidlist2,1)
        errorlog{size(invalidlist1,1)+2+i,1}=filenam3(invalidlist2(i),:);
    end
else
end
% Delete unnecessary variables
clear  filenam1 filenam2 filenam3 filenam5 foundone i j l lauf status;
clear indexlist1 indexlist2 invalidlist1 invalidlist2 datalist;
clear indexlist X elements;
% Alert if no data can be found in myfilelist
if (size(myfilelist,1)<1)
    disp('Error: myfilelist is empty! Check variable mode in'...
         ' configfile.m! Examples: fn' );
    break;
else
end
% myfilelist contains the filenames for the main program.
% errorlog contains the unused files due to several errors.
% Use for a possible GUI

% Store myfilelist and errorlog in files
logfiles;
