function [cast] = read_ocldb_write_mat(input_dir,filename,output_dir,inst) %input_dir = '/home/eleanor/data/WOD/wod05/'; %filename = 'ocldb1169076334.845.CTD'; %output_dir = '/home/eleanor/matlab/bin/glider/WOD/wod05/mat/'; headerfile = 'header1cast.csv'; datafile = 'data1cast.csv'; % Clean out files, removing string '---.---' ext4 = '.csv'; fid = fopen([input_dir,filename,ext4]); iter=0; fline = fgetl(fid); while fline(1)=='#' %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Read ONE CAST into header and data files iter = iter+1; fid_hdr = fopen(headerfile,'w'); fid_data = fopen(datafile,'w'); fline = fgetl(fid); while (fline(1)~=' ')&&(~strcmp(fline(1:3),'BIO')) % First line of data fwrite(fid_hdr,fline); fprintf(fid_hdr,'\n'); fline = fgetl(fid); end if strcmp(fline(1:3),'BIO') while fline(1)~='#' fline = fgetl(fid); end fclose(fid_hdr); fclose(fid_data); else while ~strcmp(fline(1:3),'END') % line is "END OF VARIABLES ..." fwrite(fid_data,fline); fprintf(fid_data,'\n'); fline = fgetl(fid); end fclose(fid_hdr); fclose(fid_data); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Parse the header file fid_hdr = fopen(headerfile); C = textscan(fid_hdr,'%s%s%s%s%s%s%s%s','delimiter',',','emptyValue',1); C1 = C{1}; C3 = C{3}; for n=1:length(C1) entry1 = cell2mat(C1(n)); if length(entry1)>2 entry3 = entry1(1:3); switch entry3 case {'CAS'} cast_num = str2double(cell2mat(C3(n))); all_casts(iter) = cast_num; case {'Lat'} lat = str2double(cell2mat(C3(n))); case {'Lon'} lon = str2double(cell2mat(C3(n))); case {'Yea'} yy = str2double(cell2mat(C3(n))); case {'Mon'} mm = str2double(cell2mat(C3(n))); case {'Day'} dd = str2double(cell2mat(C3(n))); case {'VAR'} for k=1:length(C) Ck = C{k}; hdr(k).name = cell2mat(Ck(n)); end end end end fclose(fid_hdr); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Create data structure cast(iter).inst = inst; cast(iter).number = cast_num; cast(iter).lat = lat; cast(iter).lon = lon; cast(iter).yy = yy; cast(iter).mm = mm; cast(iter).dd = dd; cast(iter).salin = []; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Parse the data file mydata = importdata(datafile); cast(iter).flag=0; ll = 0; for k=1:length(C) ll = ll+1; if length(hdr(k).name) hdr3 = hdr(k).name(1); switch hdr3 case 'V' cast(iter).index = mydata(:,ll); case 'D' cast(iter).depth = mydata(:,ll); case 'F' cast(iter).flag = cast(iter).flag+mydata(:,ll); case 'T' cast(iter).temp = mydata(:,ll); case 'S' cast(iter).salin = mydata(:,ll); case {'O','0'} ll=ll-1; end end end end fline = fgetl(fid); if isempty(fline) break end end fclose(fid); eval(['save ',output_dir,filename,'.mat cast']);