Merge ctdac9 v2.m

From Pnb
Jump to navigation Jump to search
% MERGE_ctdac9.M
% DEC 97, JUL 98, 9/99
% John Ubante, SRW
% Input:	none
% Look for:	upused.txt, for pairing of ctd and ac9 files;
%               LIST/list_ac9merge_ctd
% Output: 	$ctd$ac9.strip
% Assumptions:	1) This function called from directory with data,
%		2) The strip lists located in 
%                  /home/scratch51/bbop/WORK/LIST/,
%		3) ac9file have just left sac9proc and begin with "m"
%		4) ac9files have 'core' name beginning with the
%                  first 'z'
%               4) ctdfiles are flat files that are already "stripped"
%		5) ctdfiles have 'core' name beginning withb the
%                  first 'X'
% Purpose:	Merge ac9 and ctd datasets and create a strip-
%		like file that contains the columns listed in 
%               [list_ac9merge_ctd; list_ac9merge_ac9]
% Calls:	ctd2strip,merge_2files
% Maintain: 	{filename, columnarraylist, disp_text}
% Uses:     	PnB ac9 processing
% Example:  	merge_ctdac9
% Notes:        Based on merge_merac9 as written for bbop ac9 processing
%Edited by Erik S. 6/7/2010 ' % % %' are altered areas
function merge_ctdac9(pb)

                                        % initialize local variables
if findstr(computer,'PCWIN')
  LIST='z:\links\WORK\LIST';
else
    LIST = '/home/eriks/LIST/';
    %LIST='/home/data241/bbop/RADIOMETRY/ARCHIVE_lcd/LIST/';
end
list_ctdac9=[];
list_ac9merge_ac9 = 'list_ac9merge_ac9';
HERE=pwd;
check=[];

                                        % check for list files
cd ../..                                        % in $WORK/LIST 
eval([ 'cd ' LIST ]);
[s,m]=unix([ 'ls ' list_ac9merge_ac9] );
if s
 sprintf('Check your %s in %s.',list_ac9merge_ac9,LIST)
 return
end
eval([ 'cd ' HERE ])
clear HERE
                                        % copy strip lists HERE
                                        % if not already HERE
[s,m]=unix([ 'ls ' list_ac9merge_ac9 ]);
if size(m,2)~=size(list_ac9merge_ac9,2)+1 
 eval([ '!cp ' LIST list_ac9merge_ac9 ' .' ]);   
end
clear s m LIST

                                        %load list file
name_list=list_load('upused.txt',[ 1:6 ],{'dum','dum','list_ac9bmdz', ...
		    'dum','dum','list_ctd'});


% $$$                                         % check for upused.txt in pwd
% $$$ [s,m]=unix('ls upused.txt');
% $$$ if isempty(m)
% $$$  msg=sprintf('Check your upused.txt.');
% $$$  disp(msg)
% $$$  return
% $$$ end
% $$$ clear s m
% $$$ 
% $$$                                         % load upused.txt
% $$$ fid=fopen('upused.txt','r');
% $$$ while fid~=-1
% $$$   check=fgetl(fid);
% $$$   if check~=-1,
% $$$     if(check(1) ~= '#')
% $$$       list_ctdac9=[list_ctdac9; check];
% $$$     end 
% $$$   else
% $$$     break;
% $$$   end
% $$$ end
% $$$ clear fid check
% $$$ 
% $$$ 
% $$$                                         % parse ctd & ac9 names
% $$$                                         % into lists
% $$$ for i=1:size(list_ctdac9,1),
% $$$   list_ac9(i,:)=parse(list_ctdac9(i,:),3);
% $$$   list_ctd(i,:)=parse(list_ctdac9(i,:),6);
% $$$ end

                                        % check to see if lists
                                        % lengths are equal
if size(list_ctd,1)~=size(list_ac9bmdz,1)
 error('You do not have 1 to 1 correspondence in your list_ctdac9.')
end
ifread=ones(size(list_ctd,1),2);
                                        % check presence of ctd files
badfilectr=0;
for ifile=1:size(list_ctd,1)
 [s,m]=unix(['ls ',list_ctd(ifile,:)]);
 if s
  badfilectr=badfilectr+1;
  ifread(ifile,1)=0;
 end
end
clear ifile
if badfilectr
  list_ctd
  msg=sprintf('%ifile missing ctd file(s).',badfilectr);
  disp(msg)
  warning('Your ctdlist includes a file not in pwd.')
end
clear s m badfilectr msg

                                        % check presence of ac9 files
badfilectr=0;
for ifile=1:size(list_ac9bmdz,1)
 [s,m]=unix(['ls ',list_ac9bmdz(ifile,:)]);
 

 if s
  badfilectr=badfilectr+1;
  ifread(ifile,2)=0;
 end
end
clear ifile
if badfilectr
   
  list_ac9bmdz
  msg=sprintf('%ifile missing ac9 file(s).',badfilectr);
  disp(msg)
  warning('Your ac9list includes a file not in pwd.')
end
clear s m badfilectr msg

                                        % loop through pairs
for i=find(all(ifread')),
  ctd_file=list_ctd(i,:);
  ac9_lcd=list_ac9bmdz(i,:);
                                        % create output name
  location1=find(ctd_file=='X');
  output=ctd_file(location1(1):location1(1)+7);
  output=[output '.' ctd_file(size(ctd_file,2))];
  location1=find(ac9_lcd=='z');
  location2=find(ac9_lcd=='.');
  output=[output '.' ac9_lcd(location1+2:location2-1) '.'];
  output=[output ac9_lcd(size(ac9_lcd,2)) '.strip']
  clear location1 location2
  
  
  
  % strip em
  ctd_base=ctd_file(1:8)
  pb
  ctd2strip_v2(pb,ctd_base)% % %------------------------------------ mofified to have no ctd_head return ctd_head =

% %   fid=fopen('ctd_head','w');%
% %   for j=1:size(ctd_head,1),
% %     fprintf(fid,'%s\n',ctd_head(j,:)');%
% %   end
  
 %fclose(fid);
  eval([ '!cat ctd_head_n > ' ctd_file '.strip']);
  eval([ '!cat ' ctd_file ' >> ' ctd_file '.strip']);


 % %eval([ '!cat ctd_head > ' ctd_file '.strip']);
 % %eval([ '!cat ' ctd_file ' >> ' ctd_file '.strip']);

  %eval('!\rm ctd_head' );
  ctd_strip=[ctd_file '.strip']
  ac9_strip=[ac9_lcd '.strip'];
  
  %eval([ '!bbopstrip -f ' list_ac9merge_ac9 ' ' ac9_lcd ' ' ac9_strip ])
  %ac9_lcd

  E_BBstrip(pb,ac9_lcd,list_ac9merge_ac9);
         ac9_strip = strcat(ac9_lcd,'.strip');%
         
  % call merge_2files
  msg=sprintf('Merging %s and %s into %s.',ctd_file,ac9_lcd,output);

  disp(msg)%
   
 
  [data,header]=merge_2files(ctd_strip,ac9_strip);
  
                                        % write merged stripfile to disk
  fid=fopen('merging_header.temp','w');
  for j=1:size(header,1)
    fprintf(fid,'%3s\n',header(j,:)');
  end
  fclose(fid);
  save merging_data.temp data -ascii
  eval([ '!cat merging_header.temp merging_data.temp > ' output ]);
  eval('!\rm merging_header.temp merging_data.temp');
  %clear output fid header data j
  
                                        % rm strip files
% eval([ '!rm ' ctd_strip ' ' ac9_strip ])
end  % end for loop					
clear i ctd2strip_v2 ac9_strip

                                        % rm strip list files
% $$$ eval([ '!rm ' list_ac9merge_ac9 ' ' list_ac9merge_mer ]);

% MERGE_2FILES.M
% NOV 97
% John Ubante
% Input:  	[mer,ac9]:string
% Output: 	[headAB:char array,fileAB:{200x(N1+N2-1)}]
% Assumptions:	errorflag=(-9.9e+35); depth will be the second column of mer
%		data and the first column of ac9 data
% Purpose:	to paste 2 strip-version files together using binned depths
%		as index; to place errorflag values at depths that don't exist
%		if there is an error with inputted files, program will return
%		to keyboard
% Calls:	read_strip, unique
% Uses:     	97 ac9 processing, esp. called from merge_merac9.m
%
% Example:	[data,header]=merge_2files('mer.strip','ac9.strip')

% NOTES:	in script, "fileA" refers to SECOND parameter and "fileB"
%		refers to FIRST parameter;

function [fileAB,headAB]=merge_2files(ctd,ac9);

						% declare local variables
errorflag=-9.9e+35;
depthrange=[1:220]; % note that the 0m depth line is still added
fileAB=[];
headAB=['#filename ',ctd,' ',ac9];

                                        % load stripped files
%eval(['[fileA,headA]=read_strip(ac9);'])
%eval(['[fileB,headB]=read_strip(ctd);'])

eval(['[fileA,headA]=hdrload(ac9);']);
eval(['[fileB,headB]=hdrload(ctd);']);
clear ctd ac9

%add depth column in 2 for ctd, will take out at the end
fileB=[fileB(:,1) fileB(:,3) fileB(:,2:size(fileB,2))];

%zerodepth=fileB(1,:);

                                        % remove lines with
                                      % errorflag for depth
indA=find(fileA(:,1)==errorflag);
fileA(indA,:)=[];
indB=find(fileB(:,2)==errorflag);
fileB(indB,:)=[];
clear indA indB

                                        % ensure depths are
                                        % positive values
fileA(:,1)=abs(fileA(:,1));
fileB(:,2)=abs(fileB(:,2));

                              % ensure depths are
                                        % integral  values
for i=1:size(fileB,1)
 if fileB(i,2)~=round(fileB(i,2))
    %fileB(i,2)
    %round(fileB(i,2))
 error('Depth is not integral in first parameter.  Try again.')
 end
end
for i=1:size(fileA,1)
 if fileA(i,1)~=round(fileA(i,1))
     fileA(i,1);
     
  error('Depth is not integral in second parameter.  Try again.')
 end
end
clear i

                                        % remove depths outside of
                                       % depthrange
indA=find(fileA(:,1)<min(depthrange));
fileA(indA,:)=[];
indA=find(fileA(:,1)>max(depthrange));
fileA(indA,:)=[];
indB=find(fileB(:,2)<min(depthrange));
fileB(indB,:)=[];
indB=find(fileB(:,2)>max(depthrange));
fileB(indB,:)=[];
clear indA indB

                                        % ensure depths are unique
if size(unique(fileA(:,1)),1)~=size(fileA,1)
 error('Depth is not unique in second parameter.  Try again.')
end
if size(unique(fileB(:,2)),1)~=size(fileB,1)
 error('Depth is not unique in first parameter.  Try again.')
end

                                        % ensure depths are monotonic
 if sort(fileA(:,1))~=fileA(:,1)
   error('Depth is not monotonic in second parameter.  Try again.')
   keyboard
 end
if sort(fileB(:,2))~=fileB(:,2)
  error('Depth is not monotonic in first parameter.  Try again.')
  keyboard
end

% $$$ 						% join using depths
% $$$ for i=depthrange
% $$$ 						%  fill in depth with errorflag if
% $$$ 						%   missing
% $$$  if size(fileB,1)<i
% $$$   fileB=[fileB;errorflag,i,ones(1,size(fileB,2)-2)*errorflag];
% $$$  elseif fileB(i,2)~=i
% $$$   fileB=[fileB(1:i,:);errorflag,i,ones(1,size(fileB,2)-2)*errorflag;fileB(i:size(fileB,1),:)];
% $$$  end
% $$$  if size(fileA,1)<i
% $$$   fileA=[fileA;i,ones(1,size(fileA,2)-1)*errorflag];
% $$$ elseif fileA(i,1)~=i
% $$$   fileA=[fileA(1:i,:);i,ones(1,size(fileA,2)-1)*errorflag;fileA(i:size(fileA,1),:)];
% $$$  end
% $$$ 						%  do IT
% $$$  fileAB(i,:)=[fileB(i,:),fileA(i,2:size(fileA,2))];
% $$$ 
% $$$ end						% end for

                                         % join using depths,
                                         % replaces section above
emask=ones(size(depthrange,2),size(fileB,2))*errorflag;
irow=find(fileB(:,2) >= depthrange(1,1) & fileB(:,2) <= ...
    depthrange(end));
if(isempty(irow));
  error('No data in depthrange 1')
end
for i = 1:size(irow,1);
  emask(fileB(irow(i,1),2),:) = fileB(irow(i,1),:);
end
fileB=emask;
clear emask irow
emask=ones(size(depthrange,2),size(fileA,2))*errorflag;
irow=find(fileA(:,1) > depthrange(1,1) & fileA(:,1) <= ...
    depthrange(size(depthrange,2)));
if(isempty(irow));
  error('No data in depthrange 2')
end
for i = 1:size(irow,1);
  emask(fileA(irow(i,1),1),:) = fileA(irow(i,1),:);
end
fileA=emask;
                                        %leaves out bin_1.0_depth
fileAB=[fileB,fileA(:,2:size(fileA,2))];

% $$$                                         % add 0m depth
% $$$ zerodepth(1,size(fileB,2)+1:size(fileB,2)+size(fileA,2)-1)=errorflag;
% $$$ fileAB=[zerodepth; fileAB];
% $$$ clear fileA fileB i depthrange errorflag zerodepth

                                        %remove extra depth channel
                                        %in col 2
fileAB(:,2)=[];

headAB=char(headAB,headB(2:size(headB,1),:),headA(3:size(headA,1),:));
clear headA headB

% $$$                                         % force headers' width = width
% $$$ width=size(headA(1,:),2)+size(headB(1,:),2);
% $$$ headA(:,width)=' ';
% $$$ headB(:,width)=' ';
% $$$ headAB(1,width)=' ';

% $$$                                         % combine headers
% $$$ headAB=[headAB;headB(2:size(headB,1),:);headA(3:size(headA,1),:)];
% $$$ clear width headA headB

return

merge_ctdac9_v2