SWAT output.rch文件中流域出口径流提取代码

%%this program is used for data dispose of the output file ouput.rch from the result of swat model
clc
clear
outlet=5;   %the outlet of of your watershed
%% The file path for the input and output file
file_in='E:\work\文章1_土地利用变化\land use change and its effects on streamflow\figure\HT\';
file_out='E:\work\文章1_土地利用变化\land use change and its effects on streamflow\figure\stremflow\';
file_name='up_output.rch';
%%  read file
fid=fopen(strcat(file_in,file_name),'r'); %open the input file
fidout=fopen('temp.txt','w');   %   creat temporary file 
while  ~feof(fid) %circulation will not finished if the pointer is not at the end of the file
            tline=fgetl(fid); %read the characters in a line; delete the line break; return -1 for the end of the file
      
              if ~isempty(tline)&& strcmp(tline(1),'R'); %the line can'b empty and with a character 'R' in the first location
                  tline(1:8)=[]; %delete the empty characters
                  fprintf(fidout,'%s\n',tline);  % write into the tempfile    
              end         
end
fclose(fid); 
%% extract the average month streamflow for the subbasin at the outlet 
temp=load('temp.txt');
% delete the data for years
id=find(temp(:,3)>1000); 
temp(id,:)=[];
% extract the stremflow at outlet
 id_outlet=find(temp(:,1)==5);
 sub_outlet=temp(id_outlet,:);
 outlet_month=zeros(12,1);
       for j=1:12
        id_month=find(sub_outlet(:,3)==j); %find the data of each month
        sub_moth=sub_outlet(id_month,:);
        sub_moth_avg=mean(sub_moth(:,6));
        outlet_month(j,1)=sub_moth_avg;
       end 
%% delete temporary file
      fclose('all');
      delete temp.txt; 
%% write out the file
xlswrite(strcat(file_out,'stream_up_HT.xls'),outlet_month);

 

Updated: 2014-11-24 — am11:02

Leave a Reply

  Subscribe  
Notify of