Zcorr.m

From Pnb
Jump to navigation Jump to search

function  zcorr(scale,offset)
%created by Erik Stassinos 3/3/10
%function to apply depth correction with slope and offset info from matlab
%program createdepth

 
Dirname =   pwd;
pattern = fullfile(Dirname,'*.lcd'); %gets only lcd extensions
directory = dir(pattern);
clear i
%make sure only the ayymmdd.lcd files are counted
isz =0;
for i=1:size(directory)
name = directory(i).name;
%isz= strncmp(name,'z',1)
% if isz ==1
%     sprintf('There seem to be z corrected files already here; they will be ignored')
%     
%    break
% end
list_lcd(i,:) = name

end
if isz ~=1
    clear list_lcd
end
for p = 1:size(directory);
    if isz ~=1
       directory(p).name
    list_lcd(p,:) = directory(p).name
    end
end

for j = 1:size(list_lcd)
    nlcd = strrep(list_lcd(j,:),'.lcd','.lcd'); %decided to create another file beacuse I have yet to figure out how to open original 
    nlcd = strcat('z',nlcd);
    %lcd file for writing without leaving old stuff on the end or
    %completely erasing the lcd info so it cannot be modified uppon open
    id = fopen(nlcd,'w+') ;%new lcd file
    fid = fopen(list_lcd(j,:),'r'); %old lcd file to be erased
   T=1;
    
        for u = 1:500 %arbitrary amount just to go over the <data> position
            r = ftell(fid);
            line = fgets(fid);
            r3 = ftell(fid)  ;
                if strncmp(line,'1depth',6) ==1
                fseek(fid,r,-1);
                d = textscan(fid,'%s %n %n %n',1);
                d{1};
                d{2};
                dep = char(d{1});
                fprintf(id,'%s %d %5.5f %5.5f %d %d\n',dep,d{2},scale,offset,d{3},d{4});%)'%5.5f %n %n %n',d{:,1:4})
                %fprintf(id,'%5.5f %5.5f\n',offset,scale)
                r2 = ftell(id);
                  T = 30000000; %if T matches a line number it will skip the line that number matches
                  fseek(fid,r3,-1);
                end
                %so it doesn't write the same line twice  for 1depth condition
               if T ~=30000000 
               fwrite(id,line);
               end
                T = T+1 ;     
                if strncmp(line,'<data>',5) == 1;
                break
                end    
        end
            fseek(fid,r,-1);
                     
                     s = strrep(list_lcd(j,:),'.lcd','.datmatrix');
                     m = load(s);
                     a = (m(:,22));  
                     a(1,1);
                     format long e
                       anew = (a - offset)/(scale);
                       anew(1,1);
                        %test break for first time
                       m(:,22) = (anew);
                       nm = strrep(list_lcd(j,:),'.lcd','.zcal');
                       dlmwrite(nm,m,'delimiter',' ','precision',6);
                       mid = fopen(nm);
                       frewind(mid);
                   
                     for o = 1:length(m); %writes new matrix lines to file
                       mline =  fgets(mid);
                       fprintf(id,mline);
                     end
                           fprintf(id,'%s\n','<filters_used>');
                           fprintf(id,'%s %5.5f %5.5f','recal',scale, offset);
       newac9list(j,:) = nlcd;

end
fclose(fid);
fclose(id);
dlmwrite('list_ac9',newac9list,'');
sprintf('done with recal')

zcorr