clear all 

% global settings
PREFIX='CMIP6_MIP_tables_v1.00.24_20180514';
PRIORITY=true;

% read work sheets and concatenate
fid=fopen([PREFIX '.mat'],'r');
if fid<0
  for s=2:44 
    s
    [dummy,TXT]=xlsread([PREFIX '.xlsx'],s,'A2:AD100');
    if s==2
      data=TXT;
    else
      data=cat(1,data,TXT);
    end
  end 
  save([PREFIX '.mat'],'data'); 
else
  fclose(fid); 
  load([PREFIX '.mat']);
end 

% filter MIPs 
mipsraw=data(:,5);
mips=cell(0);
count=0; 
for n=1:numel(mipsraw) 
  str=char(mipsraw(n)); 
  if ~isempty(str)
    ind=findstr(str,','); 
    if isempty(ind) 
      count=count+1;
      mips(count)={str}; 
    else 
      count=count+1;
      mips(count)={str(1:(ind(1)-1))}; 
      for k=1:(numel(ind)-1) 
        count=count+1;
        mips(count)={str((ind(k)+1):(ind(k+1)-1))}; 
      end
      count=count+1;
      mips(count)={str((ind(end)+1):end)}; 
    end 
  end 
end 
mips=unique(sort(mips));
for imip=0:numel(mips)  
  if imip==0 
    mip_name='Any'; 
    datasub=data;
  else 
    mip_name=char(mips{imip});
    datasub=data(find(contains(data(:,5),mip_name)),:);
  end 

  % create worksheet 
  if imip==0 
    worksheet={'Priority','Component','CMIP name','NorESM name or implementation status',...
      'CMOR implementation status','MIPs','Frequencies','Long name','Description','Units'}; 
  else
    worksheet={'Priority','Component','CMIP name','NorESM name or implementation status',...
      'CMOR implementation status','Frequencies','Long name','Description','Units'}; 
  end 

  % filter maximum priority 
  if PRIORITY 
    maxprio=3;
  end
  for priority=1:maxprio

    % filter type  
    typesraw=datasub(:,16);  
    types=cell(0);
    count=0;
    for n=1:numel(typesraw)
      str=char(typesraw(n));
      if ~isempty(str)
        ind=findstr(str,' ');
        if isempty(ind)
          count=count+1;
          types(count)={str};
        else
          count=count+1;
          types(count)={str(1:(ind(1)-1))};
          for k=1:(numel(ind)-1)
            count=count+1;
            types(count)={str((ind(k)+1):(ind(k+1)-1))};
          end
          count=count+1;
          types(count)={str((ind(end)+1):end)};
        end
      end
    end
    types=unique(sort(types)); 
    for itype=1:numel(types)
      type_name=char(types{itype});
      datasub2=datasub(find(contains(datasub(:,16),type_name)),:);

      % filter variable 
      vars=unique(sort(datasub2(:,1)));
      for ivar=1:numel(vars)
        var_name=char(vars(ivar));
        
        datasub3=datasub2(strmatch(var_name,datasub2(:,1),'exact'),:);
        if PRIORITY & priority ~= min(str2num(char(datasub3(:,4))))
          continue; 
        end 

        % extract NorESM name 
        noresm_name=' '; 
        for n=1:size(datasub3,1) 
          str=char(datasub3(n,2));
          if isempty(str) | findstr(str,'implement')>0 
            continue; 
          end 
          noresm_name=str; 
          break; 
        end 
 
        % extract CMOR implementation status
        cmor_status=' ';
        for n=1:size(datasub3,1) 
          str=char(datasub3(n,3));
          if isempty(str)
            continue; 
          elseif strcmp(str,'yes') 
            cmor_status=str; 
            break 
          elseif findstr(str,'for')>0 
            cmor_status='partly'; 
          else  
            cmor_status='no'; 
            break
          end
        end 

        % extract frequencies 
        tmp=unique(sort(datasub3(:,17))); 
        for n=1:numel(tmp) 
          if n==1 
            frequencies=char(tmp(n));
          else 
            frequencies=[frequencies ',' char(tmp(n))];
          end 
        end 

        % extract long name, units, description
        long_name=char(datasub3(1,6)); 
        units=char(datasub3(1,8)); 
        description=char(datasub3(1,9));

        % extract MIPs 
        mip_names=' ';
        if imip==0 
          for n=1:size(datasub3,1)
            str=char(datasub3(n,5));
            if ~isempty(str) 
              if n==1 
                mip_names=str;
              else  
	        mip_names=[mip_names ',' str]; 
              end 
            end  
          end 
          mips_var=cell(0); 
          ind=findstr(mip_names,',');    
          count=0;
          if isempty(ind) 
            mips_var(1)={mip_names}; 
          else
            count=count+1;
            mips_var(count)={mip_names(1:(ind(1)-1))};
            for k=1:(numel(ind)-1)
              count=count+1;
              mips_var(count)={mip_names((ind(k)+1):(ind(k+1)-1))};
            end
            count=count+1;
            mips_var(count)={mip_names((ind(end)+1):end)};
            mips_var=unique(sort(mips_var));
          end       
          for n=1:numel(mips_var)
            if n==1 
              mip_names=char(mips_var(n));
            else   
              mip_names=[mip_names ',' char(mips_var(n))];
            end 
          end 
        end 
       
        if imip==0 
          worksheet=cat(1,worksheet,...
            {num2str(priority),type_name,var_name,noresm_name,cmor_status,...
            mip_names,frequencies,long_name,description,units});
        else
          worksheet=cat(1,worksheet,...
            {num2str(priority),type_name,var_name,noresm_name,cmor_status,...
            frequencies,long_name,description,units});
        end 
        disp(sprintf('%s %i %s %s %s %s %s %s %s %s %s',...
             mip_name,priority,type_name,var_name,...
             noresm_name,cmor_status,mip_names,frequencies,long_name,description,units))  
       %pause

      end % var       

    end % type

  end % priority 

  % write text-file
  t=array2table(worksheet); 
  writetable(t,[PREFIX '_' mip_name '.txt'],'delimiter',';','WriteVariableNames',false);

end % MIP

 
