CPSangQ

From Pnb
Revision as of 14:23, 13 February 2014 by Eriks (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

function CPSangQ(infile,channel_1,channel_2,channel_3,channel_4) %Erik Stassinos 6/7/2011
%This function flags data row with "2" that is over the set angle value
%qrange or flags data rows with a "0" if channels are within qrange.
%qrange is 10 
%Channels 3 and 4 are radiance/irradiance channels to check if dark cast


%===SET PARAMETERS===

infid = fopen(infile,'r');
qflag =2;
qrange = 10;


%===MAKE DATA FILE FOR INPUT FILE===


frewind(infid)
dataname = strcat('AQCmat',infile);
dataname = strrep(dataname,'.lcd','');

datafid = fopen(dataname,'w');
while ~feof(infid)
    testline = fgets(infid);
    if strncmp(testline,'<data>',6) ==1 %start of data section
        while strncmp(testline,'<filters_used>',14) ~=1
            testline = fgets(infid);
              if strncmp(testline,'<filters_used>',14) ==1
                  break
              end
            fprintf(datafid,testline);
        end
    end
end
fclose(datafid);
frewind(infid);



%===FIND CHANNEL 1 and 2 INDEXES===
lineind =0;
while ~feof(infid)
     line = fgets(infid);
                 if strncmp(line,'<sampled_parameters>',20) ==1
                     while strncmp(line,'<data>',6) ~= 1
                         lineind = lineind +1;
                         line = fgets(infid);
                         if strncmp(line,'<derived_parameters>',20) == 1
                              lineind = lineind -1;
                         end
                         if strncmp(channel_1,line,5) ==1
                         chanind1 = lineind;
                         end
                     end
                 end
end
chanind1;
frewind(infid);
lineind=0;
while ~feof(infid)
     line = fgets(infid);
                 if strncmp(line,'<sampled_parameters>',20) ==1
                     while strncmp(line,'<data>',6) ~= 1
                         lineind = lineind +1;
                         line = fgets(infid);
                         if strncmp(line,'<derived_parameters>',20) == 1
                              lineind = lineind -1;
                         end
                         if strncmp(channel_2,line,5) ==1
                         chanind2 = lineind;
                         end
                     end
                 end
end
chanind2;

%===FIND CHANNEL 3 and 4 INDEXES FOR DRK CAST CHECK===

frewind(infid)
lineind=0;
while ~feof(infid)
     line = fgets(infid);
                 if strncmp(line,'<sampled_parameters>',20) ==1
                     while strncmp(line,'<data>',6) ~= 1
                         lineind = lineind +1;
                         line = fgets(infid);
                         if strncmp(line,'<derived_parameters>',20) == 1
                              lineind = lineind -1;
                         end
                         if strncmp(channel_3,line,5) ==1
                         chanind3 = lineind;
                         end
                     end
                 end
end
chanind3;
frewind(infid)
lineind=0;
while ~feof(infid)
     line = fgets(infid);
                 if strncmp(line,'<sampled_parameters>',20) ==1
                     while strncmp(line,'<data>',6) ~= 1
                         lineind = lineind +1;
                         line = fgets(infid);
                         if strncmp(line,'<derived_parameters>',20) == 1
                              lineind = lineind -1;
                         end
                         if strncmp(channel_4,line,5) ==1
                         chanind4 = lineind;
                         end
                     end
                 end
end
chanind4;

data2 = load(dataname);
fd4 = find(data2(:,chanind4) > -9.9E35);
fd3 = find(data2(:,chanind3) > -9.9E35);
mean(data2(fd3,chanind3))
mean(data2(fd4,chanind4))
%keyboard

%===DO QUALITY CONTROL AND REPLACE DATA WITH FLAG IF NEEDED===

QCdata = load(dataname);
checkcol_1 = QCdata(:,chanind1);
checkcol_2 = QCdata(:,chanind2);
checkcol = checkcol_1;
y = size(QCdata,1);
x = size(QCdata,2);
QCdata_new = zeros(y,x+1);
QCdata_new(1:y,1:x) = QCdata;
size(QCdata_new);
size(QCdata);
QCdata_new(:,end) = checkcol;


%try qc another way

for i = 1:length(checkcol_1);
    if abs(checkcol_1(i)) > qrange || abs(checkcol_2(i)) > qrange
%         disp('Tilt')
%         checkcol_1(i)
%         disp('Roll')
%         checkcol_2(i)
        QCdata_new(i,end) = qflag;
    end
    if abs(checkcol_1(i)) < qrange && abs(checkcol_2(i)) < qrange
        QCdata_new(i,end) =0;

    end
end

%original = load('akrmP101216B_nhd.txt');
dlmwrite(dataname,QCdata_new,'delimiter',' ','precision',6); %overwrite old matrix file

%===INSERT NEW DATA INTO NEW LCD===

frewind(infid);
qclcdname = strcat('an',infile);
qclcd = fopen(qclcdname,'w+');

while ~feof(infid)
       pos = fgets(infid);
       if strncmp(pos,'<data>',6) ==1 %start of data section
        break
       end
       fprintf(qclcd,pos);
       
end
%===INSERT NEW DERIVED PARAMS===
newparam = strcat('aq-',channel_1,'-',channel_2);
fprintf(qclcd,'%s\n',newparam);
fprintf(qclcd,'%s\n','<data>');

datafile = fopen(dataname);
 while ~feof(datafile)
   %for loop = 1:length(QCdata_new)
        datln = fgets(datafile);
        fprintf(qclcd,datln);
 end

%===ADD TO FILTERS USED ON NEW LCD===

while ~feof(infid)
       lcdline = fgets(infid);
       if strncmp(lcdline,'<filters_used>',14) ==1
           fprintf(qclcd,'%s\n','<filters_used>');
           while ~feof(infid)
               filter = fgets(infid);
               fprintf(qclcd,'%s',filter);
           end
       end
 end
       %domathstring =strcat(['domath',type,channel1,channel2,num2str(number),outfieldname]);
       %fprintf(newlcdfile,'%s\n', domathstring);
       fprintf(qclcd,'%s\n','')
       fprintf(qclcd,'%s','angQ ');
       fprintf(qclcd,'%s',' ');
       fprintf(qclcd,'%s',channel_1);
       fprintf(qclcd,'%s',' ');
       fprintf(qclcd,'%s',channel_2);
       fprintf(qclcd,'%s',' ');
       fprintf(qclcd,'%s',num2str(qrange));
       fprintf(qclcd,'%s',' ');
       fprintf(qclcd,'%s',num2str(qflag));
       
        if strncmp(infile(1:2),'an',2) == 1
        
           movefile(qclcdname,infile)
        end
       

fclose('all');




back to C-OPS/CERBERUS Mfiles