function ApplyQc(infile)

%Open and Create all Files
[path filename ext]=fileparts(infile);
qcfilename=[path filename '_qc.nc'];
temp = netcdf(infile);
t0 = nctime(temp);
ncquit(temp);

qcfile=netcdf(qcfilename);
OK =1;

if ~isempty(qcfile)
   tqc = nctime(qcfile); 
   if length(tqc) ~= length(t0); % Check to see if the QC flag is he correct size
      ncquit(qcfile);
      OK = 0;
   end
   
else 
   OK = 0;
end

if ~OK % If qc file is not current or doesn't exist create a new one
   fprintf(1, 'Performing quality control on source data...');
   in=netcdf(infile);
   ep_qcflag(in);
   fprintf(1,'completed\n');
   qcfile = netcdf(qcfilename);
end

specprr=netcdf(infile);
qcdrawfilename=[path filename '_raw_qcd.nc'];
qcdrawfile=netcdf(qcdrawfilename,'clobber');
fprintf('Copying file')
qcdrawfile<specprr;
ncquit(qcdrawfile);%quit to save and update
ncquit(specprr);
delete (infile);
copyfile (qcdrawfilename,infile);
delete (qcdrawfilename);
qcdrawfilename=infile;
qcdrawfile=netcdf(qcdrawfilename,'write');%reopen



for i=1:length(var(qcfile));%cycle through qc vars
   
    lambda = name(qcfile{i});
  

 if strcmp(lambda(1:2),'qc')
    thevarn=lambda;
    lambda = lambda(3:5);%parse for wavelength
   
    
  	 
  
		if lambda == '412' es='_0_1';lu='_3_1';ed='_2_1';end;
      if lambda == '443' es='_0_2';lu='_3_2';ed='_2_2';end;%Some useful vars
      if lambda == '490' es='_0_3';lu='_3_3';ed='_2_3';end;%Some useful vars
      if lambda == '510' es='_0_4';lu='_3_4';ed='_2_4';end;%Some useful vars
      if lambda == '555' es='_0_5';lu='_3_5';ed='_2_5';end;%Some useful vars
      
      
      
      Esdqc=sprintf('Ed%s%s',lambda,es);
      Eddqc=sprintf('Ed%s%s',lambda,ed);
      Ludqc=sprintf('Lu%s%s',lambda,lu);
      
      Esvarnmirror=qcdrawfile{Esdqc}(:);
      Edvarnmirror=qcdrawfile{Eddqc}(:);
      Luvarnmirror=qcdrawfile{Ludqc}(:);
      
      index=find(qcfile{thevarn}~=0&qcfile{thevarn}~=2&qcfile{thevarn}~=4&qcfile{thevarn}~=6);
      Luvarnmirror(index)=-999;
 
   	
      index=find(qcfile{thevarn}~=0&qcfile{thevarn}~=1&qcfile{thevarn}~=4&qcfile{thevarn}~=5);
		Edvarnmirror(index)=-999;
   	
      
      index=find(qcfile{thevarn}~=0&qcfile{thevarn}~=1&qcfile{thevarn}~=2&qcfile{thevarn}~=3);
      Esvarnmirror(index)=-999;
      
      qcdrawfile{Esdqc}(:)=Esvarnmirror(:);
      qcdrawfile{Eddqc}(:)=Edvarnmirror(:);
      qcdrawfile{Ludqc}(:)=Luvarnmirror(:);
      
      
   end;
end;

v=var(qcdrawfile);
LuNonqcdind=find(qcdrawfile{'Lu412_3_1'}==-999&qcdrawfile{'Lu443_3_2'}==-999&qcdrawfile{'Lu490_3_3'}==-999&qcdrawfile{'Lu510_3_4'}==-999&qcdrawfile{'Lu555_3_5'}==-999);
EdNonqcdind=find(qcdrawfile{'Ed412_2_1'}==-999&qcdrawfile{'Ed443_2_2'}==-999&qcdrawfile{'Ed490_2_3'}==-999&qcdrawfile{'Ed510_2_4'}==-999&qcdrawfile{'Ed555_2_5'}==-999);   
EsNonqcdind=find(qcdrawfile{'Ed412_0_1'}==-999&qcdrawfile{'Ed443_0_2'}==-999&qcdrawfile{'Ed490_0_3'}==-999&qcdrawfile{'Ed510_0_4'}==-999&qcdrawfile{'Ed555_0_5'}==-999);
for i=1:length(v)
 if~isempty(qcdrawfile{i}.bank(:))
    if qcdrawfile{i}.bank(:)==3|qcdrawfile{i}.bank(:)==2|qcdrawfile{i}.bank(:)==0
       
     name2=ncnames(qcdrawfile{i});
     name2=char(name2);
     if qcdrawfile{i}.bank(:)==0
        if name2(1:2)=='Ed'|name2(1:2)=='PA'
        mirror=qcdrawfile{name2}(:);
        mirror(EsNonqcdind)=-999;
     	  qcdrawfile{name2}(:)=mirror(:);
     		end;

     else
     if name2(1:2)=='Lu'
        mirror=qcdrawfile{name2}(:);
        mirror(LuNonqcdind)=-999;
     	  qcdrawfile{name2}(:)=mirror(:);
     end;
       
     if name2(1:2)=='Ed'|name2(1:2)=='PA'
        mirror=qcdrawfile{name2}(:);
        mirror(EdNonqcdind)=-999;
     	  qcdrawfile{name2}(:)=mirror(:);
     end;
		end;
  	end;
 end;
end;


ncquit(qcdrawfile);
ncquit(qcfile);




   