Binner.m

From Pnb
Revision as of 16:04, 2 August 2010 by 128.111.101.185 (talk) (Created page with '<pre> function binner() %created by Erik Stassinos %4/7/2010 %this is a binning script to cycle through each row and column %counts and outputs the number of bins it's made %loop…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
function binner()
%created by Erik Stassinos
%4/7/2010
%this is a binning script to cycle through each row and column
%counts and outputs the number of bins it's made
%loops through depth data until in finds a depth value greater or equal to
%the bin end, since bin end is the binsize(1m) plus .5 it finds a value
%greater or equal to .5
%when it finds this depth value it sets the window min to start and window
%max to the counter
%then is loops from min to max windows and looks for trapflag(-9.9E35)
%n = window max - window min + 1 the it gets mean by if trapflag found mean
%= value at trapflag, if n = 0 set trapflag, mean = mean plus data at
%location, then mean = mean over n.
%then adds values to end of rows plus bin count and n value
%clear all
%latest



%% always finds a list file to open
%==============--------loading stuff-----=================
%upcast =0;
downcast =0;
error =0;
format short e
%cd(pb) %goes to correct dir
error =0;
testbin = fopen('list_ac9bmdz')
        if testbin ~= -1
            warning('A list file suggesting binning has already been done exhists')
            disp('Binned files should not be reprocessed.  If files are not binned but list is present, continue.')
            answer = input('do you want to continue anyways? y or n: ','s')
            if strncmp(answer,'n',1) == 1
                return
            end
        end
%id = fopen('list_ac9')
id = fopen('list_ac9mdz');
if id == -1
   % sprintf('running from dspike list, no movavg')
    error =1;
    id = fopen('list_ac9dz');
    if id == -1
    %    sprintf('running from recal list, no dspike');
        error =2;
        id = fopen('list_ac9')
        idtest = fgets(id);
        if id == -1 || idtest == -1
            id  = fopen('list_hs6_m');
            error = 3;
            if id == -1
                sprintf('Cannot find the list_ file, exiting')
                return
            end
        end
    end
end

h1=0;
while feof(id) ~=1 %used when making new list_ac9
    tu = fgets(id);
    h1 = h1+1;%counts number of files in list file
end
frewind(id);

for i = 1:h1%
    %%%%%initialize%%%%%
    upcast=0;
    clear h matrix m1 start start_init first bin_end;
    winmin =0;
    winmax =0;
    dflag=0;
    %error =0;
    bin_end =0;
    n=0;
    themean=0;
    %%%%%%%%%%%%%%%%%%%
    r = fgets(id);
    length(r);
    t = textscan(r,'%s');
    p = char(t{1,:}) %converts filenam%properly
    %p = 'mdza110211x.lcd.1'
    %p = p;
    if error ~= 3  
       oneortwo = p(1,13:17);
       % oneortwo = p(1,12:16); <<===for testing with other format
    end
    if error ==3
        oneortwo = p(1,(11:15));
    end
    if strncmp(oneortwo,'lcd.1',5) ==1
        downcast =1;
        if error == 1
            m1 = strrep(p,'.lcd.1','.lcd');
            m1 = strcat('desp1',m1);
            %mat1 = load(m1,'.ASC');
        end
        if error == 2
            desp('why here');
            m1 = strrep(p,'z','');
            m1 = strrep(m1,'.lcd','.zcal');
        end
        if error == 3
            m1 = strcat('mova',p);
        end
       if error == 0
           m1 = strcat('mova',p);
       end
 
       mat1 = load(m1,'.ASC');
       matrix = mat1;   
    end
    if strncmp(oneortwo,'lcd.2',5) ==1
        upcast =1;
        if error == 1
            m1 = strrep(p,'.lcd.2','.lcd');
            m1 = strcat('desp2',m1);
           % mat1 = load(m1,'.ASC');
        end
        if error ==2
            m1 = strrep(p,'z','');
            m1 = strrep(m1,'.lcd','.zcal');
        end
        if error == 3
            m1 = strcat('mova',p);
        end
        
        pis = p;
        if error ==0
        m1 = strcat('mova',p);
        end
        mat1 = load(m1,'.ASC');
        matrix = mat1;
    end
    
    if upcast ==1%---------important flips if it's an upcast matrix
        matrix = flipud(matrix);
    end
    %=========----------end loading stuff-----==================
    
    %initializing stuff again
    %setting bin dimensions
    binsize =1; %aka "the bin" %changed from 1
    bin_number =0;
    delta =0;
    winmax =0;
    winmin =0;
    %start_init =1; %initial start position is row 1
    bincol_ac9 = 22;
    bincol_hs6 =2;
    
    
    if error ==3
        bincol = bincol_hs6;
    else
        bincol = bincol_ac9;
    end
    start_init = matrix(1,bincol);
    if start_init > 1
        start_init = floor(start_init);
    end
    if start_init < 1
        start_init = abs(start_init);
        start_init =  ceil(start_init);
    end
    start = start_init; %initial setting for start is the first row
    q = 1;
    first  = 0;
    tflag =0;
    meanmat =[];
    interv =0;
    
    %=================start of major loop==============================
    %==================================================================
    
    while q <= length(matrix(:,1))% test for small amount
        %if downcast ==1
        if start_init <= start && start <= length(matrix(:,1))
            %============internal original"downcast"=======
            if q ==1 %for first time around
                bin_end = binsize +.5;
                first = bin_end;
                bin_number =0;
            else
                first = bin_end;
            end
            if q ~= 1
                bin_end = first + 1;%bin_end;%binsize;
            end
            
            %===============
            for h = start:length(matrix(:,bincol)) %length of column 22
                dflag =0;
                delta = abs(matrix(h,bincol)); %delta value is absolute of depth value
                if delta >= bin_end
                    winmax = h - 1;
                    winmin = start;
                    dflag =1;
                    interv = interv +1;
                    break
                end
            end
            %=============
            themean =0;
            winmax;
            winmin;
            n = winmax - winmin +1;
            for c = 1:length(matrix(1,:))
                for r = winmin:winmax 
                    if matrix(r,c) < -8.9E35
                        tflag =1;
                    end
                    format long e
                    themean = themean + matrix(r,c);
                end
                if n ==0
                    format short e
                    themean = -9.9E35;
                else
                    if tflag ==1
                        format short e
                        themean = -9.9E35;
                    %end
                    else
                  themean = themean/n;
                    end
                end
                
                %counts how many times the mean is taken, should be 95 per row
                meanmat(interv,c) = themean; % write compact mean matrix.
                
                if c == length(matrix(1,:))
                    meanmat(interv,c+1) = interv;
                    meanmat(interv,c+2) = n;
                    
                end
                % if meancount == length(matrix(1,:)) %if it's done all 95 columns increase counter and write
                %row = row +1
                %end
                themean =0; %resets at end of loop
                tflag =0;
            end
            
            %==============end internal "downcast"==========
            q = q+1; %update counter for size of matrix
            start = h;
            bin_number = bin_number + binsize;
        end
    end

    %==============================================================
    %===============end of major loop==============================
    if upcast ==1
       % meanmat = flipud(meanmat);
    end
    dlmwrite(strcat('binmat',p),meanmat,'delimiter',' ','precision',6); %puts binner in name and writes new mean matrix
   
    %=========================================================================
    %=========================================================================
    
    %-------------------for writing%file------------------------------------
    fflag=0;
    ogfile = fopen(p);
    if error == 0
        file = strrep(p,'mdz','bmdz');
    end
    if error ==1
       file = strcat('b',p);
    end
    if error ==3
        file  = strcat('b',p);
    end
    nfile1 = fopen(file,'r+');
    if nfile1 == -1 %if this is the first time
        fflag = 1; %if does not exist
        nfile1 = fopen(file,'w+');
    end
    %------------writes down to derived parameters
    ogline = 0; %trip into loop
    %if fflag ==1
    frewind(ogfile);
    while strncmp(ogline,'<data>',6) ~=1
        datloc = ftell(nfile1);
        ogline = fgets(ogfile);
        fwrite(nfile1,ogline);
        derivedloc = ftell(nfile1);
        
    end
    %------------end
    %------------insert bin parameters-----
    fseek(nfile1,datloc,-1);
    fprintf(nfile1,'%s\n','bin#_1.0_1depth')
    fprintf(nfile1,'%s\n','pts/bin')
    fprintf(nfile1,'%s\n','<data>')
    datainsert = ftell(nfile1);
    %==============part that actually writes matrix to lcd file=============
    
    min = fopen(strcat('binmat',p));
    %   minup = fopen(strcat('desp2m',p));
    %working with new file
    if fflag ==1
        fseek(nfile1,datainsert,-1);
        for o= 1:length(meanmat(:,1))%totalptsdn
            ln = fgets(min);
            fprintf(nfile1,ln);
        end
    end
    %-----------notes filters that have already been used
    frewind(ogfile); %makers sure it will catch the filters_used in ogfile
    %frewind(nfile1)
    
    while feof(ogfile)~=1
        loc1 = ftell(ogfile);
        lnprev = fgets(ogfile);
        if strncmp(lnprev,'<filters_used>',14) == 1 %tests for filters used location
            fprintf(nfile1,'<filters_used>\n'); %writes filters used in same loc in new file
            fil = 0;% trip into loop
            count =0;
            while feof(ogfile) ~=1 %makes variable to hold each filter
                count = count +1;
                fil = fgets(ogfile);
                fwrite(nfile1,fil);
            end
        end
    end

    %-----------end notes
    %adds bin function to filters
    fprintf(nfile1,'%s', 'binner ');
    fprintf(nfile1,'%s','1 ');
    fprintf(nfile1,'%s',' ');
    fprintf(nfile1,'%s\n',p);
    if error ~=3
        demfile = strrep(p,'mdz','bmdz');
    end
    if error ==3
        demfile = file;
    end
    deemmer(demfile);
    
    %makes new list
    %frewind(id)
    % for po=1:h1 %-1
    % %h = h+2;
    %     r = po%-1;
    %     ac9 = fgets(id);
    %    t =  textscan(ac9,'%s');
    %       newlst = char(t{1,:});
    if error ~=3
        newlst(i,:) = strrep(p,'mdz','bmdz'); %%%%%%%%%%%%%%%%%%%%%%
    end
    if error ==3
        newlst(i,:) = strrep(p,'m','bm')
    end
    clear themean
    clear n
    clear tflag
    clear demfile nfile1 loc1 matrix tflag interv tflag r winmax winmin c
    clear meanmat start delta bin_end q ln datloc ogline fflag h bin_number 
    clear start_init  

end

if error ~= 3
    dlmwrite('list_ac9bmdz',newlst,'');
end
if error ==3
    dlmwrite('list_hs6_bm',newlst,'');
end
disp('done with binner')



binner