%**************************************************************************
% CLUSTER project: Store jav and divB              Autor: Christian Maszl  
% Unit System = [SI]                  Mail: e0025754@student.tuwien.ac.at 
%**************************************************************************
%
% Version 1.0 - 20.8.2004
%
% This subroutine store the computed data to the harddisk. 
%
% Filename format for jav: 
% [yymmdd]0000[type].jav.[coordsys].[timeformat].[igmmat]
%                                                [igmasc]
% Example: 0203040000fn.jav.gse.hr.igmmat
% 
% Format of the data in the file: [time jav]
%
% Filename format for <divB>: 
% [yymmdd]0000[type].divB.[coordsys].[timeformat].[igmmat]
%                                                 [igmasc]
% Example: 0203040000fn.divB.gse.hr.igmmat
%
% Format of the data in the file: [time divB]
%
% Format of D: (only in the error case!)
% [yymmdd]0000[type].DX.[coordsys].[timeformat].[igmmat]
%                                               [igmasc]
% Example: 0203040000fn.D1.gse.hr.igmmat
%
% Format of the data in the file: [time B1 r1]

if (errorflag_nodata==1)
    if (size(datatype,2)==4)
        ende = ['gse.hr.igmmat'];
    else
        ende = ['gse.hr.igmasc'];
    end
% Try to save D1-D4
    if  (isempty(D1)~=1)
        satz = strrep(myfilelist{lauf,1},'.01','.D1.');
	filename=[outputspath satz ende];
	save(filename,'D1',datatype); 
    else
    end
    if(isempty(D2)~=1)
        satz = strrep(myfilelist{lauf,1},'.01','.D2.');
	filename=[outputspath satz ende];
	save(filename,'D2',datatype); 
    else
    end
    if(isempty(D3)~=1)
        satz = strrep(myfilelist{lauf,1},'.01','.D3.');
	filename=[outputspath satz ende];
	save(filename,'D3',datatype);     
    else
    end
    if(isempty(D4)~=1)
        satz = strrep(myfilelist{lauf,1},'.01','.D4.');
	filename=[outputspath satz ende];
	save(filename,'D4',datatype); 
    else
    end
else
% Take a name of the datapackage and replace the satellite number with
% jav (current density)
    satz = strrep(myfilelist{lauf,1},'.01','.jav.');
% Check for datatype and fix the extension
    if (size(datatype,2)==4)
        filename = [outputspath satz 'gse.hr.igmmat'];
    else
        filename = [outputspath satz 'gse.hr.igmasc'];
    end
% Save data (binary=5x faster) to disk
    save(filename,'T','jav',datatype); 
% Take a name of the datapackage and replace the satellite number with
% divB
    filename=strrep(filename,'.jav.','.divB.');
% Save data (binary=5x faster) to disk
    save(filename,'divB',datatype);
    
% Store also T, B, r for a possible seperate usage.
% Save data (binary=5x faster) to disk
    filename=strrep(filename,'.divB.','.D1.');
    save(filename,'T','B1','r1',datatype);
% Save data (binary=5x faster) to disk
    filename=strrep(filename,'.D1.','.D2.');
    save(filename,'T','B2','r2',datatype);
% Save data (binary=5x faster) to disk
    filename=strrep(filename,'.D2.','.D3.');
    save(filename,'T','B3','r3',datatype);
% Save data (binary=5x faster) to disk
    filename=strrep(filename,'.D3.','.D4.');
    save(filename,'T','B4','r4',datatype);
% Clear dispensable variables
    clear filename satz;
end

