


CNV2MAT Reads the SeaBird ASCII .CNV file format
Usage: [lat,lon,gtime,data,names,sensors]=cnv2mat(cnv_file);
Input: cnv_file = name of .CNV file (e.g. 'cast002.cnv')
Output: lon = longitude in decimal degrees, West negative
lat = latitude in decimal degrees, North positive
gtime = Gregorian time vector in UTC
data = matrix containing all the columns of data in the .CNV file
names = string matrix containing the names and units of the columns
sensors = string matrix containing the names of the sensors
NOTE: How lon,lat and time are written to the header of the .CNV
file may vary with CTD setup. For our .CNV files collected on
the Oceanus, the lat, lon & time info look like this:
* System UpLoad Time = Mar 30 1998 18:48:42
* NMEA Latitude = 42 32.15 N
* NMEA Longitude = 069 28.69 W
* NMEA UTC (Time) = 23:50:36
Modify the lat,lon and date string handling if your .CNV files are different.

0001 function [lat,lon,gtime,data,names,sensors]=cnv2mat(cnv_file); 0002 % CNV2MAT Reads the SeaBird ASCII .CNV file format 0003 % 0004 % Usage: [lat,lon,gtime,data,names,sensors]=cnv2mat(cnv_file); 0005 % 0006 % Input: cnv_file = name of .CNV file (e.g. 'cast002.cnv') 0007 % 0008 % Output: lon = longitude in decimal degrees, West negative 0009 % lat = latitude in decimal degrees, North positive 0010 % gtime = Gregorian time vector in UTC 0011 % data = matrix containing all the columns of data in the .CNV file 0012 % names = string matrix containing the names and units of the columns 0013 % sensors = string matrix containing the names of the sensors 0014 % 0015 % NOTE: How lon,lat and time are written to the header of the .CNV 0016 % file may vary with CTD setup. For our .CNV files collected on 0017 % the Oceanus, the lat, lon & time info look like this: 0018 % 0019 % * System UpLoad Time = Mar 30 1998 18:48:42 0020 % * NMEA Latitude = 42 32.15 N 0021 % * NMEA Longitude = 069 28.69 W 0022 % * NMEA UTC (Time) = 23:50:36 0023 % 0024 % Modify the lat,lon and date string handling if your .CNV files are different. 0025 0026 % 4-8-98 Rich Signell (rsignell@usgs.gov) 0027 % incorporates ideas from code by Derek Fong & Peter Brickley 0028 % 0029 0030 % Open the .cnv file as read-only text 0031 % 0032 fid=fopen(cnv_file,'rt'); 0033 % 0034 % Read the header. 0035 % Start reading header lines of .CNV file, 0036 % Stop at line that starts with '*END*' 0037 % 0038 % Pull out NMEA lat & lon along the way and look 0039 % at the '# name' fields to see how many variables we have. 0040 % 0041 str='*START*'; 0042 while (~strncmp(str,'*END*',5)); 0043 str=fgetl(fid); 0044 %----------------------------------- 0045 % 0046 % Read the NMEA latitude string. This may vary with CTD setup. 0047 % 0048 if (strncmp(str,'* NMEA Lat',10)) 0049 is=findstr(str,'='); 0050 isub=is+1:length(str); 0051 dm=sscanf(str(isub),'%f',2); 0052 if(findstr(str(isub),'N')); 0053 lat=dm(1)+dm(2)/60; 0054 else 0055 lat=-(dm(1)+dm(2)/60); 0056 end 0057 %------------------------------- 0058 % 0059 % Read the NMEA longitude string. This may vary with CTD setup. 0060 % 0061 elseif (strncmp(str,'* NMEA Lon',10)) 0062 is=findstr(str,'='); 0063 isub=is+1:length(str); 0064 dm=sscanf(str(isub),'%f',2); 0065 if(findstr(str(isub),'E')); 0066 lon=dm(1)+dm(2)/60; 0067 else 0068 lon=-(dm(1)+dm(2)/60); 0069 end 0070 %------------------------ 0071 % 0072 % Read the 'System upload time' to get the date. 0073 % This may vary with CTD setup. 0074 % 0075 % I'm reading this in to get the date, since the NMEA time string 0076 % does not contain date. Unfortunately, the system upload time is 0077 % in local time (here, EST), so I need to convert to UTC by adding 0078 % 5 hours (5/24 days). 0079 % 0080 elseif (strncmp(str,'* System UpLoad',15)) 0081 is=findstr(str,'='); 0082 % pick apart date string and reassemble in DATEFORM type 0 form 0083 datstr=[str(is+6:is+7) '-' str(is+2:is+4) '-' str(is+9:is+12)]; 0084 datstr=[datstr ' ' str(is+14:is+21)]; 0085 % convert datstr to Julian time, add 5 hours to convert from EST to GMT 0086 n=datenum(datstr)+5/24; 0087 gtime=datevec(n); 0088 %---------------------------- 0089 % 0090 % Read the NMEA TIME string. This may vary with CTD setup. 0091 % 0092 % replace the System upload time with the NMEA time 0093 elseif (strncmp(str,'* NMEA UTC',10)) 0094 is=findstr(str,':'); 0095 isub=is(1)-2:length(str); 0096 gtime([4:6])=sscanf(str(isub),'%2d:%2d:%2d'); 0097 %------------------------------ 0098 % 0099 % Read the variable names & units into a cell array 0100 % 0101 elseif (strncmp(str,'# name',6)) 0102 var=sscanf(str(7:10),'%d',1); 0103 var=var+1; % .CNV file counts from 0, Matlab counts from 1 0104 % stuff variable names into cell array 0105 names{var}=str; 0106 %------------------------------ 0107 % 0108 % Read the sensor names into a cell array 0109 % 0110 elseif (strncmp(str,'# sensor',8)) 0111 sens=sscanf(str(10:11),'%d',1); 0112 sens=sens+1; % .CNV file counts from 0, Matlab counts from 1 0113 % stuff sensor names into cell array 0114 sensors{sens}=str; 0115 % 0116 % pick up bad flag value 0117 elseif (strncmp(str,'# bad_flag',10)) 0118 isub=13:length(str); 0119 bad_flag=sscanf(str(isub),'%g',1); 0120 end 0121 end 0122 %============================================== 0123 % 0124 % Done reading header. Now read the data! 0125 % 0126 nvars=var; %number of variables 0127 0128 % Read the data into one big matrix 0129 % 0130 data=fscanf(fid,'%f',[nvars inf]); 0131 0132 fclose(fid); 0133 0134 % 0135 % Flag bad values with nan 0136 % 0137 ind=find(data==bad_flag); 0138 data(ind)=data(ind)*nan; 0139 0140 % 0141 % Flip data around so that each variable is a column 0142 data=data.'; 0143 0144 % Convert cell arrays of names to character matrices 0145 names=char(names); 0146 sensors=char(sensors); 0147 0148 return