E BBstrip.m

From Pnb
Revision as of 14:51, 5 August 2010 by 128.111.101.185 (talk) (Created page with '<pre> function out = E_BBstrip(pb,infile,varargin)%(,infile) %created by Erik Stassinos 2/22/10 %created to replace bbopstrip %this program takes input parameters and the pb numb…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
function out = E_BBstrip(pb,infile,varargin)%(,infile)
%created by Erik Stassinos 2/22/10
%created to replace bbopstrip
%this program takes input parameters and the pb number 
%program locates corresponding column and row positions and writes data to
%output file which is same as input whith .strip added

%start in processing dir

%infile = 'a110211a.dat.lcd'

%cd(pb)
clear s
it =0;
t=0;
tfound =1;
hs6proc =0;
%dir *a110211*.dat
%infiles = ls(['a110211a'*'.dat'])
%=================test to see if varargin has one file or list of params
vartest = fopen(varargin{1});
%=================
%--------------------------------------------------------------------------

Dirname =   pwd;
pattern = fullfile(Dirname,'*.lcd')
directory = dir(pattern);
directory(1).name;
directory(2).name;
size(directory);
%ignores other .lcd extensions and goes for original if it's been processed
%already
for r = 1:size(directory);
    if strncmp(directory(r).name,'z',1) ==1
        continue
    end
    if strncmp(directory(r).name,'d',1) == 1
        continue
    end
    lcdflist(r,:) = directory(r).name;
end

lcdfile = infile
s = strrep(lcdfile,'z',''); %changes name to open data matrix
s = strrep(s,'.lcd','.zcal');

if vartest == -1
    if varargin{2} == 'i'
        t = 1;
    end
end

if length(varargin) >2
    if varargin{3} == 'i'
        t=1;
    end
end

if length(varargin) >2 || t == 1 %varargin{2} == 'i'
    %if  varargin{2} == 'i' || varargin{3} == 'i'
    
    if t ==1
        sprintf('i is true'); %run independently
        it =1;
        s = strrep(s,'.zcal','.lcd');
        s = strrep(lcdfile,'.lcd','.datmatrix');
        %s = strrep(lcdfile,'dat.lcd','.datmatrix')
    end
end
%if taking in a file that has a list it assumes that it's being run by
%merge_ctdac9
if vartest ~=-1
    s = strrep(lcdfile,'bmd','binmatmd');
end
if strncmp(varargin{1},'hydro_time',10) == 1
    hs6proc =1
    s = strrep(infile,'.lcd','.datmat');
end

fp = load(s,'.ASC');
in = fopen(lcdfile);
linecount = 1;

%--------------------------------------------------------------------------
%if input is a file instead of a parameter it gets number of params and
%loads
looplength = length(varargin);
countvar =1;
if length(varargin) == 1 && vartest ~=-1
    var = fopen(varargin{1});
    while feof(var) ~= 1
        fgets(var);
        countvar = countvar + 1;
    end
    frewind(var);
    for i = 1:countvar
        varargin{i} = fgets(var);
    end
    looplength = countvar  ;
end


%--------------------------------------------------------------------------
%------------------------------gets arguments position----------------------
i=1;
if it == 1 %varargin{3} == 'i')%length(varargin) ==3
    looplength = length(varargin) -1;
end
if vartest ~=-1
    varcounter = 1;
    frewind(vartest);
    while feof(vartest) ~=1
        tline = fgets(vartest);
        tline = strtrim(tline);
        vararg{varcounter,:}  = tline; %fgets(vartest)
        varcounter = varcounter +1;
    end
    tfound = 0;
    found =0;
    looplength = varcounter -1;
end

ic =0; %initialize variable for hs6 loop counter
found =0;
while i < looplength +1 %length(varargin)+1 %because while statement, has to be one over the actual <
    
    while ~feof(in) %end of file tester
        l = fgets(in);
        if linecount == 2
            head = l;
        end
        if linecount ==3 && tfound ~= 1 %so not to interfere with head location just skips to third line
            threepos = ftell(in);
        end
        linecount = linecount +1; %keeps counting lines until it's found the first thing looking for
        if vartest == -1 && hs6proc ~=1
            if strncmp(l,varargin{i},length(varargin{i})) ==1;
                column = (linecount -16);
                % val = column;
                eval(['locin',num2str(i) '= column']) ;
                break
            end
        end
        
        if vartest == -1 && hs6proc ==1
            if strncmp(l,varargin{i},length(varargin{i})) ==1;
                if strncmp(l,varargin{1},length(varargin{i})) ==1;
                    prevleng = linecount %sets first part of params to one.  takes in total lines before first param
                    pl = 1;
                end
                column = (linecount - prevleng);
                if pl ==1
                    column = (linecount -prevleng +1); %used to be 38 for latest HS6 processing
                end
                eval(['locin',num2str(i) '= column']) ;
                break
            end
        end
        
        
        if vartest ~=-1 %for processing of list file
            if strncmp(l,'<derived_parameters>',20) ==1 %line does not count
                linecount = linecount -1;
            end
            if strncmp(l,vararg{i},length(vararg{i,:})) ==1
                
                % disp('=======in here for column assgn++++++++')
                
                %Because there are some descrepencies between the zeroth column as seen by the computer and
                %the 1st column seen by matlab some offsets need to be done in order to get
                %the correct column.  Also, because there are section header < > neglected
                %it is important to remove those from the column count.
                
                
                %first header doesn't count but sampled and derived do for first
                %run through
              
                column = linecount -20;
                if found == 0
                    column = linecount -21;
                end
                found = found +1;
                if found < looplength
                    fseek(in,threepos,-1);
                    linecount =3; %goes to third line and starts fresh
                    tfound = 1;
                    %clear column
                end
                break
            end
        end
    end
    
    x(:,i) =  fp(:,column);%places column data in to columns in x
    
    if column ==22 && vartest ==-1 && hs6proc ~=1
        x(:,i) = -1*x(:,i);
    end
    if hs6proc ==1
        if column ==2
            x(:,i) = -1*x(:,i);
        end
    end
    i = i +1;
    
end

%--------------------------------end gets----------------------------------
%--------------------------------------------------------------------------

%cd('processing')
if length(varargin) == 2
    output = fopen(strrep(lcdfile,'.lcd','.lcd.strip'),'w+');%creates .lcd.strip
end
if length(varargin) > 2
    if it == 1  %varargin{3} =='i'
        %delete output
        % cd('processing')
        pwd
        output = fopen(strrep(lcdfile,'.lcd','.lcd.strip'),'w+')
        if varargin{3} == 'i'
            %cd ..
        end
    end
end

if vartest ==-1
    output = fopen(strcat(lcdfile,'.strip'),'w+')
end
if vartest ~= -1 
    output = fopen(strcat(lcdfile,'.strip'),'w+')
end
if vartest == -1
    fprintf(output,'%s','#',head);
    if length(varargin) >2
        for i = 1:length(varargin) -1
            fprintf(output,'%s %s\n','#',varargin{i});
        end
    else    
        for i = 1:length(varargin)
            fprintf(output,'%s %s\n','#',varargin{i});
        end
    end
    for t = 1:length(x);
        fprintf(output,'%f %f\n',x(t,:));
    end
end

%need to create temporary output matrix because matlab sucks at string
%formating for columns
if vartest ~=-1
    for cfoy = 1:length(x(:,1))
        for cfox = 1:length(x(1,:));
            p(cfoy,cfox) = x(cfoy,cfox);
        end
    end
    if strncmp(lcdfile(1,length(lcdfile)),'2',1) ==1
                lcdfile(1,length(lcdfile));
    end
    
    dlmwrite('temp_merge',p,'delimiter',' ','precision',6);
    tm = fopen('temp_merge','r');
    fprintf(output,'%s','#',head);
    for i = 1:length(vararg)
        fprintf(output,'%s%s\n','#',vararg{i});
    end
    while ~feof(tm)
        tml = fgets(tm);
        fprintf(output,'%s\n',tml);
    end
    delete temp_merge
end

sprintf('done with E_BBstrip')


fclose(in);
fclose(output);

E_BBstrip