Conv2lcd HS6.m

From Pnb
Jump to navigation Jump to search
function conv2lcd_HS6(pb)
%conv2lcd_HS6
%This is the first part of the Hydroscat processing once it passes through
%Hydrosoft.
%This function reads in the .dat file and splits off the data into a
%sepparate file where is takes the cammas out.
%Section titles are inserted into the header section(<cruise_info>....) as
%well as instrument serial number at the top of the file
%the column titles are converted to rows and the "channels" section is
%removed.
%start it from directory that sees dat directory

%this is run in the dat directory
direct = pwd;
cd('dat');
unix('cp *.dat ../'); %gets dat files and moves them out to processing dir
cd ..

datprofile = fullfile(pwd,'*.dat')
datfiledir = dir(datprofile);
[s,x] = size(datfiledir);

%===========call make_meta_hs6(pb)
make_meta_hs6(pb) %makes the meta file
%===========end call

meta = fopen('metafile.txt','r');
title_ln = fgets(meta);
for i = 1:s
    info = textscan(meta,'%*s %*s %s %s %s %s',1);
%%%%%%%%%%stuff to open and/or create%%%%%%%    

fid = fopen(datfiledir(i).name,'r');
%  %fid = fopen('h100428a.dat')
name = datfiledir(i).name
%name = 'h100428a.dat';
%fidtemp = fopen(strcat(num2str(i),'temphs6_lcd'),'w+');
lcdname = strrep(name,'.dat','.lcd');
fidlcd = fopen(strrep(name,'.dat','.lcd'),'w+');
datmatrix = strrep(name,'.dat','.datmat');
mat = fopen(datmatrix,'w+');

%%%%%%%%%%%end stuff%%%%%%%%%%%%%%%%%%%%%%
%---------get parameters--------

%#######loop to get params
date = info{1};
station = info{2};
lat = char(info{3});
lon = char(info{4});
linecount =1;
line =0;

while strncmp(line,'[Channels]',10) ~= 1
      line = fgets(fid);
      if strncmp(line,'[Channels]',10) ==1
          break
      end
      if linecount ==1
          fprintf(fidlcd,'%s\n','<cruise_info>');
          %insert cruise_ID >pb# 
          fprintf(fidlcd,'%s %s\n','Cruise_ID ',pb);
          %serial number
          %insert date >080211
          fprintf(fidlcd,'%s %s\n','Date ',date{1});
          %insert station > PnB#
          fprintf(fidlcd,'%s %s\n','Station ',station{1});
          %insert latitude > 34.???
          fprintf(fidlcd,'%s %s\n','Latitude ',lat);
          %insert longitude > 119.????
          fprintf(fidlcd,'%s %s\n','Longitude ',lon);
          %insert metadata_name > metafile.txt
          fprintf(fidlcd,'%s %s\n','metadata_filename', 'metafile.txt');
          %insert '<sampled_parameters>')
          
      end
          if strncmp(line,'[Header]',8) ==1
              linecount = linecount +1;
              continue
          end
          if strncmp(line,'FileType',8) == 1
              continue
          end
          if strncmp(line,'CreationDate',12) == 1
              continue
          end
          fprintf(fidlcd,'%s',line);
    linecount = linecount +1;      
end
pline =1;
params =1;


%++++++ write data params in column
fprintf(fidlcd,'%s\n', '<sampled_parameters>');
fprintf(fidlcd,'%s\n','hydro_time');
fprintf(fidlcd,'%s\n','depth');


%=====get rest of data params
while strncmp(params,'Time',4) ~= 1
    params = fgets(fid)
end
ind  = findstr(params,',')
for p = 2:length(ind) -1
    headr = params(ind(p)+1:ind(p+1) -1)
    
   % headr(p,:) = params(1:ind(p,:) -1)
fprintf(fidlcd,'%s\n',headr)
end
lengin = length(ind);
lastin = ind(lengin);
lengpar = length(params)
header_plus1 = params(lastin+1:lengpar)%assumes the last two parameters are same character length
fprintf(fidlcd,'%s',header_plus1)
%====end get data params

%++++++end write data params

% % % while strncmp(params,'[ColumnHeadings]',16) ~= 1
% % %     params = fgets(fid);
% % %     params = strrep(params,'"','');
% % %     if strncmp(params,'[ColumnHeadings]',16) == 1
% % %         break
% % %     end
% % %     %eval([strcat('par',num2str(pline)) '= params'])
% % %     par(pline,:) = params;
% % % 
% % %     pline = pline +1;  
% % % end
% % % %======
% % % %++++++ write data params in column
% % % fprintf(fidlcd,'%s\n', '<sampled_parameters>');
% % % fprintf(fidlcd,'%s\n','hydro_time');
% % % fprintf(fidlcd,'%s\n','depth');
% % % for t = 1:pline -1
% % %     parline = strcat(par(t,:),'_corr');
% % %     fprintf(fidlcd,'%s\n', parline);
% % % end
% % % clear t;
% % % for t = 1:pline -1
% % %     %parline2 = strcat('par(,num2str(t))
% % %     par1 = par(t,:);
% % %     fprintf(fidlcd,'%s', par1);
% % % end
% % % 
 fprintf(fidlcd,'%s\n','<derived_parameters>');
 fprintf(fidlcd,'%s\n','<data>');
% % % 
tline = 0;
%++++++ end write data params in column
while strncmp(tline,'[Data]',6) ~= 1  
tline = fgets(fid);
end
 
%------- does correct and traditional formatting for data
while ~feof(fid)
    data = fgets(fid);
    %data = strrep(data,',',' ');
    data = strrep(data,',.',',0.');
    data = strrep(data,'E','e');
%--------converts time to seconds from julean days after 1970    
    k = strfind(data,',');
            time = str2num(data(1:k(1)-1));
            time = 86400*(time-25569);
            data(1:k(1)-1)=[];
            data = [num2str(time),data];
            data = strrep(data,',',' ');
    fprintf(fidlcd,data);
    fprintf(mat,data);
end

fprintf(fidlcd,'%s\n','<filters_used>');


%----to make list_hs6
newl(i,:) = lcdname;
end
dlmwrite('list_hs6',newl,'');
fclose all;

                if pwd == direct
                    unix('rm *.dat')
                else
                    disp('change to pb dir and delete .dat files')
                end


conv2lcd_HS6