Indmovavg.m

From Pnb
Revision as of 16:03, 2 August 2010 by 128.111.101.185 (talk) (Created page with '<pre> function indmovavg() %created by Erik Stassinos %4/3/2010 %function to calculated moving independently of moving avg inside dspike %function. Both moving avg functions are…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
function indmovavg()
%created by Erik Stassinos
%4/3/2010
%function to calculated moving independently of moving avg inside dspike
%function.  Both moving avg functions are very similar

%moving avg for independent case call



%================================OPENS AND CREATES FILES===================

 
 flag = 5;
 width_2 = (flag)/2;
 width_2 = ceil(width_2);
 sum =0;
 cnt=0;
 trap=0;
 hs6 =0;
 
id = fopen('list_ac9dz');
if id == -1
    id = fopen('list_hs6_s')
    if id ~= -1
        hs6 = 1;
        optemp = fopen('optemp','w+');
    end
    if id == -1
    sprintf('CANNOT RUN INDEPENDENT MOVEAVG BECAUSE ITS DESIGNED TO BE RUN WITH SPLIT (list_ac9dz) FILE THAT CANNOT BE FOUND')
    return
    end
end
h1=0; 
while feof(id) ~=1 %used when making new list_ac9
    tu = fgets(id);
    h1 = h1+1;
 end
frewind(id)

  for i = 1:h1
    clear h;
     clear h;
    clear m
    clear m1
    clear matrix
    clear whole
    clear col mark dploc auglist whole derived loc r cnt sum row mavg field
      r = fgets(id);
            length(r);
            t = textscan(r,'%s');
            p = char(t{1,:});
   
if hs6 ~= 1           
            %oneortwo = textscan(r,'%c')
            oneortwo = p(1,12:16);
            if strncmp(oneortwo,'lcd.1',5) ==1
          pp1 = strrep(p,'.lcd.1','.lcd');
          m1 = strcat('desp1',pp1);%pp1)
          mat1 = load(m1,'.ASC');
            end
            if strncmp(oneortwo,'lcd.2',5) ==1
            pp1 = strrep(p,'.lcd.2','.lcd');
          m1 = strcat('desp2',pp1);%pp1)
          mat1 = load(m1,'.ASC');
            end  
end               
if hs6 ==1
        oneortwo = p(1,(10:14))
        if strncmp(oneortwo,'lcd.1',5) ==1
            pp1 = strrep(p,'.lcd.1','.datmat');
            m1 = strcat(pp1,'.1');%pp1)
            mat1 = load(m1,'.ASC');
        end
        if strncmp(oneortwo,'lcd.2',5) ==1
            pp1 = strrep(p,'.lcd.2','.datmat');
            m1 = strcat(pp1,'.2');%pp1)
            mat1 = load(m1,'.ASC');
        end
end
     matrix = mat1;
  if hs6~=1   
 fflag=0;        
 ogfile = fopen(p);
 file = strrep(p,'dz','mdz')
 nfile1 = fopen(file,'r+')
 if nfile1 == -1 %if this is the first time 
     fflag = 1; %if does not exist
 nfile1 = fopen(file,'w+')
 end
  end
  if hs6 ==1
      ogfile = fopen(p);
      file = strcat('m',p);
      fflag =1;
      nfile1 = fopen(file,'w+');
  end
totalpts = length(matrix(:,1));%in{2} - in{1};
%totalptsup = in{4} - in{3};
%===========================END OPENS AND CREATES==========================

%-----------gets length of one matrix line to get last 18 to despike-------
field = length(matrix(1,:));
    mark = field - 17;
  if hs6 ==1
      mark =1;
  end
  for col = mark:field 
      for row = 1:totalpts
          sum = 0;
          cnt = 0;
          if matrix(row,col) <-1000 || row <= width_2 || row > totalpts - width_2
              mavg(row,col) = matrix(row,col);
          else
              for m = (row - width_2):(row+width_2)
                  if matrix(m,col) < -1000
                  else
                      sum = sum + matrix(m,col);
                      cnt = cnt + 1;
                  end
              end
              if cnt ==0
                  mavg(row,col) = -9.9E35;
              end
              mavg(row,col) = sum/cnt;
          end
      end
  end

%=====================for getting oplist if files already dspiked
stuff = 0;
 check=0;
       while strncmp(stuff,'<derived_parameters>',20) ~=1
     stuff = fgets(ogfile);
          dploc = ftell(ogfile);
       end
 %=====================rest of list commands for already processed file
 u=0;
 t=0;
 count = 0;
 if hs6 ==1
        frewind(ogfile);
        while ~feof(ogfile)
            tline = fgets(ogfile);
            if strncmp(tline,'<sampled_parameters>',20) ==1
                break
            end
        end
 end
      command =0;
   while command ~= 1     
      check = fgets(ogfile);
      count = count +1;
      if strncmp(check,'<data>',6) == 1 
          break
      end
             t = t + 1;
              if t <=18
                  check(:,1:6);
                  t;
              oplist(t,1:6) = check(:,1:6);
              if hs6 ==1
                change = strcat('m-',check);%oplist(t,1:lenck))
                fprintf(optemp,'%s\n',change);
              end
              end
              if t>18
                  oplist(t,1:8) = check(:,1:8)';
                  if hs6 ==1   
                    if  strncmp(check,'<derived_parameters>',20) == 1
                        break
                    end
                change = strcat('m-',check);%oplist(t,1:lenck))
                fprintf(optemp,'%s\n',change);
                 end
              end
                 if strncmp(check,'d-d-',4) ==1
                     u = u+1;
                  auglist(u,:) = strcat('m-',check(:,1:8));
                 end
                 if hs6 == 1 
                    command = strncmp(check,'<derived_parameters>',20);
                 end
                    command = strncmp(check,'<data>',6);
   end
      cnt = count;
     if hs6 ~=1 
  if length(oplist(:,1)) <= 18
      clear u
      for u = 1: length(oplist(:,1))
          auglist(u,:) = strcat('m-',oplist(u,1:6));
      end
  end
     end

%-------------------------make out matricies loop----------------------
%----------------------------------------------------------------------                     
                            if fflag ==1
                             whole = zeros(totalpts,field+18);
                            end
                            
%for up segments
for c =1:length(matrix(1,:)); %for all 58 columns
     for k= 1:length(matrix(:,1))%for all the rows in downcastmatrix
             whole(k,c) = matrix(k,c); %puts origina stuff in for first 41 columns
     end 
end
for r = 1:length(mavg(:,1))%totalptsdn %will skip last line because difference is one line less; it needs two lines
    count = 0;
    for c =mark:field%fielddn:fielddn+18;%length(diffdn(1,:))
        count = count+1;
        whole(r,field+count) = mavg(r,c);
    end
    clear count
end
     dlmwrite(strcat('movam',p),whole,'delimiter',' ','precision',6);


%--------------------------------------------------------------------------
%--------------------------end make out loop-------------------------------


%------------writes down to derived parameters
ogline = 0; %trip into loop
%if fflag ==1
    lock = ftell(ogfile);
    frewind(ogfile);
while strncmp(ogline,'<derived_parameters>',20) ~=1
            ogline = fgets(ogfile);
            fwrite(nfile1,ogline);
            derivedloc = ftell(nfile1);
end
%
%------------loop to print oplist or aug after derived parameters-----------------
if hs6 ==1
        frewind(optemp)
        for ct = 1:cnt -1;
            op = fgets(optemp);
            fprintf(nfile1,'%s',op);
        end
end
if hs6 ~=1
if fflag ==1 %if working with old file
     fseek(nfile1,derivedloc,-1);
      for j = 1:length(oplist) 
      fprintf(nfile1,'%s\n',oplist(j,:));
%      fprintf(nfile2,'%s\n',oplist(j,:));
      end
     for k = 1:length(auglist)
         fprintf(nfile1,'%s\n',auglist(k,:));
       %  fprintf(nfile2,'%s\n',auglist(k,:));
     end
end
end
     fprintf(nfile1,'<data>\n');
     ndatloc = ftell(nfile1);

%-----------end loop to print oplist---------------------------------------


%==============part that actually writes matrix to lcd file=============
 
     min = fopen(strcat('movam',p));
 %working with new file
 if fflag ==1
 for o= 1:length(mavg(:,1))%totalptsdn
     ln = fgets(min);
    fprintf(nfile1,ln);
 end
 end
 fclose(min);
 %==============end write matrix to lcd ===================================

 
 %-----------notes filters that have already been used
frewind(ogfile);

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');
              %  fprintf(nfile2,'<filters_used>\n');
                 % fseek(ogfile,loc1,-1)
                 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);
               %        fwrite(nfile2,fil);
                       eval(['filter',num2str(count) '= fil']);
                 end
             end
end
%-----------end notes
 
%-----------add to filters the indmovavg filters

if hs6 ~=1
if (length(oplist) - 18) > 1

for b = 19:length(oplist);
fprintf(nfile1,'%s', 'indmovavg ');
fprintf(nfile1,'%s',oplist(b,:));
fprintf(nfile1,'%s %s\n', 'f',p);
end
end
if length(oplist) - 18 <1
    for b = 1:length(oplist);
fprintf(nfile1,'%s', 'indmovavg ');
fprintf(nfile1,'%s',oplist(b,:));
fprintf(nfile1,'%s %s\n', 'f',p);
    end
end
end    
if hs6 == 1
    frewind(optemp)
        for ct = 1:cnt -1;
            op = fgets(optemp);
            fprintf(nfile1,'%s', 'indmovavg ');
            op = strcat(op,' ');
            fprintf(nfile1,'%s\n',[op ,' ', p]);  
        end
end

mark = ftell(id);
%-------make new list file
if fflag ==1
   frewind(id); 
h=0;
r =1;  
%while feof(id) ~=1
if hs6 ~=1
for po=1:h1 %-1
    r = po%-1;
    ac9 = fgets(id);
    t =  textscan(ac9,'%s');
        newlst = char(t{1,:});
        newlst = strrep(newlst,'dz','mdz');
        newl(r,:) = newlst; %strrep(newlst,'.lcd','.lcd.1');
        %newl(h,:) = strrep(newlst,'.lcd','.lcd.2');
        newl; 
end
end
if hs6==1
        for po=1:h1 %-1
            %h = h+2;
            r = po;%-1;
            var = fgets(id); %parameter for list
            t =  textscan(var,'%s');
            newlst = char(t{1,:});
            newlst = strcat('m',newlst);
            newl(r,:) = newlst; %strrep(newlst,'.lcd','.lcd.1');
            %newl(h,:) = strrep(newlst,'.lcd','.lcd.2');
            newl;     
        end
end
end
fseek(id,mark,-1);
  end
if hs6 ==1
    dlmwrite('list_hs6_m',newl,'');
end
if strncmp(p,'dz',2) ==1
    dlmwrite('list_ac9mdz',newl,'');
end
if hs6 ==1
  delete optemp
end
disp('done with indmovavg')

indmovavg