addpath /asl/matlab/h4tools
addpath /asl/matlab/rtptools

% Set maxlev
maxlev=102;

%%%%%%%%%%%%%%%
% Assign header
head.ptype=0;
head.pfields=1;
head.pmin=1E+16;
head.pmax=-1E+16;
head.ngas=5;
head.glist=[1 2 3 5 6]';
head.gunit=[10 10 10 10 10]';
head.nchan=0;
head.mwnchan=0;
xhead=head;

prof=[];

pmin=head.pmin;
pmax=head.pmax;
%%%%%%%%%%%%%%%%%%%%
% Loop over profiles
for ip=1:49
   if (ip == 49)
      str='ref';
   else
      str=int2str(ip);
   end
   eval(['! dox myp' str]);
   load xxx    % profile
   load xxx2   % latitude
   %
   p=zeros(maxlev,1);
   t=zeros(maxlev,1);
   g1=zeros(maxlev,1);
   g2=zeros(maxlev,1);
   g3=zeros(maxlev,1);
   g5=zeros(maxlev,1);
   g6=zeros(maxlev,1);
   %
   [nrow,ncol]=size(xxx);
   if (nrow > maxlev)
      disp(['profile ' int2str(ip)])
      disp(['[nrow,ncol] = ' int2str(nrow) ', ' int2str(ncol)])
      maxlev
      error('nrow exceeds maxlev')
   end
   %
   p(1:nrow)=xxx(:,1);
   t(1:nrow)=xxx(:,2);
   g1(1:nrow)=xxx(:,4);
   g2(1:nrow)=xxx(:,5);
   g3(1:nrow)=xxx(:,6);
   g5(1:nrow)=xxx(:,7);
   g6(1:nrow)=xxx(:,8);
   %
   pmin=min(xxx(:,1));
   pmax=max(xxx(:,1));
   if (pmin < head.pmin)
      xhead.pmin=pmin;
   end
   if (pmax > head.pmax)
      xhead.pmax=pmax;
   end
   %
   % Assign profile
   xprof.plat=xxx2(:,1);
   xprof.nlevs=nrow;
   xprof.plevs=p;
   xprof.ptemp=t;
   xprof.gas_1=g1;
   xprof.gas_2=g2;
   xprof.gas_3=g3;
   xprof.gas_5=g5;
   xprof.gas_6=g6;
   xprof.co2ppm=370.0;
   %
   % Concatenate profiles
   if (ip ==1)
      prof=xprof;
   else
      [head,prof]=cat_rtp(xhead,prof,xhead,xprof);
   end
end

% Assign attributes
hattr=[];
pattr=[];
hattr{1}={'header' 'comment' 'UMBC Pin_feb2002 raw regresion profiles'};

% Save to RTP file
rtpwrite('pin_feb2002_raw_ip.rtp',head, hattr, prof, pattr);

%%% end of program %%%
