


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