read_sbeasc Reads the SeaBird *.asc file format and pairs data with a hdr_struct Usage: [lat,lon,gtime,sensors,names,data]=read_sbeasc(asc_file,hdr_struct); Input: asc_file = name of .asc file (e.g. 'STC1002.asc') hdr_struct = structure returned from read_sbehdr Output:asc_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 of the columns in asc_file. NOTE: The format of asc 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 .hdr 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. BUGS: Doesn't check if the hdr_struct passed as input is the corresponding hdr file for the asc_file passed. It assumes the user passes the correct file/structure combo. CALLER: user DEPENDENCIES: AUTHOR: Derrick Snowden (Derrick.Snowden@noaa.gov) (wich was based on Rich Signell). 4-8-98 Rich Signell (rsignell@usgs.gov) incorporates ideas from code by Derek Fong & Peter Brickley
0001 function [asc_struct]=read_sbeasc(asc_file,hdr_struct); 0002 % read_sbeasc Reads the SeaBird *.asc file format and pairs data with a hdr_struct 0003 % 0004 % Usage: [lat,lon,gtime,sensors,names,data]=read_sbeasc(asc_file,hdr_struct); 0005 % 0006 % Input: asc_file = name of .asc file (e.g. 'STC1002.asc') 0007 % hdr_struct = structure returned from read_sbehdr 0008 % 0009 % Output:asc_struct with fields: 0010 % lon = longitude in decimal degrees, West negative 0011 % lat = latitude in decimal degrees, North positive 0012 % gtime = Gregorian time vector in UTC 0013 % sensors = string matrix containing the names of the sensors 0014 % names = cell of string matrix containing the names of the columns 0015 % data = matrix of the columns in asc_file. 0016 % 0017 % NOTE: The format of asc header file must be: 0018 % 0019 % * System UpLoad Time = Mar 21 2001 12:56:38 0020 % * NMEA Latitude = 06 19.96 N 0021 % * NMEA Longitude = 043 59.96 W 0022 % * NMEA UTC (Time) = 12:57:07 0023 % # name 0 = ..... 0024 % # sensor 0 = ..... 0025 % *end* 0026 % 0027 % 0028 % Modify the lat,lon and date string handling if your .hdr 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 % BUGS: Doesn't check if the hdr_struct passed as input is the corresponding 0034 % hdr file for the asc_file passed. It assumes the user passes the correct 0035 % file/structure combo. 0036 % 0037 % CALLER: user 0038 % 0039 % DEPENDENCIES: 0040 % 0041 % AUTHOR: Derrick Snowden (Derrick.Snowden@noaa.gov) 0042 % (wich was based on Rich Signell). 0043 % 4-8-98 Rich Signell (rsignell@usgs.gov) 0044 % incorporates ideas from code by Derek Fong & Peter Brickley 0045 % 0046 0047 % % Open the .asc file as read-only text 0048 % % 0049 % fid=fopen(asc_file,'rt'); 0050 % % 0051 % % Read the header. 0052 % % Start reading header lines of .asc file, 0053 % % Stop at line that starts with '*END*' 0054 % % 0055 % % Pull out NMEA lat & lon along the way and look 0056 % % at the '# name' fields to see how many variables we have. 0057 % % 0058 % str='*START*'; 0059 % 0060 % % This may change in different configurations. I suppose it depends on your 0061 % % shipboard processing scripts/procedures. look through your files for 0062 % %something that denots the end of the header but not the line containing the names ofthe columns. 0063 % % 0064 % choose_nmea = 0; 0065 % while (~strncmp(str,'*END*',5)); 0066 % str=fgetl(fid); 0067 % %----------------------------------- 0068 % % 0069 % % Read the NMEA latitude string. This may vary with CTD setup. 0070 % % 0071 % if (strncmp(str,'* NMEA Lat',10)) 0072 % choose_nmea = 1; 0073 % is=findstr(str,'='); 0074 % isub=is+1:length(str); 0075 % dm=sscanf(str(isub),'%f',2); 0076 % if(findstr(str(isub),'N')); 0077 % lat=dm(1)+dm(2)/60; 0078 % else 0079 % lat=-(dm(1)+dm(2)/60); 0080 % end 0081 % %------------------------------- 0082 % % 0083 % % Read the NMEA longitude string. This may vary with CTD setup. 0084 % % 0085 % elseif (strncmp(str,'* NMEA Lon',10)) 0086 % choose_nmea = 1; 0087 % is=findstr(str,'='); 0088 % isub=is+1:length(str); 0089 % dm=sscanf(str(isub),'%f',2); 0090 % if(findstr(str(isub),'E')); 0091 % lon=dm(1)+dm(2)/60; 0092 % else 0093 % lon=-(dm(1)+dm(2)/60); 0094 % end 0095 % %----------------------------------- 0096 % % 0097 % % Read the NON NMEA latitude string. This may vary with CTD setup. 0098 % % 0099 % elseif (strncmp(str,'** Lat',6)) 0100 % if choose_nmea == 0 0101 % is=findstr(str,':'); 0102 % isub=is+1:length(str); 0103 % dm=sscanf(str(isub),'%f',2); 0104 % if(findstr(str(isub),'N')); 0105 % if length(dm) == 2 0106 % lat=dm(1)+dm(2)/60; 0107 % else 0108 % lat = dm(1); % assume they have used decimal degrees 0109 % end 0110 % else 0111 % if length(dm) == 2 0112 % lat=-(dm(1)+dm(2)/60); 0113 % else 0114 % lat = -(dm(1)); % assume they have used decimal degrees 0115 % end 0116 % 0117 % end 0118 % end 0119 % %------------------------------- 0120 % % 0121 % % Read the NON NMEA longitude string. This may vary with CTD setup. 0122 % % 0123 % elseif (strncmp(str,'** Lon',6)) 0124 % if choose_nmea == 0 0125 % is=findstr(str,':'); 0126 % isub=is+1:length(str); 0127 % dm=sscanf(str(isub),'%f',2); 0128 % if(findstr(str(isub),'E')); 0129 % if length(dm) == 2 0130 % lon =dm(1)+dm(2)/60; 0131 % else 0132 % lon = dm(1); % assume they have used decimal degrees 0133 % end 0134 % else 0135 % if length(dm) == 2 0136 % lon=-(dm(1)+dm(2)/60); 0137 % else 0138 % lon = -(dm(1)); % assume they have used decimal degrees 0139 % end 0140 % 0141 % end 0142 % end 0143 % %------------------------ 0144 % % 0145 % % Read the 'System upload time' to get the date. 0146 % % This may vary with CTD setup. 0147 % % 0148 % % I'm reading this in to get the date, since the NMEA time string 0149 % % does not contain date. Unfortunately, the system upload time is 0150 % % in local time (here, EST), so I need to convert to UTC by adding 0151 % % 5 hours (5/24 days). 0152 % % 0153 % elseif (strncmp(str,'* System UpLoad',15)) 0154 % is=findstr(str,'='); 0155 % % pick apart date string and reassemble in DATEFORM type 0 form 0156 % datstr=[str(is+6:is+7) '-' str(is+2:is+4) '-' str(is+9:is+12)]; 0157 % datstr=[datstr ' ' str(is+14:is+21)]; 0158 % % convert datstr to Julian time, add 5 hours to convert from EST to GMT 0159 % n=datenum(datstr)+5/24; 0160 % gtime=datevec(n); 0161 % %---------------------------- 0162 % % 0163 % % Read the NMEA TIME string. This may vary with CTD setup. 0164 % % 0165 % % replace the System upload time with the NMEA time 0166 % elseif (strncmp(str,'* NMEA UTC',10)) 0167 % is=findstr(str,':'); 0168 % isub=is(1)-2:length(str); 0169 % gtime([4:6])=sscanf(str(isub),'%2d:%2d:%2d'); 0170 % %------------------------------ 0171 % % 0172 % % Read the variable names & units into a cell array 0173 % % 0174 % elseif (strncmp(str,'# name',6)) 0175 % var=sscanf(str(7:10),'%d',1); 0176 % var=var+1; % .asc file counts from 0, Matlab counts from 1 0177 % % stuff variable names into cell array 0178 % isin=findstr(str,'='); 0179 % is=findstr(str,':'); 0180 % names{var}=str(isin+1:is-1); 0181 % %------------------------------ 0182 % % 0183 % % Read the sensor names into a cell array 0184 % % 0185 % elseif (strncmp(str,'# sensor',8)) 0186 % sens=sscanf(str(10:11),'%d',1); 0187 % sens=sens+1; % .asc file counts from 0, Matlab counts from 1 0188 % % stuff sensor names into cell array 0189 % sensors{sens}=str; 0190 % % 0191 % % pick up bad flag value 0192 % elseif (strncmp(str,'# bad_flag',10)) 0193 % isub=13:length(str); 0194 % bad_flag=sscanf(str(isub),'%g',1); 0195 % end 0196 % end 0197 % %============================================== 0198 % % 0199 % % Done reading header. Now read the data! 0200 % % It can be read as binary onto matrix data 0201 % %============================================== 0202 % % Begin to assemble the output matrix/structure. 0203 0204 % asc_struct.lat = lat; 0205 % asc_struct.lon = lon; 0206 % asc_struct.gtime = gtime; 0207 % asc_struct.sensors = char(sensors'); 0208 % asc_struct.names = names; 0209 % 0210 asc_struct = hdr_struct; 0211 asc_struct.data = load(asc_file);