read_sbecnv Reads the SeaBird BINARY or ASCII *.cnv file format Usage: [cnv_struct]=read_sbecnv(cnv_file); [cnv_struct]=read_sbecnv(cnv_file,fileType); Input: CNV_file = name of .CNV file (e.g. 'STC1002.CNV') fileType = ['B'] | 'A' for binary or ascii. Output:cnv_struct with fields: lon = longitude in decimal degrees, West negative lat = latitude in decimal degrees, North positive gtime = Gregorian time vector in UTC sensors = string matrix containing the names of the sensors names = cell of string matrix containing the names of the columns data = matrix containing all the columns of data in the .CNV file NOTE: The format of CNV header file must be: * System UpLoad Time = Mar 21 2001 12:56:38 * NMEA Latitude = 06 19.96 N * NMEA Longitude = 043 59.96 W * NMEA UTC (Time) = 12:57:07 # name 0 = ..... # sensor 0 = ..... *end* Modify the lat,lon and date string handling if your .CNV files are different. Also, There are several possibilities for the way time is stored. NMEA time has been used as time and it is in GMT. HISTORY: 29 Nov 2002: Now reads both binary and ascii on Linux using command line inputs (no guis). Returns correct orientation. 29 Nov 2001 Sonia Cavalcante (Sonia.Cavalcante@noaa.gov) based on read_sbebtl by Derrick Snowden (Derrick.Snowden@noaa.gov) (which was based on cnv2mat by Rich Signell). 4-8-98 Rich Signell (rsignell@usgs.gov) incorporates ideas from code by Derek Fong & Peter Brickley
0001 function [cnv_struct]=read_sbecnv(cnv_file,varargin); 0002 % read_sbecnv Reads the SeaBird BINARY or ASCII *.cnv file format 0003 % 0004 % Usage: [cnv_struct]=read_sbecnv(cnv_file); 0005 % [cnv_struct]=read_sbecnv(cnv_file,fileType); 0006 % 0007 % Input: CNV_file = name of .CNV file (e.g. 'STC1002.CNV') 0008 % fileType = ['B'] | 'A' for binary or ascii. 0009 % 0010 % Output:cnv_struct with fields: 0011 % lon = longitude in decimal degrees, West negative 0012 % lat = latitude in decimal degrees, North positive 0013 % gtime = Gregorian time vector in UTC 0014 % sensors = string matrix containing the names of the sensors 0015 % names = cell of string matrix containing the names of the columns 0016 % data = matrix containing all the columns of data in the .CNV file 0017 % 0018 % NOTE: The format of CNV header file must be: 0019 % 0020 % * System UpLoad Time = Mar 21 2001 12:56:38 0021 % * NMEA Latitude = 06 19.96 N 0022 % * NMEA Longitude = 043 59.96 W 0023 % * NMEA UTC (Time) = 12:57:07 0024 % # name 0 = ..... 0025 % # sensor 0 = ..... 0026 % *end* 0027 % 0028 % Modify the lat,lon and date string handling if your .CNV files are different. 0029 % Also, There are several possibilities for the way time is stored. NMEA time 0030 % has been used as time and it is in GMT. 0031 % 0032 % 0033 % HISTORY: 0034 % 29 Nov 2002: Now reads both binary and ascii on Linux using 0035 % command line inputs (no guis). Returns correct orientation. 0036 % 29 Nov 2001 Sonia Cavalcante (Sonia.Cavalcante@noaa.gov) 0037 % based on read_sbebtl by Derrick Snowden (Derrick.Snowden@noaa.gov) 0038 % (which was based on cnv2mat by Rich Signell). 0039 % 4-8-98 Rich Signell (rsignell@usgs.gov) 0040 % incorporates ideas from code by Derek Fong & Peter Brickley 0041 % 0042 0043 % default assume the cnv file is written in ascii. If binary reading doesn't work on your 0044 % computer, rewrite the cnv files in ascii since that works everywhere and send 0045 % me the bug information. 0046 isbinary = false; 0047 time_offset = 0; 0048 error(nargchk(1,3,nargin)); 0049 if nargin == 2 0050 type = varargin{1}; 0051 if type== 1 | strcmp(upper(type(1)),'B') 0052 isbinary = true; 0053 elseif type== 0 | strcmp(upper(type(1)),'A') 0054 isbinary = false; 0055 else 0056 error('read_sbecnv error: Unrecognized input file type. {[B (binary)] or A (ascii)[default]}'); 0057 end 0058 elseif nargin ==3 0059 time_offset = varargin{2}; 0060 error('read_sbecnv error: Too many input variables.'); 0061 end 0062 0063 if nargin == 2 0064 time_offset = varargin{1}; 0065 else 0066 time_offset = 0; 0067 end 0068 0069 % Open the .CNV file as read-only text 0070 % 0071 fid=fopen(cnv_file,'rt'); 0072 if fid == -1 0073 error(['Unable to open file: ',cnv_file]); 0074 end 0075 0076 % 0077 % Read the header. 0078 % Start reading header lines of .CNV file, 0079 % Stop at line that starts with '*END*' 0080 % 0081 % Pull out NMEA lat & lon along the way and look 0082 % at the '# name' fields to see how many variables we have. 0083 % 0084 str='*START*'; 0085 0086 % This may change in different configurations. I suppose it depends on your 0087 % shipboard processing scripts/procedures. look through your files for 0088 %something that denots the end of the header but not the line containing the names ofthe columns. 0089 % 0090 choose_nmea = 0; 0091 while (~strncmp(str,'*END*',5)); 0092 str=fgetl(fid); 0093 %----------------------------------- 0094 % 0095 % Read the NMEA latitude string. This may vary with CTD setup. 0096 % 0097 if (strncmp(str,'* NMEA Lat',10)) 0098 choose_nmea = 1; 0099 is=findstr(str,'='); 0100 isub=is+1:length(str); 0101 dm=sscanf(str(isub),'%f',2); 0102 if(findstr(str(isub),'N')); 0103 lat=dm(1)+dm(2)/60; 0104 else 0105 lat=-(dm(1)+dm(2)/60); 0106 end 0107 %------------------------------- 0108 % 0109 % Read the NMEA longitude string. This may vary with CTD setup. 0110 % 0111 elseif (strncmp(str,'* NMEA Lon',10)) 0112 choose_nmea = 1; 0113 is=findstr(str,'='); 0114 isub=is+1:length(str); 0115 dm=sscanf(str(isub),'%f',2); 0116 if(findstr(str(isub),'E')); 0117 lon=dm(1)+dm(2)/60; 0118 else 0119 lon=-(dm(1)+dm(2)/60); 0120 end 0121 %----------------------------------- 0122 % 0123 % Read the NON NMEA latitude string. This may vary with CTD setup. 0124 % 0125 elseif (strncmp(str,'** Lat',6)) 0126 if choose_nmea == 0 0127 is=findstr(str,':'); 0128 isub=is+1:length(str); 0129 dm=sscanf(str(isub),'%f',2); 0130 if(findstr(str(isub),'N')); 0131 if length(dm) == 2 0132 lat=dm(1)+dm(2)/60; 0133 else 0134 lat = dm(1); % assume they have used decimal degrees 0135 end 0136 else 0137 if length(dm) == 2 0138 lat=-(dm(1)+dm(2)/60); 0139 else 0140 lat = -(dm(1)); % assume they have used decimal degrees 0141 end 0142 0143 end 0144 end 0145 %------------------------------- 0146 % 0147 % Read the NON NMEA longitude string. This may vary with CTD setup. 0148 % 0149 elseif (strncmp(str,'** Lon',6)) 0150 if choose_nmea == 0 0151 is=findstr(str,':'); 0152 isub=is+1:length(str); 0153 dm=sscanf(str(isub),'%f',2); 0154 if(findstr(str(isub),'E')); 0155 if length(dm) == 2 0156 lon =dm(1)+dm(2)/60; 0157 else 0158 lon = dm(1); % assume they have used decimal degrees 0159 end 0160 else 0161 if length(dm) == 2 0162 lon=-(dm(1)+dm(2)/60); 0163 else 0164 lon = -(dm(1)); % assume they have used decimal degrees 0165 end 0166 0167 end 0168 end 0169 %------------------------ 0170 % 0171 % Read the 'System upload time' to get the date. 0172 % This may vary with CTD setup. 0173 % 0174 % I'm reading this in to get the date, since the NMEA time string 0175 % does not contain date. Unfortunately, the system upload time is 0176 % in local time (here, EST), so I need to convert to UTC by adding 0177 % 5 hours (5/24 days). 0178 % 0179 elseif (strncmp(str,'* System UpLoad',15)) 0180 is=findstr(str,'='); 0181 % pick apart date string and reassemble in DATEFORM type 0 form 0182 datstr=[str(is+6:is+7) '-' str(is+2:is+4) '-' str(is+9:is+12)]; 0183 datstr=[datstr ' ' str(is+14:is+21)]; 0184 % convert datstr to Julian time, add 5 hours to convert from EST to GMT 0185 n=datenum(datstr) + time_offset/24; 0186 gtime=datevec(n); 0187 %---------------------------- 0188 % 0189 % Read the NMEA TIME string. This may vary with CTD setup. 0190 % 0191 % replace the System upload time with the NMEA time 0192 elseif (strncmp(str,'* NMEA UTC',10)) 0193 is=findstr(str,':'); 0194 isub=is(1)-2:length(str); 0195 gtime([4:6])=sscanf(str(isub),'%2d:%2d:%2d'); 0196 %------------------------------ 0197 % 0198 % Read the variable names & units into a cell array 0199 % 0200 elseif (strncmp(str,'# name',6)) 0201 var=sscanf(str(7:10),'%d',1); 0202 var=var+1; % .CNV file counts from 0, Matlab counts from 1 0203 % stuff variable names into cell array 0204 isin=findstr(str,'='); 0205 is=findstr(str,':'); 0206 names{var}=str(isin+1:is-1); 0207 %------------------------------ 0208 % 0209 % Read the sensor names into a cell array 0210 % 0211 elseif (strncmp(str,'# sensor',8)) 0212 sens=sscanf(str(10:11),'%d',1); 0213 sens=sens+1; % .CNV file counts from 0, Matlab counts from 1 0214 % stuff sensor names into cell array 0215 sensors{sens}=str; 0216 % 0217 % pick up bad flag value 0218 elseif (strncmp(str,'# bad_flag',10)) 0219 isub=13:length(str); 0220 bad_flag=sscanf(str(isub),'%g',1); 0221 end 0222 end 0223 %============================================== 0224 % 0225 % Done reading header. Now read the data! 0226 % It can be read as binary into matrix data 0227 %============================================== 0228 if isbinary 0229 data=fread(fid,[length(names) Inf],'float'); 0230 else 0231 data=fscanf(fid,'%f',[length(names) Inf]); 0232 end 0233 0234 data=data'; 0235 % Begin to assemble the output matrix/structure. 0236 0237 cnv_struct.lat = lat; 0238 cnv_struct.lon = lon; 0239 cnv_struct.gtime = gtime; 0240 cnv_struct.sensors = char(sensors'); 0241 % remove the spaces in the names cell 0242 for i = 1:length(names) 0243 str = names{i}; 0244 idxspc = isspace(str); 0245 str(idxspc) = []; 0246 names{i} = str; 0247 end 0248 cnv_struct.names = names; 0249 cnv_struct.data = data; 0250 0251 fclose(fid); 0252 return 0253 0254