Prr split file

From Pnb
Jump to navigation Jump to search
function prr_split_file(filename)
%Created by Erik Stassinos 10/29/2013
%This function takes the input single lcd and then splits it into up into
%multiple files... one cast per file
%looks at downcast matrix and splits the file accordingly


fid = fopen(filename);

%============== get the indexes of data to split the file
[in,mer,dep] =  index1(filename);
ic = size(in,1);
%==============



%==============create coppies of the filters used list

tmp_fltr = fopen(['fltr',filename],'w+');

while ~feof(fid)
    chkline =fgets(fid);
    %fprintf(tmp_fltr,'%s',chkline);
    if strncmp(chkline,'<filters_used>',6)==1
        %break
    %end
    while ~feof(fid)
            chkline =fgets(fid);
            fprintf(tmp_fltr,'%s',chkline);
    end
    end
end
fclose(tmp_fltr);
%==============
%==============end create filter copies



%============== read through the original file to create copies of header
%==============to write
frewind(fid);
tmp_hdr = fopen(['hdr',filename],'w+');

while ~feof(fid)
    chkline =fgets(fid);
    fprintf(tmp_hdr,'%s',chkline);
    if strncmp(chkline,'<data>',6)==1
        break
    end
end
fclose(tmp_hdr);
%==============
%============== end read through to create header file


%============== Read the data to make a temp data file of each indexic = size(in,1);
%==============
disp('number of files to create = ')
ic./2

tempmat = ['tempm',filename];
tm = fopen(tempmat,'w+');

for i = 1:ic/2 %generate mat file names for each cast to break up
    mname = strcat('mat_',num2str(i),'_',filename);
    mnamelist(i,:) = mname;
end
    l =0;
    while strncmp(l,'<filters_used>',14) ~=1
              l = fgets(fid);
              if strncmp(l,'<filters_used>',14) == 1 
                  break
              end
              fprintf(tm,l);
    end
fclose(tm);
mat = load(tempmat);
width = length(mat(1,:));
count =1;
for lp = 1:2:ic
    step1 = lp;
    step2 = lp+1;
    for ij = in{step1}:1:in{step2};
             castmatrix(ij-(in{step1}-1),:) = mat(ij,1:width); %starts from 1 and goes to in2
    end
    dlmwrite(mnamelist(count,:),castmatrix,'delimiter',' ','precision','%.5d');
    count = count +1;
end

%=============
%=============end make individual matrix files


%=============create new LCDs
%=============
tmp_hdr = fopen(['hdr',filename],'r');
tmp_fltr = fopen(['fltr',filename],'r');
%new list file
splitlist = fopen('slist.txt','r+');
if splitlist == -1
splitlist = fopen('slist.txt','w+');
else
    fseek(splitlist,0,1);%puts line pointer at end of file to write
end
for lp =1:ic/2
    new_f = fopen([filename,'.',num2str(lp)],'w+');
    while ~feof(tmp_hdr)
        rline = fgets(tmp_hdr);
        fprintf(new_f,rline);
    end
   matfile = fopen(mnamelist(lp,:));
   
    while ~feof(matfile)
         mline = fgets(matfile);
         fprintf(new_f,mline);
    end
    fprintf(new_f,'%s\n','<filters_used>')

    while ~feof(tmp_fltr)
        rline = fgets(tmp_fltr);
        fprintf(new_f,rline);
    end
    
    frewind(tmp_hdr);
    frewind(tmp_fltr);
    fprintf(splitlist,'%s\n',[filename,'.',num2str(lp)]) 
     
end

%===========
%===========end make new lcd's

delete mat*
delete hdr*
delete tmp*

fclose all


back to C-OPS/CERBERUS Mfiles