0001 function [lat,lon,station,time,depth] = whp_sum(flname)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 fid = fopen(flname,'r');
0030 if fid == -1
0031
0032 fid = fopen([getenv('MATLAB_DATA'),flname],'r');
0033 if fid == -1
0034 fid = fopen([getenv('MATLAB_DATA'),'bottle/woce/',flname],'r');
0035 if fid == -1;
0036 disp(['File ',flname,' not found'])
0037 break
0038 end
0039 end
0040 end
0041
0042
0043 hdr1 = fgetl(fid);
0044 hdr2 = fgetl(fid);
0045 hdr3 = fgetl(fid);
0046 hdr4 = fgetl(fid);
0047
0048
0049 iexpo = findstr(hdr3,'EXPOCODE');
0050 isect = findstr(hdr3,'SECT');
0051 istation = findstr(hdr3,'STNNBR');
0052 if length(istation)==0
0053 istation = findstr(hdr3,'STA');
0054 end
0055
0056 icast = findstr(hdr3,'CASTNO');
0057 itype = findstr(hdr3,'TYPE');
0058 idate = findstr(hdr3,'DATE');
0059 iutc = findstr(hdr3,'TIME');
0060 icode = findstr(hdr3,' CODE')+1;
0061 ilat = findstr(hdr3,'LATITUDE');
0062 ilon = findstr(hdr3,'LONGITUDE');
0063 inav = findstr(hdr3,'NAV');
0064 idepth = findstr(hdr3,'DEPTH');
0065 if length(idepth)==0
0066 idepth = min(findstr(hdr3,'MAX'));
0067 end
0068
0069 i = 0;
0070
0071
0072 for j = 1:1000;
0073 ln = [];ln = fgetl(fid);
0074 if ~isstr(ln) | all(ln == ' ') | length(ln) < 10 break; break; end
0075
0076 if length(ln) < inav;
0077 ln = [ln blanks(inav-length(ln))];
0078 end;
0079
0080 sect(j,:) = ln(isect:isect+4);
0081 station(j) = str2num(ln(isect+5:istation+6));
0082 cast(j) = str2num(ln(istation+7: icast+6));
0083 type(j,:) = ln(itype:itype+3);
0084
0085
0086 month= str2num(ln(idate:idate+1));
0087 day = str2num(ln(idate+2:idate+3));
0088 year = 1900+str2num(ln(idate+4:idate+5));
0089 hour = str2num(ln(iutc:iutc+1));
0090 if hour == []; hour = 0; end
0091 minutes = str2num(ln(iutc+2:iutc+3));
0092 if minutes == []; minutes = 0; end
0093 time(j) = year + cal2dec(month,day,hour,minutes)/365.25;
0094 code(j,1:2) =ln(icode+3:icode+4);
0095 if nargout > 4
0096 depth(j) = str2num(ln(idepth:idepth+4));
0097 end
0098
0099
0100
0101 latstr = ln(ilat:ilon-1);
0102
0103 blnks = find(latstr == ' ');
0104 blnks([0 diff(blnks) == 1]) = []; blnks(blnks == 1) = [];
0105 if length(blnks) < 3
0106 lat(j) = nan;
0107 else
0108 latint = str2num(latstr(1:blnks(1)));
0109 latmin = str2num(latstr(blnks(1):blnks(2)));
0110 hemisphere = latstr(blnks(3)-1);
0111 if strcmp(hemisphere,'N')
0112 lat(j) = latint + latmin/60;
0113 elseif strcmp(hemisphere,'S')
0114 lat(j) = -latint - latmin/60;
0115 else
0116 lat(j) = nan;
0117 end
0118 end
0119
0120 lonstr = ln(ilon:inav-1);
0121 blnks = find(lonstr == ' ');
0122 blnks([0 diff(blnks) == 1]) = [];blnks(blnks == 1) = [];
0123 if length(blnks) < 3
0124 lon(j)= nan;
0125 else
0126 lonint = str2num(lonstr(1:blnks(1)));
0127 lonmin = str2num(lonstr(blnks(1):blnks(2)));
0128 hemisphere = lonstr(blnks(3)-1);
0129 if strcmp(hemisphere,'E')
0130 lon(j) = lonint + lonmin/60;
0131 elseif strcmp(hemisphere,'W')
0132 lon(j) = -lonint - lonmin/60;
0133 else
0134 lon(j)= nan;
0135 end
0136 end
0137 end
0138
0139
0140 ok =0*ones(length(lon),1);
0141 for j = 1:length(lon)
0142 if any(findstr(type(j,:),'RO'))
0143 ok(j) = 1;
0144 end
0145 end
0146
0147 lon = lon(ok); lat = lat(ok); station = station(ok); time = time(ok);
0148
0149 fclose(fid);
0150
0151 if any(diff(station) == 0) ;
0152 newstation = station;
0153 newstation(find(diff(newstation) == 0)) = [];
0154 newlat = nan*newstation; newlon = newlat; newtime = newlat;
0155 for i = 1:length(newstation);
0156 fs = find(newstation(i) == station);
0157
0158
0159
0160 found = 0;
0161 for j = 1:length(fs)
0162 if strcmp(code(fs(j),:),'BO')
0163 found = 1;
0164 newtime(i) = time(fs(j));
0165 newlat(i) = lat(fs(j));
0166 newlon(i) = lon(fs(j));
0167 end
0168 end
0169 if found == 0;
0170 for j = 1:length(fs)
0171 if strcmp(code(fs(j),:),'BE')
0172 found = 1;
0173 newtime(i) = time(fs(j));
0174 newlat(i) = lat(fs(j));
0175 newlon(i) = lon(fs(j));
0176 if nargout > 4
0177 newdepth(i) = depth(fs(j));
0178 end
0179 end
0180 end
0181 end
0182
0183 if isnan(newlat(i)) | isnan(newlon(i));
0184
0185 alllon =lon(fs);
0186 alllat = lat(fs);
0187 newlon(i) = mean(alllon(~isnan(alllon)));
0188 newlat(i) = mean(alllat(~isnan(alllat)));
0189 if nargout > 4
0190 alldepth = depth(fs);
0191 newdepth(i) = max(alldepth(~isnan(alldepth)));
0192 end
0193 end
0194 if isnan(newtime(i))
0195 alltime = time(fs);
0196 newtime(i) = mean(alltime(~isnan(alltime)));
0197 end
0198
0199 end
0200 station = newstation;
0201 lat = newlat;
0202 lon = newlon;
0203 time = newtime;
0204 depth = newdepth;
0205 end
0206
0207
0208
0209
0210
0211
0212
0213